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>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-19 12:59:40 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2002-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,
|
2008-04-18 14:19:45 +02:00
|
|
|
terminate_if_waiting_delay/2,
|
2007-07-18 18:26:50 +02:00
|
|
|
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,
|
2008-12-01 16:53:30 +01:00
|
|
|
test_get_addr_port/1,
|
|
|
|
get_addr_port/1]).
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2008-06-30 14:13:15 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
2008-06-26 17:48:19 +02:00
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
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,
|
2008-04-18 14:19:45 +02:00
|
|
|
delay_to_retry = undefined_delay,
|
2003-12-06 20:58:49 +01:00
|
|
|
new = false, verify = false,
|
|
|
|
timer}).
|
2002-12-07 21:27:26 +01:00
|
|
|
|
2008-06-26 17:48:19 +02: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],
|
2009-10-08 13:33:32 +02:00
|
|
|
fsm_limit_opts() ++ ?FSMOPTS)).
|
2007-09-14 16:16:36 +02:00
|
|
|
-else.
|
|
|
|
-define(SUPERVISOR_START, supervisor:start_child(ejabberd_s2s_out_sup,
|
2007-12-07 00:15:04 +01:00
|
|
|
[From, Host, Type])).
|
2007-09-14 16:16:36 +02:00
|
|
|
-endif.
|
|
|
|
|
2008-05-16 17:29:33 +02:00
|
|
|
-define(FSMTIMEOUT, 30000).
|
2007-07-24 11:04:09 +02:00
|
|
|
|
2009-01-19 12:59:40 +01:00
|
|
|
%% We do not block on send anymore.
|
|
|
|
-define(TCP_SEND_TIMEOUT, 15000).
|
|
|
|
|
2008-04-18 14:19:45 +02:00
|
|
|
%% Maximum delay to wait before retrying to connect after a failed attempt.
|
|
|
|
%% Specified in miliseconds. Default value is 5 minutes.
|
|
|
|
-define(MAX_RETRY_DELAY, 300000).
|
|
|
|
|
2008-06-26 17:48:19 +02:00
|
|
|
% These are the namespace already declared by the stream opening. This is
|
|
|
|
% used at serialization time.
|
2008-06-27 15:46:08 +02:00
|
|
|
-define(DEFAULT_NS, ?NS_JABBER_SERVER).
|
|
|
|
-define(PREFIXED_NS, [
|
|
|
|
{?NS_XMPP, ?NS_XMPP_pfx}, {?NS_DIALBACK, ?NS_DIALBACK_pfx}
|
|
|
|
]).
|
|
|
|
|
2002-12-06 21:59:19 +01:00
|
|
|
|
2009-01-19 12:16:44 +01:00
|
|
|
-define(SOCKET_DEFAULT_RESULT, {error, badarg}).
|
|
|
|
|
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],
|
2009-10-08 13:33:32 +02:00
|
|
|
fsm_limit_opts() ++ ?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-12-14 10:12:14 +01:00
|
|
|
timer = Timer}}.
|
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) ->
|
2007-12-07 00:15:04 +01:00
|
|
|
case Acc of
|
|
|
|
{ok, Socket} ->
|
|
|
|
{ok, Socket};
|
|
|
|
_ ->
|
|
|
|
open_socket1(Addr, Port)
|
|
|
|
end
|
2009-01-19 12:16:44 +01:00
|
|
|
end, ?SOCKET_DEFAULT_RESULT, AddrList) of
|
2006-03-14 05:26:15 +01:00
|
|
|
{ok, Socket} ->
|
|
|
|
Version = if
|
|
|
|
StateData#state.use_v10 ->
|
2008-06-26 17:48:19 +02:00
|
|
|
"1.0";
|
2006-03-14 05:26:15 +01:00
|
|
|
true ->
|
|
|
|
""
|
|
|
|
end,
|
|
|
|
NewStateData = StateData#state{socket = Socket,
|
|
|
|
tls_enabled = false,
|
|
|
|
streamid = new_id()},
|
2008-06-26 17:48:19 +02:00
|
|
|
Opening = exmpp_stream:opening(
|
|
|
|
StateData#state.server,
|
|
|
|
?NS_JABBER_SERVER,
|
|
|
|
Version),
|
|
|
|
send_element(NewStateData,
|
|
|
|
exmpp_stream:set_dialback_support(Opening)),
|
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]),
|
2008-04-18 14:19:45 +02:00
|
|
|
wait_before_reconnect(StateData)
|
2007-09-14 16:18:32 +02:00
|
|
|
%%{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-12-14 10:12:14 +01:00
|
|
|
{next_state, open_socket, StateData}.
|
2006-03-14 05:26:15 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
2009-01-19 12:16:44 +01:00
|
|
|
%% IPv4
|
|
|
|
open_socket1({_,_,_,_} = Addr, Port) ->
|
|
|
|
open_socket2(inet, Addr, Port);
|
|
|
|
|
|
|
|
%% IPv6
|
|
|
|
open_socket1({_,_,_,_,_,_,_,_} = Addr, Port) ->
|
|
|
|
open_socket2(inet6, Addr, Port);
|
|
|
|
|
|
|
|
%% Hostname
|
|
|
|
open_socket1(Host, Port) ->
|
|
|
|
lists:foldl(fun(_Family, {ok, _Socket} = R) ->
|
|
|
|
R;
|
|
|
|
(Family, _) ->
|
|
|
|
Addrs = get_addrs(Host, Family),
|
|
|
|
lists:foldl(fun(_Addr, {ok, _Socket} = R) ->
|
|
|
|
R;
|
|
|
|
(Addr, _) ->
|
|
|
|
open_socket1(Addr, Port)
|
|
|
|
end, ?SOCKET_DEFAULT_RESULT, Addrs)
|
|
|
|
end, ?SOCKET_DEFAULT_RESULT, outgoing_s2s_families()).
|
|
|
|
|
|
|
|
open_socket2(Type, Addr, Port) ->
|
|
|
|
?DEBUG("s2s_out: connecting to ~p:~p~n", [Addr, Port]),
|
|
|
|
Timeout = outgoing_s2s_timeout(),
|
2009-10-19 16:53:39 +02:00
|
|
|
SockOpts = case erlang:system_info(otp_release) >= "R13B" of
|
|
|
|
true -> [{send_timeout_close, true}];
|
|
|
|
false -> []
|
|
|
|
end,
|
2009-01-19 12:16:44 +01:00
|
|
|
case (catch ejabberd_socket:connect(Addr, Port,
|
|
|
|
[binary, {packet, 0},
|
2009-01-19 12:59:40 +01:00
|
|
|
{send_timeout, ?TCP_SEND_TIMEOUT},
|
2009-10-19 16:53:39 +02:00
|
|
|
{active, false}, Type | SockOpts],
|
2009-01-19 12:16:44 +01:00
|
|
|
Timeout)) of
|
|
|
|
{ok, _Socket} = R -> R;
|
|
|
|
{error, Reason} = R ->
|
|
|
|
?DEBUG("s2s_out: connect return ~p~n", [Reason]),
|
|
|
|
R;
|
2004-03-08 21:20:15 +01:00
|
|
|
{'EXIT', Reason} ->
|
2009-01-19 12:16:44 +01:00
|
|
|
?DEBUG("s2s_out: 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
|
|
|
|
2008-06-26 17:48:19 +02:00
|
|
|
wait_for_stream({xmlstreamstart, Opening}, StateData) ->
|
|
|
|
case {exmpp_stream:get_default_ns(Opening),
|
2008-06-27 15:46:08 +02:00
|
|
|
exmpp_xml:is_ns_declared_here(Opening, ?NS_DIALBACK),
|
2008-06-26 17:48:19 +02:00
|
|
|
exmpp_stream:get_version(Opening) == {1, 0}} of
|
|
|
|
{?NS_JABBER_SERVER, true, false} ->
|
2005-10-25 03:08:37 +02:00
|
|
|
send_db_request(StateData);
|
2008-06-26 17:48:19 +02:00
|
|
|
{?NS_JABBER_SERVER, true, true} when
|
2007-12-07 00:15:04 +01:00
|
|
|
StateData#state.use_v10 ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, wait_for_features, StateData, ?FSMTIMEOUT};
|
2008-06-26 17:48:19 +02:00
|
|
|
{?NS_JABBER_SERVER, false, 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};
|
2009-12-28 16:31:18 +01:00
|
|
|
{NSProvided, DB, _} ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('invalid-namespace')),
|
2009-12-28 16:31:18 +01:00
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (invalid namespace).~n"
|
|
|
|
"Namespace provided: ~p~nNamespace expected: \"jabber:server\"~n"
|
2009-12-28 19:47:14 +01:00
|
|
|
"xmlns:db provided: ~p~nFull packet: ~p",
|
|
|
|
[StateData#state.myname, StateData#state.server, NSProvided, DB, Opening]),
|
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) ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('xml-not-well-formed')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
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};
|
|
|
|
|
2008-05-18 14:28:58 +02:00
|
|
|
wait_for_stream({xmlstreamend,_Name}, StateData) ->
|
|
|
|
?INFO_MSG("Closing s2s connection: ~s -> ~s (xmlstreamend)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
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]),
|
2008-03-17 11:16:21 +01:00
|
|
|
ejabberd_hooks:run(s2s_connect_hook,
|
|
|
|
[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-12-08 17:39:02 +01:00
|
|
|
NextState = wait_for_validation,
|
2007-09-14 16:18:32 +02:00
|
|
|
%% TODO: Should'nt we close the connection here ?
|
2007-12-08 17:39:02 +01:00
|
|
|
{next_state, NextState, StateData,
|
|
|
|
get_timeout_interval(NextState)};
|
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-12-08 17:39:02 +01:00
|
|
|
NextState = wait_for_validation,
|
|
|
|
{next_state, NextState, StateData,
|
|
|
|
get_timeout_interval(NextState)}
|
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;
|
|
|
|
|
2007-12-07 00:15:04 +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]),
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('xml-not-well-formed')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
2003-05-12 20:36:13 +02:00
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2008-04-29 18:59:03 +02:00
|
|
|
wait_for_validation(timeout, #state{verify = {VPid, VKey, SID}} = StateData)
|
|
|
|
when is_pid(VPid) and is_list(VKey) and is_list(SID) ->
|
|
|
|
%% This is an auxiliary s2s connection for dialback.
|
|
|
|
%% This timeout is normal and doesn't represent a problem.
|
|
|
|
?DEBUG("wait_for_validation: ~s -> ~s (timeout in verify connection)",
|
|
|
|
[StateData#state.myname, StateData#state.server]),
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
2003-05-12 20:36:13 +02:00
|
|
|
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
|
2008-06-26 17:48:19 +02:00
|
|
|
#xmlel{ns = ?NS_XMPP, name = 'features'} = Features ->
|
2005-11-03 06:04:54 +01:00
|
|
|
{SASLEXT, StartTLS, StartTLSRequired} =
|
2005-10-25 03:08:37 +02:00
|
|
|
lists:foldl(
|
2008-06-26 17:48:19 +02:00
|
|
|
fun(#xmlel{ns = ?NS_SASL, name = 'mechanisms'},
|
2007-12-07 00:15:04 +01:00
|
|
|
{_SEXT, STLS, STLSReq} = Acc) ->
|
2008-06-26 17:48:19 +02:00
|
|
|
try
|
|
|
|
Mechs = exmpp_client_sasl:announced_mechanisms(
|
|
|
|
El),
|
|
|
|
NewSEXT = lists:member("EXTERNAL", Mechs),
|
|
|
|
{NewSEXT, STLS, STLSReq}
|
|
|
|
catch
|
|
|
|
_Exception ->
|
2005-11-03 06:04:54 +01:00
|
|
|
Acc
|
|
|
|
end;
|
2008-06-26 17:48:19 +02:00
|
|
|
(#xmlel{ns = ?NS_TLS, name ='starttls'},
|
2007-12-07 00:15:04 +01:00
|
|
|
{SEXT, _STLS, _STLSReq} = Acc) ->
|
2008-06-26 17:48:19 +02:00
|
|
|
try
|
|
|
|
Support = exmpp_client_tls:announced_support(
|
|
|
|
El),
|
|
|
|
case Support of
|
|
|
|
none -> Acc;
|
|
|
|
optional -> {SEXT, true, false};
|
|
|
|
required -> {SEXT, true, true}
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_Exception ->
|
2005-10-25 03:08:37 +02:00
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
2008-06-26 17:48:19 +02:00
|
|
|
end, {false, false, false}, Features#xmlel.children),
|
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]),
|
2008-03-17 11:16:21 +01:00
|
|
|
ejabberd_hooks:run(s2s_connect_hook,
|
|
|
|
[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,
|
2008-06-26 17:48:19 +02:00
|
|
|
exmpp_client_sasl:selected_mechanism("EXTERNAL",
|
|
|
|
StateData#state.myname)),
|
2005-11-03 06:04:54 +01:00
|
|
|
{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,
|
2008-06-26 17:48:19 +02:00
|
|
|
exmpp_client_tls:starttls()),
|
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,
|
2007-12-07 00:15:04 +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,
|
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,
|
2007-12-07 00:15:04 +01: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;
|
|
|
|
_ ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('bad-format')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
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-12-07 00:15:04 +01:00
|
|
|
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) ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('xml-not-well-formed')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
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
|
2008-06-26 17:48:19 +02:00
|
|
|
#xmlel{ns = ?NS_SASL, name = 'success'} ->
|
|
|
|
?DEBUG("auth: ~p", [{StateData#state.myname,
|
|
|
|
StateData#state.server}]),
|
|
|
|
ejabberd_socket:reset_stream(StateData#state.socket),
|
|
|
|
Opening = exmpp_stream:opening(
|
|
|
|
StateData#state.server,
|
|
|
|
?NS_JABBER_SERVER,
|
|
|
|
"1.0"),
|
|
|
|
send_element(StateData,
|
|
|
|
exmpp_stream:set_dialback_support(Opening)),
|
|
|
|
{next_state, wait_for_stream,
|
|
|
|
StateData#state{streamid = new_id(),
|
|
|
|
authenticated = true
|
|
|
|
}, ?FSMTIMEOUT};
|
|
|
|
#xmlel{ns = ?NS_SASL, name = 'failure'} ->
|
|
|
|
?DEBUG("restarted: ~p", [{StateData#state.myname,
|
|
|
|
StateData#state.server}]),
|
|
|
|
ejabberd_socket:close(StateData#state.socket),
|
|
|
|
{next_state, reopen_socket,
|
|
|
|
StateData#state{socket = undefined}, ?FSMTIMEOUT};
|
2005-11-03 06:04:54 +01:00
|
|
|
_ ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('bad-format')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
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;
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
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) ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('xml-not-well-formed')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
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
|
2008-06-26 17:48:19 +02:00
|
|
|
#xmlel{ns = ?NS_TLS, name = 'proceed'} ->
|
|
|
|
?DEBUG("starttls: ~p", [{StateData#state.myname,
|
|
|
|
StateData#state.server}]),
|
|
|
|
Socket = StateData#state.socket,
|
|
|
|
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,
|
|
|
|
TLSSocket = ejabberd_socket:starttls(Socket, TLSOpts),
|
|
|
|
NewStateData = StateData#state{socket = TLSSocket,
|
|
|
|
streamid = new_id(),
|
|
|
|
tls_enabled = true
|
|
|
|
},
|
|
|
|
Opening = exmpp_stream:opening(
|
|
|
|
StateData#state.server,
|
|
|
|
?NS_JABBER_SERVER,
|
|
|
|
"1.0"),
|
|
|
|
send_element(NewStateData,
|
|
|
|
exmpp_stream:set_dialback_support(Opening)),
|
|
|
|
{next_state, wait_for_stream, NewStateData, ?FSMTIMEOUT};
|
2005-10-25 03:08:37 +02:00
|
|
|
_ ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('bad-format')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
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-12-07 00:15:04 +01:00
|
|
|
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) ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('xml-not-well-formed')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
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}.
|
|
|
|
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
reopen_socket({xmlstreamelement, _El}, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{next_state, reopen_socket, StateData, ?FSMTIMEOUT};
|
2007-12-07 00:15:04 +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
|
2007-12-07 00:15:04 +01:00
|
|
|
wait_before_retry(_Event, StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
{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
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
stream_established({xmlstreamend, _Name}, StateData) ->
|
2008-05-18 14:28:58 +02:00
|
|
|
?INFO_MSG("Connection closed in stream established: ~s -> ~s (xmlstreamend)",
|
2007-09-14 16:16:20 +02:00
|
|
|
[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) ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_stream:error('xml-not-well-formed')),
|
|
|
|
send_element(StateData, exmpp_stream:closing()),
|
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
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +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
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +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
|
2009-05-19 11:39:13 +02:00
|
|
|
%% Returns: The associated StateData for this connection
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData}
|
|
|
|
%% Reply = {state_infos, [{InfoName::atom(), InfoValue::any()]
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_sync_event(get_state_infos, _From, StateName, StateData) ->
|
|
|
|
{Addr,Port} = try ejabberd_socket:peername(StateData#state.socket) of
|
|
|
|
{ok, {A,P}} -> {A,P};
|
|
|
|
{error, _} -> {unknown,unknown}
|
|
|
|
catch
|
|
|
|
_:_ ->
|
|
|
|
{unknown,unknown}
|
|
|
|
end,
|
|
|
|
Infos = [
|
|
|
|
{direction, out},
|
|
|
|
{statename, StateName},
|
|
|
|
{addr, Addr},
|
|
|
|
{port, Port},
|
|
|
|
{streamid, StateData#state.streamid},
|
|
|
|
{use_v10, StateData#state.use_v10},
|
|
|
|
{tls, StateData#state.tls},
|
|
|
|
{tls_required, StateData#state.tls_required},
|
|
|
|
{tls_enabled, StateData#state.tls_enabled},
|
|
|
|
{tls_options, StateData#state.tls_options},
|
|
|
|
{authenticated, StateData#state.authenticated},
|
|
|
|
{db_enabled, StateData#state.db_enabled},
|
|
|
|
{try_auth, StateData#state.try_auth},
|
|
|
|
{myname, StateData#state.myname},
|
|
|
|
{server, StateData#state.server},
|
|
|
|
{delay_to_retry, StateData#state.delay_to_retry},
|
|
|
|
{verify, StateData#state.verify}
|
|
|
|
],
|
|
|
|
Reply = {state_infos, Infos},
|
|
|
|
{reply,Reply,StateName,StateData};
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
2002-12-06 21:59:19 +01:00
|
|
|
%% 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
|
|
|
%%----------------------------------------------------------------------
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName, StateData) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
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
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
{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
|
|
|
|
2008-08-26 15:38:49 +02:00
|
|
|
handle_info({send_element, El}, StateName, StateData) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
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 ->
|
2008-06-26 17:48:19 +02:00
|
|
|
bounce_element(El, 'remote-server-not-found'),
|
2007-09-14 16:18:32 +02:00
|
|
|
{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};
|
|
|
|
|
2007-12-07 00:15:04 +01:00
|
|
|
handle_info({timeout, Timer, _}, _StateName,
|
2003-12-06 20:58:49 +01:00
|
|
|
#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};
|
|
|
|
|
2008-04-18 14:19:45 +02:00
|
|
|
handle_info(terminate_if_waiting_before_retry, wait_before_retry, StateData) ->
|
|
|
|
{stop, normal, StateData};
|
|
|
|
|
|
|
|
handle_info(terminate_if_waiting_before_retry, StateName, StateData) ->
|
|
|
|
{next_state, StateName, StateData, get_timeout_interval(StateName)};
|
|
|
|
|
2003-12-06 20:58:49 +01:00
|
|
|
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
|
2008-06-26 17:48:19 +02:00
|
|
|
bounce_queue(StateData#state.queue, 'remote-server-not-found'),
|
|
|
|
bounce_messages('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
|
|
|
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, #xmlel{ns = ?NS_XMPP, name = 'stream'} = El) ->
|
2009-03-02 16:48:01 +01:00
|
|
|
send_text(StateData, exmpp_stream:to_iolist(El));
|
2005-10-25 03:08:37 +02:00
|
|
|
send_element(StateData, El) ->
|
2009-03-02 16:48:01 +01:00
|
|
|
send_text(StateData, exmpp_stanza:to_iolist(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);
|
2007-12-07 00:15:04 +01:00
|
|
|
{empty, _Q1} ->
|
2002-12-07 21:27:26 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2008-06-26 17:48:19 +02:00
|
|
|
%% Bounce a single message (xmlel)
|
|
|
|
bounce_element(El, Condition) ->
|
|
|
|
case exmpp_stanza:get_type(El) of
|
2009-01-09 20:18:46 +01:00
|
|
|
<<"error">> -> ok;
|
|
|
|
<<"result">> -> ok;
|
2007-11-25 17:14:32 +01:00
|
|
|
_ ->
|
2008-07-01 10:01:06 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(El, Condition),
|
2009-06-01 18:35:55 +02:00
|
|
|
From = exmpp_jid:parse(exmpp_stanza:get_sender(El)),
|
|
|
|
To = exmpp_jid:parse(exmpp_stanza:get_recipient(El)),
|
2008-06-30 18:55:03 +02:00
|
|
|
% No namespace conversion (:server <-> :client) is done.
|
2008-06-26 17:48:19 +02:00
|
|
|
% This is handled by C2S and S2S send_element functions.
|
2008-06-30 18:55:03 +02:00
|
|
|
ejabberd_router:route(To, From, Err)
|
2007-11-25 17:14:32 +01:00
|
|
|
end.
|
2007-09-14 16:18:32 +02:00
|
|
|
|
2008-06-26 17:48:19 +02:00
|
|
|
bounce_queue(Q, Condition) ->
|
2003-12-06 20:58:49 +01:00
|
|
|
case queue:out(Q) of
|
|
|
|
{{value, El}, Q1} ->
|
2008-06-26 17:48:19 +02:00
|
|
|
bounce_element(El, Condition),
|
|
|
|
bounce_queue(Q1, Condition);
|
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.
|
|
|
|
|
2008-06-26 17:48:19 +02:00
|
|
|
bounce_messages(Condition) ->
|
2002-12-07 21:27:26 +01:00
|
|
|
receive
|
2008-08-26 15:38:49 +02:00
|
|
|
{send_element, El} ->
|
2008-06-26 17:48:19 +02:00
|
|
|
bounce_element(El, Condition),
|
|
|
|
bounce_messages(Condition)
|
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 ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_dialback:key(
|
|
|
|
StateData#state.myname, Server, Key1))
|
2005-10-25 03:08:37 +02:00
|
|
|
end,
|
|
|
|
case StateData#state.verify of
|
|
|
|
false ->
|
|
|
|
ok;
|
2007-12-07 00:15:04 +01:00
|
|
|
{_Pid, Key2, SID} ->
|
2008-06-26 17:48:19 +02:00
|
|
|
send_element(StateData, exmpp_dialback:verify_request(
|
|
|
|
StateData#state.myname, StateData#state.server, SID, Key2))
|
2005-10-25 03:08:37 +02:00
|
|
|
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
|
|
|
|
2008-06-27 15:46:08 +02:00
|
|
|
is_verify_res(#xmlel{ns = ?NS_DIALBACK, name = 'result',
|
2008-06-26 17:48:19 +02:00
|
|
|
attrs = Attrs}) ->
|
2002-12-06 21:59:19 +01:00
|
|
|
{result,
|
2008-06-26 17:48:19 +02:00
|
|
|
exmpp_stanza:get_recipient_from_attrs(Attrs),
|
|
|
|
exmpp_stanza:get_sender_from_attrs(Attrs),
|
|
|
|
exmpp_stanza:get_id_from_attrs(Attrs),
|
2009-01-09 20:18:46 +01:00
|
|
|
binary_to_list(exmpp_stanza:get_type_from_attrs(Attrs))};
|
2008-06-27 15:46:08 +02:00
|
|
|
is_verify_res(#xmlel{ns = ?NS_DIALBACK, name = 'verify',
|
2008-06-26 17:48:19 +02:00
|
|
|
attrs = Attrs}) ->
|
2002-12-13 21:58:27 +01:00
|
|
|
{verify,
|
2008-06-26 17:48:19 +02:00
|
|
|
exmpp_stanza:get_recipient_from_attrs(Attrs),
|
|
|
|
exmpp_stanza:get_sender_from_attrs(Attrs),
|
|
|
|
exmpp_stanza:get_id_from_attrs(Attrs),
|
2009-01-09 20:18:46 +01:00
|
|
|
binary_to_list(exmpp_stanza:get_type_from_attrs(Attrs))};
|
2002-12-06 21:59:19 +01:00
|
|
|
is_verify_res(_) ->
|
|
|
|
false.
|
|
|
|
|
2003-02-04 21:45:23 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2007-12-07 00:15:04 +01:00
|
|
|
%% SRV support
|
2003-02-04 21:45:23 +01:00
|
|
|
|
|
|
|
-include_lib("kernel/include/inet.hrl").
|
|
|
|
|
|
|
|
get_addr_port(Server) ->
|
2009-07-30 14:35:18 +02:00
|
|
|
Res = srv_lookup(Server),
|
2003-10-12 20:21:16 +02:00
|
|
|
case Res of
|
2003-02-04 21:45:23 +01:00
|
|
|
{error, Reason} ->
|
|
|
|
?DEBUG("srv lookup of '~s' failed: ~p~n", [Server, Reason]),
|
2007-12-29 15:22:38 +01:00
|
|
|
[{Server, 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
|
|
|
|
[] ->
|
2007-12-29 15:22:38 +01:00
|
|
|
[{Server, outgoing_s2s_port()}];
|
2006-03-14 05:26:15 +01:00
|
|
|
AddrList ->
|
2007-12-07 00:15:04 +01:00
|
|
|
%% Probabilities are not exactly proportional to weights
|
|
|
|
%% for simplicity (higher weigths are overvalued)
|
2006-03-14 05:26:15 +01:00
|
|
|
{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
|
2008-05-18 14:28:58 +02:00
|
|
|
{'EXIT', _Reason} ->
|
2007-12-29 15:22:38 +01:00
|
|
|
[{Server, outgoing_s2s_port()}];
|
2006-03-14 05:26:15 +01:00
|
|
|
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.
|
|
|
|
|
2009-07-30 14:35:18 +02:00
|
|
|
srv_lookup(Server) ->
|
|
|
|
Options = case ejabberd_config:get_local_option(s2s_dns_options) of
|
|
|
|
L when is_list(L) -> L;
|
|
|
|
_ -> []
|
|
|
|
end,
|
2009-08-03 17:04:52 +02:00
|
|
|
TimeoutMs = timer:seconds(proplists:get_value(timeout, Options, 10)),
|
2009-07-30 14:35:18 +02:00
|
|
|
Retries = proplists:get_value(retries, Options, 2),
|
2009-08-03 17:04:52 +02:00
|
|
|
srv_lookup(Server, TimeoutMs, Retries).
|
2009-07-30 14:35:18 +02:00
|
|
|
|
|
|
|
%% XXX - this behaviour is suboptimal in the case that the domain
|
|
|
|
%% has a "_xmpp-server._tcp." but not a "_jabber._tcp." record and
|
|
|
|
%% we don't get a DNS reply for the "_xmpp-server._tcp." lookup. In this
|
|
|
|
%% case we'll give up when we get the "_jabber._tcp." nxdomain reply.
|
|
|
|
srv_lookup(_Server, _Timeout, Retries) when Retries < 1 ->
|
|
|
|
{error, timeout};
|
|
|
|
srv_lookup(Server, Timeout, Retries) ->
|
|
|
|
case inet_res:getbyname("_xmpp-server._tcp." ++ Server, srv, Timeout) of
|
|
|
|
{error, _Reason} ->
|
|
|
|
case inet_res:getbyname("_jabber._tcp." ++ Server, srv, Timeout) of
|
|
|
|
{error, timeout} ->
|
2009-09-07 16:31:34 +02:00
|
|
|
?ERROR_MSG("The DNS servers~n ~p~ntimed out on request"
|
|
|
|
" for ~p IN SRV."
|
|
|
|
" You should check your DNS configuration.",
|
|
|
|
[inet_db:res_option(nameserver), Server]),
|
2009-07-30 14:35:18 +02:00
|
|
|
srv_lookup(Server, Timeout, Retries - 1);
|
|
|
|
R -> R
|
|
|
|
end;
|
|
|
|
{ok, _HEnt} = R -> R
|
|
|
|
end.
|
|
|
|
|
2006-03-14 05:26:15 +01:00
|
|
|
test_get_addr_port(Server) ->
|
|
|
|
lists:foldl(
|
2007-12-07 00:15:04 +01:00
|
|
|
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
|
|
|
|
2009-01-19 12:16:44 +01:00
|
|
|
get_addrs(Host, Family) ->
|
|
|
|
Type = case Family of
|
|
|
|
inet4 -> inet;
|
|
|
|
ipv4 -> inet;
|
|
|
|
inet6 -> inet6;
|
|
|
|
ipv6 -> inet6
|
|
|
|
end,
|
|
|
|
case inet:gethostbyname(Host, Type) of
|
|
|
|
{ok, #hostent{h_addr_list = Addrs}} ->
|
|
|
|
?DEBUG("~s of ~s resolved to: ~p~n", [Type, Host, Addrs]),
|
|
|
|
Addrs;
|
|
|
|
{error, Reason} ->
|
|
|
|
?DEBUG("~s lookup of '~s' failed: ~p~n", [Type, Host, Reason]),
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2007-12-29 15:22:38 +01:00
|
|
|
outgoing_s2s_port() ->
|
|
|
|
case ejabberd_config:get_local_option(outgoing_s2s_port) of
|
|
|
|
Port when is_integer(Port) ->
|
|
|
|
Port;
|
|
|
|
undefined ->
|
|
|
|
5269
|
|
|
|
end.
|
2007-09-14 16:18:17 +02:00
|
|
|
|
2009-01-19 12:16:44 +01:00
|
|
|
outgoing_s2s_families() ->
|
|
|
|
case ejabberd_config:get_local_option(outgoing_s2s_options) of
|
|
|
|
{Families, _} when is_list(Families) ->
|
|
|
|
Families;
|
|
|
|
undefined ->
|
|
|
|
%% DISCUSSION: Why prefer IPv4 first?
|
|
|
|
%%
|
|
|
|
%% IPv4 connectivity will be available for everyone for
|
|
|
|
%% many years to come. So, there's absolutely no benefit
|
|
|
|
%% in preferring IPv6 connections which are flaky at best
|
|
|
|
%% nowadays.
|
|
|
|
%%
|
|
|
|
%% On the other hand content providers hesitate putting up
|
|
|
|
%% AAAA records for their sites due to the mentioned
|
|
|
|
%% quality of current IPv6 connectivity. Making IPv6 the a
|
|
|
|
%% `fallback' may avoid these problems elegantly.
|
|
|
|
[ipv4, ipv6]
|
|
|
|
end.
|
|
|
|
|
|
|
|
outgoing_s2s_timeout() ->
|
|
|
|
case ejabberd_config:get_local_option(outgoing_s2s_options) of
|
|
|
|
{_, Timeout} when is_integer(Timeout) ->
|
|
|
|
Timeout;
|
|
|
|
{_, infinity} ->
|
|
|
|
infinity;
|
|
|
|
undefined ->
|
|
|
|
%% 10 seconds
|
|
|
|
10000
|
|
|
|
end.
|
|
|
|
|
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
|
|
|
|
2008-04-18 14:19:45 +02:00
|
|
|
%% Calculate timeout depending on which state we are in:
|
2007-09-14 16:18:32 +02:00
|
|
|
%% 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.
|
2008-04-18 14:19:45 +02:00
|
|
|
wait_before_reconnect(StateData) ->
|
2007-09-14 16:18:32 +02:00
|
|
|
%% bounce queue manage by process and Erlang message queue
|
2008-06-26 17:48:19 +02:00
|
|
|
bounce_queue(StateData#state.queue, 'remote-server-not-found'),
|
|
|
|
bounce_messages('remote-server-not-found'),
|
2007-09-14 16:18:32 +02:00
|
|
|
cancel_timer(StateData#state.timer),
|
2008-04-18 14:19:45 +02:00
|
|
|
Delay = case StateData#state.delay_to_retry of
|
|
|
|
undefined_delay ->
|
|
|
|
%% The initial delay is random between 1 and 15 seconds
|
|
|
|
%% Return a random integer between 1000 and 15000
|
|
|
|
{_, _, MicroSecs} = now(),
|
|
|
|
(MicroSecs rem 14000) + 1000;
|
|
|
|
D1 ->
|
|
|
|
%% Duplicate the delay with each successive failed
|
|
|
|
%% reconnection attempt, but don't exceed the max
|
|
|
|
lists:min([D1 * 2, get_max_retry_delay()])
|
|
|
|
end,
|
2007-09-14 16:18:32 +02:00
|
|
|
Timer = erlang:start_timer(Delay, self(), []),
|
|
|
|
{next_state, wait_before_retry, StateData#state{timer=Timer,
|
2008-04-18 14:19:45 +02:00
|
|
|
delay_to_retry = Delay,
|
2007-09-14 16:18:32 +02:00
|
|
|
queue = queue:new()}}.
|
2008-04-18 14:19:45 +02:00
|
|
|
|
|
|
|
%% @doc Get the maximum allowed delay for retry to reconnect (in miliseconds).
|
|
|
|
%% The default value is 5 minutes.
|
|
|
|
%% The option {s2s_max_retry_delay, Seconds} can be used (in seconds).
|
|
|
|
%% @spec () -> integer()
|
|
|
|
get_max_retry_delay() ->
|
|
|
|
case ejabberd_config:get_local_option(s2s_max_retry_delay) of
|
|
|
|
Seconds when is_integer(Seconds) ->
|
|
|
|
Seconds*1000;
|
|
|
|
_ ->
|
|
|
|
?MAX_RETRY_DELAY
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% Terminate s2s_out connections that are in state wait_before_retry
|
|
|
|
terminate_if_waiting_delay(From, To) ->
|
|
|
|
FromTo = {From, To},
|
|
|
|
Pids = ejabberd_s2s:get_connections_pids(FromTo),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Pid) ->
|
|
|
|
Pid ! terminate_if_waiting_before_retry
|
|
|
|
end,
|
|
|
|
Pids).
|
2009-10-08 13:33:32 +02:00
|
|
|
|
|
|
|
fsm_limit_opts() ->
|
|
|
|
case ejabberd_config:get_local_option(max_fsm_queue) of
|
|
|
|
N when is_integer(N) ->
|
|
|
|
[{max_queue, N}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|