Skip to content

Commit 7a7c90b

Browse files
garazdawisverker
authored andcommitted
kernel: Add -dist_listen false option
This option makes it so that the distribution no longer listens for incoming connections. This way the node does not need to occupy a tcp port, but it also cannot be part of any global groups.
1 parent a8cac32 commit 7a7c90b

6 files changed

Lines changed: 99 additions & 16 deletions

File tree

erts/doc/src/alt_dist.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@
231231
</p>
232232
</item>
233233

234+
<tag><marker id="address"/><c>address() -></c><br/>&nbsp;&nbsp;<c>Address</c></tag>
235+
<item>
236+
<p><c>address/0</c> is called in order to get the <c>Address</c> part of the
237+
<seealso marker="#listen"><c>listen/2</c></seealso> function without creating
238+
a listen socket. All fields except <c>address</c> have to be set in the returned
239+
record</p>
240+
<p>Example:</p>
241+
<code type="erl">address() ->
242+
{ok, Host} = inet:gethostname(),
243+
#net_address{ host = Host, protocol = tcp, family = inet6 }.
244+
</code>
245+
</item>
246+
234247
<tag><marker id="accept"/><c>accept(Listen) -></c><br/>&nbsp;&nbsp;<c>AcceptorPid</c></tag>
235248
<item>
236249
<p>

lib/crypto/src/crypto.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2517,7 +2517,7 @@ on_load() ->
25172517
filename:join([PrivDir, "lib",
25182518
erlang:system_info(system_architecture)]),
25192519
Candidate =
2520-
filelib:wildcard(filename:join([ArchLibDir,LibName ++ "*" ])),
2520+
filelib:wildcard(filename:join([ArchLibDir,LibName ++ "*" ]),erl_prim_loader),
25212521
case Candidate of
25222522
[] -> Error1;
25232523
_ ->

lib/kernel/src/global_group.erl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,16 +1312,20 @@ get_own_nodes() ->
13121312
%%% -hidden command line argument
13131313
%%%====================================================================================
13141314
publish_arg() ->
1315-
case init:get_argument(hidden) of
1316-
{ok,[[]]} ->
1317-
hidden;
1318-
{ok,[["true"]]} ->
1319-
hidden;
1320-
_ ->
1321-
normal
1315+
case net_kernel:dist_listen() of
1316+
false ->
1317+
hidden;
1318+
_ ->
1319+
case init:get_argument(hidden) of
1320+
{ok,[[]]} ->
1321+
hidden;
1322+
{ok,[["true"]]} ->
1323+
hidden;
1324+
_ ->
1325+
normal
1326+
end
13221327
end.
13231328

1324-
13251329
%%%====================================================================================
13261330
%%% Own group publication type and nodes
13271331
%%%====================================================================================

lib/kernel/src/inet6_tcp_dist.erl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
%% Handles the connection setup phase with other Erlang nodes.
2323

2424
-export([listen/1, accept/1, accept_connection/5,
25-
setup/5, close/1, select/1, is_node_name/1]).
25+
setup/5, close/1, select/1, address/0, is_node_name/1]).
2626

2727
-export([setopts/2, getopts/2]).
2828

@@ -34,6 +34,14 @@
3434
select(Node) ->
3535
inet_tcp_dist:gen_select(inet6_tcp, Node).
3636

37+
%% ------------------------------------------------------------
38+
%% Get address family
39+
%% address() => #net_address{}
40+
%% ------------------------------------------------------------
41+
42+
address() ->
43+
inet_tcp_dist:gen_address(inet6_tcp).
44+
3745
%% ------------------------------------------------------------
3846
%% Create the listen socket, i.e. the port that this erlang
3947
%% node is accessible through.

lib/kernel/src/inet_tcp_dist.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
%% Handles the connection setup phase with other Erlang nodes.
2323

2424
-export([listen/1, accept/1, accept_connection/5,
25-
setup/5, close/1, select/1, is_node_name/1]).
25+
setup/5, close/1, select/1, address/0, is_node_name/1]).
2626

2727
%% Optional
2828
-export([setopts/2, getopts/2]).
2929

3030
%% Generalized dist API
3131
-export([gen_listen/2, gen_accept/2, gen_accept_connection/6,
32-
gen_setup/6, gen_select/2]).
32+
gen_setup/6, gen_select/2, gen_address/1]).
3333

3434
%% internal exports
3535

@@ -63,6 +63,14 @@ gen_select(Driver, Node) ->
6363
_ -> false
6464
end.
6565

