Debian Codegraph @

dns/rdtypes/ANY/L64.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 import struct
4
5 import dns.immutable
6 import dns.rdata
7 import dns.rdtypes.util
8
9
10 @dns.immutable.immutable
11 class L64(dns.rdata.Rdata):
12 """L64 record"""
13
14 # see: rfc6742.txt
15
16 __slots__ = ["preference", "locator64"]
17
18 def __init__(self, rdclass, rdtype, preference, locator64):
19 super().__init__(rdclass, rdtype)
20 self.preference = self._as_uint16(preference)
21 if isinstance(locator64, bytes):
22 if len(locator64) != 8:
23 raise ValueError("invalid locator64")
24 self.locator64 = dns.rdata._hexify(locator64, 4, b":")
25 else:
26 dns.rdtypes.util.parse_formatted_hex(locator64, 4, 4, ":")
27 self.locator64 = locator64
28
29 def to_text(self, origin=None, relativize=True, **kw):
30 return f"{self.preference} {self.locator64}"
31
32 @classmethod
33 def from_text(
34 cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None
35 ):
36 preference = tok.get_uint16()
37 locator64 = tok.get_identifier()
38 return cls(rdclass, rdtype, preference, locator64)
39
40 def _to_wire(self, file, compress=None, origin=None, canonicalize=False):
41 file.write(struct.pack("!H", self.preference))
42 file.write(dns.rdtypes.util.parse_formatted_hex(self.locator64, 4, 4, ":"))
43
44 @classmethod
45 def from_wire_parser(cls, rdclass, rdtype, parser, origin=None):
46 preference = parser.get_uint16()
47 locator64 = parser.get_remaining()
48 return cls(rdclass, rdtype, preference, locator64)