aboutsummaryrefslogtreecommitdiffstats
path: root/src/sctp_handler.erl
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2011-03-08 15:17:32 +0100
committerHarald Welte <laforge@gnumonks.org>2011-03-08 15:17:32 +0100
commit5df8338d70e7e3da7f5d7967b29372d39747255c (patch)
tree9cb47d353814c92a39ef59ce66f7a9eebd20a37f /src/sctp_handler.erl
parent39b19ef69d940865d79f560eddfb79782f69f3c6 (diff)
Make MGW nat more flexible by introdicng actors an function references
Instead of a static configuration of rewrite/mangling/patching steps, we now introduce the concept of a 'rewrite_actor', which is a callback function that determines which rewrite/patching code to call at which particular layer of the protocol. The default rewrite actor "bow_onw" resembles the existing rewrite behavior. There is a stub new rewrite actore "vfuk_onw", which will be used for Camel phase rewriting.
Diffstat (limited to 'src/sctp_handler.erl')
-rw-r--r--src/sctp_handler.erl38
1 files changed, 14 insertions, 24 deletions
diff --git a/src/sctp_handler.erl b/src/sctp_handler.erl
index 333d551..3011cfa 100644
--- a/src/sctp_handler.erl
+++ b/src/sctp_handler.erl
@@ -22,7 +22,7 @@
-module(sctp_handler).
-author("Harald Welte <laforge@gnumonks.org>").
--export([init/5, handle_sctp/2]).
+-export([init/6, handle_sctp/2]).
-include_lib("kernel/include/inet.hrl").
-include_lib("kernel/include/inet_sctp.hrl").
@@ -35,13 +35,14 @@
-record(loop_data,
{msc_sock, msc_local_ip, msc_remote_ip, msc_remote_port,
msc_local_port, msc_assoc_id,
- stp_sock, stp_remote_ip, stp_remote_port, stp_assoc_id
+ stp_sock, stp_remote_ip, stp_remote_port, stp_assoc_id,
+ handle_fn
}).
-define(COMMON_SOCKOPTS, [{active, once}, {reuseaddr, true}]).
% initialize the sockets towards MSC (listening) and STP (connect)
-init(MscLocalIP, MscLocalPort, MscRemoteIP, StpRemoteIP, StpRemotePort) ->
+init(MscLocalIP, MscLocalPort, MscRemoteIP, StpRemoteIP, StpRemotePort, HandleFn) ->
{ok, MscSock} = gen_sctp:open([{ip, MscLocalIP},{port,MscLocalPort}]
++ ?COMMON_SOCKOPTS),
io:format("Listening for MSC on ~w:~w. ~w~n",
@@ -51,7 +52,7 @@ init(MscLocalIP, MscLocalPort, MscRemoteIP, StpRemoteIP, StpRemotePort) ->
L = #loop_data{msc_sock = MscSock, msc_local_ip = MscLocalIP,
msc_remote_ip = MscRemoteIP,
stp_sock = StpSock, stp_remote_ip = StpRemoteIP,
- stp_remote_port = StpRemotePort},
+ stp_remote_port = StpRemotePort, handle_fn = HandleFn},
{ok, L}.
% initiate a connection to STP as a client
@@ -61,7 +62,8 @@ initiate_stp_connection(#loop_data{stp_sock = Sock, stp_remote_ip = IP, stp_remo
% main loop function
handle_sctp(L = #loop_data{msc_sock=MscSock, msc_remote_ip=MscRemoteIp, msc_remote_port=MscRemotePort,
- stp_sock=StpSock, stp_remote_ip=StpRemoteIp, stp_remote_port=StpRemotePort},
+ stp_sock=StpSock, stp_remote_ip=StpRemoteIp, stp_remote_port=StpRemotePort,
+ handle_fn=HandleFn},
Sctp) ->
io:format("Entering receive loop ~p~n", [L]),
io:format("======================================================================~n"),
@@ -102,13 +104,13 @@ handle_sctp(L = #loop_data{msc_sock=MscSock, msc_remote_ip=MscRemoteIp, msc_remo
% MSC data
{sctp, MscSock, MscRemoteIp, MscRemotePort, {[Anc], Data}} ->
io:format("MSC rx data: ~p ~p~n", [Anc, Data]),
- handle_rx_data(L, from_msc, Anc, Data),
+ handle_rx_data(HandleFn, L, from_msc, Anc, Data),
inet:setopts(MscSock, [{active, once}]),
NewL = L;
% STP data
{sctp, StpSock, StpRemoteIp, StpRemotePort, {[Anc], Data}} ->
io:format("STP rx data: ~p ~p~n", [Anc, Data]),
- handle_rx_data(L, from_stp, Anc, Data),
+ handle_rx_data(HandleFn, L, from_stp, Anc, Data),
inet:setopts(StpSock, [{active, once}]),
NewL = L;
{sctp, _Sock, RemoteIp, _Remote_port, {_Anc, Data}}
@@ -124,23 +126,10 @@ handle_sctp(L = #loop_data{msc_sock=MscSock, msc_remote_ip=MscRemoteIp, msc_remo
NewL.
-try_mangle(L, From, Data) ->
- try mgw_nat:mangle_rx_data(L, From, Data) of
- Val ->
- Val
- catch error:Error ->
- % some parser error, simply forward msg unmodified
- io:format("MGW NAT mangling Error: ~p~n", [Error]),
- Data
- end.
-
% handle incoming data on one of the SCTP sockets
-handle_rx_data(_L, From, SRInfo, Data) when is_binary(Data) ->
- io:format("Unhandled Rx Data from SCTP from ~p: ~p, ~p~n", [From, SRInfo, Data]);
-
-handle_rx_data(L, From, SRInf = #sctp_sndrcvinfo{ppid = 2,
+handle_rx_data(Fn, L, From, SRInf = #sctp_sndrcvinfo{ppid = 2,
stream = Stream}, Data) when is_binary(Data) ->
- DataOut = try_mangle(L, From, Data),
+ DataOut = Fn(sctp, From, [L, SRInf], 2, Data),
% send mangled data to other peer
case From of
from_msc ->
@@ -158,6 +147,7 @@ handle_rx_data(L, From, SRInf = #sctp_sndrcvinfo{ppid = 2,
true ->
io:format("Data is NOT equal~n")
end,
- ok = gen_sctp:send(Sock, SndRcvInfo, DataOut).
-
+ ok = gen_sctp:send(Sock, SndRcvInfo, DataOut);
+handle_rx_data(_Fn, _L, From, SRInfo, Data) when is_binary(Data) ->
+ io:format("Unhandled Rx Data from SCTP from ~p: ~p, ~p~n", [From, SRInfo, Data]).