66+
%% ------------------------------------------------------------
67+
%% Get the address family that this distribution uses
68+
%% ------------------------------------------------------------
69+
address() ->
70+
gen_address(inet_tcp).
71+
gen_address(Driver) ->
72+
get_tcp_address(Driver).
73+
6674
%% ------------------------------------------------------------
6775
%% Create the listen socket, i.e. the port that this erlang
6876
%% node is accessible through.
@@ -436,9 +444,11 @@ split_node([], _, Ack) -> [lists:reverse(Ack)].
436444
%% ------------------------------------------------------------
437445
get_tcp_address(Driver, Socket) ->
438446
{ok, Address} = inet:sockname(Socket),
447+
NetAddr = get_tcp_address(Driver),
448+
NetAddr#net_address{ address = Address }.
449+
get_tcp_address(Driver) ->
439450
{ok, Host} = inet:gethostname(),
440451
#net_address {
441-
address = Address,
442452
host = Host,
443453
protocol = tcp,
444454
family = Driver:family()

lib/kernel/src/net_kernel.erl

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
kernel_apply/3,
6969
longnames/0,
7070
protocol_childspecs/0,
71-
epmd_module/0]).
71+
epmd_module/0,
72+
dist_listen/0]).
7273

7374
-export([disconnect/1, passive_cnct/1]).
7475
-export([hidden_connect_node/1]).
@@ -693,7 +694,10 @@ terminate(no_network, State) ->
693694
terminate(_Reason, State) ->
694695
lists:foreach(
695696
fun(#listen {listen = Listen,module = Mod}) ->
696-
Mod:close(Listen)
697+
case Listen of
698+
undefined -> ignore;
699+
_ -> Mod:close(Listen)
700+
end
697701
end, State#state.listen),
698702
lists:foreach(
699703
fun(Node) -> ?nodedown(Node, State)
@@ -1541,6 +1545,18 @@ epmd_module() ->
15411545
erl_epmd
15421546
end.
15431547

1548+
%%
1549+
%% dist_listen() -> whether the erlang distribution should listen for connections
1550+
%%
1551+
dist_listen() ->
1552+
case init:get_argument(dist_listen) of
1553+
{ok,[[DoListen]]} ->
1554+
list_to_atom(DoListen) =/= false;
1555+
_ ->
1556+
true
1557+
end.
1558+
1559+
15441560
%%
15451561
%% Start all protocols
15461562
%%
@@ -1553,8 +1569,13 @@ start_protos(Name, Node, CleanHalt) ->
15531569
start_protos(Name, ["inet_tcp"], Node, CleanHalt)
15541570
end.
15551571

1572+
1573+
15561574
start_protos(Name, Ps, Node, CleanHalt) ->
1557-
case start_protos(Name, Ps, Node, [], CleanHalt) of
1575+
case case dist_listen() of
1576+
false -> start_protos_no_listen(Name, Ps, Node, [], CleanHalt);
1577+
_ -> start_protos(Name, Ps, Node, [], CleanHalt)
1578+
end of
15581579
[] ->
15591580
case CleanHalt of
15601581
true -> halt(1);
@@ -1564,6 +1585,33 @@ start_protos(Name, Ps, Node, CleanHalt) ->
15641585
{ok, Ls}
15651586
end.
15661587

1588+
start_protos_no_listen(Name, [Proto | Ps], Node, Ls, CleanHalt) ->
1589+
Mod = list_to_atom(Proto ++ "_dist"),
1590+
case set_node(Node, create_creation()) of
1591+
ok ->
1592+
auth:sync_cookie(),
1593+
L = #listen {
1594+
listen = undefined,
1595+
address = Mod:address(),
1596+
accept = undefined,
1597+
module = Mod },
1598+
start_protos_no_listen(Name, Ps, Node, [L|Ls], CleanHalt);
1599+
_ ->
1600+
S = "invalid node name: " ++ atom_to_list(Node),
1601+
proto_error(CleanHalt, Proto, S),
1602+
start_protos_no_listen(Name, Ps, Node, Ls, CleanHalt)
1603+
end;
1604+
start_protos_no_listen(_Name, [], _Node, Ls, _CleanHalt) ->
1605+
Ls.
1606+
1607+
create_creation() ->
1608+
try binary:decode_unsigned(crypto:strong_rand_bytes(4)) of
1609+
Creation ->
1610+
Creation
1611+
catch _:_ ->
1612+
rand:uniform((1 bsl 32)-1)
1613+
end.
1614+
15671615
start_protos(Name, [Proto | Ps], Node, Ls, CleanHalt) ->
15681616
Mod = list_to_atom(Proto ++ "_dist"),
15691617
case catch Mod:listen(Name) of

0 commit comments

Comments
 (0)