From 8ef3f3a21c71301d2f2bd3493566c5a97df134cc Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:03:59 +0100 Subject: [PATCH] Use IPv4-Mapping to have IPv4 compatibility Signed-off-by: Yohann D'ANELLO --- squinnondation/squinnondation.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index 2ff8d04..d99873f 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -340,8 +340,16 @@ class Hazelnut: """ def __init__(self, nickname: str = "anonymous", address: str = "localhost", port: int = 2500): self.nickname = nickname - # Resolve DNS as an IPv6 - address = socket.getaddrinfo(address, None, socket.AF_INET6)[0][4][0] + + try: + # Resolve DNS as an IPv6 + address = socket.getaddrinfo(address, None, socket.AF_INET6)[0][4][0] + except socket.gaierror: + # This is not a valid IPv6. Assume it can be resolved as an IPv4, and we use IPv4-mapping + # to compute a valid IPv6. + # See https://fr.wikipedia.org/wiki/Adresse_IPv6_mappant_IPv4 + address = "::ffff:" + socket.getaddrinfo(address, None, socket.AF_INET)[0][4][0] + self.address = IPv6Address(address) self.port = port @@ -352,8 +360,11 @@ class Squirrel(Hazelnut): """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + # Create UDP socket self.socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) + # Bind the socket self.socket.bind((str(self.address), self.port)) + self.hazelnuts = dict() print(f"Listening on {self.address}:{self.port}")