From bef4d1d05447ea485407fd1abaa9976dee326da6 Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Mon, 4 May 2020 07:17:11 -0700 Subject: [PATCH] In cases where we care that something is just an IPv4 or IPv6 address, without any extras like IPv6 scope, explicitly use dns.ipv4 and dns.ipv6 instead of dns.inet. This will let us be tolerant of scopes in other cases (e.g. ordinary network connections). --- dns/edns.py | 5 ++--- dns/rdtypes/IN/AAAA.py | 9 ++++----- dns/rdtypes/IN/APL.py | 11 ++++++----- dns/rdtypes/IN/IPSECKEY.py | 17 ++++++++--------- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/dns/edns.py b/dns/edns.py index b501590d..6618ccae 100644 --- a/dns/edns.py +++ b/dns/edns.py @@ -267,15 +267,14 @@ class ECSOption(Option): addrlen = int(math.ceil(src/8.0)) if family == 1: - af = dns.inet.AF_INET pad = 4 - addrlen + addr = dns.ipv4.inet_ntoa(wire[cur:cur+addrlen] + b'\x00' * pad) elif family == 2: - af = dns.inet.AF_INET6 pad = 16 - addrlen + addr = dns.ipv6.inet_ntoa(wire[cur:cur+addrlen] + b'\x00' * pad) else: raise ValueError('unsupported family') - addr = dns.inet.inet_ntop(af, wire[cur:cur+addrlen] + b'\x00' * pad) return cls(addr, src, scope) def _cmp(self, other): diff --git a/dns/rdtypes/IN/AAAA.py b/dns/rdtypes/IN/AAAA.py index 46ff6159..6eb1bdb8 100644 --- a/dns/rdtypes/IN/AAAA.py +++ b/dns/rdtypes/IN/AAAA.py @@ -16,7 +16,7 @@ # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import dns.exception -import dns.inet +import dns.ipv6 import dns.rdata import dns.tokenizer @@ -33,7 +33,7 @@ class AAAA(dns.rdata.Rdata): def __init__(self, rdclass, rdtype, address): super().__init__(rdclass, rdtype) # check that it's OK - dns.inet.inet_pton(dns.inet.AF_INET6, address) + dns.ipv6.inet_aton(address) object.__setattr__(self, 'address', address) def to_text(self, origin=None, relativize=True, **kw): @@ -47,10 +47,9 @@ class AAAA(dns.rdata.Rdata): return cls(rdclass, rdtype, address) def to_wire(self, file, compress=None, origin=None): - file.write(dns.inet.inet_pton(dns.inet.AF_INET6, self.address)) + file.write(dns.ipv6.inet_aton(self.address)) @classmethod def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): - address = dns.inet.inet_ntop(dns.inet.AF_INET6, - wire[current: current + rdlen]) + address = dns.ipv6.inet_ntoa(wire[current: current + rdlen]) return cls(rdclass, rdtype, address) diff --git a/dns/rdtypes/IN/APL.py b/dns/rdtypes/IN/APL.py index be9397bf..443d7149 100644 --- a/dns/rdtypes/IN/APL.py +++ b/dns/rdtypes/IN/APL.py @@ -20,7 +20,8 @@ import codecs import struct import dns.exception -import dns.inet +import dns.ipv4 +import dns.ipv6 import dns.rdata import dns.tokenizer @@ -54,9 +55,9 @@ class APLItem(object): def to_wire(self, file): if self.family == 1: - address = dns.inet.inet_pton(dns.inet.AF_INET, self.address) + address = dns.ipv4.inet_aton(self.address) elif self.family == 2: - address = dns.inet.inet_pton(dns.inet.AF_INET6, self.address) + address = dns.ipv6.inet_aton(self.address) else: address = binascii.unhexlify(self.address) # @@ -146,11 +147,11 @@ class APL(dns.rdata.Rdata): if header[0] == 1: if l < 4: address += b'\x00' * (4 - l) - address = dns.inet.inet_ntop(dns.inet.AF_INET, address) + address = dns.ipv4.inet_ntoa(address) elif header[0] == 2: if l < 16: address += b'\x00' * (16 - l) - address = dns.inet.inet_ntop(dns.inet.AF_INET6, address) + address = dns.ipv6.inet_ntoa(address) else: # # This isn't really right according to the RFC, but it diff --git a/dns/rdtypes/IN/IPSECKEY.py b/dns/rdtypes/IN/IPSECKEY.py index 85e147c6..17955155 100644 --- a/dns/rdtypes/IN/IPSECKEY.py +++ b/dns/rdtypes/IN/IPSECKEY.py @@ -19,7 +19,8 @@ import struct import base64 import dns.exception -import dns.inet +import dns.ipv4 +import dns.ipv6 import dns.name @@ -50,10 +51,10 @@ class IPSECKEY(dns.rdata.Rdata): gateway = None elif gateway_type == 1: # check that it's OK - dns.inet.inet_pton(dns.inet.AF_INET, gateway) + dns.ipv4.inet_aton(gateway) elif gateway_type == 2: # check that it's OK - dns.inet.inet_pton(dns.inet.AF_INET6, gateway) + dns.ipv6.inet_aton(gateway) elif gateway_type == 3: pass else: @@ -110,9 +111,9 @@ class IPSECKEY(dns.rdata.Rdata): if self.gateway_type == 0: pass elif self.gateway_type == 1: - file.write(dns.inet.inet_pton(dns.inet.AF_INET, self.gateway)) + file.write(dns.ipv4.inet_aton(self.gateway)) elif self.gateway_type == 2: - file.write(dns.inet.inet_pton(dns.inet.AF_INET6, self.gateway)) + file.write(dns.ipv6.inet_aton(self.gateway)) elif self.gateway_type == 3: self.gateway.to_wire(file, None, origin) else: @@ -130,13 +131,11 @@ class IPSECKEY(dns.rdata.Rdata): if gateway_type == 0: gateway = None elif gateway_type == 1: - gateway = dns.inet.inet_ntop(dns.inet.AF_INET, - wire[current: current + 4]) + gateway = dns.ipv4.inet_ntoa(wire[current: current + 4]) current += 4 rdlen -= 4 elif gateway_type == 2: - gateway = dns.inet.inet_ntop(dns.inet.AF_INET6, - wire[current: current + 16]) + gateway = dns.ipv6.inet_ntoa(wire[current: current + 16]) current += 16 rdlen -= 16 elif gateway_type == 3: -- 2.47.3