From f5640f564b93c8ceea8d4b27fa54abe37214d3e2 Mon Sep 17 00:00:00 2001 From: Filipe David Manana Date: Tue, 12 Apr 2011 19:39:08 +0100 Subject: [PATCH] More reliable IPv6 detection inet:gethostbyname/1 might fail for IPv6 literals, therefore check first if host is an IPv6 address literal. --- src/ibrowse_http_client.erl | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ibrowse_http_client.erl b/src/ibrowse_http_client.erl index c69b972..eb2bf31 100644 --- a/src/ibrowse_http_client.erl +++ b/src/ibrowse_http_client.erl @@ -496,10 +496,10 @@ do_connect(Host, Port, Options, _State, Timeout) -> get_sock_options(Host, Options, SSLOptions) -> Caller_socket_options = get_value(socket_options, Options, []), - Ipv6Options = case inet:gethostbyname(Host) of - {ok, #hostent{h_addrtype = inet6}} -> + Ipv6Options = case is_ipv6_host(Host) of + true -> [inet6]; - _ -> + false -> [] end, Other_sock_options = filter_sock_options(SSLOptions ++ Caller_socket_options ++ Ipv6Options), @@ -510,6 +510,21 @@ get_sock_options(Host, Options, SSLOptions) -> [binary, {active, false} | Other_sock_options] end. +is_ipv6_host(Host) -> + case inet_parse:address(Host) of + {ok, {_, _, _, _, _, _, _, _}} -> + true; + {ok, {_, _, _, _}} -> + false; + _ -> + case inet:gethostbyname(Host) of + {ok, #hostent{h_addrtype = inet6}} -> + true; + _ -> + false + end + end. + %% We don't want the caller to specify certain options filter_sock_options(Opts) -> lists:filter(fun({active, _}) ->