Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/tornado/netutil.py: 26%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#
2# Copyright 2011 Facebook
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16"""Miscellaneous network utility code."""
18import asyncio
19import concurrent.futures
20import errno
21import os
22import sys
23import socket
24import ssl
25import stat
27from tornado.concurrent import dummy_executor, run_on_executor
28from tornado.ioloop import IOLoop
29from tornado.util import Configurable, errno_from_exception
31from typing import List, Callable, Any, Type, Dict, Union, Tuple, Awaitable, Optional
33# Note that the naming of ssl.Purpose is confusing; the purpose
34# of a context is to authenticate the opposite side of the connection.
35_client_ssl_defaults = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
36_server_ssl_defaults = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
37if hasattr(ssl, "OP_NO_COMPRESSION"):
38 # See netutil.ssl_options_to_context
39 _client_ssl_defaults.options |= ssl.OP_NO_COMPRESSION
40 _server_ssl_defaults.options |= ssl.OP_NO_COMPRESSION
42# ThreadedResolver runs getaddrinfo on a thread. If the hostname is unicode,
43# getaddrinfo attempts to import encodings.idna. If this is done at
44# module-import time, the import lock is already held by the main thread,
45# leading to deadlock. Avoid it by caching the idna encoder on the main
46# thread now.
47"foo".encode("idna")
49# For undiagnosed reasons, 'latin1' codec may also need to be preloaded.
50"foo".encode("latin1")
52# Default backlog used when calling sock.listen()
53_DEFAULT_BACKLOG = 128
56def bind_sockets(
57 port: int,
58 address: Optional[str] = None,
59 family: socket.AddressFamily = socket.AF_UNSPEC,
60 backlog: int = _DEFAULT_BACKLOG,
61 flags: Optional[int] = None,
62 reuse_port: bool = False,
63) -> List[socket.socket]:
64 """Creates listening sockets bound to the given port and address.
66 Returns a list of socket objects (multiple sockets are returned if
67 the given address maps to multiple IP addresses, which is most common
68 for mixed IPv4 and IPv6 use).
70 Address may be either an IP address or hostname. If it's a hostname,
71 the server will listen on all IP addresses associated with the
72 name. Address may be an empty string or None to listen on all
73 available interfaces. Family may be set to either `socket.AF_INET`
74 or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise
75 both will be used if available.
77 The ``backlog`` argument has the same meaning as for
78 `socket.listen() <socket.socket.listen>`.
80 ``flags`` is a bitmask of AI_* flags to `~socket.getaddrinfo`, like
81 ``socket.AI_PASSIVE | socket.AI_NUMERICHOST``.
83 ``reuse_port`` option sets ``SO_REUSEPORT`` option for every socket
84 in the list. If your platform doesn't support this option ValueError will
85 be raised.
86 """
87 if reuse_port and not hasattr(socket, "SO_REUSEPORT"):
88 raise ValueError("the platform doesn't support SO_REUSEPORT")
90 sockets = []
91 if address == "":
92 address = None
93 if not socket.has_ipv6 and family == socket.AF_UNSPEC:
94 # Python can be compiled with --disable-ipv6, which causes
95 # operations on AF_INET6 sockets to fail, but does not
96 # automatically exclude those results from getaddrinfo
97 # results.
98 # http://bugs.python.org/issue16208
99 family = socket.AF_INET
100 if flags is None:
101 flags = socket.AI_PASSIVE
102 bound_port = None
103 unique_addresses = set() # type: set
104 for res in sorted(
105 socket.getaddrinfo(address, port, family, socket.SOCK_STREAM, 0, flags),
106 key=lambda x: x[0],
107 ):
108 if res in unique_addresses:
109 continue
111 unique_addresses.add(res)
113 af, socktype, proto, canonname, sockaddr = res
114 if (
115 sys.platform == "darwin"
116 and address == "localhost"
117 and af == socket.AF_INET6
118 and sockaddr[3] != 0 # type: ignore
119 ):
120 # Mac OS X includes a link-local address fe80::1%lo0 in the
121 # getaddrinfo results for 'localhost'. However, the firewall
122 # doesn't understand that this is a local address and will
123 # prompt for access (often repeatedly, due to an apparent
124 # bug in its ability to remember granting access to an
125 # application). Skip these addresses.
126 continue
127 try:
128 sock = socket.socket(af, socktype, proto)
129 except OSError as e:
130 if errno_from_exception(e) == errno.EAFNOSUPPORT:
131 continue
132 raise
133 if os.name != "nt":
134 try:
135 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
136 except OSError as e:
137 if errno_from_exception(e) != errno.ENOPROTOOPT:
138 # Hurd doesn't support SO_REUSEADDR.
139 raise
140 if reuse_port:
141 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
142 if af == socket.AF_INET6:
143 # On linux, ipv6 sockets accept ipv4 too by default,
144 # but this makes it impossible to bind to both
145 # 0.0.0.0 in ipv4 and :: in ipv6. On other systems,
146 # separate sockets *must* be used to listen for both ipv4
147 # and ipv6. For consistency, always disable ipv4 on our
148 # ipv6 sockets and use a separate ipv4 socket when needed.
149 #
150 # Python 2.x on windows doesn't have IPPROTO_IPV6.
151 if hasattr(socket, "IPPROTO_IPV6"):
152 sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
154 # automatic port allocation with port=None
155 # should bind on the same port on IPv4 and IPv6
156 host, requested_port = sockaddr[:2]
157 if requested_port == 0 and bound_port is not None:
158 sockaddr = tuple([host, bound_port] + list(sockaddr[2:]))
160 sock.setblocking(False)
161 try:
162 sock.bind(sockaddr)
163 except OSError as e:
164 if (
165 errno_from_exception(e) == errno.EADDRNOTAVAIL
166 and address == "localhost"
167 and sockaddr[0] == "::1"
168 ):
169 # On some systems (most notably docker with default
170 # configurations), ipv6 is partially disabled:
171 # socket.has_ipv6 is true, we can create AF_INET6
172 # sockets, and getaddrinfo("localhost", ...,
173 # AF_PASSIVE) resolves to ::1, but we get an error
174 # when binding.
175 #
176 # Swallow the error, but only for this specific case.
177 # If EADDRNOTAVAIL occurs in other situations, it
178 # might be a real problem like a typo in a
179 # configuration.
180 sock.close()
181 continue
182 else:
183 raise
184 bound_port = sock.getsockname()[1]
185 sock.listen(backlog)
186 sockets.append(sock)
187 return sockets
190if hasattr(socket, "AF_UNIX"):
192 def bind_unix_socket(
193 file: str, mode: int = 0o600, backlog: int = _DEFAULT_BACKLOG
194 ) -> socket.socket:
195 """Creates a listening unix socket.
197 If a socket with the given name already exists, it will be deleted.
198 If any other file with that name exists, an exception will be
199 raised.
201 Returns a socket object (not a list of socket objects like
202 `bind_sockets`)
203 """
204 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
205 try:
206 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
207 except OSError as e:
208 if errno_from_exception(e) != errno.ENOPROTOOPT:
209 # Hurd doesn't support SO_REUSEADDR
210 raise
211 sock.setblocking(False)
212 # File names comprising of an initial null-byte denote an abstract
213 # namespace, on Linux, and therefore are not subject to file system
214 # orientated processing.
215 if not file.startswith("\0"):
216 try:
217 st = os.stat(file)
218 except FileNotFoundError:
219 pass
220 else:
221 if stat.S_ISSOCK(st.st_mode):
222 os.remove(file)
223 else:
224 raise ValueError("File %s exists and is not a socket", file)
225 sock.bind(file)
226 os.chmod(file, mode)
227 else:
228 sock.bind(file)
229 sock.listen(backlog)
230 return sock
233def add_accept_handler(
234 sock: socket.socket, callback: Callable[[socket.socket, Any], None]
235) -> Callable[[], None]:
236 """Adds an `.IOLoop` event handler to accept new connections on ``sock``.
238 When a connection is accepted, ``callback(connection, address)`` will
239 be run (``connection`` is a socket object, and ``address`` is the
240 address of the other end of the connection). Note that this signature
241 is different from the ``callback(fd, events)`` signature used for
242 `.IOLoop` handlers.
244 A callable is returned which, when called, will remove the `.IOLoop`
245 event handler and stop processing further incoming connections.
247 .. versionchanged:: 5.0
248 The ``io_loop`` argument (deprecated since version 4.1) has been removed.
250 .. versionchanged:: 5.0
251 A callable is returned (``None`` was returned before).
252 """
253 io_loop = IOLoop.current()
254 removed = [False]
256 def accept_handler(fd: socket.socket, events: int) -> None:
257 # More connections may come in while we're handling callbacks;
258 # to prevent starvation of other tasks we must limit the number
259 # of connections we accept at a time. Ideally we would accept
260 # up to the number of connections that were waiting when we
261 # entered this method, but this information is not available
262 # (and rearranging this method to call accept() as many times
263 # as possible before running any callbacks would have adverse
264 # effects on load balancing in multiprocess configurations).
265 # Instead, we use the (default) listen backlog as a rough
266 # heuristic for the number of connections we can reasonably
267 # accept at once.
268 for i in range(_DEFAULT_BACKLOG):
269 if removed[0]:
270 # The socket was probably closed
271 return
272 try:
273 connection, address = sock.accept()
274 except BlockingIOError:
275 # EWOULDBLOCK indicates we have accepted every
276 # connection that is available.
277 return
278 except ConnectionAbortedError:
279 # ECONNABORTED indicates that there was a connection
280 # but it was closed while still in the accept queue.
281 # (observed on FreeBSD).
282 continue
283 callback(connection, address)
285 def remove_handler() -> None:
286 io_loop.remove_handler(sock)
287 removed[0] = True
289 io_loop.add_handler(sock, accept_handler, IOLoop.READ)
290 return remove_handler
293def is_valid_ip(ip: str) -> bool:
294 """Returns ``True`` if the given string is a well-formed IP address.
296 Supports IPv4 and IPv6.
297 """
298 if not ip or "\x00" in ip:
299 # getaddrinfo resolves empty strings to localhost, and truncates
300 # on zero bytes.
301 return False
302 try:
303 res = socket.getaddrinfo(
304 ip, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_NUMERICHOST
305 )
306 return bool(res)
307 except socket.gaierror as e:
308 if e.args[0] == socket.EAI_NONAME:
309 return False
310 raise
311 except UnicodeError:
312 # `socket.getaddrinfo` will raise a UnicodeError from the
313 # `idna` decoder if the input is longer than 63 characters,
314 # even for socket.AI_NUMERICHOST. See
315 # https://bugs.python.org/issue32958 for discussion
316 return False
317 return True
320class Resolver(Configurable):
321 """Configurable asynchronous DNS resolver interface.
323 By default, a blocking implementation is used (which simply calls
324 `socket.getaddrinfo`). An alternative implementation can be
325 chosen with the `Resolver.configure <.Configurable.configure>`
326 class method::
328 Resolver.configure('tornado.netutil.ThreadedResolver')
330 The implementations of this interface included with Tornado are
332 * `tornado.netutil.DefaultLoopResolver`
333 * `tornado.netutil.DefaultExecutorResolver` (deprecated)
334 * `tornado.netutil.BlockingResolver` (deprecated)
335 * `tornado.netutil.ThreadedResolver` (deprecated)
336 * `tornado.netutil.OverrideResolver`
337 * `tornado.platform.caresresolver.CaresResolver` (deprecated)
339 .. versionchanged:: 5.0
340 The default implementation has changed from `BlockingResolver` to
341 `DefaultExecutorResolver`.
343 .. versionchanged:: 6.2
344 The default implementation has changed from `DefaultExecutorResolver` to
345 `DefaultLoopResolver`.
346 """
348 @classmethod
349 def configurable_base(cls) -> Type["Resolver"]:
350 return Resolver
352 @classmethod
353 def configurable_default(cls) -> Type["Resolver"]:
354 return DefaultLoopResolver
356 def resolve(
357 self, host: str, port: int, family: socket.AddressFamily = socket.AF_UNSPEC
358 ) -> Awaitable[List[Tuple[int, Any]]]:
359 """Resolves an address.
361 The ``host`` argument is a string which may be a hostname or a
362 literal IP address.
364 Returns a `.Future` whose result is a list of (family,
365 address) pairs, where address is a tuple suitable to pass to
366 `socket.connect <socket.socket.connect>` (i.e. a ``(host,
367 port)`` pair for IPv4; additional fields may be present for
368 IPv6). If a ``callback`` is passed, it will be run with the
369 result as an argument when it is complete.
371 :raises IOError: if the address cannot be resolved.
373 .. versionchanged:: 4.4
374 Standardized all implementations to raise `IOError`.
376 .. versionchanged:: 6.0 The ``callback`` argument was removed.
377 Use the returned awaitable object instead.
379 """
380 raise NotImplementedError()
382 def close(self) -> None:
383 """Closes the `Resolver`, freeing any resources used.
385 .. versionadded:: 3.1
387 """
388 pass
391def _resolve_addr(
392 host: str, port: int, family: socket.AddressFamily = socket.AF_UNSPEC
393) -> List[Tuple[int, Any]]:
394 # On Solaris, getaddrinfo fails if the given port is not found
395 # in /etc/services and no socket type is given, so we must pass
396 # one here. The socket type used here doesn't seem to actually
397 # matter (we discard the one we get back in the results),
398 # so the addresses we return should still be usable with SOCK_DGRAM.
399 addrinfo = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM)
400 results = []
401 for fam, socktype, proto, canonname, address in addrinfo:
402 results.append((fam, address))
403 return results # type: ignore
406class DefaultExecutorResolver(Resolver):
407 """Resolver implementation using `.IOLoop.run_in_executor`.
409 .. versionadded:: 5.0
411 .. deprecated:: 6.2
413 Use `DefaultLoopResolver` instead.
414 """
416 async def resolve(
417 self, host: str, port: int, family: socket.AddressFamily = socket.AF_UNSPEC
418 ) -> List[Tuple[int, Any]]:
419 result = await IOLoop.current().run_in_executor(
420 None, _resolve_addr, host, port, family
421 )
422 return result
425class DefaultLoopResolver(Resolver):
426 """Resolver implementation using `asyncio.loop.getaddrinfo`."""
428 async def resolve(
429 self, host: str, port: int, family: socket.AddressFamily = socket.AF_UNSPEC
430 ) -> List[Tuple[int, Any]]:
431 # On Solaris, getaddrinfo fails if the given port is not found
432 # in /etc/services and no socket type is given, so we must pass
433 # one here. The socket type used here doesn't seem to actually
434 # matter (we discard the one we get back in the results),
435 # so the addresses we return should still be usable with SOCK_DGRAM.
436 return [
437 (fam, address)
438 for fam, _, _, _, address in await asyncio.get_running_loop().getaddrinfo(
439 host, port, family=family, type=socket.SOCK_STREAM
440 )
441 ]
444class ExecutorResolver(Resolver):
445 """Resolver implementation using a `concurrent.futures.Executor`.
447 Use this instead of `ThreadedResolver` when you require additional
448 control over the executor being used.
450 The executor will be shut down when the resolver is closed unless
451 ``close_resolver=False``; use this if you want to reuse the same
452 executor elsewhere.
454 .. versionchanged:: 5.0
455 The ``io_loop`` argument (deprecated since version 4.1) has been removed.
457 .. deprecated:: 5.0
458 The default `Resolver` now uses `asyncio.loop.getaddrinfo`;
459 use that instead of this class.
460 """
462 def initialize(
463 self,
464 executor: Optional[concurrent.futures.Executor] = None,
465 close_executor: bool = True,
466 ) -> None:
467 if executor is not None:
468 self.executor = executor
469 self.close_executor = close_executor
470 else:
471 self.executor = dummy_executor
472 self.close_executor = False
474 def close(self) -> None:
475 if self.close_executor:
476 self.executor.shutdown()
477 self.executor = None # type: ignore
479 @run_on_executor
480 def resolve(
481 self, host: str, port: int, family: socket.AddressFamily = socket.AF_UNSPEC
482 ) -> List[Tuple[int, Any]]:
483 return _resolve_addr(host, port, family)
486class BlockingResolver(ExecutorResolver):
487 """Default `Resolver` implementation, using `socket.getaddrinfo`.
489 The `.IOLoop` will be blocked during the resolution, although the
490 callback will not be run until the next `.IOLoop` iteration.
492 .. deprecated:: 5.0
493 The default `Resolver` now uses `.IOLoop.run_in_executor`; use that instead
494 of this class.
495 """
497 def initialize(self) -> None: # type: ignore
498 super().initialize()
501class ThreadedResolver(ExecutorResolver):
502 """Multithreaded non-blocking `Resolver` implementation.
504 The thread pool size can be configured with::
506 Resolver.configure('tornado.netutil.ThreadedResolver',
507 num_threads=10)
509 .. versionchanged:: 3.1
510 All ``ThreadedResolvers`` share a single thread pool, whose
511 size is set by the first one to be created.
513 .. deprecated:: 5.0
514 The default `Resolver` now uses `.IOLoop.run_in_executor`; use that instead
515 of this class.
516 """
518 _threadpool = None # type: ignore
519 _threadpool_pid = None # type: int
521 def initialize(self, num_threads: int = 10) -> None: # type: ignore
522 threadpool = ThreadedResolver._create_threadpool(num_threads)
523 super().initialize(executor=threadpool, close_executor=False)
525 @classmethod
526 def _create_threadpool(
527 cls, num_threads: int
528 ) -> concurrent.futures.ThreadPoolExecutor:
529 pid = os.getpid()
530 if cls._threadpool_pid != pid:
531 # Threads cannot survive after a fork, so if our pid isn't what it
532 # was when we created the pool then delete it.
533 cls._threadpool = None
534 if cls._threadpool is None:
535 cls._threadpool = concurrent.futures.ThreadPoolExecutor(num_threads)
536 cls._threadpool_pid = pid
537 return cls._threadpool
540class OverrideResolver(Resolver):
541 """Wraps a resolver with a mapping of overrides.
543 This can be used to make local DNS changes (e.g. for testing)
544 without modifying system-wide settings.
546 The mapping can be in three formats::
548 {
549 # Hostname to host or ip
550 "example.com": "127.0.1.1",
552 # Host+port to host+port
553 ("login.example.com", 443): ("localhost", 1443),
555 # Host+port+address family to host+port
556 ("login.example.com", 443, socket.AF_INET6): ("::1", 1443),
557 }
559 .. versionchanged:: 5.0
560 Added support for host-port-family triplets.
561 """
563 def initialize(self, resolver: Resolver, mapping: dict) -> None:
564 self.resolver = resolver
565 self.mapping = mapping
567 def close(self) -> None:
568 self.resolver.close()
570 def resolve(
571 self, host: str, port: int, family: socket.AddressFamily = socket.AF_UNSPEC
572 ) -> Awaitable[List[Tuple[int, Any]]]:
573 if (host, port, family) in self.mapping:
574 host, port = self.mapping[(host, port, family)]
575 elif (host, port) in self.mapping:
576 host, port = self.mapping[(host, port)]
577 elif host in self.mapping:
578 host = self.mapping[host]
579 return self.resolver.resolve(host, port, family)
582# These are the keyword arguments to ssl.wrap_socket that must be translated
583# to their SSLContext equivalents (the other arguments are still passed
584# to SSLContext.wrap_socket).
585_SSL_CONTEXT_KEYWORDS = frozenset(
586 ["ssl_version", "certfile", "keyfile", "cert_reqs", "ca_certs", "ciphers"]
587)
590def ssl_options_to_context(
591 ssl_options: Union[Dict[str, Any], ssl.SSLContext],
592 server_side: Optional[bool] = None,
593) -> ssl.SSLContext:
594 """Try to convert an ``ssl_options`` dictionary to an
595 `~ssl.SSLContext` object.
597 The ``ssl_options`` argument may be either an `ssl.SSLContext` object or a dictionary containing
598 keywords to be passed to ``ssl.SSLContext.wrap_socket``. This function converts the dict form
599 to its `~ssl.SSLContext` equivalent, and may be used when a component which accepts both forms
600 needs to upgrade to the `~ssl.SSLContext` version to use features like SNI or ALPN.
602 .. versionchanged:: 6.2
604 Added server_side argument. Omitting this argument will result in a DeprecationWarning on
605 Python 3.10.
607 """
608 if isinstance(ssl_options, ssl.SSLContext):
609 return ssl_options
610 assert isinstance(ssl_options, dict)
611 assert all(k in _SSL_CONTEXT_KEYWORDS for k in ssl_options), ssl_options
612 # TODO: Now that we have the server_side argument, can we switch to
613 # create_default_context or would that change behavior?
614 default_version = ssl.PROTOCOL_TLS
615 if server_side:
616 default_version = ssl.PROTOCOL_TLS_SERVER
617 elif server_side is not None:
618 default_version = ssl.PROTOCOL_TLS_CLIENT
619 context = ssl.SSLContext(ssl_options.get("ssl_version", default_version))
620 if "certfile" in ssl_options:
621 context.load_cert_chain(
622 ssl_options["certfile"], ssl_options.get("keyfile", None)
623 )
624 if "cert_reqs" in ssl_options:
625 if ssl_options["cert_reqs"] == ssl.CERT_NONE:
626 # This may have been set automatically by PROTOCOL_TLS_CLIENT but is
627 # incompatible with CERT_NONE so we must manually clear it.
628 context.check_hostname = False
629 context.verify_mode = ssl_options["cert_reqs"]
630 if "ca_certs" in ssl_options:
631 context.load_verify_locations(ssl_options["ca_certs"])
632 if "ciphers" in ssl_options:
633 context.set_ciphers(ssl_options["ciphers"])
634 if hasattr(ssl, "OP_NO_COMPRESSION"):
635 # Disable TLS compression to avoid CRIME and related attacks.
636 # This constant depends on openssl version 1.0.
637 # TODO: Do we need to do this ourselves or can we trust
638 # the defaults?
639 context.options |= ssl.OP_NO_COMPRESSION
640 return context
643def ssl_wrap_socket(
644 socket: socket.socket,
645 ssl_options: Union[Dict[str, Any], ssl.SSLContext],
646 server_hostname: Optional[str] = None,
647 server_side: Optional[bool] = None,
648 **kwargs: Any,
649) -> ssl.SSLSocket:
650 """Returns an ``ssl.SSLSocket`` wrapping the given socket.
652 ``ssl_options`` may be either an `ssl.SSLContext` object or a
653 dictionary (as accepted by `ssl_options_to_context`). Additional
654 keyword arguments are passed to `ssl.SSLContext.wrap_socket`.
656 .. versionchanged:: 6.2
658 Added server_side argument. Omitting this argument will
659 result in a DeprecationWarning on Python 3.10.
660 """
661 context = ssl_options_to_context(ssl_options, server_side=server_side)
662 if server_side is None:
663 server_side = False
664 assert ssl.HAS_SNI
665 # TODO: add a unittest for hostname validation (python added server-side SNI support in 3.4)
666 # In the meantime it can be manually tested with
667 # python3 -m tornado.httpclient https://sni.velox.ch
668 return context.wrap_socket(
669 socket, server_hostname=server_hostname, server_side=server_side, **kwargs
670 )