def sendUpdateForInterface(interface): # send update request per interface debug("-sendUpdateForInterface: ", interface) # get IPs for given interface ifaddrs = netifaces.ifaddresses(interface) ipArr = [] if (netifaces.AF_INET in ifaddrs): for ipEntry in ifaddrs[netifaces.AF_INET]: ipArr.append(ipEntry['addr']) if (netifaces.AF_INET6 in ifaddrs): for ipEntry in ifaddrs[netifaces.AF_INET6]: ipArr.append( ipEntry['addr'].split('%')[0]) # fix trailing %<iface> if (len(ipArr) == 0): print("No IPv4 or IPv6 Addresses found for interface: ", interface) return debug("-sendUpdateForInterface: IPs: ", ", ".join(ipArr)) # get HW Addr if ":" in interface: # handle virtual interfaces hwAddr = netifaces.ifaddresses( interface.rsplit(':')[0])[netifaces.AF_LINK][0]['addr'] else: hwAddr = ifaddrs[netifaces.AF_LINK][0]['addr'] debug("-sendUpdateForInterface: HW-Addr: ", hwAddr) # get all available sleep proxies proxy = discoverSleepProxyForInterface(interface) if (proxy == False): print("No sleep proxy available for interface: ", interface) return # get hostname host = socket.gethostname() host_local = host + ".local" debug("-sendUpdateForInterface: Host: " + host_local) # create update request update = dns.update.Update("") ## add some host stuff for currIP in ipArr: ipVersion = dns.inet.af_for_address(currIP) if ipVersion == dns.inet.AF_INET: dnsDatatype = dns.rdatatype.A elif ipVersion == dns.inet.AF_INET6: dnsDatatype = dns.rdatatype.AAAA else: continue update.add(dns.reversename.from_address(currIP), TTL_short, dns.rdatatype.PTR, host_local) update.add(host_local, TTL_short, dnsDatatype, currIP) ## add services for service in discoverServices(ipArr): service_type = service[0].decode() + ".local" service_type_host = host + "." + service_type port = service[1].decode() # add the service if ((len(service) == 2 or service[2] == b"") and not DEBUG): update.add(service_type_host, TTL_long, dns.rdatatype.TXT) else: txtrecord = "" for i in range(2, len(service)): txtrecord += " " + service[i].decode() if (DEBUG): txtrecord += " SPC_STATE=sleeping" update.add(service_type_host, TTL_long, dns.rdatatype.TXT, txtrecord) # device-info service gets a txt record only if (service_type != "device-info._tcp.local"): update.add('_services._dns-sd._udp.local', TTL_long, dns.rdatatype.PTR, service_type) update.add(service_type, TTL_long, dns.rdatatype.PTR, service_type_host) update.add(service_type_host, TTL_short, dns.rdatatype.SRV, "0 0 " + port + " " + host_local) ## add edns options # http://files.dns-sd.org/draft-sekar-dns-ul.txt # 2: Lease Time in seconds leaseTimeOption = dns.edns.GenericOption(2, struct.pack("!L", TTL)) # http://tools.ietf.org/id/draft-cheshire-edns0-owner-option-00.txt # 4: edns owner option (MAC addr for WOL Magic packet) cleanMAC = "0000" + hwAddr.replace(":", "") ownerOption = dns.edns.GenericOption(4, codecs.decode(cleanMAC, 'hex_codec')) update.use_edns(edns=True, ednsflags=TTL_long, options=[leaseTimeOption, ownerOption]) debug("-sendUpdateForInterface: request: ", update) # send request to proxy try: debug("-sendUpdateForInterface: sending update to ", proxy['ip']) response = dns.query.udp(update, proxy['ip'], timeout=10, port=int(proxy['port'])) debug("-sendUpdateForInterface: response: ", response) rcode = response.rcode() if rcode != dns.rcode.NOERROR: print("Unable to register with SleepProxy " + proxy['name'] + " (" + proxy['ip'] + ":" + proxy['port'] + ") - Errcode: " + rcode) print(response) except (DNSException, e): print("Unable to register with SleepProxy " + proxy['name'] + " (" + proxy['ip'] + ":" + proxy['port'] + ")") print(e.__class__, e)
def sendUpdateForInterface(interface) : # send update request per interface if (DEBUG) : print "-sendUpdateForInterface: ", interface # get IPs for given interface ifaddrs = netifaces.ifaddresses(interface) ipArr = [] if (netifaces.AF_INET in ifaddrs) : for ipEntry in ifaddrs[netifaces.AF_INET] : ipArr.append(ipEntry['addr']) if (netifaces.AF_INET6 in ifaddrs) : for ipEntry in ifaddrs[netifaces.AF_INET6] : ipArr.append(ipEntry['addr'].split('%')[0]) # fix trailing %<iface> if (len(ipArr) == 0) : print "No IPv4 or IPv6 Addresses found for interface: ", interface return if (DEBUG) : print "-sendUpdateForInterface: IPs: ", ", ".join(ipArr) # get HW Addr if ":" in interface : # handle virtual interfaces hwAddr = netifaces.ifaddresses(interface.rsplit(':')[0])[netifaces.AF_LINK][0]['addr'] else: hwAddr = ifaddrs[netifaces.AF_LINK][0]['addr'] if (DEBUG) : print "-sendUpdateForInterface: HW-Addr: ", hwAddr # get all available sleep proxies proxy = discoverSleepProxyForInterface(interface) if (proxy == False) : print "No sleep proxy available for interface: ", interface return # get hostname host = socket.gethostname() host_local = host + ".local" if (DEBUG) : print "-sendUpdateForInterface: Host: " + host_local # create update request update = dns.update.Update("") ## add some host stuff for currIP in ipArr : ipVersion = dns.inet.af_for_address(currIP) if ipVersion == dns.inet.AF_INET: dnsDatatype = dns.rdatatype.A elif ipVersion == dns.inet.AF_INET6: dnsDatatype = dns.rdatatype.AAAA else : continue update.add(dns.reversename.from_address(currIP), TTL_short, dns.rdatatype.PTR, host_local) update.add(host_local, TTL_short, dnsDatatype, currIP) ## add services for service in discoverServices(ipArr) : service_type = service[0] + ".local" service_type_host = host + "." + service_type port = service[1] # add the service txtrecord = "" if (len(service) == 2 or service[2] == "") : txtrecord = chr(0) else : for i in range(2,len(service)) : txtrecord += " " + service[i] if (DEBUG) : txtrecord += " SPC_STATE=sleeping" update.add(service_type_host, TTL_long, dns.rdatatype.TXT, txtrecord) # device-info service gets a txt record only if (service_type != "device-info._tcp.local") : update.add('_services._dns-sd._udp.local', TTL_long, dns.rdatatype.PTR, service_type) update.add(service_type, TTL_long, dns.rdatatype.PTR, service_type_host) update.add(service_type_host, TTL_short, dns.rdatatype.SRV, "0 0 " + port + " " + host_local) ## add edns options # http://files.dns-sd.org/draft-sekar-dns-ul.txt # 2: Lease Time in seconds leaseTimeOption = dns.edns.GenericOption(2, struct.pack("!L", TTL)) # http://tools.ietf.org/id/draft-cheshire-edns0-owner-option-00.txt # 4: edns owner option (MAC addr for WOL Magic packet) cleanMAC = hwAddr.replace(":", "") ownerOption = dns.edns.GenericOption(4, ("0000" + cleanMAC).decode('hex_codec')) update.use_edns(edns=True, ednsflags=TTL_long, options=[leaseTimeOption, ownerOption]) if (DEBUG) : print "-sendUpdateForInterface: request: ", update # send request to proxy try: if (DEBUG) : print "-sendUpdateForInterface: sending update to " + proxy['ip'] response = dns.query.udp(update, proxy['ip'], timeout=10, port=int(proxy['port'])) if (DEBUG) : print "-sendUpdateForInterface: response: ", response rcode = response.rcode() if rcode != dns.rcode.NOERROR: print "Unable to register with SleepProxy " + proxy['name'] + " (" + proxy['ip'] + ":" + proxy['port'] + ") - Errcode: " + rcode print response except DNSException, e: print "Unable to register with SleepProxy " + proxy['name'] + " (" + proxy['ip'] + ":" + proxy['port'] + ")" print e.__class__, e