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

Source Code for Module dns.rdtypes.IN.WKS

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