def from_bytes(cls, packet, offset, rdlength): """Create a RecordData object from bytes. Args: packet (bytes): packet. offset (int): offset in message. rdlength (int): length of rdata. """ mname, offset = Name.from_bytes(packet, offset) rname, offset = Name.from_bytes(packet, offset) serial = struct.unpack_from("!I", packet, offset)[0] refresh = struct.unpack_from("!i", packet, offset + 4)[0] retry = struct.unpack_from("!i", packet, offset + 8)[0] expire = struct.unpack_from("!i", packet, offset + 12)[0] minimum = struct.unpack_from("!I", packet, offset + 16)[0] return cls(mname, rname, serial, refresh, retry, expire, minimum), offset + 20
def from_bytes(cls, packet, offset): """Convert ResourceRecord from bytes.""" name, offset = Name.from_bytes(packet, offset) type_ = Type(struct.unpack_from("!H", packet, offset)[0]) class_ = Class(struct.unpack_from("!H", packet, offset + 2)[0]) ttl, rdlength = struct.unpack_from("!iH", packet, offset + 4) offset += 10 rdata = RecordData.create_from_bytes(type_, packet, offset, rdlength) offset += rdlength return cls(name, type_, class_, ttl, rdata), offset
def from_bytes(cls, packet, offset, rdlength): """Create a RecordData object from bytes. Args: packet (bytes): packet. offset (int): offset in message. rdlength (int): length of rdata. """ nsdname, offset = Name.from_bytes(packet, offset) return cls(nsdname)
def test_name_from_bytes1(self): packet = b"\x03www\x07example\x03com\x00" name1, offset = Name.from_bytes(packet, 0) name2 = Name("www.example.com") self.assertEqual(offset, 17) self.assertEqual(name1, name2)
def test_name_from_bytes4(self): packet = b"\x00" name, offset = Name.from_bytes(packet, 0) self.assertEqual(name.labels, [])
def test_name_from_bytes3(self): packet = b"\x41" + b"a" * 65 with self.assertRaises(ValueError): Name.from_bytes(packet, 0)
def test_name_from_bytes2(self): packet = b"\x03www\x07example\x03com\x00\x03ftp\xc0\x04" name1, offset = Name.from_bytes(packet, 0) name2, offset = Name.from_bytes(packet, offset) self.assertEqual(name1, Name("www.example.com")) self.assertEqual(name2, Name("ftp.example.com"))
def from_bytes(cls, packet, offset): """Convert Question from bytes.""" qname, offset = Name.from_bytes(packet, offset) qtype = Type(struct.unpack_from("!H", packet, offset)[0]) qclass = Class(struct.unpack_from("!H", packet, offset + 2)[0]) return cls(qname, qtype, qclass), offset + 4
from dns.resource import ResourceRecord, NSRecordData, ARecordData from dns.name import Name from dns.types import Type from dns.classes import Class b = Name('a.').to_bytes(0) x, y = Name.from_bytes(b, 0) print(b, x) b = Name('b.').to_bytes(0) x, y = Name.from_bytes(b, 0) print(b, x) b = Name('.').to_bytes(0) x, y = Name.from_bytes(b, 0) print(b, x)