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

Source Code for Module dns.rdtypes.ANY.NSEC

  1  # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license 
  2   
  3  # Copyright (C) 2004-2007, 2009-2011 Nominum, Inc. 
  4  # 
  5  # Permission to use, copy, modify, and distribute this software and its 
  6  # documentation for any purpose with or without fee is hereby granted, 
  7  # provided that the above copyright notice and this permission notice 
  8  # appear in all copies. 
  9  # 
 10  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 
 11  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
 12  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
 13  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
 14  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 15  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
 16  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 17   
 18  import struct 
 19   
 20  import dns.exception 
 21  import dns.rdata 
 22  import dns.rdatatype 
 23  import dns.name 
 24  from dns._compat import xrange 
25 26 27 -class NSEC(dns.rdata.Rdata):
28 29 """NSEC record 30 31 @ivar next: the next name 32 @type next: dns.name.Name object 33 @ivar windows: the windowed bitmap list 34 @type windows: list of (window number, string) tuples""" 35 36 __slots__ = ['next', 'windows'] 37
38 - def __init__(self, rdclass, rdtype, next, windows):
39 super(NSEC, self).__init__(rdclass, rdtype) 40 self.next = next 41 self.windows = windows
42
43 - def to_text(self, origin=None, relativize=True, **kw):
44 next = self.next.choose_relativity(origin, relativize) 45 text = '' 46 for (window, bitmap) in self.windows: 47 bits = [] 48 for i in xrange(0, len(bitmap)): 49 byte = bitmap[i] 50 for j in xrange(0, 8): 51 if byte & (0x80 >> j): 52 bits.append(dns.rdatatype.to_text(window * 256 + 53 i * 8 + j)) 54 text += (' ' + ' '.join(bits)) 55 return '{}{}'.format(next, text)
56 57 @classmethod
58 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
59 next = tok.get_name() 60 next = next.choose_relativity(origin, relativize) 61 rdtypes = [] 62 while 1: 63 token = tok.get().unescape() 64 if token.is_eol_or_eof(): 65 break 66 nrdtype = dns.rdatatype.from_text(token.value) 67 if nrdtype == 0: 68 raise dns.exception.SyntaxError("NSEC with bit 0") 69 if nrdtype > 65535: 70 raise dns.exception.SyntaxError("NSEC with bit > 65535") 71 rdtypes.append(nrdtype) 72 rdtypes.sort() 73 window = 0 74 octets = 0 75 prior_rdtype = 0 76 bitmap = bytearray(b'\0' * 32) 77 windows = [] 78 for nrdtype in rdtypes: 79 if nrdtype == prior_rdtype: 80 continue 81 prior_rdtype = nrdtype 82 new_window = nrdtype // 256 83 if new_window != window: 84 windows.append((window, bitmap[0:octets])) 85 bitmap = bytearray(b'\0' * 32) 86 window = new_window 87 offset = nrdtype % 256 88 byte = offset // 8 89 bit = offset % 8 90 octets = byte + 1 91 bitmap[byte] = bitmap[byte] | (0x80 >> bit) 92 93 windows.append((window, bitmap[0:octets])) 94 return cls(rdclass, rdtype, next, windows)
95
96 - def to_wire(self, file, compress=None, origin=None):
97 self.next.to_wire(file, None, origin) 98 for (window, bitmap) in self.windows: 99 file.write(struct.pack('!BB', window, len(bitmap))) 100 file.write(bitmap)
101 102 @classmethod
103 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
104 (next, cused) = dns.name.from_wire(wire[: current + rdlen], current) 105 current += cused 106 rdlen -= cused 107 windows = [] 108 while rdlen > 0: 109 if rdlen < 3: 110 raise dns.exception.FormError("NSEC too short") 111 window = wire[current] 112 octets = wire[current + 1] 113 if octets == 0 or octets > 32: 114 raise dns.exception.FormError("bad NSEC octets") 115 current += 2 116 rdlen -= 2 117 if rdlen < octets: 118 raise dns.exception.FormError("bad NSEC bitmap length") 119 bitmap = bytearray(wire[current: current + octets].unwrap()) 120 current += octets 121 rdlen -= octets 122 windows.append((window, bitmap)) 123 if origin is not None: 124 next = next.relativize(origin) 125 return cls(rdclass, rdtype, next, windows)
126
127 - def choose_relativity(self, origin=None, relativize=True):
129