blob: 14bcd7ce35959b8fe88d9cf2f8ec1d7eb0b6e092 [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/>.
20
21-module(mgw_nat_usr).
22-author("Harald Welte <laforge@gnumonks.org>").
23
24-behavior(gen_server).
25
Harald Welte3607d362011-04-14 17:46:53 +020026-export([start_link/1, stop/0, sccp_masq_reset/0, sccp_masq_dump/0,
27 reload_config/0]).
Harald Welte20ad1ba2011-02-07 20:50:39 +010028-export([init/1, handle_cast/2, handle_info/2, terminate/2]).
29
30
31start_link(Params) ->
32 gen_server:start_link({local, ?MODULE}, ?MODULE, Params, []).
33
34stop() ->
35 gen_server:cast(?MODULE, stop).
36
37sccp_masq_reset() ->
38 gen_server:cast(?MODULE, sccp_masq_reset).
39
Harald Welteaf3a9fc2011-02-25 10:36:43 +010040sccp_masq_dump() ->
41 gen_server:cast(?MODULE, sccp_masq_dump).
42
Harald Welte3607d362011-04-14 17:46:53 +020043reload_config() ->
44 gen_server:cast(?MODULE, reload_config).
Harald Welte20ad1ba2011-02-07 20:50:39 +010045
46%% Callback functions of the OTP behavior
47
Harald Welte5df83382011-03-08 15:17:32 +010048init(_Params) ->
Harald Welte20ad1ba2011-02-07 20:50:39 +010049 sccp_masq:init(),
Harald Weltef9629642011-02-10 20:44:46 +010050 map_masq:config_update(),
Harald Welte5df83382011-03-08 15:17:32 +010051 {ok, MscLocalIp} = application:get_env(msc_local_ip),
52 {ok, MscLocalPort} = application:get_env(msc_local_port),
53 {ok, MscRemoteIp} = application:get_env(msc_remote_ip),
54 {ok, StpRemoteIp} = application:get_env(stp_remote_ip),
55 {ok, StpRemotePort} = application:get_env(stp_remote_port),
Harald Weltea227e762011-04-06 01:07:00 +020056 {ok, RewriteActMod} = application:get_env(rewrite_act_mod),
57 io:format("Starting mgw_nat_usr with rewrite actor module ~p~n", [RewriteActMod]),
Harald Welte5df83382011-03-08 15:17:32 +010058 SctpHdlrArgs = [MscLocalIp, MscLocalPort, MscRemoteIp,
Harald Weltea227e762011-04-06 01:07:00 +020059 StpRemoteIp, StpRemotePort, RewriteActMod],
Harald Welte5df83382011-03-08 15:17:32 +010060 apply(sctp_handler, init, SctpHdlrArgs).
Harald Welte20ad1ba2011-02-07 20:50:39 +010061
62handle_cast(stop, LoopData) ->
63 {stop, normal, LoopData};
64
65handle_cast(sccp_masq_reset, LoopData) ->
66 sccp_masq:reset(),
Harald Welteaf3a9fc2011-02-25 10:36:43 +010067 {noreply, LoopData};
68
69handle_cast(sccp_masq_dump, LoopData) ->
70 sccp_masq:dump(),
Harald Weltea227e762011-04-06 01:07:00 +020071 {noreply, LoopData};
72
73handle_cast(reload_config, LoopData) ->
74 {ok, RewriteActMod} = application:get_env(rewrite_act_mod),
75 RewriteActMod:reload_config(),
Harald Welte20ad1ba2011-02-07 20:50:39 +010076 {noreply, LoopData}.
77
Harald Weltea227e762011-04-06 01:07:00 +020078
Harald Welte20ad1ba2011-02-07 20:50:39 +010079terminate(_Reason, _LoopData) ->
80 ok.
81
82% callback for other events like incoming SCTP message
83handle_info({sctp, Sock, Ip, Port, Data}, LoopData) ->
84 NewL = sctp_handler:handle_sctp(LoopData, {sctp, Sock, Ip, Port, Data}),
85 {noreply, NewL}.