Skip to content

Commit

Permalink
Return IPv6 address in resolve_ip, if both v4 and v6 are available, b…
Browse files Browse the repository at this point in the history
…ut only v6 is connectable.
  • Loading branch information
juangj authored and AutomatedTester committed May 18, 2016
1 parent 8f1721d commit af28d14
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 11 additions & 3 deletions py/selenium/webdriver/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ def free_port():
free_socket.close()
return port

def resolve_ip(host):
def find_connectable_ip(host, port=None):
"""Resolve a hostname to an IP, preferring IPv4 addresses.
We prefer IPv4 so that we don't change behavior from previous IPv4-only
implementations, and because some drivers (e.g., FirefoxDriver) do not
support IPv6 connections.
If the optional port number is provided, only IPs that listen on the given
port are considered.
:Args:
- host - A hostname.
- port - Optional port number.
:Returns:
A single IP address, as a string. If any IPv4 address is found, one is
Expand All @@ -62,9 +66,13 @@ def resolve_ip(host):

ip = None
for family, _, _, _, sockaddr in addrinfos:
if family == socket.AF_INET:
connectable = True
if port:
connectable = is_connectable(port, sockaddr[0])

if connectable and family == socket.AF_INET:
return sockaddr[0]
if not ip and family == socket.AF_INET6:
if connectable and not ip and family == socket.AF_INET6:
ip = sockaddr[0]
return ip

Expand Down
4 changes: 3 additions & 1 deletion py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def __init__(self, remote_server_addr, keep_alive=False, resolve_ip=True):
parsed_url = parse.urlparse(remote_server_addr)
addr = ""
if parsed_url.hostname and resolve_ip:
ip = common_utils.resolve_ip(parsed_url.hostname)
port = parsed_url.port or None
ip = common_utils.find_connectable_ip(parsed_url.hostname,
port=port)
if ip:
netloc = ip
addr = netloc
Expand Down

0 comments on commit af28d14

Please sign in to comment.