1# Copyright (C) 2003-2007, 2009, 2010 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
16import struct
17
18import dns.exception
19import dns.rdata
20import dns.name
21
22class PX(dns.rdata.Rdata):
23    """PX record.
24
25    @ivar preference: the preference value
26    @type preference: int
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
31    @see: RFC 2163"""
32
33    __slots__ = ['preference', 'map822', 'mapx400']
34
35    def __init__(self, rdclass, rdtype, preference, map822, mapx400):
36        super(PX, self).__init__(rdclass, rdtype)
37        self.preference = preference
38        self.map822 = map822
39        self.mapx400 = mapx400
40
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)
45
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)
52        tok.get_eol()
53        return cls(rdclass, rdtype, preference, map822, mapx400)
54
55    from_text = classmethod(from_text)
56
57    def to_wire(self, file, compress = None, origin = None):
58        pref = struct.pack("!H", self.preference)
59        file.write(pref)
60        self.map822.to_wire(file, None, origin)
61        self.mapx400.to_wire(file, None, origin)
62
63    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
64        (preference, ) = struct.unpack('!H', wire[current : current + 2])
65        current += 2
66        rdlen -= 2
67        (map822, cused) = dns.name.from_wire(wire[: current + rdlen],
68                                               current)
69        if cused > rdlen:
70            raise dns.exception.FormError
71        current += cused
72        rdlen -= cused
73        if not origin is None:
74            map822 = map822.relativize(origin)
75        (mapx400, cused) = dns.name.from_wire(wire[: current + rdlen],
76                                              current)
77        if cused != 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)
82
83    from_wire = classmethod(from_wire)
84
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)
88
89    def _cmp(self, other):
90        sp = struct.pack("!H", self.preference)
91        op = struct.pack("!H", other.preference)
92        v = cmp(sp, op)
93        if v == 0:
94            v = cmp(self.map822, other.map822)
95            if v == 0:
96                v = cmp(self.mapx400, other.mapx400)
97        return v
98