2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_s2s_out.erl
|
2007-07-24 11:04:09 +02:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Manage outgoing server-to-server connections
|
|
|
|
%%% Created : 6 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_s2s_out).
|
2007-07-24 11:04:09 +02:00
|
|
|
-author('alexey@process-one.net').
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2007-09-14 16:21:25 +02:00
|
|
|
-behaviour(p1_fsm).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%% External exports
|
2005-11-12 04:42:12 +01:00
|
|
|
-export([start/3,
|
|
|
|
start_link/3,
|
2007-07-18 18:26:50 +02:00
|
|
|
start_connection/1,
|
|
|
|
stop_connection/1]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2007-09-14 16:21:25 +02:00
|
|
|
%% p1_fsm callbacks (same as gen_fsm)
|
2002-12-06 21:59:19 +01:00
|
|
|
-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,
|
2005-10-25 03:08:37 +02:00
|
|
|
wait_for_features/2,
|
2005-11-03 06:04:54 +01:00
|
|
|
wait_for_auth_result/2,
|
2005-10-25 03:08:37 +02:00
|
|
|
wait_for_starttls_proceed/2,
|
2006-02-06 06:12:54 +01:00
|
|
|
reopen_socket/2,
|
2007-09-14 16:18:32 +02:00
|
|
|
wait_before_retry/2,
|
2002-12-07 21:27:26 +01:00
|
|
|
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,
|
2006-03-14 05:26:15 +01:00
|
|
|
code_change/4,
|
|
|
|
test_get_addr_port/1]).
|
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
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
-record(state, {socket,
|
2005-10-25 03:08:37 +02:00
|
|
|
streamid,
|
|
|
|
use_v10,
|
|
|
|
tls = false,
|
|
|
|
tls_required = false,
|
|
|
|
tls_enabled = false,
|
|
|
|
tls_options = [],
|
2005-11-03 06:04:54 +01:00
|
|
|
authenticated = false,
|
2006-07-28 18:18:50 +02:00
|
|
|
db_enabled = true,
|
2005-11-03 06:04:54 +01:00
|
|
|
try_auth = true,
|
2005-10-25 03:08:37 +02:00
|
|
|
myname, server, 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.
|
|
|
|
|
2007-09-14 16:16:36 +02:00
|
|
|
%% Module start with or without supervisor:
|
|
|
|
-ifdef(NO_TRANSIENT_SUPERVISORS).
|
|
|
|
-define(SUPERVISOR_START, p1_fsm:start(ejabberd_s2s_out, [From, Host, Type],
|
2007-09-14 16:18:32 +02:00
|
|
|
?FSMLIMITS ++ ?FSMOPTS)).
|
2007-09-14 16:16:36 +02:00
|
|
|
-else.
|
|
|
|
-define(SUPERVISOR_START, supervisor:start_child(ejabberd_s2s_out_sup,
|
|
|
|
[From, Host, Type])).
|
|
|
|
-endif.
|
|
|
|
|
2007-07-24 11:04:09 +02:00
|
|
|
%% Only change this value if you now what your are doing:
|
|
|
|
-define(FSMLIMITS,[]).
|
|
|
|
%% -define(FSMLIMITS, [{max_queue, 2000}]).
|
2007-09-14 16:18:32 +02:00
|
|
|
-define(FSMTIMEOUT, 5000).
|
2007-07-24 11:04:09 +02:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
-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' "
|
2005-10-25 03:08:37 +02:00
|
|
|
"to='~s'~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) ->
|
2007-09-14 16:16:36 +02:00
|
|
|
?SUPERVISOR_START.
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2003-07-14 20:06:03 +02:00
|
|
|
start_link(From, Host, Type) ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:start_link(ejabberd_s2s_out, [From, Host, Type],
|
2007-11-25 17:14:32 +01:00
|
|
|
?FSMLIMITS ++ ?FSMOPTS).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2005-11-12 04:42:12 +01:00
|
|
|
start_connection(Pid) ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:send_event(Pid, init).
|
2005-11-12 04:42:12 +01:00
|
|
|
|
2007-07-18 18:26:50 +02:00
|
|
|
stop_connection(Pid) ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:send_event(Pid, stop).
|
2007-07-18 18:26:50 +02:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2007-09-14 16:21:25 +02:00
|
|
|
%%% Callback functions from p1_fsm
|
2002-12-06 21:59:19 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
2007-09-14 16:15:44 +02:00
|
|
|
%% {stop, StopReason}
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2003-01-04 21:09:25 +01:00
|
|
|
init([From, Server, Type]) ->
|
2007-07-24 11:04:09 +02:00
|
|
|
process_flag(trap_exit, true),
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("started: ~p", [{From, Server, Type}]),
|
2005-10-25 03:08:37 +02:00
|
|
|
TLS = case ejabberd_config:get_local_option(s2s_use_starttls) of
|
|
|
|
undefined ->
|
|
|
|
false;
|
|
|
|
UseStartTLS ->
|
|
|
|
UseStartTLS
|
|
|
|
end,
|
|
|
|
UseV10 = TLS,
|
|
|
|
TLSOpts = case ejabberd_config:get_local_option(s2s_certfile) of
|
|
|
|
undefined ->
|
|
|
|
[];
|
|
|
|
CertFile ->
|
|
|
|
[{certfile, CertFile}, connect]
|
|
|
|
end,
|
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} ->
|
2005-11-12 04:42:12 +01:00
|
|
|
start_connection(self()),
|
2003-09-03 19:49:42 +02:00
|
|
|
{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(), []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{ok, open_socket, #state{use_v10 = UseV10,
|
|
|
|
tls = TLS,
|
|
|
|
tls_options = TLSOpts,
|
|
|
|
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,
|
2007-09-14 16:18:32 +02:00
|
|
|
timer = Timer}, ?FSMTIMEOUT}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2007-09-14 16:15:44 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2002-12-07 21:27:26 +01:00
|
|
|
open_socket(init, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
log_s2s_out(StateData#state.new,
|
|
|
|
StateData#state.myname,
|
|
|
|
StateData#state.server),
|
|
|
|
?DEBUG("open_socket: ~p", [{StateData#state.myname,
|
2007-11-25 17:14:32 +01:00
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.new,
|
|
|
|
StateData#state.verify}]),
|
2006-12-31 01:38:11 +01:00
|
|
|
AddrList = case idna:domain_utf8_to_ascii(StateData#state.server) of
|
|
|
|
false -> [];
|
|
|
|
ASCIIAddr ->
|
|
|
|
get_addr_port(ASCIIAddr)
|
|
|
|
end,
|
2006-03-14 05:26:15 +01:00
|
|
|
case lists:foldl(fun({Addr, Port}, Acc) ->
|
|
|
|
case Acc of
|
|
|
|
{ok, Socket} ->
|
|
|
|
{ok, Socket};
|
|
|
|
_ ->
|
|
|
|
open_socket1(Addr, Port)
|
|
|
|
end
|
|
|
|
end, {error, badarg}, AddrList) of
|
|
|
|
{ok, Socket} ->
|
|
|
|
Version = if
|
|
|
|
StateData#state.use_v10 ->
|
|
|
|
" version='1.0'";
|
|
|
|
true ->
|
|
|
|
""
|
|
|
|
end,
|
|
|
|
NewStateData = StateData#state{socket = Socket,
|
|
|
|
tls_enabled = false,
|
|
|
|
streamid = new_id()},
|
|
|
|
send_text(NewStateData, io_lib:format(?STREAM_HEADER,
|
|
|
|
[StateData#state.server,
|
|
|
|
Version])),
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_stream, NewStateData, ?FSMTIMEOUT};
|
2006-03-14 05:26:15 +01:00
|
|
|
{error, _Reason} ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("s2s connection: ~s -> ~s (remote server not found)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2007-09-14 16:18:32 +02:00
|
|
|
wait_before_reconnect(StateData, 300000)
|
|
|
|
%%{stop, normal, StateData}
|
2006-03-14 05:26:15 +01:00
|
|
|
end;
|
2007-07-18 18:26:50 +02:00
|
|
|
open_socket(stop, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
?INFO_MSG("s2s connection: ~s -> ~s (stopped in open socket)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
open_socket(timeout, StateData) ->
|
|
|
|
?INFO_MSG("s2s connection: ~s -> ~s (timeout in open socket)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2007-07-18 18:26:50 +02:00
|
|
|
{stop, normal, StateData};
|
2006-03-14 05:26:15 +01:00
|
|
|
open_socket(_, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, open_socket, StateData, ?FSMTIMEOUT}.
|
2006-03-14 05:26:15 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
open_socket1(Addr, Port) ->
|
2006-12-31 01:38:11 +01:00
|
|
|
?DEBUG("s2s_out: connecting to ~s:~p~n", [Addr, Port]),
|
|
|
|
Res = case catch ejabberd_socket:connect(
|
|
|
|
Addr, Port,
|
|
|
|
[binary, {packet, 0},
|
|
|
|
{active, false}]) of
|
|
|
|
{ok, _Socket} = R -> R;
|
|
|
|
{error, Reason1} ->
|
|
|
|
?DEBUG("s2s_out: connect return ~p~n", [Reason1]),
|
|
|
|
catch ejabberd_socket:connect(
|
|
|
|
Addr, Port,
|
|
|
|
[binary, {packet, 0},
|
|
|
|
{active, false}, inet6]);
|
|
|
|
{'EXIT', Reason1} ->
|
|
|
|
?DEBUG("s2s_out: connect crashed ~p~n", [Reason1]),
|
|
|
|
catch ejabberd_socket:connect(
|
|
|
|
Addr, Port,
|
|
|
|
[binary, {packet, 0},
|
|
|
|
{active, false}, inet6])
|
2003-10-12 20:21:16 +02:00
|
|
|
end,
|
|
|
|
case Res of
|
2002-12-07 21:27:26 +01:00
|
|
|
{ok, Socket} ->
|
2006-03-14 05:26:15 +01:00
|
|
|
{ok, Socket};
|
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]),
|
2006-03-14 05:26:15 +01:00
|
|
|
{error, Reason};
|
2004-03-08 21:20:15 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?DEBUG("s2s_out: inet6 connect crashed ~p~n", [Reason]),
|
2006-03-14 05:26:15 +01:00
|
|
|
{error, Reason}
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
wait_for_stream({xmlstreamstart, Name, Attrs}, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
case {xml:get_attr_s("xmlns", Attrs),
|
|
|
|
xml:get_attr_s("xmlns:db", Attrs),
|
|
|
|
xml:get_attr_s("version", Attrs) == "1.0"} of
|
|
|
|
{"jabber:server", "jabber:server:dialback", false} ->
|
|
|
|
send_db_request(StateData);
|
|
|
|
{"jabber:server", "jabber:server:dialback", true} when
|
|
|
|
StateData#state.use_v10 ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_features, StateData, ?FSMTIMEOUT};
|
2005-10-25 03:08:37 +02:00
|
|
|
{"jabber:server", "", true} when StateData#state.use_v10 ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_features, StateData#state{db_enabled = false}, ?FSMTIMEOUT};
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, ?INVALID_NAMESPACE_ERR),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (invalid namespace)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
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) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData,
|
|
|
|
?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (invalid xml)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_stream(timeout, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (timeout in wait_for_stream)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
wait_for_stream(closed, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (close in wait_for_stream)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2002-12-06 21:59:19 +01:00
|
|
|
{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} ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("recv result: ~p", [{From, To, Id, Type}]),
|
2002-12-07 21:27:26 +01:00
|
|
|
case Type of
|
|
|
|
"valid" ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_queue(StateData, StateData#state.queue),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Connection established: ~s -> ~s",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2003-12-12 21:35:40 +01:00
|
|
|
{next_state, stream_established,
|
|
|
|
StateData#state{queue = queue:new()}};
|
2002-12-07 21:27:26 +01:00
|
|
|
_ ->
|
2007-09-14 16:16:20 +02:00
|
|
|
%% TODO: bounce packets
|
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (invalid dialback key)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2002-12-07 21:27:26 +01:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
2002-12-13 21:58:27 +01:00
|
|
|
{verify, To, From, Id, Type} ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("recv verify: ~p", [{From, To, Id, Type}]),
|
2002-12-13 21:58:27 +01:00
|
|
|
case StateData#state.verify of
|
|
|
|
false ->
|
2007-09-14 16:18:32 +02:00
|
|
|
%% TODO: Should'nt we close the connection here ?
|
|
|
|
{next_state, wait_for_validation, StateData, ?FSMTIMEOUT};
|
2003-09-03 19:49:42 +02:00
|
|
|
{Pid, _Key, _SID} ->
|
2002-12-13 21:58:27 +01:00
|
|
|
case Type of
|
|
|
|
"valid" ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:send_event(
|
2003-10-29 21:09:09 +01:00
|
|
|
Pid, {valid,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.myname});
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:send_event(
|
2003-10-29 21:09:09 +01:00
|
|
|
Pid, {invalid,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.myname})
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
2004-10-06 16:12:04 +02:00
|
|
|
if
|
|
|
|
StateData#state.verify == false ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
true ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_validation, StateData,
|
|
|
|
?FSMTIMEOUT*3}
|
2004-10-06 16:12:04 +02:00
|
|
|
end
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
2002-12-06 21:59:19 +01:00
|
|
|
_ ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_validation, StateData, ?FSMTIMEOUT*3}
|
2002-12-06 21:59:19 +01:00
|
|
|
end;
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
wait_for_validation({xmlstreamend, Name}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
?INFO_MSG("wait for validation: ~s -> ~s (xmlstreamend)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_validation({xmlstreamerror, _}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
?INFO_MSG("wait for validation: ~s -> ~s (xmlstreamerror)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData,
|
|
|
|
?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_validation(timeout, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
?INFO_MSG("wait_for_validation: ~s -> ~s (connect timeout)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2002-12-06 21:59:19 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
wait_for_validation(closed, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
?INFO_MSG("wait for validation: ~s -> ~s (closed)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2002-12-06 21:59:19 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
wait_for_features({xmlstreamelement, El}, StateData) ->
|
|
|
|
case El of
|
|
|
|
{xmlelement, "stream:features", _Attrs, Els} ->
|
2005-11-03 06:04:54 +01:00
|
|
|
{SASLEXT, StartTLS, StartTLSRequired} =
|
2005-10-25 03:08:37 +02:00
|
|
|
lists:foldl(
|
2005-11-03 06:04:54 +01:00
|
|
|
fun({xmlelement, "mechanisms", Attrs1, Els1} = El1,
|
|
|
|
{SEXT, STLS, STLSReq} = Acc) ->
|
|
|
|
case xml:get_attr_s("xmlns", Attrs1) of
|
|
|
|
?NS_SASL ->
|
|
|
|
NewSEXT =
|
|
|
|
lists:any(
|
|
|
|
fun({xmlelement, "mechanism", _, Els2}) ->
|
|
|
|
case xml:get_cdata(Els2) of
|
|
|
|
"EXTERNAL" -> true;
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
(_) -> false
|
|
|
|
end, Els1),
|
|
|
|
{NewSEXT, STLS, STLSReq};
|
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
({xmlelement, "starttls", Attrs1, Els1} = El1,
|
|
|
|
{SEXT, STLS, STLSReq} = Acc) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
case xml:get_attr_s("xmlns", Attrs1) of
|
|
|
|
?NS_TLS ->
|
|
|
|
Req = case xml:get_subtag(El1, "required") of
|
|
|
|
{xmlelement, _, _, _} -> true;
|
|
|
|
false -> false
|
|
|
|
end,
|
2005-11-03 06:04:54 +01:00
|
|
|
{SEXT, true, Req};
|
2005-10-25 03:08:37 +02:00
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
2005-11-03 06:04:54 +01:00
|
|
|
end, {false, false, false}, Els),
|
2005-10-25 03:08:37 +02:00
|
|
|
if
|
2005-11-03 06:04:54 +01:00
|
|
|
(not SASLEXT) and (not StartTLS) and
|
|
|
|
StateData#state.authenticated ->
|
|
|
|
send_queue(StateData, StateData#state.queue),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Connection established: ~s -> ~s",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2005-11-03 06:04:54 +01:00
|
|
|
{next_state, stream_established,
|
|
|
|
StateData#state{queue = queue:new()}};
|
|
|
|
SASLEXT and StateData#state.try_auth and
|
|
|
|
(StateData#state.new /= false) ->
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "auth",
|
|
|
|
[{"xmlns", ?NS_SASL},
|
|
|
|
{"mechanism", "EXTERNAL"}],
|
|
|
|
[{xmlcdata,
|
|
|
|
jlib:encode_base64(
|
|
|
|
StateData#state.myname)}]}),
|
|
|
|
{next_state, wait_for_auth_result,
|
2007-09-14 16:18:32 +02:00
|
|
|
StateData#state{try_auth = false}, ?FSMTIMEOUT};
|
2005-10-25 03:08:37 +02:00
|
|
|
StartTLS and StateData#state.tls and
|
|
|
|
(not StateData#state.tls_enabled) ->
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement, "starttls",
|
|
|
|
[{"xmlns", ?NS_TLS}], []}),
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_starttls_proceed, StateData,
|
|
|
|
?FSMTIMEOUT};
|
2005-10-25 03:08:37 +02:00
|
|
|
StartTLSRequired and (not StateData#state.tls) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("restarted: ~p", [{StateData#state.myname,
|
2005-10-25 03:08:37 +02:00
|
|
|
StateData#state.server}]),
|
2006-09-05 06:26:28 +02:00
|
|
|
ejabberd_socket:close(StateData#state.socket),
|
2006-02-06 06:12:54 +01:00
|
|
|
{next_state, reopen_socket,
|
2005-10-25 03:08:37 +02:00
|
|
|
StateData#state{socket = undefined,
|
2007-09-14 16:18:32 +02:00
|
|
|
use_v10 = false}, ?FSMTIMEOUT};
|
2006-07-28 18:18:50 +02:00
|
|
|
StateData#state.db_enabled ->
|
|
|
|
send_db_request(StateData);
|
2005-10-25 03:08:37 +02:00
|
|
|
true ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("restarted: ~p", [{StateData#state.myname,
|
2006-07-28 18:18:50 +02:00
|
|
|
StateData#state.server}]),
|
|
|
|
% TODO: clear message queue
|
2006-09-05 06:26:28 +02:00
|
|
|
ejabberd_socket:close(StateData#state.socket),
|
2006-07-28 18:18:50 +02:00
|
|
|
{next_state, reopen_socket, StateData#state{socket = undefined,
|
2007-09-14 16:18:32 +02:00
|
|
|
use_v10 = false}, ?FSMTIMEOUT}
|
2005-10-25 03:08:37 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
send_text(StateData,
|
|
|
|
xml:element_to_string(?SERR_BAD_FORMAT) ++
|
|
|
|
?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (bad format)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_features({xmlstreamend, Name}, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait_for_features: xmlstreamend", []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_features({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData,
|
|
|
|
?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for features: xmlstreamerror", []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_features(timeout, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for features: timeout", []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_features(closed, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for features: closed", []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
2005-11-03 06:04:54 +01:00
|
|
|
wait_for_auth_result({xmlstreamelement, El}, StateData) ->
|
|
|
|
case El of
|
|
|
|
{xmlelement, "success", Attrs, _Els} ->
|
|
|
|
case xml:get_attr_s("xmlns", Attrs) of
|
|
|
|
?NS_SASL ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("auth: ~p", [{StateData#state.myname,
|
2005-11-03 06:04:54 +01:00
|
|
|
StateData#state.server}]),
|
2006-09-05 06:26:28 +02:00
|
|
|
ejabberd_socket:reset_stream(StateData#state.socket),
|
2005-11-03 06:04:54 +01:00
|
|
|
send_text(StateData,
|
|
|
|
io_lib:format(?STREAM_HEADER,
|
|
|
|
[StateData#state.server,
|
|
|
|
" version='1.0'"])),
|
|
|
|
{next_state, wait_for_stream,
|
|
|
|
StateData#state{streamid = new_id(),
|
|
|
|
authenticated = true
|
2007-09-14 16:18:32 +02:00
|
|
|
}, ?FSMTIMEOUT};
|
2005-11-03 06:04:54 +01:00
|
|
|
_ ->
|
|
|
|
send_text(StateData,
|
|
|
|
xml:element_to_string(?SERR_BAD_FORMAT) ++
|
|
|
|
?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (bad format)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2005-11-03 06:04:54 +01:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
{xmlelement, "failure", Attrs, _Els} ->
|
|
|
|
case xml:get_attr_s("xmlns", Attrs) of
|
|
|
|
?NS_SASL ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("restarted: ~p", [{StateData#state.myname,
|
2005-11-03 06:04:54 +01:00
|
|
|
StateData#state.server}]),
|
2006-09-05 06:26:28 +02:00
|
|
|
ejabberd_socket:close(StateData#state.socket),
|
2006-02-06 06:12:54 +01:00
|
|
|
{next_state, reopen_socket,
|
2007-09-14 16:18:32 +02:00
|
|
|
StateData#state{socket = undefined}, ?FSMTIMEOUT};
|
2005-11-03 06:04:54 +01:00
|
|
|
_ ->
|
|
|
|
send_text(StateData,
|
|
|
|
xml:element_to_string(?SERR_BAD_FORMAT) ++
|
|
|
|
?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (bad format)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2005-11-03 06:04:54 +01:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
send_text(StateData,
|
|
|
|
xml:element_to_string(?SERR_BAD_FORMAT) ++
|
|
|
|
?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (bad format)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2005-11-03 06:04:54 +01:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_auth_result({xmlstreamend, Name}, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for auth result: xmlstreamend", []),
|
2005-11-03 06:04:54 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_auth_result({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData,
|
|
|
|
?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for auth result: xmlstreamerror", []),
|
2005-11-03 06:04:54 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_auth_result(timeout, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for auth result: timeout", []),
|
2005-11-03 06:04:54 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_auth_result(closed, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for auth result: closed", []),
|
2005-11-03 06:04:54 +01:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
wait_for_starttls_proceed({xmlstreamelement, El}, StateData) ->
|
|
|
|
case El of
|
|
|
|
{xmlelement, "proceed", Attrs, _Els} ->
|
|
|
|
case xml:get_attr_s("xmlns", Attrs) of
|
|
|
|
?NS_TLS ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("starttls: ~p", [{StateData#state.myname,
|
2005-10-25 03:08:37 +02:00
|
|
|
StateData#state.server}]),
|
|
|
|
Socket = StateData#state.socket,
|
2005-11-05 22:15:53 +01:00
|
|
|
TLSOpts = case ejabberd_config:get_local_option(
|
|
|
|
{domain_certfile,
|
|
|
|
StateData#state.server}) of
|
|
|
|
undefined ->
|
|
|
|
StateData#state.tls_options;
|
|
|
|
CertFile ->
|
|
|
|
[{certfile, CertFile} |
|
|
|
|
lists:keydelete(
|
|
|
|
certfile, 1,
|
|
|
|
StateData#state.tls_options)]
|
|
|
|
end,
|
2006-09-05 06:26:28 +02:00
|
|
|
TLSSocket = ejabberd_socket:starttls(Socket, TLSOpts),
|
|
|
|
NewStateData = StateData#state{socket = TLSSocket,
|
2005-10-25 03:08:37 +02:00
|
|
|
streamid = new_id(),
|
|
|
|
tls_enabled = true
|
|
|
|
},
|
2005-11-03 06:04:54 +01:00
|
|
|
send_text(NewStateData,
|
|
|
|
io_lib:format(?STREAM_HEADER,
|
|
|
|
[StateData#state.server,
|
|
|
|
" version='1.0'"])),
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_stream, NewStateData, ?FSMTIMEOUT};
|
2005-10-25 03:08:37 +02:00
|
|
|
_ ->
|
2005-11-03 06:04:54 +01:00
|
|
|
send_text(StateData,
|
|
|
|
xml:element_to_string(?SERR_BAD_FORMAT) ++
|
|
|
|
?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (bad format)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
_ ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (bad format)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
|
|
|
|
wait_for_starttls_proceed({xmlstreamend, Name}, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for starttls proceed: xmlstreamend", []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_starttls_proceed({xmlstreamerror, _}, StateData) ->
|
|
|
|
send_text(StateData,
|
|
|
|
?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for starttls proceed: xmlstreamerror", []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_starttls_proceed(timeout, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for starttls proceed: timeout", []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
wait_for_starttls_proceed(closed, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("wait for starttls proceed: closed", []),
|
2005-10-25 03:08:37 +02:00
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
|
2006-02-06 06:12:54 +01:00
|
|
|
reopen_socket({xmlstreamelement, El}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, reopen_socket, StateData, ?FSMTIMEOUT};
|
2006-02-06 06:12:54 +01:00
|
|
|
reopen_socket({xmlstreamend, Name}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, reopen_socket, StateData, ?FSMTIMEOUT};
|
2006-02-06 06:12:54 +01:00
|
|
|
reopen_socket({xmlstreamerror, _}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, reopen_socket, StateData, ?FSMTIMEOUT};
|
2006-02-06 06:12:54 +01:00
|
|
|
reopen_socket(timeout, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
?INFO_MSG("reopen socket: timeout", []),
|
2006-02-06 06:12:54 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
reopen_socket(closed, StateData) ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:send_event(self(), init),
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, open_socket, StateData, ?FSMTIMEOUT}.
|
2006-02-06 06:12:54 +01:00
|
|
|
|
2007-09-14 16:18:32 +02:00
|
|
|
%% This state is use to avoid reconnecting to often to bad sockets
|
|
|
|
wait_before_retry(Event, StateData) ->
|
|
|
|
{next_state, wait_before_retry, StateData, ?FSMTIMEOUT}.
|
2006-02-06 06:12:54 +01:00
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
stream_established({xmlstreamelement, El}, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("s2S 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} ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("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" ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:send_event(
|
2003-10-29 21:09:09 +01:00
|
|
|
VPid, {valid,
|
|
|
|
StateData#state.server,
|
|
|
|
StateData#state.myname});
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2007-07-24 11:04:09 +02:00
|
|
|
p1_fsm:send_event(
|
2003-10-29 21:09:09 +01:00
|
|
|
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) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("stream established: ~s -> ~s (xmlstreamend)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2003-01-26 21:16:53 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
stream_established({xmlstreamerror, _}, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData,
|
|
|
|
?INVALID_XML_ERR ++ ?STREAM_TRAILER),
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("stream established: ~s -> ~s (xmlstreamerror)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-01-26 21:16:53 +01:00
|
|
|
stream_established(timeout, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("stream established: ~s -> ~s (timeout)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
2002-12-07 21:27:26 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
stream_established(closed, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?INFO_MSG("stream established: ~s -> ~s (closed)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
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} |
|
2007-09-14 16:15:44 +02:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
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} |
|
2007-09-14 16:15:44 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_event(Event, StateName, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, StateName, StateData, get_timeout_interval(StateName)}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% 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} |
|
2007-09-14 16:15:44 +02:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_sync_event(Event, From, StateName, StateData) ->
|
|
|
|
Reply = ok,
|
2007-09-14 16:18:32 +02:00
|
|
|
{reply, Reply, StateName, StateData, get_timeout_interval(StateName)}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
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} |
|
2007-09-14 16:15:44 +02:00
|
|
|
%% {stop, Reason, NewStateData}
|
2002-12-06 21:59:19 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, Text),
|
2003-12-06 20:58:49 +01:00
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, StateName, StateData#state{timer = Timer},
|
|
|
|
get_timeout_interval(StateName)};
|
2003-12-06 20:58:49 +01:00
|
|
|
|
2002-12-07 21:27:26 +01:00
|
|
|
handle_info({send_element, El}, StateName, StateData) ->
|
|
|
|
case StateName of
|
|
|
|
stream_established ->
|
2007-09-14 16:18:32 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = erlang:start_timer(?S2STIMEOUT, self(), []),
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData, El),
|
2003-12-06 20:58:49 +01:00
|
|
|
{next_state, StateName, StateData#state{timer = Timer}};
|
2007-09-14 16:18:32 +02:00
|
|
|
%% In this state we bounce all message: We are waiting before
|
|
|
|
%% trying to reconnect
|
|
|
|
wait_before_retry ->
|
|
|
|
bounce_element(El, ?ERR_REMOTE_SERVER_NOT_FOUND),
|
|
|
|
{next_state, StateName, StateData};
|
2002-12-07 21:27:26 +01:00
|
|
|
_ ->
|
|
|
|
Q = queue:in(El, StateData#state.queue),
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, StateName, StateData#state{queue = Q},
|
|
|
|
get_timeout_interval(StateName)}
|
2002-12-07 21:27:26 +01:00
|
|
|
end;
|
2003-12-06 20:58:49 +01:00
|
|
|
|
2007-09-14 16:18:32 +02:00
|
|
|
handle_info({timeout, Timer, _}, wait_before_retry,
|
|
|
|
#state{timer = Timer} = StateData) ->
|
|
|
|
?INFO_MSG("Reconnect delay expired: Will now retry to connect to ~s when needed.", [StateData#state.server]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-12-06 20:58:49 +01:00
|
|
|
handle_info({timeout, Timer, _}, StateName,
|
|
|
|
#state{timer = Timer} = StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
?INFO_MSG("Closing connection with ~s: timeout", [StateData#state.server]),
|
2003-12-06 20:58:49 +01:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
handle_info(_, StateName, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, StateName, StateData, get_timeout_interval(StateName)}.
|
2002-12-06 21:59:19 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
terminate(Reason, StateName, StateData) ->
|
2007-09-14 16:16:20 +02:00
|
|
|
?DEBUG("terminated: ~p", [{Reason, StateName}]),
|
2002-12-13 21:58:27 +01:00
|
|
|
case StateData#state.new of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
Key ->
|
2007-07-15 09:28:47 +02:00
|
|
|
ejabberd_s2s:remove_connection(
|
|
|
|
{StateData#state.myname, StateData#state.server}, self(), Key)
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
2007-09-14 16:16:20 +02:00
|
|
|
%% bounce queue manage by process and Erlang message queue
|
2007-07-11 16:10:28 +02:00
|
|
|
bounce_queue(StateData#state.queue, ?ERR_REMOTE_SERVER_NOT_FOUND),
|
2007-08-01 13:18:52 +02:00
|
|
|
bounce_messages(?ERR_REMOTE_SERVER_NOT_FOUND),
|
2002-12-07 21:27:26 +01:00
|
|
|
case StateData#state.socket of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
2006-01-13 02:55:20 +01:00
|
|
|
_Socket ->
|
2006-09-05 06:26:28 +02:00
|
|
|
ejabberd_socket:close(StateData#state.socket)
|
2002-12-07 21:27:26 +01:00
|
|
|
end,
|
2002-12-06 21:59:19 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
send_text(StateData, Text) ->
|
2006-09-05 06:26:28 +02:00
|
|
|
ejabberd_socket:send(StateData#state.socket, Text).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData, El) ->
|
|
|
|
send_text(StateData, xml:element_to_string(El)).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
send_queue(StateData, Q) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
case queue:out(Q) of
|
|
|
|
{{value, El}, Q1} ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData, El),
|
|
|
|
send_queue(StateData, Q1);
|
2002-12-07 21:27:26 +01:00
|
|
|
{empty, Q1} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2007-09-14 16:18:32 +02:00
|
|
|
%% Bounce a single message (xmlelement)
|
|
|
|
bounce_element(El, Error) ->
|
2007-11-25 17:14:32 +01:00
|
|
|
{xmlelement, _Name, Attrs, _SubTags} = El,
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"error" -> ok;
|
|
|
|
"result" -> ok;
|
|
|
|
_ ->
|
|
|
|
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)
|
|
|
|
end.
|
2007-09-14 16:18:32 +02:00
|
|
|
|
2003-12-06 20:58:49 +01:00
|
|
|
bounce_queue(Q, Error) ->
|
|
|
|
case queue:out(Q) of
|
|
|
|
{{value, El}, Q1} ->
|
2007-09-14 16:18:32 +02:00
|
|
|
bounce_element(El, Error),
|
2003-12-06 20:58:49 +01:00
|
|
|
bounce_queue(Q1, Error);
|
2007-09-14 16:18:32 +02:00
|
|
|
{empty, _} ->
|
2003-12-06 20:58:49 +01:00
|
|
|
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} ->
|
2007-11-25 17:14:32 +01:00
|
|
|
bounce_element(El, Error),
|
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
|
|
|
|
2005-10-25 03:08:37 +02:00
|
|
|
|
|
|
|
send_db_request(StateData) ->
|
|
|
|
Server = StateData#state.server,
|
|
|
|
New = case StateData#state.new of
|
|
|
|
false ->
|
|
|
|
case ejabberd_s2s:try_register(
|
|
|
|
{StateData#state.myname, Server}) of
|
|
|
|
{key, Key} ->
|
|
|
|
Key;
|
|
|
|
false ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
Key ->
|
|
|
|
Key
|
|
|
|
end,
|
|
|
|
case New of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
Key1 ->
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement,
|
|
|
|
"db:result",
|
|
|
|
[{"from", StateData#state.myname},
|
|
|
|
{"to", Server}],
|
|
|
|
[{xmlcdata, Key1}]})
|
|
|
|
end,
|
|
|
|
case StateData#state.verify of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
{Pid, Key2, SID} ->
|
|
|
|
send_element(StateData,
|
|
|
|
{xmlelement,
|
|
|
|
"db:verify",
|
|
|
|
[{"from", StateData#state.myname},
|
|
|
|
{"to", StateData#state.server},
|
|
|
|
{"id", SID}],
|
|
|
|
[{xmlcdata, Key2}]})
|
|
|
|
end,
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_validation, StateData#state{new = New}, ?FSMTIMEOUT*6}.
|
2005-10-25 03:08:37 +02:00
|
|
|
|
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]),
|
2006-03-14 05:26:15 +01:00
|
|
|
[{Server, ejabberd_config:get_local_option(outgoing_s2s_port)}];
|
2003-02-04 21:45:23 +01:00
|
|
|
{ok, HEnt} ->
|
|
|
|
?DEBUG("srv lookup of '~s': ~p~n",
|
|
|
|
[Server, HEnt#hostent.h_addr_list]),
|
|
|
|
case HEnt#hostent.h_addr_list of
|
|
|
|
[] ->
|
2006-03-14 05:26:15 +01:00
|
|
|
[{Server,
|
|
|
|
ejabberd_config:get_local_option(outgoing_s2s_port)}];
|
|
|
|
AddrList ->
|
|
|
|
% Probabilities are not exactly proportional to weights
|
|
|
|
% for simplicity (higher weigths are overvalued)
|
|
|
|
{A1, A2, A3} = now(),
|
|
|
|
random:seed(A1, A2, A3),
|
|
|
|
case (catch lists:map(
|
|
|
|
fun({Priority, Weight, Port, Host}) ->
|
|
|
|
N = case Weight of
|
|
|
|
0 -> 0;
|
|
|
|
_ -> (Weight + 1) * random:uniform()
|
|
|
|
end,
|
|
|
|
{Priority * 65536 - N, Host, Port}
|
|
|
|
end, AddrList)) of
|
|
|
|
{'EXIT', _Reasn} ->
|
|
|
|
[{Server,
|
|
|
|
ejabberd_config:get_local_option(outgoing_s2s_port)}];
|
|
|
|
SortedList ->
|
|
|
|
List = lists:map(
|
|
|
|
fun({_, Host, Port}) ->
|
|
|
|
{Host, Port}
|
|
|
|
end, lists:keysort(1, SortedList)),
|
|
|
|
?DEBUG("srv lookup of '~s': ~p~n", [Server, List]),
|
|
|
|
List
|
|
|
|
end
|
2003-02-04 21:45:23 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2006-03-14 05:26:15 +01:00
|
|
|
test_get_addr_port(Server) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(_, Acc) ->
|
|
|
|
[HostPort | _] = get_addr_port(Server),
|
|
|
|
case lists:keysearch(HostPort, 1, Acc) of
|
|
|
|
false ->
|
|
|
|
[{HostPort, 1} | Acc];
|
|
|
|
{value, {_, Num}} ->
|
|
|
|
lists:keyreplace(HostPort, 1, Acc, {HostPort, Num + 1})
|
|
|
|
end
|
|
|
|
end, [], lists:seq(1, 100000)).
|
2007-09-14 16:16:20 +02:00
|
|
|
|
2007-09-14 16:18:17 +02:00
|
|
|
|
2007-09-14 16:16:20 +02:00
|
|
|
%% Human readable S2S logging: Log only new outgoing connections as INFO
|
|
|
|
%% Do not log dialback
|
|
|
|
log_s2s_out(false, _, _) -> ok;
|
|
|
|
%% Log new outgoing connections:
|
|
|
|
log_s2s_out(_, Myname, Server) ->
|
|
|
|
?INFO_MSG("Trying to open s2s connection: ~s -> ~s",[Myname, Server]).
|
2007-09-14 16:18:32 +02:00
|
|
|
|
|
|
|
%% Calcultate timeout depending on which state we are in:
|
|
|
|
%% Can return integer > 0 | infinity
|
|
|
|
get_timeout_interval(StateName) ->
|
|
|
|
case StateName of
|
|
|
|
%% Validation implies dialback: Networking can take longer:
|
|
|
|
wait_for_validation ->
|
|
|
|
?FSMTIMEOUT*6;
|
|
|
|
%% When stream is established, we only rely on S2S Timeout timer:
|
|
|
|
stream_established ->
|
|
|
|
infinity;
|
|
|
|
_ ->
|
|
|
|
?FSMTIMEOUT
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% This function is intended to be called at the end of a state
|
|
|
|
%% function that want to wait for a reconnect delay before stopping.
|
|
|
|
wait_before_reconnect(StateData, Delay) ->
|
|
|
|
%% bounce queue manage by process and Erlang message queue
|
|
|
|
bounce_queue(StateData#state.queue, ?ERR_REMOTE_SERVER_NOT_FOUND),
|
|
|
|
bounce_messages(?ERR_REMOTE_SERVER_NOT_FOUND),
|
|
|
|
cancel_timer(StateData#state.timer),
|
|
|
|
Timer = erlang:start_timer(Delay, self(), []),
|
|
|
|
{next_state, wait_before_retry, StateData#state{timer=Timer,
|
|
|
|
queue = queue:new()}}.
|