2002-11-27 21:46:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_router.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2003-12-14 21:51:01 +01:00
|
|
|
%%% Purpose : Main router
|
2002-11-27 21:46:29 +01:00
|
|
|
%%% Created : 27 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_router).
|
|
|
|
-author('alexey@sevcom.net').
|
2002-11-29 21:55:12 +01:00
|
|
|
-vsn('$Revision$ ').
|
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,
|
|
|
|
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,
|
2005-04-17 20:08:34 +02:00
|
|
|
unregister_routes/1,
|
2003-01-04 21:09:25 +01:00
|
|
|
dirty_get_all_routes/0,
|
|
|
|
dirty_get_all_domains/0
|
|
|
|
]).
|
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
|
|
|
|
2002-11-29 21:55:12 +01:00
|
|
|
-include("ejabberd.hrl").
|
2003-10-07 22:31:44 +02:00
|
|
|
-include("jlib.hrl").
|
2002-11-29 21:55:12 +01:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
2002-11-29 21:55:12 +01:00
|
|
|
route(From, To, Packet) ->
|
2004-11-05 22:14:31 +01:00
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-11-29 21:55:12 +01:00
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
register_route(Domain) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case jlib:nameprep(Domain) of
|
|
|
|
error ->
|
|
|
|
[] = {invalid_domain, Domain};
|
|
|
|
LDomain ->
|
|
|
|
Pid = self(),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#route{domain = LDomain,
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
2004-11-05 22:14:31 +01:00
|
|
|
|
|
|
|
register_route(Domain, LocalHint) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case jlib:nameprep(Domain) of
|
|
|
|
error ->
|
|
|
|
[] = {invalid_domain, Domain};
|
|
|
|
LDomain ->
|
|
|
|
Pid = self(),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(#route{domain = LDomain,
|
|
|
|
pid = Pid,
|
|
|
|
local_hint = LocalHint})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
|
|
|
|
|
|
|
register_routes(Domains) ->
|
|
|
|
lists:foreach(fun(Domain) ->
|
|
|
|
register_route(Domain)
|
|
|
|
end, Domains).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
unregister_route(Domain) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case jlib:nameprep(Domain) of
|
|
|
|
error ->
|
|
|
|
[] = {invalid_domain, Domain};
|
|
|
|
LDomain ->
|
|
|
|
Pid = self(),
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete_object(#route{domain = LDomain,
|
|
|
|
pid = Pid})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
|
|
|
|
|
|
|
unregister_routes(Domains) ->
|
|
|
|
lists:foreach(fun(Domain) ->
|
|
|
|
unregister_route(Domain)
|
|
|
|
end, Domains).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2003-01-02 22:01:12 +01:00
|
|
|
|
|
|
|
dirty_get_all_routes() ->
|
2005-04-17 20:08:34 +02:00
|
|
|
lists:usort(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() ->
|
2003-12-14 21:51:01 +01:00
|
|
|
lists:usort(mnesia:dirty_all_keys(route)).
|
|
|
|
|
|
|
|
|
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
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
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({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, _ = '_'},
|
|
|
|
[],
|
|
|
|
['$_']}]),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(E)
|
|
|
|
end, Es)
|
|
|
|
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
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
do_route(OrigFrom, OrigTo, OrigPacket) ->
|
|
|
|
?DEBUG("route~n\tfrom ~p~n\tto ~p~n\tpacket ~p~n",
|
|
|
|
[OrigFrom, OrigTo, OrigPacket]),
|
|
|
|
LOrigDstDomain = OrigTo#jid.lserver,
|
|
|
|
case ejabberd_hooks:run_fold(filter_packet, LOrigDstDomain,
|
|
|
|
{OrigFrom, OrigTo, OrigPacket}, []) of
|
|
|
|
{From, To, Packet} ->
|
|
|
|
LDstDomain = To#jid.lserver,
|
|
|
|
case mnesia:dirty_read(route, LDstDomain) of
|
|
|
|
[] ->
|
|
|
|
ejabberd_s2s:route(From, To, Packet);
|
|
|
|
[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;
|
|
|
|
true ->
|
|
|
|
Pid ! {route, From, To, Packet}
|
|
|
|
end;
|
|
|
|
Rs ->
|
|
|
|
case [R || R <- Rs, node(R#route.pid) == node()] of
|
|
|
|
[] ->
|
|
|
|
R = lists:nth(erlang:phash(now(), length(Rs)), Rs),
|
|
|
|
Pid = R#route.pid,
|
|
|
|
Pid ! {route, From, To, Packet};
|
|
|
|
LRs ->
|
|
|
|
LRs,
|
|
|
|
R = lists:nth(erlang:phash(now(), 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
|
|
|
|
end;
|
|
|
|
drop ->
|
|
|
|
ok
|
|
|
|
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.
|
2003-01-04 21:09:25 +01:00
|
|
|
|