Skip to content

Commit e204e55

Browse files
authored
Merge pull request DIRACGrid#8352 from chrisburr/fix-ipv6-connect
[9.0] Use resolved socket address for SSL connections in M2SSLTransport
2 parents cfd0691 + dca65ca commit e204e55

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,18 @@ def initAsClient(self):
110110
if self.serverMode():
111111
raise RuntimeError("SSLTransport is in server mode.")
112112

113-
error = None
113+
errors = []
114114
host, port = self.stServerAddress
115115

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
@@ -151,12 +153,12 @@ def initAsClient(self):
151153
# They should be propagated upwards and caught by the BaseClient
152154
# not to enter the retry loop
153155
except OSError as e:
154-
error = f"{e}:{repr(e)}"
156+
errors.append(f"{socketAddress} {e}:{repr(e)}")
155157

156158
if self.oSocket is not None:
157159
self.close()
158160

159-
return S_ERROR(error)
161+
return S_ERROR("; ".join(errors))
160162

161163
def initAsServer(self):
162164
"""Prepare this server socket for use."""

0 commit comments

Comments
 (0)