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 struct 
 19   
 20  import dns.exception 
 21  import dns.rdata 
 22  import dns.name 
 23  import dns.util 
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 @classmethod
45 - def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
51
52 - def to_wire(self, file, compress = None, origin = None):
53 pref = struct.pack("!H", self.preference) 54 file.write(pref) 55 self.exchange.to_wire(file, compress, origin)
56
57 - def to_digestable(self, origin = None):
58 return struct.pack("!H", self.preference) + \ 59 self.exchange.to_digestable(origin)
60 61 @classmethod
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 - def choose_relativity(self, origin = None, relativize = True):
75 self.exchange = self.exchange.choose_relativity(origin, relativize)
76
77 - def _cmp(self, other):
78 sp = struct.pack("!H", self.preference) 79 op = struct.pack("!H", other.preference) 80 v = dns.util.cmp(sp, op) 81 if v == 0: 82 v = dns.util.cmp(self.exchange, other.exchange) 83 return v
84
85 -class UncompressedMX(MXBase):
86 """Base class for rdata that is like an MX record, but whose name 87 is not compressed when converted to DNS wire format, and whose 88 digestable form is not downcased.""" 89
90 - def to_wire(self, file, compress = None, origin = None):
91 super(UncompressedMX, self).to_wire(file, None, origin)
92
93 - def to_digestable(self, origin = None):
94 return self.to_wire(f, None, origin)
95
96 -class UncompressedDowncasingMX(MXBase):
97 """Base class for rdata that is like an MX record, but whose name 98 is not compressed when convert to DNS wire format.""" 99
100 - def to_wire(self, file, compress = None, origin = None):
101 super(UncompressedDowncasingMX, self).to_wire(file, None, origin)
102