1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import cStringIO
17
18 import dns.exception
19 import dns.rdata
20 import dns.rdatatype
21 import dns.name
22
23 -class NSEC(dns.rdata.Rdata):
24 """NSEC record
25
26 @ivar next: the next name
27 @type next: dns.name.Name object
28 @ivar windows: the windowed bitmap list
29 @type windows: list of (window number, string) tuples"""
30
31 __slots__ = ['next', 'windows']
32
33 - def __init__(self, rdclass, rdtype, next, windows):
37
38 - def to_text(self, origin=None, relativize=True, **kw):
39 next = self.next.choose_relativity(origin, relativize)
40 text = ''
41 for (window, bitmap) in self.windows:
42 bits = []
43 for i in xrange(0, len(bitmap)):
44 byte = ord(bitmap[i])
45 for j in xrange(0, 8):
46 if byte & (0x80 >> j):
47 bits.append(dns.rdatatype.to_text(window * 256 + \
48 i * 8 + j))
49 text += (' ' + ' '.join(bits))
50 return '%s%s' % (next, text)
51
52 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
53 next = tok.get_name()
54 next = next.choose_relativity(origin, relativize)
55 rdtypes = []
56 while 1:
57 token = tok.get().unescape()
58 if token.is_eol_or_eof():
59 break
60 nrdtype = dns.rdatatype.from_text(token.value)
61 if nrdtype == 0:
62 raise dns.exception.SyntaxError("NSEC with bit 0")
63 if nrdtype > 65535:
64 raise dns.exception.SyntaxError("NSEC with bit > 65535")
65 rdtypes.append(nrdtype)
66 rdtypes.sort()
67 window = 0
68 octets = 0
69 prior_rdtype = 0
70 bitmap = ['\0'] * 32
71 windows = []
72 for nrdtype in rdtypes:
73 if nrdtype == prior_rdtype:
74 continue
75 prior_rdtype = nrdtype
76 new_window = nrdtype // 256
77 if new_window != window:
78 windows.append((window, ''.join(bitmap[0:octets])))
79 bitmap = ['\0'] * 32
80 window = new_window
81 offset = nrdtype % 256
82 byte = offset // 8
83 bit = offset % 8
84 octets = byte + 1
85 bitmap[byte] = chr(ord(bitmap[byte]) | (0x80 >> bit))
86 windows.append((window, ''.join(bitmap[0:octets])))
87 return cls(rdclass, rdtype, next, windows)
88
89 from_text = classmethod(from_text)
90
91 - def to_wire(self, file, compress = None, origin = None):
97
98 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
121
122 from_wire = classmethod(from_wire)
123
126
127 - def _cmp(self, other):
129