Skip to content
Snippets Groups Projects
Commit e2c89ae0 authored by Pierre LALET's avatar Pierre LALET
Browse files

Cleanup IPv6 addresses with "::" handling

parent 61191024
No related branches found
No related tags found
No related merge requests found
...@@ -25,24 +25,18 @@ def inet_pton(af, addr): ...@@ -25,24 +25,18 @@ def inet_pton(af, addr):
return socket.inet_pton(af, addr) return socket.inet_pton(af, addr)
except AttributeError: except AttributeError:
pass pass
joker_pos = None
# IPv6: The use of "::" indicates one or more groups of 16 bits of zeros.
# We deal with this form of wildcard using a special marker.
JOKER = "*"
while "::" in addr:
addr = addr.replace("::", ":" + JOKER + ":")
joker_pos = None
result = "" result = ""
parts = addr.split(":") parts = addr.split(":")
nparts = len(parts) nparts = len(parts)
for i, part in enumerate(parts): for i, part in enumerate(parts):
if part == JOKER: if not part:
# Wildcard is only allowed once # "::" indicates one or more groups of 2 null bytes
if joker_pos is None: if joker_pos is None:
joker_pos = len(result) joker_pos = len(result)
else: else:
raise Exception("Illegal syntax for IP address") # Wildcard is only allowed once
raise Exception("Illegal syntax for IP address")
elif i + 1 == nparts and '.' in part: elif i + 1 == nparts and '.' in part:
# The last part of an IPv6 address can be an IPv4 address # The last part of an IPv6 address can be an IPv4 address
try: try:
...@@ -55,12 +49,10 @@ def inet_pton(af, addr): ...@@ -55,12 +49,10 @@ def inet_pton(af, addr):
result += part.rjust(4, "0").decode("hex") result += part.rjust(4, "0").decode("hex")
except TypeError: except TypeError:
raise Exception("Illegal syntax for IP address") raise Exception("Illegal syntax for IP address")
# If there's a wildcard, fill up with zeros to reach 128bit (16 bytes) # If there's a wildcard, fill up with zeros to reach 128bit (16 bytes)
if JOKER in addr: if joker_pos is not None:
result = (result[:joker_pos] + "\x00" * (16 - len(result)) result = (result[:joker_pos] + "\x00" * (16 - len(result))
+ result[joker_pos:]) + result[joker_pos:])
if len(result) != 16: if len(result) != 16:
raise Exception("Illegal syntax for IP address") raise Exception("Illegal syntax for IP address")
return result return result
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment