Changeset 1636
- Timestamp:
- 11/20/08 14:42:48 (2 months ago)
- Files:
-
- branches/0.4.7/lib/cxnet/cxnet/zeroconf.py (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/0.4.7/lib/cxnet/cxnet/zeroconf.py
r1619 r1636 155 155 _TYPE_AAAA = 28 156 156 _TYPE_SRV = 33 157 _TYPE_RRSIG = 46 158 _TYPE_DNSKEY = 48 157 159 _TYPE_ANY = 255 158 160 … … 184 186 _TYPE_AAAA : "quada", 185 187 _TYPE_SRV : "srv", 188 _TYPE_RRSIG : "rrsig", 189 _TYPE_DNSKEY : "dnskey", 186 190 _TYPE_ANY : "any" } 187 191 … … 338 342 def __eq__(self, other): 339 343 """Tests equality as per DNSRecord""" 340 if isinstance(other, DNSRecord): 341 return DNSEntry.__eq__(self, other) 342 return 0 344 return DNSEntry.__eq__(self, other) 343 345 344 346 def suppressedBy(self, msg): … … 389 391 return DNSEntry.toString(self, "record", arg) 390 392 393 class DNSSignature(DNSRecord): 394 """An abstract DNS signature record class""" 395 396 def __init__(self, name, type, clazz, ttl): 397 DNSRecord.__init__(self, name, type, clazz, ttl) 398 self.type_covered = _TYPE_ANY 399 # we use private algorithm type 253 400 # according to RFC 2535, Section 11 401 self.algorithm = 253 402 self.labels = 0 403 self.original_ttl = 0 404 self.expiration = 0 405 self.inception = 0 406 self.tag = 0 407 self.signer = "none" 408 self.signature = "none" 409 410 def write(self, out): 411 # write header 412 out.writeShort(self.type_covered) 413 out.writeUChar(self.algorithm) 414 out.writeUChar(self.labels) 415 out.writeInt(self.original_ttl) 416 out.writeInt(self.expiration) 417 out.writeInt(self.inception) 418 out.writeShort(self.tag) 419 out.writeName(self.signer) 420 out.writeUChar(len(self.signature)) 421 out.writeString(self.signature,len(self.signature)) 422 423 def __eq__(self,other): 424 if isinstance(other, DNSSignature): 425 return self.type_covered == other.type_covered and self.signer == other.signer and self.signature == other.signature 426 return 0 427 def __repr__(self): 428 return "RRSIG: [%s] %s ( %s )" % (_TYPES[self.type_covered], self.signer, self.signature) 429 430 class DNSSignatureI(DNSSignature): 431 """Create a DNSRecord from a signature""" 432 433 def __init__(self, name, type, clazz, ttl, header, signer, signature): 434 DNSSignature.__init__(self, name, type, clazz, ttl) 435 (self.type_covered,self.algorithm,self.labels,self.original_ttl,self.expiration,self.inception,self.tag) = \ 436 struct.unpack("!HBBIIIH",header) 437 self.signer = signer 438 self.signature = signature 439 440 class DNSSignatureS(DNSSignature): 441 """Create signature from a DNSRecord""" 442 443 def __init__(self, name, type, clazz, record): 444 DNSSignature.__init__(self, name, type, clazz, record.ttl) 445 self.type_covered = record.type 446 self.original_ttl = record.ttl 447 self.signer = record.name 448 self.signature = "BALADALA" 449 391 450 class DNSAddress(DNSRecord): 392 451 """A DNS address record""" … … 405 464 if isinstance(other, DNSAddress): 406 465 return self.address == other.address and self.name == other.name 407 return 0466 return DNSRecord.__eq__(self,other) 408 467 409 468 def __repr__(self): … … 432 491 if isinstance(other, DNSHinfo): 433 492 return self.cpu == other.cpu and self.os == other.os 434 return 0493 return DNSRecord.__eq__(self,other) 435 494 436 495 def __repr__(self): … … 453 512 if isinstance(other, DNSPointer): 454 513 return self.alias == other.alias 455 return 0514 return DNSRecord.__eq__(self,other) 456 515 457 516 def __repr__(self): … … 496 555 if isinstance(other, DNSText): 497 556 return self.text == other.text 498 return 0557 return DNSRecord.__eq__(self,other) 499 558 500 559 def __repr__(self): … … 526 585 if isinstance(other, DNSService): 527 586 return self.priority == other.priority and self.weight == other.weight and self.port == other.port and self.server == other.server 528 return 0587 return DNSRecord.__eq__(self,other) 529 588 530 589 def __repr__(self): … … 627 686 elif info[0] == _TYPE_HINFO: 628 687 rec = DNSHinfo(domain, info[0], info[1], info[2], self.readCharacterString(), self.readCharacterString()) 688 elif info[0] == _TYPE_RRSIG: 689 rec = DNSSignatureI(domain, info[0], info[1], info[2],self.readString(18),self.readName(),self.readCharacterString()) 629 690 elif info[0] == _TYPE_AAAA: 630 691 rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16)) … … 719 780 if record is not None: 720 781 if now == 0 or not record.isExpired(now): 782 # FIXME sign here 721 783 self.answers.append((record, now)) 784 if not isinstance(record,DNSSignature): 785 self.answers.append((DNSSignatureS(record.name, _TYPE_RRSIG, _CLASS_IN, record),now)) 722 786 723 787 def addAuthorativeAnswer(self, record): … … 733 797 format = '!c' 734 798 self.data.append(struct.pack(format, chr(value))) 799 self.size += 1 800 801 def writeUChar(self, value): 802 """Writes an unsigned char to the packet""" 803 format = '!B' 804 self.data.append(struct.pack(format, value)) 735 805 self.size += 1 736 806 … … 1289 1359 """String representation""" 1290 1360 addr = self.getAddress() 1291 if addr: 1292 addr = socket.inet_ntoa(addr) 1293 result = "service[%s,%s:%s," % (self.name, addr, self.port) 1361 addrl = [] 1362 for i in addr: 1363 addrl.append(socket.inet_ntoa(i)) 1364 result = "service[%s,%s:%s," % (self.name, addrl, self.port) 1294 1365 if self.text is None: 1295 1366 result += "None" … … 1729 1800 if __name__ == '__main__': 1730 1801 print "Multicast DNS Service Discovery for Python, version", __version__ 1731 r = Zeroconf( "127.0.0.1")1802 r = Zeroconf(("127.0.0.1",)) 1732 1803 print "1. Testing registration of a service..." 1733 1804 desc = {'version':'0.10','a':'test value', 'b':'another value'} 1734 info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc) 1805 n = "ame._http._tcp.local." 1806 d = "_http._tcp.local." 1807 info = ServiceInfo(d, n, (socket.inet_aton("127.0.0.1"),socket.inet_aton("127.0.0.2")), 1234, 0, 0, desc) 1735 1808 print " Registering service..." 1736 1809 r.registerService(info) … … 1740 1813 print " Query done." 1741 1814 print "3. Testing query of own service..." 1742 print " Getting self:", str(r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local."))1815 print " Getting self:", repr(r.getServiceInfo(d, n)) 1743 1816 print " Query done." 1744 print "4. Testing unregister of service information..." 1817 print "4. Testing cache..." 1818 # FIXME: what with names with spaces in it? 1819 print " Get by name:" 1820 for i in r.cache.entriesWithName(n): 1821 print "\t >>", i 1822 print " Get all cache:" 1823 for i in r.cache.entries(): 1824 print "\t", i.name, i 1825 print " Get by details:" 1826 print "\t",r.cache.getByDetails(n,_TYPE_A,_CLASS_IN) 1827 print r.cache.cache 1828 print "5. Testing unregister of service information..." 1745 1829 r.unregisterService(info) 1746 1830 print " Unregister done."
