diff options
author | Yury Selivanov <yury@magic.io> | 2016-11-09 20:47:00 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2016-11-09 20:47:00 (GMT) |
commit | a1a8b7d3d7f628aec31be364c77cbb3e21cdbc0b (patch) | |
tree | f1c1fa5ae61d965d131a437904bd1712c7a6f017 /Lib/asyncio | |
parent | d2fd3599abeed393ccdf4ee5cf1c7b346ba4a022 (diff) | |
download | cpython-a1a8b7d3d7f628aec31be364c77cbb3e21cdbc0b.zip cpython-a1a8b7d3d7f628aec31be364c77cbb3e21cdbc0b.tar.gz cpython-a1a8b7d3d7f628aec31be364c77cbb3e21cdbc0b.tar.bz2 |
Issue #28652: Make loop methods reject socket kinds they do not support.
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/base_events.py | 56 | ||||
-rw-r--r-- | Lib/asyncio/unix_events.py | 4 |
2 files changed, 47 insertions, 13 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py index 6488f23..aa78367 100644 --- a/Lib/asyncio/base_events.py +++ b/Lib/asyncio/base_events.py @@ -84,12 +84,26 @@ def _set_reuseport(sock): 'SO_REUSEPORT defined but not implemented.') -# Linux's sock.type is a bitmask that can include extra info about socket. -_SOCKET_TYPE_MASK = 0 -if hasattr(socket, 'SOCK_NONBLOCK'): - _SOCKET_TYPE_MASK |= socket.SOCK_NONBLOCK -if hasattr(socket, 'SOCK_CLOEXEC'): - _SOCKET_TYPE_MASK |= socket.SOCK_CLOEXEC +def _is_stream_socket(sock): + # Linux's socket.type is a bitmask that can include extra info + # about socket, therefore we can't do simple + # `sock_type == socket.SOCK_STREAM`. + return (sock.type & socket.SOCK_STREAM) == socket.SOCK_STREAM + + +def _is_dgram_socket(sock): + # Linux's socket.type is a bitmask that can include extra info + # about socket, therefore we can't do simple + # `sock_type == socket.SOCK_DGRAM`. + return (sock.type & socket.SOCK_DGRAM) == socket.SOCK_DGRAM + + +def _is_ip_socket(sock): + if sock.family == socket.AF_INET: + return True + if hasattr(socket, 'AF_INET6') and sock.family == socket.AF_INET6: + return True + return False def _ipaddr_info(host, port, family, type, proto): @@ -102,8 +116,12 @@ def _ipaddr_info(host, port, family, type, proto): host is None: return None - type &= ~_SOCKET_TYPE_MASK if type == socket.SOCK_STREAM: + # Linux only: + # getaddrinfo() can raise when socket.type is a bit mask. + # So if socket.type is a bit mask of SOCK_STREAM, and say + # SOCK_NONBLOCK, we simply return None, which will trigger + # a call to getaddrinfo() letting it process this request. proto = socket.IPPROTO_TCP elif type == socket.SOCK_DGRAM: proto = socket.IPPROTO_UDP @@ -124,7 +142,9 @@ def _ipaddr_info(host, port, family, type, proto): return None if family == socket.AF_UNSPEC: - afs = [socket.AF_INET, socket.AF_INET6] + afs = [socket.AF_INET] + if hasattr(socket, 'AF_INET6'): + afs.append(socket.AF_INET6) else: afs = [family] @@ -771,9 +791,13 @@ class BaseEventLoop(events.AbstractEventLoop): raise OSError('Multiple exceptions: {}'.format( ', '.join(str(exc) for exc in exceptions))) - elif sock is None: - raise ValueError( - 'host and port was not specified and no sock specified') + else: + if sock is None: + raise ValueError( + 'host and port was not specified and no sock specified') + if not _is_stream_socket(sock) or not _is_ip_socket(sock): + raise ValueError( + 'A TCP Stream Socket was expected, got {!r}'.format(sock)) transport, protocol = yield from self._create_connection_transport( sock, protocol_factory, ssl, server_hostname) @@ -817,6 +841,9 @@ class BaseEventLoop(events.AbstractEventLoop): allow_broadcast=None, sock=None): """Create datagram connection.""" if sock is not None: + if not _is_dgram_socket(sock): + raise ValueError( + 'A UDP Socket was expected, got {!r}'.format(sock)) if (local_addr or remote_addr or family or proto or flags or reuse_address or reuse_port or allow_broadcast): @@ -1027,6 +1054,9 @@ class BaseEventLoop(events.AbstractEventLoop): else: if sock is None: raise ValueError('Neither host/port nor sock were specified') + if not _is_stream_socket(sock) or not _is_ip_socket(sock): + raise ValueError( + 'A TCP Stream Socket was expected, got {!r}'.format(sock)) sockets = [sock] server = Server(self, sockets) @@ -1048,6 +1078,10 @@ class BaseEventLoop(events.AbstractEventLoop): This method is a coroutine. When completed, the coroutine returns a (transport, protocol) pair. """ + if not _is_stream_socket(sock): + raise ValueError( + 'A Stream Socket was expected, got {!r}'.format(sock)) + transport, protocol = yield from self._create_connection_transport( sock, protocol_factory, ssl, '', server_side=True) if self._debug: diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 65b61db..788a5a0 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -235,7 +235,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): if sock is None: raise ValueError('no path and sock were specified') if (sock.family != socket.AF_UNIX or - sock.type != socket.SOCK_STREAM): + not base_events._is_stream_socket(sock)): raise ValueError( 'A UNIX Domain Stream Socket was expected, got {!r}' .format(sock)) @@ -289,7 +289,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): 'path was not specified, and no sock specified') if (sock.family != socket.AF_UNIX or - sock.type != socket.SOCK_STREAM): + not base_events._is_stream_socket(sock)): raise ValueError( 'A UNIX Domain Stream Socket was expected, got {!r}' .format(sock)) |