Skip to content

Commit

Permalink
Restored ability to connect IPv6-only host. (#6309)
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Nov 14, 2021
1 parent 7f32513 commit a370cf4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
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

0 comments on commit a370cf4

Please sign in to comment.