Skip to content

Commit eb5855c

Browse files
committed
Resolve short names using .local suffix with inet:getaddr/2
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 <pguyot@kallisys.net>
1 parent 279c44a commit eb5855c

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

libs/estdlib/src/inet.erl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ getaddr(Name, Family) ->
108108
[] -> {error, nxdomain};
109109
[IPAddr | _] -> {ok, IPAddr}
110110
end;
111+
{error, eainoname} ->
112+
case string:split(Name, ".") of
113+
[Name] ->
114+
% BEAM succeeds to resolve short names even if gethostbyname(2) fails.
115+
% Work around for distribution by trying to add .local suffix.
116+
case net:getaddrinfo(Name ++ ".local") of
117+
{ok, ResultsLocal} ->
118+
FilteredLocal = [
119+
Addr
120+
|| #{family := F, addr := #{addr := Addr}} <- ResultsLocal,
121+
F =:= Family,
122+
Addr =:= {127, 0, 0, 1} orelse Addr =:= {0, 0, 0, 0, 0, 0, 0, 1}
123+
],
124+
case FilteredLocal of
125+
[] -> {error, nxdomain};
126+
[LocalIPAddr | _] -> {ok, LocalIPAddr}
127+
end;
128+
_ ->
129+
{error, nxdomain}
130+
end;
131+
_ ->
132+
{error, nxdomain}
133+
end;
111134
{error, _} = Err ->
112135
Err
113136
catch

libs/estdlib/src/socket_dist.erl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ setup(Node, Type, MyNode, LongOrShortNames, SetupTime) ->
128128
dist_util:net_ticker_spawn_options()
129129
).
130130

131-
do_setup(Kernel, Node, Type, MyNode, _LongOrShortNames, SetupTime) ->
131+
do_setup(Kernel, Node, Type, MyNode, _LongNames, SetupTime) ->
132132
case string:split(atom_to_list(Node), "@") of
133-
[Name, Address] ->
133+
[Name, Host] ->
134134
Timer = dist_util:start_timer(SetupTime),
135-
case inet:getaddr(Address, inet) of
135+
case inet:getaddr(Host, inet) of
136136
{ok, Ip} ->
137137
dist_util:reset_timer(Timer),
138138
ErlEpmd = net_kernel:epmd_module(),
@@ -159,17 +159,17 @@ do_setup(Kernel, Node, Type, MyNode, _LongOrShortNames, SetupTime) ->
159159
Timer
160160
),
161161
dist_util:handshake_we_started(HSData);
162-
_ ->
163-
?shutdown(Node)
162+
Other1 ->
163+
?shutdown2(Node, {unexpected, Other1})
164164
end;
165-
_ ->
166-
?shutdown(Node)
165+
Other2 ->
166+
?shutdown2(Node, {unexpected, Other2})
167167
end;
168-
_ ->
169-
?shutdown(Node)
168+
Other3 ->
169+
?shutdown2(Node, {unexpected, Other3})
170170
end;
171-
_ ->
172-
?shutdown(Node)
171+
Other4 ->
172+
?shutdown2(Node, {unexpected, Other4})
173173
end.
174174

175175
close(Listen) ->

0 commit comments

Comments
 (0)