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