Skip to content

Commit

Permalink
fix(resolver): fix resolve ipv6 only host.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi committed Nov 2, 2021
1 parent 7f0cd0d commit 463218b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/6195.bugfix
@@ -0,0 +1 @@
Fix the threaded dns resolver skip the correct dns response when connecting a IPv6-only host.
7 changes: 4 additions & 3 deletions aiohttp/resolver.py
Expand Up @@ -37,9 +37,10 @@ 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]
continue
if family == socket.AF_INET6 and not socket.has_ipv6:
continue

if family == socket.AF_INET6 and 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.
Expand Down
23 changes: 23 additions & 0 deletions tests/test_resolver.py
Expand Up @@ -143,6 +143,29 @@ async def unknown_addrinfo(*args: Any, **kwargs: Any) -> List[Any]:
assert len(res) == 0


async def test_threaded_resolver_ipv6_only_host() -> None:
loop = Mock()

async def ipv6_addrinfo(*args: Any, **kwargs: Any) -> List[Any]:
return [
(
socket.AF_INET6,
socket.SOCK_STREAM,
6,
"",
("2404:6800:4004:819::200", 443, 0, 0),
)
]

loop.getaddrinfo = ipv6_addrinfo
resolver = ThreadedResolver()
resolver._loop = loop
res = await resolver.resolve("https://ipv6.google.com")
assert len(res) == 1
assert res[0]["hostname"] == "https://ipv6.google.com"
assert res[0]["host"] == "2404:6800:4004:819::200"


async def test_close_for_threaded_resolver(loop: Any) -> None:
resolver = ThreadedResolver()
await resolver.close()
Expand Down

0 comments on commit 463218b

Please sign in to comment.