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 4443094 commit a6056fb
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 @@ -41,17 +41,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 @@ -40,7 +40,7 @@ async def fake(*args, **kwargs):
if not hosts:
raise socket.gaierror

return list((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 a6056fb

Please sign in to comment.