Skip to content

Commit

Permalink
fix IP parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Oct 10, 2024
1 parent 420d855 commit 033b181
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/hackney_happy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
connect(Hostname, Port, Opts) ->
connect(Hostname, Port, Opts, ?CONNECT_TIMEOUT).

connect(Hostname, Port, Opts, Timeout) ->
connect(Hostname0, Port, Opts, Timeout) ->
Hostname = parse_address(Hostname0),
case hackney_cidr:is_ipv6(Hostname) of
true ->
?report_debug("connect using IPv6", [{hostname, Hostname}, {port, Port}]),
Expand Down Expand Up @@ -39,6 +40,19 @@ connect(Hostname, Port, Opts, Timeout) ->
end
end
end.

parse_address(IPTuple) when is_tuple(IPTuple) -> IPTuple;
parse_address(IPBin) when is_binary(IPBin) ->
parse_address(binary_to_list(IPBin));
%% IPv6 string with brackets
parse_address("[" ++ IPString) ->
parse_address(lists:sublist(IPString, length(IPString) - 1));
parse_address(IPString) ->
case inet:parse_address(IPString) of
{ok, IP} -> IP;
{error, _} -> IPString
end.


getaddrs(Hostname) ->
IP6Addrs = [{Addr, 'inet6'} || Addr <- getbyname(Hostname, 'aaaa')],
Expand All @@ -48,7 +62,7 @@ getaddrs(Hostname) ->
getbyname(Hostname, Type) ->
case (catch inet_res:getbyname(Hostname, Type)) of
{'ok', #hostent{h_addr_list=AddrList}} -> lists:usort(AddrList);
{error, nxdomain} = Error ->
{error, nxdomain} ->
case inet:parse_address(Hostname) of
{ok, IP} -> [IP];
_ -> []
Expand Down

0 comments on commit 033b181

Please sign in to comment.