Debian Codegraph @

dns/rdtypes/ANY/ISDN.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 struct
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 ISDN(dns.rdata.Rdata):
28 """ISDN record"""
29
30 # see: RFC 1183
31
32 __slots__ = ["address", "subaddress"]
33
34 def __init__(self, rdclass, rdtype, address, subaddress):
35 super().__init__(rdclass, rdtype)
36 self.address = self._as_bytes(address, True, 255)
37 self.subaddress = self._as_bytes(subaddress, True, 255)
38
39 def to_text(self, origin=None, relativize=True, **kw):
40 if self.subaddress:
41 return (
42 f'"{dns.rdata._escapify(self.address)}" '
43 f'"{dns.rdata._escapify(self.subaddress)}"'
44 )
45 else:
46 return f'"{dns.rdata._escapify(self.address)}"'
47
48 @classmethod
49 def from_text(
50 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
51 ):
52 address = tok.get_string()
53 tokens = tok.get_remaining(max_tokens=1)
54 if len(tokens) >= 1:
55 subaddress = tokens[0].unescape().value
56 else:
57 subaddress = ""
58 return cls(rdclass, rdtype, address, subaddress)
59
60 def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
61 l = len(self.address)
62 assert l < 256
63 file.write(struct.pack("!B", l))
64 file.write(self.address)
65 l = len(self.subaddress)
66 if l > 0:
67 assert l < 256
68 file.write(struct.pack("!B", l))
69 file.write(self.subaddress)
70
71 @classmethod
72 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
73 address = parser.get_counted_bytes()
74 if parser.remaining() > 0:
75 subaddress = parser.get_counted_bytes()
76 else:
77 subaddress = b""
78 return cls(rdclass, rdtype, address, subaddress)