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

Source Code for Module dns.rdtypes.ANY.CSYNC

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