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

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

     1  # Copyright (C) 2003-2007, 2009, 2010 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.rdata
    20  import dns.name
    21
    22 -class SRV(dns.rdata.Rdata):
    23      """SRV record
    24
    25      @ivar priority: the priority
    26      @type priority: int
    27      @ivar weight: the weight
    28      @type weight: int
    29      @ivar port: the port of the service
    30      @type port: int
    31      @ivar target: the target host
    32      @type target: dns.name.Name object
    33      @see: RFC 2782"""
    34
    35      __slots__ = ['priority', 'weight', 'port', 'target']
    36
    37 -    def __init__(self, rdclass, rdtype, priority, weight, port, target):
    38          super(SRV, self).__init__(rdclass, rdtype)
    39          self.priority = priority
    40          self.weight = weight
    41          self.port = port
    42          self.target = target
    43
    44 -    def to_text(self, origin=None, relativize=True, **kw):
    45          target = self.target.choose_relativity(origin, relativize)
    46          return '%d %d %d %s' % (self.priority, self.weight, self.port,
    47                                  target)
    48
    49 -    def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
    50          priority = tok.get_uint16()
    51          weight = tok.get_uint16()
    52          port = tok.get_uint16()
    53          target = tok.get_name(None)
    54          target = target.choose_relativity(origin, relativize)
    55          tok.get_eol()
    56          return cls(rdclass, rdtype, priority, weight, port, target)
    57
    58      from_text = classmethod(from_text)
    59
    60 -    def to_wire(self, file, compress = None, origin = None):
    61          three_ints = struct.pack("!HHH", self.priority, self.weight, self.port)
    62          file.write(three_ints)
    63          self.target.to_wire(file, compress, origin)
    64
    65 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
    66          (priority, weight, port) = struct.unpack('!HHH',
    67                                                   wire[current : current + 6])
    68          current += 6
    69          rdlen -= 6
    70          (target, cused) = dns.name.from_wire(wire[: current + rdlen],
    71                                               current)
    72          if cused != rdlen:
    73              raise dns.exception.FormError
    74          if not origin is None:
    75              target = target.relativize(origin)
    76          return cls(rdclass, rdtype, priority, weight, port, target)
    77
    78      from_wire = classmethod(from_wire)
    79
    80 -    def choose_relativity(self, origin = None, relativize = True):
    81          self.target = self.target.choose_relativity(origin, relativize)
    82
    83 -    def _cmp(self, other):
    84          sp = struct.pack("!HHH", self.priority, self.weight, self.port)
    85          op = struct.pack("!HHH", other.priority, other.weight, other.port)
    86          v = cmp(sp, op)
    87          if v == 0:
    88              v = cmp(self.target, other.target)
    89          return v
    90