Package dns :: Package rdtypes :: Module dsbase
[hide private]
[frames] | no frames]

Source Code for Module dns.rdtypes.dsbase

 1  # Copyright (C) 2010, 2011 Nominum, Inc. 
 2  # 
 3  # Permission to use, copy, modify, and distribute this software and its 
 4  # documentation for any purpose with or without fee is hereby granted, 
 5  # provided that the above copyright notice and this permission notice 
 6  # appear in all copies. 
 7  # 
 8  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 
 9  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
10  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
11  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
12  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
13  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
14  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
15   
16  import struct 
17   
18  import dns.rdata 
19  import dns.rdatatype 
20  import dns.util 
21 22 -class DSBase(dns.rdata.Rdata):
23 """Base class for rdata that is like a DS record 24 25 @ivar key_tag: the key tag 26 @type key_tag: int 27 @ivar algorithm: the algorithm 28 @type algorithm: int 29 @ivar digest_type: the digest type 30 @type digest_type: int 31 @ivar digest: the digest 32 @type digest: bytes 33 @see: draft-ietf-dnsext-delegation-signer-14.txt""" 34 35 __slots__ = ['key_tag', 'algorithm', 'digest_type', 'digest'] 36
37 - def __init__(self, rdclass, rdtype, key_tag, algorithm, digest_type, 38 digest):
39 super(DSBase, self).__init__(rdclass, rdtype) 40 self.key_tag = key_tag 41 self.algorithm = algorithm 42 self.digest_type = digest_type 43 self.digest = digest
44
45 - def to_text(self, origin=None, relativize=True, **kw):
46 return '%d %d %d %s' % (self.key_tag, self.algorithm, 47 self.digest_type, 48 dns.rdata._hexify(self.digest, 49 chunksize=128))
50 51 @classmethod
52 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
53 key_tag = tok.get_uint16() 54 algorithm = tok.get_uint8() 55 digest_type = tok.get_uint8() 56 chunks = [] 57 while 1: 58 t = tok.get().unescape() 59 if t.is_eol_or_eof(): 60 break 61 if not t.is_identifier(): 62 raise dns.exception.SyntaxError 63 chunks.append(t.value) 64 digest = bytes.fromhex(''.join(chunks)) 65 return cls(rdclass, rdtype, key_tag, algorithm, digest_type, 66 digest)
67
68 - def to_wire(self, file, compress = None, origin = None):
69 header = struct.pack("!HBB", self.key_tag, self.algorithm, 70 self.digest_type) 71 file.write(header) 72 file.write(self.digest)
73 74 @classmethod
75 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
76 header = struct.unpack("!HBB", wire[current : current + 4]) 77 current += 4 78 rdlen -= 4 79 digest = wire[current : current + rdlen].unwrap() 80 return cls(rdclass, rdtype, header[0], header[1], header[2], digest)
81
82 - def _cmp(self, other):
83 hs = struct.pack("!HBB", self.key_tag, self.algorithm, 84 self.digest_type) 85 ho = struct.pack("!HBB", other.key_tag, other.algorithm, 86 other.digest_type) 87 v = dns.util.cmp(hs, ho) 88 if v == 0: 89 v = dns.util.cmp(self.digest, other.digest) 90 return v
91