1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import dns.exception
17 import dns.rdata
18 import dns.rdatatype
19 import dns.name
20 import dns.util
21
22 -class NXT(dns.rdata.Rdata):
23 """NXT record
24
25 @ivar next: the next name
26 @type next: dns.name.Name object
27 @ivar bitmap: the type bitmap
28 @type bitmap: string
29 @see: RFC 2535"""
30
31 __slots__ = ['next', 'bitmap']
32
33 - def __init__(self, rdclass, rdtype, next, bitmap):
34 super(NXT, self).__init__(rdclass, rdtype)
35 self.next = next
36 self.bitmap = bitmap
37
38 - def to_text(self, origin=None, relativize=True, **kw):
39 next = self.next.choose_relativity(origin, relativize)
40 bits = []
41 for i in range(0, len(self.bitmap)):
42 byte = self.bitmap[i]
43 for j in range(0, 8):
44 if byte & (0x80 >> j):
45 bits.append(dns.rdatatype.to_text(i * 8 + j))
46 text = ' '.join(bits)
47 return '%s %s' % (next, text)
48
49 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
50 next = tok.get_name()
51 next = next.choose_relativity(origin, relativize)
52 bitmap = bytearray(32)
53 while 1:
54 token = tok.get().unescape()
55 if token.is_eol_or_eof():
56 break
57 if token.value.isdigit():
58 nrdtype = int(token.value)
59 else:
60 nrdtype = dns.rdatatype.from_text(token.value)
61 if nrdtype == 0:
62 raise dns.exception.SyntaxError("NXT with bit 0")
63 if nrdtype > 127:
64 raise dns.exception.SyntaxError("NXT with bit > 127")
65 i = nrdtype // 8
66 bitmap[i] = bitmap[i] | (0x80 >> (nrdtype % 8))
67 bitmap = dns.rdata._truncate_bitmap(bitmap)
68 return cls(rdclass, rdtype, next, bitmap)
69
70 from_text = classmethod(from_text)
71
72 - def to_wire(self, file, compress = None, origin = None):
73 self.next.to_wire(file, None, origin)
74 file.write(self.bitmap)
75
78
79 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
80 (next, cused) = dns.name.from_wire(wire[: current + rdlen], current)
81 current += cused
82 rdlen -= cused
83 bitmap = wire[current : current + rdlen]
84 if not origin is None:
85 next = next.relativize(origin)
86 return cls(rdclass, rdtype, next, bitmap)
87
88 from_wire = classmethod(from_wire)
89
92
93 - def _cmp(self, other):
94 v = dns.util.cmp(self.next, other.next)
95 if v == 0:
96 v = dns.util.cmp(self.bitmap, other.bitmap)
97 return v
98