8000 bpo-27500: Fix sttaic ver of getaddrinfo to resolve IPv6 addresses by 1st1 · Pull Request #7993 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-27500: Fix sttaic ver of getaddrinfo to resolve IPv6 addresses #7993

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

Merged
merged 4 commits into from
Jun 29, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
_FATAL_ERROR_IGNORE = (BrokenPipeError,
ConnectionResetError, ConnectionAbortedError)

_HAS_IPv6 = hasattr(socket, 'AF_INET6')


def _format_handle(handle):
cb = handle._callback
Expand Down Expand Up @@ -123,7 +125,7 @@ def _ipaddr_info(host, port, family, type, proto):

if family == socket.AF_UNSPEC:
afs = [socket.AF_INET]
if hasattr(socket, 'AF_INET6'):
if _HAS_IPv6:
afs.append(socket.AF_INET6)
else:
afs = [family]
Expand All @@ -139,7 +141,10 @@ def _ipaddr_info(host, port, family, type, proto):
try:
socket.inet_pton(af, host)
# The host has already been resolved.
return af, type, proto, '', (host, port)
if _HAS_IPv6 and af == socket.AF_INET6:
return af, type, proto, '', (host, port, 0, 0)
else:
return af, type, proto, '', (host, port)
except OSError:
pass

Expand Down Expand Up @@ -1309,7 +1314,6 @@ async def create_server(
raise ValueError(
'host/port and sock can not be specified at the same time')

AF_INET6 = getattr(socket, 'AF_INET6', 0)
if reuse_address is None:
reuse_address = os.name == 'posix' and sys.platform != 'cygwin'
sockets = []
Expand Down Expand Up @@ -1349,7 +1353,9 @@ async def create_server(
# Disable IPv4/IPv6 dual stack support (enabled by
# default on Linux) which makes a single socket
# listen on both address families.
if af == AF_INET6 and hasattr(socket, 'IPPROTO_IPV6'):
if (_HAS_IPv6 and
af == socket.AF_INET6 and
hasattr(socket, 'IPPROTO_IPV6')):
sock.setsockopt(socket.IPPROTO_IPV6,
socket.IPV6_V6ONLY,
True)
Expand Down
24 changes: 22 additions & 2 deletions Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ def test_ipaddr_info(self):
base_events._ipaddr_info('1.2.3.4', 1, INET6, STREAM, TCP))

self.assertEqual(
(INET6, STREAM, TCP, '', ('::3', 1)),
(INET6, STREAM, TCP, '', ('::3', 1, 0, 0)),
base_events._ipaddr_info('::3', 1, INET6, STREAM, TCP))

self.assertEqual(
(INET6, STREAM, TCP, '', ('::3', 1)),
(INET6, STREAM, TCP, '', ('::3', 1, 0, 0)),
base_events._ipaddr_info('::3', 1, UNSPEC, STREAM, TCP))

# IPv6 address with family IPv4.
Expand Down Expand Up @@ -1077,6 +1077,26 @@ def test_create_server_stream_bittype(self):
srv.close()
self.loop.run_until_complete(srv.wait_closed())

@unittest.skipUnless(hasattr(socket, 'AF_INET6'), 'no IPv6 support')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might use support.IPV6_ENABLED

def test_create_server_ipv6(self):
async def main():
srv = await asyncio.start_server(
lambda: None, '::1', 0, loop=self.loop)
try:
self.assertGreater(len(srv.sockets), 0)
finally:
srv.close()
await srv.wait_closed()

try:
self.loop.run_until_complete(main())
except OSError as ex:
if (hasattr(errno, 'EADDRNOTAVAIL') and
ex.errno == errno.EADDRNOTAVAIL):
self.skipTest('failed to bind to ::1')
else:
raise

def test_create_datagram_endpoint_wrong_sock(self):
sock = socket.socket(socket.AF_INET)
with sock:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix getaddrinfo to resolve IPv6 addresses correctly.
0