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
18import dns.rdtypes.mxbase
19import struct
20
21class A(dns.rdtypes.mxbase.MXBase):
22
23    """A record for Chaosnet
24    @ivar domain: the domain of the address
25    @type domain: dns.name.Name object
26    @ivar address: the 16-bit address
27    @type address: int"""
28
29    __slots__ = ['domain', 'address']
30
31    def __init__(self, rdclass, rdtype, address, domain):
32        super(A, self).__init__(rdclass, rdtype, address, domain)
33        self.domain = domain
34        self.address = address
35
36    def to_text(self, origin=None, relativize=True, **kw):
37        domain = self.domain.choose_relativity(origin, relativize)
38        return '%s %o' % (domain, self.address)
39
40    @classmethod
41    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
42        domain = tok.get_name()
43        address = tok.get_uint16(base=8)
44        domain = domain.choose_relativity(origin, relativize)
45        tok.get_eol()
46        return cls(rdclass, rdtype, address, domain)
47
48    def to_wire(self, file, compress=None, origin=None):
49        self.domain.to_wire(file, compress, origin)
50        pref = struct.pack("!H", self.address)
51        file.write(pref)
52
53    def to_digestable(self, origin=None):
54        return self.domain.to_digestable(origin) + \
55            struct.pack("!H", self.address)
56
57    @classmethod
58    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
59        (domain, cused) = dns.name.from_wire(wire[: current + rdlen-2],
60                                               current)
61        current += cused
62        (address,) = struct.unpack('!H', wire[current: current + 2])
63        if cused+2 != rdlen:
64            raise dns.exception.FormError
65        if origin is not None:
66            domain = domain.relativize(origin)
67        return cls(rdclass, rdtype, address, domain)
68
69    def choose_relativity(self, origin=None, relativize=True):
70        self.domain = self.domain.choose_relativity(origin, relativize)
71