dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

NAPTR.py
Go to the documentation of this file.
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 
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,
77  replacement)
78 
79  @classmethod
80  def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
81  order = tok.get_uint16()
82  preference = tok.get_uint16()
83  flags = tok.get_string()
84  service = tok.get_string()
85  regexp = tok.get_string()
86  replacement = tok.get_name()
87  replacement = replacement.choose_relativity(origin, relativize)
88  tok.get_eol()
89  return cls(rdclass, rdtype, order, preference, flags, service,
90  regexp, replacement)
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:
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:
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):
126  self.replacement = self.replacement.choose_relativity(origin,
127  relativize)
dns.exception.FormError
Definition: exception.py:109
dns.rdtypes.IN.NAPTR.NAPTR.from_text
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True)
Definition: NAPTR.py:80
dns.rdtypes.IN.NAPTR.NAPTR.regexp
regexp
Definition: NAPTR.py:64
dns.rdtypes.IN.NAPTR.NAPTR.choose_relativity
def choose_relativity(self, origin=None, relativize=True)
Definition: NAPTR.py:125
dns.rdtypes.IN.NAPTR.NAPTR.service
service
Definition: NAPTR.py:63
dns.rdtypes.IN.NAPTR.NAPTR.replacement
replacement
Definition: NAPTR.py:67
dns.rdtypes.IN.NAPTR.NAPTR.flags
flags
Definition: NAPTR.py:62
dns.name.from_wire
def from_wire(message, current)
Definition: name.py:945
dns.rdtypes.IN.NAPTR.NAPTR.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: NAPTR.py:92
dns.rdata._escapify
def _escapify(qstring)
Definition: rdata.py:65
dns.rdtypes.IN.NAPTR.NAPTR.from_wire
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None)
Definition: NAPTR.py:101
dns.name
Definition: name.py:1
dns.rdtypes.IN.NAPTR.NAPTR.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: NAPTR.py:70
dns.rdtypes.IN.NAPTR.NAPTR.__init__
def __init__(self, rdclass, rdtype, order, preference, flags, service, regexp, replacement)
Definition: NAPTR.py:60
dns.rdtypes.IN.NAPTR._write_string
def _write_string(file, s)
Definition: NAPTR.py:26
dns.rdata
Definition: rdata.py:1
dns.rdtypes.IN.NAPTR._sanitize
def _sanitize(value)
Definition: NAPTR.py:33
dns.rdtypes.IN.NAPTR.NAPTR.preference
preference
Definition: NAPTR.py:66
dns.rdtypes.IN.NAPTR.NAPTR
Definition: NAPTR.py:39
dns._compat
Definition: _compat.py:1
dns.rdtypes.IN.NAPTR.NAPTR.order
order
Definition: NAPTR.py:65
dns._compat.xrange
xrange
Definition: _compat.py:11
dns.exception
Definition: exception.py:1
dns.rdata.Rdata
Definition: rdata.py:95