Add IPv6 support to create_websocket()

The current implementation is hardcoded to use AF_INET, which means
IPv4. We need to be able to support IPv6 connections, too, so we use the
socket.getaddrinfo() function as described in [1] in order to provide a
more generic solution.

[1] https://docs.python.org/2/library/socket.html

Change-Id: I2ccbe3b25e79031a3c961871034c0c1b94d7ee80
Closes-Bug: 1719575
Related-Bug: 1656329
This commit is contained in:
Jens Harbott 2017-09-27 10:29:34 +00:00
parent 86a0b0deb7
commit 6bc422d2eb
1 changed files with 14 additions and 6 deletions

View File

@ -287,13 +287,21 @@ def shelve_server(servers_client, server_id, force_shelve_offload=False):
def create_websocket(url):
url = urlparse.urlparse(url)
if url.scheme == 'https':
client_socket = ssl.wrap_socket(socket.socket(socket.AF_INET,
socket.SOCK_STREAM))
for res in socket.getaddrinfo(url.hostname, url.port,
socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, _, sa = res
client_socket = socket.socket(af, socktype, proto)
if url.scheme == 'https':
client_socket = ssl.wrap_socket(client_socket)
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
client_socket.connect(sa)
except socket.error:
client_socket.close()
continue
break
else:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client_socket.connect((url.hostname, url.port))
raise socket.error('WebSocket creation failed')
# Turn the Socket into a WebSocket to do the communication
return _WebSocket(client_socket, url)