Changeset 1636

Show
Ignore:
Timestamp:
11/20/08 14:42:48 (2 months ago)
Author:
peet
Message:

rrsig prototype

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/0.4.7/lib/cxnet/cxnet/zeroconf.py

    r1619 r1636  
    155155_TYPE_AAAA = 28 
    156156_TYPE_SRV = 33 
     157_TYPE_RRSIG = 46 
     158_TYPE_DNSKEY = 48 
    157159_TYPE_ANY =  255 
    158160 
     
    184186                   _TYPE_AAAA : "quada", 
    185187                   _TYPE_SRV : "srv", 
     188                   _TYPE_RRSIG : "rrsig", 
     189                   _TYPE_DNSKEY : "dnskey", 
    186190                   _TYPE_ANY : "any" } 
    187191 
     
    338342        def __eq__(self, other): 
    339343                """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) 
    343345 
    344346        def suppressedBy(self, msg): 
     
    389391                return DNSEntry.toString(self, "record", arg) 
    390392 
     393class 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 
     430class 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 
     440class 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 
    391450class DNSAddress(DNSRecord): 
    392451        """A DNS address record""" 
     
    405464                if isinstance(other, DNSAddress): 
    406465                        return self.address == other.address and self.name == other.name 
    407                 return 0 
     466                return DNSRecord.__eq__(self,other) 
    408467 
    409468        def __repr__(self): 
     
    432491                if isinstance(other, DNSHinfo): 
    433492                        return self.cpu == other.cpu and self.os == other.os 
    434                 return 0 
     493                return DNSRecord.__eq__(self,other) 
    435494 
    436495        def __repr__(self): 
     
    453512                if isinstance(other, DNSPointer): 
    454513                        return self.alias == other.alias 
    455                 return 0 
     514                return DNSRecord.__eq__(self,other) 
    456515 
    457516        def __repr__(self): 
     
    496555                if isinstance(other, DNSText): 
    497556                        return self.text == other.text 
    498                 return 0 
     557                return DNSRecord.__eq__(self,other) 
    499558 
    500559        def __repr__(self): 
     
    526585                if isinstance(other, DNSService): 
    527586                        return self.priority == other.priority and self.weight == other.weight and self.port == other.port and self.server == other.server 
    528                 return 0 
     587                return DNSRecord.__eq__(self,other) 
    529588 
    530589        def __repr__(self): 
     
    627686                        elif info[0] == _TYPE_HINFO: 
    628687                                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()) 
    629690                        elif info[0] == _TYPE_AAAA: 
    630691                                rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16)) 
     
    719780                if record is not None: 
    720781                        if now == 0 or not record.isExpired(now): 
     782                                # FIXME sign here 
    721783                                self.answers.append((record, now)) 
     784                                if not isinstance(record,DNSSignature): 
     785                                        self.answers.append((DNSSignatureS(record.name, _TYPE_RRSIG, _CLASS_IN, record),now)) 
    722786 
    723787        def addAuthorativeAnswer(self, record): 
     
    733797                format = '!c' 
    734798                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)) 
    735805                self.size += 1 
    736806 
     
    12891359                """String representation""" 
    12901360                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) 
    12941365                if self.text is None: 
    12951366                        result += "None" 
     
    17291800if __name__ == '__main__':       
    17301801        print "Multicast DNS Service Discovery for Python, version", __version__ 
    1731         r = Zeroconf("127.0.0.1"
     1802        r = Zeroconf(("127.0.0.1",)
    17321803        print "1. Testing registration of a service..." 
    17331804        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) 
    17351808        print "   Registering service..." 
    17361809        r.registerService(info) 
     
    17401813        print "   Query done." 
    17411814        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)) 
    17431816        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..." 
    17451829        r.unregisterService(info) 
    17461830        print "   Unregister done."