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

Source Code for Module dns.rdtypes.mxbase

  1  # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license 
  2   
  3  # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. 
  4  # 
  5  # Permission to use, copy, modify, and distribute this software and its 
  6  # documentation for any purpose with or without fee is hereby granted, 
  7  # provided that the above copyright notice and this permission notice 
  8  # appear in all copies. 
  9  # 
 10  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 
 11  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
 12  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 
 13  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
 14  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
 15  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 
 16  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 
 17   
 18  """MX-like base classes.""" 
 19   
 20  from io import BytesIO 
 21  import struct 
 22   
 23  import dns.exception 
 24  import dns.rdata 
 25  import dns.name 
26 27 28 -class MXBase(dns.rdata.Rdata):
29 30 """Base class for rdata that is like an MX record. 31 32 @ivar preference: the preference value 33 @type preference: int 34 @ivar exchange: the exchange name 35 @type exchange: dns.name.Name object""" 36 37 __slots__ = ['preference', 'exchange'] 38
39 - def __init__(self, rdclass, rdtype, preference, exchange):
40 super(MXBase, self).__init__(rdclass, rdtype) 41 self.preference = preference 42 self.exchange = exchange
43
44 - def to_text(self, origin=None, relativize=True, **kw):
45 exchange = self.exchange.choose_relativity(origin, relativize) 46 return '%d %s' % (self.preference, exchange)
47 48 @classmethod
49 - def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
55
56 - def to_wire(self, file, compress=None, origin=None):
57 pref = struct.pack("!H", self.preference) 58 file.write(pref) 59 self.exchange.to_wire(file, compress, origin)
60
61 - def to_digestable(self, origin=None):
62 return struct.pack("!H", self.preference) + \ 63 self.exchange.to_digestable(origin)
64 65 @classmethod
66 - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
67 (preference, ) = struct.unpack('!H', wire[current: current + 2]) 68 current += 2 69 rdlen -= 2 70 (exchange, cused) = dns.name.from_wire(wire[: current + rdlen], 71 current) 72 if cused != rdlen: 73 raise dns.exception.FormError 74 if origin is not None: 75 exchange = exchange.relativize(origin) 76 return cls(rdclass, rdtype, preference, exchange)
77
78 - def choose_relativity(self, origin=None, relativize=True):
80
81 82 -class UncompressedMX(MXBase):
83 84 """Base class for rdata that is like an MX record, but whose name 85 is not compressed when converted to DNS wire format, and whose 86 digestable form is not downcased.""" 87
88 - def to_wire(self, file, compress=None, origin=None):
89 super(UncompressedMX, self).to_wire(file, None, origin)
90
91 - def to_digestable(self, origin=None):
92 f = BytesIO() 93 self.to_wire(f, None, origin) 94 return f.getvalue()
95
96 97 -class UncompressedDowncasingMX(MXBase):
98 99 """Base class for rdata that is like an MX record, but whose name 100 is not compressed when convert to DNS wire format.""" 101
102 - def to_wire(self, file, compress=None, origin=None):
103 super(UncompressedDowncasingMX, self).to_wire(file, None, origin)
104