2002-12-07 21:27:26 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-12-13 22:36:53 +01:00
|
|
|
%%% Purpose : S2S connections manager
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 7 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2002-12-07 21:27:26 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/0,
|
2007-09-25 18:32:18 +02:00
|
|
|
route/3,
|
|
|
|
have_connection/1,
|
|
|
|
has_key/2,
|
2008-04-18 14:19:45 +02:00
|
|
|
get_connections_pids/1,
|
2007-09-25 18:32:18 +02:00
|
|
|
try_register/1,
|
|
|
|
remove_connection/3,
|
2010-06-11 19:28:15 +02:00
|
|
|
find_connection/2,
|
2007-09-25 18:32:18 +02:00
|
|
|
dirty_get_connections/0,
|
|
|
|
allow_host/2,
|
2008-10-13 12:11:19 +02:00
|
|
|
incoming_s2s_number/0,
|
2010-06-02 16:01:36 +02:00
|
|
|
outgoing_s2s_number/0,
|
|
|
|
migrate/1
|
2007-09-25 18:32:18 +02:00
|
|
|
]).
|
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,
|
2007-10-30 18:38:40 +01:00
|
|
|
terminate/2, code_change/3]).
|
2009-05-19 11:39:13 +02:00
|
|
|
%% ejabberd API
|
|
|
|
-export([get_info_s2s_connections/1]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2008-06-30 16:04:31 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
-include("ejabberd.hrl").
|
2008-10-13 12:11:19 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
-define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER, 1).
|
|
|
|
-define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE, 1).
|
2007-09-14 16:18:51 +02:00
|
|
|
|
2008-06-30 16:04:31 +02:00
|
|
|
% These are the namespace already declared by the stream opening. This is
|
|
|
|
% used at serialization time.
|
|
|
|
-define(DEFAULT_NS, ?NS_JABBER_CLIENT).
|
2010-10-07 17:38:18 +02:00
|
|
|
-define(PREFIXED_NS,
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx}, {?NS_DIALBACK, ?NS_DIALBACK_pfx}]).
|
2008-06-30 16:04:31 +02: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
|
|
|
|
2010-01-27 19:53:56 +01:00
|
|
|
%% #xmlelement{} used for retro-compatibility
|
2008-07-01 17:51:34 +02:00
|
|
|
route(FromOld, ToOld, #xmlelement{} = PacketOld) ->
|
|
|
|
catch throw(for_stacktrace), % To have a stacktrace.
|
|
|
|
io:format("~nS2S: old #xmlelement:~n~p~n~p~n~n",
|
|
|
|
[PacketOld, erlang:get_stacktrace()]),
|
2008-06-30 16:04:31 +02:00
|
|
|
% XXX OLD FORMAT: From, To, Packet.
|
2008-06-30 17:51:23 +02:00
|
|
|
From = jlib:from_old_jid(FromOld),
|
|
|
|
To = jlib:from_old_jid(ToOld),
|
2008-07-01 17:51:34 +02:00
|
|
|
Packet = exmpp_xml:xmlelement_to_xmlel(PacketOld, [?NS_JABBER_CLIENT],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
route(From, To, Packet);
|
|
|
|
route(From, To, Packet) ->
|
2004-11-05 22:14:31 +01:00
|
|
|
case catch do_route(From, To, Packet) of
|
2007-10-30 18:38:40 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
2004-11-05 22:14:31 +01:00
|
|
|
end.
|
|
|
|
|
2007-07-15 09:28:47 +02:00
|
|
|
remove_connection(FromTo, Pid, Key) ->
|
2007-09-25 18:32:18 +02:00
|
|
|
case catch mnesia:dirty_match_object(s2s, #s2s{fromto = FromTo,
|
|
|
|
pid = Pid,
|
|
|
|
_ = '_'}) of
|
|
|
|
[#s2s{pid = Pid, key = Key}] ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete_object(#s2s{fromto = FromTo,
|
|
|
|
pid = Pid,
|
|
|
|
key = Key})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F);
|
|
|
|
_ ->
|
|
|
|
ok
|
2007-07-18 18:26:50 +02:00
|
|
|
end.
|
2007-07-15 09:28:47 +02:00
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
have_connection(FromTo) ->
|
2010-06-02 16:01:36 +02:00
|
|
|
case ejabberd_cluster:get_node(FromTo) of
|
|
|
|
Node when Node == node() ->
|
|
|
|
case mnesia:dirty_read(s2s, FromTo) of
|
|
|
|
[_] ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
Node ->
|
|
|
|
case catch rpc:call(Node, mnesia, dirty_read,
|
|
|
|
[s2s, FromTo], 5000) of
|
|
|
|
[_] ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2007-09-14 16:15:44 +02:00
|
|
|
has_key(FromTo, Key) ->
|
2010-06-02 16:01:36 +02:00
|
|
|
Query = [{#s2s{fromto = FromTo, key = Key, _ = '_'},
|
|
|
|
[],
|
|
|
|
['$_']}],
|
|
|
|
case ejabberd_cluster:get_node(FromTo) of
|
|
|
|
Node when Node == node() ->
|
|
|
|
case mnesia:dirty_select(s2s, Query) of
|
|
|
|
[] ->
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
Node ->
|
|
|
|
case catch rpc:call(Node, mnesia, dirty_select,
|
|
|
|
[s2s, Query], 5000) of
|
|
|
|
[_|_] ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2008-04-18 14:19:45 +02:00
|
|
|
get_connections_pids(FromTo) ->
|
2010-06-02 16:01:36 +02:00
|
|
|
case ejabberd_cluster:get_node(FromTo) of
|
|
|
|
Node when Node == node() ->
|
|
|
|
case catch mnesia:dirty_read(s2s, FromTo) of
|
|
|
|
L when is_list(L) ->
|
|
|
|
[Connection#s2s.pid || Connection <- L];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
Node ->
|
|
|
|
case catch rpc:call(Node, mnesia, dirty_read,
|
|
|
|
[s2s, FromTo], 5000) of
|
|
|
|
L when is_list(L) ->
|
|
|
|
[Connection#s2s.pid || Connection <- L];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
2008-04-18 14:19:45 +02: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(),
|
2007-09-25 18:32:18 +02:00
|
|
|
MaxS2SConnectionsNumber = max_s2s_connections_number(FromTo),
|
|
|
|
MaxS2SConnectionsNumberPerNode =
|
|
|
|
max_s2s_connections_number_per_node(FromTo),
|
2002-12-13 21:58:27 +01:00
|
|
|
F = fun() ->
|
2007-09-25 18:32:18 +02:00
|
|
|
L = mnesia:read({s2s, FromTo}),
|
|
|
|
NeededConnections = needed_connections_number(
|
|
|
|
L, MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
if
|
|
|
|
NeededConnections > 0 ->
|
|
|
|
mnesia:write(#s2s{fromto = FromTo,
|
|
|
|
pid = self(),
|
|
|
|
key = Key}),
|
|
|
|
{key, Key};
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
2002-12-13 21:58:27 +01:00
|
|
|
case mnesia:transaction(F) of
|
2007-10-30 18:38:40 +01:00
|
|
|
{atomic, Res} ->
|
|
|
|
Res;
|
|
|
|
_ ->
|
|
|
|
false
|
2002-12-13 21:58:27 +01:00
|
|
|
end.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
dirty_get_connections() ->
|
2010-06-02 16:01:36 +02:00
|
|
|
lists:flatmap(
|
|
|
|
fun(Node) when Node == node() ->
|
|
|
|
mnesia:dirty_all_keys(s2s);
|
|
|
|
(Node) ->
|
|
|
|
case catch rpc:call(Node, mnesia, dirty_all_keys, [s2s], 5000) of
|
|
|
|
L when is_list(L) ->
|
|
|
|
L;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, ejabberd_cluster:get_nodes()).
|
|
|
|
|
|
|
|
migrate(After) ->
|
|
|
|
Ss = mnesia:dirty_select(
|
|
|
|
s2s,
|
|
|
|
[{#s2s{fromto = '$1', pid = '$2', _ = '_'},
|
|
|
|
[],
|
|
|
|
['$$']}]),
|
|
|
|
lists:foreach(
|
|
|
|
fun([FromTo, Pid]) ->
|
|
|
|
case ejabberd_cluster:get_node_new(FromTo) of
|
|
|
|
Node when Node /= node() ->
|
|
|
|
ejabberd_s2s_out:stop_connection(Pid, After * 2);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, Ss).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([]) ->
|
|
|
|
update_tables(),
|
2010-06-02 16:01:36 +02:00
|
|
|
mnesia:create_table(s2s, [{ram_copies, [node()]},
|
|
|
|
{type, bag}, {local_content, true},
|
2007-09-25 18:32:18 +02:00
|
|
|
{attributes, record_info(fields, s2s)}]),
|
2006-01-29 05:38:31 +01:00
|
|
|
mnesia:add_table_copy(s2s, node(), ram_copies),
|
2010-06-02 16:01:36 +02:00
|
|
|
ejabberd_hooks:add(node_hash_update, ?MODULE, migrate, 100),
|
2008-10-13 12:11:19 +02:00
|
|
|
ejabberd_commands:register_commands(commands()),
|
2006-01-29 05:38:31 +01:00
|
|
|
{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
|
|
|
|
%%--------------------------------------------------------------------
|
2010-01-27 19:53:56 +01:00
|
|
|
%% #xmlelement{} used for retro-compatibility
|
2008-07-01 17:51:34 +02:00
|
|
|
handle_info({route, FromOld, ToOld, #xmlelement{} = PacketOld}, State) ->
|
|
|
|
catch throw(for_stacktrace), % To have a stacktrace.
|
|
|
|
io:format("~nS2S: old #xmlelement:~n~p~n~p~n~n",
|
|
|
|
[PacketOld, erlang:get_stacktrace()]),
|
|
|
|
% XXX OLD FORMAT: From, To, Packet.
|
2008-06-30 17:51:23 +02:00
|
|
|
From = jlib:from_old_jid(FromOld),
|
|
|
|
To = jlib:from_old_jid(ToOld),
|
2008-07-01 17:51:34 +02:00
|
|
|
Packet = exmpp_xml:xmlelement_to_xmlel(PacketOld, [?NS_JABBER_CLIENT],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
handle_info({route, From, To, Packet}, State);
|
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
2006-01-29 05:38:31 +01:00
|
|
|
case catch do_route(From, To, Packet) of
|
2007-10-30 18:38:40 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
2006-01-29 05:38:31 +01:00
|
|
|
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) ->
|
2010-06-02 16:01:36 +02:00
|
|
|
ejabberd_hooks:delete(node_hash_update, ?MODULE, migrate, 100),
|
2008-10-13 12:11:19 +02:00
|
|
|
ejabberd_commands:unregister_commands(commands()),
|
2006-01-29 05:38:31 +01:00
|
|
|
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
|
|
|
|
%%--------------------------------------------------------------------
|
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",
|
2007-10-30 18:38:40 +01:00
|
|
|
[From, To, Packet, 8]),
|
2010-06-02 16:01:36 +02:00
|
|
|
FromTo = {exmpp_jid:prep_domain_as_list(From),
|
|
|
|
exmpp_jid:prep_domain_as_list(To)},
|
|
|
|
case ejabberd_cluster:get_node(FromTo) of
|
|
|
|
Node when Node == node() ->
|
|
|
|
do_route1(From, To, Packet);
|
|
|
|
Node ->
|
|
|
|
{?MODULE, Node} ! {route, From, To, Packet}
|
|
|
|
end.
|
|
|
|
|
|
|
|
do_route1(From, To, Packet) ->
|
2003-12-13 22:36:53 +01:00
|
|
|
case find_connection(From, To) of
|
2009-05-06 18:54:43 +02:00
|
|
|
{atomic, Pid} when is_pid(Pid) ->
|
2007-09-25 18:32:18 +02:00
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
2008-06-30 16:04:31 +02:00
|
|
|
NewPacket1 = exmpp_stanza:set_sender(Packet, From),
|
|
|
|
NewPacket = exmpp_stanza:set_recipient(NewPacket1, To),
|
2009-06-01 18:37:15 +02:00
|
|
|
MyServer = exmpp_jid:prep_domain(From),
|
2008-05-22 12:53:49 +02:00
|
|
|
ejabberd_hooks:run(
|
|
|
|
s2s_send_packet,
|
|
|
|
MyServer,
|
2008-08-26 15:38:49 +02:00
|
|
|
[From, To, NewPacket]),
|
2008-06-30 16:04:31 +02:00
|
|
|
send_element(Pid, NewPacket),
|
2007-09-25 18:32:18 +02:00
|
|
|
ok;
|
|
|
|
{aborted, _Reason} ->
|
2008-06-30 16:04:31 +02:00
|
|
|
case exmpp_stanza:get_type(Packet) of
|
2009-01-09 20:18:46 +01:00
|
|
|
<<"error">> -> ok;
|
|
|
|
<<"result">> -> ok;
|
2007-09-25 18:32:18 +02:00
|
|
|
_ ->
|
2008-06-30 16:04:31 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet,
|
2008-07-01 10:01:06 +02:00
|
|
|
'service-unavailable'),
|
2008-06-30 18:55:03 +02:00
|
|
|
ejabberd_router:route(To, From, Err)
|
2007-09-25 18:32:18 +02:00
|
|
|
end,
|
|
|
|
false
|
2002-12-07 21:27:26 +01:00
|
|
|
end.
|
|
|
|
|
2003-12-13 22:36:53 +01:00
|
|
|
find_connection(From, To) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
MyServer = exmpp_jid:prep_domain_as_list(From),
|
|
|
|
Server = exmpp_jid:prep_domain_as_list(To),
|
2003-12-13 22:36:53 +01:00
|
|
|
FromTo = {MyServer, Server},
|
2007-09-25 18:32:18 +02:00
|
|
|
MaxS2SConnectionsNumber = max_s2s_connections_number(FromTo),
|
|
|
|
MaxS2SConnectionsNumberPerNode =
|
|
|
|
max_s2s_connections_number_per_node(FromTo),
|
|
|
|
?DEBUG("Finding connection for ~p~n", [FromTo]),
|
2003-12-13 22:36:53 +01:00
|
|
|
case catch mnesia:dirty_read(s2s, FromTo) of
|
2007-09-25 18:32:18 +02:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
{aborted, Reason};
|
|
|
|
[] ->
|
|
|
|
%% We try to establish all the connections if the host is not a
|
|
|
|
%% service and if the s2s host is not blacklisted or
|
|
|
|
%% is in whitelist:
|
|
|
|
case not is_service(From, To) andalso allow_host(MyServer, Server) of
|
|
|
|
true ->
|
|
|
|
NeededConnections = needed_connections_number(
|
|
|
|
[], MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
open_several_connections(
|
|
|
|
NeededConnections, MyServer,
|
|
|
|
Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode);
|
|
|
|
false ->
|
|
|
|
{aborted, error}
|
|
|
|
end;
|
|
|
|
L when is_list(L) ->
|
|
|
|
NeededConnections = needed_connections_number(
|
|
|
|
L, MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
if
|
|
|
|
NeededConnections > 0 ->
|
|
|
|
%% We establish the missing connections for this pair.
|
|
|
|
open_several_connections(
|
|
|
|
NeededConnections, MyServer,
|
|
|
|
Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode);
|
|
|
|
true ->
|
|
|
|
%% We choose a connexion from the pool of opened ones.
|
|
|
|
{atomic, choose_connection(From, L)}
|
|
|
|
end
|
2003-01-26 21:16:53 +01:00
|
|
|
end.
|
|
|
|
|
2007-09-14 16:15:44 +02:00
|
|
|
choose_connection(From, Connections) ->
|
2007-09-25 18:32:18 +02:00
|
|
|
choose_pid(From, [C#s2s.pid || C <- Connections]).
|
|
|
|
|
|
|
|
choose_pid(From, Pids) ->
|
|
|
|
Pids1 = case [P || P <- Pids, node(P) == node()] of
|
|
|
|
[] -> Pids;
|
|
|
|
Ps -> Ps
|
|
|
|
end,
|
2007-10-30 18:38:40 +01:00
|
|
|
% Use sticky connections based on the JID of the sender (whithout
|
|
|
|
% the resource to ensure that a muc room always uses the same
|
|
|
|
% connection)
|
2009-06-01 18:30:15 +02:00
|
|
|
Pid = lists:nth(erlang:phash(exmpp_jid:bare(From), length(Pids1)),
|
2007-10-30 18:38:40 +01:00
|
|
|
Pids1),
|
2007-09-25 18:32:18 +02:00
|
|
|
?DEBUG("Using ejabberd_s2s_out ~p~n", [Pid]),
|
2007-09-14 16:19:48 +02:00
|
|
|
Pid.
|
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
open_several_connections(N, MyServer, Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode) ->
|
|
|
|
ConnectionsResult =
|
|
|
|
[new_connection(MyServer, Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode)
|
|
|
|
|| _N <- lists:seq(1, N)],
|
|
|
|
case [PID || {atomic, PID} <- ConnectionsResult] of
|
2007-09-14 16:20:43 +02:00
|
|
|
[] ->
|
2007-09-25 18:32:18 +02:00
|
|
|
hd(ConnectionsResult);
|
2007-09-14 16:20:43 +02:00
|
|
|
PIDs ->
|
2007-09-25 18:32:18 +02:00
|
|
|
{atomic, choose_pid(From, PIDs)}
|
2007-09-14 16:20:43 +02:00
|
|
|
end.
|
2007-09-14 16:15:44 +02:00
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
new_connection(MyServer, Server, From, FromTo,
|
|
|
|
MaxS2SConnectionsNumber, MaxS2SConnectionsNumberPerNode) ->
|
2007-09-14 16:15:44 +02:00
|
|
|
Key = randoms:get_string(),
|
|
|
|
{ok, Pid} = ejabberd_s2s_out:start(
|
2007-09-25 18:32:18 +02:00
|
|
|
MyServer, Server, {new, Key}),
|
2007-09-14 16:15:44 +02:00
|
|
|
F = fun() ->
|
2007-09-25 18:32:18 +02:00
|
|
|
L = mnesia:read({s2s, FromTo}),
|
|
|
|
NeededConnections = needed_connections_number(
|
|
|
|
L, MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode),
|
|
|
|
if
|
|
|
|
NeededConnections > 0 ->
|
|
|
|
mnesia:write(#s2s{fromto = FromTo,
|
|
|
|
pid = Pid,
|
|
|
|
key = Key}),
|
|
|
|
?INFO_MSG("New s2s connection started ~p", [Pid]),
|
|
|
|
Pid;
|
|
|
|
true ->
|
|
|
|
choose_connection(From, L)
|
|
|
|
end
|
|
|
|
end,
|
2007-09-14 16:15:44 +02:00
|
|
|
TRes = mnesia:transaction(F),
|
|
|
|
case TRes of
|
2007-09-25 18:32:18 +02:00
|
|
|
{atomic, Pid} ->
|
|
|
|
ejabberd_s2s_out:start_connection(Pid);
|
|
|
|
_ ->
|
|
|
|
ejabberd_s2s_out:stop_connection(Pid)
|
2007-09-14 16:15:44 +02:00
|
|
|
end,
|
|
|
|
TRes.
|
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
max_s2s_connections_number({From, To}) ->
|
|
|
|
case acl:match_rule(
|
2009-06-01 18:26:00 +02:00
|
|
|
From, max_s2s_connections, exmpp_jid:make(To)) of
|
2007-09-25 18:32:18 +02:00
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER
|
|
|
|
end.
|
|
|
|
|
|
|
|
max_s2s_connections_number_per_node({From, To}) ->
|
|
|
|
case acl:match_rule(
|
2009-06-01 18:26:00 +02:00
|
|
|
From, max_s2s_connections_per_node, exmpp_jid:make(To)) of
|
2007-09-25 18:32:18 +02:00
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
_ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE
|
2007-09-14 16:18:51 +02:00
|
|
|
end.
|
|
|
|
|
2007-09-25 18:32:18 +02:00
|
|
|
needed_connections_number(Ls, MaxS2SConnectionsNumber,
|
|
|
|
MaxS2SConnectionsNumberPerNode) ->
|
|
|
|
LocalLs = [L || L <- Ls, node(L#s2s.pid) == node()],
|
|
|
|
lists:min([MaxS2SConnectionsNumber - length(Ls),
|
|
|
|
MaxS2SConnectionsNumberPerNode - length(LocalLs)]).
|
|
|
|
|
2007-02-18 18:58:47 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: is_service(From, To) -> true | false
|
|
|
|
%% Description: Return true if the destination must be considered as a
|
|
|
|
%% service.
|
|
|
|
%% --------------------------------------------------------------------
|
|
|
|
is_service(From, To) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LFromDomain = exmpp_jid:prep_domain_as_list(From),
|
2007-02-18 18:58:47 +01:00
|
|
|
case ejabberd_config:get_local_option({route_subdomains, LFromDomain}) of
|
2010-10-14 21:14:46 +02:00
|
|
|
s2s -> % bypass RFC 3920 10.3
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
LDstDomain = exmpp_jid:prep_domain_as_list(To),
|
|
|
|
Hosts = ?MYHOSTS,
|
|
|
|
P = fun(ParentDomain) -> lists:member(ParentDomain, Hosts) end,
|
|
|
|
lists:any(P, parent_domains(LDstDomain))
|
2007-02-18 18:58:47 +01:00
|
|
|
end.
|
|
|
|
|
2010-10-14 21:14:46 +02:00
|
|
|
parent_domains(Domain) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(Label, []) ->
|
|
|
|
[Label];
|
|
|
|
(Label, [Head | Tail]) ->
|
|
|
|
[Label ++ "." ++ Head, Head | Tail]
|
|
|
|
end, [], lists:reverse(string:tokens(Domain, "."))).
|
2007-02-18 18:58:47 +01:00
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
send_element(Pid, El) ->
|
|
|
|
Pid ! {send_element, El}.
|
2003-01-10 20:44:35 +01:00
|
|
|
|
2008-10-13 12:11:19 +02:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% ejabberd commands
|
|
|
|
|
|
|
|
commands() ->
|
|
|
|
[
|
|
|
|
#ejabberd_commands{name = incoming_s2s_number,
|
|
|
|
tags = [stats, s2s],
|
|
|
|
desc = "Number of incoming s2s connections on the node",
|
|
|
|
module = ?MODULE, function = incoming_s2s_number,
|
|
|
|
args = [],
|
|
|
|
result = {s2s_incoming, integer}},
|
|
|
|
#ejabberd_commands{name = outgoing_s2s_number,
|
|
|
|
tags = [stats, s2s],
|
|
|
|
desc = "Number of outgoing s2s connections on the node",
|
|
|
|
module = ?MODULE, function = outgoing_s2s_number,
|
|
|
|
args = [],
|
|
|
|
result = {s2s_outgoing, integer}}
|
|
|
|
].
|
|
|
|
|
|
|
|
incoming_s2s_number() ->
|
|
|
|
length(supervisor:which_children(ejabberd_s2s_in_sup)).
|
|
|
|
|
|
|
|
outgoing_s2s_number() ->
|
|
|
|
length(supervisor:which_children(ejabberd_s2s_out_sup)).
|
|
|
|
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Update Mnesia tables
|
2006-09-25 15:36:41 +02:00
|
|
|
|
2003-12-13 22:36:53 +01:00
|
|
|
update_tables() ->
|
2007-09-14 16:15:44 +02:00
|
|
|
case catch mnesia:table_info(s2s, type) of
|
2007-09-25 18:32:18 +02:00
|
|
|
bag ->
|
|
|
|
ok;
|
|
|
|
{'EXIT', _} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
% XXX TODO convert it ?
|
|
|
|
mnesia:delete_table(s2s)
|
2007-09-14 16:15:44 +02:00
|
|
|
end,
|
2003-12-13 22:36:53 +01:00
|
|
|
case catch mnesia:table_info(s2s, attributes) of
|
2007-10-30 18:38:40 +01:00
|
|
|
[fromto, node, key] ->
|
|
|
|
mnesia:transform_table(s2s, ignore, [fromto, pid, key]),
|
|
|
|
mnesia:clear_table(s2s);
|
|
|
|
[fromto, pid, key] ->
|
|
|
|
ok;
|
|
|
|
{'EXIT', _} ->
|
|
|
|
ok
|
2003-12-13 22:36:53 +01:00
|
|
|
end,
|
|
|
|
case lists:member(local_s2s, mnesia:system_info(tables)) of
|
2007-10-30 18:38:40 +01:00
|
|
|
true ->
|
|
|
|
mnesia:delete_table(local_s2s);
|
|
|
|
false ->
|
|
|
|
ok
|
2010-06-02 16:01:36 +02:00
|
|
|
end,
|
|
|
|
case catch mnesia:table_info(s2s, local_content) of
|
|
|
|
false ->
|
|
|
|
mnesia:delete_table(s2s);
|
|
|
|
_ ->
|
|
|
|
ok
|
2003-12-13 22:36:53 +01:00
|
|
|
end.
|
2007-07-18 19:42:53 +02:00
|
|
|
|
|
|
|
%% Check if host is in blacklist or white list
|
|
|
|
allow_host(MyServer, S2SHost) ->
|
2010-10-14 21:14:46 +02:00
|
|
|
Hosts = ?MYHOSTS,
|
|
|
|
case lists:dropwhile(
|
|
|
|
fun(ParentDomain) ->
|
|
|
|
not lists:member(ParentDomain, Hosts)
|
|
|
|
end, parent_domains(MyServer)) of
|
2009-11-13 17:22:54 +01:00
|
|
|
[MyHost|_] ->
|
|
|
|
allow_host1(MyHost, S2SHost);
|
|
|
|
[] ->
|
|
|
|
allow_host1(MyServer, S2SHost)
|
|
|
|
end.
|
|
|
|
|
|
|
|
allow_host1(MyHost, S2SHost) ->
|
|
|
|
case ejabberd_config:get_local_option({{s2s_host, S2SHost}, MyHost}) of
|
|
|
|
deny -> false;
|
|
|
|
allow -> true;
|
|
|
|
_ ->
|
|
|
|
case ejabberd_config:get_local_option({s2s_default_policy, MyHost}) of
|
|
|
|
deny -> false;
|
|
|
|
_ ->
|
2010-04-14 20:53:05 +02:00
|
|
|
case ejabberd_hooks:run_fold(s2s_allow_host, list_to_binary(MyHost),
|
2009-11-13 17:22:54 +01:00
|
|
|
allow, [MyHost, S2SHost]) of
|
|
|
|
deny -> false;
|
|
|
|
allow -> true;
|
|
|
|
_ -> true
|
|
|
|
end
|
|
|
|
end
|
2007-07-18 19:42:53 +02:00
|
|
|
end.
|
2009-05-19 11:39:13 +02:00
|
|
|
|
|
|
|
%% Get information about S2S connections of the specified type.
|
|
|
|
%% @spec (Type) -> [Info]
|
|
|
|
%% where Type = in | out
|
|
|
|
%% Info = [{InfoName::atom(), InfoValue::any()}]
|
|
|
|
get_info_s2s_connections(Type) ->
|
|
|
|
ChildType = case Type of
|
|
|
|
in -> ejabberd_s2s_in_sup;
|
|
|
|
out -> ejabberd_s2s_out_sup
|
|
|
|
end,
|
|
|
|
Connections = supervisor:which_children(ChildType),
|
|
|
|
get_s2s_info(Connections,Type).
|
|
|
|
|
|
|
|
get_s2s_info(Connections,Type)->
|
|
|
|
complete_s2s_info(Connections,Type,[]).
|
|
|
|
complete_s2s_info([],_,Result)->
|
|
|
|
Result;
|
|
|
|
complete_s2s_info([Connection|T],Type,Result)->
|
|
|
|
{_,PID,_,_}=Connection,
|
|
|
|
State = get_s2s_state(PID),
|
|
|
|
complete_s2s_info(T,Type,[State|Result]).
|
|
|
|
|
|
|
|
get_s2s_state(S2sPid)->
|
|
|
|
Infos = case gen_fsm:sync_send_all_state_event(S2sPid,get_state_infos) of
|
|
|
|
{state_infos, Is} -> [{status, open} | Is];
|
|
|
|
{noproc,_} -> [{status, closed}]; %% Connection closed
|
|
|
|
{badrpc,_} -> [{status, error}]
|
|
|
|
end,
|
|
|
|
[{s2s_pid, S2sPid} | Infos].
|