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