2002-12-07 21:27:26 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2003-12-13 22:36:53 +01:00
|
|
|
%%% Purpose : S2S connections manager
|
2002-12-07 21:27:26 +01:00
|
|
|
%%% Created : 7 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/0,
|
2004-11-05 22:14:31 +01:00
|
|
|
route/3,
|
2002-12-07 21:27:26 +01:00
|
|
|
have_connection/1,
|
2002-12-13 21:58:27 +01:00
|
|
|
get_key/1,
|
2003-01-10 20:44:35 +01:00
|
|
|
try_register/1,
|
2004-11-05 22:14:31 +01:00
|
|
|
remove_connection/1,
|
2003-01-10 20:44:35 +01:00
|
|
|
dirty_get_connections/0]).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
-include("ejabberd.hrl").
|
2003-10-07 22:31:44 +02:00
|
|
|
-include("jlib.hrl").
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2003-12-13 22:36:53 +01:00
|
|
|
-record(s2s, {fromto, pid, key}).
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2006-01-29 05:38:31 +01:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
route(From, To, Packet) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
remove_connection(FromTo) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
F = fun() ->
|
2003-01-05 21:24:59 +01:00
|
|
|
mnesia:delete({s2s, FromTo})
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
have_connection(FromTo) ->
|
2003-01-19 21:17:56 +01:00
|
|
|
case catch mnesia:dirty_read(s2s, FromTo) of
|
|
|
|
[_] ->
|
2002-12-07 21:27:26 +01:00
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
get_key(FromTo) ->
|
2003-01-19 21:17:56 +01:00
|
|
|
case catch mnesia:dirty_read(s2s, FromTo) of
|
|
|
|
[E] ->
|
2002-12-07 21:27:26 +01:00
|
|
|
E#s2s.key;
|
|
|
|
_ ->
|
2003-10-29 21:09:09 +01:00
|
|
|
error
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
try_register(FromTo) ->
|
2002-12-13 21:58:27 +01:00
|
|
|
Key = randoms:get_string(),
|
|
|
|
F = fun() ->
|
2003-01-05 21:24:59 +01:00
|
|
|
case mnesia:read({s2s, FromTo}) of
|
2002-12-13 21:58:27 +01:00
|
|
|
[] ->
|
2003-01-05 21:24:59 +01:00
|
|
|
mnesia:write(#s2s{fromto = FromTo,
|
2003-12-13 22:36:53 +01:00
|
|
|
pid = self(),
|
2002-12-13 21:58:27 +01:00
|
|
|
key = Key}),
|
|
|
|
{key, Key};
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
dirty_get_connections() ->
|
|
|
|
mnesia:dirty_all_keys(s2s).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([]) ->
|
|
|
|
update_tables(),
|
|
|
|
mnesia:create_table(s2s, [{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, s2s)}]),
|
|
|
|
mnesia:add_table_copy(s2s, node(), ram_copies),
|
|
|
|
mnesia:subscribe(system),
|
|
|
|
{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}.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_info({mnesia_system_event, {mnesia_down, Node}}, State) ->
|
|
|
|
clean_table_from_bad_node(Node),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
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
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
clean_table_from_bad_node(Node) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:select(
|
|
|
|
s2s,
|
|
|
|
[{#s2s{pid = '$1', _ = '_'},
|
|
|
|
[{'==', {node, '$1'}, Node}],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
|
|
|
do_route(From, To, Packet) ->
|
|
|
|
?DEBUG("s2s manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n",
|
|
|
|
[From, To, Packet, 8]),
|
2003-12-13 22:36:53 +01:00
|
|
|
case find_connection(From, To) of
|
|
|
|
{atomic, Pid} when pid(Pid) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
|
|
|
% TODO
|
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
|
|
|
NewAttrs = jlib:replace_from_to_attrs(jlib:jid_to_string(From),
|
|
|
|
jlib:jid_to_string(To),
|
|
|
|
Attrs),
|
|
|
|
send_element(Pid, {xmlelement, Name, NewAttrs, Els}),
|
|
|
|
ok;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?DEBUG("delivery failed: ~p~n", [Reason]),
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2003-12-13 22:36:53 +01:00
|
|
|
find_connection(From, To) ->
|
|
|
|
#jid{lserver = MyServer} = From,
|
|
|
|
#jid{lserver = Server} = To,
|
|
|
|
FromTo = {MyServer, Server},
|
|
|
|
case catch mnesia:dirty_read(s2s, FromTo) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
{aborted, Reason};
|
|
|
|
[] ->
|
|
|
|
?DEBUG("starting new s2s connection~n", []),
|
|
|
|
Key = randoms:get_string(),
|
|
|
|
{ok, Pid} = ejabberd_s2s_out:start(MyServer, Server, {new, Key}),
|
|
|
|
F = fun() ->
|
2003-01-26 21:16:53 +01:00
|
|
|
case mnesia:read({s2s, FromTo}) of
|
2003-12-13 22:36:53 +01:00
|
|
|
[El] ->
|
|
|
|
El#s2s.pid;
|
2003-01-26 21:16:53 +01:00
|
|
|
[] ->
|
|
|
|
mnesia:write(#s2s{fromto = FromTo,
|
2003-12-13 22:36:53 +01:00
|
|
|
pid = Pid,
|
2003-01-26 21:16:53 +01:00
|
|
|
key = Key}),
|
2003-12-13 22:36:53 +01:00
|
|
|
Pid
|
|
|
|
end
|
|
|
|
end,
|
2005-11-12 04:42:12 +01:00
|
|
|
TRes = mnesia:transaction(F),
|
|
|
|
ejabberd_s2s_out:start_connection(Pid),
|
|
|
|
TRes;
|
2003-01-26 21:16:53 +01:00
|
|
|
[El] ->
|
2003-12-13 22:36:53 +01:00
|
|
|
{atomic, El#s2s.pid}
|
2003-01-26 21:16:53 +01:00
|
|
|
end.
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
send_element(Pid, El) ->
|
|
|
|
Pid ! {send_element, El}.
|
2003-01-10 20:44:35 +01:00
|
|
|
|
2003-12-13 22:36:53 +01:00
|
|
|
update_tables() ->
|
|
|
|
case catch mnesia:table_info(s2s, attributes) of
|
|
|
|
[fromto, node, key] ->
|
|
|
|
mnesia:transform_table(s2s, ignore, [fromto, pid, key]),
|
|
|
|
mnesia:clear_table(s2s);
|
|
|
|
[fromto, pid, key] ->
|
|
|
|
ok;
|
|
|
|
{'EXIT', _} ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case lists:member(local_s2s, mnesia:system_info(tables)) of
|
|
|
|
true ->
|
|
|
|
mnesia:delete_table(local_s2s);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|