2014-04-30 17:20:38 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-01-03 15:58:52 +01:00
|
|
|
%%% File : mod_sip_proxy.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Purpose :
|
2014-04-30 17:20:38 +02:00
|
|
|
%%% Created : 21 Apr 2014 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2015-01-21 14:52:37 +01:00
|
|
|
%%%
|
2015-10-07 00:06:58 +02:00
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2014-2019 ProcessOne
|
2015-01-21 14:52:37 +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.
|
|
|
|
%%%
|
|
|
|
%%% 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.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2017-11-10 17:51:22 +01:00
|
|
|
%%%
|
2014-04-30 17:20:38 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-11-10 17:51:22 +01:00
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
-module(mod_sip_proxy).
|
|
|
|
|
2017-05-23 12:12:48 +02:00
|
|
|
-ifndef(SIP).
|
|
|
|
-export([]).
|
|
|
|
-else.
|
2017-08-05 19:58:21 +02:00
|
|
|
-behaviour(p1_fsm).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
%% API
|
2014-05-23 18:13:31 +02:00
|
|
|
-export([start/2, start_link/2, route/3, route/4]).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([init/1, wait_for_request/2,
|
|
|
|
wait_for_response/2, handle_event/3,
|
|
|
|
handle_sync_event/4, handle_info/3, terminate/3,
|
2017-04-28 12:23:32 +02:00
|
|
|
code_change/4]).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
-include("logger.hrl").
|
2014-07-05 12:01:29 +02:00
|
|
|
-include_lib("esip/include/esip.hrl").
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2014-06-01 11:34:51 +02:00
|
|
|
-define(SIGN_LIFETIME, 300). %% in seconds.
|
|
|
|
|
2014-05-02 13:18:57 +02:00
|
|
|
-record(state, {host = <<"">> :: binary(),
|
|
|
|
opts = [] :: [{certfile, binary()}],
|
2014-04-30 17:20:38 +02:00
|
|
|
orig_trid,
|
2014-05-02 13:18:57 +02:00
|
|
|
responses = [] :: [#sip{}],
|
|
|
|
tr_ids = [] :: list(),
|
2017-02-18 07:36:27 +01:00
|
|
|
orig_req = #sip{} :: #sip{}}).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
start(LServer, Opts) ->
|
|
|
|
supervisor:start_child(mod_sip_proxy_sup, [LServer, Opts]).
|
|
|
|
|
|
|
|
start_link(LServer, Opts) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:start_link(?MODULE, [LServer, Opts], []).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
route(SIPMsg, _SIPSock, TrID, Pid) ->
|
2017-08-05 19:58:21 +02:00
|
|
|
p1_fsm:send_event(Pid, {SIPMsg, TrID}).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2014-06-01 11:34:51 +02:00
|
|
|
route(#sip{hdrs = Hdrs} = Req, LServer, Opts) ->
|
|
|
|
case proplists:get_bool(authenticated, Opts) of
|
|
|
|
true ->
|
|
|
|
route_statelessly(Req, LServer, Opts);
|
|
|
|
false ->
|
2014-06-02 08:16:08 +02:00
|
|
|
ConfiguredRRoute = get_configured_record_route(LServer),
|
2014-06-01 11:34:51 +02:00
|
|
|
case esip:get_hdrs('route', Hdrs) of
|
|
|
|
[{_, URI, _}|_] ->
|
2014-06-02 08:16:08 +02:00
|
|
|
case cmp_uri(URI, ConfiguredRRoute) of
|
2014-06-01 11:34:51 +02:00
|
|
|
true ->
|
|
|
|
case is_signed_by_me(URI#uri.user, Hdrs) of
|
|
|
|
true ->
|
|
|
|
route_statelessly(Req, LServer, Opts);
|
|
|
|
false ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
error
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
route_statelessly(Req, LServer, Opts) ->
|
2014-05-23 18:13:31 +02:00
|
|
|
Req1 = prepare_request(LServer, Req),
|
|
|
|
case connect(Req1, add_certfile(LServer, Opts)) of
|
2014-05-30 21:49:34 +02:00
|
|
|
{ok, SIPSocketsWithURIs} ->
|
2014-05-23 18:13:31 +02:00
|
|
|
lists:foreach(
|
2014-05-31 05:49:51 +02:00
|
|
|
fun({SIPSocket, _URI}) ->
|
2014-05-23 18:13:31 +02:00
|
|
|
Req2 = add_via(SIPSocket, LServer, Req1),
|
2014-05-31 05:49:51 +02:00
|
|
|
esip:send(SIPSocket, Req2)
|
2014-05-30 21:49:34 +02:00
|
|
|
end, SIPSocketsWithURIs);
|
2014-05-23 18:13:31 +02:00
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_fsm callbacks
|
|
|
|
%%%===================================================================
|
|
|
|
init([Host, Opts]) ->
|
2014-05-02 08:51:08 +02:00
|
|
|
Opts1 = add_certfile(Host, Opts),
|
|
|
|
{ok, wait_for_request, #state{opts = Opts1, host = Host}}.
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
wait_for_request({#sip{type = request} = Req, TrID}, State) ->
|
2014-05-01 11:27:35 +02:00
|
|
|
Opts = State#state.opts,
|
2014-05-23 18:13:31 +02:00
|
|
|
Req1 = prepare_request(State#state.host, Req),
|
2014-04-30 17:20:38 +02:00
|
|
|
case connect(Req1, Opts) of
|
2014-05-30 21:11:46 +02:00
|
|
|
{ok, SIPSocketsWithURIs} ->
|
2014-05-02 13:18:57 +02:00
|
|
|
NewState =
|
|
|
|
lists:foldl(
|
2014-05-30 21:11:46 +02:00
|
|
|
fun(_SIPSocketWithURI, {error, _} = Err) ->
|
2014-05-02 13:18:57 +02:00
|
|
|
Err;
|
2014-05-30 21:11:46 +02:00
|
|
|
({SIPSocket, URI}, #state{tr_ids = TrIDs} = AccState) ->
|
2014-05-31 08:00:27 +02:00
|
|
|
Req2 = add_record_route_and_set_uri(
|
|
|
|
URI, State#state.host, Req1),
|
2014-05-23 18:13:31 +02:00
|
|
|
Req3 = add_via(SIPSocket, State#state.host, Req2),
|
2014-05-31 08:00:27 +02:00
|
|
|
case esip:request(SIPSocket, Req3,
|
2014-05-02 13:18:57 +02:00
|
|
|
{?MODULE, route, [self()]}) of
|
|
|
|
{ok, ClientTrID} ->
|
|
|
|
NewTrIDs = [ClientTrID|TrIDs],
|
|
|
|
AccState#state{tr_ids = NewTrIDs};
|
|
|
|
Err ->
|
|
|
|
cancel_pending_transactions(AccState),
|
|
|
|
Err
|
|
|
|
end
|
2014-05-30 21:11:46 +02:00
|
|
|
end, State, SIPSocketsWithURIs),
|
2014-05-02 13:18:57 +02:00
|
|
|
case NewState of
|
|
|
|
{error, _} = Err ->
|
2014-04-30 17:20:38 +02:00
|
|
|
{Status, Reason} = esip:error_status(Err),
|
|
|
|
esip:reply(TrID, mod_sip:make_response(
|
|
|
|
Req, #sip{type = response,
|
|
|
|
status = Status,
|
|
|
|
reason = Reason})),
|
2014-05-02 13:18:57 +02:00
|
|
|
{stop, normal, State};
|
|
|
|
_ ->
|
|
|
|
{next_state, wait_for_response,
|
|
|
|
NewState#state{orig_req = Req, orig_trid = TrID}}
|
2014-04-30 17:20:38 +02:00
|
|
|
end;
|
2014-05-01 11:27:35 +02:00
|
|
|
{error, notfound} ->
|
|
|
|
esip:reply(TrID, mod_sip:make_response(
|
|
|
|
Req, #sip{type = response,
|
|
|
|
status = 480,
|
|
|
|
reason = esip:reason(480)})),
|
|
|
|
{stop, normal, State};
|
2014-04-30 17:20:38 +02:00
|
|
|
Err ->
|
|
|
|
{Status, Reason} = esip:error_status(Err),
|
|
|
|
esip:reply(TrID, mod_sip:make_response(
|
|
|
|
Req, #sip{type = response,
|
|
|
|
status = Status,
|
|
|
|
reason = Reason})),
|
|
|
|
{stop, normal, State}
|
|
|
|
end;
|
|
|
|
wait_for_request(_Event, State) ->
|
|
|
|
{next_state, wait_for_request, State}.
|
|
|
|
|
|
|
|
wait_for_response({#sip{method = <<"CANCEL">>, type = request}, _TrID}, State) ->
|
2014-05-02 13:18:57 +02:00
|
|
|
cancel_pending_transactions(State),
|
2014-04-30 17:20:38 +02:00
|
|
|
{next_state, wait_for_response, State};
|
2014-05-02 13:18:57 +02:00
|
|
|
wait_for_response({Resp, TrID},
|
|
|
|
#state{orig_req = #sip{method = Method} = Req} = State) ->
|
2014-04-30 17:20:38 +02:00
|
|
|
case Resp of
|
2014-05-02 13:18:57 +02:00
|
|
|
{error, timeout} when Method /= <<"INVITE">> ->
|
|
|
|
%% Absorb useless 408. See RFC4320
|
|
|
|
choose_best_response(State),
|
|
|
|
esip:stop_transaction(State#state.orig_trid),
|
|
|
|
{stop, normal, State};
|
|
|
|
{error, _} ->
|
|
|
|
{Status, Reason} = esip:error_status(Resp),
|
|
|
|
State1 = mark_transaction_as_complete(TrID, State),
|
|
|
|
SIPResp = mod_sip:make_response(Req,
|
|
|
|
#sip{type = response,
|
|
|
|
status = Status,
|
|
|
|
reason = Reason}),
|
|
|
|
State2 = collect_response(SIPResp, State1),
|
|
|
|
case State2#state.tr_ids of
|
|
|
|
[] ->
|
|
|
|
choose_best_response(State2),
|
|
|
|
{stop, normal, State2};
|
|
|
|
_ ->
|
|
|
|
{next_state, wait_for_response, State2}
|
|
|
|
end;
|
2014-04-30 17:20:38 +02:00
|
|
|
#sip{status = 100} ->
|
|
|
|
{next_state, wait_for_response, State};
|
|
|
|
#sip{status = Status} ->
|
2014-05-02 13:18:57 +02:00
|
|
|
{[_|Vias], NewHdrs} = esip:split_hdrs('via', Resp#sip.hdrs),
|
|
|
|
NewResp = case Vias of
|
|
|
|
[] ->
|
|
|
|
Resp#sip{hdrs = NewHdrs};
|
|
|
|
_ ->
|
|
|
|
Resp#sip{hdrs = [{'via', Vias}|NewHdrs]}
|
|
|
|
end,
|
|
|
|
if Status < 300 ->
|
|
|
|
esip:reply(State#state.orig_trid, NewResp);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
State1 = if Status >= 200 ->
|
|
|
|
mark_transaction_as_complete(TrID, State);
|
|
|
|
true ->
|
|
|
|
State
|
|
|
|
end,
|
|
|
|
State2 = if Status >= 300 ->
|
|
|
|
collect_response(NewResp, State1);
|
|
|
|
true ->
|
|
|
|
State1
|
|
|
|
end,
|
|
|
|
if Status >= 600 ->
|
|
|
|
cancel_pending_transactions(State2);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case State2#state.tr_ids of
|
|
|
|
[] ->
|
|
|
|
choose_best_response(State2),
|
|
|
|
{stop, normal, State2};
|
|
|
|
_ ->
|
|
|
|
{next_state, wait_for_response, State2}
|
|
|
|
end
|
2014-04-30 17:20:38 +02:00
|
|
|
end;
|
|
|
|
wait_for_response(_Event, State) ->
|
|
|
|
{next_state, wait_for_response, State}.
|
|
|
|
|
|
|
|
handle_event(_Event, StateName, State) ->
|
|
|
|
{next_state, StateName, State}.
|
|
|
|
|
|
|
|
handle_sync_event(_Event, _From, StateName, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, StateName, State}.
|
|
|
|
|
|
|
|
handle_info(_Info, StateName, State) ->
|
|
|
|
{next_state, StateName, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _StateName, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, StateName, State, _Extra) ->
|
|
|
|
{ok, StateName, State}.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2014-05-01 11:27:35 +02:00
|
|
|
connect(#sip{hdrs = Hdrs} = Req, Opts) ->
|
|
|
|
{_, ToURI, _} = esip:get_hdr('to', Hdrs),
|
|
|
|
case mod_sip:at_my_host(ToURI) of
|
|
|
|
true ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(ToURI#uri.user),
|
|
|
|
LServer = jid:nameprep(ToURI#uri.host),
|
2014-05-01 13:51:58 +02:00
|
|
|
case mod_sip_registrar:find_sockets(LUser, LServer) of
|
2014-05-02 13:18:57 +02:00
|
|
|
[_|_] = SIPSocks ->
|
|
|
|
{ok, SIPSocks};
|
2014-05-01 13:51:58 +02:00
|
|
|
[] ->
|
2014-05-01 11:27:35 +02:00
|
|
|
{error, notfound}
|
|
|
|
end;
|
|
|
|
false ->
|
2014-05-02 13:18:57 +02:00
|
|
|
case esip:connect(Req, Opts) of
|
|
|
|
{ok, SIPSock} ->
|
2014-05-30 21:11:46 +02:00
|
|
|
{ok, [{SIPSock, Req#sip.uri}]};
|
2014-05-02 13:18:57 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end
|
2014-04-30 17:20:38 +02:00
|
|
|
end.
|
2014-05-02 08:51:08 +02:00
|
|
|
|
2014-05-02 13:18:57 +02:00
|
|
|
cancel_pending_transactions(State) ->
|
|
|
|
lists:foreach(fun esip:cancel/1, State#state.tr_ids).
|
|
|
|
|
2014-05-02 08:51:08 +02:00
|
|
|
add_certfile(LServer, Opts) ->
|
2017-11-23 09:09:40 +01:00
|
|
|
case ejabberd_pkix:get_certfile(LServer) of
|
|
|
|
{ok, CertFile} ->
|
2014-05-02 08:51:08 +02:00
|
|
|
[{certfile, CertFile}|Opts];
|
2017-11-23 09:09:40 +01:00
|
|
|
error ->
|
|
|
|
case ejabberd_config:get_option({domain_certfile, LServer}) of
|
|
|
|
CertFile when is_binary(CertFile) ->
|
|
|
|
[{certfile, CertFile}|Opts];
|
|
|
|
_ ->
|
|
|
|
Opts
|
|
|
|
end
|
2014-05-02 08:51:08 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
add_via(#sip_socket{type = Transport}, LServer, #sip{hdrs = Hdrs} = Req) ->
|
|
|
|
ConfiguredVias = get_configured_vias(LServer),
|
|
|
|
{ViaHost, ViaPort} = proplists:get_value(
|
|
|
|
Transport, ConfiguredVias, {LServer, undefined}),
|
|
|
|
ViaTransport = case Transport of
|
|
|
|
tls -> <<"TLS">>;
|
|
|
|
tcp -> <<"TCP">>;
|
|
|
|
udp -> <<"UDP">>
|
|
|
|
end,
|
|
|
|
Via = #via{transport = ViaTransport,
|
|
|
|
host = ViaHost,
|
|
|
|
port = ViaPort,
|
2014-06-02 18:46:09 +02:00
|
|
|
params = [{<<"branch">>, esip:make_branch()}]},
|
2014-05-02 08:51:08 +02:00
|
|
|
Req#sip{hdrs = [{'via', [Via]}|Hdrs]}.
|
|
|
|
|
2014-05-31 08:00:27 +02:00
|
|
|
add_record_route_and_set_uri(URI, LServer, #sip{hdrs = Hdrs} = Req) ->
|
|
|
|
case is_request_within_dialog(Req) of
|
|
|
|
false ->
|
2014-07-07 07:23:11 +02:00
|
|
|
case need_record_route(LServer) of
|
|
|
|
true ->
|
|
|
|
RR_URI = get_configured_record_route(LServer),
|
2016-11-24 13:06:06 +01:00
|
|
|
TS = (integer_to_binary(p1_time_compat:system_time(seconds))),
|
2014-07-07 07:23:11 +02:00
|
|
|
Sign = make_sign(TS, Hdrs),
|
|
|
|
User = <<TS/binary, $-, Sign/binary>>,
|
|
|
|
NewRR_URI = RR_URI#uri{user = User},
|
|
|
|
Hdrs1 = [{'record-route', [{<<>>, NewRR_URI, []}]}|Hdrs],
|
|
|
|
Req#sip{uri = URI, hdrs = Hdrs1};
|
|
|
|
false ->
|
|
|
|
Req
|
|
|
|
end;
|
2014-05-31 08:00:27 +02:00
|
|
|
true ->
|
|
|
|
Req
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_request_within_dialog(#sip{hdrs = Hdrs}) ->
|
|
|
|
{_, _, Params} = esip:get_hdr('to', Hdrs),
|
|
|
|
esip:has_param(<<"tag">>, Params).
|
2014-05-23 18:13:31 +02:00
|
|
|
|
2014-07-07 07:23:11 +02:00
|
|
|
need_record_route(LServer) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
gen_mod:get_module_opt(LServer, mod_sip, always_record_route).
|
2014-07-07 07:23:11 +02:00
|
|
|
|
2014-06-01 11:34:51 +02:00
|
|
|
make_sign(TS, Hdrs) ->
|
|
|
|
{_, #uri{user = FUser, host = FServer}, FParams} = esip:get_hdr('from', Hdrs),
|
|
|
|
{_, #uri{user = TUser, host = TServer}, _} = esip:get_hdr('to', Hdrs),
|
2014-07-17 18:30:09 +02:00
|
|
|
LFUser = safe_nodeprep(FUser),
|
|
|
|
LTUser = safe_nodeprep(TUser),
|
|
|
|
LFServer = safe_nameprep(FServer),
|
|
|
|
LTServer = safe_nameprep(TServer),
|
2014-06-01 11:34:51 +02:00
|
|
|
FromTag = esip:get_param(<<"tag">>, FParams),
|
|
|
|
CallID = esip:get_hdr('call-id', Hdrs),
|
2017-04-29 10:39:40 +02:00
|
|
|
SharedKey = ejabberd_config:get_option(shared_key),
|
2017-03-14 00:31:51 +01:00
|
|
|
str:sha([SharedKey, LFUser, LFServer, LTUser, LTServer,
|
2014-06-01 11:34:51 +02:00
|
|
|
FromTag, CallID, TS]).
|
|
|
|
|
|
|
|
is_signed_by_me(TS_Sign, Hdrs) ->
|
|
|
|
try
|
|
|
|
[TSBin, Sign] = str:tokens(TS_Sign, <<"-">>),
|
2016-11-24 13:06:06 +01:00
|
|
|
TS = (binary_to_integer(TSBin)),
|
2015-12-07 16:08:57 +01:00
|
|
|
NowTS = p1_time_compat:system_time(seconds),
|
2014-06-01 11:34:51 +02:00
|
|
|
true = (NowTS - TS) =< ?SIGN_LIFETIME,
|
|
|
|
Sign == make_sign(TSBin, Hdrs)
|
|
|
|
catch _:_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2014-05-02 08:51:08 +02:00
|
|
|
get_configured_vias(LServer) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
gen_mod:get_module_opt(LServer, mod_sip, via).
|
2014-05-02 13:18:57 +02:00
|
|
|
|
2014-06-02 08:16:08 +02:00
|
|
|
get_configured_record_route(LServer) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
gen_mod:get_module_opt(LServer, mod_sip, record_route).
|
2014-05-31 12:20:38 +02:00
|
|
|
|
2014-06-02 08:16:08 +02:00
|
|
|
get_configured_routes(LServer) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
gen_mod:get_module_opt(LServer, mod_sip, routes).
|
2014-06-02 08:16:08 +02:00
|
|
|
|
2014-05-02 13:18:57 +02:00
|
|
|
mark_transaction_as_complete(TrID, State) ->
|
|
|
|
NewTrIDs = lists:delete(TrID, State#state.tr_ids),
|
|
|
|
State#state{tr_ids = NewTrIDs}.
|
|
|
|
|
|
|
|
collect_response(Resp, #state{responses = Resps} = State) ->
|
|
|
|
State#state{responses = [Resp|Resps]}.
|
|
|
|
|
|
|
|
choose_best_response(#state{responses = Responses} = State) ->
|
|
|
|
SortedResponses = lists:keysort(#sip.status, Responses),
|
|
|
|
case lists:filter(
|
|
|
|
fun(#sip{status = Status}) ->
|
|
|
|
Status >= 600
|
|
|
|
end, SortedResponses) of
|
|
|
|
[Resp|_] ->
|
|
|
|
esip:reply(State#state.orig_trid, Resp);
|
|
|
|
[] ->
|
|
|
|
case SortedResponses of
|
|
|
|
[Resp|_] ->
|
|
|
|
esip:reply(State#state.orig_trid, Resp);
|
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end.
|
2014-05-23 18:13:31 +02:00
|
|
|
|
2014-06-02 08:16:08 +02:00
|
|
|
%% Just compare host part only.
|
|
|
|
cmp_uri(#uri{host = H1}, #uri{host = H2}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:nameprep(H1) == jid:nameprep(H2).
|
2014-06-02 08:16:08 +02:00
|
|
|
|
|
|
|
is_my_route(URI, URIs) ->
|
|
|
|
lists:any(fun(U) -> cmp_uri(URI, U) end, URIs).
|
2014-05-31 12:20:38 +02:00
|
|
|
|
|
|
|
prepare_request(LServer, #sip{hdrs = Hdrs} = Req) ->
|
2014-06-02 08:16:08 +02:00
|
|
|
ConfiguredRRoute = get_configured_record_route(LServer),
|
|
|
|
ConfiguredRoutes = get_configured_routes(LServer),
|
2014-05-23 18:13:31 +02:00
|
|
|
Hdrs1 = lists:flatmap(
|
|
|
|
fun({Hdr, HdrList}) when Hdr == 'route';
|
|
|
|
Hdr == 'record-route' ->
|
|
|
|
case lists:filter(
|
2014-05-31 12:20:38 +02:00
|
|
|
fun({_, URI, _}) ->
|
2014-06-02 08:16:08 +02:00
|
|
|
not cmp_uri(URI, ConfiguredRRoute)
|
|
|
|
and not is_my_route(URI, ConfiguredRoutes)
|
2014-05-23 18:13:31 +02:00
|
|
|
end, HdrList) of
|
|
|
|
[] ->
|
|
|
|
[];
|
|
|
|
HdrList1 ->
|
|
|
|
[{Hdr, HdrList1}]
|
|
|
|
end;
|
|
|
|
(Hdr) ->
|
|
|
|
[Hdr]
|
|
|
|
end, Hdrs),
|
|
|
|
MF = esip:get_hdr('max-forwards', Hdrs1),
|
|
|
|
Hdrs2 = esip:set_hdr('max-forwards', MF-1, Hdrs1),
|
|
|
|
Hdrs3 = lists:filter(
|
|
|
|
fun({'proxy-authorization', {_, Params}}) ->
|
|
|
|
Realm = esip:unquote(esip:get_param(<<"realm">>, Params)),
|
2015-11-24 16:44:13 +01:00
|
|
|
not mod_sip:is_my_host(jid:nameprep(Realm));
|
2014-05-23 18:13:31 +02:00
|
|
|
(_) ->
|
|
|
|
true
|
|
|
|
end, Hdrs2),
|
|
|
|
Req#sip{hdrs = Hdrs3}.
|
2014-07-17 18:30:09 +02:00
|
|
|
|
|
|
|
safe_nodeprep(S) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nodeprep(S) of
|
2014-07-17 18:30:09 +02:00
|
|
|
error -> S;
|
|
|
|
S1 -> S1
|
|
|
|
end.
|
|
|
|
|
|
|
|
safe_nameprep(S) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:nameprep(S) of
|
2014-07-17 18:30:09 +02:00
|
|
|
error -> S;
|
|
|
|
S1 -> S1
|
|
|
|
end.
|
2017-05-23 12:12:48 +02:00
|
|
|
|
|
|
|
-endif.
|