Skip to content

Commit 5cd79eb

Browse files
committed
Avoid dual-stack
OpenBSD doesn't support dual stack, that makes this broken on machine which hasn't got IPv6. Anyway, a machine which has IPv6, still, can't access IPv4 hosts.
1 parent ff3281f commit 5cd79eb

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/aioquic/asyncio/client.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ async def connect(
4747
* ``local_port`` is the UDP port number that this client wants to bind.
4848
"""
4949
loop = asyncio.get_event_loop()
50-
local_host = "::"
51-
52-
# lookup remote address
53-
infos = await loop.getaddrinfo(host, port, type=socket.SOCK_DGRAM)
54-
addr = infos[0][4]
55-
if len(addr) == 2:
56-
addr = ("::ffff:" + addr[0], addr[1], 0, 0)
5750

5851
# prepare QUIC connection
5952
if configuration is None:
@@ -66,16 +59,28 @@ async def connect(
6659
token_handler=token_handler,
6760
)
6861

69-
# explicitly enable IPv4/IPv6 dual stack
70-
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
62+
# lookup remote address
63+
infos = await loop.getaddrinfo(host, port, type=socket.SOCK_DGRAM, flags=socket.AI_ADDRCONFIG)
64+
65+
addr = infos[0][4]
66+
# addr is 2-tuple for AF_INET and 4-tuple for AF_INET6
67+
if len(addr) == 2:
68+
local_tuple = ("0.0.0.0", local_port)
69+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
70+
elif len(addr) == 4:
71+
local_tuple = ("::", local_port, 0, 0)
72+
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
73+
else:
74+
raise Exception("Unsupported response from getaddrinfo")
75+
7176
completed = False
7277
try:
73-
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
74-
sock.bind((local_host, local_port, 0, 0))
78+
sock.bind(local_tuple)
7579
completed = True
7680
finally:
7781
if not completed:
7882
sock.close()
83+
7984
# connect
8085
transport, protocol = await loop.create_datagram_endpoint(
8186
lambda: create_protocol(connection, stream_handler=stream_handler),

0 commit comments

Comments
 (0)