1 # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
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.
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.
22 class PX(dns.rdata.Rdata):
25 @ivar preference: the preference value
27 @ivar map822: the map822 name
28 @type map822: dns.name.Name object
29 @ivar mapx400: the mapx400 name
30 @type mapx400: dns.name.Name object
33 __slots__ = ['preference', 'map822', 'mapx400']
35 def __init__(self, rdclass, rdtype, preference, map822, mapx400):
36 super(PX, self).__init__(rdclass, rdtype)
37 self.preference = preference
39 self.mapx400 = mapx400
41 def to_text(self, origin=None, relativize=True, **kw):
42 map822 = self.map822.choose_relativity(origin, relativize)
43 mapx400 = self.mapx400.choose_relativity(origin, relativize)
44 return '%d %s %s' % (self.preference, map822, mapx400)
46 def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
47 preference = tok.get_uint16()
48 map822 = tok.get_name()
49 map822 = map822.choose_relativity(origin, relativize)
50 mapx400 = tok.get_name(None)
51 mapx400 = mapx400.choose_relativity(origin, relativize)
53 return cls(rdclass, rdtype, preference, map822, mapx400)
55 from_text = classmethod(from_text)
57 def to_wire(self, file, compress = None, origin = None):
58 pref = struct.pack("!H", self.preference)
60 self.map822.to_wire(file, None, origin)
61 self.mapx400.to_wire(file, None, origin)
63 def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
64 (preference, ) = struct.unpack('!H', wire[current : current + 2])
67 (map822, cused) = dns.name.from_wire(wire[: current + rdlen],
70 raise dns.exception.FormError
73 if not origin is None:
74 map822 = map822.relativize(origin)
75 (mapx400, cused) = dns.name.from_wire(wire[: current + rdlen],
78 raise dns.exception.FormError
79 if not origin is None:
80 mapx400 = mapx400.relativize(origin)
81 return cls(rdclass, rdtype, preference, map822, mapx400)
83 from_wire = classmethod(from_wire)
85 def choose_relativity(self, origin = None, relativize = True):
86 self.map822 = self.map822.choose_relativity(origin, relativize)
87 self.mapx400 = self.mapx400.choose_relativity(origin, relativize)
89 def _cmp(self, other):
90 sp = struct.pack("!H", self.preference)
91 op = struct.pack("!H", other.preference)
94 v = cmp(self.map822, other.map822)
96 v = cmp(self.mapx400, other.mapx400)