blob: 9dd3d656d1d5a1c8706b07aab4e1bc7450c7daf5 [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 Welteaf3a9fc2011-02-25 10:36:43 +010026-export([start_link/1, stop/0, sccp_masq_reset/0, sccp_masq_dump/0]).
Harald Welte20ad1ba2011-02-07 20:50:39 +010027-export([init/1, handle_cast/2, handle_info/2, terminate/2]).
28
29
30start_link(Params) ->
31 gen_server:start_link({local, ?MODULE}, ?MODULE, Params, []).
32
33stop() ->
34 gen_server:cast(?MODULE, stop).
35
36sccp_masq_reset() ->
37 gen_server:cast(?MODULE, sccp_masq_reset).
38
Harald Welteaf3a9fc2011-02-25 10:36:43 +010039sccp_masq_dump() ->
40 gen_server:cast(?MODULE, sccp_masq_dump).
41
Harald Welte20ad1ba2011-02-07 20:50:39 +010042
43%% Callback functions of the OTP behavior
44
Harald Welte5df83382011-03-08 15:17:32 +010045init(_Params) ->
Harald Welte20ad1ba2011-02-07 20:50:39 +010046 sccp_masq:init(),
Harald Weltef9629642011-02-10 20:44:46 +010047 map_masq:config_update(),
Harald Welte5df83382011-03-08 15:17:32 +010048 {ok, MscLocalIp} = application:get_env(msc_local_ip),
49 {ok, MscLocalPort} = application:get_env(msc_local_port),
50 {ok, MscRemoteIp} = application:get_env(msc_remote_ip),
51 {ok, StpRemoteIp} = application:get_env(stp_remote_ip),
52 {ok, StpRemotePort} = application:get_env(stp_remote_port),
53 {ok, RewriteActor} = application:get_env(rewrite_actor),
54 HandleFn = get_handle_fn(RewriteActor),
55 io:format("Starting mgw_nat_usr with rewrite actor ~p~n", [RewriteActor]),
56 SctpHdlrArgs = [MscLocalIp, MscLocalPort, MscRemoteIp,
57 StpRemoteIp, StpRemotePort, HandleFn],
58 apply(sctp_handler, init, SctpHdlrArgs).
Harald Welte20ad1ba2011-02-07 20:50:39 +010059
60handle_cast(stop, LoopData) ->
61 {stop, normal, LoopData};
62
63handle_cast(sccp_masq_reset, LoopData) ->
64 sccp_masq:reset(),
Harald Welteaf3a9fc2011-02-25 10:36:43 +010065 {noreply, LoopData};
66
67handle_cast(sccp_masq_dump, LoopData) ->
68 sccp_masq:dump(),
Harald Welte20ad1ba2011-02-07 20:50:39 +010069 {noreply, LoopData}.
70
71terminate(_Reason, _LoopData) ->
72 ok.
73
74% callback for other events like incoming SCTP message
75handle_info({sctp, Sock, Ip, Port, Data}, LoopData) ->
76 NewL = sctp_handler:handle_sctp(LoopData, {sctp, Sock, Ip, Port, Data}),
77 {noreply, NewL}.
Harald Welte5df83382011-03-08 15:17:32 +010078
79
80% return rewrite_actor function reference
81get_handle_fn(bow_onw) ->
82 fun mgw_nat_act_bow_onw:rewrite_actor/5;
83get_handle_fn(vfuk_onw) ->
84 fun mgw_nat_act_vfuk_onw:rewrite_actor/5.