1
# Copyright (C) 2003-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.
24
class APLItem(object):
27
@ivar family: the address family (IANA address family registry)
29
@ivar negation: is this item negated?
31
@ivar address: the address
33
@ivar prefix: the prefix length
37
__slots__ = ['family', 'negation', 'address', 'prefix']
39
def __init__(self, family, negation, address, prefix):
41
self.negation = negation
42
self.address = address
47
return "!%d:%s/%s" % (self.family, self.address, self.prefix)
49
return "%d:%s/%s" % (self.family, self.address, self.prefix)
51
def to_wire(self, file):
53
address = dns.inet.inet_pton(dns.inet.AF_INET, self.address)
54
elif self.family == 2:
55
address = dns.inet.inet_pton(dns.inet.AF_INET6, self.address)
57
address = self.address.decode('hex_codec')
59
# Truncate least significant zero bytes.
62
for i in xrange(len(address) - 1, -1, -1):
63
if address[i] != chr(0):
66
address = address[0 : last]
71
header = struct.pack('!HBB', self.family, self.prefix, l)
75
class APL(dns.rdata.Rdata):
78
@ivar items: a list of APL items
79
@type items: list of APL_Item
84
def __init__(self, rdclass, rdtype, items):
85
super(APL, self).__init__(rdclass, rdtype)
88
def to_text(self, origin=None, relativize=True, **kw):
89
return ' '.join(map(lambda x: str(x), self.items))
91
def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
94
token = tok.get().unescape()
95
if token.is_eol_or_eof():
103
(family, rest) = item.split(':', 1)
105
(address, prefix) = rest.split('/', 1)
107
item = APLItem(family, negation, address, prefix)
110
return cls(rdclass, rdtype, items)
112
from_text = classmethod(from_text)
114
def to_wire(self, file, compress = None, origin = None):
115
for item in self.items:
118
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
122
raise dns.exception.FormError
123
header = struct.unpack('!HBB', wire[current : current + 4])
133
raise dns.exception.FormError
134
address = wire[current : current + afdlen]
138
address += '\x00' * (4 - l)
139
address = dns.inet.inet_ntop(dns.inet.AF_INET, address)
142
address += '\x00' * (16 - l)
143
address = dns.inet.inet_ntop(dns.inet.AF_INET6, address)
146
# This isn't really right according to the RFC, but it
147
# seems better than throwing an exception
149
address = address.encode('hex_codec')
152
item = APLItem(header[0], negation, address, header[1])
156
return cls(rdclass, rdtype, items)
158
from_wire = classmethod(from_wire)
160
def _cmp(self, other):
161
f = cStringIO.StringIO()
170
return cmp(wire1, wire2)