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

# Source Code for Module dns.rdtypes.IN.NAPTR

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