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/>. |
| 20 | |
| 21 | -module(mgw_nat_usr). |
| 22 | -author("Harald Welte <laforge@gnumonks.org>"). |
| 23 | |
| 24 | -behavior(gen_server). |
| 25 | |
Harald Welte | 3607d36 | 2011-04-14 17:46:53 +0200 | [diff] [blame] | 26 | -export([start_link/1, stop/0, sccp_masq_reset/0, sccp_masq_dump/0, |
| 27 | reload_config/0]). |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 28 | -export([init/1, handle_cast/2, handle_info/2, terminate/2]). |
| 29 | |
| 30 | |
| 31 | start_link(Params) -> |
| 32 | gen_server:start_link({local, ?MODULE}, ?MODULE, Params, []). |
| 33 | |
| 34 | stop() -> |
| 35 | gen_server:cast(?MODULE, stop). |
| 36 | |
| 37 | sccp_masq_reset() -> |
| 38 | gen_server:cast(?MODULE, sccp_masq_reset). |
| 39 | |
Harald Welte | af3a9fc | 2011-02-25 10:36:43 +0100 | [diff] [blame] | 40 | sccp_masq_dump() -> |
| 41 | gen_server:cast(?MODULE, sccp_masq_dump). |
| 42 | |
Harald Welte | 3607d36 | 2011-04-14 17:46:53 +0200 | [diff] [blame] | 43 | reload_config() -> |
| 44 | gen_server:cast(?MODULE, reload_config). |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 45 | |
| 46 | %% Callback functions of the OTP behavior |
| 47 | |
Harald Welte | 5df8338 | 2011-03-08 15:17:32 +0100 | [diff] [blame] | 48 | init(_Params) -> |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 49 | sccp_masq:init(), |
Harald Welte | f962964 | 2011-02-10 20:44:46 +0100 | [diff] [blame] | 50 | map_masq:config_update(), |
Harald Welte | 3ae9813 | 2011-04-14 18:05:42 +0200 | [diff] [blame^] | 51 | MscLocalIp = get_app_config(msc_local_ip), |
| 52 | MscLocalPort = get_app_config(msc_local_port), |
| 53 | MscRemoteIp = get_app_config(msc_remote_ip), |
| 54 | StpRemoteIp = get_app_config(stp_remote_ip), |
| 55 | StpRemotePort = get_app_config(stp_remote_port), |
| 56 | RewriteActMod = get_app_config(rewrite_act_mod), |
| 57 | RewriteActMod:reload_config(), |
Harald Welte | a227e76 | 2011-04-06 01:07:00 +0200 | [diff] [blame] | 58 | io:format("Starting mgw_nat_usr with rewrite actor module ~p~n", [RewriteActMod]), |
Harald Welte | 5df8338 | 2011-03-08 15:17:32 +0100 | [diff] [blame] | 59 | SctpHdlrArgs = [MscLocalIp, MscLocalPort, MscRemoteIp, |
Harald Welte | a227e76 | 2011-04-06 01:07:00 +0200 | [diff] [blame] | 60 | StpRemoteIp, StpRemotePort, RewriteActMod], |
Harald Welte | 5df8338 | 2011-03-08 15:17:32 +0100 | [diff] [blame] | 61 | apply(sctp_handler, init, SctpHdlrArgs). |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 62 | |
| 63 | handle_cast(stop, LoopData) -> |
| 64 | {stop, normal, LoopData}; |
| 65 | |
| 66 | handle_cast(sccp_masq_reset, LoopData) -> |
| 67 | sccp_masq:reset(), |
Harald Welte | af3a9fc | 2011-02-25 10:36:43 +0100 | [diff] [blame] | 68 | {noreply, LoopData}; |
| 69 | |
| 70 | handle_cast(sccp_masq_dump, LoopData) -> |
| 71 | sccp_masq:dump(), |
Harald Welte | a227e76 | 2011-04-06 01:07:00 +0200 | [diff] [blame] | 72 | {noreply, LoopData}; |
| 73 | |
| 74 | handle_cast(reload_config, LoopData) -> |
Harald Welte | 3ae9813 | 2011-04-14 18:05:42 +0200 | [diff] [blame^] | 75 | RewriteActMod = get_app_config(rewrite_act_mod), |
Harald Welte | a227e76 | 2011-04-06 01:07:00 +0200 | [diff] [blame] | 76 | RewriteActMod:reload_config(), |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 77 | {noreply, LoopData}. |
| 78 | |
Harald Welte | a227e76 | 2011-04-06 01:07:00 +0200 | [diff] [blame] | 79 | |
Harald Welte | 20ad1ba | 2011-02-07 20:50:39 +0100 | [diff] [blame] | 80 | terminate(_Reason, _LoopData) -> |
| 81 | ok. |
| 82 | |
| 83 | % callback for other events like incoming SCTP message |
| 84 | handle_info({sctp, Sock, Ip, Port, Data}, LoopData) -> |
| 85 | NewL = sctp_handler:handle_sctp(LoopData, {sctp, Sock, Ip, Port, Data}), |
| 86 | {noreply, NewL}. |
Harald Welte | 3ae9813 | 2011-04-14 18:05:42 +0200 | [diff] [blame^] | 87 | |
| 88 | get_app_config(Name) -> |
| 89 | case application:get_env(Name) of |
| 90 | undefined -> |
| 91 | error_logger:error_report([{error, app_cfg_missing}, |
| 92 | {get_app_config, Name}]), |
| 93 | throw(app_cfg_missing); |
| 94 | {ok, Val} -> |
| 95 | Val |
| 96 | end. |