2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s_out.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 6 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s_out).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
-behaviour(gen_fsm).
|
|
|
|
|
|
|
|
%% External exports
|
2003-07-20 22:35:35 +02:00
|
|
|
-export([start/3, start_link/3, send_text/2, send_element/2]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%% gen_fsm callbacks
|
|
|
|
-export([init/1,
|
2002-12-07 21:27:26 +01:00
|
|
|
open_socket/2,
|
2002-12-06 21:59:19 +01:00
|
|
|
wait_for_stream/2,
|
2002-12-07 21:27:26 +01:00
|
|
|
wait_for_validation/2,
|
|
|
|
stream_established/2,
|
2003-01-24 21:18:33 +01:00
|
|
|
handle_event/3,
|
|
|
|
handle_sync_event/4,
|
2002-12-06 21:59:19 +01:00
|
|
|
handle_info/3,
|
2003-01-24 21:18:33 +01:00
|
|
|
terminate/3,
|
|
|
|
code_change/4]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-05-09 21:32:59 +02:00
|
|
|
-include("jlib.hrl").
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
-record(state, {socket, receiver, streamid,
|
2003-01-04 21:09:25 +01:00
|
|
|
myname, server, xmlpid, queue,
|
2003-12-06 20:58:49 +01:00
|
|
|
new = false, verify = false,
|
|
|
|
timer}).
|
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' "
|
2003-01-04 21:09:25 +01:00
|
|
|
"xmlns:db='jabber:server:dialback' "
|
|
|
|
"to='~s'>"
|
2002-12-06 21:59:19 +01:00
|
|
|
).
|
|
|
|
|
|
|
|
-define(STREAM_TRAILER, "</stream:stream>").
|
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
-define(INVALID_NAMESPACE_ERR,
|
|
|
|
xml:element_to_string(?SERR_INVALID_NAMESPACE)).
|
|
|
|
|
|
|
|
-define(HOST_UNKNOWN_ERR,
|
|
|
|
xml:element_to_string(?SERR_HOST_UNKNOWN)).
|
|
|
|
|
|
|
|
-define(INVALID_XML_ERR,
|
|
|
|
xml:element_to_string(?SERR_XML_NOT_WELL_FORMED)).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-07-20 22:35:35 +02:00
|
|
|
start(From, Host, Type) ->
|
|
|
|
supervisor:start_child(ejabberd_s2s_out_sup, [From, Host, Type]).
|
|
|
|
|
2003-07-14 20:06:03 +02:00
|
|
|
start_link(From, Host, Type) ->
|
2003-07-20 22:35:35 +02:00
|
|
|
gen_fsm:start_link(ejabberd_s2s_out, [From, Host, Type], ?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-01-04 21:09:25 +01:00
|
|
|
init([From, Server, Type]) ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("started: ~p", [{From, Server, Type}]),
|
2002-12-07 21:27:26 +01:00
|
|
|
gen_fsm:send_event(self(), init),
|
2002-12-13 21:58:27 +01:00
|
|
|
{New, Verify} = case Type of
|
|
|
|
{new, Key} ->
|
|
|
|
{Key, false};
|
2003-09-03 19:49:42 +02:00
|
|
|
{verify, Pid, Key, SID} ->
|
|
|
|
{false, {Pid, Key, SID}}
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
2003-12-06 20:58:49 +01:00
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
2002-12-07 21:27:26 +01:00
|
|
|
{ok, open_socket, #state{queue = queue:new(),
|
2003-01-04 21:09:25 +01:00
|
|
|
myname = From,
|
2002-12-07 21:27:26 +01:00
|
|
|
server = Server,
|
2002-12-13 21:58:27 +01:00
|
|
|
new = New,
|
2003-12-06 20:58:49 +01:00
|
|
|
verify = Verify,
|
|
|
|
timer = Timer}}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData}
|
|
|
|
%%----------------------------------------------------------------------
|
2002-12-07 21:27:26 +01:00
|
|
|
open_socket(init, StateData) ->
|
2003-02-04 21:45:23 +01:00
|
|
|
{Addr, Port} = get_addr_port(StateData#state.server),
|
|
|
|
?DEBUG("s2s_out: connecting to ~s:~p~n", [Addr, Port]),
|
2003-10-12 20:21:16 +02:00
|
|
|
Res = case gen_tcp:connect(Addr, Port,
|
|
|
|
[binary, {packet, 0}]) of
|
|
|
|
{ok, _Socket} = R -> R;
|
|
|
|
{error, Reason1} ->
|
|
|
|
?DEBUG("s2s_out: connect return ~p~n", [Reason1]),
|
|
|
|
gen_tcp:connect(Addr, Port,
|
2003-10-29 21:09:09 +01:00
|
|
|
[binary, {packet, 0}, inet6])
|
2003-10-12 20:21:16 +02:00
|
|
|
end,
|
|
|
|
case Res of
|
2002-12-07 21:27:26 +01:00
|
|
|
{ok, Socket} ->
|
|
|
|
XMLStreamPid = xml_stream:start(self()),
|
2003-01-04 21:09:25 +01:00
|
|
|
send_text(Socket, io_lib:format(?STREAM_HEADER,
|
|
|
|
[StateData#state.server])),
|
2002-12-07 21:27:26 +01:00
|
|
|
{next_state, wait_for_stream,
|
|
|
|
StateData#state{socket = Socket,
|
|
|
|
xmlpid = XMLStreamPid,
|
2003-12-06 20:58:49 +01:00
|
|
|
streamid = new_id()}};
|
2002-12-07 21:27:26 +01:00
|
|
|
{error, Reason} ->
|
2003-10-12 20:21:16 +02:00
|
|
|
?DEBUG("s2s_out: inet6 connect return ~p~n", [Reason]),
|
|
|
|
Error = ?ERR_REMOTE_SERVER_NOT_FOUND,
|
2003-05-09 21:32:59 +02:00
|
|
|
bounce_messages(Error),
|
2002-12-07 21:27:26 +01:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end.
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
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"} ->
|
2002-12-13 21:58:27 +01:00
|
|
|
Server = StateData#state.server,
|
|
|
|
New = case StateData#state.new of
|
|
|
|
false ->
|
2003-01-05 21:24:59 +01:00
|
|
|
case
|
|
|
|
ejabberd_s2s:try_register(
|
|
|
|
{StateData#state.myname, Server}) of
|
2002-12-13 21:58:27 +01:00
|
|
|
{key, Key} ->
|
|
|
|
Key;
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
Key ->
|
|
|
|
Key
|
|
|
|
end,
|
|
|
|
case New of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
Key1 ->
|
2002-12-07 21:27:26 +01:00
|
|
|
send_element(StateData#state.socket,
|
|
|
|
{xmlelement,
|
|
|
|
"db:result",
|
2003-01-04 21:09:25 +01:00
|
|
|
[{"from", StateData#state.myname},
|
2002-12-13 21:58:27 +01:00
|
|
|
{"to", Server}],
|
|
|
|
[{xmlcdata, Key1}]})
|
|
|
|
end,
|
|
|
|
case StateData#state.verify of
|
|
|
|
false ->
|
|
|
|
ok;
|
2003-09-03 19:49:42 +02:00
|
|
|
{Pid, Key2, SID} ->
|
2002-12-06 21:59:19 +01:00
|
|
|
send_element(StateData#state.socket,
|
|
|
|
{xmlelement,
|
|
|
|
"db:verify",
|
2003-01-04 21:09:25 +01:00
|
|
|
[{"from", StateData#state.myname},
|
2003-09-02 22:13:32 +02:00
|
|
|
{"to", StateData#state.server},
|
2003-09-03 19:49:42 +02:00
|
|
|
{"id", SID}],
|
2002-12-13 21:58:27 +01:00
|
|
|
[{xmlcdata, Key2}]})
|
|
|
|
end,
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, wait_for_validation, StateData#state{new = New}};
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
2003-05-12 20:36:13 +02:00
|
|
|
send_text(StateData#state.socket, ?INVALID_NAMESPACE_ERR),
|
2002-12-06 21:59:19 +01:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
wait_for_stream({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData#state.socket,
|
|
|
|
?STREAM_HEADER ++ ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_stream(timeout, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
wait_for_stream(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
|
|
|
|
wait_for_validation({xmlstreamelement, El}, StateData) ->
|
|
|
|
case is_verify_res(El) of
|
|
|
|
{result, To, From, Id, Type} ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("recv result: ~p", [{From, To, Id, Type}]),
|
2002-12-07 21:27:26 +01:00
|
|
|
case Type of
|
|
|
|
"valid" ->
|
|
|
|
send_queue(StateData#state.socket, StateData#state.queue),
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, stream_established, StateData};
|
2002-12-07 21:27:26 +01:00
|
|
|
_ ->
|
|
|
|
% TODO: bounce packets
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
2002-12-13 21:58:27 +01:00
|
|
|
{verify, To, From, Id, Type} ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("recv verify: ~p", [{From, To, Id, Type}]),
|
2002-12-13 21:58:27 +01:00
|
|
|
case StateData#state.verify of
|
|
|
|
false ->
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, wait_for_validation, StateData};
|
2003-09-03 19:49:42 +02:00
|
|
|
{Pid, _Key, _SID} ->
|
2002-12-13 21:58:27 +01:00
|
|
|
case Type of
|
|
|
|
"valid" ->
|
2003-10-29 21:09:09 +01:00
|
|
|
gen_fsm:send_event(
|
|
|
|
Pid, {valid,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.myname});
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2003-10-29 21:09:09 +01:00
|
|
|
gen_fsm:send_event(
|
|
|
|
Pid, {invalid,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.myname})
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
2003-07-14 20:06:03 +02:00
|
|
|
{stop, normal, StateData}
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, wait_for_validation, StateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
end;
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
wait_for_validation({xmlstreamend, Name}, StateData) ->
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_validation({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData#state.socket,
|
|
|
|
?STREAM_HEADER ++ ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_validation(timeout, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
wait_for_validation(closed, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
stream_established({xmlstreamelement, El}, StateData) ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("stream established", []),
|
2002-12-06 21:59:19 +01:00
|
|
|
case is_verify_res(El) of
|
2002-12-13 21:58:27 +01:00
|
|
|
{verify, VTo, VFrom, VId, VType} ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("recv verify: ~p", [{VFrom, VTo, VId, VType}]),
|
2002-12-13 21:58:27 +01:00
|
|
|
case StateData#state.verify of
|
2003-09-03 19:49:42 +02:00
|
|
|
{VPid, _VKey, _SID} ->
|
2002-12-13 21:58:27 +01:00
|
|
|
case VType of
|
|
|
|
"valid" ->
|
2003-10-29 21:09:09 +01:00
|
|
|
gen_fsm:send_event(
|
|
|
|
VPid, {valid,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.myname});
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2003-10-29 21:09:09 +01:00
|
|
|
gen_fsm:send_event(
|
|
|
|
VPid, {invalid,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.myname})
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
2002-12-13 21:58:27 +01:00
|
|
|
ok
|
|
|
|
end;
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
2002-12-13 21:58:27 +01:00
|
|
|
ok
|
|
|
|
end,
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, stream_established, StateData};
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
stream_established({xmlstreamend, Name}, StateData) ->
|
2003-01-26 21:16:53 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
stream_established({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData#state.socket,
|
|
|
|
?STREAM_HEADER ++ ?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-01-26 21:16:53 +01:00
|
|
|
stream_established(timeout, StateData) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
stream_established(closed, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
{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),
|
2003-12-06 20:58:49 +01:00
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
|
|
|
{next_state, StateName, StateData#state{timer = Timer}};
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
handle_info({send_element, El}, StateName, StateData) ->
|
2003-12-06 20:58:49 +01:00
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
2002-12-07 21:27:26 +01:00
|
|
|
case StateName of
|
|
|
|
stream_established ->
|
|
|
|
send_element(StateData#state.socket, El),
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, StateName, StateData#state{timer = Timer}};
|
2002-12-07 21:27:26 +01:00
|
|
|
_ ->
|
|
|
|
Q = queue:in(El, StateData#state.queue),
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, StateName, StateData#state{queue = Q,
|
|
|
|
timer = Timer}}
|
2002-12-07 21:27:26 +01:00
|
|
|
end;
|
2003-12-06 20:58:49 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
handle_info({tcp, Socket, Data}, StateName, StateData) ->
|
|
|
|
xml_stream:send_text(StateData#state.xmlpid, Data),
|
|
|
|
{next_state, StateName, StateData};
|
2003-12-06 20:58:49 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
handle_info({tcp_closed, Socket}, StateName, StateData) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
gen_fsm:send_event(self(), closed),
|
2002-12-06 21:59:19 +01:00
|
|
|
{next_state, StateName, StateData};
|
2003-12-06 20:58:49 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
handle_info({tcp_error, Socket, Reason}, StateName, StateData) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
gen_fsm:send_event(self(), closed),
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
|
|
|
|
handle_info({timeout, Timer, _}, StateName,
|
|
|
|
#state{timer = Timer} = StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
handle_info(_, StateName, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
terminate(Reason, StateName, StateData) ->
|
2003-05-09 21:32:59 +02:00
|
|
|
?INFO_MSG("terminated: ~p", [Reason]),
|
2003-12-06 20:58:49 +01:00
|
|
|
Error = ?ERR_REMOTE_SERVER_NOT_FOUND,
|
|
|
|
bounce_queue(StateData#state.queue, Error),
|
2002-12-13 21:58:27 +01:00
|
|
|
case StateData#state.new of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
Key ->
|
2003-01-05 21:24:59 +01:00
|
|
|
ejabberd_s2s ! {closed_conection, {StateData#state.myname,
|
|
|
|
StateData#state.server}}
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
|
|
|
case StateData#state.socket of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
Socket ->
|
2003-03-12 20:48:05 +01:00
|
|
|
gen_tcp:close(Socket),
|
|
|
|
exit(StateData#state.xmlpid, closed)
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
2002-12-06 21:59:19 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2003-12-06 20:58:49 +01:00
|
|
|
bounce_queue(Q, Error) ->
|
|
|
|
case queue:out(Q) of
|
|
|
|
{{value, El}, Q1} ->
|
|
|
|
Err = jlib:make_error_reply(El, Error),
|
|
|
|
From = jlib:string_to_jid(xml:get_tag_attr_s("from", El)),
|
|
|
|
To = jlib:string_to_jid(xml:get_tag_attr_s("to", El)),
|
|
|
|
ejabberd_router:route(To, From, Err),
|
|
|
|
bounce_queue(Q1, Error);
|
|
|
|
{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
|
|
|
|
2003-12-06 20:58:49 +01:00
|
|
|
cancel_timer(Timer) ->
|
|
|
|
erlang:cancel_timer(Timer),
|
|
|
|
receive
|
|
|
|
{timeout, Timer, _} ->
|
|
|
|
ok
|
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2003-05-09 21:32:59 +02:00
|
|
|
bounce_messages(Error) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
receive
|
|
|
|
{send_element, El} ->
|
2003-12-06 20:58:49 +01:00
|
|
|
{xmlelement, _Name, Attrs, _SubTags} = El,
|
2002-12-07 21:27:26 +01:00
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"error" ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
2003-05-09 21:32:59 +02:00
|
|
|
Err = jlib:make_error_reply(El, Error),
|
2002-12-07 21:27:26 +01:00
|
|
|
From = jlib:string_to_jid(xml:get_attr_s("from", Attrs)),
|
|
|
|
To = jlib:string_to_jid(xml:get_attr_s("to", Attrs)),
|
2003-12-06 20:58:49 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
2003-05-09 21:32:59 +02:00
|
|
|
bounce_messages(Error)
|
2002-12-07 21:27:26 +01:00
|
|
|
after 0 ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2003-01-24 21:18:33 +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.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
is_verify_res({xmlelement, Name, Attrs, Els}) when Name == "db:result" ->
|
|
|
|
{result,
|
|
|
|
xml:get_attr_s("to", Attrs),
|
|
|
|
xml:get_attr_s("from", Attrs),
|
|
|
|
xml:get_attr_s("id", Attrs),
|
|
|
|
xml:get_attr_s("type", Attrs)};
|
|
|
|
is_verify_res({xmlelement, Name, Attrs, Els}) when Name == "db:verify" ->
|
2002-12-13 21:58:27 +01:00
|
|
|
{verify,
|
2002-12-06 21:59:19 +01:00
|
|
|
xml:get_attr_s("to", Attrs),
|
|
|
|
xml:get_attr_s("from", Attrs),
|
|
|
|
xml:get_attr_s("id", Attrs),
|
|
|
|
xml:get_attr_s("type", Attrs)};
|
|
|
|
is_verify_res(_) ->
|
|
|
|
false.
|
|
|
|
|
2003-02-04 21:45:23 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% SRV support
|
|
|
|
|
|
|
|
-include_lib("kernel/include/inet.hrl").
|
|
|
|
|
|
|
|
get_addr_port(Server) ->
|
2003-10-14 21:34:17 +02:00
|
|
|
Res = case inet_res:getbyname("_xmpp-server._tcp." ++ Server, srv) of
|
2003-10-12 20:21:16 +02:00
|
|
|
{error, _Reason} ->
|
|
|
|
inet_res:getbyname("_jabber._tcp." ++ Server, srv);
|
|
|
|
{ok, _HEnt} = R -> R
|
|
|
|
end,
|
|
|
|
case Res of
|
2003-02-04 21:45:23 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
?DEBUG("srv lookup of '~s' failed: ~p~n", [Server, Reason]),
|
|
|
|
{Server, ejabberd_config:get_local_option(outgoing_s2s_port)};
|
|
|
|
{ok, HEnt} ->
|
|
|
|
?DEBUG("srv lookup of '~s': ~p~n",
|
|
|
|
[Server, HEnt#hostent.h_addr_list]),
|
|
|
|
case HEnt#hostent.h_addr_list of
|
|
|
|
[] ->
|
|
|
|
{Server,
|
|
|
|
ejabberd_config:get_local_option(outgoing_s2s_port)};
|
|
|
|
[{_, _, Port, Host} | _] ->
|
|
|
|
{Host, Port}
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|