Debian Codegraph @

dns/rdtypes/IN/NSAP.py

raw
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 import binascii
19
20 import dns.exception
21 import dns.immutable
22 import dns.rdata
23 import dns.tokenizer
24
25
26 @dns.immutable.immutable
27 class NSAP(dns.rdata.Rdata):
28 """NSAP record."""
29
30 # see: RFC 1706
31
32 __slots__ = ["address"]
33
34 def __init__(self, rdclass, rdtype, address):
35 super().__init__(rdclass, rdtype)
36 self.address = self._as_bytes(address)
37
38 def to_text(self, origin=None, relativize=True, **kw):
39 return f"0x{binascii.hexlify(self.address).decode()}"
40
41 @classmethod
42 def from_text(
43 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
44 ):
45 address = tok.get_string()
46 if address[0:2] != "0x":
47 raise dns.exception.SyntaxError("string does not start with 0x")
48 address = address[2:].replace(".", "")
49 if len(address) % 2 != 0:
50 raise dns.exception.SyntaxError("hexstring has odd length")
51 address = binascii.unhexlify(address.encode())
52 return cls(rdclass, rdtype, address)
53
54 def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
55 file.write(self.address)
56
57 @classmethod
58 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
59 address = parser.get_remaining()
60 return cls(rdclass, rdtype, address)