1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import socket
17 import struct
18
19 import dns.ipv4
20 import dns.rdata
21
22 _proto_tcp = socket.getprotobyname('tcp')
23 _proto_udp = socket.getprotobyname('udp')
24
25 -class WKS(dns.rdata.Rdata):
26 """WKS record
27
28 @ivar address: the address
29 @type address: string
30 @ivar protocol: the protocol
31 @type protocol: int
32 @ivar bitmap: the bitmap
33 @type bitmap: string
34 @see: RFC 1035"""
35
36 __slots__ = ['address', 'protocol', 'bitmap']
37
38 - def __init__(self, rdclass, rdtype, address, protocol, bitmap):
43
44 - def to_text(self, origin=None, relativize=True, **kw):
45 bits = []
46 for i in xrange(0, len(self.bitmap)):
47 byte = ord(self.bitmap[i])
48 for j in xrange(0, 8):
49 if byte & (0x80 >> j):
50 bits.append(str(i * 8 + j))
51 text = ' '.join(bits)
52 return '%s %d %s' % (self.address, self.protocol, text)
53
54 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
55 address = tok.get_string()
56 protocol = tok.get_string()
57 if protocol.isdigit():
58 protocol = int(protocol)
59 else:
60 protocol = socket.getprotobyname(protocol)
61 bitmap = []
62 while 1:
63 token = tok.get().unescape()
64 if token.is_eol_or_eof():
65 break
66 if token.value.isdigit():
67 serv = int(token.value)
68 else:
69 if protocol != _proto_udp and protocol != _proto_tcp:
70 raise NotImplementedError("protocol must be TCP or UDP")
71 if protocol == _proto_udp:
72 protocol_text = "udp"
73 else:
74 protocol_text = "tcp"
75 serv = socket.getservbyname(token.value, protocol_text)
76 i = serv // 8
77 l = len(bitmap)
78 if l < i + 1:
79 for j in xrange(l, i + 1):
80 bitmap.append('\x00')
81 bitmap[i] = chr(ord(bitmap[i]) | (0x80 >> (serv % 8)))
82 bitmap = dns.rdata._truncate_bitmap(bitmap)
83 return cls(rdclass, rdtype, address, protocol, bitmap)
84
85 from_text = classmethod(from_text)
86
87 - def to_wire(self, file, compress = None, origin = None):
92
93 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
100
101 from_wire = classmethod(from_wire)
102
103 - def _cmp(self, other):
104 sa = dns.ipv4.inet_aton(self.address)
105 oa = dns.ipv4.inet_aton(other.address)
106 v = cmp(sa, oa)
107 if v == 0:
108 sp = struct.pack('!B', self.protocol)
109 op = struct.pack('!B', other.protocol)
110 v = cmp(sp, op)
111 if v == 0:
112 v = cmp(self.bitmap, other.bitmap)
113 return v
114