Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 1 | % 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 Welte | 0b452a8 | 2012-04-16 13:17:31 +0200 | [diff] [blame] | 20 | % |
| 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 Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 34 | |
| 35 | -module(mgw_nat_usr). |
| 36 | -author("Harald Welte <laforge@gnumonks.org>"). |
| 37 | |
| 38 | -behavior(gen_server). |
| 39 | |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 40 | -export([start_link/1, stop/0]). |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 41 | -export([init/1, handle_cast/2, handle_info/2, terminate/2]). |
| 42 | |
| 43 | |
| 44 | start_link(Params) -> |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 45 | MscName = get_cfg_pl_val(msc_name, Params), |
| 46 | gen_server:start_link({local, MscName}, ?MODULE, Params, []). |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 47 | |
| 48 | stop() -> |
| 49 | gen_server:cast(?MODULE, stop). |
| 50 | |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 51 | %% Callback functions of the OTP behavior |
| 52 | |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 53 | init(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 Welte | 3ae9813 | 2011-04-14 18:05:42 +0200 | [diff] [blame] | 61 | RewriteActMod:reload_config(), |
Harald Welte | 5df8338 | 2011-03-08 15:17:32 +0100 | [diff] [blame] | 62 | SctpHdlrArgs = [MscLocalIp, MscLocalPort, MscRemoteIp, |
Harald Welte | a227e76 | 2011-04-06 01:07:00 +0200 | [diff] [blame] | 63 | StpRemoteIp, StpRemotePort, RewriteActMod], |
Harald Welte | 26647e0 | 2011-09-24 12:07:25 +0200 | [diff] [blame] | 64 | {ok, LoopDat} = apply(sctp_handler, init, SctpHdlrArgs), |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 65 | {ok, {Params, LoopDat}}. |
| 66 | |
| 67 | % this cast is produced by mgw_nat_sup child walker |
| 68 | handle_cast(reload_config, L = {Params, _LoopData}) -> |
| 69 | RewriteActMod = get_cfg_pl_val(rewrite_act_mod, Params), |
| 70 | RewriteActMod:reload_config(), |
| 71 | {noreply, L}; |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 72 | |
| 73 | handle_cast(stop, LoopData) -> |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 74 | {stop, normal, LoopData}. |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 75 | |
Harald Welte | a227e76 | 2011-04-06 01:07:00 +0200 | [diff] [blame] | 76 | |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 77 | terminate(_Reason, _LoopData) -> |
| 78 | ok. |
| 79 | |
| 80 | % callback for other events like incoming SCTP message |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 81 | handle_info({sctp, Sock, Ip, Port, Data}, {InitParams, LoopData}) -> |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 82 | NewL = sctp_handler:handle_sctp(LoopData, {sctp, Sock, Ip, Port, Data}), |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 83 | {noreply, {InitParams, NewL}}. |
Harald Welte | 3ae9813 | 2011-04-14 18:05:42 +0200 | [diff] [blame] | 84 | |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 85 | % wrapper around proplists:get_value() to check for missing stuff |
| 86 | get_cfg_pl_val(Name, List) -> |
| 87 | case proplists:get_value(Name, List) of |
Harald Welte | 3ae9813 | 2011-04-14 18:05:42 +0200 | [diff] [blame] | 88 | undefined -> |
| 89 | error_logger:error_report([{error, app_cfg_missing}, |
Harald Welte | ce995d3 | 2011-09-15 00:15:29 +0100 | [diff] [blame] | 90 | {get_cfg_pl_val, Name}]); |
| 91 | Val -> |
Harald Welte | 3ae9813 | 2011-04-14 18:05:42 +0200 | [diff] [blame] | 92 | Val |
| 93 | end. |