1 # Copyright (C) 2006, 2007, 2009, 2010 Nominum, Inc.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 class IPSECKEY(dns.rdata.Rdata):
26 @ivar precedence: the precedence for this key data
28 @ivar gateway_type: the gateway type
29 @type gateway_type: int
30 @ivar algorithm: the algorithm to use
32 @ivar gateway: the public key
33 @type gateway: None, IPv4 address, IPV6 address, or domain name
34 @ivar key: the public key
38 __slots__ = ['precedence', 'gateway_type', 'algorithm', 'gateway', 'key']
40 def __init__(self, rdclass, rdtype, precedence, gateway_type, algorithm,
42 super(IPSECKEY, self).__init__(rdclass, rdtype)
44 if gateway != '.' and not gateway is None:
45 raise SyntaxError('invalid gateway for gateway type 0')
47 elif gateway_type == 1:
49 junk = dns.inet.inet_pton(dns.inet.AF_INET, gateway)
50 elif gateway_type == 2:
52 junk = dns.inet.inet_pton(dns.inet.AF_INET6, gateway)
53 elif gateway_type == 3:
56 raise SyntaxError('invalid IPSECKEY gateway type: %d' % gateway_type)
57 self.precedence = precedence
58 self.gateway_type = gateway_type
59 self.algorithm = algorithm
60 self.gateway = gateway
63 def to_text(self, origin=None, relativize=True, **kw):
64 if self.gateway_type == 0:
66 elif self.gateway_type == 1:
67 gateway = self.gateway
68 elif self.gateway_type == 2:
69 gateway = self.gateway
70 elif self.gateway_type == 3:
71 gateway = str(self.gateway.choose_relativity(origin, relativize))
73 raise ValueError('invalid gateway type')
74 return '%d %d %d %s %s' % (self.precedence, self.gateway_type,
75 self.algorithm, gateway,
76 dns.rdata._base64ify(self.key))
78 def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
79 precedence = tok.get_uint8()
80 gateway_type = tok.get_uint8()
81 algorithm = tok.get_uint8()
83 gateway = tok.get_name().choose_relativity(origin, relativize)
85 gateway = tok.get_string()
88 t = tok.get().unescape()
91 if not t.is_identifier():
92 raise dns.exception.SyntaxError
93 chunks.append(t.value)
95 key = b64.decode('base64_codec')
96 return cls(rdclass, rdtype, precedence, gateway_type, algorithm,
99 from_text = classmethod(from_text)
101 def to_wire(self, file, compress = None, origin = None):
102 header = struct.pack("!BBB", self.precedence, self.gateway_type,
105 if self.gateway_type == 0:
107 elif self.gateway_type == 1:
108 file.write(dns.inet.inet_pton(dns.inet.AF_INET, self.gateway))
109 elif self.gateway_type == 2:
110 file.write(dns.inet.inet_pton(dns.inet.AF_INET6, self.gateway))
111 elif self.gateway_type == 3:
112 self.gateway.to_wire(file, None, origin)
114 raise ValueError('invalid gateway type')
117 def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
119 raise dns.exception.FormError
120 header = struct.unpack('!BBB', wire[current : current + 3])
121 gateway_type = header[1]
124 if gateway_type == 0:
126 elif gateway_type == 1:
127 gateway = dns.inet.inet_ntop(dns.inet.AF_INET,
128 wire[current : current + 4])
131 elif gateway_type == 2:
132 gateway = dns.inet.inet_ntop(dns.inet.AF_INET6,
133 wire[current : current + 16])
136 elif gateway_type == 3:
137 (gateway, cused) = dns.name.from_wire(wire[: current + rdlen],
142 raise dns.exception.FormError('invalid IPSECKEY gateway type')
143 key = wire[current : current + rdlen]
144 return cls(rdclass, rdtype, header[0], gateway_type, header[2],
147 from_wire = classmethod(from_wire)
149 def _cmp(self, other):
150 f = cStringIO.StringIO()
159 return cmp(wire1, wire2)