mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-10 16:58:46 +01:00
23b3779b20
* src/ejabberd_s2s_in.erl: Partially rewriten * src/ejabberd_router.erl: More verbose error report * src/ejabberd_local.erl: Likewise * src/ejabberd_sm.erl: Likewise * src/ejabberd_s2s_out.erl: Updated to be compatible with R9C * src/ejabberd_c2s.erl: Minor fix SVN Revision: 164
109 lines
2.9 KiB
Erlang
109 lines
2.9 KiB
Erlang
%%%----------------------------------------------------------------------
|
|
%%% File : ejabberd_listener.erl
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
%%% Purpose :
|
|
%%% Created : 16 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
%%% Id : $Id$
|
|
%%%----------------------------------------------------------------------
|
|
|
|
-module(ejabberd_listener).
|
|
-author('alexey@sevcom.net').
|
|
-vsn('$Revision$ ').
|
|
|
|
-export([start_link/0, init/1, start/3,
|
|
init/3,
|
|
init_ssl/4
|
|
]).
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
start_link() ->
|
|
supervisor:start_link({local, ejabberd_listeners}, ?MODULE, []).
|
|
|
|
|
|
init(_) ->
|
|
case ejabberd_config:get_local_option(listen) of
|
|
undefined ->
|
|
ignore;
|
|
Ls ->
|
|
{ok, {{one_for_one, 10, 1},
|
|
lists:map(
|
|
fun({Port, Module, Opts}) ->
|
|
{Port,
|
|
{?MODULE, start, [Port, Module, Opts]},
|
|
permanent,
|
|
brutal_kill,
|
|
worker,
|
|
[?MODULE]}
|
|
end, Ls)}}
|
|
end.
|
|
|
|
|
|
start(Port, Module, Opts) ->
|
|
case lists:keysearch(ssl, 1, Opts) of
|
|
{value, {ssl, SSLOpts}} ->
|
|
{ok, proc_lib:spawn_link(?MODULE, init_ssl,
|
|
[Port, Module, Opts, SSLOpts])};
|
|
_ ->
|
|
{ok, proc_lib:spawn_link(?MODULE, init, [Port, Module, Opts])}
|
|
end.
|
|
|
|
init(Port, Module, Opts) ->
|
|
{ok, ListenSocket} = gen_tcp:listen(Port, [binary,
|
|
{packet, 0},
|
|
{active, false},
|
|
{reuseaddr, true},
|
|
{nodelay, true}]),
|
|
accept(ListenSocket, Module, Opts).
|
|
|
|
accept(ListenSocket, Module, Opts) ->
|
|
case gen_tcp:accept(ListenSocket) of
|
|
{ok, Socket} ->
|
|
case {inet:sockname(Socket), inet:peername(Socket)} of
|
|
{{ok, Addr}, {ok, PAddr}} ->
|
|
?INFO_MSG("(~w) Accepted connection ~w -> ~w",
|
|
[Socket, PAddr, Addr]);
|
|
_ ->
|
|
ok
|
|
end,
|
|
{ok, Pid} = Module:start({gen_tcp, Socket}, Opts),
|
|
gen_tcp:controlling_process(Socket, Pid),
|
|
accept(ListenSocket, Module, Opts);
|
|
{error, Reason} ->
|
|
?INFO_MSG("(~w) Failed TCP accept: ~w",
|
|
[ListenSocket, Reason]),
|
|
accept(ListenSocket, Module, Opts)
|
|
end.
|
|
|
|
|
|
init_ssl(Port, Module, Opts, SSLOpts) ->
|
|
{ok, ListenSocket} = ssl:listen(Port, [binary,
|
|
{packet, 0},
|
|
{active, false},
|
|
{nodelay, true} |
|
|
SSLOpts]),
|
|
accept_ssl(ListenSocket, Module, Opts).
|
|
|
|
accept_ssl(ListenSocket, Module, Opts) ->
|
|
case ssl:accept(ListenSocket, 200) of
|
|
{ok, Socket} ->
|
|
case {ssl:sockname(Socket), ssl:peername(Socket)} of
|
|
{{ok, Addr}, {ok, PAddr}} ->
|
|
?INFO_MSG("(~w) Accepted SSL connection ~w -> ~w",
|
|
[Socket, PAddr, Addr]);
|
|
_ ->
|
|
ok
|
|
end,
|
|
{ok, Pid} = Module:start({ssl, Socket}, Opts),
|
|
ssl:controlling_process(Socket, Pid),
|
|
accept_ssl(ListenSocket, Module, Opts);
|
|
{error, timeout} ->
|
|
accept_ssl(ListenSocket, Module, Opts);
|
|
{error, Reason} ->
|
|
?INFO_MSG("(~w) Failed SSL handshake: ~w",
|
|
[ListenSocket, Reason]),
|
|
accept_ssl(ListenSocket, Module, Opts)
|
|
end.
|
|
|
|
|