Use IPv4-Mapping to have IPv4 compatibility
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
parent
e06d7b9540
commit
8ef3f3a21c
|
@ -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}")
|
||||
|
||||
|
|
Loading…
Reference in New Issue