Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restored ability to connect IPv6-only host. #6309

Merged
merged 5 commits into from Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/6195.bugfix
@@ -0,0 +1 @@
Restored ability to connect IPv6-only host.
26 changes: 16 additions & 10 deletions aiohttp/resolver.py
Expand Up @@ -40,17 +40,23 @@ async def resolve(
hosts = []
for family, _, proto, _, address in infos:
if family == socket.AF_INET6:
if not (socket.has_ipv6 and address[3]): # type: ignore[misc]
if len(address) < 3:
# IPv6 is not supported by Python build,
# or IPv6 is not enabled in the host
continue
# This is essential for link-local IPv6 addresses.
# LL IPv6 is a VERY rare case. Strictly speaking, we should use
# getnameinfo() unconditionally, but performance makes sense.
host, _port = socket.getnameinfo(
address, socket.NI_NUMERICHOST | socket.NI_NUMERICSERV
)
port = int(_port)
else:
host, port = address[:2]
if address[3]: # type: ignore[misc]
# This is essential for link-local IPv6 addresses.
# LL IPv6 is a VERY rare case. Strictly speaking, we should use
# getnameinfo() unconditionally, but performance makes sense.
host, _port = socket.getnameinfo(
address, socket.NI_NUMERICHOST | socket.NI_NUMERICSERV
)
port = int(_port)
else:
host, port = address[:2]
else: # IPv4
assert family == socket.AF_INET
host, port = address # type: ignore[misc]
hosts.append(
{
"hostname": hostname,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_resolver.py
Expand Up @@ -44,7 +44,7 @@ async def fake(*args: Any, **kwargs: Any) -> List[Any]:
if not hosts:
raise socket.gaierror

return [(None, None, None, None, [h, 0]) for h in hosts]
return [(socket.AF_INET, None, socket.SOCK_STREAM, None, [h, 0]) for h in hosts]

return fake

Expand Down