25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00

Get rid of ejabberd_frontend_socket

This commit is contained in:
Evgeniy Khramtsov 2017-01-17 14:00:01 +03:00
parent c68ac1d5eb
commit 22ee16fd9d
4 changed files with 6 additions and 475 deletions

View File

@ -1,261 +0,0 @@
%%%-------------------------------------------------------------------
%%% File : ejabberd_frontend_socket.erl
%%% Author : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Frontend socket with zlib and TLS support library
%%% Created : 23 Aug 2006 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------
-module(ejabberd_frontend_socket).
-author('alexey@process-one.net').
-behaviour(gen_server).
%% API
-export([start/4,
start_link/5,
%connect/3,
starttls/2,
starttls/3,
compress/1,
compress/2,
reset_stream/1,
send/2,
change_shaper/2,
monitor/1,
get_sockmod/1,
get_transport/1,
get_peer_certificate/1,
get_verify_result/1,
close/1,
sockname/1, peername/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).
-record(state, {sockmod, socket, receiver}).
-define(HIBERNATE_TIMEOUT, 90000).
%%====================================================================
%% API
%%====================================================================
start_link(Module, SockMod, Socket, Opts, Receiver) ->
gen_server:start_link(?MODULE,
[Module, SockMod, Socket, Opts, Receiver], []).
start(Module, SockMod, Socket, Opts) ->
case Module:socket_type() of
xml_stream ->
MaxStanzaSize = case lists:keysearch(max_stanza_size, 1,
Opts)
of
{value, {_, Size}} -> Size;
_ -> infinity
end,
Receiver = ejabberd_receiver:start(Socket, SockMod,
none, MaxStanzaSize),
case SockMod:controlling_process(Socket, Receiver) of
ok -> ok;
{error, _Reason} -> SockMod:close(Socket)
end,
supervisor:start_child(ejabberd_frontend_socket_sup,
[Module, SockMod, Socket, Opts, Receiver]);
raw ->
%{ok, Pid} = Module:start({SockMod, Socket}, Opts),
%case SockMod:controlling_process(Socket, Pid) of
% ok ->
% ok;
% {error, _Reason} ->
% SockMod:close(Socket)
%end
todo
end.
starttls(FsmRef, _TLSOpts) ->
%% TODO: Frontend improvements planned by Aleksey
%%gen_server:call(FsmRef, {starttls, TLSOpts}),
FsmRef.
starttls(FsmRef, TLSOpts, Data) ->
gen_server:call(FsmRef, {starttls, TLSOpts, Data}),
FsmRef.
compress(FsmRef) -> compress(FsmRef, undefined).
compress(FsmRef, Data) ->
gen_server:call(FsmRef, {compress, Data}), FsmRef.
reset_stream(FsmRef) ->
gen_server:call(FsmRef, reset_stream).
send(FsmRef, Data) ->
gen_server:call(FsmRef, {send, Data}).
change_shaper(FsmRef, Shaper) ->
gen_server:call(FsmRef, {change_shaper, Shaper}).
monitor(FsmRef) -> erlang:monitor(process, FsmRef).
get_sockmod(FsmRef) ->
gen_server:call(FsmRef, get_sockmod).
get_transport(FsmRef) ->
gen_server:call(FsmRef, get_transport).
get_peer_certificate(FsmRef) ->
gen_server:call(FsmRef, get_peer_certificate).
get_verify_result(FsmRef) ->
gen_server:call(FsmRef, get_verify_result).
close(FsmRef) -> gen_server:call(FsmRef, close).
sockname(FsmRef) -> gen_server:call(FsmRef, sockname).
peername(_FsmRef) ->
%% TODO: Frontend improvements planned by Aleksey
%%gen_server:call(FsmRef, peername).
{ok, {{0, 0, 0, 0}, 0}}.
%%====================================================================
%% gen_server callbacks
%%====================================================================
init([Module, SockMod, Socket, Opts, Receiver]) ->
Node = ejabberd_node_groups:get_closest_node(backend),
{SockMod2, Socket2} = check_starttls(SockMod, Socket, Receiver, Opts),
{ok, Pid} =
rpc:call(Node, Module, start, [{?MODULE, self()}, Opts]),
ejabberd_receiver:become_controller(Receiver, Pid),
{ok, #state{sockmod = SockMod2,
socket = Socket2,
receiver = Receiver}}.
handle_call({starttls, TLSOpts}, _From, State) ->
{ok, TLSSocket} = fast_tls:tcp_to_tls(State#state.socket, TLSOpts),
ejabberd_receiver:starttls(State#state.receiver, TLSSocket),
Reply = ok,
{reply, Reply, State#state{socket = TLSSocket, sockmod = fast_tls},
?HIBERNATE_TIMEOUT};
handle_call({starttls, TLSOpts, Data}, _From, State) ->
{ok, TLSSocket} = fast_tls:tcp_to_tls(State#state.socket, TLSOpts),
ejabberd_receiver:starttls(State#state.receiver, TLSSocket),
catch (State#state.sockmod):send(
State#state.socket, Data),
Reply = ok,
{reply, Reply,
State#state{socket = TLSSocket, sockmod = fast_tls},
?HIBERNATE_TIMEOUT};
handle_call({compress, Data}, _From, State) ->
{ok, ZlibSocket} =
ejabberd_receiver:compress(State#state.receiver, Data),
Reply = ok,
{reply, Reply,
State#state{socket = ZlibSocket, sockmod = ezlib},
?HIBERNATE_TIMEOUT};
handle_call(reset_stream, _From, State) ->
ejabberd_receiver:reset_stream(State#state.receiver),
Reply = ok,
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call({send, Data}, _From, State) ->
catch (State#state.sockmod):send(State#state.socket, Data),
Reply = ok,
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call({change_shaper, Shaper}, _From, State) ->
ejabberd_receiver:change_shaper(State#state.receiver,
Shaper),
Reply = ok,
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_sockmod, _From, State) ->
Reply = State#state.sockmod,
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_transport, _From, State) ->
Reply = case State#state.sockmod of
gen_tcp -> tcp;
fast_tls -> tls;
ezlib ->
case ezlib:get_sockmod(State#state.socket) of
tcp -> tcp_zlib;
tls -> tls_zlib
end;
ejabberd_http_bind -> http_bind;
ejabberd_http_ws -> websocket
end,
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_peer_certificate, _From, State) ->
Reply = fast_tls:get_peer_certificate(State#state.socket),
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(get_verify_result, _From, State) ->
Reply = fast_tls:get_verify_result(State#state.socket),
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(close, _From, State) ->
ejabberd_receiver:close(State#state.receiver),
Reply = ok,
{stop, normal, Reply, State};
handle_call(sockname, _From, State) ->
#state{sockmod = SockMod, socket = Socket} = State,
Reply =
case SockMod of
gen_tcp ->
inet:sockname(Socket);
_ ->
SockMod:sockname(Socket)
end,
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(peername, _From, State) ->
#state{sockmod = SockMod, socket = Socket} = State,
Reply = case SockMod of
gen_tcp -> inet:peername(Socket);
_ -> SockMod:peername(Socket)
end,
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
handle_call(_Request, _From, State) ->
Reply = ok, {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
handle_cast(_Msg, State) ->
{noreply, State, ?HIBERNATE_TIMEOUT}.
handle_info(timeout, State) ->
proc_lib:hibernate(gen_server, enter_loop,
[?MODULE, [], State]),
{noreply, State, ?HIBERNATE_TIMEOUT};
handle_info(_Info, State) ->
{noreply, State, ?HIBERNATE_TIMEOUT}.
terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State, _Extra) -> {ok, State}.
check_starttls(SockMod, Socket, Receiver, Opts) ->
TLSEnabled = proplists:get_bool(tls, Opts),
TLSOpts = lists:filter(fun({certfile, _}) -> true;
(_) -> false
end, Opts),
if TLSEnabled ->
{ok, TLSSocket} = fast_tls:tcp_to_tls(Socket, TLSOpts),
ejabberd_receiver:starttls(Receiver, TLSSocket),
{fast_tls, TLSSocket};
true ->
{SockMod, Socket}
end.

View File

@ -56,8 +56,7 @@ bind_tcp_ports() ->
Ls -> Ls ->
lists:foreach( lists:foreach(
fun({Port, Module, Opts}) -> fun({Port, Module, Opts}) ->
ModuleRaw = strip_frontend(Module), case Module:socket_type() of
case ModuleRaw:socket_type() of
independent -> ok; independent -> ok;
_ -> _ ->
bind_tcp_port(Port, Module, Opts) bind_tcp_port(Port, Module, Opts)
@ -112,9 +111,8 @@ report_duplicated_portips(L) ->
start(Port, Module, Opts) -> start(Port, Module, Opts) ->
%% Check if the module is an ejabberd listener or an independent listener %% Check if the module is an ejabberd listener or an independent listener
ModuleRaw = strip_frontend(Module), case Module:socket_type() of
case ModuleRaw:socket_type() of independent -> Module:start_listener(Port, Opts);
independent -> ModuleRaw:start_listener(Port, Opts);
_ -> start_dependent(Port, Module, Opts) _ -> start_dependent(Port, Module, Opts)
end. end.
@ -332,12 +330,8 @@ accept(ListenSocket, Module, Opts, Interval) ->
{ok, Socket} -> {ok, Socket} ->
case {inet:sockname(Socket), inet:peername(Socket)} of case {inet:sockname(Socket), inet:peername(Socket)} of
{{ok, {Addr, Port}}, {ok, {PAddr, PPort}}} -> {{ok, {Addr, Port}}, {ok, {PAddr, PPort}}} ->
CallMod = case is_frontend(Module) of Receiver = case ejabberd_socket:start(Module,
true -> ejabberd_frontend_socket; gen_tcp, Socket, Opts) of
false -> ejabberd_socket
end,
Receiver = case CallMod:start(strip_frontend(Module),
gen_tcp, Socket, Opts) of
{ok, RecvPid} -> RecvPid; {ok, RecvPid} -> RecvPid;
_ -> none _ -> none
end, end,
@ -401,7 +395,7 @@ start_module_sup(_Port, Module) ->
Proc1 = gen_mod:get_module_proc(<<"sup">>, Module), Proc1 = gen_mod:get_module_proc(<<"sup">>, Module),
ChildSpec1 = ChildSpec1 =
{Proc1, {Proc1,
{ejabberd_tmp_sup, start_link, [Proc1, strip_frontend(Module)]}, {ejabberd_tmp_sup, start_link, [Proc1, Module]},
permanent, permanent,
infinity, infinity,
supervisor, supervisor,
@ -496,18 +490,6 @@ delete_listener(PortIP, Module, Opts) ->
stop_listener(PortIP1, Module). stop_listener(PortIP1, Module).
-spec is_frontend({frontend, module} | module()) -> boolean().
is_frontend({frontend, _Module}) -> true;
is_frontend(_) -> false.
%% @doc(FrontMod) -> atom()
%% where FrontMod = atom() | {frontend, atom()}
-spec strip_frontend({frontend, module()} | module()) -> module().
strip_frontend({frontend, Module}) -> Module;
strip_frontend(Module) when is_atom(Module) -> Module.
maybe_start_sip(esip_socket) -> maybe_start_sip(esip_socket) ->
ejabberd:start_app(esip); ejabberd:start_app(esip);
maybe_start_sip(_) -> maybe_start_sip(_) ->

View File

@ -1,173 +0,0 @@
%%%----------------------------------------------------------------------
%%% File : ejabberd_node_groups.erl
%%% Author : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Distributed named node groups based on pg2 module
%%% Created : 1 Nov 2006 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%----------------------------------------------------------------------
-module(ejabberd_node_groups).
-behaviour(ejabberd_config).
-author('alexey@process-one.net').
-behaviour(gen_server).
%% API
-export([start_link/0,
join/1,
leave/1,
get_members/1,
get_closest_node/1]).
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3, opt_type/1]).
-define(PG2, pg2).
-record(state, {}).
%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
join(Name) ->
PG = {?MODULE, Name},
pg2:create(PG),
pg2:join(PG, whereis(?MODULE)).
leave(Name) ->
PG = {?MODULE, Name},
pg2:leave(PG, whereis(?MODULE)).
get_members(Name) ->
PG = {?MODULE, Name},
[node(P) || P <- pg2:get_members(PG)].
get_closest_node(Name) ->
PG = {?MODULE, Name},
node(pg2:get_closest_pid(PG)).
%%====================================================================
%% gen_server callbacks
%%====================================================================
%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
{FE, BE} =
case ejabberd_config:get_option(
node_type,
fun(frontend) -> frontend;
(backend) -> backend;
(generic) -> generic
end, generic) of
frontend ->
{true, false};
backend ->
{false, true};
generic ->
{true, true};
undefined ->
{true, true}
end,
if
FE ->
join(frontend);
true ->
ok
end,
if
BE ->
join(backend);
true ->
ok
end,
{ok, #state{}}.
%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
opt_type(node_type) ->
fun (frontend) -> frontend;
(backend) -> backend;
(generic) -> generic
end;
opt_type(_) -> [node_type].

View File

@ -41,13 +41,6 @@ init([]) ->
brutal_kill, brutal_kill,
worker, worker,
[ejabberd_hooks]}, [ejabberd_hooks]},
NodeGroups =
{ejabberd_node_groups,
{ejabberd_node_groups, start_link, []},
permanent,
brutal_kill,
worker,
[ejabberd_node_groups]},
SystemMonitor = SystemMonitor =
{ejabberd_system_monitor, {ejabberd_system_monitor,
{ejabberd_system_monitor, start_link, []}, {ejabberd_system_monitor, start_link, []},
@ -100,14 +93,6 @@ init([]) ->
infinity, infinity,
supervisor, supervisor,
[ejabberd_tmp_sup]}, [ejabberd_tmp_sup]},
FrontendSocketSupervisor =
{ejabberd_frontend_socket_sup,
{ejabberd_tmp_sup, start_link,
[ejabberd_frontend_socket_sup, ejabberd_frontend_socket]},
permanent,
infinity,
supervisor,
[ejabberd_tmp_sup]},
IQSupervisor = IQSupervisor =
{ejabberd_iq_sup, {ejabberd_iq_sup,
{ejabberd_tmp_sup, start_link, {ejabberd_tmp_sup, start_link,
@ -118,7 +103,6 @@ init([]) ->
[ejabberd_tmp_sup]}, [ejabberd_tmp_sup]},
{ok, {{one_for_one, 10, 1}, {ok, {{one_for_one, 10, 1},
[Hooks, [Hooks,
NodeGroups,
SystemMonitor, SystemMonitor,
S2S, S2S,
Captcha, Captcha,
@ -126,5 +110,4 @@ init([]) ->
S2SOutSupervisor, S2SOutSupervisor,
ServiceSupervisor, ServiceSupervisor,
IQSupervisor, IQSupervisor,
FrontendSocketSupervisor,
Listener]}}. Listener]}}.