2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s_in.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 6 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s_in).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2003-02-01 21:21:28 +01:00
|
|
|
-export([start/2, receiver/2, send_text/2, send_element/2]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
2002-12-07 21:27:26 +01:00
|
|
|
-export([init/1,
|
|
|
|
wait_for_stream/2,
|
|
|
|
wait_for_key/2,
|
|
|
|
wait_for_verification/2,
|
|
|
|
stream_established/2,
|
2003-01-24 21:18:33 +01:00
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
|
|
|
code_change/4,
|
2002-12-06 21:59:19 +01:00
|
|
|
handle_info/3,
|
|
|
|
terminate/3]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
-record(state, {socket, receiver, streamid,
|
2003-01-04 21:09:25 +01:00
|
|
|
myname, server, queue}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2003-01-13 21:51:17 +01:00
|
|
|
%-define(DBGFSM, true).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
|
|
|
-else.
|
|
|
|
-define(FSMOPTS, []).
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
-define(STREAM_HEADER,
|
|
|
|
"<?xml version='1.0'?>"
|
|
|
|
"<stream:stream "
|
|
|
|
"xmlns:stream='http://etherx.jabber.org/streams' "
|
|
|
|
"xmlns='jabber:server' "
|
|
|
|
"xmlns:db='jabber:server:dialback'>"
|
|
|
|
).
|
|
|
|
|
|
|
|
-define(STREAM_TRAILER, "</stream:stream>").
|
|
|
|
|
|
|
|
-define(INVALID_HEADER_ERR,
|
|
|
|
"<stream:stream>"
|
|
|
|
"<stream:error>Invalid Stream Header</stream:error>"
|
|
|
|
"</stream:stream>"
|
|
|
|
).
|
|
|
|
|
2003-01-04 21:09:25 +01:00
|
|
|
-define(INVALID_DOMAIN_ERR,
|
|
|
|
"<stream:stream>"
|
|
|
|
"<stream:error>Invalid Destination</stream:error>"
|
|
|
|
"</stream:stream>"
|
|
|
|
).
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-02-06 20:09:22 +01:00
|
|
|
start(SockData, Opts) ->
|
|
|
|
gen_fsm:start(ejabberd_s2s_in, [SockData], ?FSMOPTS).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, StopReason}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-02-06 20:09:22 +01:00
|
|
|
init([{SockMod, Socket}]) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
ReceiverPid = spawn(?MODULE, receiver, [Socket, self()]),
|
|
|
|
{ok, wait_for_stream, #state{socket = Socket,
|
|
|
|
receiver = ReceiverPid,
|
2002-12-07 21:27:26 +01:00
|
|
|
streamid = new_id(),
|
|
|
|
queue = queue:new()}}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
wait_for_stream({xmlstreamstart, Name, Attrs}, StateData) ->
|
|
|
|
% TODO
|
|
|
|
case {xml:get_attr_s("xmlns", Attrs), xml:get_attr_s("xmlns:db", Attrs)} of
|
|
|
|
{"jabber:server", "jabber:server:dialback"} ->
|
2003-01-04 21:09:25 +01:00
|
|
|
Me = case xml:get_attr_s("to", Attrs) of
|
|
|
|
"" -> ?MYNAME;
|
|
|
|
Dom -> Dom
|
|
|
|
end,
|
|
|
|
case lists:member(Me, ejabberd_router:dirty_get_all_domains()) of
|
|
|
|
true ->
|
|
|
|
send_text(StateData#state.socket, ?STREAM_HEADER),
|
|
|
|
{next_state, wait_for_key, StateData#state{myname = Me}};
|
|
|
|
_ ->
|
|
|
|
send_text(StateData#state.socket, ?INVALID_DOMAIN_ERR),
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
|
|
|
send_text(StateData#state.socket, ?INVALID_HEADER_ERR),
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_stream(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
wait_for_key({xmlstreamelement, El}, StateData) ->
|
|
|
|
case is_key_packet(El) of
|
|
|
|
{key, To, From, Id, Key} ->
|
|
|
|
io:format("GET KEY: ~p~n", [{To, From, Id, Key}]),
|
2003-01-04 21:09:25 +01:00
|
|
|
ejabberd_s2s_out:start(StateData#state.myname, From,
|
|
|
|
{verify, self(), Key}),
|
2002-12-07 21:27:26 +01:00
|
|
|
{next_state,
|
|
|
|
wait_for_verification,
|
|
|
|
StateData#state{server = From}};
|
2002-12-06 21:59:19 +01:00
|
|
|
{verify, To, From, Id, Key} ->
|
|
|
|
io:format("VERIFY KEY: ~p~n", [{To, From, Id, Key}]),
|
2003-01-05 21:24:59 +01:00
|
|
|
Key1 = ejabberd_s2s:get_key({StateData#state.myname, From}),
|
2002-12-07 21:27:26 +01:00
|
|
|
Type = if Key == Key1 -> "valid";
|
|
|
|
true -> "invalid"
|
|
|
|
end,
|
|
|
|
send_element(StateData#state.socket,
|
|
|
|
{xmlelement,
|
|
|
|
"db:verify",
|
2003-01-04 21:09:25 +01:00
|
|
|
[{"from", StateData#state.myname},
|
2002-12-07 21:27:26 +01:00
|
|
|
{"to", From},
|
|
|
|
{"id", Id},
|
|
|
|
{"type", Type}],
|
|
|
|
[]}),
|
2002-12-06 21:59:19 +01:00
|
|
|
{next_state, wait_for_key, StateData};
|
|
|
|
_ ->
|
|
|
|
{next_state, wait_for_key, StateData}
|
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_key({xmlstreamend, Name}, StateData) ->
|
|
|
|
% TODO
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_key(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
wait_for_verification(valid, StateData) ->
|
|
|
|
send_element(StateData#state.socket,
|
|
|
|
{xmlelement,
|
|
|
|
"db:result",
|
2003-01-04 21:09:25 +01:00
|
|
|
[{"from", StateData#state.myname},
|
2002-12-07 21:27:26 +01:00
|
|
|
{"to", StateData#state.server},
|
|
|
|
{"type", "valid"}],
|
|
|
|
[]}),
|
|
|
|
send_queue(StateData#state.socket, StateData#state.queue),
|
|
|
|
{next_state, stream_established, StateData};
|
|
|
|
|
|
|
|
wait_for_verification(invalid, StateData) ->
|
|
|
|
send_element(StateData#state.socket,
|
|
|
|
{xmlelement,
|
|
|
|
"db:result",
|
2003-01-04 21:09:25 +01:00
|
|
|
[{"from", StateData#state.myname},
|
2002-12-07 21:27:26 +01:00
|
|
|
{"to", StateData#state.server},
|
|
|
|
{"type", "invalid"}],
|
|
|
|
[]}),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_verification({xmlstreamelement, El}, StateData) ->
|
|
|
|
case is_key_packet(El) of
|
|
|
|
{verify, To, From, Id, Key} ->
|
|
|
|
io:format("VERIFY KEY: ~p~n", [{To, From, Id, Key}]),
|
2003-01-05 21:24:59 +01:00
|
|
|
Key1 = ejabberd_s2s:get_key({StateData#state.myname, From}),
|
2002-12-07 21:27:26 +01:00
|
|
|
Type = if Key == Key1 -> "valid";
|
|
|
|
true -> "invalid"
|
|
|
|
end,
|
|
|
|
send_element(StateData#state.socket,
|
|
|
|
{xmlelement,
|
|
|
|
"db:verify",
|
2003-01-04 21:09:25 +01:00
|
|
|
[{"from", StateData#state.myname},
|
2002-12-07 21:27:26 +01:00
|
|
|
{"to", From},
|
|
|
|
{"id", Id},
|
|
|
|
{"type", Type}],
|
|
|
|
[]}),
|
|
|
|
{next_state, wait_for_verification, StateData};
|
|
|
|
_ ->
|
|
|
|
{next_state, wait_for_verification, StateData}
|
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_verification({xmlstreamend, Name}, StateData) ->
|
|
|
|
% TODO
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_verification(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
stream_established({xmlstreamelement, El}, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
{xmlelement, Name, Attrs, Els} = El,
|
2002-12-20 21:42:08 +01:00
|
|
|
case Name of
|
|
|
|
"db:verify" ->
|
|
|
|
case is_key_packet(El) of
|
|
|
|
{verify, To, From, Id, Key} ->
|
|
|
|
io:format("VERIFY KEY: ~p~n", [{To, From, Id, Key}]),
|
2003-01-05 21:24:59 +01:00
|
|
|
Key1 = ejabberd_s2s:get_key({StateData#state.myname,
|
|
|
|
From}),
|
2002-12-20 21:42:08 +01:00
|
|
|
Type = if Key == Key1 -> "valid";
|
|
|
|
true -> "invalid"
|
|
|
|
end,
|
|
|
|
send_element(StateData#state.socket,
|
|
|
|
{xmlelement,
|
|
|
|
"db:verify",
|
2003-01-04 21:09:25 +01:00
|
|
|
[{"from", StateData#state.myname},
|
2002-12-20 21:42:08 +01:00
|
|
|
{"to", From},
|
|
|
|
{"id", Id},
|
|
|
|
{"type", Type}],
|
|
|
|
[]});
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
From = xml:get_attr_s("from", Attrs),
|
|
|
|
FromJID1 = jlib:string_to_jid(From),
|
|
|
|
FromJID = case FromJID1 of
|
|
|
|
{Node, Server, Resource} ->
|
|
|
|
if Server == StateData#state.server -> FromJID1;
|
|
|
|
true -> error
|
|
|
|
end;
|
|
|
|
_ -> error
|
|
|
|
end,
|
|
|
|
To = xml:get_attr_s("to", Attrs),
|
|
|
|
ToJID = case To of
|
|
|
|
"" -> error;
|
|
|
|
_ -> jlib:string_to_jid(To)
|
|
|
|
end,
|
|
|
|
if ((Name == "iq") or
|
|
|
|
(Name == "message") or
|
|
|
|
(Name == "presence")) and
|
|
|
|
(ToJID /= error) and (FromJID /= error) ->
|
|
|
|
ejabberd_router:route(FromJID, ToJID, El);
|
|
|
|
true ->
|
|
|
|
error
|
|
|
|
end
|
2002-12-06 21:59:19 +01:00
|
|
|
end,
|
2003-01-26 21:16:53 +01:00
|
|
|
{next_state, stream_established, StateData, ?S2STIMEOUT};
|
2002-12-07 21:27:26 +01:00
|
|
|
|
|
|
|
stream_established({xmlstreamend, Name}, StateData) ->
|
|
|
|
% TODO
|
|
|
|
{stop, normal, StateData};
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2003-01-26 21:16:53 +01:00
|
|
|
stream_established(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
stream_established(closed, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
% TODO
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2003-01-24 21:18:33 +01:00
|
|
|
%state_name(Event, From, StateData) ->
|
|
|
|
% Reply = ok,
|
|
|
|
% {reply, Reply, state_name, StateData}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_event(Event, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_sync_event(Event, From, StateName, StateData) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName, StateData}.
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
code_change(OldVsn, StateName, StateData, Extra) ->
|
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
|
|
|
send_text(StateData#state.socket, Text),
|
2002-12-07 21:27:26 +01:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
handle_info({send_element, El}, StateName, StateData) ->
|
|
|
|
case StateName of
|
|
|
|
stream_established ->
|
|
|
|
send_element(StateData#state.socket, El),
|
|
|
|
{next_state, StateName, StateData};
|
|
|
|
_ ->
|
|
|
|
Q = queue:in(El, StateData#state.queue),
|
|
|
|
{next_state, StateName, StateData#state{queue = Q}}
|
|
|
|
end.
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
terminate(Reason, StateName, StateData) ->
|
|
|
|
% case StateData#state.user of
|
|
|
|
% "" ->
|
|
|
|
% ok;
|
|
|
|
% _ ->
|
|
|
|
% %ejabberd_sm:close_session(StateData#state.user,
|
|
|
|
% % StateData#state.resource)
|
|
|
|
% end,
|
2002-12-07 21:27:26 +01:00
|
|
|
%ejabberd_s2s ! {closed_conection, StateData#state.server},
|
2002-12-06 21:59:19 +01:00
|
|
|
gen_tcp:close(StateData#state.socket),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
receiver(Socket, C2SPid) ->
|
|
|
|
XMLStreamPid = xml_stream:start(C2SPid),
|
|
|
|
receiver(Socket, C2SPid, XMLStreamPid).
|
|
|
|
|
|
|
|
receiver(Socket, C2SPid, XMLStreamPid) ->
|
|
|
|
case gen_tcp:recv(Socket, 0) of
|
|
|
|
{ok, Text} ->
|
|
|
|
xml_stream:send_text(XMLStreamPid, Text),
|
|
|
|
receiver(Socket, C2SPid, XMLStreamPid);
|
2003-01-08 21:42:03 +01:00
|
|
|
{error, Reason} ->
|
2002-12-06 21:59:19 +01:00
|
|
|
exit(XMLStreamPid, closed),
|
|
|
|
gen_fsm:send_event(C2SPid, closed),
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
send_text(Socket, Text) ->
|
|
|
|
gen_tcp:send(Socket,Text).
|
|
|
|
|
|
|
|
send_element(Socket, El) ->
|
|
|
|
send_text(Socket, xml:element_to_string(El)).
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
send_queue(Socket, Q) ->
|
|
|
|
case queue:out(Q) of
|
|
|
|
{{value, El}, Q1} ->
|
|
|
|
send_element(Socket, El),
|
|
|
|
send_queue(Socket, Q1);
|
|
|
|
{empty, Q1} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
new_id() ->
|
2002-12-13 21:58:27 +01:00
|
|
|
randoms:get_string().
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
is_key_packet({xmlelement, Name, Attrs, Els}) when Name == "db:result" ->
|
|
|
|
{key,
|
|
|
|
xml:get_attr_s("to", Attrs),
|
|
|
|
xml:get_attr_s("from", Attrs),
|
|
|
|
xml:get_attr_s("id", Attrs),
|
|
|
|
xml:get_cdata(Els)};
|
|
|
|
is_key_packet({xmlelement, Name, Attrs, Els}) when Name == "db:verify" ->
|
|
|
|
{verify,
|
|
|
|
xml:get_attr_s("to", Attrs),
|
|
|
|
xml:get_attr_s("from", Attrs),
|
|
|
|
xml:get_attr_s("id", Attrs),
|
|
|
|
xml:get_cdata(Els)};
|
|
|
|
is_key_packet(_) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
|
|
|
|
|