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

Source Code for Module dns.rdtypes.IN.NAPTR

  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 struct 
 19   
 20  import dns.exception 
 21  import dns.name 
 22  import dns.rdata 
 23  from dns._compat import xrange, text_type 
24 25 26 -def _write_string(file, s):
27 l = len(s) 28 assert l < 256 29 file.write(struct.pack('!B', l)) 30 file.write(s)
31
32 33 -def _sanitize(value):
34 if isinstance(value, text_type): 35 return value.encode() 36 return value
37
38 39 -class NAPTR(dns.rdata.Rdata):
40 41 """NAPTR record 42 43 @ivar order: order 44 @type order: int 45 @ivar preference: preference 46 @type preference: int 47 @ivar flags: flags 48 @type flags: string 49 @ivar service: service 50 @type service: string 51 @ivar regexp: regular expression 52 @type regexp: string 53 @ivar replacement: replacement name 54 @type replacement: dns.name.Name object 55 @see: RFC 3403""" 56 57 __slots__ = ['order', 'preference', 'flags', 'service', 'regexp', 58 'replacement'] 59
60 - def __init__(self, rdclass, rdtype, order, preference, flags, service, 61 regexp, replacement):
62 super(NAPTR, self).__init__(rdclass, rdtype) 63 self.flags = _sanitize(flags) 64 self.service = _sanitize(service) 65 self.regexp = _sanitize(regexp) 66 self.order = order 67 self.preference = preference 68 self.replacement = replacement
69
70 - def to_text(self, origin=None, relativize=True, **kw):
71 replacement = self.replacement.choose_relativity(origin, relativize) 72 return '%d %d "%s" "%s" "%s" %s' % \ 73 (self.order, self.preference, 74 dns.rdata._escapify(self.flags), 75 dns.rdata._escapify(self.service), 76 dns.rdata._escapify(self.regexp), 77 replacement)
78 79 @classmethod
80 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
91
92 - def to_wire(self, file, compress=None, origin=None):
93 two_ints = struct.pack("!HH", self.order, self.preference) 94 file.write(two_ints) 95 _write_string(file, self.flags) 96 _write_string(file, self.service) 97 _write_string(file, self.regexp) 98 self.replacement.to_wire(file, compress, origin)
99 100 @classmethod
101 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
102 (order, preference) = struct.unpack('!HH', wire[current: current + 4]) 103 current += 4 104 rdlen -= 4 105 strings = [] 106 for i in xrange(3): 107 l = wire[current] 108 current += 1 109 rdlen -= 1 110 if l > rdlen or rdlen < 0: 111 raise dns.exception.FormError 112 s = wire[current: current + l].unwrap() 113 current += l 114 rdlen -= l 115 strings.append(s) 116 (replacement, cused) = dns.name.from_wire(wire[: current + rdlen], 117 current) 118 if cused != rdlen: 119 raise dns.exception.FormError 120 if origin is not None: 121 replacement = replacement.relativize(origin) 122 return cls(rdclass, rdtype, order, preference, strings[0], strings[1], 123 strings[2], replacement)
124
125 - def choose_relativity(self, origin=None, relativize=True):
128