blob: f19491aa89e982c135a60cb442e695f8313be3c9 [file] [log] [blame]
Harald Welte20ad1ba2011-02-07 20:50:39 +01001% Wrapper code, wrapping sctp_handler.erl into OTP gen_server
2
3% (C) 2011 by Harald Welte <laforge@gnumonks.org>
4% (C) 2011 OnWaves
5%
6% All Rights Reserved
7%
8% This program is free software; you can redistribute it and/or modify
9% it under the terms of the GNU Affero General Public License as
10% published by the Free Software Foundation; either version 3 of the
11% License, or (at your option) any later version.
12%
13% This program is distributed in the hope that it will be useful,
14% but WITHOUT ANY WARRANTY; without even the implied warranty of
15% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16% GNU General Public License for more details.
17%
18% You should have received a copy of the GNU Affero General Public License
19% along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte0b452a82012-04-16 13:17:31 +020020%
21% Additional Permission under GNU AGPL version 3 section 7:
22%
23% If you modify this Program, or any covered work, by linking or
24% combining it with runtime libraries of Erlang/OTP as released by
25% Ericsson on http://www.erlang.org (or a modified version of these
26% libraries), containing parts covered by the terms of the Erlang Public
27% License (http://www.erlang.org/EPLICENSE), the licensors of this
28% Program grant you additional permission to convey the resulting work
29% without the need to license the runtime libraries of Erlang/OTP under
30% the GNU Affero General Public License. Corresponding Source for a
31% non-source form of such a combination shall include the source code
32% for the parts of the runtime libraries of Erlang/OTP used as well as
33% that of the covered work.
Harald Welte20ad1ba2011-02-07 20:50:39 +010034
35-module(mgw_nat_usr).
36-author("Harald Welte <laforge@gnumonks.org>").
37
38-behavior(gen_server).
39
Harald Weltece995d32011-09-15 00:15:29 +010040-export([start_link/1, stop/0]).
Harald Welte20ad1ba2011-02-07 20:50:39 +010041-export([init/1, handle_cast/2, handle_info/2, terminate/2]).
42
43
44start_link(Params) ->
Harald Weltece995d32011-09-15 00:15:29 +010045 MscName = get_cfg_pl_val(msc_name, Params),
46 gen_server:start_link({local, MscName}, ?MODULE, Params, []).
Harald Welte20ad1ba2011-02-07 20:50:39 +010047
48stop() ->
49 gen_server:cast(?MODULE, stop).
50
Harald Welte20ad1ba2011-02-07 20:50:39 +010051%% Callback functions of the OTP behavior
52
Harald Weltece995d32011-09-15 00:15:29 +010053init(Params) ->
54 io:format("Starting mgw_nat_usr with Args ~p~n", [Params]),
55 MscLocalIp = get_cfg_pl_val(msc_local_ip, Params),
56 MscLocalPort = get_cfg_pl_val(msc_local_port, Params),
57 MscRemoteIp = get_cfg_pl_val(msc_remote_ip, Params),
58 StpRemoteIp = get_cfg_pl_val(stp_remote_ip, Params),
59 StpRemotePort = get_cfg_pl_val(stp_remote_port, Params),
60 RewriteActMod = get_cfg_pl_val(rewrite_act_mod, Params),
Harald Welte3ae98132011-04-14 18:05:42 +020061 RewriteActMod:reload_config(),
Harald Welte5df83382011-03-08 15:17:32 +010062 SctpHdlrArgs = [MscLocalIp, MscLocalPort, MscRemoteIp,
Harald Weltea227e762011-04-06 01:07:00 +020063 StpRemoteIp, StpRemotePort, RewriteActMod],
Harald Welte26647e02011-09-24 12:07:25 +020064 {ok, LoopDat} = apply(sctp_handler, init, SctpHdlrArgs),
Harald Weltece995d32011-09-15 00:15:29 +010065 {ok, {Params, LoopDat}}.
66
67% this cast is produced by mgw_nat_sup child walker
68handle_cast(reload_config, L = {Params, _LoopData}) ->
69 RewriteActMod = get_cfg_pl_val(rewrite_act_mod, Params),
70 RewriteActMod:reload_config(),
71 {noreply, L};
Harald Welte20ad1ba2011-02-07 20:50:39 +010072
73handle_cast(stop, LoopData) ->
Harald Weltece995d32011-09-15 00:15:29 +010074 {stop, normal, LoopData}.
Harald Welte20ad1ba2011-02-07 20:50:39 +010075
Harald Weltea227e762011-04-06 01:07:00 +020076
Harald Welte20ad1ba2011-02-07 20:50:39 +010077terminate(_Reason, _LoopData) ->
78 ok.
79
80% callback for other events like incoming SCTP message
Harald Weltece995d32011-09-15 00:15:29 +010081handle_info({sctp, Sock, Ip, Port, Data}, {InitParams, LoopData}) ->
Harald Welte20ad1ba2011-02-07 20:50:39 +010082 NewL = sctp_handler:handle_sctp(LoopData, {sctp, Sock, Ip, Port, Data}),
Harald Weltece995d32011-09-15 00:15:29 +010083 {noreply, {InitParams, NewL}}.
Harald Welte3ae98132011-04-14 18:05:42 +020084
Harald Weltece995d32011-09-15 00:15:29 +010085% wrapper around proplists:get_value() to check for missing stuff
86get_cfg_pl_val(Name, List) ->
87 case proplists:get_value(Name, List) of
Harald Welte3ae98132011-04-14 18:05:42 +020088 undefined ->
89 error_logger:error_report([{error, app_cfg_missing},
Harald Weltece995d32011-09-15 00:15:29 +010090 {get_cfg_pl_val, Name}]);
91 Val ->
Harald Welte3ae98132011-04-14 18:05:42 +020092 Val
93 end.