| 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 NID(dns.rdata.Rdata): |
| 12 |
"""NID record""" |
| 13 |
|
| 14 |
# see: rfc6742.txt |
| 15 |
|
| 16 |
__slots__ = ["preference", "nodeid"] |
| 17 |
|
| 18 |
def __init__(self, rdclass, rdtype, preference, nodeid): |
| 19 |
super().__init__(rdclass, rdtype) |
| 20 |
self.preference = self._as_uint16(preference) |
| 21 |
if isinstance(nodeid, bytes): |
| 22 |
if len(nodeid) != 8: |
| 23 |
raise ValueError("invalid nodeid") |
| 24 |
self.nodeid = dns.rdata._hexify(nodeid, 4, b":") |
| 25 |
else: |
| 26 |
dns.rdtypes.util.parse_formatted_hex(nodeid, 4, 4, ":") |
| 27 |
self.nodeid = nodeid |
| 28 |
|
| 29 |
def to_text(self, origin=None, relativize=True, **kw): |
| 30 |
return f"{self.preference} {self.nodeid}" |
| 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 |
nodeid = tok.get_identifier() |
| 38 |
return cls(rdclass, rdtype, preference, nodeid) |
| 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.nodeid, 4, 4, ":")) |
| 43 |
|
| 44 |
@classmethod |
| 45 |
def from_wire_parser(cls, rdclass, rdtype, parser, origin=None): |
| 46 |
preference = parser.get_uint16() |
| 47 |
nodeid = parser.get_remaining() |
| 48 |
return cls(rdclass, rdtype, preference, nodeid) |