2002-11-27 21:46:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_router.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-12-14 21:51:01 +01:00
|
|
|
%%% Purpose : Main router
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 27 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:47:22 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 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-11-27 21:46:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_router).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
2003-01-07 20:10:35 +01:00
|
|
|
-export([route/3,
|
2010-04-20 20:31:17 +02:00
|
|
|
route_error/4,
|
2003-01-07 20:10:35 +01:00
|
|
|
register_route/1,
|
2004-11-05 22:14:31 +01:00
|
|
|
register_route/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
register_routes/1,
|
2003-01-07 20:10:35 +01:00
|
|
|
unregister_route/1,
|
2010-07-22 18:41:53 +02:00
|
|
|
force_unregister_route/1,
|
2005-04-17 20:08:34 +02:00
|
|
|
unregister_routes/1,
|
2003-01-04 21:09:25 +01:00
|
|
|
dirty_get_all_routes/0,
|
2010-06-02 16:01:36 +02:00
|
|
|
dirty_get_all_domains/0,
|
2010-07-22 18:41:53 +02:00
|
|
|
read_route/1,
|
2010-06-02 16:01:36 +02:00
|
|
|
make_id/0
|
2003-01-04 21:09:25 +01:00
|
|
|
]).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-export([start_link/0]).
|
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2008-06-30 18:55:03 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2002-11-29 21:55:12 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
-record(route, {domain, pid, local_hint}).
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {}).
|
2002-11-27 21:46:29 +01:00
|
|
|
|
2010-06-02 16:01:36 +02:00
|
|
|
%% "rr" stands for Record-Route.
|
|
|
|
-define(ROUTE_PREFIX, "rr-").
|
|
|
|
|
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, [], []).
|
2004-11-05 22:14:31 +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("~nROUTER: old #xmlelement:~n~p~n~p~n~n",
|
|
|
|
[PacketOld, erlang:get_stacktrace()]),
|
|
|
|
% XXX OLD FORMAT: From, To, Packet.
|
|
|
|
From = jlib:from_old_jid(FromOld),
|
|
|
|
To = jlib:from_old_jid(ToOld),
|
|
|
|
Packet = exmpp_xml:xmlelement_to_xmlel(PacketOld, [?NS_JABBER_CLIENT],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
route(From, To, Packet);
|
2002-11-29 21:55:12 +01:00
|
|
|
route(From, To, Packet) ->
|
2010-06-02 16:01:36 +02:00
|
|
|
case catch route_check_id(From, To, Packet) of
|
2004-11-05 22:14:31 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-11-29 21:55:12 +01:00
|
|
|
|
2010-04-20 20:31:17 +02:00
|
|
|
%% Route the error packet only if the originating packet is not an error itself.
|
|
|
|
%% RFC3920 9.3.1
|
|
|
|
route_error(From, To, ErrPacket, OrigPacket) ->
|
|
|
|
case exmpp_stanza:is_stanza_error(OrigPacket) of
|
|
|
|
false ->
|
|
|
|
route(From, To, ErrPacket);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2010-07-22 18:41:53 +02:00
|
|
|
register_route({global, Prefix}) ->
|
|
|
|
ejabberd_global_router:register_route(Prefix);
|
|
|
|
register_route(Domain) when is_list(Domain) ->
|
2007-01-19 05:46:44 +01:00
|
|
|
register_route(Domain, undefined).
|
2004-11-05 22:14:31 +01:00
|
|
|
|
2010-12-22 17:55:31 +01:00
|
|
|
register_route(Domain, LocalHint) when is_list(Domain) ->
|
2008-10-10 16:40:04 +02:00
|
|
|
try
|
2010-07-22 18:41:53 +02:00
|
|
|
LDomain = exmpp_stringprep:nameprep(Domain),
|
|
|
|
LDomainB = list_to_binary(LDomain),
|
|
|
|
Pid = self(),
|
|
|
|
case get_component_number(LDomain) of
|
|
|
|
undefined ->
|
|
|
|
mnesia:transaction(fun register_simple_route/3, [LDomainB, Pid, LocalHint]);
|
|
|
|
N ->
|
|
|
|
mnesia:transaction(fun register_balanced_route/3, [LDomainB, Pid, N])
|
|
|
|
end
|
2008-10-10 16:40:04 +02:00
|
|
|
catch
|
2010-07-22 18:41:53 +02:00
|
|
|
_ ->
|
2008-10-10 16:40:04 +02:00
|
|
|
erlang:error({invalid_domain, Domain})
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
register_routes(Domains) ->
|
|
|
|
lists:foreach(fun(Domain) ->
|
|
|
|
register_route(Domain)
|
|
|
|
end, Domains).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2010-07-22 18:41:53 +02:00
|
|
|
register_simple_route(LDomain, Pid, LocalHint) ->
|
|
|
|
mnesia:write(#route{domain = LDomain,
|
|
|
|
pid = Pid,
|
|
|
|
local_hint = LocalHint}).
|
|
|
|
|
|
|
|
register_balanced_route(LDomain, Pid, N) ->
|
|
|
|
case mnesia:read({route, LDomain}) of
|
|
|
|
[] ->
|
|
|
|
mnesia:write(
|
|
|
|
#route{domain = LDomain,
|
|
|
|
pid = Pid,
|
|
|
|
local_hint = 1}),
|
|
|
|
lists:foreach(
|
|
|
|
fun(I) ->
|
|
|
|
mnesia:write(
|
|
|
|
#route{domain = LDomain,
|
|
|
|
pid = undefined,
|
|
|
|
local_hint = I})
|
|
|
|
end, lists:seq(2, N));
|
|
|
|
Rs ->
|
|
|
|
lists:any(
|
|
|
|
fun(#route{pid = undefined,
|
|
|
|
local_hint = I} = R) ->
|
|
|
|
mnesia:write(
|
|
|
|
#route{domain = LDomain,
|
|
|
|
pid = Pid,
|
|
|
|
local_hint = I}),
|
|
|
|
mnesia:delete_object(R),
|
|
|
|
true;
|
|
|
|
(_) ->
|
|
|
|
false
|
|
|
|
end, Rs)
|
|
|
|
end.
|
|
|
|
|
|
|
|
unregister_route({global, Prefix}) ->
|
|
|
|
ejabberd_global_router:unregister_route(Prefix);
|
|
|
|
unregister_route(Domain) when is_list(Domain) ->
|
2008-10-10 16:40:04 +02:00
|
|
|
try
|
|
|
|
LDomain = exmpp_stringprep:nameprep(Domain),
|
2009-01-03 16:15:38 +01:00
|
|
|
LDomainB = list_to_binary(LDomain),
|
2008-10-10 16:40:04 +02:00
|
|
|
Pid = self(),
|
|
|
|
case get_component_number(LDomain) of
|
|
|
|
undefined ->
|
2010-07-22 18:41:53 +02:00
|
|
|
mnesia:transaction(fun delete_simple_route/2, [LDomainB, Pid]);
|
2008-10-10 16:40:04 +02:00
|
|
|
_ ->
|
2010-07-22 18:41:53 +02:00
|
|
|
mnesia:transaction(fun delete_balanced_route/2, [LDomainB, Pid])
|
2008-10-10 16:40:04 +02:00
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
erlang:error({invalid_domain, Domain})
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2010-07-22 18:41:53 +02:00
|
|
|
delete_simple_route(LDomain, Pid) ->
|
|
|
|
case mnesia:match_object(#route{domain = LDomain,
|
|
|
|
pid = Pid,
|
|
|
|
_ = '_'}) of
|
|
|
|
[R] ->
|
|
|
|
mnesia:delete_object(R);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
delete_balanced_route(LDomain, Pid) ->
|
|
|
|
case mnesia:match_object(#route{domain=LDomain,
|
|
|
|
pid = Pid,
|
|
|
|
_ = '_'}) of
|
|
|
|
[R] ->
|
|
|
|
I = R#route.local_hint,
|
|
|
|
ok = mnesia:write(
|
|
|
|
#route{domain = LDomain,
|
|
|
|
pid = undefined,
|
|
|
|
local_hint = I}),
|
|
|
|
mnesia:delete_object(R);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2010-12-22 17:55:31 +01:00
|
|
|
force_unregister_route(Domain) when is_binary(Domain) ->
|
2010-08-13 17:01:08 +02:00
|
|
|
LDomain = exmpp_stringprep:nameprep(Domain),
|
|
|
|
F = fun() ->
|
|
|
|
case mnesia:match_object(
|
|
|
|
#route{domain = LDomain,
|
|
|
|
_ = '_'}) of
|
|
|
|
Rs when is_list(Rs) ->
|
|
|
|
lists:foreach(fun(R) ->
|
|
|
|
mnesia:delete_object(R)
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
2010-07-22 18:41:53 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
unregister_routes(Domains) ->
|
|
|
|
lists:foreach(fun(Domain) ->
|
|
|
|
unregister_route(Domain)
|
|
|
|
end, Domains).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2010-07-22 18:41:53 +02:00
|
|
|
read_route(Domain) ->
|
|
|
|
[{D,P,H}
|
|
|
|
|| #route{domain=D, pid=P, local_hint=H} <- mnesia:dirty_read({route, Domain})].
|
2003-01-02 22:01:12 +01:00
|
|
|
|
|
|
|
dirty_get_all_routes() ->
|
2009-01-03 16:15:38 +01:00
|
|
|
lists:usort(
|
|
|
|
lists:map(fun erlang:binary_to_list/1,
|
|
|
|
mnesia:dirty_all_keys(route))) -- ?MYHOSTS.
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2003-01-04 21:09:25 +01:00
|
|
|
dirty_get_all_domains() ->
|
2009-01-03 16:15:38 +01:00
|
|
|
lists:usort(
|
|
|
|
lists:map(fun erlang:binary_to_list/1,
|
|
|
|
mnesia:dirty_all_keys(route))).
|
2003-12-14 21:51:01 +01:00
|
|
|
|
2010-06-02 16:01:36 +02:00
|
|
|
make_id() ->
|
|
|
|
?ROUTE_PREFIX ++ randoms:get_string() ++ "-" ++ ejabberd_cluster:node_id().
|
2003-12-14 21:51:01 +01:00
|
|
|
|
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(),
|
|
|
|
mnesia:create_table(route,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes,
|
|
|
|
record_info(fields, route)}]),
|
|
|
|
mnesia:add_table_copy(route, node(), ram_copies),
|
|
|
|
mnesia:subscribe({table, route, simple}),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Pid) ->
|
|
|
|
erlang:monitor(process, Pid)
|
|
|
|
end,
|
|
|
|
mnesia:dirty_select(route, [{{route, '_', '$1', '_'}, [], ['$1']}])),
|
|
|
|
{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
|
|
|
|
%%--------------------------------------------------------------------
|
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("~nROUTER: old #xmlelement:~n~p~n~p~n~n",
|
|
|
|
[PacketOld, erlang:get_stacktrace()]),
|
|
|
|
% XXX OLD FORMAT: From, To, Packet.
|
|
|
|
From = jlib:from_old_jid(FromOld),
|
|
|
|
To = jlib:from_old_jid(ToOld),
|
|
|
|
Packet = exmpp_xml:xmlelement_to_xmlel(PacketOld, [?NS_JABBER_CLIENT],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
handle_info({route, From, To, Packet}, State);
|
2006-01-29 05:38:31 +01:00
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
2008-07-01 17:51:34 +02:00
|
|
|
case catch do_route(From, To, Packet) of
|
2006-01-29 05:38:31 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({mnesia_table_event, {write, #route{pid = Pid}, _ActivityId}},
|
|
|
|
State) ->
|
|
|
|
erlang:monitor(process, Pid),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({'DOWN', _Ref, _Type, Pid, _Info}, State) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:select(
|
|
|
|
route,
|
|
|
|
[{#route{pid = Pid, _ = '_'},
|
|
|
|
[],
|
|
|
|
['$_']}]),
|
2007-01-19 05:46:44 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(E) ->
|
|
|
|
if
|
|
|
|
is_integer(E#route.local_hint) ->
|
|
|
|
LDomain = E#route.domain,
|
|
|
|
I = E#route.local_hint,
|
|
|
|
mnesia:write(
|
|
|
|
#route{domain = LDomain,
|
|
|
|
pid = undefined,
|
|
|
|
local_hint = I}),
|
|
|
|
mnesia:delete_object(E);
|
|
|
|
true ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end
|
|
|
|
end, Es)
|
2006-01-29 05:38:31 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
{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
|
|
|
|
%%--------------------------------------------------------------------
|
2010-06-02 16:01:36 +02:00
|
|
|
route_check_id(From, To, #xmlel{name = iq} = Packet) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
case exmpp_xml:get_attribute_as_list(Packet, <<"id">>, "") of
|
2010-06-02 16:01:36 +02:00
|
|
|
?ROUTE_PREFIX ++ Rest ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Type = exmpp_xml:get_attribute_as_list(Packet, <<"type">>, ""),
|
2010-06-02 16:01:36 +02:00
|
|
|
if Type == "error"; Type == "result" ->
|
|
|
|
case string:tokens(Rest, "-") of
|
|
|
|
[_, NodeID] ->
|
|
|
|
case ejabberd_cluster:get_node_by_id(NodeID) of
|
|
|
|
Node when Node == node() ->
|
|
|
|
do_route(From, To, Packet);
|
|
|
|
Node ->
|
|
|
|
{ejabberd_router, Node} !
|
|
|
|
{route, From, To, Packet}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
do_route(From, To, Packet)
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
do_route(From, To, Packet)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
do_route(From, To, Packet)
|
|
|
|
end;
|
|
|
|
route_check_id(From, To, Packet) ->
|
|
|
|
do_route(From, To, Packet).
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
do_route(OrigFrom, OrigTo, OrigPacket) ->
|
|
|
|
?DEBUG("route~n\tfrom ~p~n\tto ~p~n\tpacket ~p~n",
|
|
|
|
[OrigFrom, OrigTo, OrigPacket]),
|
2006-08-14 21:46:14 +02:00
|
|
|
case ejabberd_hooks:run_fold(filter_packet,
|
2008-08-26 15:38:49 +02:00
|
|
|
{OrigFrom, OrigTo, OrigPacket}, []) of
|
|
|
|
{From, To, Packet} ->
|
2010-07-22 18:41:53 +02:00
|
|
|
LDstDomain = exmpp_jid:prep_domain_as_list(To),
|
|
|
|
Destination = ejabberd:normalize_host(LDstDomain),
|
|
|
|
case mnesia:dirty_read(route, list_to_binary(Destination)) of
|
2006-01-29 05:38:31 +01:00
|
|
|
[] ->
|
2010-07-22 18:41:53 +02:00
|
|
|
case ejabberd_global_router:find_route(Destination) of
|
|
|
|
no_route ->
|
|
|
|
ejabberd_s2s:route(From, To, Packet);
|
|
|
|
Route ->
|
|
|
|
ejabberd_global_router:route(Route, From, To, Packet)
|
|
|
|
end;
|
2006-01-29 05:38:31 +01:00
|
|
|
[R] ->
|
|
|
|
Pid = R#route.pid,
|
|
|
|
if
|
|
|
|
node(Pid) == node() ->
|
|
|
|
case R#route.local_hint of
|
|
|
|
{apply, Module, Function} ->
|
|
|
|
Module:Function(From, To, Packet);
|
|
|
|
_ ->
|
|
|
|
Pid ! {route, From, To, Packet}
|
|
|
|
end;
|
2007-01-19 05:46:44 +01:00
|
|
|
is_pid(Pid) ->
|
|
|
|
Pid ! {route, From, To, Packet};
|
2006-01-29 05:38:31 +01:00
|
|
|
true ->
|
2007-01-19 05:46:44 +01:00
|
|
|
drop
|
2006-01-29 05:38:31 +01:00
|
|
|
end;
|
|
|
|
Rs ->
|
2010-10-07 17:38:18 +02:00
|
|
|
Value = case ejabberd_config:get_local_option
|
|
|
|
({domain_balancing, LDstDomain}) of
|
2007-01-19 05:46:44 +01:00
|
|
|
undefined -> now();
|
|
|
|
random -> now();
|
2008-08-26 15:38:49 +02:00
|
|
|
source -> jlib:short_prepd_jid(From);
|
|
|
|
destination -> jlib:short_prepd_jid(To);
|
2007-01-19 05:46:44 +01:00
|
|
|
bare_source ->
|
2008-08-26 15:38:49 +02:00
|
|
|
jlib:short_prepd_bare_jid(From);
|
2007-01-19 05:46:44 +01:00
|
|
|
bare_destination ->
|
2008-08-26 15:38:49 +02:00
|
|
|
jlib:short_prepd_bare_jid(To)
|
2007-01-19 05:46:44 +01:00
|
|
|
end,
|
|
|
|
case get_component_number(LDstDomain) of
|
|
|
|
undefined ->
|
|
|
|
case [R || R <- Rs, node(R#route.pid) == node()] of
|
|
|
|
[] ->
|
|
|
|
R = lists:nth(erlang:phash(Value, length(Rs)), Rs),
|
|
|
|
Pid = R#route.pid,
|
|
|
|
if
|
|
|
|
is_pid(Pid) ->
|
|
|
|
Pid ! {route, From, To, Packet};
|
|
|
|
true ->
|
|
|
|
drop
|
|
|
|
end;
|
|
|
|
LRs ->
|
|
|
|
R = lists:nth(erlang:phash(Value, length(LRs)), LRs),
|
|
|
|
Pid = R#route.pid,
|
|
|
|
case R#route.local_hint of
|
|
|
|
{apply, Module, Function} ->
|
|
|
|
Module:Function(From, To, Packet);
|
|
|
|
_ ->
|
|
|
|
Pid ! {route, From, To, Packet}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
SRs = lists:ukeysort(#route.local_hint, Rs),
|
|
|
|
R = lists:nth(erlang:phash(Value, length(SRs)), SRs),
|
2006-01-29 05:38:31 +01:00
|
|
|
Pid = R#route.pid,
|
2007-01-19 05:46:44 +01:00
|
|
|
if
|
|
|
|
is_pid(Pid) ->
|
|
|
|
Pid ! {route, From, To, Packet};
|
|
|
|
true ->
|
|
|
|
drop
|
2006-01-29 05:38:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
drop ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2007-01-19 05:46:44 +01:00
|
|
|
get_component_number(LDomain) ->
|
2010-10-07 17:38:18 +02:00
|
|
|
case ejabberd_config:get_local_option
|
|
|
|
({domain_balancing_component_number, LDomain}) of
|
2007-01-19 05:46:44 +01:00
|
|
|
N when is_integer(N),
|
|
|
|
N > 1 ->
|
|
|
|
N;
|
|
|
|
_ ->
|
|
|
|
undefined
|
|
|
|
end.
|
2003-12-14 21:51:01 +01:00
|
|
|
|
|
|
|
update_tables() ->
|
|
|
|
case catch mnesia:table_info(route, attributes) of
|
|
|
|
[domain, node, pid] ->
|
|
|
|
mnesia:delete_table(route);
|
|
|
|
[domain, pid] ->
|
2004-11-05 22:14:31 +01:00
|
|
|
mnesia:delete_table(route);
|
|
|
|
[domain, pid, local_hint] ->
|
2003-12-14 21:51:01 +01:00
|
|
|
ok;
|
|
|
|
{'EXIT', _} ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case lists:member(local_route, mnesia:system_info(tables)) of
|
|
|
|
true ->
|
|
|
|
mnesia:delete_table(local_route);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|