25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Add support for unix socket in listeners

To use it you just need to set port value to "unix:/path/to/socket"
This commit is contained in:
Paweł Chmielowski 2020-07-28 12:19:24 +02:00
parent d5935fd1ad
commit 1b168e7d5c

View File

@ -105,10 +105,18 @@ init({_, _, Transport} = EndPoint, Module, AllOpts) ->
-spec init(endpoint(), module(), opts(), [gen_tcp:option()]) -> ok. -spec init(endpoint(), module(), opts(), [gen_tcp:option()]) -> ok.
init({Port, _, udp} = EndPoint, Module, Opts, SockOpts) -> init({Port, _, udp} = EndPoint, Module, Opts, SockOpts) ->
case gen_udp:open(Port, [binary, {Port2, ExtraOpts} = case Port of
<<"unix:", Path/binary>> ->
SO = lists:keydelete(ip, 1, SockOpts),
file:delete(Path),
{0, [{ip, {local, Path}} | SO]};
_ ->
{Port, SockOpts}
end,
case gen_udp:open(Port2, [binary,
{active, false}, {active, false},
{reuseaddr, true} | {reuseaddr, true} |
SockOpts]) of ExtraOpts]) of
{ok, Socket} -> {ok, Socket} ->
case inet:sockname(Socket) of case inet:sockname(Socket) of
{ok, {Addr, Port1}} -> {ok, {Addr, Port1}} ->
@ -174,15 +182,22 @@ init({Port, _, tcp} = EndPoint, Module, Opts, SockOpts) ->
-spec listen_tcp(inet:port_number(), [gen_tcp:option()]) -> -spec listen_tcp(inet:port_number(), [gen_tcp:option()]) ->
{ok, inet:socket()} | {error, system_limit | inet:posix()}. {ok, inet:socket()} | {error, system_limit | inet:posix()}.
listen_tcp(Port, SockOpts) -> listen_tcp(Port, SockOpts) ->
Res = gen_tcp:listen(Port, [binary, {Port2, ExtraOpts} = case Port of
<<"unix:", Path/binary>> ->
SO = lists:keydelete(ip, 1, SockOpts),
file:delete(Path),
{0, [{ip, {local, Path}} | SO]};
_ ->
{Port, SockOpts}
end,
Res = gen_tcp:listen(Port2, [binary,
{packet, 0}, {packet, 0},
{active, false}, {active, false},
{reuseaddr, true}, {reuseaddr, true},
{nodelay, true}, {nodelay, true},
{send_timeout, ?TCP_SEND_TIMEOUT}, {send_timeout, ?TCP_SEND_TIMEOUT},
{send_timeout_close, true}, {send_timeout_close, true},
{keepalive, true} | {keepalive, true} | ExtraOpts]),
SockOpts]),
case Res of case Res of
{ok, ListenSocket} -> {ok, ListenSocket} ->
{ok, ListenSocket}; {ok, ListenSocket};
@ -226,6 +241,8 @@ accept(ListenSocket, Module, State, Sup, Interval, Proxy, Arity) ->
?ERROR_MSG("(~w) Proxy protocol parsing failed: ~ts", ?ERROR_MSG("(~w) Proxy protocol parsing failed: ~ts",
[ListenSocket, format_error(Err)]), [ListenSocket, format_error(Err)]),
gen_tcp:close(Socket); gen_tcp:close(Socket);
{undefined, undefined} ->
gen_tcp:close(Socket);
{{Addr, Port}, {PAddr, PPort}} = SP -> {{Addr, Port}, {PAddr, PPort}} = SP ->
%% THIS IS WRONG %% THIS IS WRONG
State2 = [{sock_peer_name, SP} | State], State2 = [{sock_peer_name, SP} | State],
@ -464,11 +481,16 @@ format_error(Reason) ->
-spec format_endpoint(endpoint()) -> string(). -spec format_endpoint(endpoint()) -> string().
format_endpoint({Port, IP, _Transport}) -> format_endpoint({Port, IP, _Transport}) ->
case Port of
Unix when is_binary(Unix) ->
<<"unix:", Unix/binary>>;
_ ->
IPStr = case tuple_size(IP) of IPStr = case tuple_size(IP) of
4 -> inet:ntoa(IP); 4 -> inet:ntoa(IP);
8 -> "[" ++ inet:ntoa(IP) ++ "]" 8 -> "[" ++ inet:ntoa(IP) ++ "]"
end, end,
IPStr ++ ":" ++ integer_to_list(Port). IPStr ++ ":" ++ integer_to_list(Port)
end.
-spec format_transport(transport(), opts()) -> string(). -spec format_transport(transport(), opts()) -> string().
format_transport(Transport, Opts) -> format_transport(Transport, Opts) ->
@ -623,7 +645,9 @@ partition(Fun, Opts) ->
-spec listen_opt_type(atom()) -> econf:validator(). -spec listen_opt_type(atom()) -> econf:validator().
listen_opt_type(port) -> listen_opt_type(port) ->
econf:int(0, 65535); econf:either(
econf:int(0, 65535),
econf:binary("^unix:.*"));
listen_opt_type(module) -> listen_opt_type(module) ->
econf:beam([[{start, 3}, {start, 2}], econf:beam([[{start, 3}, {start, 2}],
[{start_link, 3}, {start_link, 2}], [{start_link, 3}, {start_link, 2}],