1515import collections
1616import contextlib
1717import copy
18- import ipaddress
1918import os
2019import platform
2120import socket
6160from pymongo .server_api import _add_to_command
6261from pymongo .server_type import SERVER_TYPE
6362from pymongo .socket_checker import SocketChecker
64- from pymongo .ssl_support import HAS_SNI as _HAVE_SNI
65- from pymongo .ssl_support import IPADDR_SAFE as _IPADDR_SAFE
66- from pymongo .ssl_support import SSLError as _SSLError
67-
68-
69- # For SNI support. According to RFC6066, section 3, IPv4 and IPv6 literals are
70- # not permitted for SNI hostname.
71- def is_ip_address (address ):
72- try :
73- ipaddress .ip_address (address )
74- return True
75- except (ValueError , UnicodeError ): # noqa: B014
76- return False
77-
63+ from pymongo .ssl_support import HAS_SNI , SSLError
7864
7965try :
8066 from fcntl import F_GETFD , F_SETFD , FD_CLOEXEC , fcntl
@@ -263,7 +249,7 @@ def _raise_connection_failure(
263249 msg = msg_prefix + msg
264250 if isinstance (error , socket .timeout ):
265251 raise NetworkTimeout (msg ) from error
266- elif isinstance (error , _SSLError ) and "timed out" in str (error ):
252+ elif isinstance (error , SSLError ) and "timed out" in str (error ):
267253 # Eventlet does not distinguish TLS network timeouts from other
268254 # SSLErrors (https://github.com/eventlet/eventlet/issues/692).
269255 # Luckily, we can work around this limitation because the phrase
@@ -924,7 +910,7 @@ def _raise_connection_failure(self, error):
924910 reason = ConnectionClosedReason .ERROR
925911 self .close_socket (reason )
926912 # SSLError from PyOpenSSL inherits directly from Exception.
927- if isinstance (error , (IOError , OSError , _SSLError )):
913+ if isinstance (error , (IOError , OSError , SSLError )):
928914 _raise_connection_failure (self .address , error )
929915 else :
930916 raise
@@ -1024,14 +1010,9 @@ def _configured_socket(address, options):
10241010 if ssl_context is not None :
10251011 host = address [0 ]
10261012 try :
1027- # According to RFC6066, section 3, IPv4 and IPv6 literals are
1028- # not permitted for SNI hostname.
1029- # Previous to Python 3.7 wrap_socket would blindly pass
1030- # IP addresses as SNI hostname.
1031- # https://bugs.python.org/issue32185
10321013 # We have to pass hostname / ip address to wrap_socket
10331014 # to use SSLContext.check_hostname.
1034- if _HAVE_SNI and ( not is_ip_address ( host ) or _IPADDR_SAFE ) :
1015+ if HAS_SNI :
10351016 sock = ssl_context .wrap_socket (sock , server_hostname = host )
10361017 else :
10371018 sock = ssl_context .wrap_socket (sock )
@@ -1040,15 +1021,15 @@ def _configured_socket(address, options):
10401021 # Raise _CertificateError directly like we do after match_hostname
10411022 # below.
10421023 raise
1043- except (IOError , OSError , _SSLError ) as exc : # noqa: B014
1024+ except (IOError , OSError , SSLError ) as exc : # noqa: B014
10441025 sock .close ()
10451026 # We raise AutoReconnect for transient and permanent SSL handshake
10461027 # failures alike. Permanent handshake failures, like protocol
10471028 # mismatch, will be turned into ServerSelectionTimeoutErrors later.
10481029 _raise_connection_failure (address , exc , "SSL handshake failed: " )
10491030 if (
10501031 ssl_context .verify_mode
1051- and not getattr ( ssl_context , " check_hostname" , False )
1032+ and not ssl_context . check_hostname
10521033 and not options .tls_allow_invalid_hostnames
10531034 ):
10541035 try :
@@ -1336,7 +1317,7 @@ def connect(self):
13361317 self .address , conn_id , ConnectionClosedReason .ERROR
13371318 )
13381319
1339- if isinstance (error , (IOError , OSError , _SSLError )):
1320+ if isinstance (error , (IOError , OSError , SSLError )):
13401321 _raise_connection_failure (self .address , error )
13411322
13421323 raise
0 commit comments