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 import dns.util
22
23 _proto_tcp = socket.getprotobyname('tcp')
24 _proto_udp = socket.getprotobyname('udp')
25
26 -class WKS(dns.rdata.Rdata):
27 """WKS record
28
29 @ivar address: the address
30 @type address: string
31 @ivar protocol: the protocol
32 @type protocol: int
33 @ivar bitmap: the bitmap
34 @type bitmap: bytes
35 @see: RFC 1035"""
36
37 __slots__ = ['address', 'protocol', 'bitmap']
38
39 - def __init__(self, rdclass, rdtype, address, protocol, bitmap):
44
45 - def to_text(self, origin=None, relativize=True, **kw):
46 bits = []
47 for i in range(0, len(self.bitmap)):
48 byte = self.bitmap[i]
49 for j in range(0, 8):
50 if byte & (0x80 >> j):
51 bits.append(str(i * 8 + j))
52 text = ' '.join(bits)
53 return '%s %d %s' % (self.address, self.protocol, text)
54
55 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
56 address = tok.get_string()
57 protocol = tok.get_string()
58 if protocol.isdigit():
59 protocol = int(protocol)
60 else:
61 protocol = socket.getprotobyname(protocol)
62 bitmap = bytearray(32 * 256)
63 while 1:
64 token = tok.get().unescape()
65 if token.is_eol_or_eof():
66 break
67 if token.value.isdigit():
68 serv = int(token.value)
69 else:
70 if protocol != _proto_udp and protocol != _proto_tcp:
71 raise NotImplementedError("protocol must be TCP or UDP")
72 if protocol == _proto_udp:
73 protocol_text = "udp"
74 else:
75 protocol_text = "tcp"
76 serv = socket.getservbyname(token.value, protocol_text)
77 i = serv // 8
78 bitmap[i] = bitmap[i] | (0x80 >> (serv % 8))
79 bitmap = dns.rdata._truncate_bitmap(bitmap)
80 return cls(rdclass, rdtype, address, protocol, bitmap)
81
82 from_text = classmethod(from_text)
83
84 - def to_wire(self, file, compress = None, origin = None):
89
90 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
97
98 from_wire = classmethod(from_wire)
99
100 - def _cmp(self, other):
111