Skip to content

Commit dca65ca

Browse files
committed
fix: Use resolved socket address for SSL connections in M2SSLTransport
Use the socket address obtained from getaddrinfo() instead of reconstructing it as (host, port). This ensures proper connection to IPv6 addresses and respects the resolved address family. Also set the SNI hostname using set1_host() to maintain proper TLS server name indication.
1 parent d111a74 commit dca65ca

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/DIRAC/Core/DISET/private/Transports/M2SSLTransport.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,12 @@ def initAsClient(self):
116116
# The following piece of code was inspired by the python socket documentation
117117
# as well as the implementation of M2Crypto.httpslib.HTTPSConnection
118118

119-
# We ignore the returned sockaddr because SSL.Connection.connect needs
120-
# a host name.
119+
# Get all available addresses (IPv6 and IPv4) and try them in order
121120
try:
122121
addrInfoList = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
123122
except OSError as e:
124123
return S_ERROR(f"DNS lookup failed {e!r}")
125-
for family, _socketType, _proto, _canonname, _socketAddress in addrInfoList:
124+
for family, _socketType, _proto, _canonname, socketAddress in addrInfoList:
126125
try:
127126
self.oSocket = SSL.Connection(self.__ctx, family=family)
128127

@@ -138,7 +137,10 @@ def initAsClient(self):
138137
# set SNI server name since we know it at this point
139138
self.oSocket.set_tlsext_host_name(host)
140139

141-
self.oSocket.connect((host, port))
140+
# tell the connection which host we are connecting to so we can
141+
# use the address we obtained from DNS
142+
self.oSocket.set1_host(host)
143+
self.oSocket.connect(socketAddress)
142144

143145
# Once the connection is established, we can use the timeout
144146
# asked for RPC

0 commit comments

Comments
 (0)