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

# Source Code for Module dns.rdtypes.mxbase

      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  """MX-like base classes."""
     17
     18  import cStringIO
     19  import struct
     20
     21  import dns.exception
     22  import dns.rdata
     23  import dns.name
     24
     25 -class MXBase(dns.rdata.Rdata):
     26      """Base class for rdata that is like an MX record.
     27
     28      @ivar preference: the preference value
     29      @type preference: int
     30      @ivar exchange: the exchange name
     31      @type exchange: dns.name.Name object"""
     32
     33      __slots__ = ['preference', 'exchange']
     34
     35 -    def __init__(self, rdclass, rdtype, preference, exchange):
     36          super(MXBase, self).__init__(rdclass, rdtype)
     37          self.preference = preference
     38          self.exchange = exchange
     39
     40 -    def to_text(self, origin=None, relativize=True, **kw):
     41          exchange = self.exchange.choose_relativity(origin, relativize)
     42          return '%d %s' % (self.preference, exchange)
     43
     44 -    def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
     45          preference = tok.get_uint16()
     46          exchange = tok.get_name()
     47          exchange = exchange.choose_relativity(origin, relativize)
     48          tok.get_eol()
     49          return cls(rdclass, rdtype, preference, exchange)
     50
     51      from_text = classmethod(from_text)
     52
     53 -    def to_wire(self, file, compress = None, origin = None):
     54          pref = struct.pack("!H", self.preference)
     55          file.write(pref)
     56          self.exchange.to_wire(file, compress, origin)
     57
     58 -    def to_digestable(self, origin = None):
     59          return struct.pack("!H", self.preference) + \
     60              self.exchange.to_digestable(origin)
     61
     62 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
     63          (preference, ) = struct.unpack('!H', wire[current : current + 2])
     64          current += 2
     65          rdlen -= 2
     66          (exchange, cused) = dns.name.from_wire(wire[: current + rdlen],
     67                                                 current)
     68          if cused != rdlen:
     69              raise dns.exception.FormError
     70          if not origin is None:
     71              exchange = exchange.relativize(origin)
     72          return cls(rdclass, rdtype, preference, exchange)
     73
     74      from_wire = classmethod(from_wire)
     75
     76 -    def choose_relativity(self, origin = None, relativize = True):
     77          self.exchange = self.exchange.choose_relativity(origin, relativize)
     78
     79 -    def _cmp(self, other):
     80          sp = struct.pack("!H", self.preference)
     81          op = struct.pack("!H", other.preference)
     82          v = cmp(sp, op)
     83          if v == 0:
     84              v = cmp(self.exchange, other.exchange)
     85          return v
     86
     87 -class UncompressedMX(MXBase):
     88      """Base class for rdata that is like an MX record, but whose name
     89      is not compressed when converted to DNS wire format, and whose
     90      digestable form is not downcased."""
     91
     92 -    def to_wire(self, file, compress = None, origin = None):
     93          super(UncompressedMX, self).to_wire(file, None, origin)
     94
     95 -    def to_digestable(self, origin = None):
     96          f = cStringIO.StringIO()
     97          self.to_wire(f, None, origin)
     98          return f.getvalue()
     99
    100 -class UncompressedDowncasingMX(MXBase):
    101      """Base class for rdata that is like an MX record, but whose name
    102      is not compressed when convert to DNS wire format."""
    103
    104 -    def to_wire(self, file, compress = None, origin = None):
    105          super(UncompressedDowncasingMX, self).to_wire(file, None, origin)
    106