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