dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

NSEC.py
Go to the documentation of this file.
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 
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):
128  self.next = self.next.choose_relativity(origin, relativize)
dns.exception.FormError
Definition: exception.py:109
dns.exception.SyntaxError
Definition: exception.py:113
dns.rdtypes.ANY.NSEC.NSEC.next
next
Definition: NSEC.py:40
dns.rdtypes.ANY.NSEC.NSEC.choose_relativity
def choose_relativity(self, origin=None, relativize=True)
Definition: NSEC.py:127
dns.name.from_wire
def from_wire(message, current)
Definition: name.py:945
dns.rdtypes.ANY.NSEC.NSEC.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: NSEC.py:96
dns.rdtypes.ANY.NSEC.NSEC.from_wire
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None)
Definition: NSEC.py:103
dns.rdtypes.ANY.NSEC.NSEC.windows
windows
Definition: NSEC.py:41
dns.name
Definition: name.py:1
dns.rdtypes.ANY.NSEC.NSEC.from_text
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True)
Definition: NSEC.py:58
dns.rdata
Definition: rdata.py:1
dns.rdatatype
Definition: rdatatype.py:1
dns.rdatatype.to_text
def to_text(value)
Definition: rdatatype.py:219
dns.rdatatype.from_text
def from_text(text)
Definition: rdatatype.py:193
dns._compat
Definition: _compat.py:1
dns.rdtypes.ANY.NSEC.NSEC.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: NSEC.py:43
dns._compat.xrange
xrange
Definition: _compat.py:11
dns.exception
Definition: exception.py:1
dns.rdtypes.ANY.NSEC.NSEC
Definition: NSEC.py:27
dns.rdtypes.ANY.NSEC.NSEC.__init__
def __init__(self, rdclass, rdtype, next, windows)
Definition: NSEC.py:38
dns.rdata.Rdata
Definition: rdata.py:95