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):
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):
78
79 -class UncompressedMX(MXBase):
80 """Base class for rdata that is like an MX record, but whose name 81 is not compressed when converted to DNS wire format, and whose 82 digestable form is not downcased.""" 83
84 - def to_wire(self, file, compress = None, origin = None):
85 super(UncompressedMX, self).to_wire(file, None, origin)
86
87 - def to_digestable(self, origin = None):
88 f = cStringIO.StringIO() 89 self.to_wire(f, None, origin) 90 return f.getvalue()
91
92 -class UncompressedDowncasingMX(MXBase):
93 """Base class for rdata that is like an MX record, but whose name 94 is not compressed when convert to DNS wire format.""" 95
96 - def to_wire(self, file, compress = None, origin = None):
97 super(UncompressedDowncasingMX, self).to_wire(file, None, origin)
98