summaryrefslogtreecommitdiff
path: root/src/mod_sip_proxy.erl
blob: aa749ccf7f029650750cdfcd479da68be436a174 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2014, Evgeny Khramtsov
%%% @doc
%%%
%%% @end
%%% Created : 21 Apr 2014 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------
-module(mod_sip_proxy).

-define(GEN_FSM, p1_fsm).
-behaviour(?GEN_FSM).

%% API
-export([start/2, start_link/2, route/4, route/5]).

%% gen_fsm callbacks
-export([init/1, wait_for_request/2, wait_for_response/2,
	 handle_event/3, handle_sync_event/4,
	 handle_info/3, terminate/3, code_change/4]).

-include("ejabberd.hrl").
-include("logger.hrl").
-include("esip.hrl").

-define(MAX_REDIRECTS, 5).

-record(state, {host = <<"">> :: binary(),
		opts = []     :: [{certfile, binary()}],
		orig_trid,
		orig_req      :: #sip{},
		client_trid}).

%%%===================================================================
%%% API
%%%===================================================================
start(LServer, Opts) ->
    supervisor:start_child(mod_sip_proxy_sup, [LServer, Opts]).

start_link(LServer, Opts) ->
    ?GEN_FSM:start_link(?MODULE, [LServer, Opts], []).

route(Resp, Req, _SIPSock, TrID, Pid) ->
    ?GEN_FSM:send_event(Pid, {Resp, Req, TrID}).

route(SIPMsg, _SIPSock, TrID, Pid) ->
    ?GEN_FSM:send_event(Pid, {SIPMsg, TrID}),
    wait.

%%%===================================================================
%%% gen_fsm callbacks
%%%===================================================================
init([Host, Opts]) ->
    {ok, wait_for_request, #state{opts = Opts, host = Host}}.

wait_for_request({#sip{type = request} = Req, TrID}, State) ->
    Opts = mod_sip:add_certfile(State#state.host, State#state.opts),
    Req1 = mod_sip:prepare_request(Req),
    case connect(Req1, Opts) of
	{ok, SIPSocket} ->
	    Req2 = mod_sip:add_via(SIPSocket, State#state.host, Req1),
	    case esip:request(SIPSocket, Req2, {?MODULE, route, [self()]}) of
		{ok, ClientTrID} ->
		    {next_state, wait_for_response,
		     State#state{orig_trid = TrID,
				 orig_req = Req,
				 client_trid = ClientTrID}};
		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;
	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) ->
    esip:cancel(State#state.client_trid),
    {next_state, wait_for_response, State};
wait_for_response({Resp, _TrID}, State) ->
    case Resp of
        {error, _} ->
	    Req = State#state.orig_req,
            {Status, Reason} = esip:error_status(Resp),
            case Status of
                408 when Req#sip.method /= <<"INVITE">> ->
                    %% Absorb useless 408. See RFC4320
                    esip:stop_transaction(State#state.orig_trid);
                _ ->
                    ErrResp = mod_sip:make_response(
				Req,
				#sip{type = response,
				     status = Status,
				     reason = Reason}),
		    esip:reply(State#state.orig_trid, ErrResp)
            end,
            {stop, normal, State};
        #sip{status = 100} ->
            {next_state, wait_for_response, State};
        #sip{status = Status} ->
            case esip:split_hdrs('via', Resp#sip.hdrs) of
                {[_], _} ->
                    {stop, normal, State};
                {[_|Vias], NewHdrs} ->
                    esip:reply(State#state.orig_trid,
                               Resp#sip{hdrs = [{'via', Vias}|NewHdrs]}),
                    if Status < 200 ->
                            {next_state, wait_for_response, State};
                       true ->
                            {stop, normal, State}
                    end
            end
    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
%%%===================================================================
connect(Req, Opts) ->
    case proplists:get_value(socket, Opts) of
	undefined ->
	    esip:connect(Req, Opts);
	#sip_socket{} = SIPSock ->
	    {ok, SIPSock}
    end.