"""EDNS Options"""
+import binascii
import math
import socket
import struct
return cls(code, btext)
+class NSIDOption(Option):
+ def __init__(self, nsid: bytes):
+ super().__init__(OptionType.NSID)
+ self.nsid = nsid
+
+ def to_wire(self, file: Any = None) -> Optional[bytes]:
+ if file:
+ file.write(self.nsid)
+ return None
+ else:
+ return self.nsid
+
+ def to_text(self) -> str:
+ if all(c >= 0x20 and c <= 0x7E for c in self.nsid):
+ # All ASCII printable, so it's probably a string.
+ value = self.nsid.decode()
+ else:
+ value = binascii.hexlify(self.nsid).decode()
+ return f"NSID {value}"
+
+ @classmethod
+ def from_wire_parser(
+ cls, otype: Union[OptionType, str], parser: dns.wire.Parser
+ ) -> Option:
+ return cls(parser.get_remaining())
+
+
_type_to_class: Dict[OptionType, Any] = {
OptionType.ECS: ECSOption,
OptionType.EDE: EDEOption,
+ OptionType.NSID: NSIDOption,
}
import operator
import struct
import unittest
-
from io import BytesIO
import dns.edns
self.assertFalse(o1 == 123)
self.assertTrue(o1 != 123)
+ def testNSIDOption(self):
+ opt = dns.edns.NSIDOption(b"testing")
+ io = BytesIO()
+ opt.to_wire(io)
+ data = io.getvalue()
+ self.assertEqual(data, b"testing")
+ self.assertEqual(str(opt), "NSID testing")
+ opt = dns.edns.NSIDOption(b"\xfe\xff")
+ io = BytesIO()
+ opt.to_wire(io)
+ data = io.getvalue()
+ self.assertEqual(data, b"\xfe\xff")
+ self.assertEqual(str(opt), "NSID feff")
+ o = dns.edns.option_from_wire(dns.edns.OptionType.NSID, data, 0, len(data))
+ self.assertEqual(o.nsid, b"\xfe\xff")
+
def test_option_registration(self):
U32OptionType = 9999