From f0d095d0f1c2767be6da47f885f5ed019e9fa363 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Mon, 8 Jul 2013 23:21:32 -0400 Subject: [PATCH 001/163] Remove the executable bit. --- Browser.py | 0 Zeroconf.py | 0 ZeroconfTest.py | 0 ZeroconfTest2.py | 0 lgpl.txt | 0 5 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 Browser.py mode change 100755 => 100644 Zeroconf.py mode change 100755 => 100644 ZeroconfTest.py mode change 100755 => 100644 ZeroconfTest2.py mode change 100755 => 100644 lgpl.txt diff --git a/Browser.py b/Browser.py old mode 100755 new mode 100644 diff --git a/Zeroconf.py b/Zeroconf.py old mode 100755 new mode 100644 diff --git a/ZeroconfTest.py b/ZeroconfTest.py old mode 100755 new mode 100644 diff --git a/ZeroconfTest2.py b/ZeroconfTest2.py old mode 100755 new mode 100644 diff --git a/lgpl.txt b/lgpl.txt old mode 100755 new mode 100644 From d952a9c117ae539cf4778d76618fe813b10a9a34 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 9 Jul 2013 00:19:56 -0400 Subject: [PATCH 002/163] The initial version I committed to HME for Python back in 2008. This is a step back in some respects (re-inserting tabs that will be undone a couple patches hence), so that I can apply the patches going forward. --- Zeroconf.py | 2746 ++++++++++++++++++++++++++------------------------- 1 file changed, 1380 insertions(+), 1366 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 8efaf033..6378164d 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -19,33 +19,33 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - + """ """0.12 update - allow selection of binding interface - typo fix - Thanks A. M. Kuchlingi - removed all use of word 'Rendezvous' - this is an API change""" + typo fix - Thanks A. M. Kuchlingi + removed all use of word 'Rendezvous' - this is an API change""" """0.11 update - correction to comments for addListener method support for new record types seen from OS X - - IPv6 address - - hostinfo - ignore unknown DNS record types - fixes to name decoding - works alongside other processes using port 5353 (e.g. on Mac OS X) - tested against Mac OS X 10.3.2's mDNSResponder - corrections to removal of list entries for service browser""" + - IPv6 address + - hostinfo + ignore unknown DNS record types + fixes to name decoding + works alongside other processes using port 5353 (e.g. on Mac OS X) + tested against Mac OS X 10.3.2's mDNSResponder + corrections to removal of list entries for service browser""" """0.10 update - Jonathon Paisley contributed these corrections: always multicast replies, even when query is unicast - correct a pointer encoding problem - can now write records in any order - traceback shown on failure - better TXT record parsing - server is now separate from name - can cancel a service browser + correct a pointer encoding problem + can now write records in any order + traceback shown on failure + better TXT record parsing + server is now separate from name + can cancel a service browser - modified some unit tests to accommodate these changes""" + modified some unit tests to accommodate these changes""" """0.09 update - remove all records on service unregistration fix DOS security problem with readName""" @@ -54,25 +54,25 @@ """0.07 update - faster shutdown on engine pointer encoding of outgoing names - ServiceBrowser now works - new unit tests""" + ServiceBrowser now works + new unit tests""" """0.06 update - small improvements with unit tests added defined exception types - new style objects - fixed hostname/interface problem - fixed socket timeout problem - fixed addServiceListener() typo bug - using select() for socket reads - tested on Debian unstable with Python 2.2.2""" + new style objects + fixed hostname/interface problem + fixed socket timeout problem + fixed addServiceListener() typo bug + using select() for socket reads + tested on Debian unstable with Python 2.2.2""" """0.05 update - ensure case insensitivty on domain names support for unicast DNS queries""" """0.04 update - added some unit tests added __ne__ adjuncts where required - ensure names end in '.local.' - timeout on receiving socket for clean shutdown""" + ensure names end in '.local.' + timeout on receiving socket for clean shutdown""" __author__ = "Paul Scott-Murphy" __email__ = "paul at scott dash murphy dot com" @@ -101,7 +101,7 @@ _BROWSER_TIME = 500 # Some DNS constants - + _MDNS_ADDR = '224.0.0.251' _MDNS_PORT = 5353; _DNS_PORT = 53; @@ -155,1403 +155,1417 @@ # Mapping constants to names _CLASSES = { _CLASS_IN : "in", - _CLASS_CS : "cs", - _CLASS_CH : "ch", - _CLASS_HS : "hs", - _CLASS_NONE : "none", - _CLASS_ANY : "any" } + _CLASS_CS : "cs", + _CLASS_CH : "ch", + _CLASS_HS : "hs", + _CLASS_NONE : "none", + _CLASS_ANY : "any" } _TYPES = { _TYPE_A : "a", - _TYPE_NS : "ns", - _TYPE_MD : "md", - _TYPE_MF : "mf", - _TYPE_CNAME : "cname", - _TYPE_SOA : "soa", - _TYPE_MB : "mb", - _TYPE_MG : "mg", - _TYPE_MR : "mr", - _TYPE_NULL : "null", - _TYPE_WKS : "wks", - _TYPE_PTR : "ptr", - _TYPE_HINFO : "hinfo", - _TYPE_MINFO : "minfo", - _TYPE_MX : "mx", - _TYPE_TXT : "txt", - _TYPE_AAAA : "quada", - _TYPE_SRV : "srv", - _TYPE_ANY : "any" } + _TYPE_NS : "ns", + _TYPE_MD : "md", + _TYPE_MF : "mf", + _TYPE_CNAME : "cname", + _TYPE_SOA : "soa", + _TYPE_MB : "mb", + _TYPE_MG : "mg", + _TYPE_MR : "mr", + _TYPE_NULL : "null", + _TYPE_WKS : "wks", + _TYPE_PTR : "ptr", + _TYPE_HINFO : "hinfo", + _TYPE_MINFO : "minfo", + _TYPE_MX : "mx", + _TYPE_TXT : "txt", + _TYPE_AAAA : "quada", + _TYPE_SRV : "srv", + _TYPE_ANY : "any" } # utility functions def currentTimeMillis(): - """Current system time in milliseconds""" - return time.time() * 1000 + """Current system time in milliseconds""" + return time.time() * 1000 # Exceptions class NonLocalNameException(Exception): - pass + pass class NonUniqueNameException(Exception): - pass + pass class NamePartTooLongException(Exception): - pass + pass class AbstractMethodException(Exception): - pass + pass class BadTypeInNameException(Exception): - pass + pass # implementation classes class DNSEntry(object): - """A DNS entry""" - - def __init__(self, name, type, clazz): - self.key = string.lower(name) - self.name = name - self.type = type - self.clazz = clazz & _CLASS_MASK - self.unique = (clazz & _CLASS_UNIQUE) != 0 - - def __eq__(self, other): - """Equality test on name, type, and class""" - if isinstance(other, DNSEntry): - return self.name == other.name and self.type == other.type and self.clazz == other.clazz - return 0 - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def getClazz(self, clazz): - """Class accessor""" - try: - return _CLASSES[clazz] - except: - return "?(%s)" % (clazz) - - def getType(self, type): - """Type accessor""" - try: - return _TYPES[type] - except: - return "?(%s)" % (type) - - def toString(self, hdr, other): - """String representation with additional information""" - result = "%s[%s,%s" % (hdr, self.getType(self.type), self.getClazz(self.clazz)) - if self.unique: - result += "-unique," - else: - result += "," - result += self.name - if other is not None: - result += ",%s]" % (other) - else: - result += "]" - return result + """A DNS entry""" + + def __init__(self, name, type, clazz): + self.key = string.lower(name) + self.name = name + self.type = type + self.clazz = clazz & _CLASS_MASK + self.unique = (clazz & _CLASS_UNIQUE) != 0 + + def __eq__(self, other): + """Equality test on name, type, and class""" + if isinstance(other, DNSEntry): + return self.name == other.name and self.type == other.type and self.clazz == other.clazz + return 0 + + def __ne__(self, other): + """Non-equality test""" + return not self.__eq__(other) + + def getClazz(self, clazz): + """Class accessor""" + try: + return _CLASSES[clazz] + except: + return "?(%s)" % (clazz) + + def getType(self, type): + """Type accessor""" + try: + return _TYPES[type] + except: + return "?(%s)" % (type) + + def toString(self, hdr, other): + """String representation with additional information""" + result = "%s[%s,%s" % (hdr, self.getType(self.type), self.getClazz(self.clazz)) + if self.unique: + result += "-unique," + else: + result += "," + result += self.name + if other is not None: + result += ",%s]" % (other) + else: + result += "]" + return result class DNSQuestion(DNSEntry): - """A DNS question entry""" - - def __init__(self, name, type, clazz): - if not name.endswith(".local."): - raise NonLocalNameException - DNSEntry.__init__(self, name, type, clazz) + """A DNS question entry""" + + def __init__(self, name, type, clazz): + if not name.endswith(".local."): + raise NonLocalNameException + DNSEntry.__init__(self, name, type, clazz) - def answeredBy(self, rec): - """Returns true if the question is answered by the record""" - return self.clazz == rec.clazz and (self.type == rec.type or self.type == _TYPE_ANY) and self.name == rec.name + def answeredBy(self, rec): + """Returns true if the question is answered by the record""" + return self.clazz == rec.clazz and (self.type == rec.type or self.type == _TYPE_ANY) and self.name == rec.name - def __repr__(self): - """String representation""" - return DNSEntry.toString(self, "question", None) + def __repr__(self): + """String representation""" + return DNSEntry.toString(self, "question", None) class DNSRecord(DNSEntry): - """A DNS record - like a DNS entry, but has a TTL""" - - def __init__(self, name, type, clazz, ttl): - DNSEntry.__init__(self, name, type, clazz) - self.ttl = ttl - self.created = currentTimeMillis() - - def __eq__(self, other): - """Tests equality as per DNSRecord""" - if isinstance(other, DNSRecord): - return DNSEntry.__eq__(self, other) - return 0 - - def suppressedBy(self, msg): - """Returns true if any answer in a message can suffice for the - information held in this record.""" - for record in msg.answers: - if self.suppressedByAnswer(record): - return 1 - return 0 - - def suppressedByAnswer(self, other): - """Returns true if another record has same name, type and class, - and if its TTL is at least half of this record's.""" - if self == other and other.ttl > (self.ttl / 2): - return 1 - return 0 - - def getExpirationTime(self, percent): - """Returns the time at which this record will have expired - by a certain percentage.""" - return self.created + (percent * self.ttl * 10) - - def getRemainingTTL(self, now): - """Returns the remaining TTL in seconds.""" - return max(0, (self.getExpirationTime(100) - now) / 1000) - - def isExpired(self, now): - """Returns true if this record has expired.""" - return self.getExpirationTime(100) <= now - - def isStale(self, now): - """Returns true if this record is at least half way expired.""" - return self.getExpirationTime(50) <= now - - def resetTTL(self, other): - """Sets this record's TTL and created time to that of - another record.""" - self.created = other.created - self.ttl = other.ttl - - def write(self, out): - """Abstract method""" - raise AbstractMethodException - - def toString(self, other): - """String representation with addtional information""" - arg = "%s/%s,%s" % (self.ttl, self.getRemainingTTL(currentTimeMillis()), other) - return DNSEntry.toString(self, "record", arg) + """A DNS record - like a DNS entry, but has a TTL""" + + def __init__(self, name, type, clazz, ttl): + DNSEntry.__init__(self, name, type, clazz) + self.ttl = ttl + self.created = currentTimeMillis() + + def __eq__(self, other): + """Tests equality as per DNSRecord""" + if isinstance(other, DNSRecord): + return DNSEntry.__eq__(self, other) + return 0 + + def suppressedBy(self, msg): + """Returns true if any answer in a message can suffice for the + information held in this record.""" + for record in msg.answers: + if self.suppressedByAnswer(record): + return 1 + return 0 + + def suppressedByAnswer(self, other): + """Returns true if another record has same name, type and class, + and if its TTL is at least half of this record's.""" + if self == other and other.ttl > (self.ttl / 2): + return 1 + return 0 + + def getExpirationTime(self, percent): + """Returns the time at which this record will have expired + by a certain percentage.""" + return self.created + (percent * self.ttl * 10) + + def getRemainingTTL(self, now): + """Returns the remaining TTL in seconds.""" + return max(0, (self.getExpirationTime(100) - now) / 1000) + + def isExpired(self, now): + """Returns true if this record has expired.""" + return self.getExpirationTime(100) <= now + + def isStale(self, now): + """Returns true if this record is at least half way expired.""" + return self.getExpirationTime(50) <= now + + def resetTTL(self, other): + """Sets this record's TTL and created time to that of + another record.""" + self.created = other.created + self.ttl = other.ttl + + def write(self, out): + """Abstract method""" + raise AbstractMethodException + + def toString(self, other): + """String representation with addtional information""" + arg = "%s/%s,%s" % (self.ttl, self.getRemainingTTL(currentTimeMillis()), other) + return DNSEntry.toString(self, "record", arg) class DNSAddress(DNSRecord): - """A DNS address record""" - - def __init__(self, name, type, clazz, ttl, address): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.address = address - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeString(self.address, len(self.address)) - - def __eq__(self, other): - """Tests equality on address""" - if isinstance(other, DNSAddress): - return self.address == other.address - return 0 - - def __repr__(self): - """String representation""" - try: - return socket.inet_ntoa(self.address) - except: - return self.address + """A DNS address record""" + + def __init__(self, name, type, clazz, ttl, address): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.address = address + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeString(self.address, len(self.address)) + + def __eq__(self, other): + """Tests equality on address""" + if isinstance(other, DNSAddress): + return self.address == other.address + return 0 + + def __repr__(self): + """String representation""" + try: + return socket.inet_ntoa(self.address) + except: + return self.address class DNSHinfo(DNSRecord): - """A DNS host information record""" - - def __init__(self, name, type, clazz, ttl, cpu, os): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.cpu = cpu - self.os = os - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeString(self.cpu, len(self.cpu)) - out.writeString(self.os, len(self.os)) - - def __eq__(self, other): - """Tests equality on cpu and os""" - if isinstance(other, DNSHinfo): - return self.cpu == other.cpu and self.os == other.os - return 0 - - def __repr__(self): - """String representation""" - return self.cpu + " " + self.os - + """A DNS host information record""" + + def __init__(self, name, type, clazz, ttl, cpu, os): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.cpu = cpu + self.os = os + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeString(self.cpu, len(self.cpu)) + out.writeString(self.os, len(self.os)) + + def __eq__(self, other): + """Tests equality on cpu and os""" + if isinstance(other, DNSHinfo): + return self.cpu == other.cpu and self.os == other.os + return 0 + + def __repr__(self): + """String representation""" + return self.cpu + " " + self.os + class DNSPointer(DNSRecord): - """A DNS pointer record""" - - def __init__(self, name, type, clazz, ttl, alias): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.alias = alias - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeName(self.alias) - - def __eq__(self, other): - """Tests equality on alias""" - if isinstance(other, DNSPointer): - return self.alias == other.alias - return 0 - - def __repr__(self): - """String representation""" - return self.toString(self.alias) + """A DNS pointer record""" + + def __init__(self, name, type, clazz, ttl, alias): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.alias = alias + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeName(self.alias) + + def __eq__(self, other): + """Tests equality on alias""" + if isinstance(other, DNSPointer): + return self.alias == other.alias + return 0 + + def __repr__(self): + """String representation""" + return self.toString(self.alias) class DNSText(DNSRecord): - """A DNS text record""" - - def __init__(self, name, type, clazz, ttl, text): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.text = text - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeString(self.text, len(self.text)) - - def __eq__(self, other): - """Tests equality on text""" - if isinstance(other, DNSText): - return self.text == other.text - return 0 - - def __repr__(self): - """String representation""" - if len(self.text) > 10: - return self.toString(self.text[:7] + "...") - else: - return self.toString(self.text) + """A DNS text record""" + + def __init__(self, name, type, clazz, ttl, text): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.text = text + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeString(self.text, len(self.text)) + + def __eq__(self, other): + """Tests equality on text""" + if isinstance(other, DNSText): + return self.text == other.text + return 0 + + def __repr__(self): + """String representation""" + if len(self.text) > 10: + return self.toString(self.text[:7] + "...") + else: + return self.toString(self.text) class DNSService(DNSRecord): - """A DNS service record""" - - def __init__(self, name, type, clazz, ttl, priority, weight, port, server): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.priority = priority - self.weight = weight - self.port = port - self.server = server - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeShort(self.priority) - out.writeShort(self.weight) - out.writeShort(self.port) - out.writeName(self.server) - - def __eq__(self, other): - """Tests equality on priority, weight, port and server""" - if isinstance(other, DNSService): - return self.priority == other.priority and self.weight == other.weight and self.port == other.port and self.server == other.server - return 0 - - def __repr__(self): - """String representation""" - return self.toString("%s:%s" % (self.server, self.port)) + """A DNS service record""" + + def __init__(self, name, type, clazz, ttl, priority, weight, port, server): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.priority = priority + self.weight = weight + self.port = port + self.server = server + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeShort(self.priority) + out.writeShort(self.weight) + out.writeShort(self.port) + out.writeName(self.server) + + def __eq__(self, other): + """Tests equality on priority, weight, port and server""" + if isinstance(other, DNSService): + return self.priority == other.priority and self.weight == other.weight and self.port == other.port and self.server == other.server + return 0 + + def __repr__(self): + """String representation""" + return self.toString("%s:%s" % (self.server, self.port)) class DNSIncoming(object): - """Object representation of an incoming DNS packet""" - - def __init__(self, data): - """Constructor from string holding bytes of packet""" - self.offset = 0 - self.data = data - self.questions = [] - self.answers = [] - self.numQuestions = 0 - self.numAnswers = 0 - self.numAuthorities = 0 - self.numAdditionals = 0 - - self.readHeader() - self.readQuestions() - self.readOthers() - - def readHeader(self): - """Reads header portion of packet""" - format = '!HHHHHH' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - - self.id = info[0] - self.flags = info[1] - self.numQuestions = info[2] - self.numAnswers = info[3] - self.numAuthorities = info[4] - self.numAdditionals = info[5] - - def readQuestions(self): - """Reads questions section of packet""" - format = '!HH' - length = struct.calcsize(format) - for i in range(0, self.numQuestions): - name = self.readName() - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - - question = DNSQuestion(name, info[0], info[1]) - self.questions.append(question) - - def readInt(self): - """Reads an integer from the packet""" - format = '!I' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - return info[0] - - def readCharacterString(self): - """Reads a character string from the packet""" - length = ord(self.data[self.offset]) - self.offset += 1 - return self.readString(length) - - def readString(self, len): - """Reads a string of a given length from the packet""" - format = '!' + str(len) + 's' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - return info[0] - - def readUnsignedShort(self): - """Reads an unsigned short from the packet""" - format = '!H' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - return info[0] - - def readOthers(self): - """Reads the answers, authorities and additionals section of the packet""" - format = '!HHiH' - length = struct.calcsize(format) - n = self.numAnswers + self.numAuthorities + self.numAdditionals - for i in range(0, n): - domain = self.readName() - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - - rec = None - if info[0] == _TYPE_A: - rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(4)) - elif info[0] == _TYPE_CNAME or info[0] == _TYPE_PTR: - rec = DNSPointer(domain, info[0], info[1], info[2], self.readName()) - elif info[0] == _TYPE_TXT: - rec = DNSText(domain, info[0], info[1], info[2], self.readString(info[3])) - elif info[0] == _TYPE_SRV: - rec = DNSService(domain, info[0], info[1], info[2], self.readUnsignedShort(), self.readUnsignedShort(), self.readUnsignedShort(), self.readName()) - elif info[0] == _TYPE_HINFO: - rec = DNSHinfo(domain, info[0], info[1], info[2], self.readCharacterString(), self.readCharacterString()) - elif info[0] == _TYPE_AAAA: - rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16)) - else: - # Try to ignore types we don't know about - # this may mean the rest of the name is - # unable to be parsed, and may show errors - # so this is left for debugging. New types - # encountered need to be parsed properly. - # - #print "UNKNOWN TYPE = " + str(info[0]) - #raise BadTypeInNameException - pass - - if rec is not None: - self.answers.append(rec) - - def isQuery(self): - """Returns true if this is a query""" - return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY - - def isResponse(self): - """Returns true if this is a response""" - return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE - - def readUTF(self, offset, len): - """Reads a UTF-8 string of a given length from the packet""" - result = self.data[offset:offset+len].decode('utf-8') - return result - - def readName(self): - """Reads a domain name from the packet""" - result = '' - off = self.offset - next = -1 - first = off - - while 1: - len = ord(self.data[off]) - off += 1 - if len == 0: - break - t = len & 0xC0 - if t == 0x00: - result = ''.join((result, self.readUTF(off, len) + '.')) - off += len - elif t == 0xC0: - if next < 0: - next = off + 1 - off = ((len & 0x3F) << 8) | ord(self.data[off]) - if off >= first: - raise "Bad domain name (circular) at " + str(off) - first = off - else: - raise "Bad domain name at " + str(off) - - if next >= 0: - self.offset = next - else: - self.offset = off - - return result - - + """Object representation of an incoming DNS packet""" + + def __init__(self, data): + """Constructor from string holding bytes of packet""" + self.offset = 0 + self.data = data + self.questions = [] + self.answers = [] + self.numQuestions = 0 + self.numAnswers = 0 + self.numAuthorities = 0 + self.numAdditionals = 0 + + self.readHeader() + self.readQuestions() + self.readOthers() + + def readHeader(self): + """Reads header portion of packet""" + format = '!HHHHHH' + length = struct.calcsize(format) + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + + self.id = info[0] + self.flags = info[1] + self.numQuestions = info[2] + self.numAnswers = info[3] + self.numAuthorities = info[4] + self.numAdditionals = info[5] + + def readQuestions(self): + """Reads questions section of packet""" + format = '!HH' + length = struct.calcsize(format) + for i in range(0, self.numQuestions): + name = self.readName() + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + + question = DNSQuestion(name, info[0], info[1]) + self.questions.append(question) + + def readInt(self): + """Reads an integer from the packet""" + format = '!I' + length = struct.calcsize(format) + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + return info[0] + + def readCharacterString(self): + """Reads a character string from the packet""" + length = ord(self.data[self.offset]) + self.offset += 1 + return self.readString(length) + + def readString(self, len): + """Reads a string of a given length from the packet""" + format = '!' + str(len) + 's' + length = struct.calcsize(format) + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + return info[0] + + def readUnsignedShort(self): + """Reads an unsigned short from the packet""" + format = '!H' + length = struct.calcsize(format) + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + return info[0] + + def readOthers(self): + """Reads the answers, authorities and additionals section of the packet""" + format = '!HHiH' + length = struct.calcsize(format) + n = self.numAnswers + self.numAuthorities + self.numAdditionals + for i in range(0, n): + domain = self.readName() + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + + rec = None + if info[0] == _TYPE_A: + rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(4)) + elif info[0] == _TYPE_CNAME or info[0] == _TYPE_PTR: + rec = DNSPointer(domain, info[0], info[1], info[2], self.readName()) + elif info[0] == _TYPE_TXT: + rec = DNSText(domain, info[0], info[1], info[2], self.readString(info[3])) + elif info[0] == _TYPE_SRV: + rec = DNSService(domain, info[0], info[1], info[2], self.readUnsignedShort(), self.readUnsignedShort(), self.readUnsignedShort(), self.readName()) + elif info[0] == _TYPE_HINFO: + rec = DNSHinfo(domain, info[0], info[1], info[2], self.readCharacterString(), self.readCharacterString()) + elif info[0] == _TYPE_AAAA: + rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16)) + else: + # Try to ignore types we don't know about + # this may mean the rest of the name is + # unable to be parsed, and may show errors + # so this is left for debugging. New types + # encountered need to be parsed properly. + # + #print "UNKNOWN TYPE = " + str(info[0]) + #raise BadTypeInNameException + pass + + if rec is not None: + self.answers.append(rec) + + def isQuery(self): + """Returns true if this is a query""" + return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY + + def isResponse(self): + """Returns true if this is a response""" + return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE + + def readUTF(self, offset, len): + """Reads a UTF-8 string of a given length from the packet""" + result = self.data[offset:offset+len].decode('utf-8') + return result + + def readName(self): + """Reads a domain name from the packet""" + result = '' + off = self.offset + next = -1 + first = off + + while 1: + len = ord(self.data[off]) + off += 1 + if len == 0: + break + t = len & 0xC0 + if t == 0x00: + result = ''.join((result, self.readUTF(off, len) + '.')) + off += len + elif t == 0xC0: + if next < 0: + next = off + 1 + off = ((len & 0x3F) << 8) | ord(self.data[off]) + if off >= first: + raise "Bad domain name (circular) at " + str(off) + first = off + else: + raise "Bad domain name at " + str(off) + + if next >= 0: + self.offset = next + else: + self.offset = off + + return result + + class DNSOutgoing(object): - """Object representation of an outgoing packet""" - - def __init__(self, flags, multicast = 1): - self.finished = 0 - self.id = 0 - self.multicast = multicast - self.flags = flags - self.names = {} - self.data = [] - self.size = 12 - - self.questions = [] - self.answers = [] - self.authorities = [] - self.additionals = [] - - def addQuestion(self, record): - """Adds a question""" - self.questions.append(record) - - def addAnswer(self, inp, record): - """Adds an answer""" - if not record.suppressedBy(inp): - self.addAnswerAtTime(record, 0) - - def addAnswerAtTime(self, record, now): - """Adds an answer if if does not expire by a certain time""" - if record is not None: - if now == 0 or not record.isExpired(now): - self.answers.append((record, now)) - - def addAuthorativeAnswer(self, record): - """Adds an authoritative answer""" - self.authorities.append(record) - - def addAdditionalAnswer(self, record): - """Adds an additional answer""" - self.additionals.append(record) - - def writeByte(self, value): - """Writes a single byte to the packet""" - format = '!c' - self.data.append(struct.pack(format, chr(value))) - self.size += 1 - - def insertShort(self, index, value): - """Inserts an unsigned short in a certain position in the packet""" - format = '!H' - self.data.insert(index, struct.pack(format, value)) - self.size += 2 - - def writeShort(self, value): - """Writes an unsigned short to the packet""" - format = '!H' - self.data.append(struct.pack(format, value)) - self.size += 2 - - def writeInt(self, value): - """Writes an unsigned integer to the packet""" - format = '!I' - self.data.append(struct.pack(format, value)) - self.size += 4 - - def writeString(self, value, length): - """Writes a string to the packet""" - format = '!' + str(length) + 's' - self.data.append(struct.pack(format, value)) - self.size += length - - def writeUTF(self, s): - """Writes a UTF-8 string of a given length to the packet""" - utfstr = s.encode('utf-8') - length = len(utfstr) - if length > 64: - raise NamePartTooLongException - self.writeByte(length) - self.writeString(utfstr, length) - - def writeName(self, name): - """Writes a domain name to the packet""" - - try: - # Find existing instance of this name in packet - # - index = self.names[name] - except KeyError: - # No record of this name already, so write it - # out as normal, recording the location of the name - # for future pointers to it. - # - self.names[name] = self.size - parts = name.split('.') - if parts[-1] == '': - parts = parts[:-1] - for part in parts: - self.writeUTF(part) - self.writeByte(0) - return - - # An index was found, so write a pointer to it - # - self.writeByte((index >> 8) | 0xC0) - self.writeByte(index) - - def writeQuestion(self, question): - """Writes a question to the packet""" - self.writeName(question.name) - self.writeShort(question.type) - self.writeShort(question.clazz) - - def writeRecord(self, record, now): - """Writes a record (answer, authoritative answer, additional) to - the packet""" - self.writeName(record.name) - self.writeShort(record.type) - if record.unique and self.multicast: - self.writeShort(record.clazz | _CLASS_UNIQUE) - else: - self.writeShort(record.clazz) - if now == 0: - self.writeInt(record.ttl) - else: - self.writeInt(record.getRemainingTTL(now)) - index = len(self.data) - # Adjust size for the short we will write before this record - # - self.size += 2 - record.write(self) - self.size -= 2 - - length = len(''.join(self.data[index:])) - self.insertShort(index, length) # Here is the short we adjusted for - - def packet(self): - """Returns a string containing the packet's bytes - - No further parts should be added to the packet once this - is done.""" - if not self.finished: - self.finished = 1 - for question in self.questions: - self.writeQuestion(question) - for answer, time in self.answers: - self.writeRecord(answer, time) - for authority in self.authorities: - self.writeRecord(authority, 0) - for additional in self.additionals: - self.writeRecord(additional, 0) - - self.insertShort(0, len(self.additionals)) - self.insertShort(0, len(self.authorities)) - self.insertShort(0, len(self.answers)) - self.insertShort(0, len(self.questions)) - self.insertShort(0, self.flags) - if self.multicast: - self.insertShort(0, 0) - else: - self.insertShort(0, self.id) - return ''.join(self.data) + """Object representation of an outgoing packet""" + + def __init__(self, flags, multicast = 1): + self.finished = 0 + self.id = 0 + self.multicast = multicast + self.flags = flags + self.names = {} + self.data = [] + self.size = 12 + + self.questions = [] + self.answers = [] + self.authorities = [] + self.additionals = [] + + def addQuestion(self, record): + """Adds a question""" + self.questions.append(record) + + def addAnswer(self, inp, record): + """Adds an answer""" + if not record.suppressedBy(inp): + self.addAnswerAtTime(record, 0) + + def addAnswerAtTime(self, record, now): + """Adds an answer if if does not expire by a certain time""" + if record is not None: + if now == 0 or not record.isExpired(now): + self.answers.append((record, now)) + + def addAuthorativeAnswer(self, record): + """Adds an authoritative answer""" + self.authorities.append(record) + + def addAdditionalAnswer(self, record): + """Adds an additional answer""" + self.additionals.append(record) + + def writeByte(self, value): + """Writes a single byte to the packet""" + format = '!c' + self.data.append(struct.pack(format, chr(value))) + self.size += 1 + + def insertShort(self, index, value): + """Inserts an unsigned short in a certain position in the packet""" + format = '!H' + self.data.insert(index, struct.pack(format, value)) + self.size += 2 + + def writeShort(self, value): + """Writes an unsigned short to the packet""" + format = '!H' + self.data.append(struct.pack(format, value)) + self.size += 2 + + def writeInt(self, value): + """Writes an unsigned integer to the packet""" + format = '!I' + self.data.append(struct.pack(format, int(value))) + self.size += 4 + + def writeString(self, value, length): + """Writes a string to the packet""" + format = '!' + str(length) + 's' + self.data.append(struct.pack(format, value)) + self.size += length + + def writeUTF(self, s): + """Writes a UTF-8 string of a given length to the packet""" + utfstr = s.encode('utf-8') + length = len(utfstr) + if length > 64: + raise NamePartTooLongException + self.writeByte(length) + self.writeString(utfstr, length) + + def writeName(self, name): + """Writes a domain name to the packet""" + + try: + # Find existing instance of this name in packet + # + index = self.names[name] + except KeyError: + # No record of this name already, so write it + # out as normal, recording the location of the name + # for future pointers to it. + # + self.names[name] = self.size + parts = name.split('.') + if parts[-1] == '': + parts = parts[:-1] + for part in parts: + self.writeUTF(part) + self.writeByte(0) + return + + # An index was found, so write a pointer to it + # + self.writeByte((index >> 8) | 0xC0) + self.writeByte(index) + + def writeQuestion(self, question): + """Writes a question to the packet""" + self.writeName(question.name) + self.writeShort(question.type) + self.writeShort(question.clazz) + + def writeRecord(self, record, now): + """Writes a record (answer, authoritative answer, additional) to + the packet""" + self.writeName(record.name) + self.writeShort(record.type) + if record.unique and self.multicast: + self.writeShort(record.clazz | _CLASS_UNIQUE) + else: + self.writeShort(record.clazz) + if now == 0: + self.writeInt(record.ttl) + else: + self.writeInt(record.getRemainingTTL(now)) + index = len(self.data) + # Adjust size for the short we will write before this record + # + self.size += 2 + record.write(self) + self.size -= 2 + + length = len(''.join(self.data[index:])) + self.insertShort(index, length) # Here is the short we adjusted for + + def packet(self): + """Returns a string containing the packet's bytes + + No further parts should be added to the packet once this + is done.""" + if not self.finished: + self.finished = 1 + for question in self.questions: + self.writeQuestion(question) + for answer, time in self.answers: + self.writeRecord(answer, time) + for authority in self.authorities: + self.writeRecord(authority, 0) + for additional in self.additionals: + self.writeRecord(additional, 0) + + self.insertShort(0, len(self.additionals)) + self.insertShort(0, len(self.authorities)) + self.insertShort(0, len(self.answers)) + self.insertShort(0, len(self.questions)) + self.insertShort(0, self.flags) + if self.multicast: + self.insertShort(0, 0) + else: + self.insertShort(0, self.id) + return ''.join(self.data) class DNSCache(object): - """A cache of DNS entries""" - - def __init__(self): - self.cache = {} - - def add(self, entry): - """Adds an entry""" - try: - list = self.cache[entry.key] - except: - list = self.cache[entry.key] = [] - list.append(entry) - - def remove(self, entry): - """Removes an entry""" - try: - list = self.cache[entry.key] - list.remove(entry) - except: - pass - - def get(self, entry): - """Gets an entry by key. Will return None if there is no - matching entry.""" - try: - list = self.cache[entry.key] - return list[list.index(entry)] - except: - return None - - def getByDetails(self, name, type, clazz): - """Gets an entry by details. Will return None if there is - no matching entry.""" - entry = DNSEntry(name, type, clazz) - return self.get(entry) - - def entriesWithName(self, name): - """Returns a list of entries whose key matches the name.""" - try: - return self.cache[name] - except: - return [] - - def entries(self): - """Returns a list of all entries""" - def add(x, y): return x+y - try: - return reduce(add, self.cache.values()) - except: - return [] + """A cache of DNS entries""" + + def __init__(self): + self.cache = {} + + def add(self, entry): + """Adds an entry""" + try: + list = self.cache[entry.key] + except: + list = self.cache[entry.key] = [] + list.append(entry) + + def remove(self, entry): + """Removes an entry""" + try: + list = self.cache[entry.key] + list.remove(entry) + except: + pass + + def get(self, entry): + """Gets an entry by key. Will return None if there is no + matching entry.""" + try: + list = self.cache[entry.key] + return list[list.index(entry)] + except: + return None + + def getByDetails(self, name, type, clazz): + """Gets an entry by details. Will return None if there is + no matching entry.""" + entry = DNSEntry(name, type, clazz) + return self.get(entry) + + def entriesWithName(self, name): + """Returns a list of entries whose key matches the name.""" + try: + return self.cache[name] + except: + return [] + + def entries(self): + """Returns a list of all entries""" + def add(x, y): return x+y + try: + return reduce(add, self.cache.values()) + except: + return [] class Engine(threading.Thread): - """An engine wraps read access to sockets, allowing objects that - need to receive data from sockets to be called back when the - sockets are ready. - - A reader needs a handle_read() method, which is called when the socket - it is interested in is ready for reading. - - Writers are not implemented here, because we only send short - packets. - """ - - def __init__(self, zeroconf): - threading.Thread.__init__(self) - self.zeroconf = zeroconf - self.readers = {} # maps socket to reader - self.timeout = 5 - self.condition = threading.Condition() - self.start() - - def run(self): - while not globals()['_GLOBAL_DONE']: - rs = self.getReaders() - if len(rs) == 0: - # No sockets to manage, but we wait for the timeout - # or addition of a socket - # - self.condition.acquire() - self.condition.wait(self.timeout) - self.condition.release() - else: - try: - rr, wr, er = select.select(rs, [], [], self.timeout) - for socket in rr: - try: - self.readers[socket].handle_read() - except: - # Ignore errors that occur on shutdown - pass - except: - pass - - def getReaders(self): - result = [] - self.condition.acquire() - result = self.readers.keys() - self.condition.release() - return result - - def addReader(self, reader, socket): - self.condition.acquire() - self.readers[socket] = reader - self.condition.notify() - self.condition.release() - - def delReader(self, socket): - self.condition.acquire() - del(self.readers[socket]) - self.condition.notify() - self.condition.release() - - def notify(self): - self.condition.acquire() - self.condition.notify() - self.condition.release() + """An engine wraps read access to sockets, allowing objects that + need to receive data from sockets to be called back when the + sockets are ready. + + A reader needs a handle_read() method, which is called when the socket + it is interested in is ready for reading. + + Writers are not implemented here, because we only send short + packets. + """ + + def __init__(self, zeroconf): + threading.Thread.__init__(self) + self.zeroconf = zeroconf + self.readers = {} # maps socket to reader + self.timeout = 5 + self.condition = threading.Condition() + self.start() + + def run(self): + while not globals()['_GLOBAL_DONE']: + rs = self.getReaders() + if len(rs) == 0: + # No sockets to manage, but we wait for the timeout + # or addition of a socket + # + self.condition.acquire() + self.condition.wait(self.timeout) + self.condition.release() + else: + try: + rr, wr, er = select.select(rs, [], [], self.timeout) + for socket in rr: + try: + self.readers[socket].handle_read() + except: + traceback.print_exc() + except: + pass + + def getReaders(self): + result = [] + self.condition.acquire() + result = self.readers.keys() + self.condition.release() + return result + + def addReader(self, reader, socket): + self.condition.acquire() + self.readers[socket] = reader + self.condition.notify() + self.condition.release() + + def delReader(self, socket): + self.condition.acquire() + del(self.readers[socket]) + self.condition.notify() + self.condition.release() + + def notify(self): + self.condition.acquire() + self.condition.notify() + self.condition.release() class Listener(object): - """A Listener is used by this module to listen on the multicast - group to which DNS messages are sent, allowing the implementation - to cache information as it arrives. - - It requires registration with an Engine object in order to have - the read() method called when a socket is availble for reading.""" - - def __init__(self, zeroconf): - self.zeroconf = zeroconf - self.zeroconf.engine.addReader(self, self.zeroconf.socket) - - def handle_read(self): - data, (addr, port) = self.zeroconf.socket.recvfrom(_MAX_MSG_ABSOLUTE) - self.data = data - msg = DNSIncoming(data) - if msg.isQuery(): - # Always multicast responses - # - if port == _MDNS_PORT: - self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) - # If it's not a multicast query, reply via unicast - # and multicast - # - elif port == _DNS_PORT: - self.zeroconf.handleQuery(msg, addr, port) - self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) - else: - self.zeroconf.handleResponse(msg) + """A Listener is used by this module to listen on the multicast + group to which DNS messages are sent, allowing the implementation + to cache information as it arrives. + + It requires registration with an Engine object in order to have + the read() method called when a socket is availble for reading.""" + + def __init__(self, zeroconf): + self.zeroconf = zeroconf + self.zeroconf.engine.addReader(self, self.zeroconf.socket) + + def handle_read(self): + data, (addr, port) = self.zeroconf.socket.recvfrom(_MAX_MSG_ABSOLUTE) + self.data = data + msg = DNSIncoming(data) + if msg.isQuery(): + # Always multicast responses + # + if port == _MDNS_PORT: + self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) + # If it's not a multicast query, reply via unicast + # and multicast + # + elif port == _DNS_PORT: + self.zeroconf.handleQuery(msg, addr, port) + self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) + else: + self.zeroconf.handleResponse(msg) class Reaper(threading.Thread): - """A Reaper is used by this module to remove cache entries that - have expired.""" - - def __init__(self, zeroconf): - threading.Thread.__init__(self) - self.zeroconf = zeroconf - self.start() - - def run(self): - while 1: - self.zeroconf.wait(10 * 1000) - if globals()['_GLOBAL_DONE']: - return - now = currentTimeMillis() - for record in self.zeroconf.cache.entries(): - if record.isExpired(now): - self.zeroconf.updateRecord(now, record) - self.zeroconf.cache.remove(record) + """A Reaper is used by this module to remove cache entries that + have expired.""" + + def __init__(self, zeroconf): + threading.Thread.__init__(self) + self.zeroconf = zeroconf + self.start() + + def run(self): + while 1: + self.zeroconf.wait(10 * 1000) + if globals()['_GLOBAL_DONE']: + return + now = currentTimeMillis() + for record in self.zeroconf.cache.entries(): + if record.isExpired(now): + self.zeroconf.updateRecord(now, record) + self.zeroconf.cache.remove(record) class ServiceBrowser(threading.Thread): - """Used to browse for a service of a specific type. - - The listener object will have its addService() and - removeService() methods called when this browser - discovers changes in the services availability.""" - - def __init__(self, zeroconf, type, listener): - """Creates a browser for a specific type""" - threading.Thread.__init__(self) - self.zeroconf = zeroconf - self.type = type - self.listener = listener - self.services = {} - self.nextTime = currentTimeMillis() - self.delay = _BROWSER_TIME - self.list = [] - - self.done = 0 - - self.zeroconf.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) - self.start() - - def updateRecord(self, zeroconf, now, record): - """Callback invoked by Zeroconf when new information arrives. - - Updates information required by browser in the Zeroconf cache.""" - if record.type == _TYPE_PTR and record.name == self.type: - expired = record.isExpired(now) - try: - oldrecord = self.services[record.alias.lower()] - if not expired: - oldrecord.resetTTL(record) - else: - del(self.services[record.alias.lower()]) - callback = lambda x: self.listener.removeService(x, self.type, record.alias) - self.list.append(callback) - return - except: - if not expired: - self.services[record.alias.lower()] = record - callback = lambda x: self.listener.addService(x, self.type, record.alias) - self.list.append(callback) - - expires = record.getExpirationTime(75) - if expires < self.nextTime: - self.nextTime = expires - - def cancel(self): - self.done = 1 - self.zeroconf.notifyAll() - - def run(self): - while 1: - event = None - now = currentTimeMillis() - if len(self.list) == 0 and self.nextTime > now: - self.zeroconf.wait(self.nextTime - now) - if globals()['_GLOBAL_DONE'] or self.done: - return - now = currentTimeMillis() - - if self.nextTime <= now: - out = DNSOutgoing(_FLAGS_QR_QUERY) - out.addQuestion(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) - for record in self.services.values(): - if not record.isExpired(now): - out.addAnswerAtTime(record, now) - self.zeroconf.send(out) - self.nextTime = now + self.delay - self.delay = min(20 * 1000, self.delay * 2) - - if len(self.list) > 0: - event = self.list.pop(0) - - if event is not None: - event(self.zeroconf) - + """Used to browse for a service of a specific type. + + The listener object will have its addService() and + removeService() methods called when this browser + discovers changes in the services availability.""" + + def __init__(self, zeroconf, type, listener): + """Creates a browser for a specific type""" + threading.Thread.__init__(self) + self.zeroconf = zeroconf + self.type = type + self.listener = listener + self.services = {} + self.nextTime = currentTimeMillis() + self.delay = _BROWSER_TIME + self.list = [] + + self.done = 0 + + self.zeroconf.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) + self.start() + + def updateRecord(self, zeroconf, now, record): + """Callback invoked by Zeroconf when new information arrives. + + Updates information required by browser in the Zeroconf cache.""" + if record.type == _TYPE_PTR and record.name == self.type: + expired = record.isExpired(now) + try: + oldrecord = self.services[record.alias.lower()] + if not expired: + oldrecord.resetTTL(record) + else: + del(self.services[record.alias.lower()]) + callback = lambda x: self.listener.removeService(x, self.type, record.alias) + self.list.append(callback) + return + except: + if not expired: + self.services[record.alias.lower()] = record + callback = lambda x: self.listener.addService(x, self.type, record.alias) + self.list.append(callback) + + expires = record.getExpirationTime(75) + if expires < self.nextTime: + self.nextTime = expires + + def cancel(self): + self.done = 1 + self.zeroconf.notifyAll() + + def run(self): + while 1: + event = None + now = currentTimeMillis() + if len(self.list) == 0 and self.nextTime > now: + self.zeroconf.wait(self.nextTime - now) + if globals()['_GLOBAL_DONE'] or self.done: + return + now = currentTimeMillis() + + if self.nextTime <= now: + out = DNSOutgoing(_FLAGS_QR_QUERY) + out.addQuestion(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) + for record in self.services.values(): + if not record.isExpired(now): + out.addAnswerAtTime(record, now) + self.zeroconf.send(out) + self.nextTime = now + self.delay + self.delay = min(20 * 1000, self.delay * 2) + + if len(self.list) > 0: + event = self.list.pop(0) + + if event is not None: + event(self.zeroconf) + class ServiceInfo(object): - """Service information""" - - def __init__(self, type, name, address=None, port=None, weight=0, priority=0, properties=None, server=None): - """Create a service description. - - type: fully qualified service type name - name: fully qualified service name - address: IP address as unsigned short, network byte order - port: port that the service runs on - weight: weight of the service - priority: priority of the service - properties: dictionary of properties (or a string holding the bytes for the text field) - server: fully qualified name for service host (defaults to name)""" - - if not name.endswith(type): - raise BadTypeInNameException - self.type = type - self.name = name - self.address = address - self.port = port - self.weight = weight - self.priority = priority - if server: - self.server = server - else: - self.server = name - self.setProperties(properties) - - def setProperties(self, properties): - """Sets properties and text of this info from a dictionary""" - if isinstance(properties, dict): - self.properties = properties - list = [] - result = '' - for key in properties: - value = properties[key] - if value is None: - suffix = ''.encode('utf-8') - elif isinstance(value, str): - suffix = value.encode('utf-8') - elif isinstance(value, int): - if value: - suffix = 'true' - else: - suffix = 'false' - else: - suffix = ''.encode('utf-8') - list.append('='.join((key, suffix))) - for item in list: - result = ''.join((result, struct.pack('!c', chr(len(item))), item)) - self.text = result - else: - self.text = properties - - def setText(self, text): - """Sets properties and text given a text field""" - self.text = text - try: - result = {} - end = len(text) - index = 0 - strs = [] - while index < end: - length = ord(text[index]) - index += 1 - strs.append(text[index:index+length]) - index += length - - for s in strs: - eindex = s.find('=') - if eindex == -1: - # No equals sign at all - key = s - value = 0 - else: - key = s[:eindex] - value = s[eindex+1:] - if value == 'true': - value = 1 - elif value == 'false' or not value: - value = 0 - - # Only update non-existent properties - if key and result.get(key) == None: - result[key] = value - - self.properties = result - except: - traceback.print_exc() - self.properties = None - - def getType(self): - """Type accessor""" - return self.type - - def getName(self): - """Name accessor""" - if self.type is not None and self.name.endswith("." + self.type): - return self.name[:len(self.name) - len(self.type) - 1] - return self.name - - def getAddress(self): - """Address accessor""" - return self.address - - def getPort(self): - """Port accessor""" - return self.port - - def getPriority(self): - """Pirority accessor""" - return self.priority - - def getWeight(self): - """Weight accessor""" - return self.weight - - def getProperties(self): - """Properties accessor""" - return self.properties - - def getText(self): - """Text accessor""" - return self.text - - def getServer(self): - """Server accessor""" - return self.server - - def updateRecord(self, zeroconf, now, record): - """Updates service information from a DNS record""" - if record is not None and not record.isExpired(now): - if record.type == _TYPE_A: - if record.name == self.name: - self.address = record.address - elif record.type == _TYPE_SRV: - if record.name == self.name: - self.server = record.server - self.port = record.port - self.weight = record.weight - self.priority = record.priority - self.address = None - self.updateRecord(zeroconf, now, zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) - elif record.type == _TYPE_TXT: - if record.name == self.name: - self.setText(record.text) - - def request(self, zeroconf, timeout): - """Returns true if the service could be discovered on the - network, and updates this object with details discovered. - """ - now = currentTimeMillis() - delay = _LISTENER_TIME - next = now + delay - last = now + timeout - result = 0 - try: - zeroconf.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) - while self.server is None or self.address is None or self.text is None: - if last <= now: - return 0 - if next <= now: - out = DNSOutgoing(_FLAGS_QR_QUERY) - out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_SRV, _CLASS_IN), now) - out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_TXT, _CLASS_IN), now) - if self.server is not None: - out.addQuestion(DNSQuestion(self.server, _TYPE_A, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN), now) - zeroconf.send(out) - next = now + delay - delay = delay * 2 - - zeroconf.wait(min(next, last) - now) - now = currentTimeMillis() - result = 1 - finally: - zeroconf.removeListener(self) - - return result - - def __eq__(self, other): - """Tests equality of service name""" - if isinstance(other, ServiceInfo): - return other.name == self.name - return 0 - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def __repr__(self): - """String representation""" - result = "service[%s,%s:%s," % (self.name, socket.inet_ntoa(self.getAddress()), self.port) - if self.text is None: - result += "None" - else: - if len(self.text) < 20: - result += self.text - else: - result += self.text[:17] + "..." - result += "]" - return result - + """Service information""" + + def __init__(self, type, name, address=None, port=None, weight=0, priority=0, properties=None, server=None): + """Create a service description. + + type: fully qualified service type name + name: fully qualified service name + address: IP address as unsigned short, network byte order + port: port that the service runs on + weight: weight of the service + priority: priority of the service + properties: dictionary of properties (or a string holding the bytes for the text field) + server: fully qualified name for service host (defaults to name)""" + + if not name.endswith(type): + raise BadTypeInNameException + self.type = type + self.name = name + self.address = address + self.port = port + self.weight = weight + self.priority = priority + if server: + self.server = server + else: + self.server = name + self.setProperties(properties) + + def setProperties(self, properties): + """Sets properties and text of this info from a dictionary""" + if isinstance(properties, dict): + self.properties = properties + list = [] + result = '' + for key in properties: + value = properties[key] + if value is None: + suffix = ''.encode('utf-8') + elif isinstance(value, str): + suffix = value.encode('utf-8') + elif isinstance(value, int): + if value: + suffix = 'true' + else: + suffix = 'false' + else: + suffix = ''.encode('utf-8') + list.append('='.join((key, suffix))) + for item in list: + result = ''.join((result, struct.pack('!c', chr(len(item))), item)) + self.text = result + else: + self.text = properties + + def setText(self, text): + """Sets properties and text given a text field""" + self.text = text + try: + result = {} + end = len(text) + index = 0 + strs = [] + while index < end: + length = ord(text[index]) + index += 1 + strs.append(text[index:index+length]) + index += length + + for s in strs: + eindex = s.find('=') + if eindex == -1: + # No equals sign at all + key = s + value = 0 + else: + key = s[:eindex] + value = s[eindex+1:] + if value == 'true': + value = 1 + elif value == 'false' or not value: + value = 0 + + # Only update non-existent properties + if key and result.get(key) == None: + result[key] = value + + self.properties = result + except: + traceback.print_exc() + self.properties = None + + def getType(self): + """Type accessor""" + return self.type + + def getName(self): + """Name accessor""" + if self.type is not None and self.name.endswith("." + self.type): + return self.name[:len(self.name) - len(self.type) - 1] + return self.name + + def getAddress(self): + """Address accessor""" + return self.address + + def getPort(self): + """Port accessor""" + return self.port + + def getPriority(self): + """Pirority accessor""" + return self.priority + + def getWeight(self): + """Weight accessor""" + return self.weight + + def getProperties(self): + """Properties accessor""" + return self.properties + + def getText(self): + """Text accessor""" + return self.text + + def getServer(self): + """Server accessor""" + return self.server + + def updateRecord(self, zeroconf, now, record): + """Updates service information from a DNS record""" + if record is not None and not record.isExpired(now): + if record.type == _TYPE_A: + #if record.name == self.name: + if record.name == self.server: + self.address = record.address + elif record.type == _TYPE_SRV: + if record.name == self.name: + self.server = record.server + self.port = record.port + self.weight = record.weight + self.priority = record.priority + #self.address = None + self.updateRecord(zeroconf, now, zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) + elif record.type == _TYPE_TXT: + if record.name == self.name: + self.setText(record.text) + + def request(self, zeroconf, timeout): + """Returns true if the service could be discovered on the + network, and updates this object with details discovered. + """ + now = currentTimeMillis() + delay = _LISTENER_TIME + next = now + delay + last = now + timeout + result = 0 + try: + zeroconf.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) + while self.server is None or self.address is None or self.text is None: + if last <= now: + return 0 + if next <= now: + out = DNSOutgoing(_FLAGS_QR_QUERY) + out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN)) + out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_SRV, _CLASS_IN), now) + out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN)) + out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_TXT, _CLASS_IN), now) + if self.server is not None: + out.addQuestion(DNSQuestion(self.server, _TYPE_A, _CLASS_IN)) + out.addAnswerAtTime(zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN), now) + zeroconf.send(out) + next = now + delay + delay = delay * 2 + + zeroconf.wait(min(next, last) - now) + now = currentTimeMillis() + result = 1 + finally: + zeroconf.removeListener(self) + + return result + + def __eq__(self, other): + """Tests equality of service name""" + if isinstance(other, ServiceInfo): + return other.name == self.name + return 0 + + def __ne__(self, other): + """Non-equality test""" + return not self.__eq__(other) + + def __repr__(self): + """String representation""" + result = "service[%s,%s:%s," % (self.name, socket.inet_ntoa(self.getAddress()), self.port) + if self.text is None: + result += "None" + else: + if len(self.text) < 20: + result += self.text + else: + result += self.text[:17] + "..." + result += "]" + return result + class Zeroconf(object): - """Implementation of Zeroconf Multicast DNS Service Discovery - - Supports registration, unregistration, queries and browsing. - """ - def __init__(self, bindaddress=None): - """Creates an instance of the Zeroconf class, establishing - multicast communications, listening and reaping threads.""" - globals()['_GLOBAL_DONE'] = 0 - if bindaddress is None: - self.intf = socket.gethostbyname(socket.gethostname()) - else: - self.intf = bindaddress - self.group = ('', _MDNS_PORT) - self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - try: - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) - except: - # SO_REUSEADDR should be equivalent to SO_REUSEPORT for - # multicast UDP sockets (p 731, "TCP/IP Illustrated, - # Volume 2"), but some BSD-derived systems require - # SO_REUSEPORT to be specified explicity. Also, not all - # versions of Python have SO_REUSEPORT available. So - # if you're on a BSD-based system, and haven't upgraded - # to Python 2.3 yet, you may find this library doesn't - # work as expected. - # - pass - self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 255) - self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1) - try: - self.socket.bind(self.group) - except: - # Some versions of linux raise an exception even though - # the SO_REUSE* options have been set, so ignore it - # - pass - self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) - self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) - - self.listeners = [] - self.browsers = [] - self.services = {} - - self.cache = DNSCache() - - self.condition = threading.Condition() - - self.engine = Engine(self) - self.listener = Listener(self) - self.reaper = Reaper(self) - - def isLoopback(self): - return self.intf.startswith("127.0.0.1") - - def isLinklocal(self): - return self.intf.startswith("169.254.") - - def wait(self, timeout): - """Calling thread waits for a given number of milliseconds or - until notified.""" - self.condition.acquire() - self.condition.wait(timeout/1000) - self.condition.release() - - def notifyAll(self): - """Notifies all waiting threads""" - self.condition.acquire() - self.condition.notifyAll() - self.condition.release() - - def getServiceInfo(self, type, name, timeout=3000): - """Returns network's service information for a particular - name and type, or None if no service matches by the timeout, - which defaults to 3 seconds.""" - info = ServiceInfo(type, name) - if info.request(self, timeout): - return info - return None - - def addServiceListener(self, type, listener): - """Adds a listener for a particular service type. This object - will then have its updateRecord method called when information - arrives for that type.""" - self.removeServiceListener(listener) - self.browsers.append(ServiceBrowser(self, type, listener)) - - def removeServiceListener(self, listener): - """Removes a listener from the set that is currently listening.""" - for browser in self.browsers: - if browser.listener == listener: - browser.cancel() - del(browser) - - def registerService(self, info, ttl=_DNS_TTL): - """Registers service information to the network with a default TTL - of 60 seconds. Zeroconf will then respond to requests for - information for that service. The name of the service may be - changed if needed to make it unique on the network.""" - self.checkService(info) - self.services[info.name.lower()] = info - now = currentTimeMillis() - nextTime = now - i = 0 - while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, ttl, info.priority, info.weight, info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0) - if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, ttl, info.address), 0) - self.send(out) - i += 1 - nextTime += _REGISTER_TIME - - def unregisterService(self, info): - """Unregister a service.""" - try: - del(self.services[info.name.lower()]) - except: - pass - now = currentTimeMillis() - nextTime = now - i = 0 - while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.name), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) - if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) - self.send(out) - i += 1 - nextTime += _UNREGISTER_TIME - - def unregisterAllServices(self): - """Unregister all registered services.""" - if len(self.services) > 0: - now = currentTimeMillis() - nextTime = now - i = 0 - while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - for info in self.services.values(): - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) - if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) - self.send(out) - i += 1 - nextTime += _UNREGISTER_TIME - - def checkService(self, info): - """Checks the network for a unique service name, modifying the - ServiceInfo passed in if it is not unique.""" - now = currentTimeMillis() - nextTime = now - i = 0 - while i < 3: - for record in self.cache.entriesWithName(info.type): - if record.type == _TYPE_PTR and not record.isExpired(now) and record.alias == info.name: - if (info.name.find('.') < 0): - info.name = info.name + ".[" + info.address + ":" + info.port + "]." + info.type - self.checkService(info) - return - raise NonUniqueNameException - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() - continue - out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) - self.debug = out - out.addQuestion(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) - out.addAuthorativeAnswer(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, info.name)) - self.send(out) - i += 1 - nextTime += _CHECK_TIME - - def addListener(self, listener, question): - """Adds a listener for a given question. The listener will have - its updateRecord method called when information is available to - answer the question.""" - now = currentTimeMillis() - self.listeners.append(listener) - if question is not None: - for record in self.cache.entriesWithName(question.name): - if question.answeredBy(record) and not record.isExpired(now): - listener.updateRecord(self, now, record) - self.notifyAll() - - def removeListener(self, listener): - """Removes a listener.""" - try: - self.listeners.remove(listener) - self.notifyAll() - except: - pass - - def updateRecord(self, now, rec): - """Used to notify listeners of new information that has updated - a record.""" - for listener in self.listeners: - listener.updateRecord(self, now, rec) - self.notifyAll() - - def handleResponse(self, msg): - """Deal with incoming response packets. All answers - are held in the cache, and listeners are notified.""" - now = currentTimeMillis() - for record in msg.answers: - expired = record.isExpired(now) - if record in self.cache.entries(): - if expired: - self.cache.remove(record) - else: - entry = self.cache.get(record) - if entry is not None: - entry.resetTTL(record) - record = entry - else: - self.cache.add(record) - - self.updateRecord(now, record) - - def handleQuery(self, msg, addr, port): - """Deal with incoming query packets. Provides a response if - possible.""" - out = None - - # Support unicast client responses - # - if port != _MDNS_PORT: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, 0) - for question in msg.questions: - out.addQuestion(question) - - for question in msg.questions: - if question.type == _TYPE_PTR: - for service in self.services.values(): - if question.name == service.type: - if out is None: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswer(msg, DNSPointer(service.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, service.name)) - else: - try: - if out is None: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - - # Answer A record queries for any service addresses we know - if question.type == _TYPE_A or question.type == _TYPE_ANY: - for service in self.services.values(): - if service.server == question.name.lower(): - out.addAnswer(msg, DNSAddress(question.name, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) - - service = self.services.get(question.name.lower(), None) - if not service: continue - - if question.type == _TYPE_SRV or question.type == _TYPE_ANY: - out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.priority, service.weight, service.port, service.server)) - if question.type == _TYPE_TXT or question.type == _TYPE_ANY: - out.addAnswer(msg, DNSText(question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.text)) - if question.type == _TYPE_SRV: - out.addAdditionalAnswer(DNSAddress(service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) - except: - traceback.print_exc() - - if out is not None and out.answers: - out.id = msg.id - self.send(out, addr, port) - - def send(self, out, addr = _MDNS_ADDR, port = _MDNS_PORT): - """Sends an outgoing packet.""" - # This is a quick test to see if we can parse the packets we generate - #temp = DNSIncoming(out.packet()) - try: - bytes_sent = self.socket.sendto(out.packet(), 0, (addr, port)) - except: - # Ignore this, it may be a temporary loss of network connection - pass - - def close(self): - """Ends the background threads, and prevent this instance from - servicing further queries.""" - if globals()['_GLOBAL_DONE'] == 0: - globals()['_GLOBAL_DONE'] = 1 - self.notifyAll() - self.engine.notify() - self.unregisterAllServices() - self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) - self.socket.close() - + """Implementation of Zeroconf Multicast DNS Service Discovery + + Supports registration, unregistration, queries and browsing. + """ + def __init__(self, bindaddress=None): + """Creates an instance of the Zeroconf class, establishing + multicast communications, listening and reaping threads.""" + globals()['_GLOBAL_DONE'] = 0 + if bindaddress is None: + self.intf = socket.gethostbyname(socket.gethostname()) + else: + self.intf = bindaddress + self.group = ('', _MDNS_PORT) + self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + except: + # SO_REUSEADDR should be equivalent to SO_REUSEPORT for + # multicast UDP sockets (p 731, "TCP/IP Illustrated, + # Volume 2"), but some BSD-derived systems require + # SO_REUSEPORT to be specified explicity. Also, not all + # versions of Python have SO_REUSEPORT available. So + # if you're on a BSD-based system, and haven't upgraded + # to Python 2.3 yet, you may find this library doesn't + # work as expected. + # + pass + self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 255) + self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1) + try: + self.socket.bind(self.group) + except: + # Some versions of linux raise an exception even though + # the SO_REUSE* options have been set, so ignore it + # + pass + #self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) + self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + + self.listeners = [] + self.browsers = [] + self.services = {} + self.servicetypes = {} + + self.cache = DNSCache() + + self.condition = threading.Condition() + + self.engine = Engine(self) + self.listener = Listener(self) + self.reaper = Reaper(self) + + def isLoopback(self): + return self.intf.startswith("127.0.0.1") + + def isLinklocal(self): + return self.intf.startswith("169.254.") + + def wait(self, timeout): + """Calling thread waits for a given number of milliseconds or + until notified.""" + self.condition.acquire() + self.condition.wait(timeout/1000) + self.condition.release() + + def notifyAll(self): + """Notifies all waiting threads""" + self.condition.acquire() + self.condition.notifyAll() + self.condition.release() + + def getServiceInfo(self, type, name, timeout=3000): + """Returns network's service information for a particular + name and type, or None if no service matches by the timeout, + which defaults to 3 seconds.""" + info = ServiceInfo(type, name) + if info.request(self, timeout): + return info + return None + + def addServiceListener(self, type, listener): + """Adds a listener for a particular service type. This object + will then have its updateRecord method called when information + arrives for that type.""" + self.removeServiceListener(listener) + self.browsers.append(ServiceBrowser(self, type, listener)) + + def removeServiceListener(self, listener): + """Removes a listener from the set that is currently listening.""" + for browser in self.browsers: + if browser.listener == listener: + browser.cancel() + del(browser) + + def registerService(self, info, ttl=_DNS_TTL): + """Registers service information to the network with a default TTL + of 60 seconds. Zeroconf will then respond to requests for + information for that service. The name of the service may be + changed if needed to make it unique on the network.""" + self.checkService(info) + self.services[info.name.lower()] = info + if self.servicetypes.has_key(info.type): + self.servicetypes[info.type]+=1 + else: + self.servicetypes[info.type]=1 + now = currentTimeMillis() + nextTime = now + i = 0 + while i < 3: + if now < nextTime: + self.wait(nextTime - now) + now = currentTimeMillis() + continue + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, ttl, info.priority, info.weight, info.port, info.server), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0) + if info.address: + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, ttl, info.address), 0) + self.send(out) + i += 1 + nextTime += _REGISTER_TIME + + def unregisterService(self, info): + """Unregister a service.""" + try: + del(self.services[info.name.lower()]) + if self.servicetypes[info.type]>1: + self.servicetypes[info.type]-=1 + else: + del self.servicetypes[info.type] + except: + pass + now = currentTimeMillis() + nextTime = now + i = 0 + while i < 3: + if now < nextTime: + self.wait(nextTime - now) + now = currentTimeMillis() + continue + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.name), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) + if info.address: + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) + self.send(out) + i += 1 + nextTime += _UNREGISTER_TIME + + def unregisterAllServices(self): + """Unregister all registered services.""" + if len(self.services) > 0: + now = currentTimeMillis() + nextTime = now + i = 0 + while i < 3: + if now < nextTime: + self.wait(nextTime - now) + now = currentTimeMillis() + continue + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + for info in self.services.values(): + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.server), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) + if info.address: + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) + self.send(out) + i += 1 + nextTime += _UNREGISTER_TIME + + def checkService(self, info): + """Checks the network for a unique service name, modifying the + ServiceInfo passed in if it is not unique.""" + now = currentTimeMillis() + nextTime = now + i = 0 + while i < 3: + for record in self.cache.entriesWithName(info.type): + if record.type == _TYPE_PTR and not record.isExpired(now) and record.alias == info.name: + if (info.name.find('.') < 0): + info.name = info.name + ".[" + info.address + ":" + info.port + "]." + info.type + self.checkService(info) + return + raise NonUniqueNameException + if now < nextTime: + self.wait(nextTime - now) + now = currentTimeMillis() + continue + out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) + self.debug = out + out.addQuestion(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) + out.addAuthorativeAnswer(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, info.name)) + self.send(out) + i += 1 + nextTime += _CHECK_TIME + + def addListener(self, listener, question): + """Adds a listener for a given question. The listener will have + its updateRecord method called when information is available to + answer the question.""" + now = currentTimeMillis() + self.listeners.append(listener) + if question is not None: + for record in self.cache.entriesWithName(question.name): + if question.answeredBy(record) and not record.isExpired(now): + listener.updateRecord(self, now, record) + self.notifyAll() + + def removeListener(self, listener): + """Removes a listener.""" + try: + self.listeners.remove(listener) + self.notifyAll() + except: + pass + + def updateRecord(self, now, rec): + """Used to notify listeners of new information that has updated + a record.""" + for listener in self.listeners: + listener.updateRecord(self, now, rec) + self.notifyAll() + + def handleResponse(self, msg): + """Deal with incoming response packets. All answers + are held in the cache, and listeners are notified.""" + now = currentTimeMillis() + for record in msg.answers: + expired = record.isExpired(now) + if record in self.cache.entries(): + if expired: + self.cache.remove(record) + else: + entry = self.cache.get(record) + if entry is not None: + entry.resetTTL(record) + record = entry + else: + self.cache.add(record) + + self.updateRecord(now, record) + + def handleQuery(self, msg, addr, port): + """Deal with incoming query packets. Provides a response if + possible.""" + out = None + + # Support unicast client responses + # + if port != _MDNS_PORT: + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, 0) + for question in msg.questions: + out.addQuestion(question) + + for question in msg.questions: + if question.type == _TYPE_PTR: + if question.name == "_services._dns-sd._udp.local.": + for stype in self.servicetypes.keys(): + if out is None: + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + out.addAnswer(msg, DNSPointer("_services._dns-sd._udp.local.", _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) + for service in self.services.values(): + if question.name == service.type: + if out is None: + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + out.addAnswer(msg, DNSPointer(service.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, service.name)) + else: + try: + if out is None: + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + + # Answer A record queries for any service addresses we know + if question.type == _TYPE_A or question.type == _TYPE_ANY: + for service in self.services.values(): + if service.server == question.name.lower(): + out.addAnswer(msg, DNSAddress(question.name, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) + + service = self.services.get(question.name.lower(), None) + if not service: continue + + if question.type == _TYPE_SRV or question.type == _TYPE_ANY: + out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.priority, service.weight, service.port, service.server)) + if question.type == _TYPE_TXT or question.type == _TYPE_ANY: + out.addAnswer(msg, DNSText(question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.text)) + if question.type == _TYPE_SRV: + out.addAdditionalAnswer(DNSAddress(service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) + except: + traceback.print_exc() + + if out is not None and out.answers: + out.id = msg.id + self.send(out, addr, port) + + def send(self, out, addr = _MDNS_ADDR, port = _MDNS_PORT): + """Sends an outgoing packet.""" + # This is a quick test to see if we can parse the packets we generate + #temp = DNSIncoming(out.packet()) + try: + bytes_sent = self.socket.sendto(out.packet(), 0, (addr, port)) + except: + # Ignore this, it may be a temporary loss of network connection + pass + + def close(self): + """Ends the background threads, and prevent this instance from + servicing further queries.""" + if globals()['_GLOBAL_DONE'] == 0: + globals()['_GLOBAL_DONE'] = 1 + self.notifyAll() + self.engine.notify() + self.unregisterAllServices() + self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + self.socket.close() + # Test a few module features, including service registration, service # query (for Zoe), and service unregistration. -if __name__ == '__main__': - print "Multicast DNS Service Discovery for Python, version", __version__ - r = Zeroconf() - print "1. Testing registration of a service..." - desc = {'version':'0.10','a':'test value', 'b':'another value'} - info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) - print " Registering service..." - r.registerService(info) - print " Registration done." - print "2. Testing query of service information..." - print " Getting ZOE service:", str(r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local.")) - print " Query done." - print "3. Testing query of own service..." - print " Getting self:", str(r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.")) - print " Query done." - print "4. Testing unregister of service information..." - r.unregisterService(info) - print " Unregister done." - r.close() +if __name__ == '__main__': + print "Multicast DNS Service Discovery for Python, version", __version__ + r = Zeroconf() + print "1. Testing registration of a service..." + desc = {'version':'0.10','a':'test value', 'b':'another value'} + info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) + print " Registering service..." + r.registerService(info) + print " Registration done." + print "2. Testing query of service information..." + print " Getting ZOE service:", str(r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local.")) + print " Query done." + print "3. Testing query of own service..." + print " Getting self:", str(r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.")) + print " Query done." + print "4. Testing unregister of service information..." + r.unregisterService(info) + print " Unregister done." + r.close() From f998e39cbb8d2c5556c10203957ff6a9ab2f546d Mon Sep 17 00:00:00 2001 From: William McBrine Date: Thu, 2 Oct 2008 19:38:03 -0400 Subject: [PATCH 003/163] has_key() is deprecated. --- Zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zeroconf.py b/Zeroconf.py index 6378164d..77ebd6f6 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -1331,7 +1331,7 @@ def registerService(self, info, ttl=_DNS_TTL): changed if needed to make it unique on the network.""" self.checkService(info) self.services[info.name.lower()] = info - if self.servicetypes.has_key(info.type): + if info.type in self.servicetypes: self.servicetypes[info.type]+=1 else: self.servicetypes[info.type]=1 From 892a4f095c23379a6cf5a0ef31521f9f90cb5276 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Sat, 15 Nov 2008 21:26:45 -0500 Subject: [PATCH 004/163] Untab Zeroconf. --- Zeroconf.py | 2760 +++++++++++++++++++++++++-------------------------- 1 file changed, 1380 insertions(+), 1380 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 77ebd6f6..00708a66 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -19,33 +19,33 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - + """ """0.12 update - allow selection of binding interface - typo fix - Thanks A. M. Kuchlingi - removed all use of word 'Rendezvous' - this is an API change""" + typo fix - Thanks A. M. Kuchlingi + removed all use of word 'Rendezvous' - this is an API change""" """0.11 update - correction to comments for addListener method support for new record types seen from OS X - - IPv6 address - - hostinfo - ignore unknown DNS record types - fixes to name decoding - works alongside other processes using port 5353 (e.g. on Mac OS X) - tested against Mac OS X 10.3.2's mDNSResponder - corrections to removal of list entries for service browser""" + - IPv6 address + - hostinfo + ignore unknown DNS record types + fixes to name decoding + works alongside other processes using port 5353 (e.g. on Mac OS X) + tested against Mac OS X 10.3.2's mDNSResponder + corrections to removal of list entries for service browser""" """0.10 update - Jonathon Paisley contributed these corrections: always multicast replies, even when query is unicast - correct a pointer encoding problem - can now write records in any order - traceback shown on failure - better TXT record parsing - server is now separate from name - can cancel a service browser + correct a pointer encoding problem + can now write records in any order + traceback shown on failure + better TXT record parsing + server is now separate from name + can cancel a service browser - modified some unit tests to accommodate these changes""" + modified some unit tests to accommodate these changes""" """0.09 update - remove all records on service unregistration fix DOS security problem with readName""" @@ -54,25 +54,25 @@ """0.07 update - faster shutdown on engine pointer encoding of outgoing names - ServiceBrowser now works - new unit tests""" + ServiceBrowser now works + new unit tests""" """0.06 update - small improvements with unit tests added defined exception types - new style objects - fixed hostname/interface problem - fixed socket timeout problem - fixed addServiceListener() typo bug - using select() for socket reads - tested on Debian unstable with Python 2.2.2""" + new style objects + fixed hostname/interface problem + fixed socket timeout problem + fixed addServiceListener() typo bug + using select() for socket reads + tested on Debian unstable with Python 2.2.2""" """0.05 update - ensure case insensitivty on domain names support for unicast DNS queries""" """0.04 update - added some unit tests added __ne__ adjuncts where required - ensure names end in '.local.' - timeout on receiving socket for clean shutdown""" + ensure names end in '.local.' + timeout on receiving socket for clean shutdown""" __author__ = "Paul Scott-Murphy" __email__ = "paul at scott dash murphy dot com" @@ -101,7 +101,7 @@ _BROWSER_TIME = 500 # Some DNS constants - + _MDNS_ADDR = '224.0.0.251' _MDNS_PORT = 5353; _DNS_PORT = 53; @@ -155,1417 +155,1417 @@ # Mapping constants to names _CLASSES = { _CLASS_IN : "in", - _CLASS_CS : "cs", - _CLASS_CH : "ch", - _CLASS_HS : "hs", - _CLASS_NONE : "none", - _CLASS_ANY : "any" } + _CLASS_CS : "cs", + _CLASS_CH : "ch", + _CLASS_HS : "hs", + _CLASS_NONE : "none", + _CLASS_ANY : "any" } _TYPES = { _TYPE_A : "a", - _TYPE_NS : "ns", - _TYPE_MD : "md", - _TYPE_MF : "mf", - _TYPE_CNAME : "cname", - _TYPE_SOA : "soa", - _TYPE_MB : "mb", - _TYPE_MG : "mg", - _TYPE_MR : "mr", - _TYPE_NULL : "null", - _TYPE_WKS : "wks", - _TYPE_PTR : "ptr", - _TYPE_HINFO : "hinfo", - _TYPE_MINFO : "minfo", - _TYPE_MX : "mx", - _TYPE_TXT : "txt", - _TYPE_AAAA : "quada", - _TYPE_SRV : "srv", - _TYPE_ANY : "any" } + _TYPE_NS : "ns", + _TYPE_MD : "md", + _TYPE_MF : "mf", + _TYPE_CNAME : "cname", + _TYPE_SOA : "soa", + _TYPE_MB : "mb", + _TYPE_MG : "mg", + _TYPE_MR : "mr", + _TYPE_NULL : "null", + _TYPE_WKS : "wks", + _TYPE_PTR : "ptr", + _TYPE_HINFO : "hinfo", + _TYPE_MINFO : "minfo", + _TYPE_MX : "mx", + _TYPE_TXT : "txt", + _TYPE_AAAA : "quada", + _TYPE_SRV : "srv", + _TYPE_ANY : "any" } # utility functions def currentTimeMillis(): - """Current system time in milliseconds""" - return time.time() * 1000 + """Current system time in milliseconds""" + return time.time() * 1000 # Exceptions class NonLocalNameException(Exception): - pass + pass class NonUniqueNameException(Exception): - pass + pass class NamePartTooLongException(Exception): - pass + pass class AbstractMethodException(Exception): - pass + pass class BadTypeInNameException(Exception): - pass + pass # implementation classes class DNSEntry(object): - """A DNS entry""" - - def __init__(self, name, type, clazz): - self.key = string.lower(name) - self.name = name - self.type = type - self.clazz = clazz & _CLASS_MASK - self.unique = (clazz & _CLASS_UNIQUE) != 0 - - def __eq__(self, other): - """Equality test on name, type, and class""" - if isinstance(other, DNSEntry): - return self.name == other.name and self.type == other.type and self.clazz == other.clazz - return 0 - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def getClazz(self, clazz): - """Class accessor""" - try: - return _CLASSES[clazz] - except: - return "?(%s)" % (clazz) - - def getType(self, type): - """Type accessor""" - try: - return _TYPES[type] - except: - return "?(%s)" % (type) - - def toString(self, hdr, other): - """String representation with additional information""" - result = "%s[%s,%s" % (hdr, self.getType(self.type), self.getClazz(self.clazz)) - if self.unique: - result += "-unique," - else: - result += "," - result += self.name - if other is not None: - result += ",%s]" % (other) - else: - result += "]" - return result + """A DNS entry""" + + def __init__(self, name, type, clazz): + self.key = string.lower(name) + self.name = name + self.type = type + self.clazz = clazz & _CLASS_MASK + self.unique = (clazz & _CLASS_UNIQUE) != 0 + + def __eq__(self, other): + """Equality test on name, type, and class""" + if isinstance(other, DNSEntry): + return self.name == other.name and self.type == other.type and self.clazz == other.clazz + return 0 + + def __ne__(self, other): + """Non-equality test""" + return not self.__eq__(other) + + def getClazz(self, clazz): + """Class accessor""" + try: + return _CLASSES[clazz] + except: + return "?(%s)" % (clazz) + + def getType(self, type): + """Type accessor""" + try: + return _TYPES[type] + except: + return "?(%s)" % (type) + + def toString(self, hdr, other): + """String representation with additional information""" + result = "%s[%s,%s" % (hdr, self.getType(self.type), self.getClazz(self.clazz)) + if self.unique: + result += "-unique," + else: + result += "," + result += self.name + if other is not None: + result += ",%s]" % (other) + else: + result += "]" + return result class DNSQuestion(DNSEntry): - """A DNS question entry""" - - def __init__(self, name, type, clazz): - if not name.endswith(".local."): - raise NonLocalNameException - DNSEntry.__init__(self, name, type, clazz) + """A DNS question entry""" + + def __init__(self, name, type, clazz): + if not name.endswith(".local."): + raise NonLocalNameException + DNSEntry.__init__(self, name, type, clazz) - def answeredBy(self, rec): - """Returns true if the question is answered by the record""" - return self.clazz == rec.clazz and (self.type == rec.type or self.type == _TYPE_ANY) and self.name == rec.name + def answeredBy(self, rec): + """Returns true if the question is answered by the record""" + return self.clazz == rec.clazz and (self.type == rec.type or self.type == _TYPE_ANY) and self.name == rec.name - def __repr__(self): - """String representation""" - return DNSEntry.toString(self, "question", None) + def __repr__(self): + """String representation""" + return DNSEntry.toString(self, "question", None) class DNSRecord(DNSEntry): - """A DNS record - like a DNS entry, but has a TTL""" - - def __init__(self, name, type, clazz, ttl): - DNSEntry.__init__(self, name, type, clazz) - self.ttl = ttl - self.created = currentTimeMillis() - - def __eq__(self, other): - """Tests equality as per DNSRecord""" - if isinstance(other, DNSRecord): - return DNSEntry.__eq__(self, other) - return 0 - - def suppressedBy(self, msg): - """Returns true if any answer in a message can suffice for the - information held in this record.""" - for record in msg.answers: - if self.suppressedByAnswer(record): - return 1 - return 0 - - def suppressedByAnswer(self, other): - """Returns true if another record has same name, type and class, - and if its TTL is at least half of this record's.""" - if self == other and other.ttl > (self.ttl / 2): - return 1 - return 0 - - def getExpirationTime(self, percent): - """Returns the time at which this record will have expired - by a certain percentage.""" - return self.created + (percent * self.ttl * 10) - - def getRemainingTTL(self, now): - """Returns the remaining TTL in seconds.""" - return max(0, (self.getExpirationTime(100) - now) / 1000) - - def isExpired(self, now): - """Returns true if this record has expired.""" - return self.getExpirationTime(100) <= now - - def isStale(self, now): - """Returns true if this record is at least half way expired.""" - return self.getExpirationTime(50) <= now - - def resetTTL(self, other): - """Sets this record's TTL and created time to that of - another record.""" - self.created = other.created - self.ttl = other.ttl - - def write(self, out): - """Abstract method""" - raise AbstractMethodException - - def toString(self, other): - """String representation with addtional information""" - arg = "%s/%s,%s" % (self.ttl, self.getRemainingTTL(currentTimeMillis()), other) - return DNSEntry.toString(self, "record", arg) + """A DNS record - like a DNS entry, but has a TTL""" + + def __init__(self, name, type, clazz, ttl): + DNSEntry.__init__(self, name, type, clazz) + self.ttl = ttl + self.created = currentTimeMillis() + + def __eq__(self, other): + """Tests equality as per DNSRecord""" + if isinstance(other, DNSRecord): + return DNSEntry.__eq__(self, other) + return 0 + + def suppressedBy(self, msg): + """Returns true if any answer in a message can suffice for the + information held in this record.""" + for record in msg.answers: + if self.suppressedByAnswer(record): + return 1 + return 0 + + def suppressedByAnswer(self, other): + """Returns true if another record has same name, type and class, + and if its TTL is at least half of this record's.""" + if self == other and other.ttl > (self.ttl / 2): + return 1 + return 0 + + def getExpirationTime(self, percent): + """Returns the time at which this record will have expired + by a certain percentage.""" + return self.created + (percent * self.ttl * 10) + + def getRemainingTTL(self, now): + """Returns the remaining TTL in seconds.""" + return max(0, (self.getExpirationTime(100) - now) / 1000) + + def isExpired(self, now): + """Returns true if this record has expired.""" + return self.getExpirationTime(100) <= now + + def isStale(self, now): + """Returns true if this record is at least half way expired.""" + return self.getExpirationTime(50) <= now + + def resetTTL(self, other): + """Sets this record's TTL and created time to that of + another record.""" + self.created = other.created + self.ttl = other.ttl + + def write(self, out): + """Abstract method""" + raise AbstractMethodException + + def toString(self, other): + """String representation with addtional information""" + arg = "%s/%s,%s" % (self.ttl, self.getRemainingTTL(currentTimeMillis()), other) + return DNSEntry.toString(self, "record", arg) class DNSAddress(DNSRecord): - """A DNS address record""" - - def __init__(self, name, type, clazz, ttl, address): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.address = address - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeString(self.address, len(self.address)) - - def __eq__(self, other): - """Tests equality on address""" - if isinstance(other, DNSAddress): - return self.address == other.address - return 0 - - def __repr__(self): - """String representation""" - try: - return socket.inet_ntoa(self.address) - except: - return self.address + """A DNS address record""" + + def __init__(self, name, type, clazz, ttl, address): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.address = address + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeString(self.address, len(self.address)) + + def __eq__(self, other): + """Tests equality on address""" + if isinstance(other, DNSAddress): + return self.address == other.address + return 0 + + def __repr__(self): + """String representation""" + try: + return socket.inet_ntoa(self.address) + except: + return self.address class DNSHinfo(DNSRecord): - """A DNS host information record""" - - def __init__(self, name, type, clazz, ttl, cpu, os): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.cpu = cpu - self.os = os - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeString(self.cpu, len(self.cpu)) - out.writeString(self.os, len(self.os)) - - def __eq__(self, other): - """Tests equality on cpu and os""" - if isinstance(other, DNSHinfo): - return self.cpu == other.cpu and self.os == other.os - return 0 - - def __repr__(self): - """String representation""" - return self.cpu + " " + self.os - + """A DNS host information record""" + + def __init__(self, name, type, clazz, ttl, cpu, os): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.cpu = cpu + self.os = os + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeString(self.cpu, len(self.cpu)) + out.writeString(self.os, len(self.os)) + + def __eq__(self, other): + """Tests equality on cpu and os""" + if isinstance(other, DNSHinfo): + return self.cpu == other.cpu and self.os == other.os + return 0 + + def __repr__(self): + """String representation""" + return self.cpu + " " + self.os + class DNSPointer(DNSRecord): - """A DNS pointer record""" - - def __init__(self, name, type, clazz, ttl, alias): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.alias = alias - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeName(self.alias) - - def __eq__(self, other): - """Tests equality on alias""" - if isinstance(other, DNSPointer): - return self.alias == other.alias - return 0 - - def __repr__(self): - """String representation""" - return self.toString(self.alias) + """A DNS pointer record""" + + def __init__(self, name, type, clazz, ttl, alias): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.alias = alias + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeName(self.alias) + + def __eq__(self, other): + """Tests equality on alias""" + if isinstance(other, DNSPointer): + return self.alias == other.alias + return 0 + + def __repr__(self): + """String representation""" + return self.toString(self.alias) class DNSText(DNSRecord): - """A DNS text record""" - - def __init__(self, name, type, clazz, ttl, text): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.text = text - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeString(self.text, len(self.text)) - - def __eq__(self, other): - """Tests equality on text""" - if isinstance(other, DNSText): - return self.text == other.text - return 0 - - def __repr__(self): - """String representation""" - if len(self.text) > 10: - return self.toString(self.text[:7] + "...") - else: - return self.toString(self.text) + """A DNS text record""" + + def __init__(self, name, type, clazz, ttl, text): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.text = text + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeString(self.text, len(self.text)) + + def __eq__(self, other): + """Tests equality on text""" + if isinstance(other, DNSText): + return self.text == other.text + return 0 + + def __repr__(self): + """String representation""" + if len(self.text) > 10: + return self.toString(self.text[:7] + "...") + else: + return self.toString(self.text) class DNSService(DNSRecord): - """A DNS service record""" - - def __init__(self, name, type, clazz, ttl, priority, weight, port, server): - DNSRecord.__init__(self, name, type, clazz, ttl) - self.priority = priority - self.weight = weight - self.port = port - self.server = server - - def write(self, out): - """Used in constructing an outgoing packet""" - out.writeShort(self.priority) - out.writeShort(self.weight) - out.writeShort(self.port) - out.writeName(self.server) - - def __eq__(self, other): - """Tests equality on priority, weight, port and server""" - if isinstance(other, DNSService): - return self.priority == other.priority and self.weight == other.weight and self.port == other.port and self.server == other.server - return 0 - - def __repr__(self): - """String representation""" - return self.toString("%s:%s" % (self.server, self.port)) + """A DNS service record""" + + def __init__(self, name, type, clazz, ttl, priority, weight, port, server): + DNSRecord.__init__(self, name, type, clazz, ttl) + self.priority = priority + self.weight = weight + self.port = port + self.server = server + + def write(self, out): + """Used in constructing an outgoing packet""" + out.writeShort(self.priority) + out.writeShort(self.weight) + out.writeShort(self.port) + out.writeName(self.server) + + def __eq__(self, other): + """Tests equality on priority, weight, port and server""" + if isinstance(other, DNSService): + return self.priority == other.priority and self.weight == other.weight and self.port == other.port and self.server == other.server + return 0 + + def __repr__(self): + """String representation""" + return self.toString("%s:%s" % (self.server, self.port)) class DNSIncoming(object): - """Object representation of an incoming DNS packet""" - - def __init__(self, data): - """Constructor from string holding bytes of packet""" - self.offset = 0 - self.data = data - self.questions = [] - self.answers = [] - self.numQuestions = 0 - self.numAnswers = 0 - self.numAuthorities = 0 - self.numAdditionals = 0 - - self.readHeader() - self.readQuestions() - self.readOthers() - - def readHeader(self): - """Reads header portion of packet""" - format = '!HHHHHH' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - - self.id = info[0] - self.flags = info[1] - self.numQuestions = info[2] - self.numAnswers = info[3] - self.numAuthorities = info[4] - self.numAdditionals = info[5] - - def readQuestions(self): - """Reads questions section of packet""" - format = '!HH' - length = struct.calcsize(format) - for i in range(0, self.numQuestions): - name = self.readName() - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - - question = DNSQuestion(name, info[0], info[1]) - self.questions.append(question) - - def readInt(self): - """Reads an integer from the packet""" - format = '!I' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - return info[0] - - def readCharacterString(self): - """Reads a character string from the packet""" - length = ord(self.data[self.offset]) - self.offset += 1 - return self.readString(length) - - def readString(self, len): - """Reads a string of a given length from the packet""" - format = '!' + str(len) + 's' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - return info[0] - - def readUnsignedShort(self): - """Reads an unsigned short from the packet""" - format = '!H' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - return info[0] - - def readOthers(self): - """Reads the answers, authorities and additionals section of the packet""" - format = '!HHiH' - length = struct.calcsize(format) - n = self.numAnswers + self.numAuthorities + self.numAdditionals - for i in range(0, n): - domain = self.readName() - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - - rec = None - if info[0] == _TYPE_A: - rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(4)) - elif info[0] == _TYPE_CNAME or info[0] == _TYPE_PTR: - rec = DNSPointer(domain, info[0], info[1], info[2], self.readName()) - elif info[0] == _TYPE_TXT: - rec = DNSText(domain, info[0], info[1], info[2], self.readString(info[3])) - elif info[0] == _TYPE_SRV: - rec = DNSService(domain, info[0], info[1], info[2], self.readUnsignedShort(), self.readUnsignedShort(), self.readUnsignedShort(), self.readName()) - elif info[0] == _TYPE_HINFO: - rec = DNSHinfo(domain, info[0], info[1], info[2], self.readCharacterString(), self.readCharacterString()) - elif info[0] == _TYPE_AAAA: - rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16)) - else: - # Try to ignore types we don't know about - # this may mean the rest of the name is - # unable to be parsed, and may show errors - # so this is left for debugging. New types - # encountered need to be parsed properly. - # - #print "UNKNOWN TYPE = " + str(info[0]) - #raise BadTypeInNameException - pass - - if rec is not None: - self.answers.append(rec) - - def isQuery(self): - """Returns true if this is a query""" - return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY - - def isResponse(self): - """Returns true if this is a response""" - return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE - - def readUTF(self, offset, len): - """Reads a UTF-8 string of a given length from the packet""" - result = self.data[offset:offset+len].decode('utf-8') - return result - - def readName(self): - """Reads a domain name from the packet""" - result = '' - off = self.offset - next = -1 - first = off - - while 1: - len = ord(self.data[off]) - off += 1 - if len == 0: - break - t = len & 0xC0 - if t == 0x00: - result = ''.join((result, self.readUTF(off, len) + '.')) - off += len - elif t == 0xC0: - if next < 0: - next = off + 1 - off = ((len & 0x3F) << 8) | ord(self.data[off]) - if off >= first: - raise "Bad domain name (circular) at " + str(off) - first = off - else: - raise "Bad domain name at " + str(off) - - if next >= 0: - self.offset = next - else: - self.offset = off - - return result - - + """Object representation of an incoming DNS packet""" + + def __init__(self, data): + """Constructor from string holding bytes of packet""" + self.offset = 0 + self.data = data + self.questions = [] + self.answers = [] + self.numQuestions = 0 + self.numAnswers = 0 + self.numAuthorities = 0 + self.numAdditionals = 0 + + self.readHeader() + self.readQuestions() + self.readOthers() + + def readHeader(self): + """Reads header portion of packet""" + format = '!HHHHHH' + length = struct.calcsize(format) + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + + self.id = info[0] + self.flags = info[1] + self.numQuestions = info[2] + self.numAnswers = info[3] + self.numAuthorities = info[4] + self.numAdditionals = info[5] + + def readQuestions(self): + """Reads questions section of packet""" + format = '!HH' + length = struct.calcsize(format) + for i in range(0, self.numQuestions): + name = self.readName() + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + + question = DNSQuestion(name, info[0], info[1]) + self.questions.append(question) + + def readInt(self): + """Reads an integer from the packet""" + format = '!I' + length = struct.calcsize(format) + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + return info[0] + + def readCharacterString(self): + """Reads a character string from the packet""" + length = ord(self.data[self.offset]) + self.offset += 1 + return self.readString(length) + + def readString(self, len): + """Reads a string of a given length from the packet""" + format = '!' + str(len) + 's' + length = struct.calcsize(format) + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + return info[0] + + def readUnsignedShort(self): + """Reads an unsigned short from the packet""" + format = '!H' + length = struct.calcsize(format) + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + return info[0] + + def readOthers(self): + """Reads the answers, authorities and additionals section of the packet""" + format = '!HHiH' + length = struct.calcsize(format) + n = self.numAnswers + self.numAuthorities + self.numAdditionals + for i in range(0, n): + domain = self.readName() + info = struct.unpack(format, self.data[self.offset:self.offset+length]) + self.offset += length + + rec = None + if info[0] == _TYPE_A: + rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(4)) + elif info[0] == _TYPE_CNAME or info[0] == _TYPE_PTR: + rec = DNSPointer(domain, info[0], info[1], info[2], self.readName()) + elif info[0] == _TYPE_TXT: + rec = DNSText(domain, info[0], info[1], info[2], self.readString(info[3])) + elif info[0] == _TYPE_SRV: + rec = DNSService(domain, info[0], info[1], info[2], self.readUnsignedShort(), self.readUnsignedShort(), self.readUnsignedShort(), self.readName()) + elif info[0] == _TYPE_HINFO: + rec = DNSHinfo(domain, info[0], info[1], info[2], self.readCharacterString(), self.readCharacterString()) + elif info[0] == _TYPE_AAAA: + rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16)) + else: + # Try to ignore types we don't know about + # this may mean the rest of the name is + # unable to be parsed, and may show errors + # so this is left for debugging. New types + # encountered need to be parsed properly. + # + #print "UNKNOWN TYPE = " + str(info[0]) + #raise BadTypeInNameException + pass + + if rec is not None: + self.answers.append(rec) + + def isQuery(self): + """Returns true if this is a query""" + return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY + + def isResponse(self): + """Returns true if this is a response""" + return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE + + def readUTF(self, offset, len): + """Reads a UTF-8 string of a given length from the packet""" + result = self.data[offset:offset+len].decode('utf-8') + return result + + def readName(self): + """Reads a domain name from the packet""" + result = '' + off = self.offset + next = -1 + first = off + + while 1: + len = ord(self.data[off]) + off += 1 + if len == 0: + break + t = len & 0xC0 + if t == 0x00: + result = ''.join((result, self.readUTF(off, len) + '.')) + off += len + elif t == 0xC0: + if next < 0: + next = off + 1 + off = ((len & 0x3F) << 8) | ord(self.data[off]) + if off >= first: + raise "Bad domain name (circular) at " + str(off) + first = off + else: + raise "Bad domain name at " + str(off) + + if next >= 0: + self.offset = next + else: + self.offset = off + + return result + + class DNSOutgoing(object): - """Object representation of an outgoing packet""" - - def __init__(self, flags, multicast = 1): - self.finished = 0 - self.id = 0 - self.multicast = multicast - self.flags = flags - self.names = {} - self.data = [] - self.size = 12 - - self.questions = [] - self.answers = [] - self.authorities = [] - self.additionals = [] - - def addQuestion(self, record): - """Adds a question""" - self.questions.append(record) - - def addAnswer(self, inp, record): - """Adds an answer""" - if not record.suppressedBy(inp): - self.addAnswerAtTime(record, 0) - - def addAnswerAtTime(self, record, now): - """Adds an answer if if does not expire by a certain time""" - if record is not None: - if now == 0 or not record.isExpired(now): - self.answers.append((record, now)) - - def addAuthorativeAnswer(self, record): - """Adds an authoritative answer""" - self.authorities.append(record) - - def addAdditionalAnswer(self, record): - """Adds an additional answer""" - self.additionals.append(record) - - def writeByte(self, value): - """Writes a single byte to the packet""" - format = '!c' - self.data.append(struct.pack(format, chr(value))) - self.size += 1 - - def insertShort(self, index, value): - """Inserts an unsigned short in a certain position in the packet""" - format = '!H' - self.data.insert(index, struct.pack(format, value)) - self.size += 2 - - def writeShort(self, value): - """Writes an unsigned short to the packet""" - format = '!H' - self.data.append(struct.pack(format, value)) - self.size += 2 - - def writeInt(self, value): - """Writes an unsigned integer to the packet""" - format = '!I' - self.data.append(struct.pack(format, int(value))) - self.size += 4 - - def writeString(self, value, length): - """Writes a string to the packet""" - format = '!' + str(length) + 's' - self.data.append(struct.pack(format, value)) - self.size += length - - def writeUTF(self, s): - """Writes a UTF-8 string of a given length to the packet""" - utfstr = s.encode('utf-8') - length = len(utfstr) - if length > 64: - raise NamePartTooLongException - self.writeByte(length) - self.writeString(utfstr, length) - - def writeName(self, name): - """Writes a domain name to the packet""" - - try: - # Find existing instance of this name in packet - # - index = self.names[name] - except KeyError: - # No record of this name already, so write it - # out as normal, recording the location of the name - # for future pointers to it. - # - self.names[name] = self.size - parts = name.split('.') - if parts[-1] == '': - parts = parts[:-1] - for part in parts: - self.writeUTF(part) - self.writeByte(0) - return - - # An index was found, so write a pointer to it - # - self.writeByte((index >> 8) | 0xC0) - self.writeByte(index) - - def writeQuestion(self, question): - """Writes a question to the packet""" - self.writeName(question.name) - self.writeShort(question.type) - self.writeShort(question.clazz) - - def writeRecord(self, record, now): - """Writes a record (answer, authoritative answer, additional) to - the packet""" - self.writeName(record.name) - self.writeShort(record.type) - if record.unique and self.multicast: - self.writeShort(record.clazz | _CLASS_UNIQUE) - else: - self.writeShort(record.clazz) - if now == 0: - self.writeInt(record.ttl) - else: - self.writeInt(record.getRemainingTTL(now)) - index = len(self.data) - # Adjust size for the short we will write before this record - # - self.size += 2 - record.write(self) - self.size -= 2 - - length = len(''.join(self.data[index:])) - self.insertShort(index, length) # Here is the short we adjusted for - - def packet(self): - """Returns a string containing the packet's bytes - - No further parts should be added to the packet once this - is done.""" - if not self.finished: - self.finished = 1 - for question in self.questions: - self.writeQuestion(question) - for answer, time in self.answers: - self.writeRecord(answer, time) - for authority in self.authorities: - self.writeRecord(authority, 0) - for additional in self.additionals: - self.writeRecord(additional, 0) - - self.insertShort(0, len(self.additionals)) - self.insertShort(0, len(self.authorities)) - self.insertShort(0, len(self.answers)) - self.insertShort(0, len(self.questions)) - self.insertShort(0, self.flags) - if self.multicast: - self.insertShort(0, 0) - else: - self.insertShort(0, self.id) - return ''.join(self.data) + """Object representation of an outgoing packet""" + + def __init__(self, flags, multicast = 1): + self.finished = 0 + self.id = 0 + self.multicast = multicast + self.flags = flags + self.names = {} + self.data = [] + self.size = 12 + + self.questions = [] + self.answers = [] + self.authorities = [] + self.additionals = [] + + def addQuestion(self, record): + """Adds a question""" + self.questions.append(record) + + def addAnswer(self, inp, record): + """Adds an answer""" + if not record.suppressedBy(inp): + self.addAnswerAtTime(record, 0) + + def addAnswerAtTime(self, record, now): + """Adds an answer if if does not expire by a certain time""" + if record is not None: + if now == 0 or not record.isExpired(now): + self.answers.append((record, now)) + + def addAuthorativeAnswer(self, record): + """Adds an authoritative answer""" + self.authorities.append(record) + + def addAdditionalAnswer(self, record): + """Adds an additional answer""" + self.additionals.append(record) + + def writeByte(self, value): + """Writes a single byte to the packet""" + format = '!c' + self.data.append(struct.pack(format, chr(value))) + self.size += 1 + + def insertShort(self, index, value): + """Inserts an unsigned short in a certain position in the packet""" + format = '!H' + self.data.insert(index, struct.pack(format, value)) + self.size += 2 + + def writeShort(self, value): + """Writes an unsigned short to the packet""" + format = '!H' + self.data.append(struct.pack(format, value)) + self.size += 2 + + def writeInt(self, value): + """Writes an unsigned integer to the packet""" + format = '!I' + self.data.append(struct.pack(format, int(value))) + self.size += 4 + + def writeString(self, value, length): + """Writes a string to the packet""" + format = '!' + str(length) + 's' + self.data.append(struct.pack(format, value)) + self.size += length + + def writeUTF(self, s): + """Writes a UTF-8 string of a given length to the packet""" + utfstr = s.encode('utf-8') + length = len(utfstr) + if length > 64: + raise NamePartTooLongException + self.writeByte(length) + self.writeString(utfstr, length) + + def writeName(self, name): + """Writes a domain name to the packet""" + + try: + # Find existing instance of this name in packet + # + index = self.names[name] + except KeyError: + # No record of this name already, so write it + # out as normal, recording the location of the name + # for future pointers to it. + # + self.names[name] = self.size + parts = name.split('.') + if parts[-1] == '': + parts = parts[:-1] + for part in parts: + self.writeUTF(part) + self.writeByte(0) + return + + # An index was found, so write a pointer to it + # + self.writeByte((index >> 8) | 0xC0) + self.writeByte(index) + + def writeQuestion(self, question): + """Writes a question to the packet""" + self.writeName(question.name) + self.writeShort(question.type) + self.writeShort(question.clazz) + + def writeRecord(self, record, now): + """Writes a record (answer, authoritative answer, additional) to + the packet""" + self.writeName(record.name) + self.writeShort(record.type) + if record.unique and self.multicast: + self.writeShort(record.clazz | _CLASS_UNIQUE) + else: + self.writeShort(record.clazz) + if now == 0: + self.writeInt(record.ttl) + else: + self.writeInt(record.getRemainingTTL(now)) + index = len(self.data) + # Adjust size for the short we will write before this record + # + self.size += 2 + record.write(self) + self.size -= 2 + + length = len(''.join(self.data[index:])) + self.insertShort(index, length) # Here is the short we adjusted for + + def packet(self): + """Returns a string containing the packet's bytes + + No further parts should be added to the packet once this + is done.""" + if not self.finished: + self.finished = 1 + for question in self.questions: + self.writeQuestion(question) + for answer, time in self.answers: + self.writeRecord(answer, time) + for authority in self.authorities: + self.writeRecord(authority, 0) + for additional in self.additionals: + self.writeRecord(additional, 0) + + self.insertShort(0, len(self.additionals)) + self.insertShort(0, len(self.authorities)) + self.insertShort(0, len(self.answers)) + self.insertShort(0, len(self.questions)) + self.insertShort(0, self.flags) + if self.multicast: + self.insertShort(0, 0) + else: + self.insertShort(0, self.id) + return ''.join(self.data) class DNSCache(object): - """A cache of DNS entries""" - - def __init__(self): - self.cache = {} - - def add(self, entry): - """Adds an entry""" - try: - list = self.cache[entry.key] - except: - list = self.cache[entry.key] = [] - list.append(entry) - - def remove(self, entry): - """Removes an entry""" - try: - list = self.cache[entry.key] - list.remove(entry) - except: - pass - - def get(self, entry): - """Gets an entry by key. Will return None if there is no - matching entry.""" - try: - list = self.cache[entry.key] - return list[list.index(entry)] - except: - return None - - def getByDetails(self, name, type, clazz): - """Gets an entry by details. Will return None if there is - no matching entry.""" - entry = DNSEntry(name, type, clazz) - return self.get(entry) - - def entriesWithName(self, name): - """Returns a list of entries whose key matches the name.""" - try: - return self.cache[name] - except: - return [] - - def entries(self): - """Returns a list of all entries""" - def add(x, y): return x+y - try: - return reduce(add, self.cache.values()) - except: - return [] + """A cache of DNS entries""" + + def __init__(self): + self.cache = {} + + def add(self, entry): + """Adds an entry""" + try: + list = self.cache[entry.key] + except: + list = self.cache[entry.key] = [] + list.append(entry) + + def remove(self, entry): + """Removes an entry""" + try: + list = self.cache[entry.key] + list.remove(entry) + except: + pass + + def get(self, entry): + """Gets an entry by key. Will return None if there is no + matching entry.""" + try: + list = self.cache[entry.key] + return list[list.index(entry)] + except: + return None + + def getByDetails(self, name, type, clazz): + """Gets an entry by details. Will return None if there is + no matching entry.""" + entry = DNSEntry(name, type, clazz) + return self.get(entry) + + def entriesWithName(self, name): + """Returns a list of entries whose key matches the name.""" + try: + return self.cache[name] + except: + return [] + + def entries(self): + """Returns a list of all entries""" + def add(x, y): return x+y + try: + return reduce(add, self.cache.values()) + except: + return [] class Engine(threading.Thread): - """An engine wraps read access to sockets, allowing objects that - need to receive data from sockets to be called back when the - sockets are ready. - - A reader needs a handle_read() method, which is called when the socket - it is interested in is ready for reading. - - Writers are not implemented here, because we only send short - packets. - """ - - def __init__(self, zeroconf): - threading.Thread.__init__(self) - self.zeroconf = zeroconf - self.readers = {} # maps socket to reader - self.timeout = 5 - self.condition = threading.Condition() - self.start() - - def run(self): - while not globals()['_GLOBAL_DONE']: - rs = self.getReaders() - if len(rs) == 0: - # No sockets to manage, but we wait for the timeout - # or addition of a socket - # - self.condition.acquire() - self.condition.wait(self.timeout) - self.condition.release() - else: - try: - rr, wr, er = select.select(rs, [], [], self.timeout) - for socket in rr: - try: - self.readers[socket].handle_read() - except: - traceback.print_exc() - except: - pass - - def getReaders(self): - result = [] - self.condition.acquire() - result = self.readers.keys() - self.condition.release() - return result - - def addReader(self, reader, socket): - self.condition.acquire() - self.readers[socket] = reader - self.condition.notify() - self.condition.release() - - def delReader(self, socket): - self.condition.acquire() - del(self.readers[socket]) - self.condition.notify() - self.condition.release() - - def notify(self): - self.condition.acquire() - self.condition.notify() - self.condition.release() + """An engine wraps read access to sockets, allowing objects that + need to receive data from sockets to be called back when the + sockets are ready. + + A reader needs a handle_read() method, which is called when the socket + it is interested in is ready for reading. + + Writers are not implemented here, because we only send short + packets. + """ + + def __init__(self, zeroconf): + threading.Thread.__init__(self) + self.zeroconf = zeroconf + self.readers = {} # maps socket to reader + self.timeout = 5 + self.condition = threading.Condition() + self.start() + + def run(self): + while not globals()['_GLOBAL_DONE']: + rs = self.getReaders() + if len(rs) == 0: + # No sockets to manage, but we wait for the timeout + # or addition of a socket + # + self.condition.acquire() + self.condition.wait(self.timeout) + self.condition.release() + else: + try: + rr, wr, er = select.select(rs, [], [], self.timeout) + for socket in rr: + try: + self.readers[socket].handle_read() + except: + traceback.print_exc() + except: + pass + + def getReaders(self): + result = [] + self.condition.acquire() + result = self.readers.keys() + self.condition.release() + return result + + def addReader(self, reader, socket): + self.condition.acquire() + self.readers[socket] = reader + self.condition.notify() + self.condition.release() + + def delReader(self, socket): + self.condition.acquire() + del(self.readers[socket]) + self.condition.notify() + self.condition.release() + + def notify(self): + self.condition.acquire() + self.condition.notify() + self.condition.release() class Listener(object): - """A Listener is used by this module to listen on the multicast - group to which DNS messages are sent, allowing the implementation - to cache information as it arrives. - - It requires registration with an Engine object in order to have - the read() method called when a socket is availble for reading.""" - - def __init__(self, zeroconf): - self.zeroconf = zeroconf - self.zeroconf.engine.addReader(self, self.zeroconf.socket) - - def handle_read(self): - data, (addr, port) = self.zeroconf.socket.recvfrom(_MAX_MSG_ABSOLUTE) - self.data = data - msg = DNSIncoming(data) - if msg.isQuery(): - # Always multicast responses - # - if port == _MDNS_PORT: - self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) - # If it's not a multicast query, reply via unicast - # and multicast - # - elif port == _DNS_PORT: - self.zeroconf.handleQuery(msg, addr, port) - self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) - else: - self.zeroconf.handleResponse(msg) + """A Listener is used by this module to listen on the multicast + group to which DNS messages are sent, allowing the implementation + to cache information as it arrives. + + It requires registration with an Engine object in order to have + the read() method called when a socket is availble for reading.""" + + def __init__(self, zeroconf): + self.zeroconf = zeroconf + self.zeroconf.engine.addReader(self, self.zeroconf.socket) + + def handle_read(self): + data, (addr, port) = self.zeroconf.socket.recvfrom(_MAX_MSG_ABSOLUTE) + self.data = data + msg = DNSIncoming(data) + if msg.isQuery(): + # Always multicast responses + # + if port == _MDNS_PORT: + self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) + # If it's not a multicast query, reply via unicast + # and multicast + # + elif port == _DNS_PORT: + self.zeroconf.handleQuery(msg, addr, port) + self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) + else: + self.zeroconf.handleResponse(msg) class Reaper(threading.Thread): - """A Reaper is used by this module to remove cache entries that - have expired.""" - - def __init__(self, zeroconf): - threading.Thread.__init__(self) - self.zeroconf = zeroconf - self.start() - - def run(self): - while 1: - self.zeroconf.wait(10 * 1000) - if globals()['_GLOBAL_DONE']: - return - now = currentTimeMillis() - for record in self.zeroconf.cache.entries(): - if record.isExpired(now): - self.zeroconf.updateRecord(now, record) - self.zeroconf.cache.remove(record) + """A Reaper is used by this module to remove cache entries that + have expired.""" + + def __init__(self, zeroconf): + threading.Thread.__init__(self) + self.zeroconf = zeroconf + self.start() + + def run(self): + while 1: + self.zeroconf.wait(10 * 1000) + if globals()['_GLOBAL_DONE']: + return + now = currentTimeMillis() + for record in self.zeroconf.cache.entries(): + if record.isExpired(now): + self.zeroconf.updateRecord(now, record) + self.zeroconf.cache.remove(record) class ServiceBrowser(threading.Thread): - """Used to browse for a service of a specific type. - - The listener object will have its addService() and - removeService() methods called when this browser - discovers changes in the services availability.""" - - def __init__(self, zeroconf, type, listener): - """Creates a browser for a specific type""" - threading.Thread.__init__(self) - self.zeroconf = zeroconf - self.type = type - self.listener = listener - self.services = {} - self.nextTime = currentTimeMillis() - self.delay = _BROWSER_TIME - self.list = [] - - self.done = 0 - - self.zeroconf.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) - self.start() - - def updateRecord(self, zeroconf, now, record): - """Callback invoked by Zeroconf when new information arrives. - - Updates information required by browser in the Zeroconf cache.""" - if record.type == _TYPE_PTR and record.name == self.type: - expired = record.isExpired(now) - try: - oldrecord = self.services[record.alias.lower()] - if not expired: - oldrecord.resetTTL(record) - else: - del(self.services[record.alias.lower()]) - callback = lambda x: self.listener.removeService(x, self.type, record.alias) - self.list.append(callback) - return - except: - if not expired: - self.services[record.alias.lower()] = record - callback = lambda x: self.listener.addService(x, self.type, record.alias) - self.list.append(callback) - - expires = record.getExpirationTime(75) - if expires < self.nextTime: - self.nextTime = expires - - def cancel(self): - self.done = 1 - self.zeroconf.notifyAll() - - def run(self): - while 1: - event = None - now = currentTimeMillis() - if len(self.list) == 0 and self.nextTime > now: - self.zeroconf.wait(self.nextTime - now) - if globals()['_GLOBAL_DONE'] or self.done: - return - now = currentTimeMillis() - - if self.nextTime <= now: - out = DNSOutgoing(_FLAGS_QR_QUERY) - out.addQuestion(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) - for record in self.services.values(): - if not record.isExpired(now): - out.addAnswerAtTime(record, now) - self.zeroconf.send(out) - self.nextTime = now + self.delay - self.delay = min(20 * 1000, self.delay * 2) - - if len(self.list) > 0: - event = self.list.pop(0) - - if event is not None: - event(self.zeroconf) - + """Used to browse for a service of a specific type. + + The listener object will have its addService() and + removeService() methods called when this browser + discovers changes in the services availability.""" + + def __init__(self, zeroconf, type, listener): + """Creates a browser for a specific type""" + threading.Thread.__init__(self) + self.zeroconf = zeroconf + self.type = type + self.listener = listener + self.services = {} + self.nextTime = currentTimeMillis() + self.delay = _BROWSER_TIME + self.list = [] + + self.done = 0 + + self.zeroconf.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) + self.start() + + def updateRecord(self, zeroconf, now, record): + """Callback invoked by Zeroconf when new information arrives. + + Updates information required by browser in the Zeroconf cache.""" + if record.type == _TYPE_PTR and record.name == self.type: + expired = record.isExpired(now) + try: + oldrecord = self.services[record.alias.lower()] + if not expired: + oldrecord.resetTTL(record) + else: + del(self.services[record.alias.lower()]) + callback = lambda x: self.listener.removeService(x, self.type, record.alias) + self.list.append(callback) + return + except: + if not expired: + self.services[record.alias.lower()] = record + callback = lambda x: self.listener.addService(x, self.type, record.alias) + self.list.append(callback) + + expires = record.getExpirationTime(75) + if expires < self.nextTime: + self.nextTime = expires + + def cancel(self): + self.done = 1 + self.zeroconf.notifyAll() + + def run(self): + while 1: + event = None + now = currentTimeMillis() + if len(self.list) == 0 and self.nextTime > now: + self.zeroconf.wait(self.nextTime - now) + if globals()['_GLOBAL_DONE'] or self.done: + return + now = currentTimeMillis() + + if self.nextTime <= now: + out = DNSOutgoing(_FLAGS_QR_QUERY) + out.addQuestion(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) + for record in self.services.values(): + if not record.isExpired(now): + out.addAnswerAtTime(record, now) + self.zeroconf.send(out) + self.nextTime = now + self.delay + self.delay = min(20 * 1000, self.delay * 2) + + if len(self.list) > 0: + event = self.list.pop(0) + + if event is not None: + event(self.zeroconf) + class ServiceInfo(object): - """Service information""" - - def __init__(self, type, name, address=None, port=None, weight=0, priority=0, properties=None, server=None): - """Create a service description. - - type: fully qualified service type name - name: fully qualified service name - address: IP address as unsigned short, network byte order - port: port that the service runs on - weight: weight of the service - priority: priority of the service - properties: dictionary of properties (or a string holding the bytes for the text field) - server: fully qualified name for service host (defaults to name)""" - - if not name.endswith(type): - raise BadTypeInNameException - self.type = type - self.name = name - self.address = address - self.port = port - self.weight = weight - self.priority = priority - if server: - self.server = server - else: - self.server = name - self.setProperties(properties) - - def setProperties(self, properties): - """Sets properties and text of this info from a dictionary""" - if isinstance(properties, dict): - self.properties = properties - list = [] - result = '' - for key in properties: - value = properties[key] - if value is None: - suffix = ''.encode('utf-8') - elif isinstance(value, str): - suffix = value.encode('utf-8') - elif isinstance(value, int): - if value: - suffix = 'true' - else: - suffix = 'false' - else: - suffix = ''.encode('utf-8') - list.append('='.join((key, suffix))) - for item in list: - result = ''.join((result, struct.pack('!c', chr(len(item))), item)) - self.text = result - else: - self.text = properties - - def setText(self, text): - """Sets properties and text given a text field""" - self.text = text - try: - result = {} - end = len(text) - index = 0 - strs = [] - while index < end: - length = ord(text[index]) - index += 1 - strs.append(text[index:index+length]) - index += length - - for s in strs: - eindex = s.find('=') - if eindex == -1: - # No equals sign at all - key = s - value = 0 - else: - key = s[:eindex] - value = s[eindex+1:] - if value == 'true': - value = 1 - elif value == 'false' or not value: - value = 0 - - # Only update non-existent properties - if key and result.get(key) == None: - result[key] = value - - self.properties = result - except: - traceback.print_exc() - self.properties = None - - def getType(self): - """Type accessor""" - return self.type - - def getName(self): - """Name accessor""" - if self.type is not None and self.name.endswith("." + self.type): - return self.name[:len(self.name) - len(self.type) - 1] - return self.name - - def getAddress(self): - """Address accessor""" - return self.address - - def getPort(self): - """Port accessor""" - return self.port - - def getPriority(self): - """Pirority accessor""" - return self.priority - - def getWeight(self): - """Weight accessor""" - return self.weight - - def getProperties(self): - """Properties accessor""" - return self.properties - - def getText(self): - """Text accessor""" - return self.text - - def getServer(self): - """Server accessor""" - return self.server - - def updateRecord(self, zeroconf, now, record): - """Updates service information from a DNS record""" - if record is not None and not record.isExpired(now): - if record.type == _TYPE_A: - #if record.name == self.name: - if record.name == self.server: - self.address = record.address - elif record.type == _TYPE_SRV: - if record.name == self.name: - self.server = record.server - self.port = record.port - self.weight = record.weight - self.priority = record.priority - #self.address = None - self.updateRecord(zeroconf, now, zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) - elif record.type == _TYPE_TXT: - if record.name == self.name: - self.setText(record.text) - - def request(self, zeroconf, timeout): - """Returns true if the service could be discovered on the - network, and updates this object with details discovered. - """ - now = currentTimeMillis() - delay = _LISTENER_TIME - next = now + delay - last = now + timeout - result = 0 - try: - zeroconf.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) - while self.server is None or self.address is None or self.text is None: - if last <= now: - return 0 - if next <= now: - out = DNSOutgoing(_FLAGS_QR_QUERY) - out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_SRV, _CLASS_IN), now) - out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_TXT, _CLASS_IN), now) - if self.server is not None: - out.addQuestion(DNSQuestion(self.server, _TYPE_A, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN), now) - zeroconf.send(out) - next = now + delay - delay = delay * 2 - - zeroconf.wait(min(next, last) - now) - now = currentTimeMillis() - result = 1 - finally: - zeroconf.removeListener(self) - - return result - - def __eq__(self, other): - """Tests equality of service name""" - if isinstance(other, ServiceInfo): - return other.name == self.name - return 0 - - def __ne__(self, other): - """Non-equality test""" - return not self.__eq__(other) - - def __repr__(self): - """String representation""" - result = "service[%s,%s:%s," % (self.name, socket.inet_ntoa(self.getAddress()), self.port) - if self.text is None: - result += "None" - else: - if len(self.text) < 20: - result += self.text - else: - result += self.text[:17] + "..." - result += "]" - return result - + """Service information""" + + def __init__(self, type, name, address=None, port=None, weight=0, priority=0, properties=None, server=None): + """Create a service description. + + type: fully qualified service type name + name: fully qualified service name + address: IP address as unsigned short, network byte order + port: port that the service runs on + weight: weight of the service + priority: priority of the service + properties: dictionary of properties (or a string holding the bytes for the text field) + server: fully qualified name for service host (defaults to name)""" + + if not name.endswith(type): + raise BadTypeInNameException + self.type = type + self.name = name + self.address = address + self.port = port + self.weight = weight + self.priority = priority + if server: + self.server = server + else: + self.server = name + self.setProperties(properties) + + def setProperties(self, properties): + """Sets properties and text of this info from a dictionary""" + if isinstance(properties, dict): + self.properties = properties + list = [] + result = '' + for key in properties: + value = properties[key] + if value is None: + suffix = ''.encode('utf-8') + elif isinstance(value, str): + suffix = value.encode('utf-8') + elif isinstance(value, int): + if value: + suffix = 'true' + else: + suffix = 'false' + else: + suffix = ''.encode('utf-8') + list.append('='.join((key, suffix))) + for item in list: + result = ''.join((result, struct.pack('!c', chr(len(item))), item)) + self.text = result + else: + self.text = properties + + def setText(self, text): + """Sets properties and text given a text field""" + self.text = text + try: + result = {} + end = len(text) + index = 0 + strs = [] + while index < end: + length = ord(text[index]) + index += 1 + strs.append(text[index:index+length]) + index += length + + for s in strs: + eindex = s.find('=') + if eindex == -1: + # No equals sign at all + key = s + value = 0 + else: + key = s[:eindex] + value = s[eindex+1:] + if value == 'true': + value = 1 + elif value == 'false' or not value: + value = 0 + + # Only update non-existent properties + if key and result.get(key) == None: + result[key] = value + + self.properties = result + except: + traceback.print_exc() + self.properties = None + + def getType(self): + """Type accessor""" + return self.type + + def getName(self): + """Name accessor""" + if self.type is not None and self.name.endswith("." + self.type): + return self.name[:len(self.name) - len(self.type) - 1] + return self.name + + def getAddress(self): + """Address accessor""" + return self.address + + def getPort(self): + """Port accessor""" + return self.port + + def getPriority(self): + """Pirority accessor""" + return self.priority + + def getWeight(self): + """Weight accessor""" + return self.weight + + def getProperties(self): + """Properties accessor""" + return self.properties + + def getText(self): + """Text accessor""" + return self.text + + def getServer(self): + """Server accessor""" + return self.server + + def updateRecord(self, zeroconf, now, record): + """Updates service information from a DNS record""" + if record is not None and not record.isExpired(now): + if record.type == _TYPE_A: + #if record.name == self.name: + if record.name == self.server: + self.address = record.address + elif record.type == _TYPE_SRV: + if record.name == self.name: + self.server = record.server + self.port = record.port + self.weight = record.weight + self.priority = record.priority + #self.address = None + self.updateRecord(zeroconf, now, zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) + elif record.type == _TYPE_TXT: + if record.name == self.name: + self.setText(record.text) + + def request(self, zeroconf, timeout): + """Returns true if the service could be discovered on the + network, and updates this object with details discovered. + """ + now = currentTimeMillis() + delay = _LISTENER_TIME + next = now + delay + last = now + timeout + result = 0 + try: + zeroconf.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) + while self.server is None or self.address is None or self.text is None: + if last <= now: + return 0 + if next <= now: + out = DNSOutgoing(_FLAGS_QR_QUERY) + out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN)) + out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_SRV, _CLASS_IN), now) + out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN)) + out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_TXT, _CLASS_IN), now) + if self.server is not None: + out.addQuestion(DNSQuestion(self.server, _TYPE_A, _CLASS_IN)) + out.addAnswerAtTime(zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN), now) + zeroconf.send(out) + next = now + delay + delay = delay * 2 + + zeroconf.wait(min(next, last) - now) + now = currentTimeMillis() + result = 1 + finally: + zeroconf.removeListener(self) + + return result + + def __eq__(self, other): + """Tests equality of service name""" + if isinstance(other, ServiceInfo): + return other.name == self.name + return 0 + + def __ne__(self, other): + """Non-equality test""" + return not self.__eq__(other) + + def __repr__(self): + """String representation""" + result = "service[%s,%s:%s," % (self.name, socket.inet_ntoa(self.getAddress()), self.port) + if self.text is None: + result += "None" + else: + if len(self.text) < 20: + result += self.text + else: + result += self.text[:17] + "..." + result += "]" + return result + class Zeroconf(object): - """Implementation of Zeroconf Multicast DNS Service Discovery - - Supports registration, unregistration, queries and browsing. - """ - def __init__(self, bindaddress=None): - """Creates an instance of the Zeroconf class, establishing - multicast communications, listening and reaping threads.""" - globals()['_GLOBAL_DONE'] = 0 - if bindaddress is None: - self.intf = socket.gethostbyname(socket.gethostname()) - else: - self.intf = bindaddress - self.group = ('', _MDNS_PORT) - self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - try: - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) - except: - # SO_REUSEADDR should be equivalent to SO_REUSEPORT for - # multicast UDP sockets (p 731, "TCP/IP Illustrated, - # Volume 2"), but some BSD-derived systems require - # SO_REUSEPORT to be specified explicity. Also, not all - # versions of Python have SO_REUSEPORT available. So - # if you're on a BSD-based system, and haven't upgraded - # to Python 2.3 yet, you may find this library doesn't - # work as expected. - # - pass - self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 255) - self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1) - try: - self.socket.bind(self.group) - except: - # Some versions of linux raise an exception even though - # the SO_REUSE* options have been set, so ignore it - # - pass - #self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) - self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) - - self.listeners = [] - self.browsers = [] - self.services = {} - self.servicetypes = {} - - self.cache = DNSCache() - - self.condition = threading.Condition() - - self.engine = Engine(self) - self.listener = Listener(self) - self.reaper = Reaper(self) - - def isLoopback(self): - return self.intf.startswith("127.0.0.1") - - def isLinklocal(self): - return self.intf.startswith("169.254.") - - def wait(self, timeout): - """Calling thread waits for a given number of milliseconds or - until notified.""" - self.condition.acquire() - self.condition.wait(timeout/1000) - self.condition.release() - - def notifyAll(self): - """Notifies all waiting threads""" - self.condition.acquire() - self.condition.notifyAll() - self.condition.release() - - def getServiceInfo(self, type, name, timeout=3000): - """Returns network's service information for a particular - name and type, or None if no service matches by the timeout, - which defaults to 3 seconds.""" - info = ServiceInfo(type, name) - if info.request(self, timeout): - return info - return None - - def addServiceListener(self, type, listener): - """Adds a listener for a particular service type. This object - will then have its updateRecord method called when information - arrives for that type.""" - self.removeServiceListener(listener) - self.browsers.append(ServiceBrowser(self, type, listener)) - - def removeServiceListener(self, listener): - """Removes a listener from the set that is currently listening.""" - for browser in self.browsers: - if browser.listener == listener: - browser.cancel() - del(browser) - - def registerService(self, info, ttl=_DNS_TTL): - """Registers service information to the network with a default TTL - of 60 seconds. Zeroconf will then respond to requests for - information for that service. The name of the service may be - changed if needed to make it unique on the network.""" - self.checkService(info) - self.services[info.name.lower()] = info - if info.type in self.servicetypes: - self.servicetypes[info.type]+=1 - else: - self.servicetypes[info.type]=1 - now = currentTimeMillis() - nextTime = now - i = 0 - while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, ttl, info.priority, info.weight, info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0) - if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, ttl, info.address), 0) - self.send(out) - i += 1 - nextTime += _REGISTER_TIME - - def unregisterService(self, info): - """Unregister a service.""" - try: - del(self.services[info.name.lower()]) - if self.servicetypes[info.type]>1: - self.servicetypes[info.type]-=1 - else: - del self.servicetypes[info.type] - except: - pass - now = currentTimeMillis() - nextTime = now - i = 0 - while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.name), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) - if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) - self.send(out) - i += 1 - nextTime += _UNREGISTER_TIME - - def unregisterAllServices(self): - """Unregister all registered services.""" - if len(self.services) > 0: - now = currentTimeMillis() - nextTime = now - i = 0 - while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() - continue - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - for info in self.services.values(): - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) - if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) - self.send(out) - i += 1 - nextTime += _UNREGISTER_TIME - - def checkService(self, info): - """Checks the network for a unique service name, modifying the - ServiceInfo passed in if it is not unique.""" - now = currentTimeMillis() - nextTime = now - i = 0 - while i < 3: - for record in self.cache.entriesWithName(info.type): - if record.type == _TYPE_PTR and not record.isExpired(now) and record.alias == info.name: - if (info.name.find('.') < 0): - info.name = info.name + ".[" + info.address + ":" + info.port + "]." + info.type - self.checkService(info) - return - raise NonUniqueNameException - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() - continue - out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) - self.debug = out - out.addQuestion(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) - out.addAuthorativeAnswer(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, info.name)) - self.send(out) - i += 1 - nextTime += _CHECK_TIME - - def addListener(self, listener, question): - """Adds a listener for a given question. The listener will have - its updateRecord method called when information is available to - answer the question.""" - now = currentTimeMillis() - self.listeners.append(listener) - if question is not None: - for record in self.cache.entriesWithName(question.name): - if question.answeredBy(record) and not record.isExpired(now): - listener.updateRecord(self, now, record) - self.notifyAll() - - def removeListener(self, listener): - """Removes a listener.""" - try: - self.listeners.remove(listener) - self.notifyAll() - except: - pass - - def updateRecord(self, now, rec): - """Used to notify listeners of new information that has updated - a record.""" - for listener in self.listeners: - listener.updateRecord(self, now, rec) - self.notifyAll() - - def handleResponse(self, msg): - """Deal with incoming response packets. All answers - are held in the cache, and listeners are notified.""" - now = currentTimeMillis() - for record in msg.answers: - expired = record.isExpired(now) - if record in self.cache.entries(): - if expired: - self.cache.remove(record) - else: - entry = self.cache.get(record) - if entry is not None: - entry.resetTTL(record) - record = entry - else: - self.cache.add(record) - - self.updateRecord(now, record) - - def handleQuery(self, msg, addr, port): - """Deal with incoming query packets. Provides a response if - possible.""" - out = None - - # Support unicast client responses - # - if port != _MDNS_PORT: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, 0) - for question in msg.questions: - out.addQuestion(question) - - for question in msg.questions: - if question.type == _TYPE_PTR: - if question.name == "_services._dns-sd._udp.local.": - for stype in self.servicetypes.keys(): - if out is None: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswer(msg, DNSPointer("_services._dns-sd._udp.local.", _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) - for service in self.services.values(): - if question.name == service.type: - if out is None: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswer(msg, DNSPointer(service.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, service.name)) - else: - try: - if out is None: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - - # Answer A record queries for any service addresses we know - if question.type == _TYPE_A or question.type == _TYPE_ANY: - for service in self.services.values(): - if service.server == question.name.lower(): - out.addAnswer(msg, DNSAddress(question.name, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) - - service = self.services.get(question.name.lower(), None) - if not service: continue - - if question.type == _TYPE_SRV or question.type == _TYPE_ANY: - out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.priority, service.weight, service.port, service.server)) - if question.type == _TYPE_TXT or question.type == _TYPE_ANY: - out.addAnswer(msg, DNSText(question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.text)) - if question.type == _TYPE_SRV: - out.addAdditionalAnswer(DNSAddress(service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) - except: - traceback.print_exc() - - if out is not None and out.answers: - out.id = msg.id - self.send(out, addr, port) - - def send(self, out, addr = _MDNS_ADDR, port = _MDNS_PORT): - """Sends an outgoing packet.""" - # This is a quick test to see if we can parse the packets we generate - #temp = DNSIncoming(out.packet()) - try: - bytes_sent = self.socket.sendto(out.packet(), 0, (addr, port)) - except: - # Ignore this, it may be a temporary loss of network connection - pass - - def close(self): - """Ends the background threads, and prevent this instance from - servicing further queries.""" - if globals()['_GLOBAL_DONE'] == 0: - globals()['_GLOBAL_DONE'] = 1 - self.notifyAll() - self.engine.notify() - self.unregisterAllServices() - self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) - self.socket.close() - + """Implementation of Zeroconf Multicast DNS Service Discovery + + Supports registration, unregistration, queries and browsing. + """ + def __init__(self, bindaddress=None): + """Creates an instance of the Zeroconf class, establishing + multicast communications, listening and reaping threads.""" + globals()['_GLOBAL_DONE'] = 0 + if bindaddress is None: + self.intf = socket.gethostbyname(socket.gethostname()) + else: + self.intf = bindaddress + self.group = ('', _MDNS_PORT) + self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + except: + # SO_REUSEADDR should be equivalent to SO_REUSEPORT for + # multicast UDP sockets (p 731, "TCP/IP Illustrated, + # Volume 2"), but some BSD-derived systems require + # SO_REUSEPORT to be specified explicity. Also, not all + # versions of Python have SO_REUSEPORT available. So + # if you're on a BSD-based system, and haven't upgraded + # to Python 2.3 yet, you may find this library doesn't + # work as expected. + # + pass + self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 255) + self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1) + try: + self.socket.bind(self.group) + except: + # Some versions of linux raise an exception even though + # the SO_REUSE* options have been set, so ignore it + # + pass + #self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) + self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + + self.listeners = [] + self.browsers = [] + self.services = {} + self.servicetypes = {} + + self.cache = DNSCache() + + self.condition = threading.Condition() + + self.engine = Engine(self) + self.listener = Listener(self) + self.reaper = Reaper(self) + + def isLoopback(self): + return self.intf.startswith("127.0.0.1") + + def isLinklocal(self): + return self.intf.startswith("169.254.") + + def wait(self, timeout): + """Calling thread waits for a given number of milliseconds or + until notified.""" + self.condition.acquire() + self.condition.wait(timeout/1000) + self.condition.release() + + def notifyAll(self): + """Notifies all waiting threads""" + self.condition.acquire() + self.condition.notifyAll() + self.condition.release() + + def getServiceInfo(self, type, name, timeout=3000): + """Returns network's service information for a particular + name and type, or None if no service matches by the timeout, + which defaults to 3 seconds.""" + info = ServiceInfo(type, name) + if info.request(self, timeout): + return info + return None + + def addServiceListener(self, type, listener): + """Adds a listener for a particular service type. This object + will then have its updateRecord method called when information + arrives for that type.""" + self.removeServiceListener(listener) + self.browsers.append(ServiceBrowser(self, type, listener)) + + def removeServiceListener(self, listener): + """Removes a listener from the set that is currently listening.""" + for browser in self.browsers: + if browser.listener == listener: + browser.cancel() + del(browser) + + def registerService(self, info, ttl=_DNS_TTL): + """Registers service information to the network with a default TTL + of 60 seconds. Zeroconf will then respond to requests for + information for that service. The name of the service may be + changed if needed to make it unique on the network.""" + self.checkService(info) + self.services[info.name.lower()] = info + if info.type in self.servicetypes: + self.servicetypes[info.type]+=1 + else: + self.servicetypes[info.type]=1 + now = currentTimeMillis() + nextTime = now + i = 0 + while i < 3: + if now < nextTime: + self.wait(nextTime - now) + now = currentTimeMillis() + continue + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, ttl, info.priority, info.weight, info.port, info.server), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0) + if info.address: + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, ttl, info.address), 0) + self.send(out) + i += 1 + nextTime += _REGISTER_TIME + + def unregisterService(self, info): + """Unregister a service.""" + try: + del(self.services[info.name.lower()]) + if self.servicetypes[info.type]>1: + self.servicetypes[info.type]-=1 + else: + del self.servicetypes[info.type] + except: + pass + now = currentTimeMillis() + nextTime = now + i = 0 + while i < 3: + if now < nextTime: + self.wait(nextTime - now) + now = currentTimeMillis() + continue + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.name), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) + if info.address: + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) + self.send(out) + i += 1 + nextTime += _UNREGISTER_TIME + + def unregisterAllServices(self): + """Unregister all registered services.""" + if len(self.services) > 0: + now = currentTimeMillis() + nextTime = now + i = 0 + while i < 3: + if now < nextTime: + self.wait(nextTime - now) + now = currentTimeMillis() + continue + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + for info in self.services.values(): + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.server), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) + if info.address: + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) + self.send(out) + i += 1 + nextTime += _UNREGISTER_TIME + + def checkService(self, info): + """Checks the network for a unique service name, modifying the + ServiceInfo passed in if it is not unique.""" + now = currentTimeMillis() + nextTime = now + i = 0 + while i < 3: + for record in self.cache.entriesWithName(info.type): + if record.type == _TYPE_PTR and not record.isExpired(now) and record.alias == info.name: + if (info.name.find('.') < 0): + info.name = info.name + ".[" + info.address + ":" + info.port + "]." + info.type + self.checkService(info) + return + raise NonUniqueNameException + if now < nextTime: + self.wait(nextTime - now) + now = currentTimeMillis() + continue + out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) + self.debug = out + out.addQuestion(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) + out.addAuthorativeAnswer(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, info.name)) + self.send(out) + i += 1 + nextTime += _CHECK_TIME + + def addListener(self, listener, question): + """Adds a listener for a given question. The listener will have + its updateRecord method called when information is available to + answer the question.""" + now = currentTimeMillis() + self.listeners.append(listener) + if question is not None: + for record in self.cache.entriesWithName(question.name): + if question.answeredBy(record) and not record.isExpired(now): + listener.updateRecord(self, now, record) + self.notifyAll() + + def removeListener(self, listener): + """Removes a listener.""" + try: + self.listeners.remove(listener) + self.notifyAll() + except: + pass + + def updateRecord(self, now, rec): + """Used to notify listeners of new information that has updated + a record.""" + for listener in self.listeners: + listener.updateRecord(self, now, rec) + self.notifyAll() + + def handleResponse(self, msg): + """Deal with incoming response packets. All answers + are held in the cache, and listeners are notified.""" + now = currentTimeMillis() + for record in msg.answers: + expired = record.isExpired(now) + if record in self.cache.entries(): + if expired: + self.cache.remove(record) + else: + entry = self.cache.get(record) + if entry is not None: + entry.resetTTL(record) + record = entry + else: + self.cache.add(record) + + self.updateRecord(now, record) + + def handleQuery(self, msg, addr, port): + """Deal with incoming query packets. Provides a response if + possible.""" + out = None + + # Support unicast client responses + # + if port != _MDNS_PORT: + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, 0) + for question in msg.questions: + out.addQuestion(question) + + for question in msg.questions: + if question.type == _TYPE_PTR: + if question.name == "_services._dns-sd._udp.local.": + for stype in self.servicetypes.keys(): + if out is None: + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + out.addAnswer(msg, DNSPointer("_services._dns-sd._udp.local.", _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) + for service in self.services.values(): + if question.name == service.type: + if out is None: + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + out.addAnswer(msg, DNSPointer(service.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, service.name)) + else: + try: + if out is None: + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) + + # Answer A record queries for any service addresses we know + if question.type == _TYPE_A or question.type == _TYPE_ANY: + for service in self.services.values(): + if service.server == question.name.lower(): + out.addAnswer(msg, DNSAddress(question.name, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) + + service = self.services.get(question.name.lower(), None) + if not service: continue + + if question.type == _TYPE_SRV or question.type == _TYPE_ANY: + out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.priority, service.weight, service.port, service.server)) + if question.type == _TYPE_TXT or question.type == _TYPE_ANY: + out.addAnswer(msg, DNSText(question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.text)) + if question.type == _TYPE_SRV: + out.addAdditionalAnswer(DNSAddress(service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) + except: + traceback.print_exc() + + if out is not None and out.answers: + out.id = msg.id + self.send(out, addr, port) + + def send(self, out, addr = _MDNS_ADDR, port = _MDNS_PORT): + """Sends an outgoing packet.""" + # This is a quick test to see if we can parse the packets we generate + #temp = DNSIncoming(out.packet()) + try: + bytes_sent = self.socket.sendto(out.packet(), 0, (addr, port)) + except: + # Ignore this, it may be a temporary loss of network connection + pass + + def close(self): + """Ends the background threads, and prevent this instance from + servicing further queries.""" + if globals()['_GLOBAL_DONE'] == 0: + globals()['_GLOBAL_DONE'] = 1 + self.notifyAll() + self.engine.notify() + self.unregisterAllServices() + self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + self.socket.close() + # Test a few module features, including service registration, service # query (for Zoe), and service unregistration. -if __name__ == '__main__': - print "Multicast DNS Service Discovery for Python, version", __version__ - r = Zeroconf() - print "1. Testing registration of a service..." - desc = {'version':'0.10','a':'test value', 'b':'another value'} - info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) - print " Registering service..." - r.registerService(info) - print " Registration done." - print "2. Testing query of service information..." - print " Getting ZOE service:", str(r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local.")) - print " Query done." - print "3. Testing query of own service..." - print " Getting self:", str(r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.")) - print " Query done." - print "4. Testing unregister of service information..." - r.unregisterService(info) - print " Unregister done." - r.close() +if __name__ == '__main__': + print "Multicast DNS Service Discovery for Python, version", __version__ + r = Zeroconf() + print "1. Testing registration of a service..." + desc = {'version':'0.10','a':'test value', 'b':'another value'} + info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) + print " Registering service..." + r.registerService(info) + print " Registration done." + print "2. Testing query of service information..." + print " Getting ZOE service:", str(r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local.")) + print " Query done." + print "3. Testing query of own service..." + print " Getting self:", str(r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.")) + print " Query done." + print "4. Testing unregister of service information..." + r.unregisterService(info) + print " Unregister done." + r.close() From f80df7b0f8b9124970e109c51f7a49b7bd75906c Mon Sep 17 00:00:00 2001 From: William McBrine Date: Mon, 29 Jun 2009 02:56:49 -0400 Subject: [PATCH 005/163] This doesn't seem to be necessary, and it's generating a lot of exceptions... --- Zeroconf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 00708a66..706bf009 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -258,8 +258,8 @@ class DNSQuestion(DNSEntry): """A DNS question entry""" def __init__(self, name, type, clazz): - if not name.endswith(".local."): - raise NonLocalNameException + #if not name.endswith(".local."): + # raise NonLocalNameException DNSEntry.__init__(self, name, type, clazz) def answeredBy(self, rec): From 4c8aac95613df62d001bd7192ec75247a2bb9b9d Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 8 Sep 2009 01:24:46 -0400 Subject: [PATCH 006/163] Suppress EBADF errors in Zeroconf.py. --- Zeroconf.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Zeroconf.py b/Zeroconf.py index 706bf009..6f6cc0f9 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -902,7 +902,16 @@ def __init__(self, zeroconf): self.zeroconf.engine.addReader(self, self.zeroconf.socket) def handle_read(self): - data, (addr, port) = self.zeroconf.socket.recvfrom(_MAX_MSG_ABSOLUTE) + try: + data, (addr, port) = self.zeroconf.socket.recvfrom(_MAX_MSG_ABSOLUTE) + except socket.error, e: + # If the socket was closed by another thread -- which happens + # regularly on shutdown -- an EBADF exception is thrown here. + # Ignore it. + if e[0] == socket.EBADF: + return + else: + raise e self.data = data msg = DNSIncoming(data) if msg.isQuery(): From f76529c685868dcdb62b6477f15ecb1122310cc5 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Mon, 9 Nov 2009 12:25:58 -0500 Subject: [PATCH 007/163] Don't need the string module here. --- Zeroconf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 6f6cc0f9..20231668 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -78,7 +78,6 @@ __email__ = "paul at scott dash murphy dot com" __version__ = "0.12" -import string import time import struct import socket @@ -210,7 +209,7 @@ class DNSEntry(object): """A DNS entry""" def __init__(self, name, type, clazz): - self.key = string.lower(name) + self.key = name.lower() self.name = name self.type = type self.clazz = clazz & _CLASS_MASK From fe77e371cc68ea211508908e6180867c420ca042 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 29 Dec 2009 22:25:51 -0500 Subject: [PATCH 008/163] Some people are reporting bogus data coming back from Zeroconf scans, causing exceptions. --- Zeroconf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 20231668..e6c227a8 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -571,8 +571,7 @@ def isResponse(self): def readUTF(self, offset, len): """Reads a UTF-8 string of a given length from the packet""" - result = self.data[offset:offset+len].decode('utf-8') - return result + return unicode(self.data[offset:offset+len], 'utf-8', 'replace') def readName(self): """Reads a domain name from the packet""" From 488de8826ddd58646358900d057a4a1632492948 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Sun, 28 Feb 2010 15:45:27 -0500 Subject: [PATCH 009/163] Skip unknown resource records in Zeroconf -- https://bugs.launchpad.net/pyzeroconf/+bug/498411 --- Zeroconf.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index e6c227a8..84640777 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -549,14 +549,9 @@ def readOthers(self): rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16)) else: # Try to ignore types we don't know about - # this may mean the rest of the name is - # unable to be parsed, and may show errors - # so this is left for debugging. New types - # encountered need to be parsed properly. - # - #print "UNKNOWN TYPE = " + str(info[0]) - #raise BadTypeInNameException - pass + # Skip the payload for the resource record so the next + # records can be parsed correctly + self.offset += info[3] if rec is not None: self.answers.append(rec) From e814dd1e6848d8c7ec03660d347ea4a34390c37d Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 18 Jan 2011 00:24:51 -0500 Subject: [PATCH 010/163] Minor simplification of the pack/unpack routines in Zeroconf. --- Zeroconf.py | 75 +++++++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 46 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 84640777..359712ec 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -340,7 +340,7 @@ def __init__(self, name, type, clazz, ttl, address): def write(self, out): """Used in constructing an outgoing packet""" - out.writeString(self.address, len(self.address)) + out.writeString(self.address) def __eq__(self, other): """Tests equality on address""" @@ -365,8 +365,8 @@ def __init__(self, name, type, clazz, ttl, cpu, os): def write(self, out): """Used in constructing an outgoing packet""" - out.writeString(self.cpu, len(self.cpu)) - out.writeString(self.os, len(self.os)) + out.writeString(self.cpu) + out.writeString(self.oso) def __eq__(self, other): """Tests equality on cpu and os""" @@ -408,7 +408,7 @@ def __init__(self, name, type, clazz, ttl, text): def write(self, out): """Used in constructing an outgoing packet""" - out.writeString(self.text, len(self.text)) + out.writeString(self.text) def __eq__(self, other): """Tests equality on text""" @@ -468,12 +468,15 @@ def __init__(self, data): self.readQuestions() self.readOthers() - def readHeader(self): - """Reads header portion of packet""" - format = '!HHHHHH' + def unpack(self, format): length = struct.calcsize(format) info = struct.unpack(format, self.data[self.offset:self.offset+length]) self.offset += length + return info + + def readHeader(self): + """Reads header portion of packet""" + info = self.unpack('!HHHHHH') self.id = info[0] self.flags = info[1] @@ -484,23 +487,16 @@ def readHeader(self): def readQuestions(self): """Reads questions section of packet""" - format = '!HH' - length = struct.calcsize(format) for i in range(0, self.numQuestions): name = self.readName() - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length + info = self.unpack('!HH') question = DNSQuestion(name, info[0], info[1]) self.questions.append(question) def readInt(self): """Reads an integer from the packet""" - format = '!I' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - return info[0] + return self.unpack('!I')[0] def readCharacterString(self): """Reads a character string from the packet""" @@ -508,31 +504,22 @@ def readCharacterString(self): self.offset += 1 return self.readString(length) - def readString(self, len): + def readString(self, length): """Reads a string of a given length from the packet""" - format = '!' + str(len) + 's' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) + info = self.data[self.offset:self.offset+length] self.offset += length - return info[0] + return info def readUnsignedShort(self): """Reads an unsigned short from the packet""" - format = '!H' - length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length - return info[0] + return self.unpack('!H')[0] def readOthers(self): """Reads the answers, authorities and additionals section of the packet""" - format = '!HHiH' - length = struct.calcsize(format) n = self.numAnswers + self.numAuthorities + self.numAdditionals for i in range(0, n): domain = self.readName() - info = struct.unpack(format, self.data[self.offset:self.offset+length]) - self.offset += length + info = self.unpack('!HHiH') rec = None if info[0] == _TYPE_A: @@ -642,35 +629,31 @@ def addAdditionalAnswer(self, record): """Adds an additional answer""" self.additionals.append(record) + def pack(self, format, value): + self.data.append(struct.pack(format, value)) + self.size += struct.calcsize(format) + def writeByte(self, value): """Writes a single byte to the packet""" - format = '!c' - self.data.append(struct.pack(format, chr(value))) - self.size += 1 + self.pack('!c', chr(value)) def insertShort(self, index, value): """Inserts an unsigned short in a certain position in the packet""" - format = '!H' - self.data.insert(index, struct.pack(format, value)) + self.data.insert(index, struct.pack('!H', value)) self.size += 2 def writeShort(self, value): """Writes an unsigned short to the packet""" - format = '!H' - self.data.append(struct.pack(format, value)) - self.size += 2 + self.pack('!H', value) def writeInt(self, value): """Writes an unsigned integer to the packet""" - format = '!I' - self.data.append(struct.pack(format, int(value))) - self.size += 4 + self.pack('!I', int(value)) - def writeString(self, value, length): + def writeString(self, value): """Writes a string to the packet""" - format = '!' + str(length) + 's' - self.data.append(struct.pack(format, value)) - self.size += length + self.data.append(value) + self.size += len(value) def writeUTF(self, s): """Writes a UTF-8 string of a given length to the packet""" @@ -679,7 +662,7 @@ def writeUTF(self, s): if length > 64: raise NamePartTooLongException self.writeByte(length) - self.writeString(utfstr, length) + self.writeString(utfstr) def writeName(self, name): """Writes a domain name to the packet""" From e5d930bb681f5544827fc0c9f37daa778dec5930 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 18 Jan 2011 01:19:35 -0500 Subject: [PATCH 011/163] 80-column fixes for Zeroconf. --- Zeroconf.py | 246 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 160 insertions(+), 86 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 359712ec..bb7851ba 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -32,7 +32,8 @@ - hostinfo ignore unknown DNS record types fixes to name decoding - works alongside other processes using port 5353 (e.g. on Mac OS X) + works alongside other processes using port 5353 + (e.g. on Mac OS X) tested against Mac OS X 10.3.2's mDNSResponder corrections to removal of list entries for service browser""" @@ -218,7 +219,9 @@ def __init__(self, name, type, clazz): def __eq__(self, other): """Equality test on name, type, and class""" if isinstance(other, DNSEntry): - return self.name == other.name and self.type == other.type and self.clazz == other.clazz + return (self.name == other.name and + self.type == other.type and + self.clazz == other.clazz) return 0 def __ne__(self, other): @@ -241,7 +244,8 @@ def getType(self, type): def toString(self, hdr, other): """String representation with additional information""" - result = "%s[%s,%s" % (hdr, self.getType(self.type), self.getClazz(self.clazz)) + result = "%s[%s,%s" % (hdr, self.getType(self.type), + self.getClazz(self.clazz)) if self.unique: result += "-unique," else: @@ -263,7 +267,9 @@ def __init__(self, name, type, clazz): def answeredBy(self, rec): """Returns true if the question is answered by the record""" - return self.clazz == rec.clazz and (self.type == rec.type or self.type == _TYPE_ANY) and self.name == rec.name + return (self.clazz == rec.clazz and + (self.type == rec.type or self.type == _TYPE_ANY) and + self.name == rec.name) def __repr__(self): """String representation""" @@ -328,7 +334,8 @@ def write(self, out): def toString(self, other): """String representation with addtional information""" - arg = "%s/%s,%s" % (self.ttl, self.getRemainingTTL(currentTimeMillis()), other) + arg = "%s/%s,%s" % (self.ttl, + self.getRemainingTTL(currentTimeMillis()), other) return DNSEntry.toString(self, "record", arg) class DNSAddress(DNSRecord): @@ -443,7 +450,10 @@ def write(self, out): def __eq__(self, other): """Tests equality on priority, weight, port and server""" if isinstance(other, DNSService): - return self.priority == other.priority and self.weight == other.weight and self.port == other.port and self.server == other.server + return (self.priority == other.priority and + self.weight == other.weight and + self.port == other.port and + self.server == other.server) return 0 def __repr__(self): @@ -515,7 +525,8 @@ def readUnsignedShort(self): return self.unpack('!H')[0] def readOthers(self): - """Reads the answers, authorities and additionals section of the packet""" + """Reads the answers, authorities and additionals section of the + packet""" n = self.numAnswers + self.numAuthorities + self.numAdditionals for i in range(0, n): domain = self.readName() @@ -523,17 +534,24 @@ def readOthers(self): rec = None if info[0] == _TYPE_A: - rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(4)) + rec = DNSAddress(domain, info[0], info[1], info[2], + self.readString(4)) elif info[0] == _TYPE_CNAME or info[0] == _TYPE_PTR: - rec = DNSPointer(domain, info[0], info[1], info[2], self.readName()) + rec = DNSPointer(domain, info[0], info[1], info[2], + self.readName()) elif info[0] == _TYPE_TXT: - rec = DNSText(domain, info[0], info[1], info[2], self.readString(info[3])) + rec = DNSText(domain, info[0], info[1], info[2], + self.readString(info[3])) elif info[0] == _TYPE_SRV: - rec = DNSService(domain, info[0], info[1], info[2], self.readUnsignedShort(), self.readUnsignedShort(), self.readUnsignedShort(), self.readName()) + rec = DNSService(domain, info[0], info[1], info[2], + self.readUnsignedShort(), self.readUnsignedShort(), + self.readUnsignedShort(), self.readName()) elif info[0] == _TYPE_HINFO: - rec = DNSHinfo(domain, info[0], info[1], info[2], self.readCharacterString(), self.readCharacterString()) + rec = DNSHinfo(domain, info[0], info[1], info[2], + self.readCharacterString(), self.readCharacterString()) elif info[0] == _TYPE_AAAA: - rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16)) + rec = DNSAddress(domain, info[0], info[1], info[2], + self.readString(16)) else: # Try to ignore types we don't know about # Skip the payload for the resource record so the next @@ -812,9 +830,9 @@ class Engine(threading.Thread): packets. """ - def __init__(self, zeroconf): + def __init__(self, zc): threading.Thread.__init__(self) - self.zeroconf = zeroconf + self.zc = zc self.readers = {} # maps socket to reader self.timeout = 5 self.condition = threading.Condition() @@ -873,13 +891,13 @@ class Listener(object): It requires registration with an Engine object in order to have the read() method called when a socket is availble for reading.""" - def __init__(self, zeroconf): - self.zeroconf = zeroconf - self.zeroconf.engine.addReader(self, self.zeroconf.socket) + def __init__(self, zc): + self.zc = zc + self.zc.engine.addReader(self, self.zc.socket) def handle_read(self): try: - data, (addr, port) = self.zeroconf.socket.recvfrom(_MAX_MSG_ABSOLUTE) + data, (addr, port) = self.zc.socket.recvfrom(_MAX_MSG_ABSOLUTE) except socket.error, e: # If the socket was closed by another thread -- which happens # regularly on shutdown -- an EBADF exception is thrown here. @@ -894,36 +912,36 @@ def handle_read(self): # Always multicast responses # if port == _MDNS_PORT: - self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) + self.zc.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) # If it's not a multicast query, reply via unicast # and multicast # elif port == _DNS_PORT: - self.zeroconf.handleQuery(msg, addr, port) - self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) + self.zc.handleQuery(msg, addr, port) + self.zc.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) else: - self.zeroconf.handleResponse(msg) + self.zc.handleResponse(msg) class Reaper(threading.Thread): """A Reaper is used by this module to remove cache entries that have expired.""" - def __init__(self, zeroconf): + def __init__(self, zc): threading.Thread.__init__(self) - self.zeroconf = zeroconf + self.zc = zc self.start() def run(self): while 1: - self.zeroconf.wait(10 * 1000) + self.zc.wait(10 * 1000) if globals()['_GLOBAL_DONE']: return now = currentTimeMillis() - for record in self.zeroconf.cache.entries(): + for record in self.zc.cache.entries(): if record.isExpired(now): - self.zeroconf.updateRecord(now, record) - self.zeroconf.cache.remove(record) + self.zc.updateRecord(now, record) + self.zc.cache.remove(record) class ServiceBrowser(threading.Thread): @@ -933,10 +951,10 @@ class ServiceBrowser(threading.Thread): removeService() methods called when this browser discovers changes in the services availability.""" - def __init__(self, zeroconf, type, listener): + def __init__(self, zc, type, listener): """Creates a browser for a specific type""" threading.Thread.__init__(self) - self.zeroconf = zeroconf + self.zc = zc self.type = type self.listener = listener self.services = {} @@ -946,10 +964,10 @@ def __init__(self, zeroconf, type, listener): self.done = 0 - self.zeroconf.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) + self.zc.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) self.start() - def updateRecord(self, zeroconf, now, record): + def updateRecord(self, zc, now, record): """Callback invoked by Zeroconf when new information arrives. Updates information required by browser in the Zeroconf cache.""" @@ -961,13 +979,15 @@ def updateRecord(self, zeroconf, now, record): oldrecord.resetTTL(record) else: del(self.services[record.alias.lower()]) - callback = lambda x: self.listener.removeService(x, self.type, record.alias) + callback = lambda x: self.listener.removeService(x, + self.type, record.alias) self.list.append(callback) return except: if not expired: self.services[record.alias.lower()] = record - callback = lambda x: self.listener.addService(x, self.type, record.alias) + callback = lambda x: self.listener.addService(x, + self.type, record.alias) self.list.append(callback) expires = record.getExpirationTime(75) @@ -976,14 +996,14 @@ def updateRecord(self, zeroconf, now, record): def cancel(self): self.done = 1 - self.zeroconf.notifyAll() + self.zc.notifyAll() def run(self): while 1: event = None now = currentTimeMillis() if len(self.list) == 0 and self.nextTime > now: - self.zeroconf.wait(self.nextTime - now) + self.zc.wait(self.nextTime - now) if globals()['_GLOBAL_DONE'] or self.done: return now = currentTimeMillis() @@ -994,7 +1014,7 @@ def run(self): for record in self.services.values(): if not record.isExpired(now): out.addAnswerAtTime(record, now) - self.zeroconf.send(out) + self.zc.send(out) self.nextTime = now + self.delay self.delay = min(20 * 1000, self.delay * 2) @@ -1002,13 +1022,14 @@ def run(self): event = self.list.pop(0) if event is not None: - event(self.zeroconf) + event(self.zc) class ServiceInfo(object): """Service information""" - def __init__(self, type, name, address=None, port=None, weight=0, priority=0, properties=None, server=None): + def __init__(self, type, name, address=None, port=None, weight=0, + priority=0, properties=None, server=None): """Create a service description. type: fully qualified service type name @@ -1017,7 +1038,8 @@ def __init__(self, type, name, address=None, port=None, weight=0, priority=0, pr port: port that the service runs on weight: weight of the service priority: priority of the service - properties: dictionary of properties (or a string holding the bytes for the text field) + properties: dictionary of properties (or a string holding the + bytes for the text field) server: fully qualified name for service host (defaults to name)""" if not name.endswith(type): @@ -1055,7 +1077,7 @@ def setProperties(self, properties): suffix = ''.encode('utf-8') list.append('='.join((key, suffix))) for item in list: - result = ''.join((result, struct.pack('!c', chr(len(item))), item)) + result = ''.join((result, chr(len(item)), item)) self.text = result else: self.text = properties @@ -1135,7 +1157,7 @@ def getServer(self): """Server accessor""" return self.server - def updateRecord(self, zeroconf, now, record): + def updateRecord(self, zc, now, record): """Updates service information from a DNS record""" if record is not None and not record.isExpired(now): if record.type == _TYPE_A: @@ -1149,12 +1171,13 @@ def updateRecord(self, zeroconf, now, record): self.weight = record.weight self.priority = record.priority #self.address = None - self.updateRecord(zeroconf, now, zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) + self.updateRecord(zc, now, + zc.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) elif record.type == _TYPE_TXT: if record.name == self.name: self.setText(record.text) - def request(self, zeroconf, timeout): + def request(self, zc, timeout): """Returns true if the service could be discovered on the network, and updates this object with details discovered. """ @@ -1164,28 +1187,35 @@ def request(self, zeroconf, timeout): last = now + timeout result = 0 try: - zeroconf.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) - while self.server is None or self.address is None or self.text is None: + zc.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) + while (self.server is None or self.address is None or + self.text is None): if last <= now: return 0 if next <= now: out = DNSOutgoing(_FLAGS_QR_QUERY) - out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_SRV, _CLASS_IN), now) - out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_TXT, _CLASS_IN), now) + out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, + _CLASS_IN)) + out.addAnswerAtTime(zc.cache.getByDetails(self.name, + _TYPE_SRV, _CLASS_IN), now) + out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, + _CLASS_IN)) + out.addAnswerAtTime(zc.cache.getByDetails(self.name, + _TYPE_TXT, _CLASS_IN), now) if self.server is not None: - out.addQuestion(DNSQuestion(self.server, _TYPE_A, _CLASS_IN)) - out.addAnswerAtTime(zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN), now) - zeroconf.send(out) + out.addQuestion(DNSQuestion(self.server, + _TYPE_A, _CLASS_IN)) + out.addAnswerAtTime(zc.cache.getByDetails(self.server, + _TYPE_A, _CLASS_IN), now) + zc.send(out) next = now + delay delay = delay * 2 - zeroconf.wait(min(next, last) - now) + zc.wait(min(next, last) - now) now = currentTimeMillis() result = 1 finally: - zeroconf.removeListener(self) + zc.removeListener(self) return result @@ -1201,7 +1231,8 @@ def __ne__(self, other): def __repr__(self): """String representation""" - result = "service[%s,%s:%s," % (self.name, socket.inet_ntoa(self.getAddress()), self.port) + result = "service[%s,%s:%s," % (self.name, + socket.inet_ntoa(self.getAddress()), self.port) if self.text is None: result += "None" else: @@ -1251,8 +1282,10 @@ def __init__(self, bindaddress=None): # the SO_REUSE* options have been set, so ignore it # pass - #self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) - self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + #self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, + # socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) + self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, + socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) self.listeners = [] self.browsers = [] @@ -1329,11 +1362,16 @@ def registerService(self, info, ttl=_DNS_TTL): now = currentTimeMillis() continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, ttl, info.priority, info.weight, info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0) + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, + _CLASS_IN, ttl, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, ttl, info.priority, info.weight, info.port, + info.server), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, + ttl, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, ttl, info.address), 0) + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, + _CLASS_IN, ttl, info.address), 0) self.send(out) i += 1 nextTime += _REGISTER_TIME @@ -1357,11 +1395,16 @@ def unregisterService(self, info): now = currentTimeMillis() continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.name), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, + _CLASS_IN, 0, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, 0, info.priority, info.weight, info.port, + info.name), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, + 0, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, + _CLASS_IN, 0, info.address), 0) self.send(out) i += 1 nextTime += _UNREGISTER_TIME @@ -1379,11 +1422,16 @@ def unregisterAllServices(self): continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) for info in self.services.values(): - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, + _CLASS_IN, 0, info.name), 0) + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, 0, info.priority, info.weight, + info.port, info.server), 0) + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, + _CLASS_IN, 0, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) + out.addAnswerAtTime(DNSAddress(info.server, + _TYPE_A, _CLASS_IN, 0, info.address), 0) self.send(out) i += 1 nextTime += _UNREGISTER_TIME @@ -1396,9 +1444,13 @@ def checkService(self, info): i = 0 while i < 3: for record in self.cache.entriesWithName(info.type): - if record.type == _TYPE_PTR and not record.isExpired(now) and record.alias == info.name: - if (info.name.find('.') < 0): - info.name = info.name + ".[" + info.address + ":" + info.port + "]." + info.type + if (record.type == _TYPE_PTR and not + record.isExpired(now) and + record.alias == info.name): + if info.name.find('.') < 0: + info.name = '%s.[%s:%s].%s' % (info.name, + info.address, info.port, info.type) + self.checkService(info) return raise NonUniqueNameException @@ -1409,7 +1461,8 @@ def checkService(self, info): out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) self.debug = out out.addQuestion(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) - out.addAuthorativeAnswer(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, info.name)) + out.addAuthorativeAnswer(DNSPointer(info.type, _TYPE_PTR, + _CLASS_IN, _DNS_TTL, info.name)) self.send(out) i += 1 nextTime += _CHECK_TIME @@ -1478,12 +1531,16 @@ def handleQuery(self, msg, addr, port): for stype in self.servicetypes.keys(): if out is None: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswer(msg, DNSPointer("_services._dns-sd._udp.local.", _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) + out.addAnswer(msg, + DNSPointer("_services._dns-sd._udp.local.", + _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) for service in self.services.values(): if question.name == service.type: if out is None: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswer(msg, DNSPointer(service.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, service.name)) + out.addAnswer(msg, + DNSPointer(service.type, _TYPE_PTR, + _CLASS_IN, _DNS_TTL, service.name)) else: try: if out is None: @@ -1493,17 +1550,26 @@ def handleQuery(self, msg, addr, port): if question.type == _TYPE_A or question.type == _TYPE_ANY: for service in self.services.values(): if service.server == question.name.lower(): - out.addAnswer(msg, DNSAddress(question.name, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) - + out.addAnswer(msg, DNSAddress(question.name, + _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.address)) + service = self.services.get(question.name.lower(), None) if not service: continue if question.type == _TYPE_SRV or question.type == _TYPE_ANY: - out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.priority, service.weight, service.port, service.server)) + out.addAnswer(msg, DNSService(question.name, + _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.priority, service.weight, + service.port, service.server)) if question.type == _TYPE_TXT or question.type == _TYPE_ANY: - out.addAnswer(msg, DNSText(question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.text)) + out.addAnswer(msg, DNSText(question.name, + _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.text)) if question.type == _TYPE_SRV: - out.addAdditionalAnswer(DNSAddress(service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) + out.addAdditionalAnswer(DNSAddress(service.server, + _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.address)) except: traceback.print_exc() @@ -1529,7 +1595,10 @@ def close(self): self.notifyAll() self.engine.notify() self.unregisterAllServices() - self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + self.socket.setsockopt(socket.SOL_IP, + socket.IP_DROP_MEMBERSHIP, + socket.inet_aton(_MDNS_ADDR) + + socket.inet_aton('0.0.0.0')) self.socket.close() # Test a few module features, including service registration, service @@ -1540,15 +1609,20 @@ def close(self): r = Zeroconf() print "1. Testing registration of a service..." desc = {'version':'0.10','a':'test value', 'b':'another value'} - info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) + info = ServiceInfo("_http._tcp.local.", + "My Service Name._http._tcp.local.", + socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) print " Registering service..." r.registerService(info) print " Registration done." print "2. Testing query of service information..." - print " Getting ZOE service:", str(r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local.")) + print " Getting ZOE service:", + print str(r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local.")) print " Query done." print "3. Testing query of own service..." - print " Getting self:", str(r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.")) + print " Getting self:", + print str(r.getServiceInfo("_http._tcp.local.", + "My Service Name._http._tcp.local.")) print " Query done." print "4. Testing unregister of service information..." r.unregisterService(info) From 3e718b55becd883324bf40eda700431b302a0da8 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 18 Jan 2011 07:34:46 -0500 Subject: [PATCH 012/163] Clearer. --- Zeroconf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index bb7851ba..b42d3610 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -1444,8 +1444,8 @@ def checkService(self, info): i = 0 while i < 3: for record in self.cache.entriesWithName(info.type): - if (record.type == _TYPE_PTR and not - record.isExpired(now) and + if (record.type == _TYPE_PTR and + not record.isExpired(now) and record.alias == info.name): if info.name.find('.') < 0: info.name = '%s.[%s:%s].%s' % (info.name, From 173350e415e66c9629d553f820677453bdbe5724 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 18 Jan 2011 16:17:39 -0500 Subject: [PATCH 013/163] Minor style things for Zeroconf (use True/False instead of 1/0, etc.). --- Zeroconf.py | 105 +++++++++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 59 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index b42d3610..edd8cf3a 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -90,7 +90,7 @@ # hook for threads -globals()['_GLOBAL_DONE'] = 0 +_GLOBAL_DONE = False # Some timing constants @@ -218,11 +218,10 @@ def __init__(self, name, type, clazz): def __eq__(self, other): """Equality test on name, type, and class""" - if isinstance(other, DNSEntry): - return (self.name == other.name and - self.type == other.type and - self.clazz == other.clazz) - return 0 + return (isinstance(other, DNSEntry) and + self.name == other.name and + self.type == other.type and + self.clazz == other.clazz) def __ne__(self, other): """Non-equality test""" @@ -286,24 +285,20 @@ def __init__(self, name, type, clazz, ttl): def __eq__(self, other): """Tests equality as per DNSRecord""" - if isinstance(other, DNSRecord): - return DNSEntry.__eq__(self, other) - return 0 + return isinstance(other, DNSRecord) and DNSEntry.__eq__(self, other) def suppressedBy(self, msg): """Returns true if any answer in a message can suffice for the information held in this record.""" for record in msg.answers: if self.suppressedByAnswer(record): - return 1 - return 0 + return True + return False def suppressedByAnswer(self, other): """Returns true if another record has same name, type and class, and if its TTL is at least half of this record's.""" - if self == other and other.ttl > (self.ttl / 2): - return 1 - return 0 + return self == other and other.ttl > (self.ttl / 2) def getExpirationTime(self, percent): """Returns the time at which this record will have expired @@ -351,9 +346,7 @@ def write(self, out): def __eq__(self, other): """Tests equality on address""" - if isinstance(other, DNSAddress): - return self.address == other.address - return 0 + return isinstance(other, DNSAddress) and self.address == other.address def __repr__(self): """String representation""" @@ -377,9 +370,8 @@ def write(self, out): def __eq__(self, other): """Tests equality on cpu and os""" - if isinstance(other, DNSHinfo): - return self.cpu == other.cpu and self.os == other.os - return 0 + return (isinstance(other, DNSHinfo) and + self.cpu == other.cpu and self.os == other.os) def __repr__(self): """String representation""" @@ -398,9 +390,7 @@ def write(self, out): def __eq__(self, other): """Tests equality on alias""" - if isinstance(other, DNSPointer): - return self.alias == other.alias - return 0 + return isinstance(other, DNSPointer) and self.alias == other.alias def __repr__(self): """String representation""" @@ -419,9 +409,7 @@ def write(self, out): def __eq__(self, other): """Tests equality on text""" - if isinstance(other, DNSText): - return self.text == other.text - return 0 + return isinstance(other, DNSText) and self.text == other.text def __repr__(self): """String representation""" @@ -449,12 +437,11 @@ def write(self, out): def __eq__(self, other): """Tests equality on priority, weight, port and server""" - if isinstance(other, DNSService): - return (self.priority == other.priority and - self.weight == other.weight and - self.port == other.port and - self.server == other.server) - return 0 + return (isinstance(other, DNSService) and + self.priority == other.priority and + self.weight == other.weight and + self.port == other.port and + self.server == other.server) def __repr__(self): """String representation""" @@ -580,7 +567,7 @@ def readName(self): next = -1 first = off - while 1: + while True: len = ord(self.data[off]) off += 1 if len == 0: @@ -611,7 +598,7 @@ class DNSOutgoing(object): """Object representation of an outgoing packet""" def __init__(self, flags, multicast = 1): - self.finished = 0 + self.finished = False self.id = 0 self.multicast = multicast self.flags = flags @@ -743,7 +730,7 @@ def packet(self): No further parts should be added to the packet once this is done.""" if not self.finished: - self.finished = 1 + self.finished = True for question in self.questions: self.writeQuestion(question) for answer, time in self.answers: @@ -839,7 +826,7 @@ def __init__(self, zc): self.start() def run(self): - while not globals()['_GLOBAL_DONE']: + while not _GLOBAL_DONE: rs = self.getReaders() if len(rs) == 0: # No sockets to manage, but we wait for the timeout @@ -933,9 +920,9 @@ def __init__(self, zc): self.start() def run(self): - while 1: + while True: self.zc.wait(10 * 1000) - if globals()['_GLOBAL_DONE']: + if _GLOBAL_DONE: return now = currentTimeMillis() for record in self.zc.cache.entries(): @@ -962,7 +949,7 @@ def __init__(self, zc, type, listener): self.delay = _BROWSER_TIME self.list = [] - self.done = 0 + self.done = False self.zc.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) self.start() @@ -995,16 +982,16 @@ def updateRecord(self, zc, now, record): self.nextTime = expires def cancel(self): - self.done = 1 + self.done = True self.zc.notifyAll() def run(self): - while 1: + while True: event = None now = currentTimeMillis() if len(self.list) == 0 and self.nextTime > now: self.zc.wait(self.nextTime - now) - if globals()['_GLOBAL_DONE'] or self.done: + if _GLOBAL_DONE or self.done: return now = currentTimeMillis() @@ -1097,18 +1084,16 @@ def setText(self, text): index += length for s in strs: - eindex = s.find('=') - if eindex == -1: - # No equals sign at all - key = s - value = 0 - else: - key = s[:eindex] - value = s[eindex+1:] + try: + key, value = s.split('=', 1) if value == 'true': - value = 1 + value = True elif value == 'false' or not value: - value = 0 + value = False + except: + # No equals sign at all + key = s + value = False # Only update non-existent properties if key and result.get(key) == None: @@ -1185,13 +1170,13 @@ def request(self, zc, timeout): delay = _LISTENER_TIME next = now + delay last = now + timeout - result = 0 + result = False try: zc.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) while (self.server is None or self.address is None or self.text is None): if last <= now: - return 0 + return False if next <= now: out = DNSOutgoing(_FLAGS_QR_QUERY) out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, @@ -1213,7 +1198,7 @@ def request(self, zc, timeout): zc.wait(min(next, last) - now) now = currentTimeMillis() - result = 1 + result = True finally: zc.removeListener(self) @@ -1223,7 +1208,7 @@ def __eq__(self, other): """Tests equality of service name""" if isinstance(other, ServiceInfo): return other.name == self.name - return 0 + return False def __ne__(self, other): """Non-equality test""" @@ -1252,7 +1237,8 @@ class Zeroconf(object): def __init__(self, bindaddress=None): """Creates an instance of the Zeroconf class, establishing multicast communications, listening and reaping threads.""" - globals()['_GLOBAL_DONE'] = 0 + global _GLOBAL_DONE + _GLOBAL_DONE = False if bindaddress is None: self.intf = socket.gethostbyname(socket.gethostname()) else: @@ -1590,8 +1576,9 @@ def send(self, out, addr = _MDNS_ADDR, port = _MDNS_PORT): def close(self): """Ends the background threads, and prevent this instance from servicing further queries.""" - if globals()['_GLOBAL_DONE'] == 0: - globals()['_GLOBAL_DONE'] = 1 + global _GLOBAL_DONE + if not _GLOBAL_DONE: + _GLOBAL_DONE = True self.notifyAll() self.engine.notify() self.unregisterAllServices() From b8baed3a2876c126cac65a7d95bb88661b31483c Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 18 Jan 2011 20:55:17 -0500 Subject: [PATCH 014/163] More. --- Zeroconf.py | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index edd8cf3a..d1d33984 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -473,22 +473,16 @@ def unpack(self, format): def readHeader(self): """Reads header portion of packet""" - info = self.unpack('!HHHHHH') - - self.id = info[0] - self.flags = info[1] - self.numQuestions = info[2] - self.numAnswers = info[3] - self.numAuthorities = info[4] - self.numAdditionals = info[5] + (self.id, self.flags, self.numQuestions, self.numAnswers, + self.numAuthorities, self.numAdditionals) = self.unpack('!HHHHHH') def readQuestions(self): """Reads questions section of packet""" - for i in range(0, self.numQuestions): + for i in xrange(self.numQuestions): name = self.readName() - info = self.unpack('!HH') + type, clazz = self.unpack('!HH') - question = DNSQuestion(name, info[0], info[1]) + question = DNSQuestion(name, type, clazz) self.questions.append(question) def readInt(self): @@ -515,35 +509,31 @@ def readOthers(self): """Reads the answers, authorities and additionals section of the packet""" n = self.numAnswers + self.numAuthorities + self.numAdditionals - for i in range(0, n): + for i in xrange(n): domain = self.readName() - info = self.unpack('!HHiH') + type, clazz, ttl, length = self.unpack('!HHiH') rec = None - if info[0] == _TYPE_A: - rec = DNSAddress(domain, info[0], info[1], info[2], - self.readString(4)) - elif info[0] == _TYPE_CNAME or info[0] == _TYPE_PTR: - rec = DNSPointer(domain, info[0], info[1], info[2], - self.readName()) - elif info[0] == _TYPE_TXT: - rec = DNSText(domain, info[0], info[1], info[2], - self.readString(info[3])) - elif info[0] == _TYPE_SRV: - rec = DNSService(domain, info[0], info[1], info[2], + if type == _TYPE_A: + rec = DNSAddress(domain, type, clazz, ttl, self.readString(4)) + elif type == _TYPE_CNAME or type == _TYPE_PTR: + rec = DNSPointer(domain, type, clazz, ttl, self.readName()) + elif type == _TYPE_TXT: + rec = DNSText(domain, type, clazz, ttl, self.readString(length)) + elif type == _TYPE_SRV: + rec = DNSService(domain, type, clazz, ttl, self.readUnsignedShort(), self.readUnsignedShort(), self.readUnsignedShort(), self.readName()) - elif info[0] == _TYPE_HINFO: - rec = DNSHinfo(domain, info[0], info[1], info[2], + elif type == _TYPE_HINFO: + rec = DNSHinfo(domain, type, clazz, ttl, self.readCharacterString(), self.readCharacterString()) - elif info[0] == _TYPE_AAAA: - rec = DNSAddress(domain, info[0], info[1], info[2], - self.readString(16)) + elif type == _TYPE_AAAA: + rec = DNSAddress(domain, type, clazz, ttl, self.readString(16)) else: # Try to ignore types we don't know about # Skip the payload for the resource record so the next # records can be parsed correctly - self.offset += info[3] + self.offset += length if rec is not None: self.answers.append(rec) From 24205054309e110238fc5a986cdc27b17c44abef Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 18 Jan 2011 23:14:00 -0500 Subject: [PATCH 015/163] More again. --- Zeroconf.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index d1d33984..a6b3a031 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -587,7 +587,7 @@ def readName(self): class DNSOutgoing(object): """Object representation of an outgoing packet""" - def __init__(self, flags, multicast = 1): + def __init__(self, flags, multicast=True): self.finished = False self.id = 0 self.multicast = multicast @@ -1497,7 +1497,7 @@ def handleQuery(self, msg, addr, port): # Support unicast client responses # if port != _MDNS_PORT: - out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, 0) + out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, False) for question in msg.questions: out.addQuestion(question) @@ -1523,7 +1523,7 @@ def handleQuery(self, msg, addr, port): out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) # Answer A record queries for any service addresses we know - if question.type == _TYPE_A or question.type == _TYPE_ANY: + if question.type in (_TYPE_A, _TYPE_ANY): for service in self.services.values(): if service.server == question.name.lower(): out.addAnswer(msg, DNSAddress(question.name, @@ -1533,12 +1533,12 @@ def handleQuery(self, msg, addr, port): service = self.services.get(question.name.lower(), None) if not service: continue - if question.type == _TYPE_SRV or question.type == _TYPE_ANY: + if question.type in (_TYPE_SRV, _TYPE_ANY): out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.priority, service.weight, service.port, service.server)) - if question.type == _TYPE_TXT or question.type == _TYPE_ANY: + if question.type in (_TYPE_TXT, _TYPE_ANY): out.addAnswer(msg, DNSText(question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.text)) From f6d47316a47d9d04539f1a4215dd7eec06c33d4c Mon Sep 17 00:00:00 2001 From: William McBrine Date: Thu, 18 Aug 2011 10:59:09 -0400 Subject: [PATCH 016/163] In Zeroconf, use the same method of determining the default IP as elsewhere, instead of the unreliable gethostbyname(gethostname()) method (but fall back to that). --- Zeroconf.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Zeroconf.py b/Zeroconf.py index a6b3a031..f04c3392 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -1230,7 +1230,12 @@ def __init__(self, bindaddress=None): global _GLOBAL_DONE _GLOBAL_DONE = False if bindaddress is None: - self.intf = socket.gethostbyname(socket.gethostname()) + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(('4.2.2.1', 123)) + self.intf = s.getsockname()[0] + except: + self.intf = socket.gethostbyname(socket.gethostname()) else: self.intf = bindaddress self.group = ('', _MDNS_PORT) From 9a949532484a55e52f1d2f14eb27277a5133ce29 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 2 May 2012 22:40:11 -0400 Subject: [PATCH 017/163] HHHHHH -> 6H --- Zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zeroconf.py b/Zeroconf.py index f04c3392..c458a7b3 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -474,7 +474,7 @@ def unpack(self, format): def readHeader(self): """Reads header portion of packet""" (self.id, self.flags, self.numQuestions, self.numAnswers, - self.numAuthorities, self.numAdditionals) = self.unpack('!HHHHHH') + self.numAuthorities, self.numAdditionals) = self.unpack('!6H') def readQuestions(self): """Reads questions section of packet""" From a81f3ababc585acca4bacc51a832703286ec5cfb Mon Sep 17 00:00:00 2001 From: William McBrine Date: Sun, 6 May 2012 14:13:14 -0400 Subject: [PATCH 018/163] Update the version to indicate a fork. --- Zeroconf.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index c458a7b3..36313780 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -1,4 +1,4 @@ -""" Multicast DNS Service Discovery for Python, v0.12 +""" Multicast DNS Service Discovery for Python, v0.12-wmcbrine Copyright (C) 2003, Paul Scott-Murphy This module provides a framework for the use of DNS Service Discovery @@ -22,6 +22,8 @@ """ +"""0.12-wmcbrine update - see git for changes""" + """0.12 update - allow selection of binding interface typo fix - Thanks A. M. Kuchlingi removed all use of word 'Rendezvous' - this is an API change""" @@ -77,7 +79,7 @@ __author__ = "Paul Scott-Murphy" __email__ = "paul at scott dash murphy dot com" -__version__ = "0.12" +__version__ = "0.12-wmcbrine" import time import struct From ac91642b0ea90a3c84b605e19d562b897e2cd1fd Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 2 Jul 2013 16:47:36 -0400 Subject: [PATCH 019/163] Although beacons _should_ fit within single packets, maybe we should allow for the possibility that they won't? (Or, does this even make sense with sendto()?) --- Zeroconf.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 36313780..da7e693f 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -1562,10 +1562,13 @@ def handleQuery(self, msg, addr, port): def send(self, out, addr = _MDNS_ADDR, port = _MDNS_PORT): """Sends an outgoing packet.""" - # This is a quick test to see if we can parse the packets we generate - #temp = DNSIncoming(out.packet()) + packet = out.packet() try: - bytes_sent = self.socket.sendto(out.packet(), 0, (addr, port)) + while packet: + bytes_sent = self.socket.sendto(packet, 0, (addr, port)) + if bytes_sent < 0: + break + packet = packet[bytes_sent:] except: # Ignore this, it may be a temporary loss of network connection pass From d34610768812906ff07974c1314f6073b431d96e Mon Sep 17 00:00:00 2001 From: William McBrine Date: Mon, 8 Jul 2013 09:04:44 -0400 Subject: [PATCH 020/163] Write only a byte. --- Zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Zeroconf.py b/Zeroconf.py index da7e693f..871e243e 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -685,7 +685,7 @@ def writeName(self, name): # An index was found, so write a pointer to it # self.writeByte((index >> 8) | 0xC0) - self.writeByte(index) + self.writeByte(index & 0xFF) def writeQuestion(self, question): """Writes a question to the packet""" From 656f959c26310629953cc661ffad681194295131 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 9 Jul 2013 01:15:46 -0400 Subject: [PATCH 021/163] Describe this fork. --- README | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README b/README index 266b1c5b..6cbe503b 100644 --- a/README +++ b/README @@ -1,4 +1,20 @@ -This is Multicast DNS Service Discovery for Python v0.12 by Paul Scott-Murphy. +This is Multicast DNS Service Discovery for Python v0.12-wmcbrine +Originally by Paul Scott-Murphy, modified by William McBrine. Zeroconf.py is the implementation file, look at the end for examples of basic use. You can also view Browser.py to see how to browse for services. + +This fork is used in all of my TiVo-related projects: HME for Python +(and therefore HME/VLC), Network Remote, and pyTivo. Until now, I was +tracking the changes for Zeroconf.py in three separate repos. I figured +I should have an authoritative source. + +Although I make changes based on my experience with TiVos, I expect that +they're generally applicable. This version also includes patches found +on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere +around the net -- not always well-documented, sorry. + +I only use Zeroconf.py myself, so I can't vouch for the other bits here +yet. I'll check them out. + +Long-term goals: Python 3.x From d89ddfcecc7b336aa59a4ff784cb8b810772d24f Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 9 Jul 2013 03:56:05 -0400 Subject: [PATCH 022/163] Don't test for NonLocalNameException, since I killed it. --- ZeroconfTest.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/ZeroconfTest.py b/ZeroconfTest.py index 4a435bd9..a1c5a214 100644 --- a/ZeroconfTest.py +++ b/ZeroconfTest.py @@ -123,9 +123,6 @@ def testNumbersAnswers(self): class Names(unittest.TestCase): - def testNonLocalName(self): - self.assertRaises(r.NonLocalNameException, r.DNSQuestion, "this.wont.work.com.", r._TYPE_SRV, r._CLASS_IN) - def testLongName(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", r._TYPE_SRV, r._CLASS_IN) From 0a05f423ad591454a25c515d811556d10e5fc99f Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 9 Jul 2013 04:00:05 -0400 Subject: [PATCH 023/163] Allow for the failure of getServiceInfo(). Not sure why it's happening, though. --- Browser.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Browser.py b/Browser.py index 7347f39a..d00391ba 100644 --- a/Browser.py +++ b/Browser.py @@ -13,13 +13,14 @@ def addService(self, zeroconf, type, name): print "Service", name, "added" print "Type is", type info = self.r.getServiceInfo(type, name) - print "Address is", str(socket.inet_ntoa(info.getAddress())) - print "Port is", info.getPort() - print "Weight is", info.getWeight() - print "Priority is", info.getPriority() - print "Server is", info.getServer() - print "Text is", info.getText() - print "Properties are", info.getProperties() + if info: + print "Address is", str(socket.inet_ntoa(info.getAddress())) + print "Port is", info.getPort() + print "Weight is", info.getWeight() + print "Priority is", info.getPriority() + print "Server is", info.getServer() + print "Text is", info.getText() + print "Properties are", info.getProperties() if __name__ == '__main__': print "Multicast DNS Service Discovery for Python Browser test" From 8adab79a64a73e76841b37e53e55fe8aad8eb580 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 9 Jul 2013 04:06:35 -0400 Subject: [PATCH 024/163] Remove the comment about the test programs. --- README | 3 --- 1 file changed, 3 deletions(-) diff --git a/README b/README index 6cbe503b..95c8e217 100644 --- a/README +++ b/README @@ -14,7 +14,4 @@ they're generally applicable. This version also includes patches found on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere around the net -- not always well-documented, sorry. -I only use Zeroconf.py myself, so I can't vouch for the other bits here -yet. I'll check them out. - Long-term goals: Python 3.x From c13e4fab3b0b95674fbc93cd2ac30fd2ba462a24 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 9 Jul 2013 04:11:13 -0400 Subject: [PATCH 025/163] Untab the test programs. --- Browser.py | 48 +++++----- ZeroconfTest.py | 242 ++++++++++++++++++++++++------------------------ 2 files changed, 145 insertions(+), 145 deletions(-) diff --git a/Browser.py b/Browser.py index d00391ba..80f4c4f6 100644 --- a/Browser.py +++ b/Browser.py @@ -2,30 +2,30 @@ import socket class MyListener(object): - def __init__(self): - self.r = Zeroconf() - pass + def __init__(self): + self.r = Zeroconf() + pass - def removeService(self, zeroconf, type, name): - print "Service", name, "removed" + def removeService(self, zeroconf, type, name): + print "Service", name, "removed" - def addService(self, zeroconf, type, name): - print "Service", name, "added" - print "Type is", type - info = self.r.getServiceInfo(type, name) - if info: - print "Address is", str(socket.inet_ntoa(info.getAddress())) - print "Port is", info.getPort() - print "Weight is", info.getWeight() - print "Priority is", info.getPriority() - print "Server is", info.getServer() - print "Text is", info.getText() - print "Properties are", info.getProperties() + def addService(self, zeroconf, type, name): + print "Service", name, "added" + print "Type is", type + info = self.r.getServiceInfo(type, name) + if info: + print "Address is", str(socket.inet_ntoa(info.getAddress())) + print "Port is", info.getPort() + print "Weight is", info.getWeight() + print "Priority is", info.getPriority() + print "Server is", info.getServer() + print "Text is", info.getText() + print "Properties are", info.getProperties() -if __name__ == '__main__': - print "Multicast DNS Service Discovery for Python Browser test" - r = Zeroconf() - print "1. Testing browsing for a service..." - type = "_http._tcp.local." - listener = MyListener() - browser = ServiceBrowser(r, type, listener) +if __name__ == '__main__': + print "Multicast DNS Service Discovery for Python Browser test" + r = Zeroconf() + print "1. Testing browsing for a service..." + type = "_http._tcp.local." + listener = MyListener() + browser = ServiceBrowser(r, type, listener) diff --git a/ZeroconfTest.py b/ZeroconfTest.py index a1c5a214..b6e3d12e 100644 --- a/ZeroconfTest.py +++ b/ZeroconfTest.py @@ -17,7 +17,7 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - + """ __author__ = "Paul Scott-Murphy" @@ -30,133 +30,133 @@ class PacketGeneration(unittest.TestCase): - def testParseOwnPacketSimple(self): - generated = r.DNSOutgoing(0) - parsed = r.DNSIncoming(generated.packet()) - - def testParseOwnPacketSimpleUnicast(self): - generated = r.DNSOutgoing(0, 0) - parsed = r.DNSIncoming(generated.packet()) - - def testParseOwnPacketFlags(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - parsed = r.DNSIncoming(generated.packet()) - - def testParseOwnPacketQuestion(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN)) - parsed = r.DNSIncoming(generated.packet()) - - def testMatchQuestion(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) - self.assertEqual(len(generated.questions), 1) - self.assertEqual(len(generated.questions), len(parsed.questions)) - self.assertEqual(question, parsed.questions[0]) + def testParseOwnPacketSimple(self): + generated = r.DNSOutgoing(0) + parsed = r.DNSIncoming(generated.packet()) + + def testParseOwnPacketSimpleUnicast(self): + generated = r.DNSOutgoing(0, 0) + parsed = r.DNSIncoming(generated.packet()) + + def testParseOwnPacketFlags(self): + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + parsed = r.DNSIncoming(generated.packet()) + + def testParseOwnPacketQuestion(self): + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN)) + parsed = r.DNSIncoming(generated.packet()) + + def testMatchQuestion(self): + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + parsed = r.DNSIncoming(generated.packet()) + self.assertEqual(len(generated.questions), 1) + self.assertEqual(len(generated.questions), len(parsed.questions)) + self.assertEqual(question, parsed.questions[0]) class PacketForm(unittest.TestCase): - def testTransactionID(self): - """ID must be zero in a DNS-SD packet""" - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - bytes = generated.packet() - id = ord(bytes[0]) << 8 | ord(bytes[1]) - self.assertEqual(id, 0) - - def testQueryHeaderBits(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - bytes = generated.packet() - flags = ord(bytes[2]) << 8 | ord(bytes[3]) - self.assertEqual(flags, 0x0) - - def testResponseHeaderBits(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - bytes = generated.packet() - flags = ord(bytes[2]) << 8 | ord(bytes[3]) - self.assertEqual(flags, 0x8000) - - def testNumbers(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - bytes = generated.packet() - numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) - numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) - numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) - numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) - self.assertEqual(numQuestions, 0) - self.assertEqual(numAnswers, 0) - self.assertEqual(numAuthorities, 0) - self.assertEqual(numAddtionals, 0) - - def testNumbersQuestions(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - for i in range(0, 10): - generated.addQuestion(question) - bytes = generated.packet() - numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) - numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) - numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) - numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) - self.assertEqual(numQuestions, 10) - self.assertEqual(numAnswers, 0) - self.assertEqual(numAuthorities, 0) - self.assertEqual(numAddtionals, 0) - - def testNumbersAnswers(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - for i in range(0, 10): - generated.addQuestion(question) - bytes = generated.packet() - numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) - numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) - numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) - numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) - self.assertEqual(numQuestions, 10) - self.assertEqual(numAnswers, 0) - self.assertEqual(numAuthorities, 0) - self.assertEqual(numAddtionals, 0) + def testTransactionID(self): + """ID must be zero in a DNS-SD packet""" + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + bytes = generated.packet() + id = ord(bytes[0]) << 8 | ord(bytes[1]) + self.assertEqual(id, 0) + + def testQueryHeaderBits(self): + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + bytes = generated.packet() + flags = ord(bytes[2]) << 8 | ord(bytes[3]) + self.assertEqual(flags, 0x0) + + def testResponseHeaderBits(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + bytes = generated.packet() + flags = ord(bytes[2]) << 8 | ord(bytes[3]) + self.assertEqual(flags, 0x8000) + + def testNumbers(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + bytes = generated.packet() + numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) + numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) + numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) + numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) + self.assertEqual(numQuestions, 0) + self.assertEqual(numAnswers, 0) + self.assertEqual(numAuthorities, 0) + self.assertEqual(numAddtionals, 0) + + def testNumbersQuestions(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) + for i in range(0, 10): + generated.addQuestion(question) + bytes = generated.packet() + numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) + numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) + numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) + numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) + self.assertEqual(numQuestions, 10) + self.assertEqual(numAnswers, 0) + self.assertEqual(numAuthorities, 0) + self.assertEqual(numAddtionals, 0) + + def testNumbersAnswers(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) + for i in range(0, 10): + generated.addQuestion(question) + bytes = generated.packet() + numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) + numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) + numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) + numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) + self.assertEqual(numQuestions, 10) + self.assertEqual(numAnswers, 0) + self.assertEqual(numAuthorities, 0) + self.assertEqual(numAddtionals, 0) class Names(unittest.TestCase): - - def testLongName(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) - - def testExceedinglyLongName(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - name = "%slocal." % ("part." * 1000) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) - - def testExceedinglyLongNamePart(self): - name = "%s.local." % ("a" * 1000) - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - self.assertRaises(r.NamePartTooLongException, generated.packet) - - def testSameName(self): - name = "paired.local." - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) - - + + def testLongName(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + parsed = r.DNSIncoming(generated.packet()) + + def testExceedinglyLongName(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + name = "%slocal." % ("part." * 1000) + question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + parsed = r.DNSIncoming(generated.packet()) + + def testExceedinglyLongNamePart(self): + name = "%s.local." % ("a" * 1000) + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + self.assertRaises(r.NamePartTooLongException, generated.packet) + + def testSameName(self): + name = "paired.local." + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + generated.addQuestion(question) + parsed = r.DNSIncoming(generated.packet()) + + class Framework(unittest.TestCase): - def testLaunchAndClose(self): - rv = r.Zeroconf() - rv.close() - + def testLaunchAndClose(self): + rv = r.Zeroconf() + rv.close() + if __name__ == '__main__': - unittest.main() + unittest.main() From 21d7c950f50827bc8ac6dd18fb0577c11b5cefac Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 9 Jul 2013 19:49:36 -0400 Subject: [PATCH 026/163] Outdated docs. --- Zeroconf.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 871e243e..092f1912 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -2,9 +2,7 @@ Copyright (C) 2003, Paul Scott-Murphy This module provides a framework for the use of DNS Service Discovery - using IP multicast. It has been tested against the JRendezvous - implementation from StrangeBerry, - and against the mDNSResponder from Mac OS X 10.3.8. + using IP multicast. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public From df986eed46e3ec7dadc6604d0b26e4fcf0b6291a Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 9 Jul 2013 20:12:35 -0400 Subject: [PATCH 027/163] Excess spaces; don't use "len" as a label. After eblot. --- Zeroconf.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 092f1912..2919d16d 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -460,7 +460,7 @@ def __init__(self, data): self.numAnswers = 0 self.numAuthorities = 0 self.numAdditionals = 0 - + self.readHeader() self.readQuestions() self.readOthers() @@ -481,7 +481,7 @@ def readQuestions(self): for i in xrange(self.numQuestions): name = self.readName() type, clazz = self.unpack('!HH') - + question = DNSQuestion(name, type, clazz) self.questions.append(question) @@ -537,7 +537,7 @@ def readOthers(self): if rec is not None: self.answers.append(rec) - + def isQuery(self): """Returns true if this is a query""" return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY @@ -546,10 +546,10 @@ def isResponse(self): """Returns true if this is a response""" return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE - def readUTF(self, offset, len): + def readUTF(self, offset, length): """Reads a UTF-8 string of a given length from the packet""" - return unicode(self.data[offset:offset+len], 'utf-8', 'replace') - + return unicode(self.data[offset:offset+length], 'utf-8', 'replace') + def readName(self): """Reads a domain name from the packet""" result = '' @@ -558,18 +558,18 @@ def readName(self): first = off while True: - len = ord(self.data[off]) + length = ord(self.data[off]) off += 1 - if len == 0: + if length == 0: break - t = len & 0xC0 + t = length & 0xC0 if t == 0x00: - result = ''.join((result, self.readUTF(off, len) + '.')) - off += len + result = ''.join((result, self.readUTF(off, length) + '.')) + off += length elif t == 0xC0: if next < 0: next = off + 1 - off = ((len & 0x3F) << 8) | ord(self.data[off]) + off = ((length & 0x3F) << 8) | ord(self.data[off]) if off >= first: raise "Bad domain name (circular) at " + str(off) first = off @@ -586,7 +586,7 @@ def readName(self): class DNSOutgoing(object): """Object representation of an outgoing packet""" - + def __init__(self, flags, multicast=True): self.finished = False self.id = 0 @@ -595,7 +595,7 @@ def __init__(self, flags, multicast=True): self.names = {} self.data = [] self.size = 12 - + self.questions = [] self.answers = [] self.authorities = [] @@ -636,7 +636,7 @@ def insertShort(self, index, value): """Inserts an unsigned short in a certain position in the packet""" self.data.insert(index, struct.pack('!H', value)) self.size += 2 - + def writeShort(self, value): """Writes an unsigned short to the packet""" self.pack('!H', value) @@ -729,7 +729,7 @@ def packet(self): self.writeRecord(authority, 0) for additional in self.additionals: self.writeRecord(additional, 0) - + self.insertShort(0, len(self.additionals)) self.insertShort(0, len(self.authorities)) self.insertShort(0, len(self.answers)) @@ -744,7 +744,7 @@ def packet(self): class DNSCache(object): """A cache of DNS entries""" - + def __init__(self): self.cache = {} @@ -867,7 +867,7 @@ class Listener(object): It requires registration with an Engine object in order to have the read() method called when a socket is availble for reading.""" - + def __init__(self, zc): self.zc = zc self.zc.engine.addReader(self, self.zc.socket) @@ -1000,11 +1000,11 @@ def run(self): if event is not None: event(self.zc) - + class ServiceInfo(object): """Service information""" - + def __init__(self, type, name, address=None, port=None, weight=0, priority=0, properties=None, server=None): """Create a service description. @@ -1072,7 +1072,7 @@ def setText(self, text): index += 1 strs.append(text[index:index+length]) index += length - + for s in strs: try: key, value = s.split('=', 1) @@ -1093,7 +1093,7 @@ def setText(self, text): except: traceback.print_exc() self.properties = None - + def getType(self): """Type accessor""" return self.type @@ -1191,7 +1191,7 @@ def request(self, zc, timeout): result = True finally: zc.removeListener(self) - + return result def __eq__(self, other): @@ -1217,7 +1217,7 @@ def __repr__(self): result += self.text[:17] + "..." result += "]" return result - + class Zeroconf(object): """Implementation of Zeroconf Multicast DNS Service Discovery @@ -1276,7 +1276,7 @@ def __init__(self, bindaddress=None): self.cache = DNSCache() self.condition = threading.Condition() - + self.engine = Engine(self) self.listener = Listener(self) self.reaper = Reaper(self) From f772d4e5e208431378bf01d75eddc7df5119dff7 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Sun, 14 Jul 2013 13:39:17 -0400 Subject: [PATCH 028/163] If name isn't in self.names, it's unexceptional. (And yes, I actually tested, and this is faster.) --- Zeroconf.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 2919d16d..7117a199 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -662,11 +662,16 @@ def writeUTF(self, s): def writeName(self, name): """Writes a domain name to the packet""" - try: + if name in self.names: # Find existing instance of this name in packet # index = self.names[name] - except KeyError: + + # An index was found, so write a pointer to it + # + self.writeByte((index >> 8) | 0xC0) + self.writeByte(index & 0xFF) + else: # No record of this name already, so write it # out as normal, recording the location of the name # for future pointers to it. @@ -678,12 +683,6 @@ def writeName(self, name): for part in parts: self.writeUTF(part) self.writeByte(0) - return - - # An index was found, so write a pointer to it - # - self.writeByte((index >> 8) | 0xC0) - self.writeByte(index & 0xFF) def writeQuestion(self, question): """Writes a question to the packet""" From c36e3af2f6e0ea857f383f9b014f50b65fca641c Mon Sep 17 00:00:00 2001 From: William McBrine Date: Sun, 14 Jul 2013 13:57:55 -0400 Subject: [PATCH 029/163] Also unexceptional. --- Zeroconf.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Zeroconf.py b/Zeroconf.py index 7117a199..3499490c 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -229,17 +229,11 @@ def __ne__(self, other): def getClazz(self, clazz): """Class accessor""" - try: - return _CLASSES[clazz] - except: - return "?(%s)" % (clazz) + return _CLASSES.get(clazz, "?(%s)" % clazz) - def getType(self, type): + def getType(self, t): """Type accessor""" - try: - return _TYPES[type] - except: - return "?(%s)" % (type) + return _TYPES.get(t, "?(%s)" % t) def toString(self, hdr, other): """String representation with additional information""" From 6147a6ed20222851ba4438dd65366f907b4c189f Mon Sep 17 00:00:00 2001 From: William McBrine Date: Mon, 15 Jul 2013 02:10:01 -0400 Subject: [PATCH 030/163] Meaningless. --- Browser.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Browser.py b/Browser.py index 80f4c4f6..344e948c 100644 --- a/Browser.py +++ b/Browser.py @@ -4,7 +4,6 @@ class MyListener(object): def __init__(self): self.r = Zeroconf() - pass def removeService(self, zeroconf, type, name): print "Service", name, "removed" From 015bae258b5ce73a2a12361e4c9295107126963c Mon Sep 17 00:00:00 2001 From: William McBrine Date: Mon, 15 Jul 2013 15:24:07 -0400 Subject: [PATCH 031/163] Moved history to README; updated version number, etc. --- README | 62 ++++++++++++++++++++++++++++++++++++++++++++- Zeroconf.py | 66 +++++------------------------------------------- ZeroconfTest.py | 11 ++++---- ZeroconfTest2.py | 11 ++++---- 4 files changed, 79 insertions(+), 71 deletions(-) diff --git a/README b/README index 95c8e217..c6904e80 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -This is Multicast DNS Service Discovery for Python v0.12-wmcbrine +This is Multicast DNS Service Discovery for Python v0.13-wmcbrine Originally by Paul Scott-Murphy, modified by William McBrine. Zeroconf.py is the implementation file, look at the end for examples of @@ -15,3 +15,63 @@ on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere around the net -- not always well-documented, sorry. Long-term goals: Python 3.x + + +History +------- +0.13 - Various minor changes; see git for details. + No longer compatible with Python 2.2. Only tested with 2.5-2.7. + Fork by William McBrine. + +0.12 - allow selection of binding interface + typo fix - Thanks A. M. Kuchlingi + removed all use of word 'Rendezvous' - this is an API change + +0.11 - correction to comments for addListener method + support for new record types seen from OS X + - IPv6 address + - hostinfo + ignore unknown DNS record types + fixes to name decoding + works alongside other processes using port 5353 + (e.g. on Mac OS X) + tested against Mac OS X 10.3.2's mDNSResponder + corrections to removal of list entries for service browser + +0.10 - Jonathon Paisley contributed these corrections: + always multicast replies, even when query is unicast + correct a pointer encoding problem + can now write records in any order + traceback shown on failure + better TXT record parsing + server is now separate from name + can cancel a service browser + + modified some unit tests to accommodate these changes + +0.09 - remove all records on service unregistration + fix DOS security problem with readName + +0.08 - changed licensing to LGPL + +0.07 - faster shutdown on engine + pointer encoding of outgoing names + ServiceBrowser now works + new unit tests + +0.06 - small improvements with unit tests + added defined exception types + new style objects + fixed hostname/interface problem + fixed socket timeout problem + fixed addServiceListener() typo bug + using select() for socket reads + tested on Debian unstable with Python 2.2.2 + +0.05 - ensure case insensitivty on domain names + support for unicast DNS queries + +0.04 - added some unit tests + added __ne__ adjuncts where required + ensure names end in '.local.' + timeout on receiving socket for clean shutdown diff --git a/Zeroconf.py b/Zeroconf.py index 3499490c..69341f8d 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -1,5 +1,5 @@ -""" Multicast DNS Service Discovery for Python, v0.12-wmcbrine - Copyright (C) 2003, Paul Scott-Murphy +""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine + Copyright 2003 Paul Scott-Murphy, 2013 William McBrine This module provides a framework for the use of DNS Service Discovery using IP multicast. @@ -20,64 +20,10 @@ """ -"""0.12-wmcbrine update - see git for changes""" - -"""0.12 update - allow selection of binding interface - typo fix - Thanks A. M. Kuchlingi - removed all use of word 'Rendezvous' - this is an API change""" - -"""0.11 update - correction to comments for addListener method - support for new record types seen from OS X - - IPv6 address - - hostinfo - ignore unknown DNS record types - fixes to name decoding - works alongside other processes using port 5353 - (e.g. on Mac OS X) - tested against Mac OS X 10.3.2's mDNSResponder - corrections to removal of list entries for service browser""" - -"""0.10 update - Jonathon Paisley contributed these corrections: - always multicast replies, even when query is unicast - correct a pointer encoding problem - can now write records in any order - traceback shown on failure - better TXT record parsing - server is now separate from name - can cancel a service browser - - modified some unit tests to accommodate these changes""" - -"""0.09 update - remove all records on service unregistration - fix DOS security problem with readName""" - -"""0.08 update - changed licensing to LGPL""" - -"""0.07 update - faster shutdown on engine - pointer encoding of outgoing names - ServiceBrowser now works - new unit tests""" - -"""0.06 update - small improvements with unit tests - added defined exception types - new style objects - fixed hostname/interface problem - fixed socket timeout problem - fixed addServiceListener() typo bug - using select() for socket reads - tested on Debian unstable with Python 2.2.2""" - -"""0.05 update - ensure case insensitivty on domain names - support for unicast DNS queries""" - -"""0.04 update - added some unit tests - added __ne__ adjuncts where required - ensure names end in '.local.' - timeout on receiving socket for clean shutdown""" - -__author__ = "Paul Scott-Murphy" -__email__ = "paul at scott dash murphy dot com" -__version__ = "0.12-wmcbrine" +__author__ = 'Paul Scott-Murphy' +__maintainer__ = 'William McBrine ' +__version__ = '0.13-wmcbrine' +__license__ = 'LGPL' import time import struct diff --git a/ZeroconfTest.py b/ZeroconfTest.py index b6e3d12e..8c07e8de 100644 --- a/ZeroconfTest.py +++ b/ZeroconfTest.py @@ -1,5 +1,5 @@ -""" Multicast DNS Service Discovery for Python, v0.12 - Copyright (C) 2003, Paul Scott-Murphy +""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine + Copyright 2003 Paul Scott-Murphy, 2013 William McBrine This module provides a unit test suite for the Multicast DNS Service Discovery for Python module. @@ -20,9 +20,10 @@ """ -__author__ = "Paul Scott-Murphy" -__email__ = "paul at scott dash murphy dot com" -__version__ = "0.12" +__author__ = 'Paul Scott-Murphy' +__maintainer__ = 'William McBrine ' +__version__ = '0.13-wmcbrine' +__license__ = 'LGPL' import Zeroconf as r import unittest diff --git a/ZeroconfTest2.py b/ZeroconfTest2.py index 2071e3d6..59fc8dd2 100644 --- a/ZeroconfTest2.py +++ b/ZeroconfTest2.py @@ -1,5 +1,5 @@ -""" Multicast DNS Service Discovery for Python, v0.11 - Copyright (C) 2003, Paul Scott-Murphy +""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine + Copyright 2003 Paul Scott-Murphy, 2013 William McBrine This module provides a unit test suite for the Multicast DNS Service Discovery for Python module. @@ -20,9 +20,10 @@ """ -__author__ = "Paul Scott-Murphy" -__email__ = "paul at scott dash murphy dot com" -__version__ = "0.12" +__author__ = 'Paul Scott-Murphy' +__maintainer__ = 'William McBrine ' +__version__ = '0.13-wmcbrine' +__license__ = 'LGPL' from Zeroconf import * import socket From f6615a9d7632f3510d2f0a36cab155ac753141ab Mon Sep 17 00:00:00 2001 From: William McBrine Date: Tue, 17 Jun 2014 18:07:13 -0400 Subject: [PATCH 032/163] Extraneous spaces. --- Browser.py | 2 +- Zeroconf.py | 128 ++++++++++++++++++++++++------------------------ ZeroconfTest.py | 18 ++++--- 3 files changed, 75 insertions(+), 73 deletions(-) diff --git a/Browser.py b/Browser.py index 344e948c..86b1ff6a 100644 --- a/Browser.py +++ b/Browser.py @@ -21,7 +21,7 @@ def addService(self, zeroconf, type, name): print "Text is", info.getText() print "Properties are", info.getProperties() -if __name__ == '__main__': +if __name__ == '__main__': print "Multicast DNS Service Discovery for Python Browser test" r = Zeroconf() print "1. Testing browsing for a service..." diff --git a/Zeroconf.py b/Zeroconf.py index 69341f8d..9052c1cf 100644 --- a/Zeroconf.py +++ b/Zeroconf.py @@ -49,9 +49,9 @@ # Some DNS constants _MDNS_ADDR = '224.0.0.251' -_MDNS_PORT = 5353; -_DNS_PORT = 53; -_DNS_TTL = 60 * 60; # one hour default TTL +_MDNS_PORT = 5353 +_DNS_PORT = 53 +_DNS_TTL = 60 * 60 # one hour default TTL _MAX_MSG_TYPICAL = 1460 # unused _MAX_MSG_ABSOLUTE = 8972 @@ -154,7 +154,7 @@ class BadTypeInNameException(Exception): class DNSEntry(object): """A DNS entry""" - + def __init__(self, name, type, clazz): self.key = name.lower() self.name = name @@ -198,7 +198,7 @@ def toString(self, hdr, other): class DNSQuestion(DNSEntry): """A DNS question entry""" - + def __init__(self, name, type, clazz): #if not name.endswith(".local."): # raise NonLocalNameException @@ -275,7 +275,7 @@ def toString(self, other): class DNSAddress(DNSRecord): """A DNS address record""" - + def __init__(self, name, type, clazz, ttl, address): DNSRecord.__init__(self, name, type, clazz, ttl) self.address = address @@ -316,10 +316,10 @@ def __eq__(self, other): def __repr__(self): """String representation""" return self.cpu + " " + self.os - + class DNSPointer(DNSRecord): """A DNS pointer record""" - + def __init__(self, name, type, clazz, ttl, alias): DNSRecord.__init__(self, name, type, clazz, ttl) self.alias = alias @@ -338,7 +338,7 @@ def __repr__(self): class DNSText(DNSRecord): """A DNS text record""" - + def __init__(self, name, type, clazz, ttl, text): DNSRecord.__init__(self, name, type, clazz, ttl) self.text = text @@ -360,7 +360,7 @@ def __repr__(self): class DNSService(DNSRecord): """A DNS service record""" - + def __init__(self, name, type, clazz, ttl, priority, weight, port, server): DNSRecord.__init__(self, name, type, clazz, ttl) self.priority = priority @@ -389,7 +389,7 @@ def __repr__(self): class DNSIncoming(object): """Object representation of an incoming DNS packet""" - + def __init__(self, data): """Constructor from string holding bytes of packet""" self.offset = 0 @@ -446,7 +446,7 @@ def readUnsignedShort(self): return self.unpack('!H')[0] def readOthers(self): - """Reads the answers, authorities and additionals section of the + """Reads the answers, authorities and additionals section of the packet""" n = self.numAnswers + self.numAuthorities + self.numAdditionals for i in xrange(n): @@ -522,8 +522,8 @@ def readName(self): self.offset = off return result - - + + class DNSOutgoing(object): """Object representation of an outgoing packet""" @@ -649,7 +649,7 @@ def writeRecord(self, record, now): self.size += 2 record.write(self) self.size -= 2 - + length = len(''.join(self.data[index:])) self.insertShort(index, length) # Here is the short we adjusted for @@ -727,7 +727,7 @@ def entriesWithName(self, name): def entries(self): """Returns a list of all entries""" - def add(x, y): return x+y + def add(x, y): return x + y try: return reduce(add, self.cache.values()) except: @@ -842,7 +842,7 @@ def handle_read(self): class Reaper(threading.Thread): """A Reaper is used by this module to remove cache entries that have expired.""" - + def __init__(self, zc): threading.Thread.__init__(self) self.zc = zc @@ -866,7 +866,7 @@ class ServiceBrowser(threading.Thread): The listener object will have its addService() and removeService() methods called when this browser discovers changes in the services availability.""" - + def __init__(self, zc, type, listener): """Creates a browser for a specific type""" threading.Thread.__init__(self) @@ -895,14 +895,14 @@ def updateRecord(self, zc, now, record): oldrecord.resetTTL(record) else: del(self.services[record.alias.lower()]) - callback = lambda x: self.listener.removeService(x, + callback = lambda x: self.listener.removeService(x, self.type, record.alias) self.list.append(callback) return except: if not expired: self.services[record.alias.lower()] = record - callback = lambda x: self.listener.addService(x, + callback = lambda x: self.listener.addService(x, self.type, record.alias) self.list.append(callback) @@ -944,7 +944,7 @@ def run(self): class ServiceInfo(object): """Service information""" - def __init__(self, type, name, address=None, port=None, weight=0, + def __init__(self, type, name, address=None, port=None, weight=0, priority=0, properties=None, server=None): """Create a service description. @@ -954,7 +954,7 @@ def __init__(self, type, name, address=None, port=None, weight=0, port: port that the service runs on weight: weight of the service priority: priority of the service - properties: dictionary of properties (or a string holding the + properties: dictionary of properties (or a string holding the bytes for the text field) server: fully qualified name for service host (defaults to name)""" @@ -1085,7 +1085,7 @@ def updateRecord(self, zc, now, record): self.weight = record.weight self.priority = record.priority #self.address = None - self.updateRecord(zc, now, + self.updateRecord(zc, now, zc.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) elif record.type == _TYPE_TXT: if record.name == self.name: @@ -1102,24 +1102,24 @@ def request(self, zc, timeout): result = False try: zc.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) - while (self.server is None or self.address is None or + while (self.server is None or self.address is None or self.text is None): if last <= now: return False if next <= now: out = DNSOutgoing(_FLAGS_QR_QUERY) - out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, + out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN)) - out.addAnswerAtTime(zc.cache.getByDetails(self.name, + out.addAnswerAtTime(zc.cache.getByDetails(self.name, _TYPE_SRV, _CLASS_IN), now) - out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, + out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN)) - out.addAnswerAtTime(zc.cache.getByDetails(self.name, + out.addAnswerAtTime(zc.cache.getByDetails(self.name, _TYPE_TXT, _CLASS_IN), now) if self.server is not None: - out.addQuestion(DNSQuestion(self.server, + out.addQuestion(DNSQuestion(self.server, _TYPE_A, _CLASS_IN)) - out.addAnswerAtTime(zc.cache.getByDetails(self.server, + out.addAnswerAtTime(zc.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN), now) zc.send(out) next = now + delay @@ -1145,7 +1145,7 @@ def __ne__(self, other): def __repr__(self): """String representation""" - result = "service[%s,%s:%s," % (self.name, + result = "service[%s,%s:%s," % (self.name, socket.inet_ntoa(self.getAddress()), self.port) if self.text is None: result += "None" @@ -1202,9 +1202,9 @@ def __init__(self, bindaddress=None): # the SO_REUSE* options have been set, so ignore it # pass - #self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, + #self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, # socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) - self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, + self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) self.listeners = [] @@ -1230,7 +1230,7 @@ def wait(self, timeout): """Calling thread waits for a given number of milliseconds or until notified.""" self.condition.acquire() - self.condition.wait(timeout/1000) + self.condition.wait(timeout / 1000) self.condition.release() def notifyAll(self): @@ -1270,9 +1270,9 @@ def registerService(self, info, ttl=_DNS_TTL): self.checkService(info) self.services[info.name.lower()] = info if info.type in self.servicetypes: - self.servicetypes[info.type]+=1 + self.servicetypes[info.type] += 1 else: - self.servicetypes[info.type]=1 + self.servicetypes[info.type] = 1 now = currentTimeMillis() nextTime = now i = 0 @@ -1282,15 +1282,15 @@ def registerService(self, info, ttl=_DNS_TTL): now = currentTimeMillis() continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, ttl, info.priority, info.weight, info.port, + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, ttl, info.priority, info.weight, info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, ttl, info.address), 0) self.send(out) i += 1 @@ -1300,8 +1300,8 @@ def unregisterService(self, info): """Unregister a service.""" try: del(self.services[info.name.lower()]) - if self.servicetypes[info.type]>1: - self.servicetypes[info.type]-=1 + if self.servicetypes[info.type] > 1: + self.servicetypes[info.type] -= 1 else: del self.servicetypes[info.type] except: @@ -1315,15 +1315,15 @@ def unregisterService(self, info): now = currentTimeMillis() continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, 0, info.priority, info.weight, info.port, + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, 0, info.priority, info.weight, info.port, info.name), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) self.send(out) i += 1 @@ -1342,15 +1342,15 @@ def unregisterAllServices(self): continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) for info in self.services.values(): - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, + out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, 0, info.priority, info.weight, + out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, 0, info.priority, info.weight, info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, + out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, + out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0) self.send(out) i += 1 @@ -1430,7 +1430,7 @@ def handleResponse(self, msg): record = entry else: self.cache.add(record) - + self.updateRecord(now, record) def handleQuery(self, msg, addr, port): @@ -1444,15 +1444,15 @@ def handleQuery(self, msg, addr, port): out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, False) for question in msg.questions: out.addQuestion(question) - + for question in msg.questions: if question.type == _TYPE_PTR: if question.name == "_services._dns-sd._udp.local.": for stype in self.servicetypes.keys(): if out is None: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswer(msg, - DNSPointer("_services._dns-sd._udp.local.", + out.addAnswer(msg, + DNSPointer("_services._dns-sd._udp.local.", _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) for service in self.services.values(): if question.name == service.type: @@ -1465,7 +1465,7 @@ def handleQuery(self, msg, addr, port): try: if out is None: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - + # Answer A record queries for any service addresses we know if question.type in (_TYPE_A, _TYPE_ANY): for service in self.services.values(): @@ -1476,11 +1476,11 @@ def handleQuery(self, msg, addr, port): service = self.services.get(question.name.lower(), None) if not service: continue - + if question.type in (_TYPE_SRV, _TYPE_ANY): out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.priority, service.weight, + _DNS_TTL, service.priority, service.weight, service.port, service.server)) if question.type in (_TYPE_TXT, _TYPE_ANY): out.addAnswer(msg, DNSText(question.name, @@ -1492,7 +1492,7 @@ def handleQuery(self, msg, addr, port): _DNS_TTL, service.address)) except: traceback.print_exc() - + if out is not None and out.answers: out.id = msg.id self.send(out, addr, port) @@ -1524,17 +1524,17 @@ def close(self): socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) self.socket.close() - + # Test a few module features, including service registration, service # query (for Zoe), and service unregistration. -if __name__ == '__main__': +if __name__ == '__main__': print "Multicast DNS Service Discovery for Python, version", __version__ r = Zeroconf() print "1. Testing registration of a service..." desc = {'version':'0.10','a':'test value', 'b':'another value'} info = ServiceInfo("_http._tcp.local.", - "My Service Name._http._tcp.local.", + "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) print " Registering service..." r.registerService(info) diff --git a/ZeroconfTest.py b/ZeroconfTest.py index 8c07e8de..d28334c6 100644 --- a/ZeroconfTest.py +++ b/ZeroconfTest.py @@ -38,14 +38,15 @@ def testParseOwnPacketSimple(self): def testParseOwnPacketSimpleUnicast(self): generated = r.DNSOutgoing(0, 0) parsed = r.DNSIncoming(generated.packet()) - + def testParseOwnPacketFlags(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) parsed = r.DNSIncoming(generated.packet()) def testParseOwnPacketQuestion(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN)) + generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, + r._CLASS_IN)) parsed = r.DNSIncoming(generated.packet()) def testMatchQuestion(self): @@ -66,7 +67,7 @@ def testTransactionID(self): bytes = generated.packet() id = ord(bytes[0]) << 8 | ord(bytes[1]) self.assertEqual(id, 0) - + def testQueryHeaderBits(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() @@ -105,7 +106,7 @@ def testNumbersQuestions(self): self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) self.assertEqual(numAddtionals, 0) - + def testNumbersAnswers(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) @@ -123,10 +124,11 @@ def testNumbersAnswers(self): class Names(unittest.TestCase): - + def testLongName(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", r._TYPE_SRV, r._CLASS_IN) + question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", + r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) parsed = r.DNSIncoming(generated.packet()) @@ -152,12 +154,12 @@ def testSameName(self): generated.addQuestion(question) parsed = r.DNSIncoming(generated.packet()) - + class Framework(unittest.TestCase): def testLaunchAndClose(self): rv = r.Zeroconf() rv.close() - + if __name__ == '__main__': unittest.main() From 416054d407013af8678928b949d6579df4044d46 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 09:23:10 -0400 Subject: [PATCH 033/163] testNumbersAnswers() was identical to testNumbersQuestions(). (Presumably it was intended to test addAnswer() instead...) --- ZeroconfTest.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/ZeroconfTest.py b/ZeroconfTest.py index d28334c6..9eb484d7 100644 --- a/ZeroconfTest.py +++ b/ZeroconfTest.py @@ -107,21 +107,6 @@ def testNumbersQuestions(self): self.assertEqual(numAuthorities, 0) self.assertEqual(numAddtionals, 0) - def testNumbersAnswers(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - for i in range(0, 10): - generated.addQuestion(question) - bytes = generated.packet() - numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) - numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) - numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) - numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) - self.assertEqual(numQuestions, 10) - self.assertEqual(numAnswers, 0) - self.assertEqual(numAuthorities, 0) - self.assertEqual(numAddtionals, 0) - class Names(unittest.TestCase): From b36e7d5dd5922b1739911878b29aba921ec9ecb6 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 09:31:49 -0400 Subject: [PATCH 034/163] Don't need range() here. --- ZeroconfTest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ZeroconfTest.py b/ZeroconfTest.py index 9eb484d7..e47d98a3 100644 --- a/ZeroconfTest.py +++ b/ZeroconfTest.py @@ -95,7 +95,7 @@ def testNumbers(self): def testNumbersQuestions(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - for i in range(0, 10): + for i in xrange(10): generated.addQuestion(question) bytes = generated.packet() numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) From ad6b1ecf9fa71a5ec14f7a08fc3d6a689a19e6d2 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 09:53:50 -0400 Subject: [PATCH 035/163] Replaced unwrapped "lgpl.txt" with traditional "COPYING". --- COPYING | 458 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lgpl.txt | 151 ------------------ 2 files changed, 458 insertions(+), 151 deletions(-) create mode 100644 COPYING delete mode 100644 lgpl.txt diff --git a/COPYING b/COPYING new file mode 100644 index 00000000..2cba2ac7 --- /dev/null +++ b/COPYING @@ -0,0 +1,458 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS diff --git a/lgpl.txt b/lgpl.txt deleted file mode 100644 index fdbedf8e..00000000 --- a/lgpl.txt +++ /dev/null @@ -1,151 +0,0 @@ -GNU Lesser General Public License - -Version 2.1, February 1999 - - Copyright (C) 1991, 1999 Free Software Foundation, Inc. - 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - [This is the first released version of the Lesser GPL. It also counts - as the successor of the GNU Library Public License, version 2, hence - the version number 2.1.] - -Preamble - -The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. - -This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. - -When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. - -To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. - -For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. - -We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. - -To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. - -Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. - -Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. - -When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. - -We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. - -For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. - -In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. - -Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. - -The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. -TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - -0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". - -A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. - -The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) - -"Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. - -Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. - -1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. - -You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. - -2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: - - a) The modified work must itself be a software library. - - b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. - - c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. - - d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. - - (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) - - These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. - - Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. - - In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. - -3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. - -Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. - -This option is useful when you wish to copy part of the code of the Library into a program that is not a library. - -4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. - -If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. - -5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. - -However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. - -When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. - -If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) - -Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. - -6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. - -You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: - - a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) - - b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. - - c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. - - d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. - - e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. - -For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. - -It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. - -7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: - - a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. - - b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. - -8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. - -9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. - -10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. - -11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. - -If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. - -It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. - -This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. - -12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. - -13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. - -14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. - -NO WARRANTY - -15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - -16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - -END OF TERMS AND CONDITIONS \ No newline at end of file From 4bb88b0952833b84c15c85190c0a9cac01922cbe Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 10:02:25 -0400 Subject: [PATCH 036/163] Renamed tests. --- ZeroconfTest.py => zunittest.py | 0 Browser.py => zwebbrowse.py | 0 ZeroconfTest2.py => zwebtest.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename ZeroconfTest.py => zunittest.py (100%) rename Browser.py => zwebbrowse.py (100%) rename ZeroconfTest2.py => zwebtest.py (100%) diff --git a/ZeroconfTest.py b/zunittest.py similarity index 100% rename from ZeroconfTest.py rename to zunittest.py diff --git a/Browser.py b/zwebbrowse.py similarity index 100% rename from Browser.py rename to zwebbrowse.py diff --git a/ZeroconfTest2.py b/zwebtest.py similarity index 100% rename from ZeroconfTest2.py rename to zwebtest.py From 5541813fbb8e1d7b233d09ee2d20ac0ca322a9f2 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 10:06:59 -0400 Subject: [PATCH 037/163] The great lowercasing. --- Zeroconf.py => zeroconf.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Zeroconf.py => zeroconf.py (100%) diff --git a/Zeroconf.py b/zeroconf.py similarity index 100% rename from Zeroconf.py rename to zeroconf.py From e292868f9c7e817cb04dfce2d545f45db4041e5e Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 10:09:34 -0400 Subject: [PATCH 038/163] Lowercase imports. --- zunittest.py | 2 +- zwebbrowse.py | 2 +- zwebtest.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/zunittest.py b/zunittest.py index e47d98a3..727cd671 100644 --- a/zunittest.py +++ b/zunittest.py @@ -25,7 +25,7 @@ __version__ = '0.13-wmcbrine' __license__ = 'LGPL' -import Zeroconf as r +import zeroconf as r import unittest diff --git a/zwebbrowse.py b/zwebbrowse.py index 86b1ff6a..3f9edaff 100644 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -1,4 +1,4 @@ -from Zeroconf import * +from zeroconf import * import socket class MyListener(object): diff --git a/zwebtest.py b/zwebtest.py index 59fc8dd2..a381e821 100644 --- a/zwebtest.py +++ b/zwebtest.py @@ -25,7 +25,7 @@ __version__ = '0.13-wmcbrine' __license__ = 'LGPL' -from Zeroconf import * +from zeroconf import * import socket desc = {'path':'/~paulsm/'} From 1dc3436e6357b66d0bb53f9b285f123b164984da Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 10:11:41 -0400 Subject: [PATCH 039/163] De-DOSification. --- zunittest.py | 300 +++++++++++++++++++++++++------------------------- zwebbrowse.py | 60 +++++----- zwebtest.py | 74 ++++++------- 3 files changed, 217 insertions(+), 217 deletions(-) diff --git a/zunittest.py b/zunittest.py index 727cd671..a45ddcd1 100644 --- a/zunittest.py +++ b/zunittest.py @@ -1,150 +1,150 @@ -""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine - Copyright 2003 Paul Scott-Murphy, 2013 William McBrine - - This module provides a unit test suite for the Multicast DNS - Service Discovery for Python module. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -""" - -__author__ = 'Paul Scott-Murphy' -__maintainer__ = 'William McBrine ' -__version__ = '0.13-wmcbrine' -__license__ = 'LGPL' - -import zeroconf as r -import unittest - - -class PacketGeneration(unittest.TestCase): - - def testParseOwnPacketSimple(self): - generated = r.DNSOutgoing(0) - parsed = r.DNSIncoming(generated.packet()) - - def testParseOwnPacketSimpleUnicast(self): - generated = r.DNSOutgoing(0, 0) - parsed = r.DNSIncoming(generated.packet()) - - def testParseOwnPacketFlags(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - parsed = r.DNSIncoming(generated.packet()) - - def testParseOwnPacketQuestion(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, - r._CLASS_IN)) - parsed = r.DNSIncoming(generated.packet()) - - def testMatchQuestion(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) - self.assertEqual(len(generated.questions), 1) - self.assertEqual(len(generated.questions), len(parsed.questions)) - self.assertEqual(question, parsed.questions[0]) - - -class PacketForm(unittest.TestCase): - - def testTransactionID(self): - """ID must be zero in a DNS-SD packet""" - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - bytes = generated.packet() - id = ord(bytes[0]) << 8 | ord(bytes[1]) - self.assertEqual(id, 0) - - def testQueryHeaderBits(self): - generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - bytes = generated.packet() - flags = ord(bytes[2]) << 8 | ord(bytes[3]) - self.assertEqual(flags, 0x0) - - def testResponseHeaderBits(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - bytes = generated.packet() - flags = ord(bytes[2]) << 8 | ord(bytes[3]) - self.assertEqual(flags, 0x8000) - - def testNumbers(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - bytes = generated.packet() - numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) - numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) - numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) - numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) - self.assertEqual(numQuestions, 0) - self.assertEqual(numAnswers, 0) - self.assertEqual(numAuthorities, 0) - self.assertEqual(numAddtionals, 0) - - def testNumbersQuestions(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - for i in xrange(10): - generated.addQuestion(question) - bytes = generated.packet() - numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) - numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) - numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) - numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) - self.assertEqual(numQuestions, 10) - self.assertEqual(numAnswers, 0) - self.assertEqual(numAuthorities, 0) - self.assertEqual(numAddtionals, 0) - - -class Names(unittest.TestCase): - - def testLongName(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", - r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) - - def testExceedinglyLongName(self): - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - name = "%slocal." % ("part." * 1000) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) - - def testExceedinglyLongNamePart(self): - name = "%s.local." % ("a" * 1000) - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - self.assertRaises(r.NamePartTooLongException, generated.packet) - - def testSameName(self): - name = "paired.local." - generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) - question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) - - -class Framework(unittest.TestCase): - - def testLaunchAndClose(self): - rv = r.Zeroconf() - rv.close() - -if __name__ == '__main__': - unittest.main() +""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine + Copyright 2003 Paul Scott-Murphy, 2013 William McBrine + + This module provides a unit test suite for the Multicast DNS + Service Discovery for Python module. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" + +__author__ = 'Paul Scott-Murphy' +__maintainer__ = 'William McBrine ' +__version__ = '0.13-wmcbrine' +__license__ = 'LGPL' + +import zeroconf as r +import unittest + + +class PacketGeneration(unittest.TestCase): + + def testParseOwnPacketSimple(self): + generated = r.DNSOutgoing(0) + parsed = r.DNSIncoming(generated.packet()) + + def testParseOwnPacketSimpleUnicast(self): + generated = r.DNSOutgoing(0, 0) + parsed = r.DNSIncoming(generated.packet()) + + def testParseOwnPacketFlags(self): + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + parsed = r.DNSIncoming(generated.packet()) + + def testParseOwnPacketQuestion(self): + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, + r._CLASS_IN)) + parsed = r.DNSIncoming(generated.packet()) + + def testMatchQuestion(self): + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + parsed = r.DNSIncoming(generated.packet()) + self.assertEqual(len(generated.questions), 1) + self.assertEqual(len(generated.questions), len(parsed.questions)) + self.assertEqual(question, parsed.questions[0]) + + +class PacketForm(unittest.TestCase): + + def testTransactionID(self): + """ID must be zero in a DNS-SD packet""" + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + bytes = generated.packet() + id = ord(bytes[0]) << 8 | ord(bytes[1]) + self.assertEqual(id, 0) + + def testQueryHeaderBits(self): + generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) + bytes = generated.packet() + flags = ord(bytes[2]) << 8 | ord(bytes[3]) + self.assertEqual(flags, 0x0) + + def testResponseHeaderBits(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + bytes = generated.packet() + flags = ord(bytes[2]) << 8 | ord(bytes[3]) + self.assertEqual(flags, 0x8000) + + def testNumbers(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + bytes = generated.packet() + numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) + numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) + numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) + numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) + self.assertEqual(numQuestions, 0) + self.assertEqual(numAnswers, 0) + self.assertEqual(numAuthorities, 0) + self.assertEqual(numAddtionals, 0) + + def testNumbersQuestions(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) + for i in xrange(10): + generated.addQuestion(question) + bytes = generated.packet() + numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) + numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) + numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) + numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) + self.assertEqual(numQuestions, 10) + self.assertEqual(numAnswers, 0) + self.assertEqual(numAuthorities, 0) + self.assertEqual(numAddtionals, 0) + + +class Names(unittest.TestCase): + + def testLongName(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", + r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + parsed = r.DNSIncoming(generated.packet()) + + def testExceedinglyLongName(self): + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + name = "%slocal." % ("part." * 1000) + question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + parsed = r.DNSIncoming(generated.packet()) + + def testExceedinglyLongNamePart(self): + name = "%s.local." % ("a" * 1000) + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + self.assertRaises(r.NamePartTooLongException, generated.packet) + + def testSameName(self): + name = "paired.local." + generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) + question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) + generated.addQuestion(question) + generated.addQuestion(question) + parsed = r.DNSIncoming(generated.packet()) + + +class Framework(unittest.TestCase): + + def testLaunchAndClose(self): + rv = r.Zeroconf() + rv.close() + +if __name__ == '__main__': + unittest.main() diff --git a/zwebbrowse.py b/zwebbrowse.py index 3f9edaff..d82a3686 100644 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -1,30 +1,30 @@ -from zeroconf import * -import socket - -class MyListener(object): - def __init__(self): - self.r = Zeroconf() - - def removeService(self, zeroconf, type, name): - print "Service", name, "removed" - - def addService(self, zeroconf, type, name): - print "Service", name, "added" - print "Type is", type - info = self.r.getServiceInfo(type, name) - if info: - print "Address is", str(socket.inet_ntoa(info.getAddress())) - print "Port is", info.getPort() - print "Weight is", info.getWeight() - print "Priority is", info.getPriority() - print "Server is", info.getServer() - print "Text is", info.getText() - print "Properties are", info.getProperties() - -if __name__ == '__main__': - print "Multicast DNS Service Discovery for Python Browser test" - r = Zeroconf() - print "1. Testing browsing for a service..." - type = "_http._tcp.local." - listener = MyListener() - browser = ServiceBrowser(r, type, listener) +from zeroconf import * +import socket + +class MyListener(object): + def __init__(self): + self.r = Zeroconf() + + def removeService(self, zeroconf, type, name): + print "Service", name, "removed" + + def addService(self, zeroconf, type, name): + print "Service", name, "added" + print "Type is", type + info = self.r.getServiceInfo(type, name) + if info: + print "Address is", str(socket.inet_ntoa(info.getAddress())) + print "Port is", info.getPort() + print "Weight is", info.getWeight() + print "Priority is", info.getPriority() + print "Server is", info.getServer() + print "Text is", info.getText() + print "Properties are", info.getProperties() + +if __name__ == '__main__': + print "Multicast DNS Service Discovery for Python Browser test" + r = Zeroconf() + print "1. Testing browsing for a service..." + type = "_http._tcp.local." + listener = MyListener() + browser = ServiceBrowser(r, type, listener) diff --git a/zwebtest.py b/zwebtest.py index a381e821..5d8c9b0c 100644 --- a/zwebtest.py +++ b/zwebtest.py @@ -1,37 +1,37 @@ -""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine - Copyright 2003 Paul Scott-Murphy, 2013 William McBrine - - This module provides a unit test suite for the Multicast DNS - Service Discovery for Python module. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -""" - -__author__ = 'Paul Scott-Murphy' -__maintainer__ = 'William McBrine ' -__version__ = '0.13-wmcbrine' -__license__ = 'LGPL' - -from zeroconf import * -import socket - -desc = {'path':'/~paulsm/'} -info = ServiceInfo("_http._tcp.local.", "Paul's Test Web Site._http._tcp.local.", socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.") - -r = Zeroconf() -print "Registration of a service..." -r.registerService(info) -print "Waiting..." +""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine + Copyright 2003 Paul Scott-Murphy, 2013 William McBrine + + This module provides a unit test suite for the Multicast DNS + Service Discovery for Python module. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +""" + +__author__ = 'Paul Scott-Murphy' +__maintainer__ = 'William McBrine ' +__version__ = '0.13-wmcbrine' +__license__ = 'LGPL' + +from zeroconf import * +import socket + +desc = {'path':'/~paulsm/'} +info = ServiceInfo("_http._tcp.local.", "Paul's Test Web Site._http._tcp.local.", socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.") + +r = Zeroconf() +print "Registration of a service..." +r.registerService(info) +print "Waiting..." From 4e396025ed666775973d54a50b69e8f635e28658 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 11:14:14 -0400 Subject: [PATCH 040/163] Updated FSF address. --- zeroconf.py | 3 ++- zunittest.py | 3 ++- zwebtest.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 9052c1cf..00af1350 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -16,7 +16,8 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + USA """ diff --git a/zunittest.py b/zunittest.py index a45ddcd1..02322913 100644 --- a/zunittest.py +++ b/zunittest.py @@ -16,7 +16,8 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + USA """ diff --git a/zwebtest.py b/zwebtest.py index 5d8c9b0c..4e167779 100644 --- a/zwebtest.py +++ b/zwebtest.py @@ -16,7 +16,8 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + USA """ From 2a000c589302147129eed990c842b38ac61f7514 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 12:12:52 -0400 Subject: [PATCH 041/163] New names, numbers. --- README | 13 +++++++------ zeroconf.py | 6 +++--- zunittest.py | 6 +++--- zwebtest.py | 6 +++--- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/README b/README index c6904e80..c2666f58 100644 --- a/README +++ b/README @@ -1,13 +1,14 @@ -This is Multicast DNS Service Discovery for Python v0.13-wmcbrine +This is Multicast DNS Service Discovery for Python v0.14-wmcbrine Originally by Paul Scott-Murphy, modified by William McBrine. -Zeroconf.py is the implementation file, look at the end for examples of -basic use. You can also view Browser.py to see how to browse for services. +zeroconf.py is the implementation file, look at the end for examples of +basic use. You can also view zwebbrowse.py to see how to browse for +services. This fork is used in all of my TiVo-related projects: HME for Python -(and therefore HME/VLC), Network Remote, and pyTivo. Until now, I was -tracking the changes for Zeroconf.py in three separate repos. I figured -I should have an authoritative source. +(and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. +Before this, I was tracking the changes for zeroconf.py in three +separate repos. I figured I should have an authoritative source. Although I make changes based on my experience with TiVos, I expect that they're generally applicable. This version also includes patches found diff --git a/zeroconf.py b/zeroconf.py index 00af1350..ab9f071b 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1,5 +1,5 @@ -""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine - Copyright 2003 Paul Scott-Murphy, 2013 William McBrine +""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine + Copyright 2003 Paul Scott-Murphy, 2014 William McBrine This module provides a framework for the use of DNS Service Discovery using IP multicast. @@ -23,7 +23,7 @@ __author__ = 'Paul Scott-Murphy' __maintainer__ = 'William McBrine ' -__version__ = '0.13-wmcbrine' +__version__ = '0.14-wmcbrine' __license__ = 'LGPL' import time diff --git a/zunittest.py b/zunittest.py index 02322913..7e679e5c 100644 --- a/zunittest.py +++ b/zunittest.py @@ -1,5 +1,5 @@ -""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine - Copyright 2003 Paul Scott-Murphy, 2013 William McBrine +""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine + Copyright 2003 Paul Scott-Murphy, 2014 William McBrine This module provides a unit test suite for the Multicast DNS Service Discovery for Python module. @@ -23,7 +23,7 @@ __author__ = 'Paul Scott-Murphy' __maintainer__ = 'William McBrine ' -__version__ = '0.13-wmcbrine' +__version__ = '0.14-wmcbrine' __license__ = 'LGPL' import zeroconf as r diff --git a/zwebtest.py b/zwebtest.py index 4e167779..f927e071 100644 --- a/zwebtest.py +++ b/zwebtest.py @@ -1,5 +1,5 @@ -""" Multicast DNS Service Discovery for Python, v0.13-wmcbrine - Copyright 2003 Paul Scott-Murphy, 2013 William McBrine +""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine + Copyright 2003 Paul Scott-Murphy, 2014 William McBrine This module provides a unit test suite for the Multicast DNS Service Discovery for Python module. @@ -23,7 +23,7 @@ __author__ = 'Paul Scott-Murphy' __maintainer__ = 'William McBrine ' -__version__ = '0.13-wmcbrine' +__version__ = '0.14-wmcbrine' __license__ = 'LGPL' from zeroconf import * From eee4530d7b8216338634282f3097cb96932aa28e Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 18:38:35 -0400 Subject: [PATCH 042/163] More readable display in browser; automatically quit after giving ten seconds to respond. --- zwebbrowse.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/zwebbrowse.py b/zwebbrowse.py index d82a3686..97ab59ba 100644 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -1,30 +1,41 @@ from zeroconf import * import socket +import time class MyListener(object): def __init__(self): self.r = Zeroconf() def removeService(self, zeroconf, type, name): + print print "Service", name, "removed" def addService(self, zeroconf, type, name): + print print "Service", name, "added" - print "Type is", type + print " Type is", type info = self.r.getServiceInfo(type, name) if info: - print "Address is", str(socket.inet_ntoa(info.getAddress())) - print "Port is", info.getPort() - print "Weight is", info.getWeight() - print "Priority is", info.getPriority() - print "Server is", info.getServer() - print "Text is", info.getText() - print "Properties are", info.getProperties() + print " Address is %s:%d" % (socket.inet_ntoa(info.getAddress()), + info.getPort()) + print " Weight is %d, Priority is %d" % (info.getWeight(), + info.getPriority()) + print " Server is", info.getServer() + text = info.getText() + if text: + print " Text is", repr(text) + prop = info.getProperties() + if prop: + print " Properties are" + for key, value in prop.items(): + print " %s: %s" % (key, value) if __name__ == '__main__': print "Multicast DNS Service Discovery for Python Browser test" r = Zeroconf() - print "1. Testing browsing for a service..." + print "Testing browsing for a service..." type = "_http._tcp.local." listener = MyListener() browser = ServiceBrowser(r, type, listener) + time.sleep(10) + r.close() From 0f3b413b269f8b95b6f8073ba39d11f156ae632c Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 18:54:22 -0400 Subject: [PATCH 043/163] Allow graceful exit from announcement test. --- zwebtest.py | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/zwebtest.py b/zwebtest.py index f927e071..5ee0419f 100644 --- a/zwebtest.py +++ b/zwebtest.py @@ -1,38 +1,17 @@ -""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine - Copyright 2003 Paul Scott-Murphy, 2014 William McBrine - - This module provides a unit test suite for the Multicast DNS - Service Discovery for Python module. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 - USA - -""" - -__author__ = 'Paul Scott-Murphy' -__maintainer__ = 'William McBrine ' -__version__ = '0.14-wmcbrine' -__license__ = 'LGPL' - from zeroconf import * import socket -desc = {'path':'/~paulsm/'} -info = ServiceInfo("_http._tcp.local.", "Paul's Test Web Site._http._tcp.local.", socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.") +desc = {'path': '/~paulsm/'} + +info = ServiceInfo("_http._tcp.local.", + "Paul's Test Web Site._http._tcp.local.", + socket.inet_aton("10.0.1.2"), 80, 0, 0, + desc, "ash-2.local.") r = Zeroconf() print "Registration of a service..." r.registerService(info) -print "Waiting..." +raw_input("Waiting (press Enter to exit)...") +print "Unregistering..." +r.unregisterService(info) +r.close() From a11518726321b15059be255b6329cba591887197 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 19:02:46 -0400 Subject: [PATCH 044/163] getText() is redundant with getProperties(). --- zwebbrowse.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zwebbrowse.py b/zwebbrowse.py index 97ab59ba..dd5a524a 100644 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -21,9 +21,9 @@ def addService(self, zeroconf, type, name): print " Weight is %d, Priority is %d" % (info.getWeight(), info.getPriority()) print " Server is", info.getServer() - text = info.getText() - if text: - print " Text is", repr(text) + #text = info.getText() + #if text: + # print " Text is", repr(text) prop = info.getProperties() if prop: print " Properties are" From 2ac738f84bbcf29d03bad289cb243182ecdf48d6 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 19:07:36 -0400 Subject: [PATCH 045/163] Unneeded. --- zunittest.py | 32 -------------------------------- zwebbrowse.py | 3 --- 2 files changed, 35 deletions(-) diff --git a/zunittest.py b/zunittest.py index 7e679e5c..fd9caee6 100644 --- a/zunittest.py +++ b/zunittest.py @@ -1,35 +1,6 @@ -""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine - Copyright 2003 Paul Scott-Murphy, 2014 William McBrine - - This module provides a unit test suite for the Multicast DNS - Service Discovery for Python module. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 - USA - -""" - -__author__ = 'Paul Scott-Murphy' -__maintainer__ = 'William McBrine ' -__version__ = '0.14-wmcbrine' -__license__ = 'LGPL' - import zeroconf as r import unittest - class PacketGeneration(unittest.TestCase): def testParseOwnPacketSimple(self): @@ -59,7 +30,6 @@ def testMatchQuestion(self): self.assertEqual(len(generated.questions), len(parsed.questions)) self.assertEqual(question, parsed.questions[0]) - class PacketForm(unittest.TestCase): def testTransactionID(self): @@ -108,7 +78,6 @@ def testNumbersQuestions(self): self.assertEqual(numAuthorities, 0) self.assertEqual(numAddtionals, 0) - class Names(unittest.TestCase): def testLongName(self): @@ -140,7 +109,6 @@ def testSameName(self): generated.addQuestion(question) parsed = r.DNSIncoming(generated.packet()) - class Framework(unittest.TestCase): def testLaunchAndClose(self): diff --git a/zwebbrowse.py b/zwebbrowse.py index dd5a524a..5cbda4b5 100644 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -21,9 +21,6 @@ def addService(self, zeroconf, type, name): print " Weight is %d, Priority is %d" % (info.getWeight(), info.getPriority()) print " Server is", info.getServer() - #text = info.getText() - #if text: - # print " Text is", repr(text) prop = info.getProperties() if prop: print " Properties are" From 5e5e78e27240e7e03d1c8aa96ee0e1f7877d0d5d Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 20:12:41 -0400 Subject: [PATCH 046/163] Make examples executable. --- zunittest.py | 2 ++ zwebbrowse.py | 2 ++ zwebtest.py | 2 ++ 3 files changed, 6 insertions(+) mode change 100644 => 100755 zunittest.py mode change 100644 => 100755 zwebbrowse.py mode change 100644 => 100755 zwebtest.py diff --git a/zunittest.py b/zunittest.py old mode 100644 new mode 100755 index fd9caee6..285c3b90 --- a/zunittest.py +++ b/zunittest.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + import zeroconf as r import unittest diff --git a/zwebbrowse.py b/zwebbrowse.py old mode 100644 new mode 100755 index 5cbda4b5..2a3c0787 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + from zeroconf import * import socket import time diff --git a/zwebtest.py b/zwebtest.py old mode 100644 new mode 100755 index 5ee0419f..2c1374d5 --- a/zwebtest.py +++ b/zwebtest.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + from zeroconf import * import socket From 0884d6a56afc6fb559b6c90a923762393187e50a Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 21:01:48 -0400 Subject: [PATCH 047/163] Docstrings for examples; shorter timeout; struct.unpack() vs. ord(). --- zunittest.py | 19 +++++++++---------- zwebbrowse.py | 4 +++- zwebtest.py | 2 ++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/zunittest.py b/zunittest.py index 285c3b90..a830de40 100755 --- a/zunittest.py +++ b/zunittest.py @@ -1,6 +1,9 @@ #!/usr/bin/env python +""" Unit tests for zeroconf.py """ + import zeroconf as r +import struct import unittest class PacketGeneration(unittest.TestCase): @@ -56,14 +59,12 @@ def testResponseHeaderBits(self): def testNumbers(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() - numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) - numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) - numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) - numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) + (numQuestions, numAnswers, numAuthorities, + numAdditionals) = struct.unpack('>4H', bytes[4:12]) self.assertEqual(numQuestions, 0) self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) - self.assertEqual(numAddtionals, 0) + self.assertEqual(numAdditionals, 0) def testNumbersQuestions(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) @@ -71,14 +72,12 @@ def testNumbersQuestions(self): for i in xrange(10): generated.addQuestion(question) bytes = generated.packet() - numQuestions = ord(bytes[4]) << 8 | ord(bytes[5]) - numAnswers = ord(bytes[6]) << 8 | ord(bytes[7]) - numAuthorities = ord(bytes[8]) << 8 | ord(bytes[9]) - numAddtionals = ord(bytes[10]) << 8 | ord(bytes[11]) + (numQuestions, numAnswers, numAuthorities, + numAdditionals) = struct.unpack('>4H', bytes[4:12]) self.assertEqual(numQuestions, 10) self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) - self.assertEqual(numAddtionals, 0) + self.assertEqual(numAdditionals, 0) class Names(unittest.TestCase): diff --git a/zwebbrowse.py b/zwebbrowse.py index 2a3c0787..ab59bf9f 100755 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +""" Example of browsing for a service (in this case, HTTP) """ + from zeroconf import * import socket import time @@ -36,5 +38,5 @@ def addService(self, zeroconf, type, name): type = "_http._tcp.local." listener = MyListener() browser = ServiceBrowser(r, type, listener) - time.sleep(10) + time.sleep(5) r.close() diff --git a/zwebtest.py b/zwebtest.py index 2c1374d5..d74741d6 100755 --- a/zwebtest.py +++ b/zwebtest.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +""" Example of announcing a service (in this case, a fake HTTP server) """ + from zeroconf import * import socket From 5e10a20a9cb6bbc09356cbf957f3f7fa3e169ff2 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 22:34:22 -0400 Subject: [PATCH 048/163] Really these should be network order. --- zunittest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zunittest.py b/zunittest.py index a830de40..b4908d14 100755 --- a/zunittest.py +++ b/zunittest.py @@ -60,7 +60,7 @@ def testNumbers(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() (numQuestions, numAnswers, numAuthorities, - numAdditionals) = struct.unpack('>4H', bytes[4:12]) + numAdditionals) = struct.unpack('!4H', bytes[4:12]) self.assertEqual(numQuestions, 0) self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) @@ -73,7 +73,7 @@ def testNumbersQuestions(self): generated.addQuestion(question) bytes = generated.packet() (numQuestions, numAnswers, numAuthorities, - numAdditionals) = struct.unpack('>4H', bytes[4:12]) + numAdditionals) = struct.unpack('!4H', bytes[4:12]) self.assertEqual(numQuestions, 10) self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) From eee7196626773eae2dc0dc1a68de03a99d778139 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Wed, 18 Jun 2014 23:24:54 -0400 Subject: [PATCH 049/163] Obsolete comment. --- zeroconf.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index ab9f071b..0615973a 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1188,10 +1188,7 @@ def __init__(self, bindaddress=None): # multicast UDP sockets (p 731, "TCP/IP Illustrated, # Volume 2"), but some BSD-derived systems require # SO_REUSEPORT to be specified explicity. Also, not all - # versions of Python have SO_REUSEPORT available. So - # if you're on a BSD-based system, and haven't upgraded - # to Python 2.3 yet, you may find this library doesn't - # work as expected. + # versions of Python have SO_REUSEPORT available. # pass self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 255) From 443aca867d694432d466d20bdf7c49ebc7a4e684 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Thu, 19 Jun 2014 12:00:04 -0400 Subject: [PATCH 050/163] Although SOL_IP is considered more correct here, it's undefined on some systems, where IPPROTO_IP is available. (Both equate to 0.) Reported by Mike Erdely. --- zeroconf.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 0615973a..41f0003e 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1191,8 +1191,8 @@ def __init__(self, bindaddress=None): # versions of Python have SO_REUSEPORT available. # pass - self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 255) - self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1) + self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) + self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) try: self.socket.bind(self.group) except: @@ -1200,9 +1200,9 @@ def __init__(self, bindaddress=None): # the SO_REUSE* options have been set, so ignore it # pass - #self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, + #self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, # socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) - self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, + self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) self.listeners = [] @@ -1517,7 +1517,7 @@ def close(self): self.notifyAll() self.engine.notify() self.unregisterAllServices() - self.socket.setsockopt(socket.SOL_IP, + self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) From 83aa0f3803cdf79470f4a754c7b9ab616544eea1 Mon Sep 17 00:00:00 2001 From: William McBrine Date: Thu, 19 Jun 2014 12:13:57 -0400 Subject: [PATCH 051/163] Updated for 0.14. --- README | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README b/README index c2666f58..839ddfd2 100644 --- a/README +++ b/README @@ -1,9 +1,9 @@ This is Multicast DNS Service Discovery for Python v0.14-wmcbrine Originally by Paul Scott-Murphy, modified by William McBrine. -zeroconf.py is the implementation file, look at the end for examples of +zeroconf.py is the implementation file; look at the end for examples of basic use. You can also view zwebbrowse.py to see how to browse for -services. +services, and zwebtest.py for an example of announcing them. This fork is used in all of my TiVo-related projects: HME for Python (and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. @@ -20,6 +20,10 @@ Long-term goals: Python 3.x History ------- +0.14 - Fix for SOL_IP undefined on some systems - thanks Mike Erdely. + Cleaned up examples. + Lowercased module name. + 0.13 - Various minor changes; see git for details. No longer compatible with Python 2.2. Only tested with 2.5-2.7. Fork by William McBrine. From e2964ed48263e72159e95cb0691af0dcb9ba498b Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 7 Jul 2014 00:27:14 +0100 Subject: [PATCH 052/163] PEP8ize and clean up --- zeroconf.py | 256 ++++++++++++++++++++++++++++---------------------- zunittest.py | 22 +++-- zwebbrowse.py | 5 +- zwebtest.py | 3 +- 4 files changed, 164 insertions(+), 122 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 41f0003e..521f9467 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -16,9 +16,8 @@ You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - """ __author__ = 'Paul Scott-Murphy' @@ -48,27 +47,27 @@ _BROWSER_TIME = 500 # Some DNS constants - + _MDNS_ADDR = '224.0.0.251' _MDNS_PORT = 5353 _DNS_PORT = 53 -_DNS_TTL = 60 * 60 # one hour default TTL +_DNS_TTL = 60 * 60 # one hour default TTL -_MAX_MSG_TYPICAL = 1460 # unused +_MAX_MSG_TYPICAL = 1460 # unused _MAX_MSG_ABSOLUTE = 8972 -_FLAGS_QR_MASK = 0x8000 # query response mask -_FLAGS_QR_QUERY = 0x0000 # query -_FLAGS_QR_RESPONSE = 0x8000 # response +_FLAGS_QR_MASK = 0x8000 # query response mask +_FLAGS_QR_QUERY = 0x0000 # query +_FLAGS_QR_RESPONSE = 0x8000 # response -_FLAGS_AA = 0x0400 # Authorative answer -_FLAGS_TC = 0x0200 # Truncated -_FLAGS_RD = 0x0100 # Recursion desired -_FLAGS_RA = 0x8000 # Recursion available +_FLAGS_AA = 0x0400 # Authorative answer +_FLAGS_TC = 0x0200 # Truncated +_FLAGS_RD = 0x0100 # Recursion desired +_FLAGS_RA = 0x8000 # Recursion available -_FLAGS_Z = 0x0040 # Zero -_FLAGS_AD = 0x0020 # Authentic data -_FLAGS_CD = 0x0010 # Checking disabled +_FLAGS_Z = 0x0040 # Zero +_FLAGS_AD = 0x0020 # Authentic data +_FLAGS_CD = 0x0010 # Checking disabled _CLASS_IN = 1 _CLASS_CS = 2 @@ -97,63 +96,71 @@ _TYPE_TXT = 16 _TYPE_AAAA = 28 _TYPE_SRV = 33 -_TYPE_ANY = 255 +_TYPE_ANY = 255 # Mapping constants to names -_CLASSES = { _CLASS_IN : "in", - _CLASS_CS : "cs", - _CLASS_CH : "ch", - _CLASS_HS : "hs", - _CLASS_NONE : "none", - _CLASS_ANY : "any" } - -_TYPES = { _TYPE_A : "a", - _TYPE_NS : "ns", - _TYPE_MD : "md", - _TYPE_MF : "mf", - _TYPE_CNAME : "cname", - _TYPE_SOA : "soa", - _TYPE_MB : "mb", - _TYPE_MG : "mg", - _TYPE_MR : "mr", - _TYPE_NULL : "null", - _TYPE_WKS : "wks", - _TYPE_PTR : "ptr", - _TYPE_HINFO : "hinfo", - _TYPE_MINFO : "minfo", - _TYPE_MX : "mx", - _TYPE_TXT : "txt", - _TYPE_AAAA : "quada", - _TYPE_SRV : "srv", - _TYPE_ANY : "any" } +_CLASSES = {_CLASS_IN: "in", + _CLASS_CS: "cs", + _CLASS_CH: "ch", + _CLASS_HS: "hs", + _CLASS_NONE: "none", + _CLASS_ANY: "any"} + +_TYPES = {_TYPE_A: "a", + _TYPE_NS: "ns", + _TYPE_MD: "md", + _TYPE_MF: "mf", + _TYPE_CNAME: "cname", + _TYPE_SOA: "soa", + _TYPE_MB: "mb", + _TYPE_MG: "mg", + _TYPE_MR: "mr", + _TYPE_NULL: "null", + _TYPE_WKS: "wks", + _TYPE_PTR: "ptr", + _TYPE_HINFO: "hinfo", + _TYPE_MINFO: "minfo", + _TYPE_MX: "mx", + _TYPE_TXT: "txt", + _TYPE_AAAA: "quada", + _TYPE_SRV: "srv", + _TYPE_ANY: "any"} # utility functions + def currentTimeMillis(): """Current system time in milliseconds""" return time.time() * 1000 # Exceptions + class NonLocalNameException(Exception): pass + class NonUniqueNameException(Exception): pass + class NamePartTooLongException(Exception): pass + class AbstractMethodException(Exception): pass + class BadTypeInNameException(Exception): pass # implementation classes + class DNSEntry(object): + """A DNS entry""" def __init__(self, name, type, clazz): @@ -185,7 +192,7 @@ def getType(self, t): def toString(self, hdr, other): """String representation with additional information""" result = "%s[%s,%s" % (hdr, self.getType(self.type), - self.getClazz(self.clazz)) + self.getClazz(self.clazz)) if self.unique: result += "-unique," else: @@ -197,11 +204,13 @@ def toString(self, hdr, other): result += "]" return result + class DNSQuestion(DNSEntry): + """A DNS question entry""" def __init__(self, name, type, clazz): - #if not name.endswith(".local."): + # if not name.endswith(".local."): # raise NonLocalNameException DNSEntry.__init__(self, name, type, clazz) @@ -217,8 +226,9 @@ def __repr__(self): class DNSRecord(DNSEntry): + """A DNS record - like a DNS entry, but has a TTL""" - + def __init__(self, name, type, clazz, ttl): DNSEntry.__init__(self, name, type, clazz) self.ttl = ttl @@ -271,10 +281,12 @@ def write(self, out): def toString(self, other): """String representation with addtional information""" arg = "%s/%s,%s" % (self.ttl, - self.getRemainingTTL(currentTimeMillis()), other) + self.getRemainingTTL(currentTimeMillis()), other) return DNSEntry.toString(self, "record", arg) + class DNSAddress(DNSRecord): + """A DNS address record""" def __init__(self, name, type, clazz, ttl, address): @@ -296,7 +308,9 @@ def __repr__(self): except: return self.address + class DNSHinfo(DNSRecord): + """A DNS host information record""" def __init__(self, name, type, clazz, ttl, cpu, os): @@ -318,7 +332,9 @@ def __repr__(self): """String representation""" return self.cpu + " " + self.os + class DNSPointer(DNSRecord): + """A DNS pointer record""" def __init__(self, name, type, clazz, ttl, alias): @@ -337,7 +353,9 @@ def __repr__(self): """String representation""" return self.toString(self.alias) + class DNSText(DNSRecord): + """A DNS text record""" def __init__(self, name, type, clazz, ttl, text): @@ -359,7 +377,9 @@ def __repr__(self): else: return self.toString(self.text) + class DNSService(DNSRecord): + """A DNS service record""" def __init__(self, name, type, clazz, ttl, priority, weight, port, server): @@ -388,7 +408,9 @@ def __repr__(self): """String representation""" return self.toString("%s:%s" % (self.server, self.port)) + class DNSIncoming(object): + """Object representation of an incoming DNS packet""" def __init__(self, data): @@ -408,7 +430,7 @@ def __init__(self, data): def unpack(self, format): length = struct.calcsize(format) - info = struct.unpack(format, self.data[self.offset:self.offset+length]) + info = struct.unpack(format, self.data[self.offset:self.offset + length]) self.offset += length return info @@ -438,7 +460,7 @@ def readCharacterString(self): def readString(self, length): """Reads a string of a given length from the packet""" - info = self.data[self.offset:self.offset+length] + info = self.data[self.offset:self.offset + length] self.offset += length return info @@ -463,11 +485,11 @@ def readOthers(self): rec = DNSText(domain, type, clazz, ttl, self.readString(length)) elif type == _TYPE_SRV: rec = DNSService(domain, type, clazz, ttl, - self.readUnsignedShort(), self.readUnsignedShort(), - self.readUnsignedShort(), self.readName()) + self.readUnsignedShort(), self.readUnsignedShort(), + self.readUnsignedShort(), self.readName()) elif type == _TYPE_HINFO: rec = DNSHinfo(domain, type, clazz, ttl, - self.readCharacterString(), self.readCharacterString()) + self.readCharacterString(), self.readCharacterString()) elif type == _TYPE_AAAA: rec = DNSAddress(domain, type, clazz, ttl, self.readString(16)) else: @@ -489,7 +511,7 @@ def isResponse(self): def readUTF(self, offset, length): """Reads a UTF-8 string of a given length from the packet""" - return unicode(self.data[offset:offset+length], 'utf-8', 'replace') + return unicode(self.data[offset:offset + length], 'utf-8', 'replace') def readName(self): """Reads a domain name from the packet""" @@ -526,6 +548,7 @@ def readName(self): class DNSOutgoing(object): + """Object representation of an outgoing packet""" def __init__(self, flags, multicast=True): @@ -652,7 +675,7 @@ def writeRecord(self, record, now): self.size -= 2 length = len(''.join(self.data[index:])) - self.insertShort(index, length) # Here is the short we adjusted for + self.insertShort(index, length) # Here is the short we adjusted for def packet(self): """Returns a string containing the packet's bytes @@ -663,8 +686,8 @@ def packet(self): self.finished = True for question in self.questions: self.writeQuestion(question) - for answer, time in self.answers: - self.writeRecord(answer, time) + for answer, time_ in self.answers: + self.writeRecord(answer, time_) for authority in self.authorities: self.writeRecord(authority, 0) for additional in self.additionals: @@ -683,6 +706,7 @@ def packet(self): class DNSCache(object): + """A cache of DNS entries""" def __init__(self): @@ -728,7 +752,8 @@ def entriesWithName(self, name): def entries(self): """Returns a list of all entries""" - def add(x, y): return x + y + def add(x, y): + return x + y try: return reduce(add, self.cache.values()) except: @@ -736,6 +761,7 @@ def add(x, y): return x + y class Engine(threading.Thread): + """An engine wraps read access to sockets, allowing objects that need to receive data from sockets to be called back when the sockets are ready. @@ -750,7 +776,7 @@ class Engine(threading.Thread): def __init__(self, zc): threading.Thread.__init__(self) self.zc = zc - self.readers = {} # maps socket to reader + self.readers = {} # maps socket to reader self.timeout = 5 self.condition = threading.Condition() self.start() @@ -768,9 +794,9 @@ def run(self): else: try: rr, wr, er = select.select(rs, [], [], self.timeout) - for socket in rr: + for socket_ in rr: try: - self.readers[socket].handle_read() + self.readers[socket_].handle_read() except: traceback.print_exc() except: @@ -782,7 +808,7 @@ def getReaders(self): result = self.readers.keys() self.condition.release() return result - + def addReader(self, reader, socket): self.condition.acquire() self.readers[socket] = reader @@ -800,7 +826,9 @@ def notify(self): self.condition.notify() self.condition.release() + class Listener(object): + """A Listener is used by this module to listen on the multicast group to which DNS messages are sent, allowing the implementation to cache information as it arrives. @@ -841,6 +869,7 @@ def handle_read(self): class Reaper(threading.Thread): + """A Reaper is used by this module to remove cache entries that have expired.""" @@ -862,6 +891,7 @@ def run(self): class ServiceBrowser(threading.Thread): + """Used to browse for a service of a specific type. The listener object will have its addService() and @@ -878,7 +908,7 @@ def __init__(self, zc, type, listener): self.nextTime = currentTimeMillis() self.delay = _BROWSER_TIME self.list = [] - + self.done = False self.zc.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) @@ -897,14 +927,14 @@ def updateRecord(self, zc, now, record): else: del(self.services[record.alias.lower()]) callback = lambda x: self.listener.removeService(x, - self.type, record.alias) + self.type, record.alias) self.list.append(callback) return except: if not expired: self.services[record.alias.lower()] = record callback = lambda x: self.listener.addService(x, - self.type, record.alias) + self.type, record.alias) self.list.append(callback) expires = record.getExpirationTime(75) @@ -943,6 +973,7 @@ def run(self): class ServiceInfo(object): + """Service information""" def __init__(self, type, name, address=None, port=None, weight=0, @@ -1010,7 +1041,7 @@ def setText(self, text): while index < end: length = ord(text[index]) index += 1 - strs.append(text[index:index+length]) + strs.append(text[index:index + length]) index += length for s in strs: @@ -1026,7 +1057,7 @@ def setText(self, text): value = False # Only update non-existent properties - if key and result.get(key) == None: + if key and result.get(key) is None: result[key] = value self.properties = result @@ -1076,7 +1107,7 @@ def updateRecord(self, zc, now, record): """Updates service information from a DNS record""" if record is not None and not record.isExpired(now): if record.type == _TYPE_A: - #if record.name == self.name: + # if record.name == self.name: if record.name == self.server: self.address = record.address elif record.type == _TYPE_SRV: @@ -1085,9 +1116,9 @@ def updateRecord(self, zc, now, record): self.port = record.port self.weight = record.weight self.priority = record.priority - #self.address = None + # self.address = None self.updateRecord(zc, now, - zc.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) + zc.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) elif record.type == _TYPE_TXT: if record.name == self.name: self.setText(record.text) @@ -1110,18 +1141,18 @@ def request(self, zc, timeout): if next <= now: out = DNSOutgoing(_FLAGS_QR_QUERY) out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, - _CLASS_IN)) + _CLASS_IN)) out.addAnswerAtTime(zc.cache.getByDetails(self.name, - _TYPE_SRV, _CLASS_IN), now) + _TYPE_SRV, _CLASS_IN), now) out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, - _CLASS_IN)) + _CLASS_IN)) out.addAnswerAtTime(zc.cache.getByDetails(self.name, - _TYPE_TXT, _CLASS_IN), now) + _TYPE_TXT, _CLASS_IN), now) if self.server is not None: out.addQuestion(DNSQuestion(self.server, - _TYPE_A, _CLASS_IN)) + _TYPE_A, _CLASS_IN)) out.addAnswerAtTime(zc.cache.getByDetails(self.server, - _TYPE_A, _CLASS_IN), now) + _TYPE_A, _CLASS_IN), now) zc.send(out) next = now + delay delay = delay * 2 @@ -1147,7 +1178,7 @@ def __ne__(self, other): def __repr__(self): """String representation""" result = "service[%s,%s:%s," % (self.name, - socket.inet_ntoa(self.getAddress()), self.port) + socket.inet_ntoa(self.getAddress()), self.port) if self.text is None: result += "None" else: @@ -1160,10 +1191,12 @@ def __repr__(self): class Zeroconf(object): + """Implementation of Zeroconf Multicast DNS Service Discovery Supports registration, unregistration, queries and browsing. """ + def __init__(self, bindaddress=None): """Creates an instance of the Zeroconf class, establishing multicast communications, listening and reaping threads.""" @@ -1200,10 +1233,10 @@ def __init__(self, bindaddress=None): # the SO_REUSE* options have been set, so ignore it # pass - #self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, + # self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, # socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, - socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) self.listeners = [] self.browsers = [] @@ -1281,15 +1314,15 @@ def registerService(self, info, ttl=_DNS_TTL): continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, - _CLASS_IN, ttl, info.name), 0) + _CLASS_IN, ttl, info.name), 0) out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, ttl, info.priority, info.weight, info.port, - info.server), 0) + _CLASS_IN, ttl, info.priority, info.weight, info.port, + info.server), 0) out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, - ttl, info.text), 0) + ttl, info.text), 0) if info.address: out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, - _CLASS_IN, ttl, info.address), 0) + _CLASS_IN, ttl, info.address), 0) self.send(out) i += 1 nextTime += _REGISTER_TIME @@ -1314,15 +1347,15 @@ def unregisterService(self, info): continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, - _CLASS_IN, 0, info.name), 0) + _CLASS_IN, 0, info.name), 0) out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, 0, info.priority, info.weight, info.port, - info.name), 0) + _CLASS_IN, 0, info.priority, info.weight, info.port, + info.name), 0) out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, - 0, info.text), 0) + 0, info.text), 0) if info.address: out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, - _CLASS_IN, 0, info.address), 0) + _CLASS_IN, 0, info.address), 0) self.send(out) i += 1 nextTime += _UNREGISTER_TIME @@ -1341,15 +1374,15 @@ def unregisterAllServices(self): out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) for info in self.services.values(): out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, - _CLASS_IN, 0, info.name), 0) + _CLASS_IN, 0, info.name), 0) out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, 0, info.priority, info.weight, - info.port, info.server), 0) + _CLASS_IN, 0, info.priority, info.weight, + info.port, info.server), 0) out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, - _CLASS_IN, 0, info.text), 0) + _CLASS_IN, 0, info.text), 0) if info.address: out.addAnswerAtTime(DNSAddress(info.server, - _TYPE_A, _CLASS_IN, 0, info.address), 0) + _TYPE_A, _CLASS_IN, 0, info.address), 0) self.send(out) i += 1 nextTime += _UNREGISTER_TIME @@ -1363,11 +1396,11 @@ def checkService(self, info): while i < 3: for record in self.cache.entriesWithName(info.type): if (record.type == _TYPE_PTR and - not record.isExpired(now) and - record.alias == info.name): + not record.isExpired(now) and + record.alias == info.name): if info.name.find('.') < 0: info.name = '%s.[%s:%s].%s' % (info.name, - info.address, info.port, info.type) + info.address, info.port, info.type) self.checkService(info) return @@ -1450,15 +1483,15 @@ def handleQuery(self, msg, addr, port): if out is None: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) out.addAnswer(msg, - DNSPointer("_services._dns-sd._udp.local.", - _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) + DNSPointer("_services._dns-sd._udp.local.", + _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) for service in self.services.values(): if question.name == service.type: if out is None: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) out.addAnswer(msg, - DNSPointer(service.type, _TYPE_PTR, - _CLASS_IN, _DNS_TTL, service.name)) + DNSPointer(service.type, _TYPE_PTR, + _CLASS_IN, _DNS_TTL, service.name)) else: try: if out is None: @@ -1469,25 +1502,26 @@ def handleQuery(self, msg, addr, port): for service in self.services.values(): if service.server == question.name.lower(): out.addAnswer(msg, DNSAddress(question.name, - _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.address)) + _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.address)) service = self.services.get(question.name.lower(), None) - if not service: continue + if not service: + continue if question.type in (_TYPE_SRV, _TYPE_ANY): out.addAnswer(msg, DNSService(question.name, - _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.priority, service.weight, - service.port, service.server)) + _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.priority, service.weight, + service.port, service.server)) if question.type in (_TYPE_TXT, _TYPE_ANY): out.addAnswer(msg, DNSText(question.name, - _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.text)) + _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.text)) if question.type == _TYPE_SRV: out.addAdditionalAnswer(DNSAddress(service.server, - _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.address)) + _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.address)) except: traceback.print_exc() @@ -1495,7 +1529,7 @@ def handleQuery(self, msg, addr, port): out.id = msg.id self.send(out, addr, port) - def send(self, out, addr = _MDNS_ADDR, port = _MDNS_PORT): + def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): """Sends an outgoing packet.""" packet = out.packet() try: @@ -1530,7 +1564,7 @@ def close(self): print "Multicast DNS Service Discovery for Python, version", __version__ r = Zeroconf() print "1. Testing registration of a service..." - desc = {'version':'0.10','a':'test value', 'b':'another value'} + desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'} info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) diff --git a/zunittest.py b/zunittest.py index b4908d14..cefd4bb4 100755 --- a/zunittest.py +++ b/zunittest.py @@ -6,25 +6,26 @@ import struct import unittest + class PacketGeneration(unittest.TestCase): def testParseOwnPacketSimple(self): generated = r.DNSOutgoing(0) - parsed = r.DNSIncoming(generated.packet()) + r.DNSIncoming(generated.packet()) def testParseOwnPacketSimpleUnicast(self): generated = r.DNSOutgoing(0, 0) - parsed = r.DNSIncoming(generated.packet()) + r.DNSIncoming(generated.packet()) def testParseOwnPacketFlags(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - parsed = r.DNSIncoming(generated.packet()) + r.DNSIncoming(generated.packet()) def testParseOwnPacketQuestion(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN)) - parsed = r.DNSIncoming(generated.packet()) + r.DNSIncoming(generated.packet()) def testMatchQuestion(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) @@ -35,6 +36,7 @@ def testMatchQuestion(self): self.assertEqual(len(generated.questions), len(parsed.questions)) self.assertEqual(question, parsed.questions[0]) + class PacketForm(unittest.TestCase): def testTransactionID(self): @@ -60,7 +62,7 @@ def testNumbers(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() (numQuestions, numAnswers, numAuthorities, - numAdditionals) = struct.unpack('!4H', bytes[4:12]) + numAdditionals) = struct.unpack('!4H', bytes[4:12]) self.assertEqual(numQuestions, 0) self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) @@ -73,12 +75,13 @@ def testNumbersQuestions(self): generated.addQuestion(question) bytes = generated.packet() (numQuestions, numAnswers, numAuthorities, - numAdditionals) = struct.unpack('!4H', bytes[4:12]) + numAdditionals) = struct.unpack('!4H', bytes[4:12]) self.assertEqual(numQuestions, 10) self.assertEqual(numAnswers, 0) self.assertEqual(numAuthorities, 0) self.assertEqual(numAdditionals, 0) + class Names(unittest.TestCase): def testLongName(self): @@ -86,14 +89,14 @@ def testLongName(self): question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) + r.DNSIncoming(generated.packet()) def testExceedinglyLongName(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) name = "%slocal." % ("part." * 1000) question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) + r.DNSIncoming(generated.packet()) def testExceedinglyLongNamePart(self): name = "%s.local." % ("a" * 1000) @@ -108,7 +111,8 @@ def testSameName(self): question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) generated.addQuestion(question) generated.addQuestion(question) - parsed = r.DNSIncoming(generated.packet()) + r.DNSIncoming(generated.packet()) + class Framework(unittest.TestCase): diff --git a/zwebbrowse.py b/zwebbrowse.py index ab59bf9f..4fb39175 100755 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -2,11 +2,14 @@ """ Example of browsing for a service (in this case, HTTP) """ -from zeroconf import * import socket import time +from zeroconf import ServiceBrowser, Zeroconf + + class MyListener(object): + def __init__(self): self.r = Zeroconf() diff --git a/zwebtest.py b/zwebtest.py index d74741d6..d7d6b39b 100755 --- a/zwebtest.py +++ b/zwebtest.py @@ -2,9 +2,10 @@ """ Example of announcing a service (in this case, a fake HTTP server) """ -from zeroconf import * import socket +from zeroconf import ServiceInfo, Zeroconf + desc = {'path': '/~paulsm/'} info = ServiceInfo("_http._tcp.local.", From a2a6125dd03d9a810dac72163d545e413387217b Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 7 Jul 2014 00:41:59 +0100 Subject: [PATCH 053/163] Set up Travis build --- .travis.yml | 18 ++++++++++++++++++ Makefile | 20 ++++++++++++++++++++ requirements-dev.txt | 4 ++++ zunittest.py => test_zeroconf.py | 3 --- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 .travis.yml create mode 100644 Makefile create mode 100644 requirements-dev.txt rename zunittest.py => test_zeroconf.py (98%) mode change 100755 => 100644 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..98ace4cf --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +language: python +python: + - "2.6" + - "2.7" + - "3.3" + - "3.4" + - "pypy" +matrix: + allow_failures: + - python: "2.6" + - python: "3.3" + - python: "3.4" + - python: "pypy" + fast_finish: true +install: + - pip install -r requirements-dev.txt +script: + - make flake8 diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..57985a46 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +.PHONY: all virtualenv +MAX_LINE_LENGTH=110 + +virtualenv: ./env/requirements.built + +env: + virtualenv env + +./env/requirements.built: env requirements-dev.txt + ./env/bin/pip install -r requirements-dev.txt + cp requirements-dev.txt ./env/requirements.built + +flake8: + flake8 --max-line-length=$(MAX_LINE_LENGTH) *.py + +test: + nosetests -v + +autopep8: + autopep8 --max-line-length=$(MAX_LINE_LENGTH) -i *.py diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 00000000..64fffe1f --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,4 @@ +autopep8 +flake8 +flake8-blind-except +nose diff --git a/zunittest.py b/test_zeroconf.py old mode 100755 new mode 100644 similarity index 98% rename from zunittest.py rename to test_zeroconf.py index cefd4bb4..354c57ac --- a/zunittest.py +++ b/test_zeroconf.py @@ -119,6 +119,3 @@ class Framework(unittest.TestCase): def testLaunchAndClose(self): rv = r.Zeroconf() rv.close() - -if __name__ == '__main__': - unittest.main() From 41a013c8a051b3f80018f37d4f254263cc890a68 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 7 Jul 2014 00:52:24 +0100 Subject: [PATCH 054/163] Stop catching BaseExceptions --- zeroconf.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 521f9467..4731b480 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -305,7 +305,7 @@ def __repr__(self): """String representation""" try: return socket.inet_ntoa(self.address) - except: + except Exception: # TODO stop catching all Exceptions return self.address @@ -716,7 +716,7 @@ def add(self, entry): """Adds an entry""" try: list = self.cache[entry.key] - except: + except Exception: # TODO stop catching all Exceptions list = self.cache[entry.key] = [] list.append(entry) @@ -725,7 +725,7 @@ def remove(self, entry): try: list = self.cache[entry.key] list.remove(entry) - except: + except Exception: # TODO stop catching all Exceptions pass def get(self, entry): @@ -734,7 +734,7 @@ def get(self, entry): try: list = self.cache[entry.key] return list[list.index(entry)] - except: + except Exception: # TODO stop catching all Exceptions return None def getByDetails(self, name, type, clazz): @@ -747,7 +747,7 @@ def entriesWithName(self, name): """Returns a list of entries whose key matches the name.""" try: return self.cache[name] - except: + except Exception: # TODO stop catching all Exceptions return [] def entries(self): @@ -756,7 +756,7 @@ def add(x, y): return x + y try: return reduce(add, self.cache.values()) - except: + except Exception: # TODO stop catching all Exceptions return [] @@ -797,9 +797,9 @@ def run(self): for socket_ in rr: try: self.readers[socket_].handle_read() - except: + except Exception: # TODO stop catching all Exceptions traceback.print_exc() - except: + except Exception: # TODO stop catching all Exceptions pass def getReaders(self): @@ -930,7 +930,7 @@ def updateRecord(self, zc, now, record): self.type, record.alias) self.list.append(callback) return - except: + except Exception: # TODO stop catching all Exceptions if not expired: self.services[record.alias.lower()] = record callback = lambda x: self.listener.addService(x, @@ -1051,7 +1051,7 @@ def setText(self, text): value = True elif value == 'false' or not value: value = False - except: + except Exception: # TODO stop catching all Exceptions # No equals sign at all key = s value = False @@ -1061,7 +1061,7 @@ def setText(self, text): result[key] = value self.properties = result - except: + except Exception: # TODO stop catching all Exceptions traceback.print_exc() self.properties = None @@ -1207,7 +1207,7 @@ def __init__(self, bindaddress=None): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('4.2.2.1', 123)) self.intf = s.getsockname()[0] - except: + except Exception: # TODO stop catching all Exceptions self.intf = socket.gethostbyname(socket.gethostname()) else: self.intf = bindaddress @@ -1216,7 +1216,7 @@ def __init__(self, bindaddress=None): try: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) - except: + except Exception: # TODO stop catching all Exceptions # SO_REUSEADDR should be equivalent to SO_REUSEPORT for # multicast UDP sockets (p 731, "TCP/IP Illustrated, # Volume 2"), but some BSD-derived systems require @@ -1228,7 +1228,7 @@ def __init__(self, bindaddress=None): self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) try: self.socket.bind(self.group) - except: + except Exception: # TODO stop catching all Exceptions # Some versions of linux raise an exception even though # the SO_REUSE* options have been set, so ignore it # @@ -1335,7 +1335,7 @@ def unregisterService(self, info): self.servicetypes[info.type] -= 1 else: del self.servicetypes[info.type] - except: + except Exception: # TODO stop catching all Exceptions pass now = currentTimeMillis() nextTime = now @@ -1435,7 +1435,7 @@ def removeListener(self, listener): try: self.listeners.remove(listener) self.notifyAll() - except: + except Exception: # TODO stop catching all Exceptions pass def updateRecord(self, now, rec): @@ -1522,7 +1522,7 @@ def handleQuery(self, msg, addr, port): out.addAdditionalAnswer(DNSAddress(service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) - except: + except Exception: # TODO stop catching all Exceptions traceback.print_exc() if out is not None and out.answers: @@ -1538,7 +1538,7 @@ def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): if bytes_sent < 0: break packet = packet[bytes_sent:] - except: + except Exception: # TODO stop catching all Exceptions # Ignore this, it may be a temporary loss of network connection pass From 353b700df79b49c49db62e0a6e6eb0eae3ccb444 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 7 Jul 2014 00:59:58 +0100 Subject: [PATCH 055/163] Move readme to README.rst --- README.rst | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 README.rst diff --git a/README.rst b/README.rst new file mode 100644 index 00000000..839ddfd2 --- /dev/null +++ b/README.rst @@ -0,0 +1,82 @@ +This is Multicast DNS Service Discovery for Python v0.14-wmcbrine +Originally by Paul Scott-Murphy, modified by William McBrine. + +zeroconf.py is the implementation file; look at the end for examples of +basic use. You can also view zwebbrowse.py to see how to browse for +services, and zwebtest.py for an example of announcing them. + +This fork is used in all of my TiVo-related projects: HME for Python +(and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. +Before this, I was tracking the changes for zeroconf.py in three +separate repos. I figured I should have an authoritative source. + +Although I make changes based on my experience with TiVos, I expect that +they're generally applicable. This version also includes patches found +on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere +around the net -- not always well-documented, sorry. + +Long-term goals: Python 3.x + + +History +------- +0.14 - Fix for SOL_IP undefined on some systems - thanks Mike Erdely. + Cleaned up examples. + Lowercased module name. + +0.13 - Various minor changes; see git for details. + No longer compatible with Python 2.2. Only tested with 2.5-2.7. + Fork by William McBrine. + +0.12 - allow selection of binding interface + typo fix - Thanks A. M. Kuchlingi + removed all use of word 'Rendezvous' - this is an API change + +0.11 - correction to comments for addListener method + support for new record types seen from OS X + - IPv6 address + - hostinfo + ignore unknown DNS record types + fixes to name decoding + works alongside other processes using port 5353 + (e.g. on Mac OS X) + tested against Mac OS X 10.3.2's mDNSResponder + corrections to removal of list entries for service browser + +0.10 - Jonathon Paisley contributed these corrections: + always multicast replies, even when query is unicast + correct a pointer encoding problem + can now write records in any order + traceback shown on failure + better TXT record parsing + server is now separate from name + can cancel a service browser + + modified some unit tests to accommodate these changes + +0.09 - remove all records on service unregistration + fix DOS security problem with readName + +0.08 - changed licensing to LGPL + +0.07 - faster shutdown on engine + pointer encoding of outgoing names + ServiceBrowser now works + new unit tests + +0.06 - small improvements with unit tests + added defined exception types + new style objects + fixed hostname/interface problem + fixed socket timeout problem + fixed addServiceListener() typo bug + using select() for socket reads + tested on Debian unstable with Python 2.2.2 + +0.05 - ensure case insensitivty on domain names + support for unicast DNS queries + +0.04 - added some unit tests + added __ne__ adjuncts where required + ensure names end in '.local.' + timeout on receiving socket for clean shutdown From fd3401efb55ae91324d12ba80affd2f3b3ebcf5e Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 7 Jul 2014 01:06:43 +0100 Subject: [PATCH 056/163] Move readme to README.rst --- README | 82 ------------------------------------------------------ README.rst | 22 +++++++++------ 2 files changed, 13 insertions(+), 91 deletions(-) delete mode 100644 README diff --git a/README b/README deleted file mode 100644 index 839ddfd2..00000000 --- a/README +++ /dev/null @@ -1,82 +0,0 @@ -This is Multicast DNS Service Discovery for Python v0.14-wmcbrine -Originally by Paul Scott-Murphy, modified by William McBrine. - -zeroconf.py is the implementation file; look at the end for examples of -basic use. You can also view zwebbrowse.py to see how to browse for -services, and zwebtest.py for an example of announcing them. - -This fork is used in all of my TiVo-related projects: HME for Python -(and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. -Before this, I was tracking the changes for zeroconf.py in three -separate repos. I figured I should have an authoritative source. - -Although I make changes based on my experience with TiVos, I expect that -they're generally applicable. This version also includes patches found -on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere -around the net -- not always well-documented, sorry. - -Long-term goals: Python 3.x - - -History -------- -0.14 - Fix for SOL_IP undefined on some systems - thanks Mike Erdely. - Cleaned up examples. - Lowercased module name. - -0.13 - Various minor changes; see git for details. - No longer compatible with Python 2.2. Only tested with 2.5-2.7. - Fork by William McBrine. - -0.12 - allow selection of binding interface - typo fix - Thanks A. M. Kuchlingi - removed all use of word 'Rendezvous' - this is an API change - -0.11 - correction to comments for addListener method - support for new record types seen from OS X - - IPv6 address - - hostinfo - ignore unknown DNS record types - fixes to name decoding - works alongside other processes using port 5353 - (e.g. on Mac OS X) - tested against Mac OS X 10.3.2's mDNSResponder - corrections to removal of list entries for service browser - -0.10 - Jonathon Paisley contributed these corrections: - always multicast replies, even when query is unicast - correct a pointer encoding problem - can now write records in any order - traceback shown on failure - better TXT record parsing - server is now separate from name - can cancel a service browser - - modified some unit tests to accommodate these changes - -0.09 - remove all records on service unregistration - fix DOS security problem with readName - -0.08 - changed licensing to LGPL - -0.07 - faster shutdown on engine - pointer encoding of outgoing names - ServiceBrowser now works - new unit tests - -0.06 - small improvements with unit tests - added defined exception types - new style objects - fixed hostname/interface problem - fixed socket timeout problem - fixed addServiceListener() typo bug - using select() for socket reads - tested on Debian unstable with Python 2.2.2 - -0.05 - ensure case insensitivty on domain names - support for unicast DNS queries - -0.04 - added some unit tests - added __ne__ adjuncts where required - ensure names end in '.local.' - timeout on receiving socket for clean shutdown diff --git a/README.rst b/README.rst index 839ddfd2..07b5a355 100644 --- a/README.rst +++ b/README.rst @@ -1,25 +1,28 @@ +pyzeroconf +========== + This is Multicast DNS Service Discovery for Python v0.14-wmcbrine Originally by Paul Scott-Murphy, modified by William McBrine. -zeroconf.py is the implementation file; look at the end for examples of -basic use. You can also view zwebbrowse.py to see how to browse for +zeroconf.py is the implementation file; look at the end for examples of +basic use. You can also view zwebbrowse.py to see how to browse for services, and zwebtest.py for an example of announcing them. -This fork is used in all of my TiVo-related projects: HME for Python -(and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. -Before this, I was tracking the changes for zeroconf.py in three +This fork is used in all of my TiVo-related projects: HME for Python +(and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. +Before this, I was tracking the changes for zeroconf.py in three separate repos. I figured I should have an authoritative source. -Although I make changes based on my experience with TiVos, I expect that -they're generally applicable. This version also includes patches found -on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere +Although I make changes based on my experience with TiVos, I expect that +they're generally applicable. This version also includes patches found +on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere around the net -- not always well-documented, sorry. Long-term goals: Python 3.x History -------- +======= 0.14 - Fix for SOL_IP undefined on some systems - thanks Mike Erdely. Cleaned up examples. Lowercased module name. @@ -36,6 +39,7 @@ History support for new record types seen from OS X - IPv6 address - hostinfo + ignore unknown DNS record types fixes to name decoding works alongside other processes using port 5353 From 43b182cce40bcb21eb1e052a0bc42bf367a963ca Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 7 Jul 2014 01:07:02 +0100 Subject: [PATCH 057/163] Advertise Python 2.6 and PyPy support --- .travis.yml | 2 -- README.rst | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 98ace4cf..8e123fa5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,8 @@ python: - "pypy" matrix: allow_failures: - - python: "2.6" - python: "3.3" - python: "3.4" - - python: "pypy" fast_finish: true install: - pip install -r requirements-dev.txt diff --git a/README.rst b/README.rst index 07b5a355..9fc0d8b3 100644 --- a/README.rst +++ b/README.rst @@ -20,6 +20,10 @@ around the net -- not always well-documented, sorry. Long-term goals: Python 3.x +Compatible with: +* CPython 2.6, 2.7 +* PyPy 2.2+ (possibly 1.9-2.1 as well) + History ======= From f8cea82177cea3577d2b4f70fec32e85229abdce Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 7 Jul 2014 01:08:30 +0100 Subject: [PATCH 058/163] Run actual tests on Travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 8e123fa5..d0dac302 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,4 +13,5 @@ matrix: install: - pip install -r requirements-dev.txt script: + - make test - make flake8 From 35eced310fbe1782fd87eb33e7f4befcb0a78499 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 01:07:26 +0100 Subject: [PATCH 059/163] Update README.rst --- README.rst | 174 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 104 insertions(+), 70 deletions(-) diff --git a/README.rst b/README.rst index 9fc0d8b3..45537cf4 100644 --- a/README.rst +++ b/README.rst @@ -1,12 +1,8 @@ -pyzeroconf -========== +python-zeroconf +=============== -This is Multicast DNS Service Discovery for Python v0.14-wmcbrine -Originally by Paul Scott-Murphy, modified by William McBrine. - -zeroconf.py is the implementation file; look at the end for examples of -basic use. You can also view zwebbrowse.py to see how to browse for -services, and zwebtest.py for an example of announcing them. +This is fork of pyzeroconf, Multicast DNS Service Discovery for Python, +originally by Paul Scott-Murphy, modified by William McBrine. This fork is used in all of my TiVo-related projects: HME for Python (and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. @@ -21,70 +17,108 @@ around the net -- not always well-documented, sorry. Long-term goals: Python 3.x Compatible with: + * CPython 2.6, 2.7 * PyPy 2.2+ (possibly 1.9-2.1 as well) -History +Changelog +========= + +0.14 +---- + +* Fix for SOL_IP undefined on some systems - thanks Mike Erdely. +* Cleaned up examples. +* Lowercased module name. + +0.13 +---- + +* Various minor changes; see git for details. +* No longer compatible with Python 2.2. Only tested with 2.5-2.7. +* Fork by William McBrine. + +0.12 +---- + +* allow selection of binding interface +* typo fix - Thanks A. M. Kuchlingi +* removed all use of word 'Rendezvous' - this is an API change + +0.11 +---- + +* correction to comments for addListener method +* support for new record types seen from OS X + - IPv6 address + - hostinfo + +* ignore unknown DNS record types +* fixes to name decoding +* works alongside other processes using port 5353 (e.g. on Mac OS X) +* tested against Mac OS X 10.3.2's mDNSResponder +* corrections to removal of list entries for service browser + +0.10 +---- + +* Jonathon Paisley contributed these corrections: + - always multicast replies, even when query is unicast + - correct a pointer encoding problem + - can now write records in any order + - traceback shown on failure + - better TXT record parsing + - server is now separate from name + - can cancel a service browser +* modified some unit tests to accommodate these changes + +0.09 +---- + +* remove all records on service unregistration +* fix DOS security problem with readName + +0.08 +---- + +* changed licensing to LGPL + +0.07 +---- + +* faster shutdown on engine +* pointer encoding of outgoing names +* ServiceBrowser now works +* new unit tests + +0.06 +---- +* small improvements with unit tests +* added defined exception types +* new style objects +* fixed hostname/interface problem +* fixed socket timeout problem +* fixed addServiceListener() typo bug +* using select() for socket reads +* tested on Debian unstable with Python 2.2.2 + +0.05 +---- + +* ensure case insensitivty on domain names +* support for unicast DNS queries + +0.04 +---- + +* added some unit tests +* added __ne__ adjuncts where required +* ensure names end in '.local.' +* timeout on receiving socket for clean shutdown + + +License ======= -0.14 - Fix for SOL_IP undefined on some systems - thanks Mike Erdely. - Cleaned up examples. - Lowercased module name. - -0.13 - Various minor changes; see git for details. - No longer compatible with Python 2.2. Only tested with 2.5-2.7. - Fork by William McBrine. - -0.12 - allow selection of binding interface - typo fix - Thanks A. M. Kuchlingi - removed all use of word 'Rendezvous' - this is an API change - -0.11 - correction to comments for addListener method - support for new record types seen from OS X - - IPv6 address - - hostinfo - - ignore unknown DNS record types - fixes to name decoding - works alongside other processes using port 5353 - (e.g. on Mac OS X) - tested against Mac OS X 10.3.2's mDNSResponder - corrections to removal of list entries for service browser - -0.10 - Jonathon Paisley contributed these corrections: - always multicast replies, even when query is unicast - correct a pointer encoding problem - can now write records in any order - traceback shown on failure - better TXT record parsing - server is now separate from name - can cancel a service browser - - modified some unit tests to accommodate these changes - -0.09 - remove all records on service unregistration - fix DOS security problem with readName - -0.08 - changed licensing to LGPL - -0.07 - faster shutdown on engine - pointer encoding of outgoing names - ServiceBrowser now works - new unit tests - -0.06 - small improvements with unit tests - added defined exception types - new style objects - fixed hostname/interface problem - fixed socket timeout problem - fixed addServiceListener() typo bug - using select() for socket reads - tested on Debian unstable with Python 2.2.2 - -0.05 - ensure case insensitivty on domain names - support for unicast DNS queries - -0.04 - added some unit tests - added __ne__ adjuncts where required - ensure names end in '.local.' - timeout on receiving socket for clean shutdown + +LGPL, see COPYING file for details. From 916bd38ddb48a959c597ae1763193b4c2c74334f Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 01:13:11 +0100 Subject: [PATCH 060/163] Add setup.py --- setup.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ zeroconf.py | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100755 setup.py diff --git a/setup.py b/setup.py new file mode 100755 index 00000000..98d595a0 --- /dev/null +++ b/setup.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +from __future__ import absolute_import, division, print_function + +from os.path import abspath, dirname, join + +from setuptools import setup + +from zeroconf import __version__ + +PROJECT_ROOT = abspath(dirname(__file__)) +with open(join(PROJECT_ROOT, 'README.rst')) as f: + readme = f.read() + +setup( + name='zeroconf', + version=__version__, + description='Pure Python Multicast DNS Service Discovery Library', + long_description=readme, + author='Paul Scott-Murphy, William McBrine', + url='https://github.com/jstasiak/python-zeroconf', + py_modules=['zeroconf'], + platforms=['unix', 'linux', 'osx'], + license='LGPL', + zip_safe=False, + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Intended Audience :: Developers', + 'Intended Audience :: System Administrators', + 'License :: OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)', + 'Operating System :: POSIX', + 'Operating System :: POSIX :: Linux', + 'Operating System :: MacOS :: MacOS X', + 'Topic :: Software Development :: Libraries', + 'Programming Language :: Python', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: Implementation :: CPython', + 'Programming Language :: Python :: Implementation :: PyPy', + ], + keywords=[ + 'Bonjour', 'Avahi', 'Zeroconf', 'Multicast DNS', 'Service Discovery', + 'mDNS', + ], +) diff --git a/zeroconf.py b/zeroconf.py index 4731b480..d18cefdc 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -22,7 +22,7 @@ __author__ = 'Paul Scott-Murphy' __maintainer__ = 'William McBrine ' -__version__ = '0.14-wmcbrine' +__version__ = '0.14' __license__ = 'LGPL' import time From 0ef1b0d3481b68a752efe822ff4e9ce8356bcffa Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 01:21:58 +0100 Subject: [PATCH 061/163] Gitignore build/ --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..dc84959d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ + From b8191741d4ef8e347f6dd138fa48da5aec9b6549 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 01:26:02 +0100 Subject: [PATCH 062/163] Tune package description --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 98d595a0..b6db69d3 100755 --- a/setup.py +++ b/setup.py @@ -14,7 +14,8 @@ setup( name='zeroconf', version=__version__, - description='Pure Python Multicast DNS Service Discovery Library', + description='Pure Python Multicast DNS Service Discovery Library ' + '(Bonjour/Avahi compatible)', long_description=readme, author='Paul Scott-Murphy, William McBrine', url='https://github.com/jstasiak/python-zeroconf', From 6feec3459d2561f00402d627ea91a8a4981ad309 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 01:30:07 +0100 Subject: [PATCH 063/163] Update README.rst --- README.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.rst b/README.rst index 45537cf4..cce99551 100644 --- a/README.rst +++ b/README.rst @@ -21,6 +21,16 @@ Compatible with: * CPython 2.6, 2.7 * PyPy 2.2+ (possibly 1.9-2.1 as well) +How to get python-zeroconf? +=========================== + +* PyPI page https://github.com/jstasiak/python-zeroconf +* GitHub project https://github.com/jstasiak/python-zeroconf + +The easiest way to install python-zeroconf is using pip: + +``pip install zeroconf`` + Changelog ========= From 09a1f4f9d76f64cc8c85f0525e05bdac53de210c Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 01:32:03 +0100 Subject: [PATCH 064/163] Update README.rst --- README.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.rst b/README.rst index cce99551..8ba1658f 100644 --- a/README.rst +++ b/README.rst @@ -1,6 +1,9 @@ python-zeroconf =============== +.. image:: https://travis-ci.org/jstasiak/python-zeroconf.svg?branch=master + :target: https://travis-ci.org/jstasiak/python-zeroconf + This is fork of pyzeroconf, Multicast DNS Service Discovery for Python, originally by Paul Scott-Murphy, modified by William McBrine. From 9a99aa727f4e041a726aed3736c0a8ab625c4cb6 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 7 Jul 2014 01:37:16 +0100 Subject: [PATCH 065/163] Add Python 3 support --- test_zeroconf.py | 8 ++-- zeroconf.py | 111 ++++++++++++++++++++++++++++++----------------- zwebbrowse.py | 28 ++++++------ zwebtest.py | 9 +++- 4 files changed, 96 insertions(+), 60 deletions(-) diff --git a/test_zeroconf.py b/test_zeroconf.py index 354c57ac..4b6b035e 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -6,6 +6,8 @@ import struct import unittest +from zeroconf import byte_ord, xrange + class PacketGeneration(unittest.TestCase): @@ -43,19 +45,19 @@ def testTransactionID(self): """ID must be zero in a DNS-SD packet""" generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() - id = ord(bytes[0]) << 8 | ord(bytes[1]) + id = byte_ord(bytes[0]) << 8 | byte_ord(bytes[1]) self.assertEqual(id, 0) def testQueryHeaderBits(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() - flags = ord(bytes[2]) << 8 | ord(bytes[3]) + flags = byte_ord(bytes[2]) << 8 | byte_ord(bytes[3]) self.assertEqual(flags, 0x0) def testResponseHeaderBits(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() - flags = ord(bytes[2]) << 8 | ord(bytes[3]) + flags = byte_ord(bytes[2]) << 8 | byte_ord(bytes[3]) self.assertEqual(flags, 0x8000) def testNumbers(self): diff --git a/zeroconf.py b/zeroconf.py index d18cefdc..ead7bff6 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import, division, print_function, unicode_literals + """ Multicast DNS Service Discovery for Python, v0.14-wmcbrine Copyright 2003 Paul Scott-Murphy, 2014 William McBrine @@ -31,9 +33,30 @@ import threading import select import traceback +from functools import reduce __all__ = ["Zeroconf", "ServiceInfo", "ServiceBrowser"] +try: + xrange = xrange +except NameError: + xrange = range + +try: + unicode +except NameError: + unicode = str + +if isinstance(chr(8), unicode): + byte_chr = lambda num: bytes([num]) +else: + byte_chr = chr + +if isinstance(bytes([8])[0], int): + byte_ord = lambda x: x +else: + byte_ord = ord + # hook for threads _GLOBAL_DONE = False @@ -359,6 +382,7 @@ class DNSText(DNSRecord): """A DNS text record""" def __init__(self, name, type, clazz, ttl, text): + assert isinstance(text, bytes) DNSRecord.__init__(self, name, type, clazz, ttl) self.text = text @@ -437,24 +461,24 @@ def unpack(self, format): def readHeader(self): """Reads header portion of packet""" (self.id, self.flags, self.numQuestions, self.numAnswers, - self.numAuthorities, self.numAdditionals) = self.unpack('!6H') + self.numAuthorities, self.numAdditionals) = self.unpack(b'!6H') def readQuestions(self): """Reads questions section of packet""" for i in xrange(self.numQuestions): name = self.readName() - type, clazz = self.unpack('!HH') + type, clazz = self.unpack(b'!HH') question = DNSQuestion(name, type, clazz) self.questions.append(question) def readInt(self): """Reads an integer from the packet""" - return self.unpack('!I')[0] + return self.unpack(b'!I')[0] def readCharacterString(self): """Reads a character string from the packet""" - length = ord(self.data[self.offset]) + length = byte_ord(self.data[self.offset]) self.offset += 1 return self.readString(length) @@ -466,7 +490,7 @@ def readString(self, length): def readUnsignedShort(self): """Reads an unsigned short from the packet""" - return self.unpack('!H')[0] + return self.unpack(b'!H')[0] def readOthers(self): """Reads the answers, authorities and additionals section of the @@ -474,7 +498,7 @@ def readOthers(self): n = self.numAnswers + self.numAuthorities + self.numAdditionals for i in xrange(n): domain = self.readName() - type, clazz, ttl, length = self.unpack('!HHiH') + type, clazz, ttl, length = self.unpack(b'!HHiH') rec = None if type == _TYPE_A: @@ -521,7 +545,7 @@ def readName(self): first = off while True: - length = ord(self.data[off]) + length = byte_ord(self.data[off]) off += 1 if length == 0: break @@ -532,12 +556,14 @@ def readName(self): elif t == 0xC0: if next < 0: next = off + 1 - off = ((length & 0x3F) << 8) | ord(self.data[off]) + off = ((length & 0x3F) << 8) | byte_ord(self.data[off]) if off >= first: - raise "Bad domain name (circular) at " + str(off) + # TODO raise more specific exception + raise Exception("Bad domain name (circular) at %s" % (off,)) first = off else: - raise "Bad domain name at " + str(off) + # TODO raise more specific exception + raise Exception("Bad domain name at %s" % (off,)) if next >= 0: self.offset = next @@ -594,23 +620,24 @@ def pack(self, format, value): def writeByte(self, value): """Writes a single byte to the packet""" - self.pack('!c', chr(value)) + self.pack(b'!c', byte_chr(value)) def insertShort(self, index, value): """Inserts an unsigned short in a certain position in the packet""" - self.data.insert(index, struct.pack('!H', value)) + self.data.insert(index, struct.pack(b'!H', value)) self.size += 2 def writeShort(self, value): """Writes an unsigned short to the packet""" - self.pack('!H', value) + self.pack(b'!H', value) def writeInt(self, value): """Writes an unsigned integer to the packet""" - self.pack('!I', int(value)) + self.pack(b'!I', int(value)) def writeString(self, value): """Writes a string to the packet""" + assert isinstance(value, bytes) self.data.append(value) self.size += len(value) @@ -674,7 +701,7 @@ def writeRecord(self, record, now): record.write(self) self.size -= 2 - length = len(''.join(self.data[index:])) + length = len(b''.join(self.data[index:])) self.insertShort(index, length) # Here is the short we adjusted for def packet(self): @@ -702,7 +729,7 @@ def packet(self): self.insertShort(0, 0) else: self.insertShort(0, self.id) - return ''.join(self.data) + return b''.join(self.data) class DNSCache(object): @@ -843,7 +870,7 @@ def __init__(self, zc): def handle_read(self): try: data, (addr, port) = self.zc.socket.recvfrom(_MAX_MSG_ABSOLUTE) - except socket.error, e: + except socket.error as e: # If the socket was closed by another thread -- which happens # regularly on shutdown -- an EBADF exception is thrown here. # Ignore it. @@ -1009,23 +1036,26 @@ def setProperties(self, properties): if isinstance(properties, dict): self.properties = properties list = [] - result = '' + result = b'' for key in properties: value = properties[key] + if isinstance(key, unicode): + key = key.encode('utf-8') + if value is None: - suffix = ''.encode('utf-8') - elif isinstance(value, str): + suffix = b'' + elif isinstance(value, unicode): suffix = value.encode('utf-8') elif isinstance(value, int): if value: - suffix = 'true' + suffix = b'true' else: - suffix = 'false' + suffix = b'false' else: - suffix = ''.encode('utf-8') - list.append('='.join((key, suffix))) + suffix = b'' + list.append(b'='.join((key, suffix))) for item in list: - result = ''.join((result, chr(len(item)), item)) + result = b''.join((result, byte_chr(len(item)), item)) self.text = result else: self.text = properties @@ -1039,7 +1069,7 @@ def setText(self, text): index = 0 strs = [] while index < end: - length = ord(text[index]) + length = byte_ord(text[index]) index += 1 strs.append(text[index:index + length]) index += length @@ -1561,26 +1591,25 @@ def close(self): # query (for Zoe), and service unregistration. if __name__ == '__main__': - print "Multicast DNS Service Discovery for Python, version", __version__ + print("Multicast DNS Service Discovery for Python, version %s" % __version__) r = Zeroconf() - print "1. Testing registration of a service..." + print("1. Testing registration of a service...") desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'} info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) - print " Registering service..." + print(" Registering service...") r.registerService(info) - print " Registration done." - print "2. Testing query of service information..." - print " Getting ZOE service:", - print str(r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local.")) - print " Query done." - print "3. Testing query of own service..." - print " Getting self:", - print str(r.getServiceInfo("_http._tcp.local.", - "My Service Name._http._tcp.local.")) - print " Query done." - print "4. Testing unregister of service information..." + print(" Registration done.") + print("2. Testing query of service information...") + print(" Getting ZOE service: %s" % ( + r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local."))) + print(" Query done.") + print("3. Testing query of own service...") + print(" Getting self: %s" % ( + r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.")),) + print(" Query done.") + print("4. Testing unregister of service information...") r.unregisterService(info) - print " Unregister done." + print(" Unregister done.") r.close() diff --git a/zwebbrowse.py b/zwebbrowse.py index 4fb39175..bef632d5 100755 --- a/zwebbrowse.py +++ b/zwebbrowse.py @@ -14,30 +14,30 @@ def __init__(self): self.r = Zeroconf() def removeService(self, zeroconf, type, name): - print - print "Service", name, "removed" + print() + print("Service %s removed" % (name,)) def addService(self, zeroconf, type, name): - print - print "Service", name, "added" - print " Type is", type + print() + print("Service %s added" % (name,)) + print(" Type is %s" % (type,)) info = self.r.getServiceInfo(type, name) if info: - print " Address is %s:%d" % (socket.inet_ntoa(info.getAddress()), - info.getPort()) - print " Weight is %d, Priority is %d" % (info.getWeight(), - info.getPriority()) - print " Server is", info.getServer() + print(" Address is %s:%d" % (socket.inet_ntoa(info.getAddress()), + info.getPort())) + print(" Weight is %d, Priority is %d" % (info.getWeight(), + info.getPriority())) + print(" Server is", info.getServer()) prop = info.getProperties() if prop: - print " Properties are" + print(" Properties are") for key, value in prop.items(): - print " %s: %s" % (key, value) + print(" %s: %s" % (key, value)) if __name__ == '__main__': - print "Multicast DNS Service Discovery for Python Browser test" + print("Multicast DNS Service Discovery for Python Browser test") r = Zeroconf() - print "Testing browsing for a service..." + print("Testing browsing for a service...") type = "_http._tcp.local." listener = MyListener() browser = ServiceBrowser(r, type, listener) diff --git a/zwebtest.py b/zwebtest.py index d7d6b39b..f916dfc5 100755 --- a/zwebtest.py +++ b/zwebtest.py @@ -6,6 +6,11 @@ from zeroconf import ServiceInfo, Zeroconf +try: + raw_input +except NameError: + raw_input = input + desc = {'path': '/~paulsm/'} info = ServiceInfo("_http._tcp.local.", @@ -14,9 +19,9 @@ desc, "ash-2.local.") r = Zeroconf() -print "Registration of a service..." +print("Registration of a service...") r.registerService(info) raw_input("Waiting (press Enter to exit)...") -print "Unregistering..." +print("Unregistering...") r.unregisterService(info) r.close() From cd7ca98010044eb965bc988c23a8be59e09eb69a Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 02:00:33 +0100 Subject: [PATCH 066/163] Update README.rst --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 8ba1658f..2e33b640 100644 --- a/README.rst +++ b/README.rst @@ -27,7 +27,7 @@ Compatible with: How to get python-zeroconf? =========================== -* PyPI page https://github.com/jstasiak/python-zeroconf +* PyPI page https://pypi.python.org/pypi/zeroconf * GitHub project https://github.com/jstasiak/python-zeroconf The easiest way to install python-zeroconf is using pip: From b8cfc7996941afded5c9c7e7903378279590b20f Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 01:49:31 +0100 Subject: [PATCH 067/163] Mark threads as daemonic (at least for now) --- zeroconf.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/zeroconf.py b/zeroconf.py index ead7bff6..1be410fc 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -802,6 +802,7 @@ class Engine(threading.Thread): def __init__(self, zc): threading.Thread.__init__(self) + self.daemon = True self.zc = zc self.readers = {} # maps socket to reader self.timeout = 5 @@ -902,6 +903,7 @@ class Reaper(threading.Thread): def __init__(self, zc): threading.Thread.__init__(self) + self.daemon = True self.zc = zc self.start() @@ -928,6 +930,7 @@ class ServiceBrowser(threading.Thread): def __init__(self, zc, type, listener): """Creates a browser for a specific type""" threading.Thread.__init__(self) + self.daemon = True self.zc = zc self.type = type self.listener = listener From 0a0f7e0e72d7f9ed08231d94b66ff44bcff60151 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 02:02:57 +0100 Subject: [PATCH 068/163] Fix regression introduced with Python 3 compat --- test_zeroconf.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- zeroconf.py | 6 +++--- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/test_zeroconf.py b/test_zeroconf.py index 4b6b035e..131a1b30 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -2,11 +2,13 @@ """ Unit tests for zeroconf.py """ -import zeroconf as r +import socket import struct import unittest +from time import sleep -from zeroconf import byte_ord, xrange +import zeroconf as r +from zeroconf import byte_ord, ServiceBrowser, ServiceInfo, xrange, Zeroconf class PacketGeneration(unittest.TestCase): @@ -121,3 +123,45 @@ class Framework(unittest.TestCase): def testLaunchAndClose(self): rv = r.Zeroconf() rv.close() + + +def test_integration(): + + services = set() + + class Listener(object): + + def removeService(self, zeroconf, type_, name): + print('remove') + services.remove((type_, name)) + + def addService(self, zeroconf, type_, name): + print('add') + services.add((type_, name)) + + type_ = "_http._tcp.local." + + zeroconf_browser = Zeroconf() + listener = Listener() + browser = ServiceBrowser(zeroconf_browser, type_, listener) + + zeroconf_registrar = Zeroconf() + desc = {'path': '/~paulsm/'} + name = "xxxyyy.%s" % type_ + info = ServiceInfo( + type_, name, + socket.inet_aton("10.0.1.2"), 80, 0, 0, + desc, "ash-2.local.") + zeroconf_registrar.registerService(info) + + # TODO replace those blind sleeps with events + sleep(2) + try: + assert (type_, name) in services, services + zeroconf_registrar.unregisterService(info) + sleep(2) + assert (type_, name) not in services, services + finally: + zeroconf_registrar.close() + browser.cancel() + zeroconf_browser.close() diff --git a/zeroconf.py b/zeroconf.py index 1be410fc..0d528fc7 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -381,9 +381,9 @@ class DNSText(DNSRecord): """A DNS text record""" - def __init__(self, name, type, clazz, ttl, text): - assert isinstance(text, bytes) - DNSRecord.__init__(self, name, type, clazz, ttl) + def __init__(self, name, type_, clazz, ttl, text): + assert isinstance(text, (bytes, type(None))) + DNSRecord.__init__(self, name, type_, clazz, ttl) self.text = text def write(self, out): From c83891c9dd2f20e8dee44f1b412a536d20cbcbe3 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 02:05:03 +0100 Subject: [PATCH 069/163] Move examples to examples directory --- Makefile | 4 ++-- zwebbrowse.py => examples/zwebbrowse.py | 0 zwebtest.py => examples/zwebtest.py | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename zwebbrowse.py => examples/zwebbrowse.py (100%) rename zwebtest.py => examples/zwebtest.py (100%) diff --git a/Makefile b/Makefile index 57985a46..d29fb433 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,10 @@ env: cp requirements-dev.txt ./env/requirements.built flake8: - flake8 --max-line-length=$(MAX_LINE_LENGTH) *.py + flake8 --max-line-length=$(MAX_LINE_LENGTH) examples *.py test: nosetests -v autopep8: - autopep8 --max-line-length=$(MAX_LINE_LENGTH) -i *.py + autopep8 --max-line-length=$(MAX_LINE_LENGTH) -i examples *.py diff --git a/zwebbrowse.py b/examples/zwebbrowse.py similarity index 100% rename from zwebbrowse.py rename to examples/zwebbrowse.py diff --git a/zwebtest.py b/examples/zwebtest.py similarity index 100% rename from zwebtest.py rename to examples/zwebtest.py From 6aae20e1c1bef8413573139d62d3d2b889fe8776 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 02:08:22 +0100 Subject: [PATCH 070/163] Update README.rst --- README.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.rst b/README.rst index 2e33b640..02ea0a64 100644 --- a/README.rst +++ b/README.rst @@ -34,6 +34,8 @@ The easiest way to install python-zeroconf is using pip: ``pip install zeroconf`` +See examples directory for examples of use. + Changelog ========= From d330918970d719d6b26a3f81e83dbb8b8adac0a4 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 02:09:49 +0100 Subject: [PATCH 071/163] Advertise Python 3 support --- .travis.yml | 3 --- README.rst | 4 +--- setup.py | 3 +++ 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index d0dac302..8abdfbe8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,6 @@ python: - "3.4" - "pypy" matrix: - allow_failures: - - python: "3.3" - - python: "3.4" fast_finish: true install: - pip install -r requirements-dev.txt diff --git a/README.rst b/README.rst index 02ea0a64..26337752 100644 --- a/README.rst +++ b/README.rst @@ -17,11 +17,9 @@ they're generally applicable. This version also includes patches found on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere around the net -- not always well-documented, sorry. -Long-term goals: Python 3.x - Compatible with: -* CPython 2.6, 2.7 +* CPython 2.6, 2.7, 3.3+ * PyPy 2.2+ (possibly 1.9-2.1 as well) How to get python-zeroconf? diff --git a/setup.py b/setup.py index b6db69d3..68e5d8b2 100755 --- a/setup.py +++ b/setup.py @@ -36,6 +36,9 @@ 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', ], From 6a7cd3197ee6ae5690b29b6543fc86d1b1a420d8 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 02:11:38 +0100 Subject: [PATCH 072/163] Update README.rst --- README.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.rst b/README.rst index 26337752..97f75ab1 100644 --- a/README.rst +++ b/README.rst @@ -38,6 +38,11 @@ See examples directory for examples of use. Changelog ========= +0.15 (not yet released) +----------------------- + +* Made Python 3 compatible + 0.14 ---- From 2ce95f52e7a02c7f1113ba7ebee3c89babb9a26e Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 08:15:15 +0100 Subject: [PATCH 073/163] Refactor examples --- examples/zwebbrowse.py | 14 +++++++------- examples/zwebtest.py | 21 +++++++++------------ zeroconf.py | 5 +++++ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/examples/zwebbrowse.py b/examples/zwebbrowse.py index bef632d5..5d827841 100755 --- a/examples/zwebbrowse.py +++ b/examples/zwebbrowse.py @@ -3,9 +3,8 @@ """ Example of browsing for a service (in this case, HTTP) """ import socket -import time -from zeroconf import ServiceBrowser, Zeroconf +from zeroconf import raw_input, ServiceBrowser, Zeroconf class MyListener(object): @@ -36,10 +35,11 @@ def addService(self, zeroconf, type, name): if __name__ == '__main__': print("Multicast DNS Service Discovery for Python Browser test") - r = Zeroconf() + zeroconf = Zeroconf() print("Testing browsing for a service...") - type = "_http._tcp.local." listener = MyListener() - browser = ServiceBrowser(r, type, listener) - time.sleep(5) - r.close() + browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) + try: + raw_input("Waiting (press Enter to exit)...") + finally: + zeroconf.close() diff --git a/examples/zwebtest.py b/examples/zwebtest.py index f916dfc5..293047f4 100755 --- a/examples/zwebtest.py +++ b/examples/zwebtest.py @@ -4,12 +4,7 @@ import socket -from zeroconf import ServiceInfo, Zeroconf - -try: - raw_input -except NameError: - raw_input = input +from zeroconf import raw_input, ServiceInfo, Zeroconf desc = {'path': '/~paulsm/'} @@ -18,10 +13,12 @@ socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.") -r = Zeroconf() +zeroconf = Zeroconf() print("Registration of a service...") -r.registerService(info) -raw_input("Waiting (press Enter to exit)...") -print("Unregistering...") -r.unregisterService(info) -r.close() +zeroconf.registerService(info) +try: + raw_input("Waiting (press Enter to exit)...") +finally: + print("Unregistering...") + zeroconf.unregisterService(info) + zeroconf.close() diff --git a/zeroconf.py b/zeroconf.py index 0d528fc7..58195fdb 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -57,6 +57,11 @@ else: byte_ord = ord +try: + raw_input = raw_input +except NameError: + raw_input = input + # hook for threads _GLOBAL_DONE = False From 3502198768062b49564121b48a792ce5e7b7b288 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 08:20:44 +0100 Subject: [PATCH 074/163] Rename examples --- examples/{zwebbrowse.py => browser.py} | 0 examples/{zwebtest.py => registration.py} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename examples/{zwebbrowse.py => browser.py} (100%) rename examples/{zwebtest.py => registration.py} (100%) diff --git a/examples/zwebbrowse.py b/examples/browser.py similarity index 100% rename from examples/zwebbrowse.py rename to examples/browser.py diff --git a/examples/zwebtest.py b/examples/registration.py similarity index 100% rename from examples/zwebtest.py rename to examples/registration.py From c7bfe63f9a7eff9a1ede0ac63a329a316d3192ab Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 08:26:06 +0100 Subject: [PATCH 075/163] Update README.rst --- README.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.rst b/README.rst index 97f75ab1..93c814cd 100644 --- a/README.rst +++ b/README.rst @@ -19,6 +19,12 @@ around the net -- not always well-documented, sorry. Compatible with: +* Bonjour +* Avahi + +Python compatibility +-------------------- + * CPython 2.6, 2.7, 3.3+ * PyPy 2.2+ (possibly 1.9-2.1 as well) From fba4215be1804a13e454e609ed6df2cf98e149f2 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 08:31:13 +0100 Subject: [PATCH 076/163] Update README.rst --- README.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.rst b/README.rst index 93c814cd..b8d78220 100644 --- a/README.rst +++ b/README.rst @@ -47,7 +47,13 @@ Changelog 0.15 (not yet released) ----------------------- +* Forked by Jakub Stasiak * Made Python 3 compatible +* Added setup script, made installable by pip and uploaded to PyPI +* Set up Travis build +* Reformatted the code and moved files around +* Stopped catching BaseException in several places, that could hide errors +* Marked threads as daemonic, they won't keep application alive now 0.14 ---- From 1ee00b318eab386b709351ffae81c8293f4e6d4d Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 08:34:02 +0100 Subject: [PATCH 077/163] Reuse one Zeroconf instance in browser example --- examples/browser.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/browser.py b/examples/browser.py index 5d827841..04813d47 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -9,9 +9,6 @@ class MyListener(object): - def __init__(self): - self.r = Zeroconf() - def removeService(self, zeroconf, type, name): print() print("Service %s removed" % (name,)) @@ -20,7 +17,7 @@ def addService(self, zeroconf, type, name): print() print("Service %s added" % (name,)) print(" Type is %s" % (type,)) - info = self.r.getServiceInfo(type, name) + info = zeroconf.getServiceInfo(type, name) if info: print(" Address is %s:%d" % (socket.inet_ntoa(info.getAddress()), info.getPort())) From b9f886bf2815c86c7004e123146293c48ea68f1e Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 09:09:44 +0100 Subject: [PATCH 078/163] Add myself to authors --- setup.py | 2 +- zeroconf.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 68e5d8b2..a22657b4 100755 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ description='Pure Python Multicast DNS Service Discovery Library ' '(Bonjour/Avahi compatible)', long_description=readme, - author='Paul Scott-Murphy, William McBrine', + author='Paul Scott-Murphy, William McBrine, Jakub Stasiak', url='https://github.com/jstasiak/python-zeroconf', py_modules=['zeroconf'], platforms=['unix', 'linux', 'osx'], diff --git a/zeroconf.py b/zeroconf.py index 58195fdb..6b1c0b99 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -22,8 +22,8 @@ USA """ -__author__ = 'Paul Scott-Murphy' -__maintainer__ = 'William McBrine ' +__author__ = 'Paul Scott-Murphy, William McBrine' +__maintainer__ = 'Jakub Stasiak ' __version__ = '0.14' __license__ = 'LGPL' From 77bcaddbd1964fb0b494e98ec3ae6d66ea42c509 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 8 Jul 2014 09:10:04 +0100 Subject: [PATCH 079/163] Bump version to 0.15 --- README.rst | 4 ++-- zeroconf.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index b8d78220..d2f490a3 100644 --- a/README.rst +++ b/README.rst @@ -44,8 +44,8 @@ See examples directory for examples of use. Changelog ========= -0.15 (not yet released) ------------------------ +0.15 +---- * Forked by Jakub Stasiak * Made Python 3 compatible diff --git a/zeroconf.py b/zeroconf.py index 6b1c0b99..120974f4 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -24,7 +24,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.14' +__version__ = '0.15' __license__ = 'LGPL' import time From 49af26350390484bc6f4b66dab4f6b004040cd4a Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 9 Jul 2014 00:37:17 +0100 Subject: [PATCH 080/163] Update README.rst --- README.rst | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index d2f490a3..241f4e08 100644 --- a/README.rst +++ b/README.rst @@ -5,7 +5,8 @@ python-zeroconf :target: https://travis-ci.org/jstasiak/python-zeroconf This is fork of pyzeroconf, Multicast DNS Service Discovery for Python, -originally by Paul Scott-Murphy, modified by William McBrine. +originally by Paul Scott-Murphy (https://github.com/paulsm/pyzeroconf), +modified by William McBrine (https://github.com/wmcbrine/pyzeroconf). This fork is used in all of my TiVo-related projects: HME for Python (and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. @@ -22,12 +23,23 @@ Compatible with: * Bonjour * Avahi +Compared to some other Zeroconf/Bonjour/Avahi Python packages, python-zeroconf: + +* has zero external dependencies +* isn't tied to Bonjour or Avahi +* doesn't use D-Bus +* doesn't force you to use particular event loop or Twisted +* is pip-installable +* has PyPI distribution + Python compatibility -------------------- * CPython 2.6, 2.7, 3.3+ * PyPy 2.2+ (possibly 1.9-2.1 as well) + + How to get python-zeroconf? =========================== From 8328aed1444781b6fac854eb722ae0fef14a3cc4 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 9 Jul 2014 00:44:41 +0100 Subject: [PATCH 081/163] Refactor browser example --- examples/browser.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/browser.py b/examples/browser.py index 04813d47..219c9edf 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import absolute_import, division, print_function, unicode_literals """ Example of browsing for a service (in this case, HTTP) """ @@ -10,11 +11,10 @@ class MyListener(object): def removeService(self, zeroconf, type, name): - print() print("Service %s removed" % (name,)) + print('\n') def addService(self, zeroconf, type, name): - print() print("Service %s added" % (name,)) print(" Type is %s" % (type,)) info = zeroconf.getServiceInfo(type, name) @@ -29,14 +29,16 @@ def addService(self, zeroconf, type, name): print(" Properties are") for key, value in prop.items(): print(" %s: %s" % (key, value)) + else: + print(" No info") + print('\n') if __name__ == '__main__': - print("Multicast DNS Service Discovery for Python Browser test") zeroconf = Zeroconf() - print("Testing browsing for a service...") + print("Browsing services...") listener = MyListener() browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) try: - raw_input("Waiting (press Enter to exit)...") + raw_input("Waiting (press Enter to exit)...\n\n") finally: zeroconf.close() From 83fd618328aff29892c71f9ba5b9ff983fe4a202 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 9 Jul 2014 00:49:43 +0100 Subject: [PATCH 082/163] Update README.rst --- README.rst | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 241f4e08..91f18bd9 100644 --- a/README.rst +++ b/README.rst @@ -50,8 +50,36 @@ The easiest way to install python-zeroconf is using pip: ``pip install zeroconf`` -See examples directory for examples of use. +How do I use it? +================ + +Here's an example: + +```python +from zeroconf import raw_input, ServiceBrowser, Zeroconf + + +class MyListener(object): + + def removeService(self, zeroconf, type, name): + print("Service %s removed" % (name,)) + + def addService(self, zeroconf, type, name): + info = zeroconf.getServiceInfo(type, name) + print("Service %s added, service info: %s" % (name, info)) + + +zeroconf = Zeroconf() +listener = MyListener() +browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) +try: + raw_input("Press enter to exit...\n\n") +finally: + zeroconf.close() +``` + +See examples directory for more. Changelog ========= From 7b23734356f85ccaa6ca66ffaeea8484a2d45d3d Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 9 Jul 2014 00:51:07 +0100 Subject: [PATCH 083/163] Fix readme formatting --- README.rst | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/README.rst b/README.rst index 91f18bd9..f3c64fec 100644 --- a/README.rst +++ b/README.rst @@ -56,28 +56,28 @@ How do I use it? Here's an example: -```python -from zeroconf import raw_input, ServiceBrowser, Zeroconf - - -class MyListener(object): - - def removeService(self, zeroconf, type, name): - print("Service %s removed" % (name,)) - - def addService(self, zeroconf, type, name): - info = zeroconf.getServiceInfo(type, name) - print("Service %s added, service info: %s" % (name, info)) - - -zeroconf = Zeroconf() -listener = MyListener() -browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) -try: - raw_input("Press enter to exit...\n\n") -finally: - zeroconf.close() -``` +.. code-block:: python + + from zeroconf import raw_input, ServiceBrowser, Zeroconf + + + class MyListener(object): + + def removeService(self, zeroconf, type, name): + print("Service %s removed" % (name,)) + + def addService(self, zeroconf, type, name): + info = zeroconf.getServiceInfo(type, name) + print("Service %s added, service info: %s" % (name, info)) + + + zeroconf = Zeroconf() + listener = MyListener() + browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) + try: + raw_input("Press enter to exit...\n\n") + finally: + zeroconf.close() See examples directory for more. From 922eab05596b72d141d459e83146a4cdb6c84389 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 9 Jul 2014 01:01:29 +0100 Subject: [PATCH 084/163] Refactor integration test to use events --- test_zeroconf.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/test_zeroconf.py b/test_zeroconf.py index 131a1b30..5d1d0a6e 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -5,7 +5,7 @@ import socket import struct import unittest -from time import sleep +from threading import Event import zeroconf as r from zeroconf import byte_ord, ServiceBrowser, ServiceInfo, xrange, Zeroconf @@ -126,20 +126,21 @@ def testLaunchAndClose(self): def test_integration(): + service_added = Event() + service_removed = Event() - services = set() + type_ = "_http._tcp.local." + registration_name = "xxxyyy.%s" % type_ class Listener(object): def removeService(self, zeroconf, type_, name): - print('remove') - services.remove((type_, name)) + if name == registration_name: + service_removed.set() def addService(self, zeroconf, type_, name): - print('add') - services.add((type_, name)) - - type_ = "_http._tcp.local." + if name == registration_name: + service_added.set() zeroconf_browser = Zeroconf() listener = Listener() @@ -147,20 +148,18 @@ def addService(self, zeroconf, type_, name): zeroconf_registrar = Zeroconf() desc = {'path': '/~paulsm/'} - name = "xxxyyy.%s" % type_ info = ServiceInfo( - type_, name, + type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.") zeroconf_registrar.registerService(info) - # TODO replace those blind sleeps with events - sleep(2) try: - assert (type_, name) in services, services + service_added.wait(1) + assert service_added.is_set() zeroconf_registrar.unregisterService(info) - sleep(2) - assert (type_, name) not in services, services + service_removed.wait(1) + assert service_removed.is_set() finally: zeroconf_registrar.close() browser.cancel() From 4c852d424d07925ae01c24a51ffc36ecae49b48d Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 9 Jul 2014 08:29:51 +0100 Subject: [PATCH 085/163] Add PyPI version badge to the readme --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index f3c64fec..dc5a8098 100644 --- a/README.rst +++ b/README.rst @@ -3,7 +3,11 @@ python-zeroconf .. image:: https://travis-ci.org/jstasiak/python-zeroconf.svg?branch=master :target: https://travis-ci.org/jstasiak/python-zeroconf + +.. image:: http://img.shields.io/pypi/v/zeroconf.svg + :target: https://pypi.python.org/pypi/zeroconf + This is fork of pyzeroconf, Multicast DNS Service Discovery for Python, originally by Paul Scott-Murphy (https://github.com/paulsm/pyzeroconf), modified by William McBrine (https://github.com/wmcbrine/pyzeroconf). From 89531e641f15b24a60f9fb2e9f71a7aa8450363a Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:10:30 +0100 Subject: [PATCH 086/163] Add test_coverage make target --- Makefile | 3 +++ requirements-dev.txt | 1 + 2 files changed, 4 insertions(+) diff --git a/Makefile b/Makefile index d29fb433..5c3ebbbc 100644 --- a/Makefile +++ b/Makefile @@ -16,5 +16,8 @@ flake8: test: nosetests -v +test_coverage: + nosetests -v --with-coverage --cover-package=zeroconf + autopep8: autopep8 --max-line-length=$(MAX_LINE_LENGTH) -i examples *.py diff --git a/requirements-dev.txt b/requirements-dev.txt index 64fffe1f..e514300c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,5 @@ autopep8 +coverage flake8 flake8-blind-except nose From 475e80b90e96364a183c63f09fa3858f34aa3646 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:10:49 +0100 Subject: [PATCH 087/163] Fix socket.error handling This closes #4 --- requirements-dev.txt | 1 + test_zeroconf.py | 26 +++++++++++++++++++++++--- zeroconf.py | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index e514300c..df982feb 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -2,4 +2,5 @@ autopep8 coverage flake8 flake8-blind-except +mock nose diff --git a/test_zeroconf.py b/test_zeroconf.py index 5d1d0a6e..675bef69 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -7,8 +7,17 @@ import unittest from threading import Event +from mock import Mock + import zeroconf as r -from zeroconf import byte_ord, ServiceBrowser, ServiceInfo, xrange, Zeroconf +from zeroconf import ( + byte_ord, + Listener, + ServiceBrowser, + ServiceInfo, + xrange, + Zeroconf, +) class PacketGeneration(unittest.TestCase): @@ -132,7 +141,7 @@ def test_integration(): type_ = "_http._tcp.local." registration_name = "xxxyyy.%s" % type_ - class Listener(object): + class MyListener(object): def removeService(self, zeroconf, type_, name): if name == registration_name: @@ -143,7 +152,7 @@ def addService(self, zeroconf, type_, name): service_added.set() zeroconf_browser = Zeroconf() - listener = Listener() + listener = MyListener() browser = ServiceBrowser(zeroconf_browser, type_, listener) zeroconf_registrar = Zeroconf() @@ -164,3 +173,14 @@ def addService(self, zeroconf, type_, name): zeroconf_registrar.close() browser.cancel() zeroconf_browser.close() + + +def test_listener_handles_closed_socket_situation_gracefully(): + error = socket.error(socket.EBADF) + error.errno = socket.EBADF + + zeroconf = Mock() + zeroconf.socket.recvfrom.side_effect = error + + listener = Listener(zeroconf) + listener.handle_read() diff --git a/zeroconf.py b/zeroconf.py index 120974f4..2bb58940 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -880,7 +880,7 @@ def handle_read(self): # If the socket was closed by another thread -- which happens # regularly on shutdown -- an EBADF exception is thrown here. # Ignore it. - if e[0] == socket.EBADF: + if e.errno == socket.EBADF: return else: raise e From 1d90a9f91f87753a1ea649ce5da1bc6a7da4013d Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:16:10 +0100 Subject: [PATCH 088/163] Send coverage to coveralls --- .travis.yml | 4 +++- requirements-dev.txt | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8abdfbe8..65bf4320 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,5 +10,7 @@ matrix: install: - pip install -r requirements-dev.txt script: - - make test + - make test_coverage - make flake8 +after_success: + - coveralls diff --git a/requirements-dev.txt b/requirements-dev.txt index df982feb..09761ce4 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,5 @@ autopep8 +coveralls coverage flake8 flake8-blind-except From 8502a7e1c9770a42e44b4f1beb34c887212e7d48 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:23:07 +0100 Subject: [PATCH 089/163] Add coverage badge to the readme --- README.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.rst b/README.rst index dc5a8098..71610e45 100644 --- a/README.rst +++ b/README.rst @@ -7,6 +7,9 @@ python-zeroconf .. image:: http://img.shields.io/pypi/v/zeroconf.svg :target: https://pypi.python.org/pypi/zeroconf +.. image:: http://img.shields.io/coveralls/jstasiak/python-zeroconf.svg + :target: https://coveralls.io/r/jstasiak/python-zeroconf + This is fork of pyzeroconf, Multicast DNS Service Discovery for Python, originally by Paul Scott-Murphy (https://github.com/paulsm/pyzeroconf), From 161743ea387c961d3554488239f93df4b39be18c Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:25:57 +0100 Subject: [PATCH 090/163] Update README.rst --- README.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 71610e45..e12a37a9 100644 --- a/README.rst +++ b/README.rst @@ -53,9 +53,10 @@ How to get python-zeroconf? * PyPI page https://pypi.python.org/pypi/zeroconf * GitHub project https://github.com/jstasiak/python-zeroconf -The easiest way to install python-zeroconf is using pip: +The easiest way to install python-zeroconf is using pip:: + + pip install zeroconf -``pip install zeroconf`` How do I use it? From 9e81863de37e2ab972d5a76a1dc2d5c517f83cc6 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:26:35 +0100 Subject: [PATCH 091/163] Bump version to 0.15.1 --- zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index 2bb58940..309b646e 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -24,7 +24,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.15' +__version__ = '0.15.1' __license__ = 'LGPL' import time From bf97c1459a9d91d6aa88d7bf34c5f8b4cd3cedc5 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:30:57 +0100 Subject: [PATCH 092/163] Create universal wheels --- setup.cfg | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 setup.cfg diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 00000000..5e409001 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[wheel] +universal = 1 From b290965ecd589ca4feb1f88a4232d1ec2725dc44 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:32:53 +0100 Subject: [PATCH 093/163] Update README.rst --- README.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.rst b/README.rst index e12a37a9..e1359b06 100644 --- a/README.rst +++ b/README.rst @@ -92,6 +92,11 @@ See examples directory for more. Changelog ========= +0.15.1 +------ + +* Fixed handling closed socket (GitHub #4) + 0.15 ---- From 47ff62bae1fd69ffd953c82bd480e4770bfee97b Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 10 Jul 2014 01:39:55 +0100 Subject: [PATCH 094/163] Update README.rst --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index e1359b06..cad0ab08 100644 --- a/README.rst +++ b/README.rst @@ -4,10 +4,10 @@ python-zeroconf .. image:: https://travis-ci.org/jstasiak/python-zeroconf.svg?branch=master :target: https://travis-ci.org/jstasiak/python-zeroconf -.. image:: http://img.shields.io/pypi/v/zeroconf.svg +.. image:: https://img.shields.io/pypi/v/zeroconf.svg :target: https://pypi.python.org/pypi/zeroconf -.. image:: http://img.shields.io/coveralls/jstasiak/python-zeroconf.svg +.. image:: https://img.shields.io/coveralls/jstasiak/python-zeroconf.svg :target: https://coveralls.io/r/jstasiak/python-zeroconf From ada563c5a1f6d7c54f2ae5c495503079c395438f Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 2 Nov 2014 19:40:19 +0000 Subject: [PATCH 095/163] Rationalize exception handling a bit and setup logging --- zeroconf.py | 98 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 40 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 309b646e..0b82779c 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -27,16 +27,32 @@ __version__ = '0.15.1' __license__ = 'LGPL' +import logging import time import struct import socket import threading import select -import traceback from functools import reduce + +try: + NullHandler = logging.NullHandler +except AttributeError: + # Python 2.6 fallback + class NullHandler(logging.Handler): + def emit(self, record): + pass + __all__ = ["Zeroconf", "ServiceInfo", "ServiceBrowser"] + +log = logging.getLogger(__name__) +log.addHandler(NullHandler()) + +if log.level == logging.NOTSET: + log.setLevel(logging.WARN) + try: xrange = xrange except NameError: @@ -333,7 +349,8 @@ def __repr__(self): """String representation""" try: return socket.inet_ntoa(self.address) - except Exception: # TODO stop catching all Exceptions + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) return self.address @@ -746,27 +763,23 @@ def __init__(self): def add(self, entry): """Adds an entry""" - try: - list = self.cache[entry.key] - except Exception: # TODO stop catching all Exceptions - list = self.cache[entry.key] = [] - list.append(entry) + self.cache.setdefault(entry.key, []).append(entry) def remove(self, entry): """Removes an entry""" try: - list = self.cache[entry.key] - list.remove(entry) - except Exception: # TODO stop catching all Exceptions + list_ = self.cache[entry.key] + list_.remove(entry) + except (KeyError, ValueError): pass def get(self, entry): """Gets an entry by key. Will return None if there is no matching entry.""" try: - list = self.cache[entry.key] - return list[list.index(entry)] - except Exception: # TODO stop catching all Exceptions + list_ = self.cache[entry.key] + return list_[list_.index(entry)] + except (KeyError, ValueError): return None def getByDetails(self, name, type, clazz): @@ -779,17 +792,15 @@ def entriesWithName(self, name): """Returns a list of entries whose key matches the name.""" try: return self.cache[name] - except Exception: # TODO stop catching all Exceptions + except KeyError: return [] def entries(self): """Returns a list of all entries""" - def add(x, y): - return x + y - try: - return reduce(add, self.cache.values()) - except Exception: # TODO stop catching all Exceptions + if not self.cache: return [] + else: + return reduce(lambda a, b: a + b, self.cache.values()) class Engine(threading.Thread): @@ -830,10 +841,10 @@ def run(self): for socket_ in rr: try: self.readers[socket_].handle_read() - except Exception: # TODO stop catching all Exceptions - traceback.print_exc() - except Exception: # TODO stop catching all Exceptions - pass + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) def getReaders(self): result = [] @@ -965,7 +976,8 @@ def updateRecord(self, zc, now, record): self.type, record.alias) self.list.append(callback) return - except Exception: # TODO stop catching all Exceptions + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) if not expired: self.services[record.alias.lower()] = record callback = lambda x: self.listener.addService(x, @@ -1089,7 +1101,8 @@ def setText(self, text): value = True elif value == 'false' or not value: value = False - except Exception: # TODO stop catching all Exceptions + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) # No equals sign at all key = s value = False @@ -1099,8 +1112,8 @@ def setText(self, text): result[key] = value self.properties = result - except Exception: # TODO stop catching all Exceptions - traceback.print_exc() + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) self.properties = None def getType(self): @@ -1245,32 +1258,36 @@ def __init__(self, bindaddress=None): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('4.2.2.1', 123)) self.intf = s.getsockname()[0] - except Exception: # TODO stop catching all Exceptions + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) self.intf = socket.gethostbyname(socket.gethostname()) else: self.intf = bindaddress + log.debug('Bind address is %r' % (self.intf,)) self.group = ('', _MDNS_PORT) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) - except Exception: # TODO stop catching all Exceptions + except Exception as e: # TODO stop catching all Exceptions # SO_REUSEADDR should be equivalent to SO_REUSEPORT for # multicast UDP sockets (p 731, "TCP/IP Illustrated, # Volume 2"), but some BSD-derived systems require # SO_REUSEPORT to be specified explicity. Also, not all # versions of Python have SO_REUSEPORT available. # - pass + log.exception('Unknown error, possibly benign: %r', e) + self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) try: + log.debug('Binding to %r', self.group) self.socket.bind(self.group) - except Exception: # TODO stop catching all Exceptions + except Exception as e: # TODO stop catching all Exceptions # Some versions of linux raise an exception even though # the SO_REUSE* options have been set, so ignore it # - pass + log.exception('Unknown error, possibly benign: %r', e) # self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, # socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, @@ -1373,8 +1390,8 @@ def unregisterService(self, info): self.servicetypes[info.type] -= 1 else: del self.servicetypes[info.type] - except Exception: # TODO stop catching all Exceptions - pass + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) now = currentTimeMillis() nextTime = now i = 0 @@ -1473,8 +1490,8 @@ def removeListener(self, listener): try: self.listeners.remove(listener) self.notifyAll() - except Exception: # TODO stop catching all Exceptions - pass + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) def updateRecord(self, now, rec): """Used to notify listeners of new information that has updated @@ -1560,8 +1577,8 @@ def handleQuery(self, msg, addr, port): out.addAdditionalAnswer(DNSAddress(service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address)) - except Exception: # TODO stop catching all Exceptions - traceback.print_exc() + except Exception as e: # TODO stop catching all Exceptions + log.exception('Unknown error, possibly benign: %r', e) if out is not None and out.answers: out.id = msg.id @@ -1576,9 +1593,9 @@ def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): if bytes_sent < 0: break packet = packet[bytes_sent:] - except Exception: # TODO stop catching all Exceptions + except Exception as e: # TODO stop catching all Exceptions # Ignore this, it may be a temporary loss of network connection - pass + log.exception('Unknown error, possibly benign: %r', e) def close(self): """Ends the background threads, and prevent this instance from @@ -1599,6 +1616,7 @@ def close(self): # query (for Zoe), and service unregistration. if __name__ == '__main__': + log.setLevel(logging.DEBUG) print("Multicast DNS Service Discovery for Python, version %s" % __version__) r = Zeroconf() print("1. Testing registration of a service...") From 5429748190950a5daf7e9cf91de824dfbd06ee7a Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 2 Nov 2014 20:31:54 +0000 Subject: [PATCH 096/163] Switch to snake case and clean up import order Closes #2 --- README.rst | 8 +- examples/browser.py | 21 +- examples/registration.py | 4 +- requirements-dev.txt | 2 + setup.cfg | 5 + test_zeroconf.py | 56 ++-- zeroconf.py | 619 +++++++++++++++++++-------------------- 7 files changed, 347 insertions(+), 368 deletions(-) diff --git a/README.rst b/README.rst index cad0ab08..34a851a3 100644 --- a/README.rst +++ b/README.rst @@ -71,11 +71,11 @@ Here's an example: class MyListener(object): - def removeService(self, zeroconf, type, name): + def remove_service(self, zeroconf, type, name): print("Service %s removed" % (name,)) - def addService(self, zeroconf, type, name): - info = zeroconf.getServiceInfo(type, name) + def add_service(self, zeroconf, type, name): + info = zeroconf.get_service_info(type, name) print("Service %s added, service info: %s" % (name, info)) @@ -182,7 +182,7 @@ Changelog * new style objects * fixed hostname/interface problem * fixed socket timeout problem -* fixed addServiceListener() typo bug +* fixed add_service_listener() typo bug * using select() for socket reads * tested on Debian unstable with Python 2.2.2 diff --git a/examples/browser.py b/examples/browser.py index 219c9edf..1b3b7179 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -10,24 +10,23 @@ class MyListener(object): - def removeService(self, zeroconf, type, name): + def remove_service(self, zeroconf, type, name): print("Service %s removed" % (name,)) print('\n') - def addService(self, zeroconf, type, name): + def add_service(self, zeroconf, type, name): print("Service %s added" % (name,)) print(" Type is %s" % (type,)) - info = zeroconf.getServiceInfo(type, name) + info = zeroconf.get_service_info(type, name) if info: - print(" Address is %s:%d" % (socket.inet_ntoa(info.getAddress()), - info.getPort())) - print(" Weight is %d, Priority is %d" % (info.getWeight(), - info.getPriority())) - print(" Server is", info.getServer()) - prop = info.getProperties() - if prop: + print(" Address is %s:%d" % (socket.inet_ntoa(info.address), + info.port)) + print(" Weight is %d, Priority is %d" % (info.weight, + info.priority)) + print(" Server is", info.server) + if info.properties: print(" Properties are") - for key, value in prop.items(): + for key, value in info.properties.items(): print(" %s: %s" % (key, value)) else: print(" No info") diff --git a/examples/registration.py b/examples/registration.py index 293047f4..18e440f3 100755 --- a/examples/registration.py +++ b/examples/registration.py @@ -15,10 +15,10 @@ zeroconf = Zeroconf() print("Registration of a service...") -zeroconf.registerService(info) +zeroconf.register_service(info) try: raw_input("Waiting (press Enter to exit)...") finally: print("Unregistering...") - zeroconf.unregisterService(info) + zeroconf.unregister_service(info) zeroconf.close() diff --git a/requirements-dev.txt b/requirements-dev.txt index 09761ce4..4bc24d07 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,5 +3,7 @@ coveralls coverage flake8 flake8-blind-except +flake8-import-order>=0.4.0 mock nose +pep8-naming diff --git a/setup.cfg b/setup.cfg index 5e409001..51017e12 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,7 @@ [wheel] universal = 1 + +[flake8] +show-source = 1 +import-order-style=google +application-import-names=zeroconf diff --git a/test_zeroconf.py b/test_zeroconf.py index 675bef69..57c7bbd9 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -22,28 +22,28 @@ class PacketGeneration(unittest.TestCase): - def testParseOwnPacketSimple(self): + def test_parse_own_packet_simple(self): generated = r.DNSOutgoing(0) r.DNSIncoming(generated.packet()) - def testParseOwnPacketSimpleUnicast(self): + def test_parse_own_packet_simple_unicast(self): generated = r.DNSOutgoing(0, 0) r.DNSIncoming(generated.packet()) - def testParseOwnPacketFlags(self): + def test_parse_own_packet_flags(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) r.DNSIncoming(generated.packet()) - def testParseOwnPacketQuestion(self): + def test_parse_own_packet_question(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) - generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV, - r._CLASS_IN)) + generated.add_question(r.DNSQuestion("testname.local.", r._TYPE_SRV, + r._CLASS_IN)) r.DNSIncoming(generated.packet()) - def testMatchQuestion(self): + def test_match_question(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) + generated.add_question(question) parsed = r.DNSIncoming(generated.packet()) self.assertEqual(len(generated.questions), 1) self.assertEqual(len(generated.questions), len(parsed.questions)) @@ -52,26 +52,26 @@ def testMatchQuestion(self): class PacketForm(unittest.TestCase): - def testTransactionID(self): + def test_transaction_id(self): """ID must be zero in a DNS-SD packet""" generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() id = byte_ord(bytes[0]) << 8 | byte_ord(bytes[1]) self.assertEqual(id, 0) - def testQueryHeaderBits(self): + def test_query_header_bits(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() flags = byte_ord(bytes[2]) << 8 | byte_ord(bytes[3]) self.assertEqual(flags, 0x0) - def testResponseHeaderBits(self): + def test_response_header_bits(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() flags = byte_ord(bytes[2]) << 8 | byte_ord(bytes[3]) self.assertEqual(flags, 0x8000) - def testNumbers(self): + def test_numbers(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() (numQuestions, numAnswers, numAuthorities, @@ -81,11 +81,11 @@ def testNumbers(self): self.assertEqual(numAuthorities, 0) self.assertEqual(numAdditionals, 0) - def testNumbersQuestions(self): + def test_numbers_questions(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN) for i in xrange(10): - generated.addQuestion(question) + generated.add_question(question) bytes = generated.packet() (numQuestions, numAnswers, numAuthorities, numAdditionals) = struct.unpack('!4H', bytes[4:12]) @@ -97,39 +97,39 @@ def testNumbersQuestions(self): class Names(unittest.TestCase): - def testLongName(self): + def test_long_name(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion("this.is.a.very.long.name.with.lots.of.parts.in.it.local.", r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) + generated.add_question(question) r.DNSIncoming(generated.packet()) - def testExceedinglyLongName(self): + def test_exceedingly_long_name(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) name = "%slocal." % ("part." * 1000) question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) + generated.add_question(question) r.DNSIncoming(generated.packet()) - def testExceedinglyLongNamePart(self): + def test_exceedingly_long_name_part(self): name = "%s.local." % ("a" * 1000) generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) + generated.add_question(question) self.assertRaises(r.NamePartTooLongException, generated.packet) - def testSameName(self): + def test_same_name(self): name = "paired.local." generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN) - generated.addQuestion(question) - generated.addQuestion(question) + generated.add_question(question) + generated.add_question(question) r.DNSIncoming(generated.packet()) class Framework(unittest.TestCase): - def testLaunchAndClose(self): + def test_launch_and_close(self): rv = r.Zeroconf() rv.close() @@ -143,11 +143,11 @@ def test_integration(): class MyListener(object): - def removeService(self, zeroconf, type_, name): + def remove_service(self, zeroconf, type_, name): if name == registration_name: service_removed.set() - def addService(self, zeroconf, type_, name): + def add_service(self, zeroconf, type_, name): if name == registration_name: service_added.set() @@ -161,12 +161,12 @@ def addService(self, zeroconf, type_, name): type_, registration_name, socket.inet_aton("10.0.1.2"), 80, 0, 0, desc, "ash-2.local.") - zeroconf_registrar.registerService(info) + zeroconf_registrar.register_service(info) try: service_added.wait(1) assert service_added.is_set() - zeroconf_registrar.unregisterService(info) + zeroconf_registrar.unregister_service(info) service_removed.wait(1) assert service_removed.is_set() finally: diff --git a/zeroconf.py b/zeroconf.py index 0b82779c..74f27378 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -28,11 +28,11 @@ __license__ = 'LGPL' import logging -import time -import struct +import select import socket +import struct import threading -import select +import time from functools import reduce @@ -41,6 +41,7 @@ except AttributeError: # Python 2.6 fallback class NullHandler(logging.Handler): + def emit(self, record): pass @@ -174,7 +175,7 @@ def emit(self, record): # utility functions -def currentTimeMillis(): +def current_time_millis(): """Current system time in milliseconds""" return time.time() * 1000 @@ -225,18 +226,18 @@ def __ne__(self, other): """Non-equality test""" return not self.__eq__(other) - def getClazz(self, clazz): + def get_clazz(self, clazz): """Class accessor""" return _CLASSES.get(clazz, "?(%s)" % clazz) - def getType(self, t): + def get_type(self, t): """Type accessor""" return _TYPES.get(t, "?(%s)" % t) - def toString(self, hdr, other): + def to_string(self, hdr, other): """String representation with additional information""" - result = "%s[%s,%s" % (hdr, self.getType(self.type), - self.getClazz(self.clazz)) + result = "%s[%s,%s" % (hdr, self.get_type(self.type), + self.get_clazz(self.clazz)) if self.unique: result += "-unique," else: @@ -258,7 +259,7 @@ def __init__(self, name, type, clazz): # raise NonLocalNameException DNSEntry.__init__(self, name, type, clazz) - def answeredBy(self, rec): + def answered_by(self, rec): """Returns true if the question is answered by the record""" return (self.clazz == rec.clazz and (self.type == rec.type or self.type == _TYPE_ANY) and @@ -266,7 +267,7 @@ def answeredBy(self, rec): def __repr__(self): """String representation""" - return DNSEntry.toString(self, "question", None) + return DNSEntry.to_string(self, "question", None) class DNSRecord(DNSEntry): @@ -276,43 +277,43 @@ class DNSRecord(DNSEntry): def __init__(self, name, type, clazz, ttl): DNSEntry.__init__(self, name, type, clazz) self.ttl = ttl - self.created = currentTimeMillis() + self.created = current_time_millis() def __eq__(self, other): """Tests equality as per DNSRecord""" return isinstance(other, DNSRecord) and DNSEntry.__eq__(self, other) - def suppressedBy(self, msg): + def suppressed_by(self, msg): """Returns true if any answer in a message can suffice for the information held in this record.""" for record in msg.answers: - if self.suppressedByAnswer(record): + if self.suppressed_by_answer(record): return True return False - def suppressedByAnswer(self, other): + def suppressed_by_answer(self, other): """Returns true if another record has same name, type and class, and if its TTL is at least half of this record's.""" return self == other and other.ttl > (self.ttl / 2) - def getExpirationTime(self, percent): + def get_expiration_time(self, percent): """Returns the time at which this record will have expired by a certain percentage.""" return self.created + (percent * self.ttl * 10) - def getRemainingTTL(self, now): + def get_remaining_ttl(self, now): """Returns the remaining TTL in seconds.""" - return max(0, (self.getExpirationTime(100) - now) / 1000) + return max(0, (self.get_expiration_time(100) - now) / 1000) - def isExpired(self, now): + def is_expired(self, now): """Returns true if this record has expired.""" - return self.getExpirationTime(100) <= now + return self.get_expiration_time(100) <= now - def isStale(self, now): + def is_stale(self, now): """Returns true if this record is at least half way expired.""" - return self.getExpirationTime(50) <= now + return self.get_expiration_time(50) <= now - def resetTTL(self, other): + def reset_ttl(self, other): """Sets this record's TTL and created time to that of another record.""" self.created = other.created @@ -322,11 +323,11 @@ def write(self, out): """Abstract method""" raise AbstractMethodException - def toString(self, other): + def to_string(self, other): """String representation with addtional information""" arg = "%s/%s,%s" % (self.ttl, - self.getRemainingTTL(currentTimeMillis()), other) - return DNSEntry.toString(self, "record", arg) + self.get_remaining_ttl(current_time_millis()), other) + return DNSEntry.to_string(self, "record", arg) class DNSAddress(DNSRecord): @@ -339,7 +340,7 @@ def __init__(self, name, type, clazz, ttl, address): def write(self, out): """Used in constructing an outgoing packet""" - out.writeString(self.address) + out.write_string(self.address) def __eq__(self, other): """Tests equality on address""" @@ -365,8 +366,8 @@ def __init__(self, name, type, clazz, ttl, cpu, os): def write(self, out): """Used in constructing an outgoing packet""" - out.writeString(self.cpu) - out.writeString(self.oso) + out.write_string(self.cpu) + out.write_string(self.oso) def __eq__(self, other): """Tests equality on cpu and os""" @@ -388,7 +389,7 @@ def __init__(self, name, type, clazz, ttl, alias): def write(self, out): """Used in constructing an outgoing packet""" - out.writeName(self.alias) + out.write_name(self.alias) def __eq__(self, other): """Tests equality on alias""" @@ -396,7 +397,7 @@ def __eq__(self, other): def __repr__(self): """String representation""" - return self.toString(self.alias) + return self.to_string(self.alias) class DNSText(DNSRecord): @@ -410,7 +411,7 @@ def __init__(self, name, type_, clazz, ttl, text): def write(self, out): """Used in constructing an outgoing packet""" - out.writeString(self.text) + out.write_string(self.text) def __eq__(self, other): """Tests equality on text""" @@ -419,9 +420,9 @@ def __eq__(self, other): def __repr__(self): """String representation""" if len(self.text) > 10: - return self.toString(self.text[:7] + "...") + return self.to_string(self.text[:7] + "...") else: - return self.toString(self.text) + return self.to_string(self.text) class DNSService(DNSRecord): @@ -437,10 +438,10 @@ def __init__(self, name, type, clazz, ttl, priority, weight, port, server): def write(self, out): """Used in constructing an outgoing packet""" - out.writeShort(self.priority) - out.writeShort(self.weight) - out.writeShort(self.port) - out.writeName(self.server) + out.write_short(self.priority) + out.write_short(self.weight) + out.write_short(self.port) + out.write_name(self.server) def __eq__(self, other): """Tests equality on priority, weight, port and server""" @@ -452,7 +453,7 @@ def __eq__(self, other): def __repr__(self): """String representation""" - return self.toString("%s:%s" % (self.server, self.port)) + return self.to_string("%s:%s" % (self.server, self.port)) class DNSIncoming(object): @@ -470,9 +471,9 @@ def __init__(self, data): self.numAuthorities = 0 self.numAdditionals = 0 - self.readHeader() - self.readQuestions() - self.readOthers() + self.read_header() + self.read_questions() + self.read_others() def unpack(self, format): length = struct.calcsize(format) @@ -480,64 +481,64 @@ def unpack(self, format): self.offset += length return info - def readHeader(self): + def read_header(self): """Reads header portion of packet""" (self.id, self.flags, self.numQuestions, self.numAnswers, self.numAuthorities, self.numAdditionals) = self.unpack(b'!6H') - def readQuestions(self): + def read_questions(self): """Reads questions section of packet""" for i in xrange(self.numQuestions): - name = self.readName() + name = self.read_name() type, clazz = self.unpack(b'!HH') question = DNSQuestion(name, type, clazz) self.questions.append(question) - def readInt(self): + def read_int(self): """Reads an integer from the packet""" return self.unpack(b'!I')[0] - def readCharacterString(self): + def read_character_string(self): """Reads a character string from the packet""" length = byte_ord(self.data[self.offset]) self.offset += 1 - return self.readString(length) + return self.read_string(length) - def readString(self, length): + def read_string(self, length): """Reads a string of a given length from the packet""" info = self.data[self.offset:self.offset + length] self.offset += length return info - def readUnsignedShort(self): + def read_unsigned_short(self): """Reads an unsigned short from the packet""" return self.unpack(b'!H')[0] - def readOthers(self): + def read_others(self): """Reads the answers, authorities and additionals section of the packet""" n = self.numAnswers + self.numAuthorities + self.numAdditionals for i in xrange(n): - domain = self.readName() + domain = self.read_name() type, clazz, ttl, length = self.unpack(b'!HHiH') rec = None if type == _TYPE_A: - rec = DNSAddress(domain, type, clazz, ttl, self.readString(4)) + rec = DNSAddress(domain, type, clazz, ttl, self.read_string(4)) elif type == _TYPE_CNAME or type == _TYPE_PTR: - rec = DNSPointer(domain, type, clazz, ttl, self.readName()) + rec = DNSPointer(domain, type, clazz, ttl, self.read_name()) elif type == _TYPE_TXT: - rec = DNSText(domain, type, clazz, ttl, self.readString(length)) + rec = DNSText(domain, type, clazz, ttl, self.read_string(length)) elif type == _TYPE_SRV: rec = DNSService(domain, type, clazz, ttl, - self.readUnsignedShort(), self.readUnsignedShort(), - self.readUnsignedShort(), self.readName()) + self.read_unsigned_short(), self.readUnsignedShort(), + self.read_unsigned_short(), self.read_name()) elif type == _TYPE_HINFO: rec = DNSHinfo(domain, type, clazz, ttl, - self.readCharacterString(), self.readCharacterString()) + self.read_character_string(), self.readCharacterString()) elif type == _TYPE_AAAA: - rec = DNSAddress(domain, type, clazz, ttl, self.readString(16)) + rec = DNSAddress(domain, type, clazz, ttl, self.read_string(16)) else: # Try to ignore types we don't know about # Skip the payload for the resource record so the next @@ -547,19 +548,19 @@ def readOthers(self): if rec is not None: self.answers.append(rec) - def isQuery(self): + def is_query(self): """Returns true if this is a query""" return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY - def isResponse(self): + def is_response(self): """Returns true if this is a response""" return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE - def readUTF(self, offset, length): + def read_utf(self, offset, length): """Reads a UTF-8 string of a given length from the packet""" return unicode(self.data[offset:offset + length], 'utf-8', 'replace') - def readName(self): + def read_name(self): """Reads a domain name from the packet""" result = '' off = self.offset @@ -573,7 +574,7 @@ def readName(self): break t = length & 0xC0 if t == 0x00: - result = ''.join((result, self.readUTF(off, length) + '.')) + result = ''.join((result, self.read_utf(off, length) + '.')) off += length elif t == 0xC0: if next < 0: @@ -613,26 +614,26 @@ def __init__(self, flags, multicast=True): self.authorities = [] self.additionals = [] - def addQuestion(self, record): + def add_question(self, record): """Adds a question""" self.questions.append(record) - def addAnswer(self, inp, record): + def add_answer(self, inp, record): """Adds an answer""" - if not record.suppressedBy(inp): - self.addAnswerAtTime(record, 0) + if not record.suppressed_by(inp): + self.add_answer_at_time(record, 0) - def addAnswerAtTime(self, record, now): + def add_answer_at_time(self, record, now): """Adds an answer if if does not expire by a certain time""" if record is not None: - if now == 0 or not record.isExpired(now): + if now == 0 or not record.is_expired(now): self.answers.append((record, now)) - def addAuthorativeAnswer(self, record): + def add_authorative_answer(self, record): """Adds an authoritative answer""" self.authorities.append(record) - def addAdditionalAnswer(self, record): + def add_additional_answer(self, record): """Adds an additional answer""" self.additionals.append(record) @@ -640,39 +641,39 @@ def pack(self, format, value): self.data.append(struct.pack(format, value)) self.size += struct.calcsize(format) - def writeByte(self, value): + def write_byte(self, value): """Writes a single byte to the packet""" self.pack(b'!c', byte_chr(value)) - def insertShort(self, index, value): + def insert_short(self, index, value): """Inserts an unsigned short in a certain position in the packet""" self.data.insert(index, struct.pack(b'!H', value)) self.size += 2 - def writeShort(self, value): + def write_short(self, value): """Writes an unsigned short to the packet""" self.pack(b'!H', value) - def writeInt(self, value): + def write_int(self, value): """Writes an unsigned integer to the packet""" self.pack(b'!I', int(value)) - def writeString(self, value): + def write_string(self, value): """Writes a string to the packet""" assert isinstance(value, bytes) self.data.append(value) self.size += len(value) - def writeUTF(self, s): + def write_utf(self, s): """Writes a UTF-8 string of a given length to the packet""" utfstr = s.encode('utf-8') length = len(utfstr) if length > 64: raise NamePartTooLongException - self.writeByte(length) - self.writeString(utfstr) + self.write_byte(length) + self.write_string(utfstr) - def writeName(self, name): + def write_name(self, name): """Writes a domain name to the packet""" if name in self.names: @@ -682,8 +683,8 @@ def writeName(self, name): # An index was found, so write a pointer to it # - self.writeByte((index >> 8) | 0xC0) - self.writeByte(index & 0xFF) + self.write_byte((index >> 8) | 0xC0) + self.write_byte(index & 0xFF) else: # No record of this name already, so write it # out as normal, recording the location of the name @@ -694,28 +695,28 @@ def writeName(self, name): if parts[-1] == '': parts = parts[:-1] for part in parts: - self.writeUTF(part) - self.writeByte(0) + self.write_utf(part) + self.write_byte(0) - def writeQuestion(self, question): + def write_question(self, question): """Writes a question to the packet""" - self.writeName(question.name) - self.writeShort(question.type) - self.writeShort(question.clazz) + self.write_name(question.name) + self.write_short(question.type) + self.write_short(question.clazz) - def writeRecord(self, record, now): + def write_record(self, record, now): """Writes a record (answer, authoritative answer, additional) to the packet""" - self.writeName(record.name) - self.writeShort(record.type) + self.write_name(record.name) + self.write_short(record.type) if record.unique and self.multicast: - self.writeShort(record.clazz | _CLASS_UNIQUE) + self.write_short(record.clazz | _CLASS_UNIQUE) else: - self.writeShort(record.clazz) + self.write_short(record.clazz) if now == 0: - self.writeInt(record.ttl) + self.write_int(record.ttl) else: - self.writeInt(record.getRemainingTTL(now)) + self.write_int(record.get_remaining_ttl(now)) index = len(self.data) # Adjust size for the short we will write before this record # @@ -724,7 +725,7 @@ def writeRecord(self, record, now): self.size -= 2 length = len(b''.join(self.data[index:])) - self.insertShort(index, length) # Here is the short we adjusted for + self.insert_short(index, length) # Here is the short we adjusted for def packet(self): """Returns a string containing the packet's bytes @@ -734,23 +735,23 @@ def packet(self): if not self.finished: self.finished = True for question in self.questions: - self.writeQuestion(question) + self.write_question(question) for answer, time_ in self.answers: - self.writeRecord(answer, time_) + self.write_record(answer, time_) for authority in self.authorities: - self.writeRecord(authority, 0) + self.write_record(authority, 0) for additional in self.additionals: - self.writeRecord(additional, 0) + self.write_record(additional, 0) - self.insertShort(0, len(self.additionals)) - self.insertShort(0, len(self.authorities)) - self.insertShort(0, len(self.answers)) - self.insertShort(0, len(self.questions)) - self.insertShort(0, self.flags) + self.insert_short(0, len(self.additionals)) + self.insert_short(0, len(self.authorities)) + self.insert_short(0, len(self.answers)) + self.insert_short(0, len(self.questions)) + self.insert_short(0, self.flags) if self.multicast: - self.insertShort(0, 0) + self.insert_short(0, 0) else: - self.insertShort(0, self.id) + self.insert_short(0, self.id) return b''.join(self.data) @@ -782,13 +783,13 @@ def get(self, entry): except (KeyError, ValueError): return None - def getByDetails(self, name, type, clazz): + def get_by_details(self, name, type, clazz): """Gets an entry by details. Will return None if there is no matching entry.""" entry = DNSEntry(name, type, clazz) return self.get(entry) - def entriesWithName(self, name): + def entries_with_name(self, name): """Returns a list of entries whose key matches the name.""" try: return self.cache[name] @@ -827,7 +828,7 @@ def __init__(self, zc): def run(self): while not _GLOBAL_DONE: - rs = self.getReaders() + rs = self.get_readers() if len(rs) == 0: # No sockets to manage, but we wait for the timeout # or addition of a socket @@ -846,20 +847,20 @@ def run(self): except Exception as e: # TODO stop catching all Exceptions log.exception('Unknown error, possibly benign: %r', e) - def getReaders(self): + def get_readers(self): result = [] self.condition.acquire() result = self.readers.keys() self.condition.release() return result - def addReader(self, reader, socket): + def add_reader(self, reader, socket): self.condition.acquire() self.readers[socket] = reader self.condition.notify() self.condition.release() - def delReader(self, socket): + def del_reader(self, socket): self.condition.acquire() del(self.readers[socket]) self.condition.notify() @@ -882,7 +883,7 @@ class Listener(object): def __init__(self, zc): self.zc = zc - self.zc.engine.addReader(self, self.zc.socket) + self.zc.engine.add_reader(self, self.zc.socket) def handle_read(self): try: @@ -897,19 +898,19 @@ def handle_read(self): raise e self.data = data msg = DNSIncoming(data) - if msg.isQuery(): + if msg.is_query(): # Always multicast responses # if port == _MDNS_PORT: - self.zc.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) + self.zc.handle_query(msg, _MDNS_ADDR, _MDNS_PORT) # If it's not a multicast query, reply via unicast # and multicast # elif port == _DNS_PORT: - self.zc.handleQuery(msg, addr, port) - self.zc.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) + self.zc.handle_query(msg, addr, port) + self.zc.handle_query(msg, _MDNS_ADDR, _MDNS_PORT) else: - self.zc.handleResponse(msg) + self.zc.handle_response(msg) class Reaper(threading.Thread): @@ -928,10 +929,10 @@ def run(self): self.zc.wait(10 * 1000) if _GLOBAL_DONE: return - now = currentTimeMillis() + now = current_time_millis() for record in self.zc.cache.entries(): - if record.isExpired(now): - self.zc.updateRecord(now, record) + if record.is_expired(now): + self.zc.update_record(now, record) self.zc.cache.remove(record) @@ -939,8 +940,8 @@ class ServiceBrowser(threading.Thread): """Used to browse for a service of a specific type. - The listener object will have its addService() and - removeService() methods called when this browser + The listener object will have its add_service() and + remove_service() methods called when this browser discovers changes in the services availability.""" def __init__(self, zc, type, listener): @@ -951,65 +952,65 @@ def __init__(self, zc, type, listener): self.type = type self.listener = listener self.services = {} - self.nextTime = currentTimeMillis() + self.next_time = current_time_millis() self.delay = _BROWSER_TIME self.list = [] self.done = False - self.zc.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) + self.zc.add_listener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) self.start() - def updateRecord(self, zc, now, record): + def update_record(self, zc, now, record): """Callback invoked by Zeroconf when new information arrives. Updates information required by browser in the Zeroconf cache.""" if record.type == _TYPE_PTR and record.name == self.type: - expired = record.isExpired(now) + expired = record.is_expired(now) try: oldrecord = self.services[record.alias.lower()] if not expired: - oldrecord.resetTTL(record) + oldrecord.reset_ttl(record) else: del(self.services[record.alias.lower()]) - callback = lambda x: self.listener.removeService(x, - self.type, record.alias) + callback = lambda x: self.listener.remove_service(x, + self.type, record.alias) self.list.append(callback) return except Exception as e: # TODO stop catching all Exceptions log.exception('Unknown error, possibly benign: %r', e) if not expired: self.services[record.alias.lower()] = record - callback = lambda x: self.listener.addService(x, - self.type, record.alias) + callback = lambda x: self.listener.add_service(x, + self.type, record.alias) self.list.append(callback) - expires = record.getExpirationTime(75) - if expires < self.nextTime: - self.nextTime = expires + expires = record.get_expiration_time(75) + if expires < self.next_time: + self.next_time = expires def cancel(self): self.done = True - self.zc.notifyAll() + self.zc.notify_all() def run(self): while True: event = None - now = currentTimeMillis() - if len(self.list) == 0 and self.nextTime > now: - self.zc.wait(self.nextTime - now) + now = current_time_millis() + if len(self.list) == 0 and self.next_time > now: + self.zc.wait(self.next_time - now) if _GLOBAL_DONE or self.done: return - now = currentTimeMillis() + now = current_time_millis() - if self.nextTime <= now: + if self.next_time <= now: out = DNSOutgoing(_FLAGS_QR_QUERY) - out.addQuestion(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) + out.add_question(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) for record in self.services.values(): - if not record.isExpired(now): - out.addAnswerAtTime(record, now) + if not record.is_expired(now): + out.add_answer_at_time(record, now) self.zc.send(out) - self.nextTime = now + self.delay + self.next_time = now + self.delay self.delay = min(20 * 1000, self.delay * 2) if len(self.list) > 0: @@ -1049,12 +1050,16 @@ def __init__(self, type, name, address=None, port=None, weight=0, self.server = server else: self.server = name - self.setProperties(properties) + self._set_properties(properties) - def setProperties(self, properties): + @property + def properties(self): + return self._properties + + def _set_properties(self, properties): """Sets properties and text of this info from a dictionary""" if isinstance(properties, dict): - self.properties = properties + self._properties = properties list = [] result = b'' for key in properties: @@ -1080,7 +1085,7 @@ def setProperties(self, properties): else: self.text = properties - def setText(self, text): + def _set_text(self, text): """Sets properties and text given a text field""" self.text = text try: @@ -1111,52 +1116,20 @@ def setText(self, text): if key and result.get(key) is None: result[key] = value - self.properties = result + self._properties = result except Exception as e: # TODO stop catching all Exceptions log.exception('Unknown error, possibly benign: %r', e) - self.properties = None + self._properties = None - def getType(self): - """Type accessor""" - return self.type - - def getName(self): + def get_name(self): """Name accessor""" if self.type is not None and self.name.endswith("." + self.type): return self.name[:len(self.name) - len(self.type) - 1] return self.name - def getAddress(self): - """Address accessor""" - return self.address - - def getPort(self): - """Port accessor""" - return self.port - - def getPriority(self): - """Pirority accessor""" - return self.priority - - def getWeight(self): - """Weight accessor""" - return self.weight - - def getProperties(self): - """Properties accessor""" - return self.properties - - def getText(self): - """Text accessor""" - return self.text - - def getServer(self): - """Server accessor""" - return self.server - - def updateRecord(self, zc, now, record): + def update_record(self, zc, now, record): """Updates service information from a DNS record""" - if record is not None and not record.isExpired(now): + if record is not None and not record.is_expired(now): if record.type == _TYPE_A: # if record.name == self.name: if record.name == self.server: @@ -1168,51 +1141,51 @@ def updateRecord(self, zc, now, record): self.weight = record.weight self.priority = record.priority # self.address = None - self.updateRecord(zc, now, - zc.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN)) + self.update_record(zc, now, + zc.cache.get_by_details(self.server, _TYPE_A, _CLASS_IN)) elif record.type == _TYPE_TXT: if record.name == self.name: - self.setText(record.text) + self._set_text(record.text) def request(self, zc, timeout): """Returns true if the service could be discovered on the network, and updates this object with details discovered. """ - now = currentTimeMillis() + now = current_time_millis() delay = _LISTENER_TIME next = now + delay last = now + timeout result = False try: - zc.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) + zc.add_listener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN)) while (self.server is None or self.address is None or self.text is None): if last <= now: return False if next <= now: out = DNSOutgoing(_FLAGS_QR_QUERY) - out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, - _CLASS_IN)) - out.addAnswerAtTime(zc.cache.getByDetails(self.name, - _TYPE_SRV, _CLASS_IN), now) - out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, - _CLASS_IN)) - out.addAnswerAtTime(zc.cache.getByDetails(self.name, - _TYPE_TXT, _CLASS_IN), now) + out.add_question(DNSQuestion(self.name, _TYPE_SRV, + _CLASS_IN)) + out.add_answer_at_time(zc.cache.get_by_details(self.name, + _TYPE_SRV, _CLASS_IN), now) + out.add_question(DNSQuestion(self.name, _TYPE_TXT, + _CLASS_IN)) + out.add_answer_at_time(zc.cache.get_by_details(self.name, + _TYPE_TXT, _CLASS_IN), now) if self.server is not None: - out.addQuestion(DNSQuestion(self.server, - _TYPE_A, _CLASS_IN)) - out.addAnswerAtTime(zc.cache.getByDetails(self.server, - _TYPE_A, _CLASS_IN), now) + out.add_question(DNSQuestion(self.server, + _TYPE_A, _CLASS_IN)) + out.add_answer_at_time(zc.cache.get_by_details(self.server, + _TYPE_A, _CLASS_IN), now) zc.send(out) next = now + delay delay = delay * 2 zc.wait(min(next, last) - now) - now = currentTimeMillis() + now = current_time_millis() result = True finally: - zc.removeListener(self) + zc.remove_listener(self) return result @@ -1229,7 +1202,7 @@ def __ne__(self, other): def __repr__(self): """String representation""" result = "service[%s,%s:%s," % (self.name, - socket.inet_ntoa(self.getAddress()), self.port) + socket.inet_ntoa(self.address), self.port) if self.text is None: result += "None" else: @@ -1306,10 +1279,10 @@ def __init__(self, bindaddress=None): self.listener = Listener(self) self.reaper = Reaper(self) - def isLoopback(self): + def is_loopback(self): return self.intf.startswith("127.0.0.1") - def isLinklocal(self): + def is_linklocal(self): return self.intf.startswith("169.254.") def wait(self, timeout): @@ -1319,13 +1292,13 @@ def wait(self, timeout): self.condition.wait(timeout / 1000) self.condition.release() - def notifyAll(self): + def notify_all(self): """Notifies all waiting threads""" self.condition.acquire() - self.condition.notifyAll() + self.condition.notify_all() self.condition.release() - def getServiceInfo(self, type, name, timeout=3000): + def get_service_info(self, type, name, timeout=3000): """Returns network's service information for a particular name and type, or None if no service matches by the timeout, which defaults to 3 seconds.""" @@ -1334,55 +1307,55 @@ def getServiceInfo(self, type, name, timeout=3000): return info return None - def addServiceListener(self, type, listener): + def add_service_listener(self, type, listener): """Adds a listener for a particular service type. This object - will then have its updateRecord method called when information + will then have its update_record method called when information arrives for that type.""" - self.removeServiceListener(listener) + self.remove_service_listener(listener) self.browsers.append(ServiceBrowser(self, type, listener)) - def removeServiceListener(self, listener): + def remove_service_listener(self, listener): """Removes a listener from the set that is currently listening.""" for browser in self.browsers: if browser.listener == listener: browser.cancel() del(browser) - def registerService(self, info, ttl=_DNS_TTL): + def register_service(self, info, ttl=_DNS_TTL): """Registers service information to the network with a default TTL of 60 seconds. Zeroconf will then respond to requests for information for that service. The name of the service may be changed if needed to make it unique on the network.""" - self.checkService(info) + self.check_service(info) self.services[info.name.lower()] = info if info.type in self.servicetypes: self.servicetypes[info.type] += 1 else: self.servicetypes[info.type] = 1 - now = currentTimeMillis() - nextTime = now + now = current_time_millis() + next_time = now i = 0 while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() + if now < next_time: + self.wait(next_time - now) + now = current_time_millis() continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, - _CLASS_IN, ttl, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, ttl, info.priority, info.weight, info.port, - info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, - ttl, info.text), 0) + out.add_answer_at_time(DNSPointer(info.type, _TYPE_PTR, + _CLASS_IN, ttl, info.name), 0) + out.add_answer_at_time(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, ttl, info.priority, info.weight, info.port, + info.server), 0) + out.add_answer_at_time(DNSText(info.name, _TYPE_TXT, _CLASS_IN, + ttl, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, - _CLASS_IN, ttl, info.address), 0) + out.add_answer_at_time(DNSAddress(info.server, _TYPE_A, + _CLASS_IN, ttl, info.address), 0) self.send(out) i += 1 - nextTime += _REGISTER_TIME + next_time += _REGISTER_TIME - def unregisterService(self, info): + def unregister_service(self, info): """Unregister a service.""" try: del(self.services[info.name.lower()]) @@ -1392,134 +1365,134 @@ def unregisterService(self, info): del self.servicetypes[info.type] except Exception as e: # TODO stop catching all Exceptions log.exception('Unknown error, possibly benign: %r', e) - now = currentTimeMillis() - nextTime = now + now = current_time_millis() + next_time = now i = 0 while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() + if now < next_time: + self.wait(next_time - now) + now = current_time_millis() continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, - _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, 0, info.priority, info.weight, info.port, - info.name), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, - 0, info.text), 0) + out.add_answer_at_time(DNSPointer(info.type, _TYPE_PTR, + _CLASS_IN, 0, info.name), 0) + out.add_answer_at_time(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, 0, info.priority, info.weight, info.port, + info.name), 0) + out.add_answer_at_time(DNSText(info.name, _TYPE_TXT, _CLASS_IN, + 0, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, - _CLASS_IN, 0, info.address), 0) + out.add_answer_at_time(DNSAddress(info.server, _TYPE_A, + _CLASS_IN, 0, info.address), 0) self.send(out) i += 1 - nextTime += _UNREGISTER_TIME + next_time += _UNREGISTER_TIME - def unregisterAllServices(self): + def unregister_all_services(self): """Unregister all registered services.""" if len(self.services) > 0: - now = currentTimeMillis() - nextTime = now + now = current_time_millis() + next_time = now i = 0 while i < 3: - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() + if now < next_time: + self.wait(next_time - now) + now = current_time_millis() continue out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) for info in self.services.values(): - out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, - _CLASS_IN, 0, info.name), 0) - out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, - _CLASS_IN, 0, info.priority, info.weight, - info.port, info.server), 0) - out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, - _CLASS_IN, 0, info.text), 0) + out.add_answer_at_time(DNSPointer(info.type, _TYPE_PTR, + _CLASS_IN, 0, info.name), 0) + out.add_answer_at_time(DNSService(info.name, _TYPE_SRV, + _CLASS_IN, 0, info.priority, info.weight, + info.port, info.server), 0) + out.add_answer_at_time(DNSText(info.name, _TYPE_TXT, + _CLASS_IN, 0, info.text), 0) if info.address: - out.addAnswerAtTime(DNSAddress(info.server, - _TYPE_A, _CLASS_IN, 0, info.address), 0) + out.add_answer_at_time(DNSAddress(info.server, + _TYPE_A, _CLASS_IN, 0, info.address), 0) self.send(out) i += 1 - nextTime += _UNREGISTER_TIME + next_time += _UNREGISTER_TIME - def checkService(self, info): + def check_service(self, info): """Checks the network for a unique service name, modifying the ServiceInfo passed in if it is not unique.""" - now = currentTimeMillis() - nextTime = now + now = current_time_millis() + next_time = now i = 0 while i < 3: - for record in self.cache.entriesWithName(info.type): + for record in self.cache.entries_with_name(info.type): if (record.type == _TYPE_PTR and - not record.isExpired(now) and + not record.is_expired(now) and record.alias == info.name): if info.name.find('.') < 0: info.name = '%s.[%s:%s].%s' % (info.name, info.address, info.port, info.type) - self.checkService(info) + self.check_service(info) return raise NonUniqueNameException - if now < nextTime: - self.wait(nextTime - now) - now = currentTimeMillis() + if now < next_time: + self.wait(next_time - now) + now = current_time_millis() continue out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) self.debug = out - out.addQuestion(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) - out.addAuthorativeAnswer(DNSPointer(info.type, _TYPE_PTR, - _CLASS_IN, _DNS_TTL, info.name)) + out.add_question(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) + out.add_authorative_answer(DNSPointer(info.type, _TYPE_PTR, + _CLASS_IN, _DNS_TTL, info.name)) self.send(out) i += 1 - nextTime += _CHECK_TIME + next_time += _CHECK_TIME - def addListener(self, listener, question): + def add_listener(self, listener, question): """Adds a listener for a given question. The listener will have - its updateRecord method called when information is available to + its update_record method called when information is available to answer the question.""" - now = currentTimeMillis() + now = current_time_millis() self.listeners.append(listener) if question is not None: - for record in self.cache.entriesWithName(question.name): - if question.answeredBy(record) and not record.isExpired(now): - listener.updateRecord(self, now, record) - self.notifyAll() + for record in self.cache.entries_with_name(question.name): + if question.answered_by(record) and not record.is_expired(now): + listener.update_record(self, now, record) + self.notify_all() - def removeListener(self, listener): + def remove_listener(self, listener): """Removes a listener.""" try: self.listeners.remove(listener) - self.notifyAll() + self.notify_all() except Exception as e: # TODO stop catching all Exceptions log.exception('Unknown error, possibly benign: %r', e) - def updateRecord(self, now, rec): + def update_record(self, now, rec): """Used to notify listeners of new information that has updated a record.""" for listener in self.listeners: - listener.updateRecord(self, now, rec) - self.notifyAll() + listener.update_record(self, now, rec) + self.notify_all() - def handleResponse(self, msg): + def handle_response(self, msg): """Deal with incoming response packets. All answers are held in the cache, and listeners are notified.""" - now = currentTimeMillis() + now = current_time_millis() for record in msg.answers: - expired = record.isExpired(now) + expired = record.is_expired(now) if record in self.cache.entries(): if expired: self.cache.remove(record) else: entry = self.cache.get(record) if entry is not None: - entry.resetTTL(record) + entry.reset_ttl(record) record = entry else: self.cache.add(record) - self.updateRecord(now, record) + self.update_record(now, record) - def handleQuery(self, msg, addr, port): + def handle_query(self, msg, addr, port): """Deal with incoming query packets. Provides a response if possible.""" out = None @@ -1529,7 +1502,7 @@ def handleQuery(self, msg, addr, port): if port != _MDNS_PORT: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, False) for question in msg.questions: - out.addQuestion(question) + out.add_question(question) for question in msg.questions: if question.type == _TYPE_PTR: @@ -1537,16 +1510,16 @@ def handleQuery(self, msg, addr, port): for stype in self.servicetypes.keys(): if out is None: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswer(msg, - DNSPointer("_services._dns-sd._udp.local.", - _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) + out.add_answer(msg, + DNSPointer("_services._dns-sd._udp.local.", + _TYPE_PTR, _CLASS_IN, _DNS_TTL, stype)) for service in self.services.values(): if question.name == service.type: if out is None: out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) - out.addAnswer(msg, - DNSPointer(service.type, _TYPE_PTR, - _CLASS_IN, _DNS_TTL, service.name)) + out.add_answer(msg, + DNSPointer(service.type, _TYPE_PTR, + _CLASS_IN, _DNS_TTL, service.name)) else: try: if out is None: @@ -1556,27 +1529,27 @@ def handleQuery(self, msg, addr, port): if question.type in (_TYPE_A, _TYPE_ANY): for service in self.services.values(): if service.server == question.name.lower(): - out.addAnswer(msg, DNSAddress(question.name, - _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.address)) + out.add_answer(msg, DNSAddress(question.name, + _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.address)) service = self.services.get(question.name.lower(), None) if not service: continue if question.type in (_TYPE_SRV, _TYPE_ANY): - out.addAnswer(msg, DNSService(question.name, - _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.priority, service.weight, - service.port, service.server)) + out.add_answer(msg, DNSService(question.name, + _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.priority, service.weight, + service.port, service.server)) if question.type in (_TYPE_TXT, _TYPE_ANY): - out.addAnswer(msg, DNSText(question.name, - _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.text)) + out.add_answer(msg, DNSText(question.name, + _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.text)) if question.type == _TYPE_SRV: - out.addAdditionalAnswer(DNSAddress(service.server, - _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, - _DNS_TTL, service.address)) + out.add_additional_answer(DNSAddress(service.server, + _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, + _DNS_TTL, service.address)) except Exception as e: # TODO stop catching all Exceptions log.exception('Unknown error, possibly benign: %r', e) @@ -1603,9 +1576,9 @@ def close(self): global _GLOBAL_DONE if not _GLOBAL_DONE: _GLOBAL_DONE = True - self.notifyAll() + self.notify_all() self.engine.notify() - self.unregisterAllServices() + self.unregister_all_services() self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + @@ -1625,17 +1598,17 @@ def close(self): "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) print(" Registering service...") - r.registerService(info) + r.register_service(info) print(" Registration done.") print("2. Testing query of service information...") print(" Getting ZOE service: %s" % ( - r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local."))) + r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local."))) print(" Query done.") print("3. Testing query of own service...") print(" Getting self: %s" % ( - r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.")),) + r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")),) print(" Query done.") print("4. Testing unregister of service information...") - r.unregisterService(info) + r.unregister_service(info) print(" Unregister done.") r.close() From 92e47132dc761a9a722caec261ae53de1785838f Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 2 Nov 2014 21:15:41 +0000 Subject: [PATCH 097/163] Migrate more camel case names to snake case --- zeroconf.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 74f27378..8f2e56c0 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -466,10 +466,10 @@ def __init__(self, data): self.data = data self.questions = [] self.answers = [] - self.numQuestions = 0 - self.numAnswers = 0 - self.numAuthorities = 0 - self.numAdditionals = 0 + self.num_questions = 0 + self.num_answers = 0 + self.num_authorities = 0 + self.num_additionals = 0 self.read_header() self.read_questions() @@ -483,12 +483,12 @@ def unpack(self, format): def read_header(self): """Reads header portion of packet""" - (self.id, self.flags, self.numQuestions, self.numAnswers, - self.numAuthorities, self.numAdditionals) = self.unpack(b'!6H') + (self.id, self.flags, self.num_questions, self.num_answers, + self.num_quthorities, self.num_additionals) = self.unpack(b'!6H') def read_questions(self): """Reads questions section of packet""" - for i in xrange(self.numQuestions): + for i in xrange(self.num_questions): name = self.read_name() type, clazz = self.unpack(b'!HH') @@ -518,7 +518,7 @@ def read_unsigned_short(self): def read_others(self): """Reads the answers, authorities and additionals section of the packet""" - n = self.numAnswers + self.numAuthorities + self.numAdditionals + n = self.num_answers + self.num_authorities + self.num_additionals for i in xrange(n): domain = self.read_name() type, clazz, ttl, length = self.unpack(b'!HHiH') @@ -532,11 +532,11 @@ def read_others(self): rec = DNSText(domain, type, clazz, ttl, self.read_string(length)) elif type == _TYPE_SRV: rec = DNSService(domain, type, clazz, ttl, - self.read_unsigned_short(), self.readUnsignedShort(), + self.read_unsigned_short(), self.read_unsigned_short(), self.read_unsigned_short(), self.read_name()) elif type == _TYPE_HINFO: rec = DNSHinfo(domain, type, clazz, ttl, - self.read_character_string(), self.readCharacterString()) + self.read_character_string(), self.read_character_string()) elif type == _TYPE_AAAA: rec = DNSAddress(domain, type, clazz, ttl, self.read_string(16)) else: From 4a67e124cd8f8c4d19f8c6c4a455d075bb948362 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 2 Nov 2014 21:16:25 +0000 Subject: [PATCH 098/163] Migrate from clazz to class_ --- zeroconf.py | 72 ++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 8f2e56c0..77a9df8d 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -208,27 +208,27 @@ class DNSEntry(object): """A DNS entry""" - def __init__(self, name, type, clazz): + def __init__(self, name, type, class_): self.key = name.lower() self.name = name self.type = type - self.clazz = clazz & _CLASS_MASK - self.unique = (clazz & _CLASS_UNIQUE) != 0 + self.class_ = class_ & _CLASS_MASK + self.unique = (class_ & _CLASS_UNIQUE) != 0 def __eq__(self, other): """Equality test on name, type, and class""" return (isinstance(other, DNSEntry) and self.name == other.name and self.type == other.type and - self.clazz == other.clazz) + self.class_ == other.class_) def __ne__(self, other): """Non-equality test""" return not self.__eq__(other) - def get_clazz(self, clazz): + def get_class_(self, class_): """Class accessor""" - return _CLASSES.get(clazz, "?(%s)" % clazz) + return _CLASSES.get(class_, "?(%s)" % class_) def get_type(self, t): """Type accessor""" @@ -237,7 +237,7 @@ def get_type(self, t): def to_string(self, hdr, other): """String representation with additional information""" result = "%s[%s,%s" % (hdr, self.get_type(self.type), - self.get_clazz(self.clazz)) + self.get_class_(self.class_)) if self.unique: result += "-unique," else: @@ -254,14 +254,14 @@ class DNSQuestion(DNSEntry): """A DNS question entry""" - def __init__(self, name, type, clazz): + def __init__(self, name, type, class_): # if not name.endswith(".local."): # raise NonLocalNameException - DNSEntry.__init__(self, name, type, clazz) + DNSEntry.__init__(self, name, type, class_) def answered_by(self, rec): """Returns true if the question is answered by the record""" - return (self.clazz == rec.clazz and + return (self.class_ == rec.class_ and (self.type == rec.type or self.type == _TYPE_ANY) and self.name == rec.name) @@ -274,8 +274,8 @@ class DNSRecord(DNSEntry): """A DNS record - like a DNS entry, but has a TTL""" - def __init__(self, name, type, clazz, ttl): - DNSEntry.__init__(self, name, type, clazz) + def __init__(self, name, type, class_, ttl): + DNSEntry.__init__(self, name, type, class_) self.ttl = ttl self.created = current_time_millis() @@ -334,8 +334,8 @@ class DNSAddress(DNSRecord): """A DNS address record""" - def __init__(self, name, type, clazz, ttl, address): - DNSRecord.__init__(self, name, type, clazz, ttl) + def __init__(self, name, type, class_, ttl, address): + DNSRecord.__init__(self, name, type, class_, ttl) self.address = address def write(self, out): @@ -359,8 +359,8 @@ class DNSHinfo(DNSRecord): """A DNS host information record""" - def __init__(self, name, type, clazz, ttl, cpu, os): - DNSRecord.__init__(self, name, type, clazz, ttl) + def __init__(self, name, type, class_, ttl, cpu, os): + DNSRecord.__init__(self, name, type, class_, ttl) self.cpu = cpu self.os = os @@ -383,8 +383,8 @@ class DNSPointer(DNSRecord): """A DNS pointer record""" - def __init__(self, name, type, clazz, ttl, alias): - DNSRecord.__init__(self, name, type, clazz, ttl) + def __init__(self, name, type, class_, ttl, alias): + DNSRecord.__init__(self, name, type, class_, ttl) self.alias = alias def write(self, out): @@ -404,9 +404,9 @@ class DNSText(DNSRecord): """A DNS text record""" - def __init__(self, name, type_, clazz, ttl, text): + def __init__(self, name, type_, class_, ttl, text): assert isinstance(text, (bytes, type(None))) - DNSRecord.__init__(self, name, type_, clazz, ttl) + DNSRecord.__init__(self, name, type_, class_, ttl) self.text = text def write(self, out): @@ -429,8 +429,8 @@ class DNSService(DNSRecord): """A DNS service record""" - def __init__(self, name, type, clazz, ttl, priority, weight, port, server): - DNSRecord.__init__(self, name, type, clazz, ttl) + def __init__(self, name, type, class_, ttl, priority, weight, port, server): + DNSRecord.__init__(self, name, type, class_, ttl) self.priority = priority self.weight = weight self.port = port @@ -490,9 +490,9 @@ def read_questions(self): """Reads questions section of packet""" for i in xrange(self.num_questions): name = self.read_name() - type, clazz = self.unpack(b'!HH') + type, class_ = self.unpack(b'!HH') - question = DNSQuestion(name, type, clazz) + question = DNSQuestion(name, type, class_) self.questions.append(question) def read_int(self): @@ -521,24 +521,24 @@ def read_others(self): n = self.num_answers + self.num_authorities + self.num_additionals for i in xrange(n): domain = self.read_name() - type, clazz, ttl, length = self.unpack(b'!HHiH') + type, class_, ttl, length = self.unpack(b'!HHiH') rec = None if type == _TYPE_A: - rec = DNSAddress(domain, type, clazz, ttl, self.read_string(4)) + rec = DNSAddress(domain, type, class_, ttl, self.read_string(4)) elif type == _TYPE_CNAME or type == _TYPE_PTR: - rec = DNSPointer(domain, type, clazz, ttl, self.read_name()) + rec = DNSPointer(domain, type, class_, ttl, self.read_name()) elif type == _TYPE_TXT: - rec = DNSText(domain, type, clazz, ttl, self.read_string(length)) + rec = DNSText(domain, type, class_, ttl, self.read_string(length)) elif type == _TYPE_SRV: - rec = DNSService(domain, type, clazz, ttl, + rec = DNSService(domain, type, class_, ttl, self.read_unsigned_short(), self.read_unsigned_short(), self.read_unsigned_short(), self.read_name()) elif type == _TYPE_HINFO: - rec = DNSHinfo(domain, type, clazz, ttl, + rec = DNSHinfo(domain, type, class_, ttl, self.read_character_string(), self.read_character_string()) elif type == _TYPE_AAAA: - rec = DNSAddress(domain, type, clazz, ttl, self.read_string(16)) + rec = DNSAddress(domain, type, class_, ttl, self.read_string(16)) else: # Try to ignore types we don't know about # Skip the payload for the resource record so the next @@ -702,7 +702,7 @@ def write_question(self, question): """Writes a question to the packet""" self.write_name(question.name) self.write_short(question.type) - self.write_short(question.clazz) + self.write_short(question.class_) def write_record(self, record, now): """Writes a record (answer, authoritative answer, additional) to @@ -710,9 +710,9 @@ def write_record(self, record, now): self.write_name(record.name) self.write_short(record.type) if record.unique and self.multicast: - self.write_short(record.clazz | _CLASS_UNIQUE) + self.write_short(record.class_ | _CLASS_UNIQUE) else: - self.write_short(record.clazz) + self.write_short(record.class_) if now == 0: self.write_int(record.ttl) else: @@ -783,10 +783,10 @@ def get(self, entry): except (KeyError, ValueError): return None - def get_by_details(self, name, type, clazz): + def get_by_details(self, name, type, class_): """Gets an entry by details. Will return None if there is no matching entry.""" - entry = DNSEntry(name, type, clazz) + entry = DNSEntry(name, type, class_) return self.get(entry) def entries_with_name(self, name): From 05c3c02044d2b4bff946e00803d0ddb2619f0927 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 2 Nov 2014 21:19:34 +0000 Subject: [PATCH 099/163] Tune logging --- zeroconf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/zeroconf.py b/zeroconf.py index 77a9df8d..779aef75 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1589,6 +1589,7 @@ def close(self): # query (for Zoe), and service unregistration. if __name__ == '__main__': + logging.basicConfig(level=logging.DEBUG) log.setLevel(logging.DEBUG) print("Multicast DNS Service Discovery for Python, version %s" % __version__) r = Zeroconf() From 4e54b6738a490dcc7d2f9e7e1040c5da53727155 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 2 Dec 2014 23:51:40 +0000 Subject: [PATCH 100/163] Release as 0.16.0 --- README.rst | 6 ++++++ zeroconf.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 34a851a3..bfafd184 100644 --- a/README.rst +++ b/README.rst @@ -92,6 +92,12 @@ See examples directory for more. Changelog ========= +0.16.0 +------ + +* Set up Python logging and started using it +* Clean up code style (includes migrating from camel case to snak case) + 0.15.1 ------ diff --git a/zeroconf.py b/zeroconf.py index 779aef75..81484de9 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -24,7 +24,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.15.1' +__version__ = '0.16.0' __license__ = 'LGPL' import logging From 8f1860956ee9c86b7ba095fc1293919933e1c0ad Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 2 Dec 2014 23:55:37 +0000 Subject: [PATCH 101/163] Update README.rst --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index bfafd184..bc69688b 100644 --- a/README.rst +++ b/README.rst @@ -96,7 +96,7 @@ Changelog ------ * Set up Python logging and started using it -* Clean up code style (includes migrating from camel case to snak case) +* Cleaned up code style (includes migrating from camel case to snak case) 0.15.1 ------ From 4b8f68b39230bb9cc3c202395b58cc822b8fe862 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sat, 6 Dec 2014 16:15:01 +0000 Subject: [PATCH 102/163] Refactor exception handling here --- zeroconf.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 81484de9..41b9c2e7 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1239,17 +1239,19 @@ def __init__(self, bindaddress=None): log.debug('Bind address is %r' % (self.intf,)) self.group = ('', _MDNS_PORT) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + # SO_REUSEADDR should be equivalent to SO_REUSEPORT for + # multicast UDP sockets (p 731, "TCP/IP Illustrated, + # Volume 2"), but some BSD-derived systems require + # SO_REUSEPORT to be specified explicity. Also, not all + # versions of Python have SO_REUSEPORT available. try: - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) - except Exception as e: # TODO stop catching all Exceptions - # SO_REUSEADDR should be equivalent to SO_REUSEPORT for - # multicast UDP sockets (p 731, "TCP/IP Illustrated, - # Volume 2"), but some BSD-derived systems require - # SO_REUSEPORT to be specified explicity. Also, not all - # versions of Python have SO_REUSEPORT available. - # - log.exception('Unknown error, possibly benign: %r', e) + reuseport = socket.SO_REUSEPORT + except AttributeError: + pass + else: + self.socket.setsockopt(socket.SOL_SOCKET, reuseport, 1) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) From 80e8e1008bc28c8ab9ca966b89109146112d0edd Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sat, 6 Dec 2014 16:15:28 +0000 Subject: [PATCH 103/163] Remove some unused methods --- zeroconf.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 41b9c2e7..88ed7de5 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1281,12 +1281,6 @@ def __init__(self, bindaddress=None): self.listener = Listener(self) self.reaper = Reaper(self) - def is_loopback(self): - return self.intf.startswith("127.0.0.1") - - def is_linklocal(self): - return self.intf.startswith("169.254.") - def wait(self, timeout): """Calling thread waits for a given number of milliseconds or until notified.""" From 903cb78d3ff7bc8762bf23910562b8f5042c2f85 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 7 Dec 2014 23:26:21 +0000 Subject: [PATCH 104/163] Stop using Zeroconf.group attribute --- zeroconf.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 88ed7de5..14f7e567 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1237,7 +1237,6 @@ def __init__(self, bindaddress=None): else: self.intf = bindaddress log.debug('Bind address is %r' % (self.intf,)) - self.group = ('', _MDNS_PORT) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -1256,8 +1255,7 @@ def __init__(self, bindaddress=None): self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) try: - log.debug('Binding to %r', self.group) - self.socket.bind(self.group) + self.socket.bind(('', _MDNS_PORT)) except Exception as e: # TODO stop catching all Exceptions # Some versions of linux raise an exception even though # the SO_REUSE* options have been set, so ignore it From 88f5a5193ba2ab0eefc99481ccc6a1b911d8dbea Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 8 Dec 2014 16:14:44 +0000 Subject: [PATCH 105/163] Remove dead code --- zeroconf.py | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 14f7e567..621ec900 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1221,22 +1221,11 @@ class Zeroconf(object): Supports registration, unregistration, queries and browsing. """ - def __init__(self, bindaddress=None): + def __init__(self): """Creates an instance of the Zeroconf class, establishing multicast communications, listening and reaping threads.""" global _GLOBAL_DONE _GLOBAL_DONE = False - if bindaddress is None: - try: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(('4.2.2.1', 123)) - self.intf = s.getsockname()[0] - except Exception as e: # TODO stop catching all Exceptions - log.exception('Unknown error, possibly benign: %r', e) - self.intf = socket.gethostbyname(socket.gethostname()) - else: - self.intf = bindaddress - log.debug('Bind address is %r' % (self.intf,)) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -1261,8 +1250,6 @@ def __init__(self, bindaddress=None): # the SO_REUSE* options have been set, so ignore it # log.exception('Unknown error, possibly benign: %r', e) - # self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, - # socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0')) self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) From f6425d1d727edfa124264bcabeffd77397809965 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 8 Dec 2014 16:22:23 +0000 Subject: [PATCH 106/163] Stop dropping multicast group membership It'll be taken care of by socket being closed --- zeroconf.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 621ec900..fe5606d6 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1560,10 +1560,6 @@ def close(self): self.notify_all() self.engine.notify() self.unregister_all_services() - self.socket.setsockopt(socket.IPPROTO_IP, - socket.IP_DROP_MEMBERSHIP, - socket.inet_aton(_MDNS_ADDR) + - socket.inet_aton('0.0.0.0')) self.socket.close() # Test a few module features, including service registration, service From d8c1ec8ee13191e8ec4412770994f0676ace442c Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Mon, 8 Dec 2014 20:24:53 +0000 Subject: [PATCH 107/163] Drop "zero dependencies" feature --- README.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/README.rst b/README.rst index bc69688b..d4345f40 100644 --- a/README.rst +++ b/README.rst @@ -32,7 +32,6 @@ Compatible with: Compared to some other Zeroconf/Bonjour/Avahi Python packages, python-zeroconf: -* has zero external dependencies * isn't tied to Bonjour or Avahi * doesn't use D-Bus * doesn't force you to use particular event loop or Twisted From 1c2205d5c9b364a825d51acd03add4de91cb645a Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 10 Dec 2014 14:29:31 +0000 Subject: [PATCH 108/163] Refactor version detection in the setup script This doesn't depend on zeroconf module being importable when setup is ran --- setup.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index a22657b4..2cfba019 100755 --- a/setup.py +++ b/setup.py @@ -5,15 +5,19 @@ from setuptools import setup -from zeroconf import __version__ - PROJECT_ROOT = abspath(dirname(__file__)) with open(join(PROJECT_ROOT, 'README.rst')) as f: readme = f.read() +version = ( + [l for l in open(join(PROJECT_ROOT, 'zeroconf.py')) if '__version__' in l][0] + .split('=')[-1] + .strip().strip('\'"') +) + setup( name='zeroconf', - version=__version__, + version=version, description='Pure Python Multicast DNS Service Discovery Library ' '(Bonjour/Avahi compatible)', long_description=readme, From 54ed4b79bb8de9523b5a5b74a79b01c8aa2291a7 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 10 Dec 2014 15:03:43 +0000 Subject: [PATCH 109/163] Use six for Python 2/3 compatibility --- README.rst | 5 +++-- examples/browser.py | 6 ++++-- examples/registration.py | 6 ++++-- setup.py | 3 +++ zeroconf.py | 46 +++++++++++----------------------------- 5 files changed, 26 insertions(+), 40 deletions(-) diff --git a/README.rst b/README.rst index d4345f40..75e9dd12 100644 --- a/README.rst +++ b/README.rst @@ -65,7 +65,8 @@ Here's an example: .. code-block:: python - from zeroconf import raw_input, ServiceBrowser, Zeroconf + from six.moves import input + from zeroconf import ServiceBrowser, Zeroconf class MyListener(object): @@ -82,7 +83,7 @@ Here's an example: listener = MyListener() browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) try: - raw_input("Press enter to exit...\n\n") + input("Press enter to exit...\n\n") finally: zeroconf.close() diff --git a/examples/browser.py b/examples/browser.py index 1b3b7179..9ac6d849 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -5,7 +5,9 @@ import socket -from zeroconf import raw_input, ServiceBrowser, Zeroconf +from six.moves import input + +from zeroconf import ServiceBrowser, Zeroconf class MyListener(object): @@ -38,6 +40,6 @@ def add_service(self, zeroconf, type, name): listener = MyListener() browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) try: - raw_input("Waiting (press Enter to exit)...\n\n") + input("Waiting (press Enter to exit)...\n\n") finally: zeroconf.close() diff --git a/examples/registration.py b/examples/registration.py index 18e440f3..50d53c1b 100755 --- a/examples/registration.py +++ b/examples/registration.py @@ -4,7 +4,9 @@ import socket -from zeroconf import raw_input, ServiceInfo, Zeroconf +from six.moves import input + +from zeroconf import ServiceInfo, Zeroconf desc = {'path': '/~paulsm/'} @@ -17,7 +19,7 @@ print("Registration of a service...") zeroconf.register_service(info) try: - raw_input("Waiting (press Enter to exit)...") + input("Waiting (press Enter to exit)...") finally: print("Unregistering...") zeroconf.unregister_service(info) diff --git a/setup.py b/setup.py index 2cfba019..229a653d 100755 --- a/setup.py +++ b/setup.py @@ -50,4 +50,7 @@ 'Bonjour', 'Avahi', 'Zeroconf', 'Multicast DNS', 'Service Discovery', 'mDNS', ], + install_requires=[ + 'six', + ], ) diff --git a/zeroconf.py b/zeroconf.py index fe5606d6..f3c5cc37 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -35,6 +35,9 @@ import time from functools import reduce +from six import byte2int, int2byte, text_type +from six.moves import xrange + try: NullHandler = logging.NullHandler @@ -54,31 +57,6 @@ def emit(self, record): if log.level == logging.NOTSET: log.setLevel(logging.WARN) -try: - xrange = xrange -except NameError: - xrange = range - -try: - unicode -except NameError: - unicode = str - -if isinstance(chr(8), unicode): - byte_chr = lambda num: bytes([num]) -else: - byte_chr = chr - -if isinstance(bytes([8])[0], int): - byte_ord = lambda x: x -else: - byte_ord = ord - -try: - raw_input = raw_input -except NameError: - raw_input = input - # hook for threads _GLOBAL_DONE = False @@ -501,7 +479,7 @@ def read_int(self): def read_character_string(self): """Reads a character string from the packet""" - length = byte_ord(self.data[self.offset]) + length = byte2int(self.data[self.offset]) self.offset += 1 return self.read_string(length) @@ -558,7 +536,7 @@ def is_response(self): def read_utf(self, offset, length): """Reads a UTF-8 string of a given length from the packet""" - return unicode(self.data[offset:offset + length], 'utf-8', 'replace') + return text_type(self.data[offset:offset + length], 'utf-8', 'replace') def read_name(self): """Reads a domain name from the packet""" @@ -568,7 +546,7 @@ def read_name(self): first = off while True: - length = byte_ord(self.data[off]) + length = byte2int(self.data[off]) off += 1 if length == 0: break @@ -579,7 +557,7 @@ def read_name(self): elif t == 0xC0: if next < 0: next = off + 1 - off = ((length & 0x3F) << 8) | byte_ord(self.data[off]) + off = ((length & 0x3F) << 8) | byte2int(self.data[off]) if off >= first: # TODO raise more specific exception raise Exception("Bad domain name (circular) at %s" % (off,)) @@ -643,7 +621,7 @@ def pack(self, format, value): def write_byte(self, value): """Writes a single byte to the packet""" - self.pack(b'!c', byte_chr(value)) + self.pack(b'!c', int2byte(value)) def insert_short(self, index, value): """Inserts an unsigned short in a certain position in the packet""" @@ -1064,12 +1042,12 @@ def _set_properties(self, properties): result = b'' for key in properties: value = properties[key] - if isinstance(key, unicode): + if isinstance(key, text_type): key = key.encode('utf-8') if value is None: suffix = b'' - elif isinstance(value, unicode): + elif isinstance(value, text_type): suffix = value.encode('utf-8') elif isinstance(value, int): if value: @@ -1080,7 +1058,7 @@ def _set_properties(self, properties): suffix = b'' list.append(b'='.join((key, suffix))) for item in list: - result = b''.join((result, byte_chr(len(item)), item)) + result = b''.join((result, int2byte(len(item)), item)) self.text = result else: self.text = properties @@ -1094,7 +1072,7 @@ def _set_text(self, text): index = 0 strs = [] while index < end: - length = byte_ord(text[index]) + length = byte2int(text[index]) index += 1 strs.append(text[index:index + length]) index += length From f0c39797869175cf88d76c75d39835abb2052f88 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 10 Dec 2014 15:03:43 +0000 Subject: [PATCH 110/163] Use six for Python 2/3 compatibility --- requirements-dev.txt | 1 + test_zeroconf.py | 10 +++++----- zeroconf.py | 10 +++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 4bc24d07..362737ff 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,3 +7,4 @@ flake8-import-order>=0.4.0 mock nose pep8-naming +six diff --git a/test_zeroconf.py b/test_zeroconf.py index 57c7bbd9..02a3ca6c 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -8,14 +8,14 @@ from threading import Event from mock import Mock +from six import indexbytes +from six.moves import xrange import zeroconf as r from zeroconf import ( - byte_ord, Listener, ServiceBrowser, ServiceInfo, - xrange, Zeroconf, ) @@ -56,19 +56,19 @@ def test_transaction_id(self): """ID must be zero in a DNS-SD packet""" generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() - id = byte_ord(bytes[0]) << 8 | byte_ord(bytes[1]) + id = indexbytes(bytes, 0) << 8 | indexbytes(bytes, 1) self.assertEqual(id, 0) def test_query_header_bits(self): generated = r.DNSOutgoing(r._FLAGS_QR_QUERY) bytes = generated.packet() - flags = byte_ord(bytes[2]) << 8 | byte_ord(bytes[3]) + flags = indexbytes(bytes, 2) << 8 | indexbytes(bytes, 3) self.assertEqual(flags, 0x0) def test_response_header_bits(self): generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE) bytes = generated.packet() - flags = byte_ord(bytes[2]) << 8 | byte_ord(bytes[3]) + flags = indexbytes(bytes, 2) << 8 | indexbytes(bytes, 3) self.assertEqual(flags, 0x8000) def test_numbers(self): diff --git a/zeroconf.py b/zeroconf.py index f3c5cc37..a9ffe53b 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -35,7 +35,7 @@ import time from functools import reduce -from six import byte2int, int2byte, text_type +from six import indexbytes, int2byte, text_type from six.moves import xrange @@ -479,7 +479,7 @@ def read_int(self): def read_character_string(self): """Reads a character string from the packet""" - length = byte2int(self.data[self.offset]) + length = indexbytes(self.data, self.offset) self.offset += 1 return self.read_string(length) @@ -546,7 +546,7 @@ def read_name(self): first = off while True: - length = byte2int(self.data[off]) + length = indexbytes(self.data, off) off += 1 if length == 0: break @@ -557,7 +557,7 @@ def read_name(self): elif t == 0xC0: if next < 0: next = off + 1 - off = ((length & 0x3F) << 8) | byte2int(self.data[off]) + off = ((length & 0x3F) << 8) | indexbytes(self.data, off) if off >= first: # TODO raise more specific exception raise Exception("Bad domain name (circular) at %s" % (off,)) @@ -1072,7 +1072,7 @@ def _set_text(self, text): index = 0 strs = [] while index < end: - length = byte2int(text[index]) + length = indexbytes(text, index) index += 1 strs.append(text[index:index + length]) index += length From 8d32fa4b12e1b52d72a7ba9588437c4c787e0ffd Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 12 Dec 2014 03:22:09 +0000 Subject: [PATCH 111/163] Refactor Condition usage to use context manager interface --- zeroconf.py | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index a9ffe53b..9014ad05 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -811,9 +811,8 @@ def run(self): # No sockets to manage, but we wait for the timeout # or addition of a socket # - self.condition.acquire() - self.condition.wait(self.timeout) - self.condition.release() + with self.condition: + self.condition.wait(self.timeout) else: try: rr, wr, er = select.select(rs, [], [], self.timeout) @@ -827,27 +826,23 @@ def run(self): def get_readers(self): result = [] - self.condition.acquire() - result = self.readers.keys() - self.condition.release() + with self.condition: + result = self.readers.keys() return result def add_reader(self, reader, socket): - self.condition.acquire() - self.readers[socket] = reader - self.condition.notify() - self.condition.release() + with self.condition: + self.readers[socket] = reader + self.condition.notify() def del_reader(self, socket): - self.condition.acquire() - del(self.readers[socket]) - self.condition.notify() - self.condition.release() + with self.condition: + del(self.readers[socket]) + self.condition.notify() def notify(self): - self.condition.acquire() - self.condition.notify() - self.condition.release() + with self.condition: + self.condition.notify() class Listener(object): @@ -1247,15 +1242,13 @@ def __init__(self): def wait(self, timeout): """Calling thread waits for a given number of milliseconds or until notified.""" - self.condition.acquire() - self.condition.wait(timeout / 1000) - self.condition.release() + with self.condition: + self.condition.wait(timeout / 1000) def notify_all(self): """Notifies all waiting threads""" - self.condition.acquire() - self.condition.notify_all() - self.condition.release() + with self.condition: + self.condition.notify_all() def get_service_info(self, type, name, timeout=3000): """Returns network's service information for a particular From 78449ef1e07dc68b63bb68038cb66f22e083fdfe Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 12 Dec 2014 09:41:43 +0000 Subject: [PATCH 112/163] Make Zeroconf.socket private --- test_zeroconf.py | 2 +- zeroconf.py | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/test_zeroconf.py b/test_zeroconf.py index 02a3ca6c..5a1f4ea1 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -183,4 +183,4 @@ def test_listener_handles_closed_socket_situation_gracefully(): zeroconf.socket.recvfrom.side_effect = error listener = Listener(zeroconf) - listener.handle_read() + listener.handle_read(zeroconf.socket) diff --git a/zeroconf.py b/zeroconf.py index 9014ad05..8bb452e9 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -818,7 +818,7 @@ def run(self): rr, wr, er = select.select(rs, [], [], self.timeout) for socket_ in rr: try: - self.readers[socket_].handle_read() + self.readers[socket_].handle_read(socket_) except Exception as e: # TODO stop catching all Exceptions log.exception('Unknown error, possibly benign: %r', e) except Exception as e: # TODO stop catching all Exceptions @@ -856,11 +856,10 @@ class Listener(object): def __init__(self, zc): self.zc = zc - self.zc.engine.add_reader(self, self.zc.socket) - def handle_read(self): + def handle_read(self, socket_): try: - data, (addr, port) = self.zc.socket.recvfrom(_MAX_MSG_ABSOLUTE) + data, (addr, port) = socket_.recvfrom(_MAX_MSG_ABSOLUTE) except socket.error as e: # If the socket was closed by another thread -- which happens # regularly on shutdown -- an EBADF exception is thrown here. @@ -1199,8 +1198,8 @@ def __init__(self): multicast communications, listening and reaping threads.""" global _GLOBAL_DONE _GLOBAL_DONE = False - self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # SO_REUSEADDR should be equivalent to SO_REUSEPORT for # multicast UDP sockets (p 731, "TCP/IP Illustrated, @@ -1212,19 +1211,19 @@ def __init__(self): except AttributeError: pass else: - self.socket.setsockopt(socket.SOL_SOCKET, reuseport, 1) + self._socket.setsockopt(socket.SOL_SOCKET, reuseport, 1) - self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) - self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) + self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) + self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) try: - self.socket.bind(('', _MDNS_PORT)) + self._socket.bind(('', _MDNS_PORT)) except Exception as e: # TODO stop catching all Exceptions # Some versions of linux raise an exception even though # the SO_REUSE* options have been set, so ignore it # log.exception('Unknown error, possibly benign: %r', e) - self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, - socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, + socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) self.listeners = [] self.browsers = [] @@ -1237,6 +1236,7 @@ def __init__(self): self.engine = Engine(self) self.listener = Listener(self) + self.engine.add_reader(self.listener, self._socket) self.reaper = Reaper(self) def wait(self, timeout): @@ -1514,7 +1514,7 @@ def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): packet = out.packet() try: while packet: - bytes_sent = self.socket.sendto(packet, 0, (addr, port)) + bytes_sent = self._socket.sendto(packet, 0, (addr, port)) if bytes_sent < 0: break packet = packet[bytes_sent:] @@ -1531,7 +1531,7 @@ def close(self): self.notify_all() self.engine.notify() self.unregister_all_services() - self.socket.close() + self._socket.close() # Test a few module features, including service registration, service # query (for Zoe), and service unregistration. From a0ee3d62db7b5350a21091e37824e187ebf99348 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 12 Dec 2014 09:56:02 +0000 Subject: [PATCH 113/163] Rationalize error handling when sending data --- zeroconf.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 8bb452e9..48031be9 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -160,6 +160,10 @@ def current_time_millis(): # Exceptions +class Error(Exception): + pass + + class NonLocalNameException(Exception): pass @@ -1512,15 +1516,10 @@ def handle_query(self, msg, addr, port): def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): """Sends an outgoing packet.""" packet = out.packet() - try: - while packet: - bytes_sent = self._socket.sendto(packet, 0, (addr, port)) - if bytes_sent < 0: - break - packet = packet[bytes_sent:] - except Exception as e: # TODO stop catching all Exceptions - # Ignore this, it may be a temporary loss of network connection - log.exception('Unknown error, possibly benign: %r', e) + bytes_sent = self._socket.sendto(packet, 0, (addr, port)) + if bytes_sent != len(packet): + raise Error( + 'Should not happen, sent %d out of %d bytes' % (bytes_sent, len(packet))) def close(self): """Ends the background threads, and prevent this instance from From 442a59967f7b0f2d5c2ef512874ad2ab13dedae4 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 12 Dec 2014 11:04:36 +0000 Subject: [PATCH 114/163] Add preliminary support for mulitple net interfaces --- requirements-dev.txt | 2 + setup.py | 2 + zeroconf.py | 117 ++++++++++++++++++++++++++++++------------- 3 files changed, 86 insertions(+), 35 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 362737ff..757ad1e5 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,10 +1,12 @@ autopep8 coveralls coverage +enum34 flake8 flake8-blind-except flake8-import-order>=0.4.0 mock +netifaces nose pep8-naming six diff --git a/setup.py b/setup.py index 229a653d..653d963a 100755 --- a/setup.py +++ b/setup.py @@ -51,6 +51,8 @@ 'mDNS', ], install_requires=[ + 'enum34', + 'netifaces', 'six', ], ) diff --git a/zeroconf.py b/zeroconf.py index 48031be9..c65d7f30 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -27,6 +27,7 @@ __version__ = '0.16.0' __license__ = 'LGPL' +import enum import logging import select import socket @@ -35,6 +36,7 @@ import time from functools import reduce +import netifaces from six import indexbytes, int2byte, text_type from six.moves import xrange @@ -1190,6 +1192,51 @@ def __repr__(self): return result +@enum.unique +class InterfaceChoice(enum.Enum): + Default = 1 + All = 2 + + +def get_all_addresses(address_family): + return [ + addr['addr'] + for iface in netifaces.interfaces() + for addr in netifaces.ifaddresses(iface).get(address_family, []) + ] + + +def normalize_interface_choice(choice, address_family): + if choice is InterfaceChoice.Default: + choice = ['0.0.0.0'] + elif choice is InterfaceChoice.All: + choice = get_all_addresses(address_family) + return choice + + +def new_socket(): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + # SO_REUSEADDR should be equivalent to SO_REUSEPORT for + # multicast UDP sockets (p 731, "TCP/IP Illustrated, + # Volume 2"), but some BSD-derived systems require + # SO_REUSEPORT to be specified explicity. Also, not all + # versions of Python have SO_REUSEPORT available. + try: + reuseport = socket.SO_REUSEPORT + except AttributeError: + pass + else: + s.setsockopt(socket.SOL_SOCKET, reuseport, 1) + + s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) + s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) + + s.bind(('', _MDNS_PORT)) + return s + + class Zeroconf(object): """Implementation of Zeroconf Multicast DNS Service Discovery @@ -1197,37 +1244,33 @@ class Zeroconf(object): Supports registration, unregistration, queries and browsing. """ - def __init__(self): + def __init__( + self, + interfaces=InterfaceChoice.Default, + ): """Creates an instance of the Zeroconf class, establishing - multicast communications, listening and reaping threads.""" + multicast communications, listening and reaping threads. + + :type interfaces: :class:`InterfaceChoice` or sequence of ip addresses + """ global _GLOBAL_DONE _GLOBAL_DONE = False - self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - # SO_REUSEADDR should be equivalent to SO_REUSEPORT for - # multicast UDP sockets (p 731, "TCP/IP Illustrated, - # Volume 2"), but some BSD-derived systems require - # SO_REUSEPORT to be specified explicity. Also, not all - # versions of Python have SO_REUSEPORT available. - try: - reuseport = socket.SO_REUSEPORT - except AttributeError: - pass - else: - self._socket.setsockopt(socket.SOL_SOCKET, reuseport, 1) - self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) - self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) - try: - self._socket.bind(('', _MDNS_PORT)) - except Exception as e: # TODO stop catching all Exceptions - # Some versions of linux raise an exception even though - # the SO_REUSE* options have been set, so ignore it - # - log.exception('Unknown error, possibly benign: %r', e) - self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, - socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) + self._listen_socket = new_socket() + interfaces = normalize_interface_choice(interfaces, socket.AF_INET) + + self._respond_sockets = [] + + for i in interfaces: + self._listen_socket.setsockopt( + socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, + socket.inet_aton(_MDNS_ADDR) + socket.inet_aton(i)) + + respond_socket = new_socket() + respond_socket.setsockopt( + socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(i)) + + self._respond_sockets.append(respond_socket) self.listeners = [] self.browsers = [] @@ -1240,7 +1283,7 @@ def __init__(self): self.engine = Engine(self) self.listener = Listener(self) - self.engine.add_reader(self.listener, self._socket) + self.engine.add_reader(self.listener, self._listen_socket) self.reaper = Reaper(self) def wait(self, timeout): @@ -1516,10 +1559,12 @@ def handle_query(self, msg, addr, port): def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): """Sends an outgoing packet.""" packet = out.packet() - bytes_sent = self._socket.sendto(packet, 0, (addr, port)) - if bytes_sent != len(packet): - raise Error( - 'Should not happen, sent %d out of %d bytes' % (bytes_sent, len(packet))) + for s in self._respond_sockets: + bytes_sent = s.sendto(packet, 0, (addr, port)) + if bytes_sent != len(packet): + raise Error( + 'Should not happen, sent %d out of %d bytes' % ( + bytes_sent, len(packet))) def close(self): """Ends the background threads, and prevent this instance from @@ -1530,7 +1575,8 @@ def close(self): self.notify_all() self.engine.notify() self.unregister_all_services() - self._socket.close() + for s in [self._listen_socket] + self._respond_sockets: + s.close() # Test a few module features, including service registration, service # query (for Zoe), and service unregistration. @@ -1553,8 +1599,9 @@ def close(self): r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local."))) print(" Query done.") print("3. Testing query of own service...") - print(" Getting self: %s" % ( - r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.")),) + info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.") + assert info + print(" Getting self: %s" % (info,)) print(" Query done.") print("4. Testing unregister of service information...") r.unregister_service(info) From f3fd4cd69e9707221d8bd5ee6b3bb86b0985f604 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 12 Dec 2014 15:21:13 +0000 Subject: [PATCH 115/163] Fix ServiceInfo repr and text on Python 3 Closes #1 --- zeroconf.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index c65d7f30..341abf45 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1079,10 +1079,10 @@ def _set_text(self, text): for s in strs: try: - key, value = s.split('=', 1) - if value == 'true': + key, value = s.split(b'=', 1) + if value == b'true': value = True - elif value == 'false' or not value: + elif value == b'false' or not value: value = False except Exception as e: # TODO stop catching all Exceptions log.exception('Unknown error, possibly benign: %r', e) @@ -1179,17 +1179,16 @@ def __ne__(self, other): def __repr__(self): """String representation""" - result = "service[%s,%s:%s," % (self.name, - socket.inet_ntoa(self.address), self.port) - if self.text is None: - result += "None" - else: - if len(self.text) < 20: - result += self.text - else: - result += self.text[:17] + "..." - result += "]" - return result + return '%s(%s)' % ( + type(self).__name__, + ', '.join( + '%s=%r' % (name, getattr(self, name)) + for name in ( + 'type', 'name', 'address', 'port', 'weight', 'priority', + 'server', 'properties', + ) + ) + ) @enum.unique From 500a76bb1332fe34b45e681c767baddfbece4916 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 12 Dec 2014 20:21:38 +0000 Subject: [PATCH 116/163] Export Error and InterfaceChoice --- zeroconf.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index 341abf45..976897a7 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -50,7 +50,10 @@ class NullHandler(logging.Handler): def emit(self, record): pass -__all__ = ["Zeroconf", "ServiceInfo", "ServiceBrowser"] +__all__ = [ + "Zeroconf", "ServiceInfo", "ServiceBrowser", + "Error", "InterfaceChoice", +] log = logging.getLogger(__name__) From 0f7c64f8cdacae34c227edd5da4f445ece12da89 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 4 Feb 2015 00:44:51 +0100 Subject: [PATCH 117/163] Ignore EADDRINUSE when appropriate On some systems it's necessary to do so --- zeroconf.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 976897a7..de99bdbd 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -28,6 +28,7 @@ __license__ = 'LGPL' import enum +import errno import logging import select import socket @@ -1239,6 +1240,11 @@ def new_socket(): return s +def get_errno(e): + assert isinstance(e, socket.error) + return e.args[0] + + class Zeroconf(object): """Implementation of Zeroconf Multicast DNS Service Discovery @@ -1264,9 +1270,19 @@ def __init__( self._respond_sockets = [] for i in interfaces: - self._listen_socket.setsockopt( - socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, - socket.inet_aton(_MDNS_ADDR) + socket.inet_aton(i)) + log.debug('Adding %r to multicast group', i) + try: + self._listen_socket.setsockopt( + socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, + socket.inet_aton(_MDNS_ADDR) + socket.inet_aton(i)) + except socket.error as e: + if get_errno(e) == errno.EADDRINUSE: + log.info( + 'Address in use when adding %s to multicast group, ' + 'it is expected to happen on some systems', i, + ) + else: + raise respond_socket = new_socket() respond_socket.setsockopt( From 193cf47a1144afc9158f0075a886c1f754d96f18 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 4 Feb 2015 00:47:01 +0100 Subject: [PATCH 118/163] Use all network interfaces by default --- README.rst | 6 ++++++ zeroconf.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 75e9dd12..ffd9bc91 100644 --- a/README.rst +++ b/README.rst @@ -87,6 +87,12 @@ Here's an example: finally: zeroconf.close() +.. note:: + + Discovery and service registration use *all* available network interfaces by default. + If you want to customize that you need to specify ``interfaces`` argument when + constructing ``Zeroconf`` object (see the code for details). + See examples directory for more. Changelog diff --git a/zeroconf.py b/zeroconf.py index de99bdbd..ce5de5da 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1254,7 +1254,7 @@ class Zeroconf(object): def __init__( self, - interfaces=InterfaceChoice.Default, + interfaces=InterfaceChoice.All, ): """Creates an instance of the Zeroconf class, establishing multicast communications, listening and reaping threads. From 0208228d8c760f3672954f5434c2ea54d7fd4196 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 4 Feb 2015 01:09:51 +0100 Subject: [PATCH 119/163] Configure logging during the tests --- test_zeroconf.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test_zeroconf.py b/test_zeroconf.py index 5a1f4ea1..ebe616b2 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -2,6 +2,7 @@ """ Unit tests for zeroconf.py """ +import logging import socket import struct import unittest @@ -19,6 +20,18 @@ Zeroconf, ) +log = logging.getLogger('zeroconf') +original_logging_level = [None] + + +def setup_module(): + original_logging_level[0] = log.level + log.setLevel(logging.DEBUG) + + +def teardown_module(): + log.setLevel(original_logging_level[0]) + class PacketGeneration(unittest.TestCase): From b5e9e944e6f3c990862b3b03831bb988579ed340 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 4 Feb 2015 23:26:26 +0100 Subject: [PATCH 120/163] Skip host only network interfaces On Ubuntu Linux treating such interface (network mask 255.255.255.255) would result in: * EADDRINUSE "Address already in use" when trying to add multicast group membership using IP_ADD_MEMBERSHIP * success when setting the interface as outgoing multicast interface using IP_MULTICAST_IF * EINVAL "Invalid argument" when trying to send multicast datagram using socket with that interface set as the multicast outgoing interface --- zeroconf.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zeroconf.py b/zeroconf.py index ce5de5da..02cdb9c6 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1201,11 +1201,15 @@ class InterfaceChoice(enum.Enum): All = 2 +HOST_ONLY_NETWORK_MASK = '255.255.255.255' + + def get_all_addresses(address_family): return [ addr['addr'] for iface in netifaces.interfaces() for addr in netifaces.ifaddresses(iface).get(address_family, []) + if addr['netmask'] != HOST_ONLY_NETWORK_MASK ] From 463428ff8550a4f0e12b60e6f6a35efedca31271 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 00:41:56 +0100 Subject: [PATCH 121/163] Don't fail when the netmask is unknown --- zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index 02cdb9c6..e5f06428 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1209,7 +1209,7 @@ def get_all_addresses(address_family): addr['addr'] for iface in netifaces.interfaces() for addr in netifaces.ifaddresses(iface).get(address_family, []) - if addr['netmask'] != HOST_ONLY_NETWORK_MASK + if addr.get('netmask') != HOST_ONLY_NETWORK_MASK ] From af5e363e7fcb392081dc98915defd93c5002c3fc Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 00:44:20 +0100 Subject: [PATCH 122/163] Remove duplicates from all interfaces list It has been mentioned in GH #12 that the list of all machine's network interfaces can contain duplicates; it shouldn't break anything but there's no need to open multiple sockets in such case. --- zeroconf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index e5f06428..a2f170d9 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1205,12 +1205,12 @@ class InterfaceChoice(enum.Enum): def get_all_addresses(address_family): - return [ + return list(set( addr['addr'] for iface in netifaces.interfaces() for addr in netifaces.ifaddresses(iface).get(address_family, []) if addr.get('netmask') != HOST_ONLY_NETWORK_MASK - ] + )) def normalize_interface_choice(choice, address_family): From 193ee64d6212ff9a814b76b13f9ef46676025dc3 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 22:29:44 +0100 Subject: [PATCH 123/163] Make examples nicer and make them show all logs --- examples/browser.py | 14 ++++++++++---- examples/registration.py | 39 +++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/examples/browser.py b/examples/browser.py index 9ac6d849..5b6d5369 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -3,9 +3,9 @@ """ Example of browsing for a service (in this case, HTTP) """ +import logging import socket - -from six.moves import input +from time import sleep from zeroconf import ServiceBrowser, Zeroconf @@ -35,11 +35,17 @@ def add_service(self, zeroconf, type, name): print('\n') if __name__ == '__main__': + logging.basicConfig(level=logging.DEBUG) + logging.getLogger('zeroconf').setLevel(logging.DEBUG) + zeroconf = Zeroconf() - print("Browsing services...") + print("\nBrowsing services, press Ctrl-C to exit...\n") listener = MyListener() browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) try: - input("Waiting (press Enter to exit)...\n\n") + while True: + sleep(0.1) + except KeyboardInterrupt: + pass finally: zeroconf.close() diff --git a/examples/registration.py b/examples/registration.py index 50d53c1b..64c0d08f 100755 --- a/examples/registration.py +++ b/examples/registration.py @@ -2,25 +2,32 @@ """ Example of announcing a service (in this case, a fake HTTP server) """ +import logging import socket - -from six.moves import input +from time import sleep from zeroconf import ServiceInfo, Zeroconf -desc = {'path': '/~paulsm/'} +if __name__ == '__main__': + logging.basicConfig(level=logging.DEBUG) + logging.getLogger('zeroconf').setLevel(logging.DEBUG) + + desc = {'path': '/~paulsm/'} -info = ServiceInfo("_http._tcp.local.", - "Paul's Test Web Site._http._tcp.local.", - socket.inet_aton("10.0.1.2"), 80, 0, 0, - desc, "ash-2.local.") + info = ServiceInfo("_http._tcp.local.", + "Paul's Test Web Site._http._tcp.local.", + socket.inet_aton("127.0.0.1"), 80, 0, 0, + desc, "ash-2.local.") -zeroconf = Zeroconf() -print("Registration of a service...") -zeroconf.register_service(info) -try: - input("Waiting (press Enter to exit)...") -finally: - print("Unregistering...") - zeroconf.unregister_service(info) - zeroconf.close() + zeroconf = Zeroconf() + print("Registration of a service, press Ctrl-C to exit...") + zeroconf.register_service(info) + try: + while True: + sleep(0.1) + except KeyboardInterrupt: + pass + finally: + print("Unregistering...") + zeroconf.unregister_service(info) + zeroconf.close() From 3775c47d8cf3c941603fa393265b86d05f61b915 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 22:31:23 +0100 Subject: [PATCH 124/163] Remove redundant parentheses --- zeroconf.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index a2f170d9..0fae7337 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -847,7 +847,7 @@ def add_reader(self, reader, socket): def del_reader(self, socket): with self.condition: - del(self.readers[socket]) + del self.readers[socket] self.condition.notify() def notify(self): @@ -954,7 +954,7 @@ def update_record(self, zc, now, record): if not expired: oldrecord.reset_ttl(record) else: - del(self.services[record.alias.lower()]) + del self.services[record.alias.lower()] callback = lambda x: self.listener.remove_service(x, self.type, record.alias) self.list.append(callback) @@ -1340,7 +1340,7 @@ def remove_service_listener(self, listener): for browser in self.browsers: if browser.listener == listener: browser.cancel() - del(browser) + del browser def register_service(self, info, ttl=_DNS_TTL): """Registers service information to the network with a default TTL @@ -1379,7 +1379,7 @@ def register_service(self, info, ttl=_DNS_TTL): def unregister_service(self, info): """Unregister a service.""" try: - del(self.services[info.name.lower()]) + del self.services[info.name.lower()] if self.servicetypes[info.type] > 1: self.servicetypes[info.type] -= 1 else: From df88670963e8c3a1f11a6af026b484ff4343d271 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 22:39:17 +0100 Subject: [PATCH 125/163] Make the code nicer This includes: * rearranging code to make it more readable * catching KeyError instead of all exceptions and making it obvious what can possibly raise there * renaming things --- zeroconf.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 0fae7337..46565ef1 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -936,7 +936,7 @@ def __init__(self, zc, type, listener): self.services = {} self.next_time = current_time_millis() self.delay = _BROWSER_TIME - self.list = [] + self._handlers_to_call = [] self.done = False @@ -949,23 +949,22 @@ def update_record(self, zc, now, record): Updates information required by browser in the Zeroconf cache.""" if record.type == _TYPE_PTR and record.name == self.type: expired = record.is_expired(now) + service_key = record.alias.lower() try: - oldrecord = self.services[record.alias.lower()] + old_record = self.services[service_key] + except KeyError: if not expired: - oldrecord.reset_ttl(record) + self.services[service_key] = record + self._handlers_to_call.append( + lambda x: self.listener.add_service(x, self.type, record.alias)) + else: + if not expired: + old_record.reset_ttl(record) else: - del self.services[record.alias.lower()] - callback = lambda x: self.listener.remove_service(x, - self.type, record.alias) - self.list.append(callback) + del self.services[service_key] + self._handlers_to_call.append( + lambda x: self.listener.remove_service(x, self.type, record.alias)) return - except Exception as e: # TODO stop catching all Exceptions - log.exception('Unknown error, possibly benign: %r', e) - if not expired: - self.services[record.alias.lower()] = record - callback = lambda x: self.listener.add_service(x, - self.type, record.alias) - self.list.append(callback) expires = record.get_expiration_time(75) if expires < self.next_time: @@ -977,9 +976,8 @@ def cancel(self): def run(self): while True: - event = None now = current_time_millis() - if len(self.list) == 0 and self.next_time > now: + if len(self._handlers_to_call) == 0 and self.next_time > now: self.zc.wait(self.next_time - now) if _GLOBAL_DONE or self.done: return @@ -995,11 +993,9 @@ def run(self): self.next_time = now + self.delay self.delay = min(20 * 1000, self.delay * 2) - if len(self.list) > 0: - event = self.list.pop(0) - - if event is not None: - event(self.zc) + if len(self._handlers_to_call) > 0: + handler = self._handlers_to_call.pop(0) + handler(self.zc) class ServiceInfo(object): From 451c0729e2490ac6283010ddcbbcc723d86e6765 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 22:43:59 +0100 Subject: [PATCH 126/163] Add some debug logging --- zeroconf.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zeroconf.py b/zeroconf.py index 46565ef1..e6d39656 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -878,6 +878,9 @@ def handle_read(self, socket_): return else: raise e + else: + log.debug('Received %r from %r:%r', data, addr, port) + self.data = data msg = DNSIncoming(data) if msg.is_query(): @@ -1577,6 +1580,7 @@ def handle_query(self, msg, addr, port): def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): """Sends an outgoing packet.""" packet = out.packet() + log.debug('Sending %r as %r...', out, packet) for s in self._respond_sockets: bytes_sent = s.sendto(packet, 0, (addr, port)) if bytes_sent != len(packet): From 7cad7a43179e3f547796b125e3ed8169ef3f4157 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 22:51:04 +0100 Subject: [PATCH 127/163] Handle exceptions better --- zeroconf.py | 55 +++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index e6d39656..23ac7222 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1069,38 +1069,35 @@ def _set_properties(self, properties): def _set_text(self, text): """Sets properties and text given a text field""" self.text = text - try: - result = {} - end = len(text) - index = 0 - strs = [] - while index < end: - length = indexbytes(text, index) - index += 1 - strs.append(text[index:index + length]) - index += length - - for s in strs: - try: - key, value = s.split(b'=', 1) - if value == b'true': - value = True - elif value == b'false' or not value: - value = False - except Exception as e: # TODO stop catching all Exceptions - log.exception('Unknown error, possibly benign: %r', e) - # No equals sign at all - key = s + result = {} + end = len(text) + index = 0 + strs = [] + while index < end: + length = indexbytes(text, index) + index += 1 + strs.append(text[index:index + length]) + index += length + + for s in strs: + parts = s.split(b'=', 1) + try: + key, value = parts + except ValueError: + # No equals sign at all + key = s + value = False + else: + if value == b'true': + value = True + elif value == b'false' or not value: value = False - # Only update non-existent properties - if key and result.get(key) is None: - result[key] = value + # Only update non-existent properties + if key and result.get(key) is None: + result[key] = value - self._properties = result - except Exception as e: # TODO stop catching all Exceptions - log.exception('Unknown error, possibly benign: %r', e) - self._properties = None + self._properties = result def get_name(self): """Name accessor""" From d162e54c6aad175505028aa7beb8a1a0cb7a231d Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 23:25:30 +0100 Subject: [PATCH 128/163] Change ServiceBrowser interface experimentally --- examples/browser.py | 19 +++++--------- zeroconf.py | 64 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/examples/browser.py b/examples/browser.py index 5b6d5369..8f5052c0 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -7,19 +7,14 @@ import socket from time import sleep -from zeroconf import ServiceBrowser, Zeroconf +from zeroconf import ServiceBrowser, ServiceStateChange, Zeroconf -class MyListener(object): +def on_service_state_change(zeroconf, service_type, name, state_change): + print("Service %s of type %s state changed: %s" % (name, service_type, state_change)) - def remove_service(self, zeroconf, type, name): - print("Service %s removed" % (name,)) - print('\n') - - def add_service(self, zeroconf, type, name): - print("Service %s added" % (name,)) - print(" Type is %s" % (type,)) - info = zeroconf.get_service_info(type, name) + if state_change is ServiceStateChange.Added: + info = zeroconf.get_service_info(service_type, name) if info: print(" Address is %s:%d" % (socket.inet_ntoa(info.address), info.port)) @@ -40,8 +35,8 @@ def add_service(self, zeroconf, type, name): zeroconf = Zeroconf() print("\nBrowsing services, press Ctrl-C to exit...\n") - listener = MyListener() - browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) + browser = ServiceBrowser(zeroconf, "_http._tcp.local.", handlers=[on_service_state_change]) + try: while True: sleep(0.1) diff --git a/zeroconf.py b/zeroconf.py index 23ac7222..53d7cabf 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -53,7 +53,7 @@ def emit(self, record): __all__ = [ "Zeroconf", "ServiceInfo", "ServiceBrowser", - "Error", "InterfaceChoice", + "Error", "InterfaceChoice", "ServiceStateChange", ] @@ -921,6 +921,33 @@ def run(self): self.zc.cache.remove(record) +class Signal(object): + def __init__(self): + self._handlers = [] + + def fire(self, **kwargs): + for h in list(self._handlers): + h(**kwargs) + + @property + def registration_interface(self): + return SignalRegistrationInterface(self._handlers) + + +class SignalRegistrationInterface(object): + + def __init__(self, handlers): + self._handlers = handlers + + def register_handler(self, handler): + self._handlers.append(handler) + return self + + def unregister_handler(self, handler): + self._handlers.remove(handler) + return self + + class ServiceBrowser(threading.Thread): """Used to browse for a service of a specific type. @@ -929,13 +956,12 @@ class ServiceBrowser(threading.Thread): remove_service() methods called when this browser discovers changes in the services availability.""" - def __init__(self, zc, type, listener): + def __init__(self, zc, type_, handlers): """Creates a browser for a specific type""" threading.Thread.__init__(self) self.daemon = True self.zc = zc - self.type = type - self.listener = listener + self.type = type_ self.services = {} self.next_time = current_time_millis() self.delay = _BROWSER_TIME @@ -946,10 +972,28 @@ def __init__(self, zc, type, listener): self.zc.add_listener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) self.start() + self._service_state_changed = Signal() + for h in handlers: + self.service_state_changed.register_handler(h) + + @property + def service_state_changed(self): + return self._service_state_changed.registration_interface + def update_record(self, zc, now, record): """Callback invoked by Zeroconf when new information arrives. Updates information required by browser in the Zeroconf cache.""" + + def enqueue_callback(state_change, name): + self._handlers_to_call.append( + lambda zeroconf: self._service_state_changed.fire( + zeroconf=zeroconf, + service_type=self.type, + name=name, + state_change=state_change, + )) + if record.type == _TYPE_PTR and record.name == self.type: expired = record.is_expired(now) service_key = record.alias.lower() @@ -958,15 +1002,13 @@ def update_record(self, zc, now, record): except KeyError: if not expired: self.services[service_key] = record - self._handlers_to_call.append( - lambda x: self.listener.add_service(x, self.type, record.alias)) + enqueue_callback(ServiceStateChange.Added, record.alias) else: if not expired: old_record.reset_ttl(record) else: del self.services[service_key] - self._handlers_to_call.append( - lambda x: self.listener.remove_service(x, self.type, record.alias)) + enqueue_callback(ServiceStateChange.Removed, record.alias) return expires = record.get_expiration_time(75) @@ -1197,6 +1239,12 @@ class InterfaceChoice(enum.Enum): All = 2 +@enum.unique +class ServiceStateChange(enum.Enum): + Added = 1 + Removed = 2 + + HOST_ONLY_NETWORK_MASK = '255.255.255.255' From 08e0dc2c7c1551ffa9a1e7297112b0f46b7ccc4e Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 23:25:49 +0100 Subject: [PATCH 129/163] Make examples' output quiet by default --- examples/browser.py | 5 ++++- examples/registration.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/browser.py b/examples/browser.py index 8f5052c0..7ded30b6 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -5,6 +5,7 @@ import logging import socket +import sys from time import sleep from zeroconf import ServiceBrowser, ServiceStateChange, Zeroconf @@ -31,7 +32,9 @@ def on_service_state_change(zeroconf, service_type, name, state_change): if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) - logging.getLogger('zeroconf').setLevel(logging.DEBUG) + if len(sys.argv) > 1: + assert sys.argv[1:] == ['--debug'] + logging.getLogger('zeroconf').setLevel(logging.DEBUG) zeroconf = Zeroconf() print("\nBrowsing services, press Ctrl-C to exit...\n") diff --git a/examples/registration.py b/examples/registration.py index 64c0d08f..f292ea2c 100755 --- a/examples/registration.py +++ b/examples/registration.py @@ -4,13 +4,16 @@ import logging import socket +import sys from time import sleep from zeroconf import ServiceInfo, Zeroconf if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) - logging.getLogger('zeroconf').setLevel(logging.DEBUG) + if len(sys.argv) > 1: + assert sys.argv[1:] == ['--debug'] + logging.getLogger('zeroconf').setLevel(logging.DEBUG) desc = {'path': '/~paulsm/'} From 219081860d28e49b1ae71a78e1a0da459689ab9c Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 23:38:53 +0100 Subject: [PATCH 130/163] Reformat a bit --- examples/browser.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/browser.py b/examples/browser.py index 7ded30b6..633ea0e1 100755 --- a/examples/browser.py +++ b/examples/browser.py @@ -17,15 +17,15 @@ def on_service_state_change(zeroconf, service_type, name, state_change): if state_change is ServiceStateChange.Added: info = zeroconf.get_service_info(service_type, name) if info: - print(" Address is %s:%d" % (socket.inet_ntoa(info.address), - info.port)) - print(" Weight is %d, Priority is %d" % (info.weight, - info.priority)) - print(" Server is", info.server) + print(" Address: %s:%d" % (socket.inet_ntoa(info.address), info.port)) + print(" Weight: %d, priority: %d" % (info.weight, info.priority)) + print(" Server: %s" % (info.server,)) if info.properties: - print(" Properties are") + print(" Properties are:") for key, value in info.properties.items(): print(" %s: %s" % (key, value)) + else: + print(" No properties") else: print(" No info") print('\n') From b4430274ba8355ceaadc2d89a84752f1ac1485e7 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 23:44:52 +0100 Subject: [PATCH 131/163] Fix using binary strings as property values Previously it'd fall trough and set the value to False --- zeroconf.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 53d7cabf..3a8c3ef6 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -38,7 +38,7 @@ from functools import reduce import netifaces -from six import indexbytes, int2byte, text_type +from six import binary_type, indexbytes, int2byte, iteritems, text_type from six.moves import xrange @@ -1085,8 +1085,7 @@ def _set_properties(self, properties): self._properties = properties list = [] result = b'' - for key in properties: - value = properties[key] + for key, value in iteritems(properties): if isinstance(key, text_type): key = key.encode('utf-8') @@ -1094,6 +1093,8 @@ def _set_properties(self, properties): suffix = b'' elif isinstance(value, text_type): suffix = value.encode('utf-8') + elif isinstance(value, binary_type): + suffix = value elif isinstance(value, int): if value: suffix = b'true' From ac5a63ece96fbf9d64e41e7a4867cc1d8b2f6b96 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 23:45:52 +0100 Subject: [PATCH 132/163] Move self test example out of main module --- examples/self_test.py | 40 ++++++++++++++++++++++++++++++++++++++++ zeroconf.py | 31 +------------------------------ 2 files changed, 41 insertions(+), 30 deletions(-) create mode 100755 examples/self_test.py diff --git a/examples/self_test.py b/examples/self_test.py new file mode 100755 index 00000000..00c022d2 --- /dev/null +++ b/examples/self_test.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +from __future__ import absolute_import, division, print_function, unicode_literals + +import logging +import socket +import sys + +from zeroconf import __version__, ServiceInfo, Zeroconf + +if __name__ == '__main__': + logging.basicConfig(level=logging.DEBUG) + if len(sys.argv) > 1: + assert sys.argv[1:] == ['--debug'] + logging.getLogger('zeroconf').setLevel(logging.DEBUG) + + # Test a few module features, including service registration, service + # query (for Zoe), and service unregistration. + print("Multicast DNS Service Discovery for Python, version %s" % (__version__,)) + r = Zeroconf() + print("1. Testing registration of a service...") + desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'} + info = ServiceInfo("_http._tcp.local.", + "My Service Name._http._tcp.local.", + socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) + print(" Registering service...") + r.register_service(info) + print(" Registration done.") + print("2. Testing query of service information...") + print(" Getting ZOE service: %s" % ( + r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local."))) + print(" Query done.") + print("3. Testing query of own service...") + info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.") + assert info + print(" Getting self: %s" % (info,)) + print(" Query done.") + print("4. Testing unregister of service information...") + r.unregister_service(info) + print(" Unregister done.") + r.close() diff --git a/zeroconf.py b/zeroconf.py index 3a8c3ef6..893cd16c 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -52,6 +52,7 @@ def emit(self, record): pass __all__ = [ + "__version__", "Zeroconf", "ServiceInfo", "ServiceBrowser", "Error", "InterfaceChoice", "ServiceStateChange", ] @@ -1645,33 +1646,3 @@ def close(self): self.unregister_all_services() for s in [self._listen_socket] + self._respond_sockets: s.close() - -# Test a few module features, including service registration, service -# query (for Zoe), and service unregistration. - -if __name__ == '__main__': - logging.basicConfig(level=logging.DEBUG) - log.setLevel(logging.DEBUG) - print("Multicast DNS Service Discovery for Python, version %s" % __version__) - r = Zeroconf() - print("1. Testing registration of a service...") - desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'} - info = ServiceInfo("_http._tcp.local.", - "My Service Name._http._tcp.local.", - socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) - print(" Registering service...") - r.register_service(info) - print(" Registration done.") - print("2. Testing query of service information...") - print(" Getting ZOE service: %s" % ( - r.get_service_info("_http._tcp.local.", "ZOE._http._tcp.local."))) - print(" Query done.") - print("3. Testing query of own service...") - info = r.get_service_info("_http._tcp.local.", "My Service Name._http._tcp.local.") - assert info - print(" Getting self: %s" % (info,)) - print(" Query done.") - print("4. Testing unregister of service information...") - r.unregister_service(info) - print(" Unregister done.") - r.close() From 275a22b997331d499526293b98faff11ca6edea5 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 23:46:42 +0100 Subject: [PATCH 133/163] Prepare for new release --- zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index 893cd16c..fc17bd8e 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -24,7 +24,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.16.0' +__version__ = '0.17.0a1' __license__ = 'LGPL' import enum From b5fb3e86a688f6161c1292ccdffeec9f455c1fbd Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Thu, 5 Feb 2015 23:53:22 +0100 Subject: [PATCH 134/163] Fix test breakage --- test_zeroconf.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test_zeroconf.py b/test_zeroconf.py index ebe616b2..fb82ad16 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -17,6 +17,7 @@ Listener, ServiceBrowser, ServiceInfo, + ServiceStateChange, Zeroconf, ) @@ -154,19 +155,15 @@ def test_integration(): type_ = "_http._tcp.local." registration_name = "xxxyyy.%s" % type_ - class MyListener(object): - - def remove_service(self, zeroconf, type_, name): - if name == registration_name: - service_removed.set() - - def add_service(self, zeroconf, type_, name): - if name == registration_name: + def on_service_state_change(zeroconf, service_type, state_change, name): + if name == registration_name: + if state_change is ServiceStateChange.Added: service_added.set() + elif state_change is ServiceStateChange.Removed: + service_removed.set() zeroconf_browser = Zeroconf() - listener = MyListener() - browser = ServiceBrowser(zeroconf_browser, type_, listener) + browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) zeroconf_registrar = Zeroconf() desc = {'path': '/~paulsm/'} From c748294fdc6f3bf527f62d4c0cb76ace32890128 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 15 Feb 2015 17:52:14 +0100 Subject: [PATCH 135/163] Restore old listener interface (and example) for now --- examples/old_browser.py | 54 +++++++++++++++++++++++++++++++++++++++++ zeroconf.py | 21 +++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100755 examples/old_browser.py diff --git a/examples/old_browser.py b/examples/old_browser.py new file mode 100755 index 00000000..4ad1cc13 --- /dev/null +++ b/examples/old_browser.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +from __future__ import absolute_import, division, print_function, unicode_literals + +""" Example of browsing for a service (in this case, HTTP) """ + +import logging +import socket +import sys +from time import sleep + +from zeroconf import ServiceBrowser, Zeroconf + + +class MyListener(object): + + def remove_service(self, zeroconf, type, name): + print("Service %s removed" % (name,)) + print('\n') + + def add_service(self, zeroconf, type, name): + print("Service %s added" % (name,)) + print(" Type is %s" % (type,)) + info = zeroconf.get_service_info(type, name) + if info: + print(" Address is %s:%d" % (socket.inet_ntoa(info.address), + info.port)) + print(" Weight is %d, Priority is %d" % (info.weight, + info.priority)) + print(" Server is", info.server) + if info.properties: + print(" Properties are") + for key, value in info.properties.items(): + print(" %s: %s" % (key, value)) + else: + print(" No info") + print('\n') + +if __name__ == '__main__': + logging.basicConfig(level=logging.DEBUG) + if len(sys.argv) > 1: + assert sys.argv[1:] == ['--debug'] + logging.getLogger('zeroconf').setLevel(logging.DEBUG) + + zeroconf = Zeroconf() + print("\nBrowsing services, press Ctrl-C to exit...\n") + listener = MyListener() + browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener) + try: + while True: + sleep(0.1) + except KeyboardInterrupt: + pass + finally: + zeroconf.close() diff --git a/zeroconf.py b/zeroconf.py index fc17bd8e..b5c8d2a0 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -957,8 +957,9 @@ class ServiceBrowser(threading.Thread): remove_service() methods called when this browser discovers changes in the services availability.""" - def __init__(self, zc, type_, handlers): + def __init__(self, zc, type_, handlers=None, listener=None): """Creates a browser for a specific type""" + assert handlers or listener, 'You need to specify at least one handler' threading.Thread.__init__(self) self.daemon = True self.zc = zc @@ -974,6 +975,24 @@ def __init__(self, zc, type_, handlers): self.start() self._service_state_changed = Signal() + + if hasattr(handlers, 'add_service'): + listener = handlers + handlers = None + + handlers = handlers or [] + + if listener: + def on_change(zeroconf, service_type, name, state_change): + args = (zeroconf, service_type, name) + if state_change is ServiceStateChange.Added: + listener.add_service(*args) + elif state_change is ServiceStateChange.Removed: + listener.remove_service(*args) + else: + raise NotImplementedError(state_change) + handlers.append(on_change) + for h in handlers: self.service_state_changed.register_handler(h) From a298785cf63d26b184495f972c619d31515a1468 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 15 Feb 2015 18:01:41 +0100 Subject: [PATCH 136/163] Add pypy3 build --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 65bf4320..fda478d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,11 @@ python: - "3.3" - "3.4" - "pypy" + - "pypy3" matrix: fast_finish: true + allow_failures: + - python: pypy3 install: - pip install -r requirements-dev.txt script: From 5c32a27a6ae0cccf7af25961cd98560a5173b065 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 15 Feb 2015 18:08:44 +0100 Subject: [PATCH 137/163] Describe recent changes --- README.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.rst b/README.rst index ffd9bc91..d53cea98 100644 --- a/README.rst +++ b/README.rst @@ -98,6 +98,18 @@ See examples directory for more. Changelog ========= +0.17.0 (unreleased yet) +----------------------- + +* Added some Python dependencies so it's not zero-dependencies anymore +* Improved exception handlin (it'll be quieter now) +* Messages are listened to and sent using all available network interfaces + by default (configurable) +* Started using logging more freely +* Fixed a bug with binary strings as property values being converted to False + (https://github.com/jstasiak/python-zeroconf/pull/10) +* Added new ``ServiceBrowser`` event handler interface (see the examples) + 0.16.0 ------ From 0009b5ea2bca77f395eb2bacc69d0dcfa5dd37dc Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 15 Feb 2015 18:11:48 +0100 Subject: [PATCH 138/163] Handle recent flake8 change --- zeroconf.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index b5c8d2a0..5fa27869 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -22,11 +22,6 @@ USA """ -__author__ = 'Paul Scott-Murphy, William McBrine' -__maintainer__ = 'Jakub Stasiak ' -__version__ = '0.17.0a1' -__license__ = 'LGPL' - import enum import errno import logging @@ -41,6 +36,11 @@ from six import binary_type, indexbytes, int2byte, iteritems, text_type from six.moves import xrange +__author__ = 'Paul Scott-Murphy, William McBrine' +__maintainer__ = 'Jakub Stasiak ' +__version__ = '0.17.0a1' +__license__ = 'LGPL' + try: NullHandler = logging.NullHandler From 4783611de72ac11bdbfea9e4324e58746a91e70a Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 15 Feb 2015 18:16:16 +0100 Subject: [PATCH 139/163] Advertise pypy3 support --- .travis.yml | 2 -- README.rst | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index fda478d0..3c09140a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,6 @@ python: - "pypy3" matrix: fast_finish: true - allow_failures: - - python: pypy3 install: - pip install -r requirements-dev.txt script: diff --git a/README.rst b/README.rst index d53cea98..05cfdda6 100644 --- a/README.rst +++ b/README.rst @@ -109,6 +109,7 @@ Changelog * Fixed a bug with binary strings as property values being converted to False (https://github.com/jstasiak/python-zeroconf/pull/10) * Added new ``ServiceBrowser`` event handler interface (see the examples) +* Pypy3 now officially supported 0.16.0 ------ From a6d75b3d63a0c13c63473910b832e6db12635e79 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 15 Feb 2015 18:17:22 +0100 Subject: [PATCH 140/163] Do 0.17.0 release --- zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index 5fa27869..9fd19fdf 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -38,7 +38,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.17.0a1' +__version__ = '0.17.0' __license__ = 'LGPL' From 93d34f925cd8913ff6836f9393cdce15679e4794 Mon Sep 17 00:00:00 2001 From: daid Date: Mon, 16 Feb 2015 13:48:58 +0100 Subject: [PATCH 141/163] Getting an EADDRNOTAVAIL error when adding an address to the multicast group on windows. --- zeroconf.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zeroconf.py b/zeroconf.py index 9fd19fdf..fb17d1b3 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1350,6 +1350,12 @@ def __init__( 'Address in use when adding %s to multicast group, ' 'it is expected to happen on some systems', i, ) + elif get_errno(e) == errno.EADDRNOTAVAIL: + log.info( + 'Address not available when adding %s to multicast group, ' + 'it is expected to happen on some systems', i, + ) + continue else: raise From a22484af90c7c4cbdee849d2b75efab2772c3592 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 17 Feb 2015 21:10:32 +0100 Subject: [PATCH 142/163] Update README.rst --- README.rst | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 05cfdda6..31b64489 100644 --- a/README.rst +++ b/README.rst @@ -43,7 +43,15 @@ Python compatibility * CPython 2.6, 2.7, 3.3+ * PyPy 2.2+ (possibly 1.9-2.1 as well) +* PyPy3 2.4+ +Versioning +---------- + +This project's versions follow the following pattern: MAJOR.MINOR.PATCH. +* MAJOR version has been 0 so far +* MINOR version is incremented on backward incompatible changes +* PATCH version is incremented on backward compatible changes How to get python-zeroconf? @@ -98,24 +106,31 @@ See examples directory for more. Changelog ========= -0.17.0 (unreleased yet) ------------------------ +0.17.1 +------ + +* Fixed EADDRNOTAVAIL when attempting to use dummy network interfaces on Windows, + thanks to daid + +0.17.0 +------ * Added some Python dependencies so it's not zero-dependencies anymore * Improved exception handlin (it'll be quieter now) * Messages are listened to and sent using all available network interfaces - by default (configurable) + by default (configurable); thanks to Marcus Müller * Started using logging more freely * Fixed a bug with binary strings as property values being converted to False - (https://github.com/jstasiak/python-zeroconf/pull/10) + (https://github.com/jstasiak/python-zeroconf/pull/10); thanks to Dr. Seuss * Added new ``ServiceBrowser`` event handler interface (see the examples) * Pypy3 now officially supported +* Fixed ServiceInfo repr on Python 3, thanks to Yordan Miladinov 0.16.0 ------ * Set up Python logging and started using it -* Cleaned up code style (includes migrating from camel case to snak case) +* Cleaned up code style (includes migrating from camel case to snake case) 0.15.1 ------ From 44fa62a738335781ecdd789ad636f82e6542ecd2 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 17 Feb 2015 21:11:02 +0100 Subject: [PATCH 143/163] Update README.rst --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index 31b64489..d0418a48 100644 --- a/README.rst +++ b/README.rst @@ -49,6 +49,7 @@ Versioning ---------- This project's versions follow the following pattern: MAJOR.MINOR.PATCH. + * MAJOR version has been 0 so far * MINOR version is incremented on backward incompatible changes * PATCH version is incremented on backward compatible changes From 7c64ebf6129fb6c0c533a1fed618c9d5926d5100 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 10 Apr 2015 02:44:59 +0200 Subject: [PATCH 144/163] Fix some typos in the readme --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index d0418a48..a4f7ea1f 100644 --- a/README.rst +++ b/README.rst @@ -117,14 +117,14 @@ Changelog ------ * Added some Python dependencies so it's not zero-dependencies anymore -* Improved exception handlin (it'll be quieter now) +* Improved exception handling (it'll be quieter now) * Messages are listened to and sent using all available network interfaces by default (configurable); thanks to Marcus Müller * Started using logging more freely * Fixed a bug with binary strings as property values being converted to False (https://github.com/jstasiak/python-zeroconf/pull/10); thanks to Dr. Seuss * Added new ``ServiceBrowser`` event handler interface (see the examples) -* Pypy3 now officially supported +* PyPy3 now officially supported * Fixed ServiceInfo repr on Python 3, thanks to Yordan Miladinov 0.16.0 From 0b8936b94011c0783c7d0469b9ebae76cd4d1976 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 10 Apr 2015 02:46:33 +0200 Subject: [PATCH 145/163] Bump version to 0.17.1 --- zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index fb17d1b3..99883f66 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -38,7 +38,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.17.0' +__version__ = '0.17.1' __license__ = 'LGPL' From 4dbd04b807813384108ff8e4cb5291c2560eed6b Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Fri, 10 Apr 2015 02:50:10 +0200 Subject: [PATCH 146/163] Restrict pep8 version as something depends on it --- requirements-dev.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements-dev.txt b/requirements-dev.txt index 757ad1e5..915a635f 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -8,5 +8,6 @@ flake8-import-order>=0.4.0 mock netifaces nose +pep8==1.5.7 pep8-naming six From ba894559f43fa6955989b92533c06fd8e8b92c74 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sat, 11 Jul 2015 21:55:27 +0200 Subject: [PATCH 147/163] Use enum-compat instead of enum34 directly This is in order for the package's installation to work on Python 3.4+, solves the same issue as https://github.com/jstasiak/python-zeroconf/pull/22. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 653d963a..54e05e1b 100755 --- a/setup.py +++ b/setup.py @@ -51,7 +51,7 @@ 'mDNS', ], install_requires=[ - 'enum34', + 'enum-compat', 'netifaces', 'six', ], From 4435a2a4ae1c0b0877785f1a5047f65bb80a14bd Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sat, 11 Jul 2015 22:39:24 +0200 Subject: [PATCH 148/163] Restrict flake8-import-order version There seems to be a bug in 0.6.x, see https://github.com/public/flake8-import-order/issues/42 --- requirements-dev.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 915a635f..bf064590 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,7 +4,8 @@ coverage enum34 flake8 flake8-blind-except -flake8-import-order>=0.4.0 +# Upper bound because of https://github.com/public/flake8-import-order/issues/42 +flake8-import-order>=0.4.0, <=0.6.0 mock netifaces nose From dadbbfc9e1787561981807d3e008433a107c1e5e Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sat, 11 Jul 2015 22:59:05 +0200 Subject: [PATCH 149/163] Fix a typo, meant strictly lesser than 0.6 :< --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index bf064590..bc92af54 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -5,7 +5,7 @@ enum34 flake8 flake8-blind-except # Upper bound because of https://github.com/public/flake8-import-order/issues/42 -flake8-import-order>=0.4.0, <=0.6.0 +flake8-import-order>=0.4.0, <0.6.0 mock netifaces nose From d1ee5ce7558060ea8d92f804172f67f960f814bb Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sun, 12 Jul 2015 23:14:06 +0200 Subject: [PATCH 150/163] Release version 0.17.2 --- README.rst | 7 +++++++ zeroconf.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index a4f7ea1f..769df0cb 100644 --- a/README.rst +++ b/README.rst @@ -107,6 +107,13 @@ See examples directory for more. Changelog ========= +0.17.2 +------ + +* Fixed installation on Python 3.4.3+ (was failing because of enum34 dependency + which fails to install on 3.4.3+, changed to depend on enum-compat instead; + thanks to Michael Brennan for the original patch, GitHub pull request #22) + 0.17.1 ------ diff --git a/zeroconf.py b/zeroconf.py index 99883f66..de427ea9 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -38,7 +38,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.17.1' +__version__ = '0.17.2' __license__ = 'LGPL' From e8299c0527c965f83c1326b18e484652a9eb829c Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 17 Aug 2015 22:23:46 -0700 Subject: [PATCH 151/163] Fix DNSText repr Python 3 issue Prevents following exception: ``` File "/Users/paulus/dev/python/netdisco/lib/python3.4/site-packages/zeroconf.py", line 412, in __repr__ return self.to_string(self.text[:7] + "...") TypeError: can't concat bytes to str ``` --- zeroconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index de427ea9..d73c41e8 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -409,7 +409,7 @@ def __eq__(self, other): def __repr__(self): """String representation""" if len(self.text) > 10: - return self.to_string(self.text[:7] + "...") + return self.to_string(self.text[:7]) + "..." else: return self.to_string(self.text) From c7567d6b065d7460e2022b8cde5dd0b52a3828a7 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 19 Aug 2015 01:46:49 +0200 Subject: [PATCH 152/163] Add a DNSText __repr__ test The test helps making sure the situation fixed by e8299c0527c965f83c1326b18e484652a9eb829c doesn't happen again. --- test_zeroconf.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test_zeroconf.py b/test_zeroconf.py index fb82ad16..6ecf6728 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- + """ Unit tests for zeroconf.py """ @@ -14,6 +16,7 @@ import zeroconf as r from zeroconf import ( + DNSText, Listener, ServiceBrowser, ServiceInfo, @@ -194,3 +197,10 @@ def test_listener_handles_closed_socket_situation_gracefully(): listener = Listener(zeroconf) listener.handle_read(zeroconf.socket) + + +def test_dnstext_repr_works(): + # There was an issue on Python 3 that prevented DNSText's repr + # from working when the text was longer than 10 bytes + text = DNSText('irrelevant', None, 0, 0, b'12345678901') + repr(text) From 9c3a81af84c3450459795e5fc5142300f9680804 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 19 Aug 2015 01:54:02 +0200 Subject: [PATCH 153/163] Prepare release 0.17.3 --- README.rst | 6 ++++++ zeroconf.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 769df0cb..de496063 100644 --- a/README.rst +++ b/README.rst @@ -107,6 +107,12 @@ See examples directory for more. Changelog ========= +0.17.3 +------ + +* Fixed DNSText repr on Python 3 (it'd crash when the text was longer than + 10 bytes), thanks to Paulus Schoutsen for the patch, GitHub pull request #24 + 0.17.2 ------ diff --git a/zeroconf.py b/zeroconf.py index d73c41e8..e636a41f 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -38,7 +38,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.17.2' +__version__ = '0.17.3' __license__ = 'LGPL' From f29c0f475be76f70ecbb1586deb4618180dd1969 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 19 Aug 2015 01:58:20 +0200 Subject: [PATCH 154/163] Make the package's status explicit --- README.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.rst b/README.rst index de496063..d603ee75 100644 --- a/README.rst +++ b/README.rst @@ -54,6 +54,12 @@ This project's versions follow the following pattern: MAJOR.MINOR.PATCH. * MINOR version is incremented on backward incompatible changes * PATCH version is incremented on backward compatible changes +Status +------ + +There are some people using this package. I don't actively use it and as such +any help I can offer with regard to any issues is very limited. + How to get python-zeroconf? =========================== From ddb10485ef17aec3f37ef70dcb37af167271bfe1 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Wed, 19 Aug 2015 02:02:29 +0200 Subject: [PATCH 155/163] Make it explicit who says what in the readme --- README.rst | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index d603ee75..082ada95 100644 --- a/README.rst +++ b/README.rst @@ -15,15 +15,17 @@ This is fork of pyzeroconf, Multicast DNS Service Discovery for Python, originally by Paul Scott-Murphy (https://github.com/paulsm/pyzeroconf), modified by William McBrine (https://github.com/wmcbrine/pyzeroconf). -This fork is used in all of my TiVo-related projects: HME for Python -(and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. -Before this, I was tracking the changes for zeroconf.py in three -separate repos. I figured I should have an authoritative source. - -Although I make changes based on my experience with TiVos, I expect that -they're generally applicable. This version also includes patches found -on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere -around the net -- not always well-documented, sorry. +The original William McBrine's fork note:: + + This fork is used in all of my TiVo-related projects: HME for Python + (and therefore HME/VLC), Network Remote, Remote Proxy, and pyTivo. + Before this, I was tracking the changes for zeroconf.py in three + separate repos. I figured I should have an authoritative source. + + Although I make changes based on my experience with TiVos, I expect that + they're generally applicable. This version also includes patches found + on the now-defunct (?) Launchpad repo of pyzeroconf, and elsewhere + around the net -- not always well-documented, sorry. Compatible with: From 023426e0f8982640f46bca3dfcd3abeee2cb832f Mon Sep 17 00:00:00 2001 From: Giovanni Harting Date: Tue, 1 Sep 2015 23:47:40 +0200 Subject: [PATCH 156/163] Support kernel versions <3.9 added catch of OSError added catch of socket.error for python2 --- zeroconf.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index e636a41f..3348fb65 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1295,12 +1295,18 @@ def new_socket(): # Volume 2"), but some BSD-derived systems require # SO_REUSEPORT to be specified explicity. Also, not all # versions of Python have SO_REUSEPORT available. + # Catch OSError and socket.error for kernel versions <3.9 because lacking + # SO_REUSEPORT support. try: reuseport = socket.SO_REUSEPORT except AttributeError: pass else: - s.setsockopt(socket.SOL_SOCKET, reuseport, 1) + try: + s.setsockopt(socket.SOL_SOCKET, reuseport, 1) + except (OSError, socket.error) as err: # OSError on python 3, socket.error on python 2 + if not err.errno == errno.ENOPROTOOPT: + raise s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) From 0b9093de863928d7f13092aaf2be1f0a33f4ead2 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 22 Sep 2015 00:05:02 +0100 Subject: [PATCH 157/163] Prepare release 0.17.4 --- README.rst | 6 ++++++ zeroconf.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 082ada95..bb862296 100644 --- a/README.rst +++ b/README.rst @@ -115,6 +115,12 @@ See examples directory for more. Changelog ========= +0.17.4 +------ + +* Fixed support for Linux kernel versions < 3.9 (thanks to Giovanni Harting + and Luckydonald, GitHub pull request #26) + 0.17.3 ------ diff --git a/zeroconf.py b/zeroconf.py index 3348fb65..2d67563e 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -38,7 +38,7 @@ __author__ = 'Paul Scott-Murphy, William McBrine' __maintainer__ = 'Jakub Stasiak ' -__version__ = '0.17.3' +__version__ = '0.17.4' __license__ = 'LGPL' From 0f46a0609931e6dc299c0473312e434e84abe7b0 Mon Sep 17 00:00:00 2001 From: Alessio Sergi Date: Sun, 29 Nov 2015 09:29:29 +0100 Subject: [PATCH 158/163] Fix multicast TTL and LOOP options on OpenBSD IP_MULTICAST_TTL and IP_MULTICAST_LOOP socket options on OpenBSD don't accept int, only unsigned char. Otherwise you will get an error: [Errno 22] Invalid argument. --- zeroconf.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/zeroconf.py b/zeroconf.py index 2d67563e..bf2402ef 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1308,8 +1308,12 @@ def new_socket(): if not err.errno == errno.ENOPROTOOPT: raise - s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255) - s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1) + # OpenBSD needs the ttl and loop values for the IP_MULTICAST_TTL and + # IP_MULTICAST_LOOP socket options as an unsigned char. + ttl = struct.pack(b'B', 255) + s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl) + loop = struct.pack(b'B', 1) + s.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, loop) s.bind(('', _MDNS_PORT)) return s From dd907f2eed3768a3c1e3889af84b5dbeb700a1e7 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 1 Dec 2015 01:40:09 +0100 Subject: [PATCH 159/163] Switch from netifaces to psutil netifaces installation on Python 3.x is broken and there doesn't seem to be any plan to release a working version on PyPI, instead of using its fork I decided to use another package providing the required information. This closes https://github.com/jstasiak/python-zeroconf/issues/31 [1] https://bitbucket.org/al45tair/netifaces/issues/13/0104-install-is-broken-on-python-3x --- requirements-dev.txt | 2 +- setup.py | 2 +- zeroconf.py | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index bc92af54..adce92b1 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,8 +7,8 @@ flake8-blind-except # Upper bound because of https://github.com/public/flake8-import-order/issues/42 flake8-import-order>=0.4.0, <0.6.0 mock -netifaces nose pep8==1.5.7 pep8-naming +psutil six diff --git a/setup.py b/setup.py index 54e05e1b..8e43bc52 100755 --- a/setup.py +++ b/setup.py @@ -52,7 +52,7 @@ ], install_requires=[ 'enum-compat', - 'netifaces', + 'psutil', 'six', ], ) diff --git a/zeroconf.py b/zeroconf.py index bf2402ef..c1601d2d 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -32,7 +32,7 @@ import time from functools import reduce -import netifaces +import psutil from six import binary_type, indexbytes, int2byte, iteritems, text_type from six.moves import xrange @@ -1271,10 +1271,11 @@ class ServiceStateChange(enum.Enum): def get_all_addresses(address_family): return list(set( - addr['addr'] - for iface in netifaces.interfaces() - for addr in netifaces.ifaddresses(iface).get(address_family, []) - if addr.get('netmask') != HOST_ONLY_NETWORK_MASK + nic.address + for iface, nics in psutil.net_if_addrs().items() + for nic in nics + if nic.family == address_family and + nic.netmask != HOST_ONLY_NETWORK_MASK )) From 30bd44f04f94a9b26622a7213dd9950ae57df21c Mon Sep 17 00:00:00 2001 From: Giles Date: Wed, 18 Nov 2015 14:26:52 -0800 Subject: [PATCH 160/163] fix issue #23 race-condition on ServiceBrowser startup --- zeroconf.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zeroconf.py b/zeroconf.py index c1601d2d..9d82ea6a 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -972,7 +972,6 @@ def __init__(self, zc, type_, handlers=None, listener=None): self.done = False self.zc.add_listener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) - self.start() self._service_state_changed = Signal() @@ -996,6 +995,8 @@ def on_change(zeroconf, service_type, name, state_change): for h in handlers: self.service_state_changed.register_handler(h) + self.start() + @property def service_state_changed(self): return self._service_state_changed.registration_interface From 6349d197b442209331a0ff8676541967f7142991 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Tue, 1 Dec 2015 09:55:50 +0100 Subject: [PATCH 161/163] Revert "Switch from netifaces to psutil" psutil doesn't seem to work on pypy3: Traceback (most recent call last): File "/home/travis/virtualenv/pypy3-2.4.0/site-packages/nose/failure.py", line 39, in runTest raise self.exc_val.with_traceback(self.tb) File "/home/travis/virtualenv/pypy3-2.4.0/site-packages/nose/loader.py", line 414, in loadTestsFromName addr.filename, addr.module) File "/home/travis/virtualenv/pypy3-2.4.0/site-packages/nose/importer.py", line 47, in importFromPath return self.importFromDir(dir_path, fqname) File "/home/travis/virtualenv/pypy3-2.4.0/site-packages/nose/importer.py", line 94, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "/home/travis/build/jstasiak/python-zeroconf/test_zeroconf.py", line 17, in import zeroconf as r File "/home/travis/build/jstasiak/python-zeroconf/zeroconf.py", line 35, in import psutil File "/home/travis/virtualenv/pypy3-2.4.0/site-packages/psutil/__init__.py", line 62, in from . import _pslinux as _psplatform File "/home/travis/virtualenv/pypy3-2.4.0/site-packages/psutil/_pslinux.py", line 23, in from . import _psutil_linux as cext ImportError: unable to load extension module '/home/travis/virtualenv/pypy3-2.4.0/site-packages/psutil/_psutil_linux.pypy3-24.so': /home/travis/virtualenv/pypy3-2.4.0/site-packages/psutil/_psutil_linux.pypy3-24.so: undefined symbol: PyModule_GetState Additionally netifaces turns out to be possible to install on Python 3, therefore making it necessary to investigate the original issue. This reverts commit dd907f2eed3768a3c1e3889af84b5dbeb700a1e7. --- requirements-dev.txt | 2 +- setup.py | 2 +- zeroconf.py | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index adce92b1..bc92af54 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -7,8 +7,8 @@ flake8-blind-except # Upper bound because of https://github.com/public/flake8-import-order/issues/42 flake8-import-order>=0.4.0, <0.6.0 mock +netifaces nose pep8==1.5.7 pep8-naming -psutil six diff --git a/setup.py b/setup.py index 8e43bc52..54e05e1b 100755 --- a/setup.py +++ b/setup.py @@ -52,7 +52,7 @@ ], install_requires=[ 'enum-compat', - 'psutil', + 'netifaces', 'six', ], ) diff --git a/zeroconf.py b/zeroconf.py index 9d82ea6a..b5dfb956 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -32,7 +32,7 @@ import time from functools import reduce -import psutil +import netifaces from six import binary_type, indexbytes, int2byte, iteritems, text_type from six.moves import xrange @@ -1272,11 +1272,10 @@ class ServiceStateChange(enum.Enum): def get_all_addresses(address_family): return list(set( - nic.address - for iface, nics in psutil.net_if_addrs().items() - for nic in nics - if nic.family == address_family and - nic.netmask != HOST_ONLY_NETWORK_MASK + addr['addr'] + for iface in netifaces.interfaces() + for addr in netifaces.ifaddresses(iface).get(address_family, []) + if addr.get('netmask') != HOST_ONLY_NETWORK_MASK )) From d5818c18dc3e64427715885b3923625a28b1623b Mon Sep 17 00:00:00 2001 From: Justin Giorgi Date: Tue, 1 Mar 2016 16:03:34 -0800 Subject: [PATCH 162/163] Added thread safety to Zeroconf.close() Zeroconf.close() immediately closed sockets which child threads may still need to finish their operations. This was causing EBADF (bad file descriptor) errors in some cases. Zeroconf.close() now blocks until all known child threads have stopped then closes the sockets safely. If a child thread was created that Zeroconf is not aware of (ie a ServiceBrowser object manually created but not registered in zeroconf.browsers) the EBADF errors may still occur. Also some of the unit tests were not in a unittest.TestCase subclass so they were not running. Added test cases to Misc TestCase and added default execution of unittest.main(). --- test_zeroconf.py | 124 +++++++++++++++++++++++++++-------------------- zeroconf.py | 26 +++++++++- 2 files changed, 96 insertions(+), 54 deletions(-) diff --git a/test_zeroconf.py b/test_zeroconf.py index 6ecf6728..7d06d754 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -151,56 +151,74 @@ def test_launch_and_close(self): rv.close() -def test_integration(): - service_added = Event() - service_removed = Event() - - type_ = "_http._tcp.local." - registration_name = "xxxyyy.%s" % type_ - - def on_service_state_change(zeroconf, service_type, state_change, name): - if name == registration_name: - if state_change is ServiceStateChange.Added: - service_added.set() - elif state_change is ServiceStateChange.Removed: - service_removed.set() - - zeroconf_browser = Zeroconf() - browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) - - zeroconf_registrar = Zeroconf() - desc = {'path': '/~paulsm/'} - info = ServiceInfo( - type_, registration_name, - socket.inet_aton("10.0.1.2"), 80, 0, 0, - desc, "ash-2.local.") - zeroconf_registrar.register_service(info) - - try: - service_added.wait(1) - assert service_added.is_set() - zeroconf_registrar.unregister_service(info) - service_removed.wait(1) - assert service_removed.is_set() - finally: - zeroconf_registrar.close() - browser.cancel() - zeroconf_browser.close() - - -def test_listener_handles_closed_socket_situation_gracefully(): - error = socket.error(socket.EBADF) - error.errno = socket.EBADF - - zeroconf = Mock() - zeroconf.socket.recvfrom.side_effect = error - - listener = Listener(zeroconf) - listener.handle_read(zeroconf.socket) - - -def test_dnstext_repr_works(): - # There was an issue on Python 3 that prevented DNSText's repr - # from working when the text was longer than 10 bytes - text = DNSText('irrelevant', None, 0, 0, b'12345678901') - repr(text) +class Misc(unittest.TestCase): + def test_integration(self): + service_added = Event() + service_removed = Event() + + type_ = "_http._tcp.local." + registration_name = "xxxyyy.%s" % type_ + + def on_service_state_change(zeroconf, service_type, state_change, name): + if name == registration_name: + if state_change is ServiceStateChange.Added: + service_added.set() + elif state_change is ServiceStateChange.Removed: + service_removed.set() + + zeroconf_browser = Zeroconf() + browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) + + zeroconf_registrar = Zeroconf() + desc = {'path': '/~paulsm/'} + info = ServiceInfo( + type_, registration_name, + socket.inet_aton("10.0.1.2"), 80, 0, 0, + desc, "ash-2.local.") + zeroconf_registrar.register_service(info) + + try: + service_added.wait(1) + assert service_added.is_set() + zeroconf_registrar.unregister_service(info) + service_removed.wait(1) + assert service_removed.is_set() + finally: + zeroconf_registrar.close() + browser.cancel() + zeroconf_browser.close() + + + def test_listener_handles_closed_socket_situation_gracefully(self): + error = socket.error(socket.EBADF) + error.errno = socket.EBADF + + zeroconf = Mock() + zeroconf.socket.recvfrom.side_effect = error + + listener = Listener(zeroconf) + listener.handle_read(zeroconf.socket) + + + def test_dnstext_repr_works(self): + # There was an issue on Python 3 that prevented DNSText's repr + # from working when the text was longer than 10 bytes + text = DNSText('irrelevant', None, 0, 0, b'12345678901') + repr(text) + + + def test_close_waits_for_threads(self): + class Dummy(object): + def add_service(self, zeroconf_obj, service_type, name): + pass + def remove_service(self, zeroconf_obj, service_type, name): + pass + + z = Zeroconf() + z.add_service_listener('_privet._tcp.local.', listener=Dummy()) + z.close() + assert not z.browsers[0].is_alive() + + +if __name__ == '__main__': + unittest.main() diff --git a/zeroconf.py b/zeroconf.py index b5dfb956..4392d449 100644 --- a/zeroconf.py +++ b/zeroconf.py @@ -1671,14 +1671,38 @@ def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): 'Should not happen, sent %d out of %d bytes' % ( bytes_sent, len(packet))) + def _check_threads(self): + """Check if any threads are still active. + + Returns: + True if the Reaper or Engine started by this object is still alive or + if any ServiceBrowser known to this object is still alive. Otherwise + False. + """ + threads = [self.reaper, self.engine] + self.browsers + for t in threads: + if isinstance(t, threading.Thread) and t.is_alive(): + return True + return False + def close(self): """Ends the background threads, and prevent this instance from - servicing further queries.""" + servicing further queries. + + This operation blocks until all threads exit. + """ global _GLOBAL_DONE if not _GLOBAL_DONE: _GLOBAL_DONE = True self.notify_all() self.engine.notify() self.unregister_all_services() + + # Wait for threads to actually die before destroying the sockets + threads_alive = self._check_threads() + while threads_alive: + time.sleep(0.01) + threads_alive = self._check_threads() + for s in [self._listen_socket] + self._respond_sockets: s.close() From c8ff62c76699c70d3194800042ac44d0e50fb1a9 Mon Sep 17 00:00:00 2001 From: Justin Giorgi Date: Thu, 10 Mar 2016 14:34:23 -0800 Subject: [PATCH 163/163] Removed unnecessary changes to tests. --- test_zeroconf.py | 133 +++++++++++++++++++++++------------------------ 1 file changed, 66 insertions(+), 67 deletions(-) diff --git a/test_zeroconf.py b/test_zeroconf.py index 7d06d754..ad1c00dc 100644 --- a/test_zeroconf.py +++ b/test_zeroconf.py @@ -151,73 +151,72 @@ def test_launch_and_close(self): rv.close() -class Misc(unittest.TestCase): - def test_integration(self): - service_added = Event() - service_removed = Event() - - type_ = "_http._tcp.local." - registration_name = "xxxyyy.%s" % type_ - - def on_service_state_change(zeroconf, service_type, state_change, name): - if name == registration_name: - if state_change is ServiceStateChange.Added: - service_added.set() - elif state_change is ServiceStateChange.Removed: - service_removed.set() - - zeroconf_browser = Zeroconf() - browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) - - zeroconf_registrar = Zeroconf() - desc = {'path': '/~paulsm/'} - info = ServiceInfo( - type_, registration_name, - socket.inet_aton("10.0.1.2"), 80, 0, 0, - desc, "ash-2.local.") - zeroconf_registrar.register_service(info) - - try: - service_added.wait(1) - assert service_added.is_set() - zeroconf_registrar.unregister_service(info) - service_removed.wait(1) - assert service_removed.is_set() - finally: - zeroconf_registrar.close() - browser.cancel() - zeroconf_browser.close() - - - def test_listener_handles_closed_socket_situation_gracefully(self): - error = socket.error(socket.EBADF) - error.errno = socket.EBADF - - zeroconf = Mock() - zeroconf.socket.recvfrom.side_effect = error - - listener = Listener(zeroconf) - listener.handle_read(zeroconf.socket) - - - def test_dnstext_repr_works(self): - # There was an issue on Python 3 that prevented DNSText's repr - # from working when the text was longer than 10 bytes - text = DNSText('irrelevant', None, 0, 0, b'12345678901') - repr(text) - - - def test_close_waits_for_threads(self): - class Dummy(object): - def add_service(self, zeroconf_obj, service_type, name): - pass - def remove_service(self, zeroconf_obj, service_type, name): - pass - - z = Zeroconf() - z.add_service_listener('_privet._tcp.local.', listener=Dummy()) - z.close() - assert not z.browsers[0].is_alive() +def test_integration(): + service_added = Event() + service_removed = Event() + + type_ = "_http._tcp.local." + registration_name = "xxxyyy.%s" % type_ + + def on_service_state_change(zeroconf, service_type, state_change, name): + if name == registration_name: + if state_change is ServiceStateChange.Added: + service_added.set() + elif state_change is ServiceStateChange.Removed: + service_removed.set() + + zeroconf_browser = Zeroconf() + browser = ServiceBrowser(zeroconf_browser, type_, [on_service_state_change]) + + zeroconf_registrar = Zeroconf() + desc = {'path': '/~paulsm/'} + info = ServiceInfo( + type_, registration_name, + socket.inet_aton("10.0.1.2"), 80, 0, 0, + desc, "ash-2.local.") + zeroconf_registrar.register_service(info) + + try: + service_added.wait(1) + assert service_added.is_set() + zeroconf_registrar.unregister_service(info) + service_removed.wait(1) + assert service_removed.is_set() + finally: + zeroconf_registrar.close() + browser.cancel() + zeroconf_browser.close() + + +def test_listener_handles_closed_socket_situation_gracefully(): + error = socket.error(socket.EBADF) + error.errno = socket.EBADF + + zeroconf = Mock() + zeroconf.socket.recvfrom.side_effect = error + + listener = Listener(zeroconf) + listener.handle_read(zeroconf.socket) + + +def test_dnstext_repr_works(): + # There was an issue on Python 3 that prevented DNSText's repr + # from working when the text was longer than 10 bytes + text = DNSText('irrelevant', None, 0, 0, b'12345678901') + repr(text) + + +def test_close_waits_for_threads(): + class Dummy(object): + def add_service(self, zeroconf_obj, service_type, name): + pass + def remove_service(self, zeroconf_obj, service_type, name): + pass + + z = Zeroconf() + z.add_service_listener('_privet._tcp.local.', listener=Dummy()) + z.close() + assert not z.browsers[0].is_alive() if __name__ == '__main__':