25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +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,10 +104,12 @@ init({Port, _, udp} = EndPoint, Module, Opts, SockOpts) ->
{reuseaddr, true} |
SockOpts]) of
{ok, Socket} ->
case inet:sockname(Socket) of
{ok, {Addr, Port1}} ->
proc_lib:init_ack({ok, self()}),
application:ensure_started(ejabberd),
?INFO_MSG("Start accepting UDP connections at ~s for ~p",
[format_endpoint(EndPoint), Module]),
[format_endpoint({Port1, Addr, udp}), Module]),
case erlang:function_exported(Module, udp_init, 2) of
false ->
udp_recv(Socket, Module, Opts);
@ -126,14 +128,20 @@ init({Port, _, udp} = EndPoint, Module, Opts, SockOpts) ->
report_socket_error(Reason, EndPoint, Module),
proc_lib:init_ack(Err)
end;
init({_, _, tcp} = EndPoint, Module, Opts, SockOpts) ->
case listen_tcp(EndPoint, Module, SockOpts) of
{error, Reason} = Err ->
report_socket_error(Reason, EndPoint, Module),
proc_lib:init_ack(Err)
end;
init({Port, _, tcp} = EndPoint, Module, Opts, SockOpts) ->
case listen_tcp(Port, SockOpts) of
{ok, ListenSocket} ->
case inet:sockname(ListenSocket) of
{ok, {Addr, Port1}} ->
proc_lib:init_ack({ok, self()}),
application:ensure_started(ejabberd),
Sup = start_module_sup(Module, Opts),
?INFO_MSG("Start accepting TCP connections at ~s for ~p",
[format_endpoint(EndPoint), Module]),
[format_endpoint({Port1, Addr, tcp}), Module]),
case erlang:function_exported(Module, tcp_init, 2) of
false ->
accept(ListenSocket, Module, Opts, Sup);
@ -148,13 +156,18 @@ init({_, _, tcp} = EndPoint, Module, Opts, SockOpts) ->
accept(ListenSocket, Module, NewOpts, Sup)
end
end;
{error, _} = Err ->
{error, Reason} = Err ->
report_socket_error(Reason, EndPoint, Module),
Err
end;
{error, Reason} = Err ->
report_socket_error(Reason, EndPoint, Module),
proc_lib:init_ack(Err)
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()}.
listen_tcp({Port, _, _} = EndPoint, Module, SockOpts) ->
listen_tcp(Port, SockOpts) ->
Res = gen_tcp:listen(Port, [binary,
{packet, 0},
{active, false},
@ -167,8 +180,7 @@ listen_tcp({Port, _, _} = EndPoint, Module, SockOpts) ->
case Res of
{ok, ListenSocket} ->
{ok, ListenSocket};
{error, Reason} = Err ->
report_socket_error(Reason, EndPoint, Module),
{error, _} = Err ->
Err
end.

View File

@ -115,8 +115,10 @@ code_change(_OldVsn, State, _Extra) -> {ok, State}.
%%%------------------------
add_listener(Host, Opts) ->
NewOpts = [{server_host, Host} | Opts],
ejabberd_listener:add_listener(get_endpoint(Host), mod_proxy65_stream, NewOpts).
{_, IP, _} = EndPoint = get_endpoint(Host),
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) ->
ejabberd_listener:delete_listener(get_endpoint(Host), mod_proxy65_stream).