2006-10-01 03:53:37 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_frontend_socket.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Frontend socket with zlib and TLS support library
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 23 Aug 2006 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2014-03-13 12:29:21 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2014 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% 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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2006-10-01 03:53:37 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_frontend_socket).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
-author('alexey@process-one.net').
|
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start/4,
|
2006-11-07 03:08:51 +01:00
|
|
|
start_link/5,
|
2006-10-01 03:53:37 +02:00
|
|
|
%connect/3,
|
|
|
|
starttls/2,
|
|
|
|
starttls/3,
|
|
|
|
compress/1,
|
|
|
|
compress/2,
|
|
|
|
reset_stream/1,
|
|
|
|
send/2,
|
|
|
|
change_shaper/2,
|
2007-04-12 08:08:32 +02:00
|
|
|
monitor/1,
|
2006-10-01 03:53:37 +02:00
|
|
|
get_sockmod/1,
|
|
|
|
get_peer_certificate/1,
|
|
|
|
get_verify_result/1,
|
2006-10-19 06:46:24 +02:00
|
|
|
close/1,
|
|
|
|
sockname/1, peername/1]).
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
-record(state, {sockmod, socket, receiver}).
|
|
|
|
|
2008-11-23 10:43:52 +01:00
|
|
|
-define(HIBERNATE_TIMEOUT, 90000).
|
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
2006-11-07 03:08:51 +01:00
|
|
|
start_link(Module, SockMod, Socket, Opts, Receiver) ->
|
2006-10-01 03:53:37 +02:00
|
|
|
gen_server:start_link(?MODULE,
|
2006-11-07 03:08:51 +01:00
|
|
|
[Module, SockMod, Socket, Opts, Receiver], []).
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
start(Module, SockMod, Socket, Opts) ->
|
|
|
|
case Module:socket_type() of
|
2013-03-14 10:33:02 +01:00
|
|
|
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
|
2006-10-01 03:53:37 +02:00
|
|
|
end.
|
|
|
|
|
2009-04-08 20:56:11 +02:00
|
|
|
starttls(FsmRef, _TLSOpts) ->
|
|
|
|
%% TODO: Frontend improvements planned by Aleksey
|
|
|
|
%%gen_server:call(FsmRef, {starttls, TLSOpts}),
|
2006-10-01 03:53:37 +02:00
|
|
|
FsmRef.
|
|
|
|
|
|
|
|
starttls(FsmRef, TLSOpts, Data) ->
|
|
|
|
gen_server:call(FsmRef, {starttls, TLSOpts, Data}),
|
|
|
|
FsmRef.
|
|
|
|
|
2013-06-17 15:33:25 +02:00
|
|
|
compress(FsmRef) -> compress(FsmRef, undefined).
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
compress(FsmRef, Data) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:call(FsmRef, {compress, Data}), FsmRef.
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
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}).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
monitor(FsmRef) -> erlang:monitor(process, FsmRef).
|
2007-04-12 08:08:32 +02:00
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
get_sockmod(FsmRef) ->
|
|
|
|
gen_server:call(FsmRef, get_sockmod).
|
|
|
|
|
|
|
|
get_peer_certificate(FsmRef) ->
|
|
|
|
gen_server:call(FsmRef, get_peer_certificate).
|
|
|
|
|
|
|
|
get_verify_result(FsmRef) ->
|
|
|
|
gen_server:call(FsmRef, get_verify_result).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
close(FsmRef) -> gen_server:call(FsmRef, close).
|
2006-10-01 03:53:37 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
sockname(FsmRef) -> gen_server:call(FsmRef, sockname).
|
2006-10-19 06:46:24 +02:00
|
|
|
|
2009-04-08 20:56:11 +02:00
|
|
|
peername(_FsmRef) ->
|
|
|
|
%% TODO: Frontend improvements planned by Aleksey
|
|
|
|
%%gen_server:call(FsmRef, peername).
|
2008-10-23 16:59:49 +02:00
|
|
|
{ok, {{0, 0, 0, 0}, 0}}.
|
2006-10-19 06:46:24 +02:00
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([Module, SockMod, Socket, Opts, Receiver]) ->
|
2006-11-07 03:08:51 +01:00
|
|
|
Node = ejabberd_node_groups:get_closest_node(backend),
|
2008-10-23 16:59:49 +02:00
|
|
|
{SockMod2, Socket2} = check_starttls(SockMod, Socket, Receiver, Opts),
|
2006-10-01 03:53:37 +02:00
|
|
|
{ok, Pid} =
|
|
|
|
rpc:call(Node, Module, start, [{?MODULE, self()}, Opts]),
|
|
|
|
ejabberd_receiver:become_controller(Receiver, Pid),
|
2008-10-23 16:59:49 +02:00
|
|
|
{ok, #state{sockmod = SockMod2,
|
|
|
|
socket = Socket2,
|
2006-10-01 03:53:37 +02:00
|
|
|
receiver = Receiver}}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% 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({starttls, TLSOpts}, _From, State) ->
|
2013-06-20 10:40:44 +02:00
|
|
|
{ok, TLSSocket} = p1_tls:tcp_to_tls(State#state.socket, TLSOpts),
|
2006-10-01 03:53:37 +02:00
|
|
|
ejabberd_receiver:starttls(State#state.receiver, TLSSocket),
|
|
|
|
Reply = ok,
|
2013-06-20 10:40:44 +02:00
|
|
|
{reply, Reply, State#state{socket = TLSSocket, sockmod = p1_tls},
|
2008-11-23 10:43:52 +01:00
|
|
|
?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
handle_call({starttls, TLSOpts, Data}, _From, State) ->
|
2013-06-20 10:40:44 +02:00
|
|
|
{ok, TLSSocket} = p1_tls:tcp_to_tls(State#state.socket, TLSOpts),
|
2006-10-01 03:53:37 +02:00
|
|
|
ejabberd_receiver:starttls(State#state.receiver, TLSSocket),
|
|
|
|
catch (State#state.sockmod):send(
|
|
|
|
State#state.socket, Data),
|
|
|
|
Reply = ok,
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, Reply,
|
2013-06-20 10:40:44 +02:00
|
|
|
State#state{socket = TLSSocket, sockmod = p1_tls},
|
2008-11-23 10:43:52 +01:00
|
|
|
?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
handle_call({compress, Data}, _From, State) ->
|
2013-06-17 15:33:25 +02:00
|
|
|
{ok, ZlibSocket} =
|
|
|
|
ejabberd_receiver:compress(State#state.receiver, Data),
|
2006-10-01 03:53:37 +02:00
|
|
|
Reply = ok,
|
2013-03-14 10:33:02 +01:00
|
|
|
{reply, Reply,
|
2013-04-08 11:12:54 +02:00
|
|
|
State#state{socket = ZlibSocket, sockmod = ezlib},
|
2008-11-23 10:43:52 +01:00
|
|
|
?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_call(reset_stream, _From, State) ->
|
|
|
|
ejabberd_receiver:reset_stream(State#state.receiver),
|
|
|
|
Reply = ok,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_call({send, Data}, _From, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
catch (State#state.sockmod):send(State#state.socket, Data),
|
2006-10-01 03:53:37 +02:00
|
|
|
Reply = ok,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_call({change_shaper, Shaper}, _From, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_receiver:change_shaper(State#state.receiver,
|
|
|
|
Shaper),
|
2006-10-01 03:53:37 +02:00
|
|
|
Reply = ok,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_call(get_sockmod, _From, State) ->
|
|
|
|
Reply = State#state.sockmod,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_call(get_peer_certificate, _From, State) ->
|
2013-06-20 10:40:44 +02:00
|
|
|
Reply = p1_tls:get_peer_certificate(State#state.socket),
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_call(get_verify_result, _From, State) ->
|
2013-06-20 10:40:44 +02:00
|
|
|
Reply = p1_tls:get_verify_result(State#state.socket),
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_call(close, _From, State) ->
|
|
|
|
ejabberd_receiver:close(State#state.receiver),
|
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, Reply, State};
|
2006-10-19 06:46:24 +02:00
|
|
|
handle_call(sockname, _From, State) ->
|
|
|
|
#state{sockmod = SockMod, socket = Socket} = State,
|
|
|
|
Reply =
|
|
|
|
case SockMod of
|
|
|
|
gen_tcp ->
|
|
|
|
inet:sockname(Socket);
|
|
|
|
_ ->
|
|
|
|
SockMod:sockname(Socket)
|
|
|
|
end,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-19 06:46:24 +02:00
|
|
|
handle_call(peername, _From, State) ->
|
|
|
|
#state{sockmod = SockMod, socket = Socket} = State,
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = case SockMod of
|
|
|
|
gen_tcp -> inet:peername(Socket);
|
|
|
|
_ -> SockMod:peername(Socket)
|
|
|
|
end,
|
2008-11-23 10:43:52 +01:00
|
|
|
{reply, Reply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_call(_Request, _From, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = ok, {reply, Reply, State, ?HIBERNATE_TIMEOUT}.
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast(_Msg, State) ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT}.
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
2008-11-23 10:43:52 +01:00
|
|
|
handle_info(timeout, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
proc_lib:hibernate(gen_server, enter_loop,
|
|
|
|
[?MODULE, [], State]),
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT};
|
2006-10-01 03:53:37 +02:00
|
|
|
handle_info(_Info, State) ->
|
2008-11-23 10:43:52 +01:00
|
|
|
{noreply, State, ?HIBERNATE_TIMEOUT}.
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% 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.
|
|
|
|
%%--------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
terminate(_Reason, _State) -> ok.
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2006-10-01 03:53:37 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2008-10-23 16:59:49 +02:00
|
|
|
check_starttls(SockMod, Socket, Receiver, Opts) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
TLSEnabled = proplists:get_bool(tls, Opts),
|
2008-10-23 16:59:49 +02:00
|
|
|
TLSOpts = lists:filter(fun({certfile, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Opts),
|
|
|
|
if
|
|
|
|
TLSEnabled ->
|
2013-06-20 10:40:44 +02:00
|
|
|
{ok, TLSSocket} = p1_tls:tcp_to_tls(Socket, TLSOpts),
|
2008-10-23 16:59:49 +02:00
|
|
|
ejabberd_receiver:starttls(Receiver, TLSSocket),
|
2013-06-20 10:40:44 +02:00
|
|
|
{p1_tls, TLSSocket};
|
2008-10-23 16:59:49 +02:00
|
|
|
true ->
|
|
|
|
{SockMod, Socket}
|
|
|
|
end.
|