Home | Trees | Indices | Help |
|
---|
|
1 # Copyright (C) 2003-2007, 2009 Nominum, Inc. 2 # 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. 7 # 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. 15 16 import cStringIO 17 import struct 18 19 import dns.exception 20 import dns.inet 21 import dns.rdata 22 import dns.tokenizer 2325 """An APL list item. 26 27 @ivar family: the address family (IANA address family registry) 28 @type family: int 29 @ivar negation: is this item negated? 30 @type negation: bool 31 @ivar address: the address 32 @type address: string 33 @ivar prefix: the prefix length 34 @type prefix: int 35 """ 36 37 __slots__ = ['family', 'negation', 'address', 'prefix'] 387440 self.family = family 41 self.negation = negation 42 self.address = address 43 self.prefix = prefix4446 if self.negation: 47 return "!%d:%s/%s" % (self.family, self.address, self.prefix) 48 else: 49 return "%d:%s/%s" % (self.family, self.address, self.prefix)5052 if self.family == 1: 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) 56 else: 57 address = self.address.decode('hex_codec') 58 # 59 # Truncate least significant zero bytes. 60 # 61 last = 0 62 for i in xrange(len(address) - 1, -1, -1): 63 if address[i] != chr(0): 64 last = i + 1 65 break 66 address = address[0 : last] 67 l = len(address) 68 assert l < 128 69 if self.negation: 70 l |= 0x80 71 header = struct.pack('!HBB', self.family, self.prefix, l) 72 file.write(header) 73 file.write(address)76 """APL record. 77 78 @ivar items: a list of APL items 79 @type items: list of APL_Item 80 @see: RFC 3123""" 81 82 __slots__ = ['items'] 83 87 9017092 items = [] 93 while 1: 94 (ttype, item) = tok.get() 95 if ttype == dns.tokenizer.EOL or ttype == dns.tokenizer.EOF: 96 break 97 if item[0] == '!': 98 negation = True 99 item = item[1:] 100 else: 101 negation = False 102 (family, rest) = item.split(':', 1) 103 family = int(family) 104 (address, prefix) = rest.split('/', 1) 105 prefix = int(prefix) 106 item = APLItem(family, negation, address, prefix) 107 items.append(item) 108 109 return cls(rdclass, rdtype, items)110 111 from_text = classmethod(from_text) 112 116118 items = [] 119 while 1: 120 if rdlen < 4: 121 raise dns.exception.FormError 122 header = struct.unpack('!HBB', wire[current : current + 4]) 123 afdlen = header[2] 124 if afdlen > 127: 125 negation = True 126 afdlen -= 128 127 else: 128 negation = False 129 current += 4 130 rdlen -= 4 131 if rdlen < afdlen: 132 raise dns.exception.FormError 133 address = wire[current : current + afdlen] 134 l = len(address) 135 if header[0] == 1: 136 if l < 4: 137 address += '\x00' * (4 - l) 138 address = dns.inet.inet_ntop(dns.inet.AF_INET, address) 139 elif header[0] == 2: 140 if l < 16: 141 address += '\x00' * (16 - l) 142 address = dns.inet.inet_ntop(dns.inet.AF_INET6, address) 143 else: 144 # 145 # This isn't really right according to the RFC, but it 146 # seems better than throwing an exception 147 # 148 address = address.encode('hex_codec') 149 current += afdlen 150 rdlen -= afdlen 151 item = APLItem(header[0], negation, address, header[1]) 152 items.append(item) 153 if rdlen == 0: 154 break 155 return cls(rdclass, rdtype, items)156 157 from_wire = classmethod(from_wire) 158
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Fri Jun 19 12:39:22 2009 | http://epydoc.sourceforge.net |