Skip to content

Commit

Permalink
Resolve short names using .local suffix with inet:getaddr/2
Browse files Browse the repository at this point in the history
This allows to connect to localhost if shortnames are used and host
cannot be resolved. BEAM succeeds even if gethostbyname(2) fails,
typically on macOS 15.

Signed-off-by: Paul Guyot <[email protected]>
  • Loading branch information
pguyot committed Jan 25, 2025
1 parent 279c44a commit eb5855c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
23 changes: 23 additions & 0 deletions libs/estdlib/src/inet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ getaddr(Name, Family) ->
[] -> {error, nxdomain};
[IPAddr | _] -> {ok, IPAddr}
end;
{error, eainoname} ->
case string:split(Name, ".") of
[Name] ->
% BEAM succeeds to resolve short names even if gethostbyname(2) fails.
% Work around for distribution by trying to add .local suffix.
case net:getaddrinfo(Name ++ ".local") of
{ok, ResultsLocal} ->
FilteredLocal = [
Addr
|| #{family := F, addr := #{addr := Addr}} <- ResultsLocal,
F =:= Family,
Addr =:= {127, 0, 0, 1} orelse Addr =:= {0, 0, 0, 0, 0, 0, 0, 1}
],
case FilteredLocal of
[] -> {error, nxdomain};
[LocalIPAddr | _] -> {ok, LocalIPAddr}
end;
_ ->
{error, nxdomain}
end;
_ ->
{error, nxdomain}
end;
{error, _} = Err ->
Err
catch
Expand Down
22 changes: 11 additions & 11 deletions libs/estdlib/src/socket_dist.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ setup(Node, Type, MyNode, LongOrShortNames, SetupTime) ->
dist_util:net_ticker_spawn_options()
).

do_setup(Kernel, Node, Type, MyNode, _LongOrShortNames, SetupTime) ->
do_setup(Kernel, Node, Type, MyNode, _LongNames, SetupTime) ->
case string:split(atom_to_list(Node), "@") of
[Name, Address] ->
[Name, Host] ->
Timer = dist_util:start_timer(SetupTime),
case inet:getaddr(Address, inet) of
case inet:getaddr(Host, inet) of
{ok, Ip} ->
dist_util:reset_timer(Timer),
ErlEpmd = net_kernel:epmd_module(),
Expand All @@ -159,17 +159,17 @@ do_setup(Kernel, Node, Type, MyNode, _LongOrShortNames, SetupTime) ->
Timer
),
dist_util:handshake_we_started(HSData);
_ ->
?shutdown(Node)
Other1 ->
?shutdown2(Node, {unexpected, Other1})
end;
_ ->
?shutdown(Node)
Other2 ->
?shutdown2(Node, {unexpected, Other2})
end;
_ ->
?shutdown(Node)
Other3 ->
?shutdown2(Node, {unexpected, Other3})
end;
_ ->
?shutdown(Node)
Other4 ->
?shutdown2(Node, {unexpected, Other4})
end.

close(Listen) ->
Expand Down

0 comments on commit eb5855c

Please sign in to comment.