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 3aa752d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 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,11 +60,24 @@ 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

# 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 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)
if len(addr) == 2:
local_tuple = ("0.0.0.0", local_port)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
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 3aa752d

Please sign in to comment.