1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import struct
19 import base64
20
21 import dns.exception
22 import dns.inet
23 import dns.name
27
28 """IPSECKEY record
29
30 @ivar precedence: the precedence for this key data
31 @type precedence: int
32 @ivar gateway_type: the gateway type
33 @type gateway_type: int
34 @ivar algorithm: the algorithm to use
35 @type algorithm: int
36 @ivar gateway: the public key
37 @type gateway: None, IPv4 address, IPV6 address, or domain name
38 @ivar key: the public key
39 @type key: string
40 @see: RFC 4025"""
41
42 __slots__ = ['precedence', 'gateway_type', 'algorithm', 'gateway', 'key']
43
44 - def __init__(self, rdclass, rdtype, precedence, gateway_type, algorithm,
45 gateway, key):
67
68 - def to_text(self, origin=None, relativize=True, **kw):
69 if self.gateway_type == 0:
70 gateway = '.'
71 elif self.gateway_type == 1:
72 gateway = self.gateway
73 elif self.gateway_type == 2:
74 gateway = self.gateway
75 elif self.gateway_type == 3:
76 gateway = str(self.gateway.choose_relativity(origin, relativize))
77 else:
78 raise ValueError('invalid gateway type')
79 return '%d %d %d %s %s' % (self.precedence, self.gateway_type,
80 self.algorithm, gateway,
81 dns.rdata._base64ify(self.key))
82
83 @classmethod
84 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
85 precedence = tok.get_uint8()
86 gateway_type = tok.get_uint8()
87 algorithm = tok.get_uint8()
88 if gateway_type == 3:
89 gateway = tok.get_name().choose_relativity(origin, relativize)
90 else:
91 gateway = tok.get_string()
92 chunks = []
93 while 1:
94 t = tok.get().unescape()
95 if t.is_eol_or_eof():
96 break
97 if not t.is_identifier():
98 raise dns.exception.SyntaxError
99 chunks.append(t.value.encode())
100 b64 = b''.join(chunks)
101 key = base64.b64decode(b64)
102 return cls(rdclass, rdtype, precedence, gateway_type, algorithm,
103 gateway, key)
104
105 - def to_wire(self, file, compress=None, origin=None):
120
121 @classmethod
122 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
123 if rdlen < 3:
124 raise dns.exception.FormError
125 header = struct.unpack('!BBB', wire[current: current + 3])
126 gateway_type = header[1]
127 current += 3
128 rdlen -= 3
129 if gateway_type == 0:
130 gateway = None
131 elif gateway_type == 1:
132 gateway = dns.inet.inet_ntop(dns.inet.AF_INET,
133 wire[current: current + 4])
134 current += 4
135 rdlen -= 4
136 elif gateway_type == 2:
137 gateway = dns.inet.inet_ntop(dns.inet.AF_INET6,
138 wire[current: current + 16])
139 current += 16
140 rdlen -= 16
141 elif gateway_type == 3:
142 (gateway, cused) = dns.name.from_wire(wire[: current + rdlen],
143 current)
144 current += cused
145 rdlen -= cused
146 else:
147 raise dns.exception.FormError('invalid IPSECKEY gateway type')
148 key = wire[current: current + rdlen].unwrap()
149 return cls(rdclass, rdtype, header[0], gateway_type, header[2],
150 gateway, key)
151