Package dns :: Package rdtypes :: Package IN :: Module WKS
[hide private]
[frames] | no frames]

Source Code for Module dns.rdtypes.IN.WKS

  1  # Copyright (C) 2003-2007, 2009-2011 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 socket 
 17  import struct 
 18   
 19  import dns.ipv4 
 20  import dns.rdata 
 21  from dns._compat import xrange 
 22   
 23  _proto_tcp = socket.getprotobyname('tcp') 
 24  _proto_udp = socket.getprotobyname('udp') 
25 26 27 -class WKS(dns.rdata.Rdata):
28 29 """WKS record 30 31 @ivar address: the address 32 @type address: string 33 @ivar protocol: the protocol 34 @type protocol: int 35 @ivar bitmap: the bitmap 36 @type bitmap: string 37 @see: RFC 1035""" 38 39 __slots__ = ['address', 'protocol', 'bitmap'] 40
41 - def __init__(self, rdclass, rdtype, address, protocol, bitmap):
42 super(WKS, self).__init__(rdclass, rdtype) 43 self.address = address 44 self.protocol = protocol 45 if not isinstance(bitmap, bytearray): 46 self.bitmap = bytearray(bitmap) 47 else: 48 self.bitmap = bitmap
49
50 - def to_text(self, origin=None, relativize=True, **kw):
51 bits = [] 52 for i in xrange(0, len(self.bitmap)): 53 byte = self.bitmap[i] 54 for j in xrange(0, 8): 55 if byte & (0x80 >> j): 56 bits.append(str(i * 8 + j)) 57 text = ' '.join(bits) 58 return '%s %d %s' % (self.address, self.protocol, text)
59 60 @classmethod
61 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
62 address = tok.get_string() 63 protocol = tok.get_string() 64 if protocol.isdigit(): 65 protocol = int(protocol) 66 else: 67 protocol = socket.getprotobyname(protocol) 68 bitmap = bytearray() 69 while 1: 70 token = tok.get().unescape() 71 if token.is_eol_or_eof(): 72 break 73 if token.value.isdigit(): 74 serv = int(token.value) 75 else: 76 if protocol != _proto_udp and protocol != _proto_tcp: 77 raise NotImplementedError("protocol must be TCP or UDP") 78 if protocol == _proto_udp: 79 protocol_text = "udp" 80 else: 81 protocol_text = "tcp" 82 serv = socket.getservbyname(token.value, protocol_text) 83 i = serv // 8 84 l = len(bitmap) 85 if l < i + 1: 86 for j in xrange(l, i + 1): 87 bitmap.append(0) 88 bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) 89 bitmap = dns.rdata._truncate_bitmap(bitmap) 90 return cls(rdclass, rdtype, address, protocol, bitmap)
91
92 - def to_wire(self, file, compress=None, origin=None):
93 file.write(dns.ipv4.inet_aton(self.address)) 94 protocol = struct.pack('!B', self.protocol) 95 file.write(protocol) 96 file.write(self.bitmap)
97 98 @classmethod
99 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
100 address = dns.ipv4.inet_ntoa(wire[current: current + 4]) 101 protocol, = struct.unpack('!B', wire[current + 4: current + 5]) 102 current += 5 103 rdlen -= 5 104 bitmap = wire[current: current + rdlen].unwrap() 105 return cls(rdclass, rdtype, address, protocol, bitmap)
106