Package dns :: Package rdtypes :: Package ANY :: Module NXT
[hide private]
[frames] | no frames]

Source Code for Module dns.rdtypes.ANY.NXT

 1  # Copyright (C) 2003-2007, 2009, 2010 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 dns.exception 
17  import dns.rdata 
18  import dns.rdatatype 
19  import dns.name 
20  import dns.util 
21   
22 -class NXT(dns.rdata.Rdata):
23 """NXT record 24 25 @ivar next: the next name 26 @type next: dns.name.Name object 27 @ivar bitmap: the type bitmap 28 @type bitmap: string 29 @see: RFC 2535""" 30 31 __slots__ = ['next', 'bitmap'] 32
33 - def __init__(self, rdclass, rdtype, next, bitmap):
34 super(NXT, self).__init__(rdclass, rdtype) 35 self.next = next 36 self.bitmap = bitmap
37
38 - def to_text(self, origin=None, relativize=True, **kw):
39 next = self.next.choose_relativity(origin, relativize) 40 bits = [] 41 for i in range(0, len(self.bitmap)): 42 byte = self.bitmap[i] 43 for j in range(0, 8): 44 if byte & (0x80 >> j): 45 bits.append(dns.rdatatype.to_text(i * 8 + j)) 46 text = ' '.join(bits) 47 return '%s %s' % (next, text)
48
49 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
50 next = tok.get_name() 51 next = next.choose_relativity(origin, relativize) 52 bitmap = bytearray(32) 53 while 1: 54 token = tok.get().unescape() 55 if token.is_eol_or_eof(): 56 break 57 if token.value.isdigit(): 58 nrdtype = int(token.value) 59 else: 60 nrdtype = dns.rdatatype.from_text(token.value) 61 if nrdtype == 0: 62 raise dns.exception.SyntaxError("NXT with bit 0") 63 if nrdtype > 127: 64 raise dns.exception.SyntaxError("NXT with bit > 127") 65 i = nrdtype // 8 66 bitmap[i] = bitmap[i] | (0x80 >> (nrdtype % 8)) 67 bitmap = dns.rdata._truncate_bitmap(bitmap) 68 return cls(rdclass, rdtype, next, bitmap)
69 70 from_text = classmethod(from_text) 71
72 - def to_wire(self, file, compress = None, origin = None):
73 self.next.to_wire(file, None, origin) 74 file.write(self.bitmap)
75
76 - def to_digestable(self, origin = None):
77 return self.next.to_digestable(origin) + self.bitmap
78
79 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
80 (next, cused) = dns.name.from_wire(wire[: current + rdlen], current) 81 current += cused 82 rdlen -= cused 83 bitmap = wire[current : current + rdlen] 84 if not origin is None: 85 next = next.relativize(origin) 86 return cls(rdclass, rdtype, next, bitmap)
87 88 from_wire = classmethod(from_wire) 89
90 - def choose_relativity(self, origin = None, relativize = True):
91 self.next = self.next.choose_relativity(origin, relativize)
92
93 - def _cmp(self, other):
94 v = dns.util.cmp(self.next, other.next) 95 if v == 0: 96 v = dns.util.cmp(self.bitmap, other.bitmap) 97 return v
98