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

# Source Code for Module dns.rdtypes.ANY.CSYNC

      1  # Copyright (C) 2004-2007, 2009-2011, 2016 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 struct
     17
     18  import dns.exception
     19  import dns.rdata
     20  import dns.rdatatype
     21  import dns.name
     22  from dns._compat import xrange
     23
     24 -class CSYNC(dns.rdata.Rdata):
     25
     26      """CSYNC record
     27
     28      @ivar serial: the SOA serial number
     29      @type serial: int
     30      @ivar flags: the CSYNC flags
     31      @type flags: int
     32      @ivar windows: the windowed bitmap list
     33      @type windows: list of (window number, string) tuples"""
     34
     35      __slots__ = ['serial', 'flags', 'windows']
     36
     37 -    def __init__(self, rdclass, rdtype, serial, flags, windows):
     38          super(CSYNC, self).__init__(rdclass, rdtype)
     39          self.serial = serial
     40          self.flags = flags
     41          self.windows = windows
     42
     43 -    def to_text(self, origin=None, relativize=True, **kw):
     44          text = ''
     45          for (window, bitmap) in self.windows:
     46              bits = []
     47              for i in xrange(0, len(bitmap)):
     48                  byte = bitmap[i]
     49                  for j in xrange(0, 8):
     50                      if byte & (0x80 >> j):
     51                          bits.append(dns.rdatatype.to_text(window * 256 +
     52                                                            i * 8 + j))
     53              text += (' ' + ' '.join(bits))
     54          return '%d %d%s' % (self.serial, self.flags, text)
     55
     56      @classmethod
     57 -    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
     58          serial = tok.get_uint32()
     59          flags = tok.get_uint16()
     60          rdtypes = []
     61          while 1:
     62              token = tok.get().unescape()
     63              if token.is_eol_or_eof():
     64                  break
     65              nrdtype = dns.rdatatype.from_text(token.value)
     66              if nrdtype == 0:
     67                  raise dns.exception.SyntaxError("CSYNC with bit 0")
     68              if nrdtype > 65535:
     69                  raise dns.exception.SyntaxError("CSYNC with bit > 65535")
     70              rdtypes.append(nrdtype)
     71          rdtypes.sort()
     72          window = 0
     73          octets = 0
     74          prior_rdtype = 0
     75          bitmap = bytearray(b'\0' * 32)
     76          windows = []
     77          for nrdtype in rdtypes:
     78              if nrdtype == prior_rdtype:
     79                  continue
     80              prior_rdtype = nrdtype
     81              new_window = nrdtype // 256
     82              if new_window != window:
     83                  windows.append((window, bitmap[0:octets]))
     84                  bitmap = bytearray(b'\0' * 32)
     85                  window = new_window
     86              offset = nrdtype % 256
     87              byte = offset // 8
     88              bit = offset % 8
     89              octets = byte + 1
     90              bitmap[byte] = bitmap[byte] | (0x80 >> bit)
     91
     92          windows.append((window, bitmap[0:octets]))
     93          return cls(rdclass, rdtype, serial, flags, windows)
     94
     95 -    def to_wire(self, file, compress=None, origin=None):
     96          file.write(struct.pack('!IH', self.serial, self.flags))
     97          for (window, bitmap) in self.windows:
     98              file.write(struct.pack('!BB', window, len(bitmap)))
     99              file.write(bitmap)
    100
    101      @classmethod
    102 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
    103          if rdlen < 6:
    104              raise dns.exception.FormError("CSYNC too short")
    105          (serial, flags) = struct.unpack("!IH", wire[current: current + 6])
    106          current += 6
    107          rdlen -= 6
    108          windows = []
    109          while rdlen > 0:
    110              if rdlen < 3:
    111                  raise dns.exception.FormError("CSYNC too short")
    112              window = wire[current]
    113              octets = wire[current + 1]
    114              if octets == 0 or octets > 32:
    115                  raise dns.exception.FormError("bad CSYNC octets")
    116              current += 2
    117              rdlen -= 2
    118              if rdlen < octets:
    119                  raise dns.exception.FormError("bad CSYNC bitmap length")
    120              bitmap = bytearray(wire[current: current + octets].unwrap())
    121              current += octets
    122              rdlen -= octets
    123              windows.append((window, bitmap))
    124          return cls(rdclass, rdtype, serial, flags, windows)
    125