From 61191024691eaf556bfd9922cc65196d0361296e Mon Sep 17 00:00:00 2001 From: Pierre LALET <pierre.lalet@cea.fr> Date: Sun, 29 Jan 2017 13:09:54 +0100 Subject: [PATCH] Cleanup IPv4 in IPv6 address handling --- scapy/pton_ntop.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/scapy/pton_ntop.py b/scapy/pton_ntop.py index bd3a3a41..a998d498 100644 --- a/scapy/pton_ntop.py +++ b/scapy/pton_ntop.py @@ -13,7 +13,6 @@ without IPv6 support, on Windows for instance. import socket import re -_IP4_FORMAT = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") _IP6_ZEROS = re.compile('(?::|^)(0(?::0)+)(?::|$)') def inet_pton(af, addr): @@ -34,29 +33,22 @@ def inet_pton(af, addr): addr = addr.replace("::", ":" + JOKER + ":") joker_pos = None - # The last part of an IPv6 address can be an IPv4 address - ipv4_bin = None - ipv4_addr = None - if "." in addr: - ipv4_addr = addr.split(":")[-1] - if _IP4_FORMAT.match(ipv4_addr) is None: - raise Exception("Illegal syntax for IP address") - try: - ipv4_bin = socket.inet_aton(ipv4_addr) - except socket.error: - raise Exception("Illegal syntax for IP address") - result = "" parts = addr.split(":") - for part in parts: + nparts = len(parts) + for i, part in enumerate(parts): if part == JOKER: # Wildcard is only allowed once if joker_pos is None: joker_pos = len(result) else: raise Exception("Illegal syntax for IP address") - elif part == ipv4_addr: - result += ipv4_bin + elif i + 1 == nparts and '.' in part: + # The last part of an IPv6 address can be an IPv4 address + try: + result += socket.inet_aton(part) + except socket.error: + raise Exception("Illegal syntax for IP address") else: # Each part must be 16bit. Add missing zeroes before decoding. try: -- GitLab