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