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