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