2014-04-30 17:20:38 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2017-01-03 15:58:52 +01:00
|
|
|
%%% File : mod_sip_registrar.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Purpose :
|
2014-04-30 17:20:38 +02:00
|
|
|
%%% Created : 23 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
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2014-2018 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_registrar).
|
|
|
|
|
2017-05-23 12:12:48 +02:00
|
|
|
-ifndef(SIP).
|
|
|
|
-export([]).
|
|
|
|
-else.
|
2017-02-14 10:39:26 +01:00
|
|
|
-ifndef(GEN_SERVER).
|
|
|
|
-define(GEN_SERVER, gen_server).
|
|
|
|
-endif.
|
2014-04-30 17:20:38 +02:00
|
|
|
-behaviour(?GEN_SERVER).
|
|
|
|
|
|
|
|
%% API
|
2014-06-06 07:32:07 +02:00
|
|
|
-export([start_link/0, request/2, find_sockets/2, ping/1]).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-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-05-01 18:43:51 +02:00
|
|
|
-define(CALL_TIMEOUT, timer:seconds(30)).
|
2014-05-30 21:11:46 +02:00
|
|
|
-define(DEFAULT_EXPIRES, 3600).
|
2014-05-01 13:51:58 +02:00
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
-record(sip_session, {us = {<<"">>, <<"">>} :: {binary(), binary()},
|
2014-06-06 07:32:07 +02:00
|
|
|
socket = #sip_socket{} :: #sip_socket{},
|
|
|
|
call_id = <<"">> :: binary(),
|
|
|
|
cseq = 0 :: non_neg_integer(),
|
2015-12-07 16:08:57 +01:00
|
|
|
timestamp = p1_time_compat:timestamp() :: erlang:timestamp(),
|
2014-06-06 07:32:07 +02:00
|
|
|
contact :: {binary(), #uri{}, [{binary(), binary()}]},
|
2017-02-18 07:36:27 +01:00
|
|
|
flow_tref :: reference() | undefined,
|
2014-06-06 11:52:55 +02:00
|
|
|
reg_tref = make_ref() :: reference(),
|
|
|
|
conn_mref = make_ref() :: reference(),
|
2014-06-06 07:32:07 +02:00
|
|
|
expires = 0 :: non_neg_integer()}).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
|
|
|
-record(state, {}).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
start_link() ->
|
|
|
|
?GEN_SERVER:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
request(#sip{hdrs = Hdrs} = Req, SIPSock) ->
|
|
|
|
{_, #uri{user = U, host = S}, _} = esip:get_hdr('to', Hdrs),
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(U),
|
|
|
|
LServer = jid:nameprep(S),
|
2014-04-30 17:20:38 +02:00
|
|
|
{PeerIP, _} = SIPSock#sip_socket.peer,
|
|
|
|
US = {LUser, LServer},
|
2014-05-01 13:51:58 +02:00
|
|
|
CallID = esip:get_hdr('call-id', Hdrs),
|
|
|
|
CSeq = esip:get_hdr('cseq', Hdrs),
|
2014-05-30 21:11:46 +02:00
|
|
|
Expires = esip:get_hdr('expires', Hdrs, ?DEFAULT_EXPIRES),
|
2014-06-06 07:32:07 +02:00
|
|
|
Supported = esip:get_hdrs('supported', Hdrs),
|
|
|
|
IsOutboundSupported = lists:member(<<"outbound">>, Supported),
|
2014-04-30 17:20:38 +02:00
|
|
|
case esip:get_hdrs('contact', Hdrs) of
|
|
|
|
[<<"*">>] when Expires == 0 ->
|
2014-05-30 21:11:46 +02:00
|
|
|
case unregister_session(US, CallID, CSeq) of
|
|
|
|
{ok, ContactsWithExpires} ->
|
2014-05-01 13:51:58 +02:00
|
|
|
?INFO_MSG("unregister SIP session for user ~s@~s from ~s",
|
|
|
|
[LUser, LServer, inet_parse:ntoa(PeerIP)]),
|
2014-05-30 21:11:46 +02:00
|
|
|
Cs = prepare_contacts_to_send(ContactsWithExpires),
|
2014-05-01 13:51:58 +02:00
|
|
|
mod_sip:make_response(
|
2014-05-26 17:43:15 +02:00
|
|
|
Req,
|
|
|
|
#sip{type = response,
|
|
|
|
status = 200,
|
2014-05-30 21:11:46 +02:00
|
|
|
hdrs = [{'contact', Cs}]});
|
2014-05-01 13:51:58 +02:00
|
|
|
{error, Why} ->
|
|
|
|
{Status, Reason} = make_status(Why),
|
|
|
|
mod_sip:make_response(
|
|
|
|
Req, #sip{type = response,
|
|
|
|
status = Status,
|
|
|
|
reason = Reason})
|
|
|
|
end;
|
2014-04-30 17:20:38 +02:00
|
|
|
[{_, _URI, _Params}|_] = Contacts ->
|
2014-05-30 21:11:46 +02:00
|
|
|
ContactsWithExpires = make_contacts_with_expires(Contacts, Expires),
|
2014-06-06 07:32:07 +02:00
|
|
|
ContactsHaveManyRegID = contacts_have_many_reg_id(Contacts),
|
2014-05-30 21:11:46 +02:00
|
|
|
Expires1 = lists:max([E || {_, E} <- ContactsWithExpires]),
|
2014-04-30 17:20:38 +02:00
|
|
|
MinExpires = min_expires(),
|
2014-05-30 21:11:46 +02:00
|
|
|
if Expires1 > 0, Expires1 < MinExpires ->
|
|
|
|
mod_sip:make_response(
|
2014-04-30 17:20:38 +02:00
|
|
|
Req, #sip{type = response,
|
|
|
|
status = 423,
|
|
|
|
hdrs = [{'min-expires', MinExpires}]});
|
2014-06-06 07:32:07 +02:00
|
|
|
ContactsHaveManyRegID ->
|
|
|
|
mod_sip:make_response(
|
|
|
|
Req, #sip{type = response, status = 400,
|
|
|
|
reason = <<"Multiple 'reg-id' parameter">>});
|
2014-05-30 21:11:46 +02:00
|
|
|
true ->
|
|
|
|
case register_session(US, SIPSock, CallID, CSeq,
|
2014-06-06 07:32:07 +02:00
|
|
|
IsOutboundSupported,
|
2014-05-30 21:11:46 +02:00
|
|
|
ContactsWithExpires) of
|
|
|
|
{ok, Res} ->
|
2014-05-31 07:06:28 +02:00
|
|
|
?INFO_MSG("~s SIP session for user ~s@~s from ~s",
|
|
|
|
[Res, LUser, LServer,
|
|
|
|
inet_parse:ntoa(PeerIP)]),
|
2014-05-30 21:11:46 +02:00
|
|
|
Cs = prepare_contacts_to_send(ContactsWithExpires),
|
2014-06-06 07:32:07 +02:00
|
|
|
Require = case need_ob_hdrs(
|
|
|
|
Contacts, IsOutboundSupported) of
|
|
|
|
true -> [{'require', [<<"outbound">>]},
|
|
|
|
{'flow-timer',
|
|
|
|
get_flow_timeout(LServer, SIPSock)}];
|
|
|
|
false -> []
|
|
|
|
end,
|
2014-05-01 13:51:58 +02:00
|
|
|
mod_sip:make_response(
|
|
|
|
Req,
|
2014-05-30 21:11:46 +02:00
|
|
|
#sip{type = response,
|
|
|
|
status = 200,
|
2014-06-06 07:32:07 +02:00
|
|
|
hdrs = [{'contact', Cs}|Require]});
|
2014-05-01 13:51:58 +02:00
|
|
|
{error, Why} ->
|
|
|
|
{Status, Reason} = make_status(Why),
|
|
|
|
mod_sip:make_response(
|
|
|
|
Req, #sip{type = response,
|
|
|
|
status = Status,
|
|
|
|
reason = Reason})
|
|
|
|
end
|
2014-04-30 17:20:38 +02:00
|
|
|
end;
|
2014-05-01 13:51:58 +02:00
|
|
|
[] ->
|
2014-05-01 19:52:47 +02:00
|
|
|
case mnesia:dirty_read(sip_session, US) of
|
2014-06-06 07:32:07 +02:00
|
|
|
[_|_] = Sessions ->
|
2014-05-30 21:11:46 +02:00
|
|
|
ContactsWithExpires =
|
|
|
|
lists:map(
|
2014-06-06 07:32:07 +02:00
|
|
|
fun(#sip_session{contact = Contact, expires = Es}) ->
|
2014-05-30 21:11:46 +02:00
|
|
|
{Contact, Es}
|
2014-06-06 07:32:07 +02:00
|
|
|
end, Sessions),
|
2014-05-30 21:11:46 +02:00
|
|
|
Cs = prepare_contacts_to_send(ContactsWithExpires),
|
|
|
|
mod_sip:make_response(
|
|
|
|
Req, #sip{type = response, status = 200,
|
|
|
|
hdrs = [{'contact', Cs}]});
|
2014-05-01 19:52:47 +02:00
|
|
|
[] ->
|
|
|
|
{Status, Reason} = make_status(notfound),
|
|
|
|
mod_sip:make_response(
|
|
|
|
Req, #sip{type = response,
|
|
|
|
status = Status,
|
|
|
|
reason = Reason})
|
|
|
|
end;
|
2014-04-30 17:20:38 +02:00
|
|
|
_ ->
|
|
|
|
mod_sip:make_response(Req, #sip{type = response, status = 400})
|
|
|
|
end.
|
|
|
|
|
2014-05-01 13:51:58 +02:00
|
|
|
find_sockets(U, S) ->
|
2014-04-30 17:20:38 +02:00
|
|
|
case mnesia:dirty_read(sip_session, {U, S}) of
|
2014-06-06 07:32:07 +02:00
|
|
|
[_|_] = Sessions ->
|
2014-05-30 21:11:46 +02:00
|
|
|
lists:map(
|
2014-06-06 07:32:07 +02:00
|
|
|
fun(#sip_session{contact = {_, URI, _},
|
2014-05-30 21:11:46 +02:00
|
|
|
socket = Socket}) ->
|
|
|
|
{Socket, URI}
|
2014-06-06 07:32:07 +02:00
|
|
|
end, Sessions);
|
2014-04-30 17:20:38 +02:00
|
|
|
[] ->
|
2014-05-01 13:51:58 +02:00
|
|
|
[]
|
2014-04-30 17:20:38 +02:00
|
|
|
end.
|
|
|
|
|
2014-06-06 11:52:55 +02:00
|
|
|
ping(SIPSocket) ->
|
|
|
|
call({ping, SIPSocket}).
|
2014-06-06 07:32:07 +02:00
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% gen_server callbacks
|
|
|
|
%%%===================================================================
|
|
|
|
init([]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2014-06-06 07:32:07 +02:00
|
|
|
update_table(),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, sip_session,
|
2014-04-30 17:20:38 +02:00
|
|
|
[{ram_copies, [node()]},
|
2014-06-06 07:32:07 +02:00
|
|
|
{type, bag},
|
2017-01-17 15:05:14 +01:00
|
|
|
{attributes, record_info(fields, sip_session)},
|
|
|
|
{index, [conn_mref,socket]}]),
|
2014-04-30 17:20:38 +02:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
2014-06-06 07:32:07 +02:00
|
|
|
handle_call({write, Sessions, Supported}, _From, State) ->
|
|
|
|
Res = write_session(Sessions, Supported),
|
2014-04-30 17:20:38 +02:00
|
|
|
{reply, Res, State};
|
2014-05-30 21:11:46 +02:00
|
|
|
handle_call({delete, US, CallID, CSeq}, _From, State) ->
|
|
|
|
Res = delete_session(US, CallID, CSeq),
|
2014-04-30 17:20:38 +02:00
|
|
|
{reply, Res, State};
|
2014-06-06 11:52:55 +02:00
|
|
|
handle_call({ping, SIPSocket}, _From, State) ->
|
|
|
|
Res = process_ping(SIPSocket),
|
|
|
|
{reply, Res, State};
|
2014-04-30 17:20:38 +02:00
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
2014-06-06 07:32:07 +02:00
|
|
|
handle_info({write, Sessions, Supported}, State) ->
|
|
|
|
write_session(Sessions, Supported),
|
2014-04-30 17:20:38 +02:00
|
|
|
{noreply, State};
|
2014-05-30 21:11:46 +02:00
|
|
|
handle_info({delete, US, CallID, CSeq}, State) ->
|
|
|
|
delete_session(US, CallID, CSeq),
|
2014-04-30 17:20:38 +02:00
|
|
|
{noreply, State};
|
|
|
|
handle_info({timeout, TRef, US}, State) ->
|
2014-05-01 14:31:03 +02:00
|
|
|
delete_expired_session(US, TRef),
|
2014-04-30 17:20:38 +02:00
|
|
|
{noreply, State};
|
2014-06-06 07:32:07 +02:00
|
|
|
handle_info({'DOWN', MRef, process, _Pid, _Reason}, State) ->
|
2014-06-06 11:52:55 +02:00
|
|
|
case mnesia:dirty_index_read(sip_session, MRef, #sip_session.conn_mref) of
|
2014-06-06 07:32:07 +02:00
|
|
|
[Session] ->
|
|
|
|
mnesia:dirty_delete_object(Session);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
2014-04-30 17:20:38 +02:00
|
|
|
handle_info(_Info, State) ->
|
|
|
|
?ERROR_MSG("got unexpected info: ~p", [_Info]),
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2014-06-06 07:32:07 +02:00
|
|
|
register_session(US, SIPSocket, CallID, CSeq, IsOutboundSupported,
|
|
|
|
ContactsWithExpires) ->
|
|
|
|
Sessions = lists:map(
|
2014-05-30 21:11:46 +02:00
|
|
|
fun({Contact, Expires}) ->
|
2014-06-06 07:32:07 +02:00
|
|
|
#sip_session{us = US,
|
|
|
|
socket = SIPSocket,
|
|
|
|
call_id = CallID,
|
|
|
|
cseq = CSeq,
|
2015-12-07 16:08:57 +01:00
|
|
|
timestamp = p1_time_compat:timestamp(),
|
2014-06-06 07:32:07 +02:00
|
|
|
contact = Contact,
|
|
|
|
expires = Expires}
|
2014-05-30 21:11:46 +02:00
|
|
|
end, ContactsWithExpires),
|
2014-06-06 07:32:07 +02:00
|
|
|
Msg = {write, Sessions, IsOutboundSupported},
|
|
|
|
call(Msg).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2014-05-30 21:11:46 +02:00
|
|
|
unregister_session(US, CallID, CSeq) ->
|
|
|
|
Msg = {delete, US, CallID, CSeq},
|
2014-05-01 18:43:51 +02:00
|
|
|
call(Msg).
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2014-06-06 07:32:07 +02:00
|
|
|
write_session([#sip_session{us = {U, S} = US}|_] = NewSessions,
|
|
|
|
IsOutboundSupported) ->
|
|
|
|
PrevSessions = mnesia:dirty_read(sip_session, US),
|
2014-05-30 21:11:46 +02:00
|
|
|
Res = lists:foldl(
|
|
|
|
fun(_, {error, _} = Err) ->
|
|
|
|
Err;
|
2014-06-06 07:32:07 +02:00
|
|
|
(#sip_session{call_id = CallID,
|
|
|
|
expires = Expires,
|
|
|
|
cseq = CSeq} = Session, {Add, Del}) ->
|
|
|
|
case find_session(Session, PrevSessions,
|
|
|
|
IsOutboundSupported) of
|
|
|
|
{ok, normal, #sip_session{call_id = CallID,
|
|
|
|
cseq = PrevCSeq}}
|
2014-05-30 21:11:46 +02:00
|
|
|
when PrevCSeq > CSeq ->
|
|
|
|
{error, cseq_out_of_order};
|
2014-06-06 07:32:07 +02:00
|
|
|
{ok, _Type, PrevSession} when Expires == 0 ->
|
|
|
|
{Add, [PrevSession|Del]};
|
|
|
|
{ok, _Type, PrevSession} ->
|
|
|
|
{[Session|Add], [PrevSession|Del]};
|
2014-05-30 21:11:46 +02:00
|
|
|
{error, notfound} when Expires == 0 ->
|
|
|
|
{error, notfound};
|
|
|
|
{error, notfound} ->
|
2014-06-06 07:32:07 +02:00
|
|
|
{[Session|Add], Del}
|
2014-05-01 18:58:14 +02:00
|
|
|
end
|
2014-06-06 07:32:07 +02:00
|
|
|
end, {[], []}, NewSessions),
|
2014-05-30 21:11:46 +02:00
|
|
|
MaxSessions = ejabberd_sm:get_max_user_sessions(U, S),
|
|
|
|
case Res of
|
|
|
|
{error, Why} ->
|
|
|
|
{error, Why};
|
2014-06-06 07:32:07 +02:00
|
|
|
{AddSessions, DelSessions} ->
|
2014-05-31 07:06:28 +02:00
|
|
|
MaxSessions = ejabberd_sm:get_max_user_sessions(U, S),
|
2014-06-06 07:32:07 +02:00
|
|
|
AllSessions = AddSessions ++ PrevSessions -- DelSessions,
|
|
|
|
if length(AllSessions) > MaxSessions ->
|
2014-05-31 07:06:28 +02:00
|
|
|
{error, too_many_sessions};
|
|
|
|
true ->
|
2014-06-06 20:29:25 +02:00
|
|
|
lists:foreach(fun delete_session/1, DelSessions),
|
2014-06-06 07:32:07 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Session) ->
|
|
|
|
NewSession = set_monitor_and_timer(
|
|
|
|
Session, IsOutboundSupported),
|
|
|
|
mnesia:dirty_write(NewSession)
|
|
|
|
end, AddSessions),
|
|
|
|
case {AllSessions, AddSessions} of
|
|
|
|
{[], _} ->
|
|
|
|
{ok, unregister};
|
|
|
|
{_, []} ->
|
2014-05-31 07:06:28 +02:00
|
|
|
{ok, unregister};
|
|
|
|
_ ->
|
2014-06-06 07:32:07 +02:00
|
|
|
{ok, register}
|
2014-05-31 07:06:28 +02:00
|
|
|
end
|
2014-05-30 21:11:46 +02:00
|
|
|
end
|
2014-05-01 13:51:58 +02:00
|
|
|
end.
|
2014-04-30 17:20:38 +02:00
|
|
|
|
2014-05-30 21:11:46 +02:00
|
|
|
delete_session(US, CallID, CSeq) ->
|
2014-04-30 17:20:38 +02:00
|
|
|
case mnesia:dirty_read(sip_session, US) of
|
2014-06-06 07:32:07 +02:00
|
|
|
[_|_] = Sessions ->
|
2014-05-30 21:11:46 +02:00
|
|
|
case lists:all(
|
2014-06-06 07:32:07 +02:00
|
|
|
fun(S) when S#sip_session.call_id == CallID,
|
|
|
|
S#sip_session.cseq > CSeq ->
|
2014-05-30 21:11:46 +02:00
|
|
|
false;
|
|
|
|
(_) ->
|
|
|
|
true
|
2014-06-06 07:32:07 +02:00
|
|
|
end, Sessions) of
|
2014-05-30 21:11:46 +02:00
|
|
|
true ->
|
|
|
|
ContactsWithExpires =
|
|
|
|
lists:map(
|
2014-06-06 20:29:25 +02:00
|
|
|
fun(#sip_session{contact = Contact} = Session) ->
|
|
|
|
delete_session(Session),
|
2014-05-30 21:11:46 +02:00
|
|
|
{Contact, 0}
|
2014-06-06 07:32:07 +02:00
|
|
|
end, Sessions),
|
2014-05-30 21:11:46 +02:00
|
|
|
{ok, ContactsWithExpires};
|
|
|
|
false ->
|
|
|
|
{error, cseq_out_of_order}
|
2014-05-01 13:51:58 +02:00
|
|
|
end;
|
2014-04-30 17:20:38 +02:00
|
|
|
[] ->
|
2014-05-01 13:51:58 +02:00
|
|
|
{error, notfound}
|
2014-04-30 17:20:38 +02:00
|
|
|
end.
|
|
|
|
|
2014-05-01 14:31:03 +02:00
|
|
|
delete_expired_session(US, TRef) ->
|
|
|
|
case mnesia:dirty_read(sip_session, US) of
|
2014-06-06 07:32:07 +02:00
|
|
|
[_|_] = Sessions ->
|
2014-06-06 11:52:55 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(#sip_session{reg_tref = T1,
|
|
|
|
flow_tref = T2} = Session)
|
|
|
|
when T1 == TRef; T2 == TRef ->
|
2014-06-12 07:27:32 +02:00
|
|
|
if T2 /= undefined ->
|
|
|
|
close_socket(Session);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2014-06-06 20:29:25 +02:00
|
|
|
delete_session(Session);
|
2014-06-06 11:52:55 +02:00
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, Sessions);
|
2014-05-01 14:31:03 +02:00
|
|
|
[] ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2014-04-30 17:20:38 +02:00
|
|
|
min_expires() ->
|
|
|
|
60.
|
|
|
|
|
|
|
|
to_integer(Bin, Min, Max) ->
|
2016-11-24 13:06:06 +01:00
|
|
|
case catch (binary_to_integer(Bin)) of
|
2014-04-30 17:20:38 +02:00
|
|
|
N when N >= Min, N =< Max ->
|
|
|
|
{ok, N};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
2014-05-01 13:51:58 +02:00
|
|
|
|
2014-05-01 18:43:51 +02:00
|
|
|
call(Msg) ->
|
|
|
|
case catch ?GEN_SERVER:call(?MODULE, Msg, ?CALL_TIMEOUT) of
|
|
|
|
{'EXIT', {timeout, _}} ->
|
|
|
|
{error, timeout};
|
|
|
|
{'EXIT', Why} ->
|
|
|
|
{error, Why};
|
|
|
|
Reply ->
|
|
|
|
Reply
|
|
|
|
end.
|
|
|
|
|
2014-05-30 21:11:46 +02:00
|
|
|
make_contacts_with_expires(Contacts, Expires) ->
|
|
|
|
lists:map(
|
|
|
|
fun({Name, URI, Params}) ->
|
|
|
|
E1 = case to_integer(esip:get_param(<<"expires">>, Params),
|
|
|
|
0, (1 bsl 32)-1) of
|
|
|
|
{ok, E} -> E;
|
|
|
|
_ -> Expires
|
|
|
|
end,
|
|
|
|
Params1 = lists:keydelete(<<"expires">>, 1, Params),
|
|
|
|
{{Name, URI, Params1}, E1}
|
|
|
|
end, Contacts).
|
|
|
|
|
|
|
|
prepare_contacts_to_send(ContactsWithExpires) ->
|
|
|
|
lists:map(
|
|
|
|
fun({{Name, URI, Params}, Expires}) ->
|
|
|
|
Params1 = esip:set_param(<<"expires">>,
|
|
|
|
list_to_binary(
|
|
|
|
integer_to_list(Expires)),
|
|
|
|
Params),
|
|
|
|
{Name, URI, Params1}
|
|
|
|
end, ContactsWithExpires).
|
|
|
|
|
2014-06-06 07:32:07 +02:00
|
|
|
contacts_have_many_reg_id(Contacts) ->
|
|
|
|
Sum = lists:foldl(
|
|
|
|
fun({_Name, _URI, Params}, Acc) ->
|
|
|
|
case get_ob_params(Params) of
|
|
|
|
error ->
|
|
|
|
Acc;
|
|
|
|
{_, _} ->
|
|
|
|
Acc + 1
|
|
|
|
end
|
|
|
|
end, 0, Contacts),
|
|
|
|
if Sum > 1 ->
|
|
|
|
true;
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
find_session(#sip_session{contact = {_, URI, Params}}, Sessions,
|
|
|
|
IsOutboundSupported) ->
|
|
|
|
if IsOutboundSupported ->
|
|
|
|
case get_ob_params(Params) of
|
|
|
|
{InstanceID, RegID} ->
|
|
|
|
find_session_by_ob({InstanceID, RegID}, Sessions);
|
|
|
|
error ->
|
|
|
|
find_session_by_uri(URI, Sessions)
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
find_session_by_uri(URI, Sessions)
|
|
|
|
end.
|
|
|
|
|
|
|
|
find_session_by_ob({InstanceID, RegID},
|
|
|
|
[#sip_session{contact = {_, _, Params}} = Session|Sessions]) ->
|
|
|
|
case get_ob_params(Params) of
|
|
|
|
{InstanceID, RegID} ->
|
|
|
|
{ok, flow, Session};
|
|
|
|
_ ->
|
|
|
|
find_session_by_ob({InstanceID, RegID}, Sessions)
|
|
|
|
end;
|
|
|
|
find_session_by_ob(_, []) ->
|
|
|
|
{error, notfound}.
|
|
|
|
|
|
|
|
find_session_by_uri(URI1,
|
|
|
|
[#sip_session{contact = {_, URI2, _}} = Session|Sessions]) ->
|
2014-05-30 21:11:46 +02:00
|
|
|
case cmp_uri(URI1, URI2) of
|
|
|
|
true ->
|
2014-06-06 07:32:07 +02:00
|
|
|
{ok, normal, Session};
|
2014-05-30 21:11:46 +02:00
|
|
|
false ->
|
2014-06-06 07:32:07 +02:00
|
|
|
find_session_by_uri(URI1, Sessions)
|
2014-05-30 21:11:46 +02:00
|
|
|
end;
|
2014-06-06 07:32:07 +02:00
|
|
|
find_session_by_uri(_, []) ->
|
2014-05-30 21:11:46 +02:00
|
|
|
{error, notfound}.
|
|
|
|
|
|
|
|
%% TODO: this is *totally* wrong.
|
|
|
|
%% Rewrite this using URI comparison rules
|
|
|
|
cmp_uri(#uri{user = U, host = H, port = P},
|
|
|
|
#uri{user = U, host = H, port = P}) ->
|
|
|
|
true;
|
|
|
|
cmp_uri(_, _) ->
|
|
|
|
false.
|
|
|
|
|
2014-05-01 13:51:58 +02:00
|
|
|
make_status(notfound) ->
|
|
|
|
{404, esip:reason(404)};
|
|
|
|
make_status(cseq_out_of_order) ->
|
|
|
|
{500, <<"CSeq is Out of Order">>};
|
2014-05-01 18:43:51 +02:00
|
|
|
make_status(timeout) ->
|
|
|
|
{408, esip:reason(408)};
|
2014-05-01 18:58:14 +02:00
|
|
|
make_status(too_many_sessions) ->
|
|
|
|
{503, <<"Too Many Registered Sessions">>};
|
2014-05-01 13:51:58 +02:00
|
|
|
make_status(_) ->
|
|
|
|
{500, esip:reason(500)}.
|
2014-06-06 07:32:07 +02:00
|
|
|
|
|
|
|
get_ob_params(Params) ->
|
|
|
|
case esip:get_param(<<"+sip.instance">>, Params) of
|
|
|
|
<<>> ->
|
|
|
|
error;
|
|
|
|
InstanceID ->
|
|
|
|
case to_integer(esip:get_param(<<"reg-id">>, Params),
|
|
|
|
0, (1 bsl 32)-1) of
|
|
|
|
{ok, RegID} ->
|
|
|
|
{InstanceID, RegID};
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
need_ob_hdrs(_Contacts, _IsOutboundSupported = false) ->
|
|
|
|
false;
|
|
|
|
need_ob_hdrs(Contacts, _IsOutboundSupported = true) ->
|
|
|
|
lists:any(
|
|
|
|
fun({_Name, _URI, Params}) ->
|
|
|
|
case get_ob_params(Params) of
|
|
|
|
error -> false;
|
|
|
|
{_, _} -> true
|
|
|
|
end
|
|
|
|
end, Contacts).
|
|
|
|
|
|
|
|
get_flow_timeout(LServer, #sip_socket{type = Type}) ->
|
2015-06-01 12:19:01 +02:00
|
|
|
case Type of
|
|
|
|
udp ->
|
|
|
|
gen_mod:get_module_opt(
|
2018-01-23 08:54:52 +01:00
|
|
|
LServer, mod_sip, flow_timeout_udp);
|
2015-06-01 12:19:01 +02:00
|
|
|
_ ->
|
|
|
|
gen_mod:get_module_opt(
|
2018-01-23 08:54:52 +01:00
|
|
|
LServer, mod_sip, flow_timeout_tcp)
|
2015-06-01 12:19:01 +02:00
|
|
|
end.
|
2014-06-06 07:32:07 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, sip_session),
|
|
|
|
case catch mnesia:table_info(sip_session, attributes) of
|
|
|
|
Fields ->
|
|
|
|
ok;
|
|
|
|
[_|_] ->
|
|
|
|
mnesia:delete_table(sip_session);
|
|
|
|
{'EXIT', _} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2014-06-06 11:52:55 +02:00
|
|
|
set_monitor_and_timer(#sip_session{socket = #sip_socket{type = Type,
|
|
|
|
pid = Pid} = SIPSock,
|
|
|
|
conn_mref = MRef,
|
2014-06-06 07:32:07 +02:00
|
|
|
expires = Expires,
|
|
|
|
us = {_, LServer},
|
|
|
|
contact = {_, _, Params}} = Session,
|
2014-06-06 11:52:55 +02:00
|
|
|
IsOutboundSupported) ->
|
|
|
|
RegTRef = set_timer(Session, Expires),
|
|
|
|
Session1 = Session#sip_session{reg_tref = RegTRef},
|
|
|
|
if IsOutboundSupported ->
|
|
|
|
case get_ob_params(Params) of
|
|
|
|
error ->
|
|
|
|
Session1;
|
|
|
|
{_, _} ->
|
|
|
|
FlowTimeout = get_flow_timeout(LServer, SIPSock),
|
|
|
|
FlowTRef = set_timer(Session1, FlowTimeout),
|
|
|
|
NewMRef = if Type == udp -> MRef;
|
|
|
|
true -> erlang:monitor(process, Pid)
|
|
|
|
end,
|
|
|
|
Session1#sip_session{conn_mref = NewMRef,
|
|
|
|
flow_tref = FlowTRef}
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
Session1
|
2014-06-06 07:32:07 +02:00
|
|
|
end.
|
|
|
|
|
2014-06-06 11:52:55 +02:00
|
|
|
set_timer(#sip_session{us = US}, Timeout) ->
|
|
|
|
erlang:start_timer(Timeout * 1000, self(), US).
|
|
|
|
|
2014-06-12 07:27:32 +02:00
|
|
|
close_socket(#sip_session{socket = SIPSocket}) ->
|
|
|
|
if SIPSocket#sip_socket.type /= udp ->
|
|
|
|
esip_socket:close(SIPSocket);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2014-06-06 20:29:25 +02:00
|
|
|
delete_session(#sip_session{reg_tref = RegTRef,
|
|
|
|
flow_tref = FlowTRef,
|
|
|
|
conn_mref = MRef} = Session) ->
|
|
|
|
erlang:cancel_timer(RegTRef),
|
|
|
|
catch erlang:cancel_timer(FlowTRef),
|
|
|
|
catch erlang:demonitor(MRef, [flush]),
|
|
|
|
mnesia:dirty_delete_object(Session).
|
|
|
|
|
2014-06-06 11:52:55 +02:00
|
|
|
process_ping(SIPSocket) ->
|
2014-07-16 08:33:34 +02:00
|
|
|
ErrResponse = if SIPSocket#sip_socket.type == udp -> pang;
|
2014-06-06 11:52:55 +02:00
|
|
|
true -> drop
|
|
|
|
end,
|
|
|
|
Sessions = mnesia:dirty_index_read(
|
|
|
|
sip_session, SIPSocket, #sip_session.socket),
|
|
|
|
lists:foldl(
|
|
|
|
fun(#sip_session{flow_tref = TRef,
|
|
|
|
us = {_, LServer}} = Session, _)
|
|
|
|
when TRef /= undefined ->
|
|
|
|
erlang:cancel_timer(TRef),
|
|
|
|
mnesia:dirty_delete_object(Session),
|
|
|
|
Timeout = get_flow_timeout(LServer, SIPSocket),
|
|
|
|
NewTRef = set_timer(Session, Timeout),
|
2014-07-16 08:33:34 +02:00
|
|
|
case mnesia:dirty_write(
|
|
|
|
Session#sip_session{flow_tref = NewTRef}) of
|
|
|
|
ok ->
|
|
|
|
pong;
|
|
|
|
_Err ->
|
|
|
|
pang
|
|
|
|
end;
|
2014-06-06 11:52:55 +02:00
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, ErrResponse, Sessions).
|
2017-05-23 12:12:48 +02:00
|
|
|
|
|
|
|
-endif.
|