From f3f8d12d38b4e141a239cbb140b97d27e5fe0d98 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Thu, 2 Jul 2020 11:32:53 -0700 Subject: convert a bunch of types --- dns/rdtypes/IN/A.py | 4 ++-- dns/rdtypes/IN/AAAA.py | 4 ++-- dns/rdtypes/IN/APL.py | 18 ++++-------------- dns/rdtypes/IN/DHCID.py | 4 ++-- dns/rdtypes/IN/NAPTR.py | 23 +++++------------------ dns/rdtypes/IN/NSAP.py | 4 ++-- dns/rdtypes/IN/PX.py | 22 ++++------------------ dns/rdtypes/IN/SRV.py | 14 +++----------- dns/rdtypes/IN/WKS.py | 10 ++++------ dns/rdtypes/euibase.py | 4 ++-- tests/test_rdtypeanyeui.py | 28 ++++++++-------------------- 11 files changed, 38 insertions(+), 97 deletions(-) diff --git a/dns/rdtypes/IN/A.py b/dns/rdtypes/IN/A.py index 461ce92..ef17f35 100644 --- a/dns/rdtypes/IN/A.py +++ b/dns/rdtypes/IN/A.py @@ -47,6 +47,6 @@ class A(dns.rdata.Rdata): file.write(dns.ipv4.inet_aton(self.address)) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - address = dns.ipv4.inet_ntoa(wire[current: current + rdlen]) + def from_parser(cls, rdclass, rdtype, parser, origin=None): + address = dns.ipv4.inet_ntoa(parser.get_remaining()) return cls(rdclass, rdtype, address) diff --git a/dns/rdtypes/IN/AAAA.py b/dns/rdtypes/IN/AAAA.py index 9751f82..a9fb10c 100644 --- a/dns/rdtypes/IN/AAAA.py +++ b/dns/rdtypes/IN/AAAA.py @@ -47,6 +47,6 @@ class AAAA(dns.rdata.Rdata): file.write(dns.ipv6.inet_aton(self.address)) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - address = dns.ipv6.inet_ntoa(wire[current: current + rdlen]) + def from_parser(cls, rdclass, rdtype, parser, origin=None): + address = dns.ipv6.inet_ntoa(parser.get_remaining()) return cls(rdclass, rdtype, address) diff --git a/dns/rdtypes/IN/APL.py b/dns/rdtypes/IN/APL.py index 7149a64..b3e13a3 100644 --- a/dns/rdtypes/IN/APL.py +++ b/dns/rdtypes/IN/APL.py @@ -111,26 +111,18 @@ class APL(dns.rdata.Rdata): item.to_wire(file) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): + def from_parser(cls, rdclass, rdtype, parser, origin=None): items = [] - while 1: - if rdlen == 0: - break - if rdlen < 4: - raise dns.exception.FormError - header = struct.unpack('!HBB', wire[current: current + 4]) + while parser.remaining() > 0: + header = parser.get_struct('!HBB') afdlen = header[2] if afdlen > 127: negation = True afdlen -= 128 else: negation = False - current += 4 - rdlen -= 4 - if rdlen < afdlen: - raise dns.exception.FormError - address = wire[current: current + afdlen].unwrap() + address = parser.get_bytes(afdlen) l = len(address) if header[0] == 1: if l < 4: @@ -146,8 +138,6 @@ class APL(dns.rdata.Rdata): # seems better than throwing an exception # address = codecs.encode(address, 'hex_codec') - current += afdlen - rdlen -= afdlen item = APLItem(header[0], negation, address, header[1]) items.append(item) return cls(rdclass, rdtype, items) diff --git a/dns/rdtypes/IN/DHCID.py b/dns/rdtypes/IN/DHCID.py index da83450..ba729d4 100644 --- a/dns/rdtypes/IN/DHCID.py +++ b/dns/rdtypes/IN/DHCID.py @@ -46,6 +46,6 @@ class DHCID(dns.rdata.Rdata): file.write(self.data) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - data = wire[current: current + rdlen].unwrap() + def from_parser(cls, rdclass, rdtype, parser, origin=None): + data = parser.get_remaining() return cls(rdclass, rdtype, data) diff --git a/dns/rdtypes/IN/NAPTR.py b/dns/rdtypes/IN/NAPTR.py index d8f2958..747df4c 100644 --- a/dns/rdtypes/IN/NAPTR.py +++ b/dns/rdtypes/IN/NAPTR.py @@ -85,26 +85,13 @@ class NAPTR(dns.rdata.Rdata): self.replacement.to_wire(file, compress, origin, canonicalize) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - (order, preference) = struct.unpack('!HH', wire[current: current + 4]) - current += 4 - rdlen -= 4 + def from_parser(cls, rdclass, rdtype, parser, origin=None): + (order, preference) = parser.get_struct('!HH') strings = [] for i in range(3): - l = wire[current] - current += 1 - rdlen -= 1 - if l > rdlen or rdlen < 0: - raise dns.exception.FormError - s = wire[current: current + l].unwrap() - current += l - rdlen -= l + l = parser.get_uint8() + s = parser.get_bytes(l) strings.append(s) - (replacement, cused) = dns.name.from_wire(wire[: current + rdlen], - current) - if cused != rdlen: - raise dns.exception.FormError - if origin is not None: - replacement = replacement.relativize(origin) + replacement = parser.get_name(origin) return cls(rdclass, rdtype, order, preference, strings[0], strings[1], strings[2], replacement) diff --git a/dns/rdtypes/IN/NSAP.py b/dns/rdtypes/IN/NSAP.py index a42e592..2ec7a07 100644 --- a/dns/rdtypes/IN/NSAP.py +++ b/dns/rdtypes/IN/NSAP.py @@ -54,6 +54,6 @@ class NSAP(dns.rdata.Rdata): file.write(self.address) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - address = wire[current: current + rdlen].unwrap() + def from_parser(cls, rdclass, rdtype, parser, origin=None): + address = parser.get_remaining() return cls(rdclass, rdtype, address) diff --git a/dns/rdtypes/IN/PX.py b/dns/rdtypes/IN/PX.py index e8e0ada..59edf30 100644 --- a/dns/rdtypes/IN/PX.py +++ b/dns/rdtypes/IN/PX.py @@ -57,22 +57,8 @@ class PX(dns.rdata.Rdata): self.mapx400.to_wire(file, None, origin, canonicalize) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - (preference, ) = struct.unpack('!H', wire[current: current + 2]) - current += 2 - rdlen -= 2 - (map822, cused) = dns.name.from_wire(wire[: current + rdlen], - current) - if cused > rdlen: - raise dns.exception.FormError - current += cused - rdlen -= cused - if origin is not None: - map822 = map822.relativize(origin) - (mapx400, cused) = dns.name.from_wire(wire[: current + rdlen], - current) - if cused != rdlen: - raise dns.exception.FormError - if origin is not None: - mapx400 = mapx400.relativize(origin) + def from_parser(cls, rdclass, rdtype, parser, origin=None): + preference = parser.get_uint16() + map822 = parser.get_name(origin) + mapx400 = parser.get_name(origin) return cls(rdclass, rdtype, preference, map822, mapx400) diff --git a/dns/rdtypes/IN/SRV.py b/dns/rdtypes/IN/SRV.py index 02603f2..a43aa98 100644 --- a/dns/rdtypes/IN/SRV.py +++ b/dns/rdtypes/IN/SRV.py @@ -58,15 +58,7 @@ class SRV(dns.rdata.Rdata): self.target.to_wire(file, compress, origin, canonicalize) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - (priority, weight, port) = struct.unpack('!HHH', - wire[current: current + 6]) - current += 6 - rdlen -= 6 - (target, cused) = dns.name.from_wire(wire[: current + rdlen], - current) - if cused != rdlen: - raise dns.exception.FormError - if origin is not None: - target = target.relativize(origin) + def from_parser(cls, rdclass, rdtype, parser, origin=None): + (priority, weight, port) = parser.get_struct('!HHH') + target = parser.get_name(origin) return cls(rdclass, rdtype, priority, weight, port, target) diff --git a/dns/rdtypes/IN/WKS.py b/dns/rdtypes/IN/WKS.py index a564135..dc66c8e 100644 --- a/dns/rdtypes/IN/WKS.py +++ b/dns/rdtypes/IN/WKS.py @@ -89,10 +89,8 @@ class WKS(dns.rdata.Rdata): file.write(self.bitmap) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - address = dns.ipv4.inet_ntoa(wire[current: current + 4]) - protocol, = struct.unpack('!B', wire[current + 4: current + 5]) - current += 5 - rdlen -= 5 - bitmap = wire[current: current + rdlen].unwrap() + def from_parser(cls, rdclass, rdtype, parser, origin=None): + address = dns.ipv4.inet_ntoa(parser.get_bytes(4)) + protocol = parser.get_uint8() + bitmap = parser.get_remaining() return cls(rdclass, rdtype, address, protocol, bitmap) diff --git a/dns/rdtypes/euibase.py b/dns/rdtypes/euibase.py index 7756538..7fa73ed 100644 --- a/dns/rdtypes/euibase.py +++ b/dns/rdtypes/euibase.py @@ -63,6 +63,6 @@ class EUIBase(dns.rdata.Rdata): file.write(self.eui) @classmethod - def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - eui = wire[current:current + rdlen].unwrap() + def from_parser(cls, rdclass, rdtype, parser, origin=None): + eui = parser.get_remaining() return cls(rdclass, rdtype, eui) diff --git a/tests/test_rdtypeanyeui.py b/tests/test_rdtypeanyeui.py index e864582..a9111af 100644 --- a/tests/test_rdtypeanyeui.py +++ b/tests/test_rdtypeanyeui.py @@ -95,11 +95,8 @@ class RdtypeAnyEUI48TestCase(unittest.TestCase): eui = b'\x01\x23\x45\x67\x89\xab' pad_len = 100 wire = dns.wiredata.WireData(b'x' * pad_len + eui + b'y' * pad_len * 2) - inst = dns.rdtypes.ANY.EUI48.EUI48.from_wire(dns.rdataclass.IN, - dns.rdatatype.EUI48, - wire, - pad_len, - len(eui)) + inst = dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.EUI48, + wire, pad_len, len(eui)) self.assertEqual(inst.eui, eui) def testFromWireLength(self): @@ -108,11 +105,8 @@ class RdtypeAnyEUI48TestCase(unittest.TestCase): pad_len = 100 wire = dns.wiredata.WireData(b'x' * pad_len + eui + b'y' * pad_len * 2) with self.assertRaises(dns.exception.FormError): - dns.rdtypes.ANY.EUI48.EUI48.from_wire(dns.rdataclass.IN, - dns.rdatatype.EUI48, - wire, - pad_len, - len(eui)) + dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.EUI48, + wire, pad_len, len(eui)) class RdtypeAnyEUI64TestCase(unittest.TestCase): @@ -192,11 +186,8 @@ class RdtypeAnyEUI64TestCase(unittest.TestCase): eui = b'\x01\x23\x45\x67\x89\xab\xcd\xef' pad_len = 100 wire = dns.wiredata.WireData(b'x' * pad_len + eui + b'y' * pad_len * 2) - inst = dns.rdtypes.ANY.EUI64.EUI64.from_wire(dns.rdataclass.IN, - dns.rdatatype.EUI64, - wire, - pad_len, - len(eui)) + inst = dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.EUI64, + wire, pad_len, len(eui)) self.assertEqual(inst.eui, eui) def testFromWireLength(self): @@ -205,11 +196,8 @@ class RdtypeAnyEUI64TestCase(unittest.TestCase): pad_len = 100 wire = dns.wiredata.WireData(b'x' * pad_len + eui + b'y' * pad_len * 2) with self.assertRaises(dns.exception.FormError): - dns.rdtypes.ANY.EUI64.EUI64.from_wire(dns.rdataclass.IN, - dns.rdatatype.EUI64, - wire, - pad_len, - len(eui)) + dns.rdata.from_wire(dns.rdataclass.IN, dns.rdatatype.EUI64, + wire, pad_len, len(eui)) if __name__ == '__main__': -- cgit v1.2.1