Skip to content

Commit

Permalink
Enable dual-stack on all platform with exception for OpenBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
catap committed Jun 22, 2024
1 parent 5cd79eb commit 3c4a567
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/aioquic/asyncio/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
import socket
import sys
from contextlib import asynccontextmanager
from typing import AsyncGenerator, Callable, Optional, cast
from typing import AsyncGenerator, Callable, Optional, Tuple, Union, cast

from ..quic.configuration import QuicConfiguration
from ..quic.connection import QuicConnection, QuicTokenHandler
Expand Down Expand Up @@ -59,12 +60,25 @@ async def connect(
token_handler=token_handler,
)

# OpenBSD well known to not support dual-stack
dual_stack = not sys.platform.startswith("openbsd")

# Use AI_ADDRCONFIG on platforms which doesn't support dual-stack
flags = 0
if not dual_stack:
flags = socket.AI_ADDRCONFIG

Check warning on line 69 in src/aioquic/asyncio/client.py

View check run for this annotation

Codecov / codecov/patch

src/aioquic/asyncio/client.py#L69

Added line #L69 was not covered by tests

# lookup remote address
infos = await loop.getaddrinfo(host, port, type=socket.SOCK_DGRAM, flags=socket.AI_ADDRCONFIG)
infos = await loop.getaddrinfo(host, port, type=socket.SOCK_DGRAM, flags=flags)

local_tuple: Union[Tuple[str, int], Tuple[str, int, int, int]]
addr = infos[0][4]
# addr is 2-tuple for AF_INET and 4-tuple for AF_INET6
if len(addr) == 2:
if dual_stack and len(addr) == 2:
addr = ("::ffff:" + addr[0], addr[1], 0, 0)
local_tuple = ("::", local_port, 0, 0)
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
elif len(addr) == 2:
local_tuple = ("0.0.0.0", local_port)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

Check warning on line 83 in src/aioquic/asyncio/client.py

View check run for this annotation

Codecov / codecov/patch

src/aioquic/asyncio/client.py#L82-L83

Added lines #L82 - L83 were not covered by tests
elif len(addr) == 4:
Expand All @@ -75,6 +89,8 @@ async def connect(

completed = False
try:
if dual_stack:
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
sock.bind(local_tuple)
completed = True
finally:
Expand Down

0 comments on commit 3c4a567

Please sign in to comment.