25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Report real address of a listener

This commit is contained in:
Evgeny Khramtsov 2018-09-18 14:51:42 +03:00
parent e96bfbdbfa
commit 1866b56e3b
2 changed files with 56 additions and 42 deletions

View File

@ -104,57 +104,70 @@ init({Port, _, udp} = EndPoint, Module, Opts, SockOpts) ->
{reuseaddr, true} | {reuseaddr, true} |
SockOpts]) of SockOpts]) of
{ok, Socket} -> {ok, Socket} ->
proc_lib:init_ack({ok, self()}), case inet:sockname(Socket) of
application:ensure_started(ejabberd), {ok, {Addr, Port1}} ->
?INFO_MSG("Start accepting UDP connections at ~s for ~p", proc_lib:init_ack({ok, self()}),
[format_endpoint(EndPoint), Module]), application:ensure_started(ejabberd),
case erlang:function_exported(Module, udp_init, 2) of ?INFO_MSG("Start accepting UDP connections at ~s for ~p",
false -> [format_endpoint({Port1, Addr, udp}), Module]),
udp_recv(Socket, Module, Opts); case erlang:function_exported(Module, udp_init, 2) of
true -> false ->
case catch Module:udp_init(Socket, Opts) of
{'EXIT', _} = Err ->
?ERROR_MSG("failed to process callback function "
"~p:~s(~p, ~p): ~p",
[Module, udp_init, Socket, Opts, Err]),
udp_recv(Socket, Module, Opts); udp_recv(Socket, Module, Opts);
NewOpts -> true ->
udp_recv(Socket, Module, NewOpts) case catch Module:udp_init(Socket, Opts) of
end {'EXIT', _} = Err ->
?ERROR_MSG("failed to process callback function "
"~p:~s(~p, ~p): ~p",
[Module, udp_init, Socket, Opts, Err]),
udp_recv(Socket, Module, Opts);
NewOpts ->
udp_recv(Socket, Module, NewOpts)
end
end;
{error, Reason} = Err ->
report_socket_error(Reason, EndPoint, Module),
proc_lib:init_ack(Err)
end; end;
{error, Reason} = Err -> {error, Reason} = Err ->
report_socket_error(Reason, EndPoint, Module), report_socket_error(Reason, EndPoint, Module),
proc_lib:init_ack(Err) proc_lib:init_ack(Err)
end; end;
init({_, _, tcp} = EndPoint, Module, Opts, SockOpts) -> init({Port, _, tcp} = EndPoint, Module, Opts, SockOpts) ->
case listen_tcp(EndPoint, Module, SockOpts) of case listen_tcp(Port, SockOpts) of
{ok, ListenSocket} -> {ok, ListenSocket} ->
proc_lib:init_ack({ok, self()}), case inet:sockname(ListenSocket) of
application:ensure_started(ejabberd), {ok, {Addr, Port1}} ->
Sup = start_module_sup(Module, Opts), proc_lib:init_ack({ok, self()}),
?INFO_MSG("Start accepting TCP connections at ~s for ~p", application:ensure_started(ejabberd),
[format_endpoint(EndPoint), Module]), Sup = start_module_sup(Module, Opts),
case erlang:function_exported(Module, tcp_init, 2) of ?INFO_MSG("Start accepting TCP connections at ~s for ~p",
false -> [format_endpoint({Port1, Addr, tcp}), Module]),
accept(ListenSocket, Module, Opts, Sup); case erlang:function_exported(Module, tcp_init, 2) of
true -> false ->
case catch Module:tcp_init(ListenSocket, Opts) of
{'EXIT', _} = Err ->
?ERROR_MSG("failed to process callback function "
"~p:~s(~p, ~p): ~p",
[Module, tcp_init, ListenSocket, Opts, Err]),
accept(ListenSocket, Module, Opts, Sup); accept(ListenSocket, Module, Opts, Sup);
NewOpts -> true ->
accept(ListenSocket, Module, NewOpts, Sup) case catch Module:tcp_init(ListenSocket, Opts) of
end {'EXIT', _} = Err ->
?ERROR_MSG("failed to process callback function "
"~p:~s(~p, ~p): ~p",
[Module, tcp_init, ListenSocket, Opts, Err]),
accept(ListenSocket, Module, Opts, Sup);
NewOpts ->
accept(ListenSocket, Module, NewOpts, Sup)
end
end;
{error, Reason} = Err ->
report_socket_error(Reason, EndPoint, Module),
Err
end; end;
{error, _} = Err -> {error, Reason} = Err ->
report_socket_error(Reason, EndPoint, Module),
proc_lib:init_ack(Err) proc_lib:init_ack(Err)
end. end.
-spec listen_tcp(endpoint(), module(), [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, _, _} = EndPoint, Module, SockOpts) -> listen_tcp(Port, SockOpts) ->
Res = gen_tcp:listen(Port, [binary, Res = gen_tcp:listen(Port, [binary,
{packet, 0}, {packet, 0},
{active, false}, {active, false},
@ -167,8 +180,7 @@ listen_tcp({Port, _, _} = EndPoint, Module, SockOpts) ->
case Res of case Res of
{ok, ListenSocket} -> {ok, ListenSocket} ->
{ok, ListenSocket}; {ok, ListenSocket};
{error, Reason} = Err -> {error, _} = Err ->
report_socket_error(Reason, EndPoint, Module),
Err Err
end. end.

View File

@ -115,8 +115,10 @@ code_change(_OldVsn, State, _Extra) -> {ok, State}.
%%%------------------------ %%%------------------------
add_listener(Host, Opts) -> add_listener(Host, Opts) ->
NewOpts = [{server_host, Host} | Opts], {_, IP, _} = EndPoint = get_endpoint(Host),
ejabberd_listener:add_listener(get_endpoint(Host), mod_proxy65_stream, NewOpts). Opts1 = [{server_host, Host} | Opts],
Opts2 = lists:keystore(ip, 1, Opts1, {ip, IP}),
ejabberd_listener:add_listener(EndPoint, mod_proxy65_stream, Opts2).
delete_listener(Host) -> delete_listener(Host) ->
ejabberd_listener:delete_listener(get_endpoint(Host), mod_proxy65_stream). ejabberd_listener:delete_listener(get_endpoint(Host), mod_proxy65_stream).