2002-12-07 21:27:26 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 7 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
-export([start_link/0, init/0,
|
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,
|
|
|
|
dirty_get_connections/0]).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
|
|
|
-include_lib("mnemosyne/include/mnemosyne.hrl").
|
|
|
|
-include("ejabberd.hrl").
|
2003-10-07 22:31:44 +02:00
|
|
|
-include("jlib.hrl").
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
-record(s2s, {fromto, node, key}).
|
2003-01-21 21:36:55 +01:00
|
|
|
-record(local_s2s, {fromto, pid}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
|
|
|
{ok, proc_lib:spawn_link(ejabberd_s2s, init, [])}.
|
2002-12-07 21:27:26 +01:00
|
|
|
|
|
|
|
init() ->
|
|
|
|
register(ejabberd_s2s, self()),
|
|
|
|
mnesia:create_table(s2s,[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, s2s)}]),
|
|
|
|
mnesia:add_table_index(session, node),
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:create_table(local_s2s,
|
2002-12-07 21:27:26 +01:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{local_content, true},
|
2003-01-21 21:36:55 +01:00
|
|
|
{attributes, record_info(fields, local_s2s)}]),
|
|
|
|
mnesia:add_table_copy(local_s2s, node(), ram_copies),
|
2002-12-07 21:27:26 +01:00
|
|
|
mnesia:subscribe(system),
|
|
|
|
loop().
|
|
|
|
|
|
|
|
loop() ->
|
|
|
|
receive
|
|
|
|
%{open_connection, User, Resource, From} ->
|
|
|
|
% replace_and_register_my_connection(User, Resource, From),
|
|
|
|
% replace_alien_connection(User, Resource),
|
|
|
|
% loop();
|
2003-01-05 21:24:59 +01:00
|
|
|
{closed_conection, FromTo} ->
|
|
|
|
remove_connection(FromTo),
|
2002-12-07 21:27:26 +01:00
|
|
|
loop();
|
|
|
|
%{replace, User, Resource} ->
|
|
|
|
% replace_my_connection(User, Resource),
|
|
|
|
% loop();
|
|
|
|
{mnesia_system_event, {mnesia_down, Node}} ->
|
|
|
|
clean_table_from_bad_node(Node),
|
|
|
|
loop();
|
|
|
|
{route, From, To, Packet} ->
|
2003-05-15 20:16:13 +02:00
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2002-12-07 21:27:26 +01:00
|
|
|
loop();
|
|
|
|
_ ->
|
|
|
|
loop()
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
%open_session(User, Resource) ->
|
|
|
|
% ejabberd_s2s ! {open_session, User, Resource, self()}.
|
2002-12-07 21:27:26 +01:00
|
|
|
%
|
2002-12-13 21:58:27 +01:00
|
|
|
%close_session(User, Resource) ->
|
|
|
|
% ejabberd_s2s ! {close_session, User, Resource}.
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
remove_connection(FromTo) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
F = fun() ->
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:delete({local_s2s, FromTo}),
|
2003-01-05 21:24:59 +01:00
|
|
|
mnesia:delete({s2s, FromTo})
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clean_table_from_bad_node(Node) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:index_read(s2s, Node, #s2s.node),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(s2s, E, write)
|
|
|
|
end, Es)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2003-01-19 21:17:56 +01:00
|
|
|
%have_connection(FromTo) ->
|
|
|
|
% F = fun() ->
|
|
|
|
% [E] = mnesia:read({s2s, FromTo})
|
|
|
|
% end,
|
|
|
|
% case mnesia:transaction(F) of
|
|
|
|
% {atomic, _} ->
|
|
|
|
% true;
|
|
|
|
% _ ->
|
|
|
|
% false
|
|
|
|
% end.
|
|
|
|
|
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;
|
|
|
|
_ ->
|
|
|
|
""
|
|
|
|
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,
|
2002-12-13 21:58:27 +01:00
|
|
|
node = node(),
|
|
|
|
key = Key}),
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:write(#local_s2s{fromto = FromTo,
|
|
|
|
pid = self()}),
|
2002-12-13 21:58:27 +01:00
|
|
|
{key, Key};
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
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-10-07 22:31:44 +02:00
|
|
|
#jid{lserver = MyServer} = From,
|
|
|
|
#jid{lserver = Server} = To,
|
2003-01-05 21:24:59 +01:00
|
|
|
FromTo = {MyServer, Server},
|
2002-12-13 21:58:27 +01:00
|
|
|
Key = randoms:get_string(),
|
2003-01-26 21:16:53 +01:00
|
|
|
case find_connection(FromTo, Key) of
|
2002-12-07 21:27:26 +01:00
|
|
|
{atomic, {local, Pid}} ->
|
|
|
|
?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;
|
|
|
|
{atomic, {remote, Node}} ->
|
|
|
|
?DEBUG("sending to node ~p~n", [Node]),
|
|
|
|
{ejabberd_s2s, Node} ! {route, From, To, Packet},
|
|
|
|
ok;
|
|
|
|
{atomic, new} ->
|
|
|
|
?DEBUG("starting new s2s connection~n", []),
|
2003-07-20 22:35:35 +02:00
|
|
|
{ok, Pid} = ejabberd_s2s_out:start(MyServer, Server, {new, Key}),
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:transaction(fun() ->
|
|
|
|
mnesia:write(#local_s2s{fromto = FromTo,
|
|
|
|
pid = Pid})
|
|
|
|
end),
|
2002-12-07 21:27:26 +01:00
|
|
|
{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;
|
|
|
|
{atomic, not_exists} ->
|
|
|
|
?DEBUG("packet droped~n", []),
|
|
|
|
ok;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?DEBUG("delivery failed: ~p~n", [Reason]),
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2003-01-26 21:16:53 +01:00
|
|
|
find_connection(FromTo, Key) ->
|
|
|
|
F = fun() ->
|
|
|
|
case mnesia:read({local_s2s, FromTo}) of
|
|
|
|
[] ->
|
|
|
|
case mnesia:read({s2s, FromTo}) of
|
|
|
|
[Er] ->
|
|
|
|
{remote, Er#s2s.node};
|
|
|
|
[] ->
|
|
|
|
mnesia:write(#s2s{fromto = FromTo,
|
|
|
|
node = node(),
|
|
|
|
key = Key}),
|
|
|
|
new
|
|
|
|
end;
|
|
|
|
[El] ->
|
|
|
|
{local, El#local_s2s.pid}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case catch mnesia:dirty_read({local_s2s, FromTo}) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
{aborted, Reason};
|
|
|
|
[] ->
|
|
|
|
case catch mnesia:dirty_read({s2s, FromTo}) of
|
|
|
|
[Er] ->
|
|
|
|
{atomic, {remote, Er#s2s.node}};
|
|
|
|
[] ->
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end;
|
|
|
|
[El] ->
|
|
|
|
{atomic, {local, El#local_s2s.pid}}
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
dirty_get_connections() ->
|
|
|
|
mnesia:dirty_all_keys(s2s).
|
|
|
|
|