blob: 6a033c4ee0a23fc3288c8285bcc41dd6ae23b3f0 [file] [log] [blame]
Harald Welted17e75a2011-01-18 18:25:20 +01001% SCTP Handler for gateway between MSC and STP, transparently
2% rewriting addresses on the fly
3
4% (C) 2011 by Harald Welte <laforge@gnumonks.org>
5% (C) 2011 OnWaves
6%
7% All Rights Reserved
8%
9% This program is free software; you can redistribute it and/or modify
10% it under the terms of the GNU Affero General Public License as
11% published by the Free Software Foundation; either version 3 of the
12% License, or (at your option) any later version.
13%
14% This program is distributed in the hope that it will be useful,
15% but WITHOUT ANY WARRANTY; without even the implied warranty of
16% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17% GNU General Public License for more details.
18%
19% You should have received a copy of the GNU Affero General Public License
20% along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22
23-module(sctp_handler).
24-author("Harald Welte <laforge@gnumonks.org>").
Harald Welteaca4edc2011-01-21 16:21:12 +000025-export([init/5]).
Harald Welted17e75a2011-01-18 18:25:20 +010026
27-include_lib("kernel/include/inet.hrl").
28-include_lib("kernel/include/inet_sctp.hrl").
29
Harald Welteaca4edc2011-01-21 16:21:12 +000030%-include("m2ua.hrl").
31%-include("mtp3.hrl").
32%-include("isup.hrl").
33%-include("sccp.hrl").
Harald Welted17e75a2011-01-18 18:25:20 +010034
35-record(loop_data,
36 {msc_sock, msc_local_ip, msc_remote_ip, msc_remote_port,
37 msc_local_port, msc_assoc_id,
38 stp_sock, stp_remote_ip, stp_remote_port, stp_assoc_id
39 }).
40
41-define(COMMON_SOCKOPTS, [{active, once}, {reuseaddr, true}]).
42
43% initialize the sockets towards MSC (listening) and STP (connect)
44init(MscLocalIP, MscLocalPort, MscRemoteIP, StpRemoteIP, StpRemotePort) ->
45 {ok, MscSock} = gen_sctp:open([{ip, MscLocalIP},{port,MscLocalPort}]
46 ++ ?COMMON_SOCKOPTS),
47 io:format("Listening for MSC on ~w:~w. ~w~n",
48 [MscLocalIP, MscLocalPort, MscSock]),
49 ok = gen_sctp:listen(MscSock, true),
50 {ok, StpSock} = gen_sctp:open(?COMMON_SOCKOPTS),
51 L = #loop_data{msc_sock = MscSock, msc_local_ip = MscLocalIP,
52 msc_remote_ip = MscRemoteIP,
53 stp_sock = StpSock, stp_remote_ip = StpRemoteIP,
54 stp_remote_port = StpRemotePort},
55 loop(L).
56
57% initiate a connection to STP as a client
58initiate_stp_connection(#loop_data{stp_sock = Sock, stp_remote_ip = IP, stp_remote_port = Port}, Opts) ->
59 io:format("Establishing SCTP conn to STP ~p Port ~p~n", [IP, Port]),
60 gen_sctp:connect(Sock, IP, Port, Opts ++ ?COMMON_SOCKOPTS).
61
62% main loop function
63loop(L = #loop_data{msc_sock=MscSock, msc_remote_ip=MscRemoteIp, msc_remote_port=MscRemotePort,
64 stp_sock=StpSock, stp_remote_ip=StpRemoteIp, stp_remote_port=StpRemotePort}) ->
65 io:format("Entering receive loop ~p~n", [L]),
66 io:format("======================================================================~n"),
67 receive
68 % MSC connect or disconnect
69 {sctp, MscSock, MscRemoteIp, Port, {ANC, SAC}}
70 when is_record(SAC, sctp_assoc_change) ->
71 io:format("MSC sctp_assoc_change ~p ~p~n", [ANC, SAC]),
72 #sctp_assoc_change{state = SacState, outbound_streams = OutStreams,
73 inbound_streams = InStreams, assoc_id = MscAssocId} = SAC,
74 case SacState of
75 comm_up ->
76 InitMsg = #sctp_initmsg{num_ostreams=InStreams,
77 max_instreams=OutStreams},
78 {ok, StpAssoc} = initiate_stp_connection(L, [{sctp_initmsg,InitMsg}]),
79 io:format("STP Assoc: ~p~n", [StpAssoc]),
80 NewL = L#loop_data{msc_remote_port = Port,
81 msc_assoc_id = MscAssocId,
82 stp_assoc_id = StpAssoc#sctp_assoc_change.assoc_id};
83 comm_lost ->
84 NewL = L,
85 % maybe we should simply die?
Harald Weltebdc7a5c2011-01-21 14:50:15 +000086 io:format("MSC SCTP comm_lost~n"),
Harald Welted17e75a2011-01-18 18:25:20 +010087 foo:bar();
88 addr_unreachable ->
89 NewL = L,
Harald Weltebdc7a5c2011-01-21 14:50:15 +000090 io:format("MSC SCTP addr_unreachable~n"),
Harald Welted17e75a2011-01-18 18:25:20 +010091 % maybe we should simply die?
92 foo:bar()
93 end,
94 inet:setopts(MscSock, [{active, once}]);
95 % STP connect or disconnect
96 {sctp, StpSock, StpRemoteIp, StpRemotePort, {_Anc, SAC}}
97 when is_record(SAC, sctp_assoc_change) ->
98 io:format("STP sctp_assoc_change ~p~n", [SAC]),
99 inet:setopts(StpSock, [{active, once}]),
100 NewL = L;
101 % MSC data
102 {sctp, MscSock, MscRemoteIp, MscRemotePort, {[Anc], Data}} ->
103 io:format("MSC rx data: ~p ~p~n", [Anc, Data]),
104 handle_rx_data(L, from_msc, Anc, Data),
105 inet:setopts(MscSock, [{active, once}]),
106 NewL = L;
107 % STP data
108 {sctp, StpSock, StpRemoteIp, StpRemotePort, {[Anc], Data}} ->
109 io:format("STP rx data: ~p ~p~n", [Anc, Data]),
110 handle_rx_data(L, from_stp, Anc, Data),
111 inet:setopts(StpSock, [{active, once}]),
112 NewL = L;
Harald Weltebdc7a5c2011-01-21 14:50:15 +0000113 {sctp, _Sock, RemoteIp, _Remote_port, {_Anc, Data}}
114 when is_record(Data, sctp_shutdown_event) ->
115 % maybe we should simply die?
116 NewL = L,
117 io:format("SCTP remote ~p shutdown~n", [RemoteIp]),
118 foo:bar();
Harald Welted17e75a2011-01-18 18:25:20 +0100119 Other ->
120 io:format("OTHER ~p~n", [Other]),
121 NewL = L
122 end,
123 loop(NewL).
124
Harald Welted17e75a2011-01-18 18:25:20 +0100125% handle incoming data on one of the SCTP sockets
126handle_rx_data(L, From, SRInf = #sctp_sndrcvinfo{ppid = 2,
127 stream = Stream}, Data) when is_binary(Data) ->
Harald Welteaca4edc2011-01-21 16:21:12 +0000128 DataOut = mgw_nat:mangle_rx_data(L, From, Data),
Harald Welted17e75a2011-01-18 18:25:20 +0100129 % send mangled data to other peer
130 case From of
131 from_msc ->
132 Sock = L#loop_data.stp_sock,
133 AssocId = L#loop_data.stp_assoc_id;
134 from_stp ->
135 Sock = L#loop_data.msc_sock,
136 AssocId = L#loop_data.msc_assoc_id
137 end,
138 SndRcvInfo = #sctp_sndrcvinfo{ppid = 2, stream = Stream, assoc_id = AssocId},
Harald Weltebdc7a5c2011-01-21 14:50:15 +0000139 %io:format("Sending ~p to ~p ~p~n", [DataOut, Sock, SndRcvInfo]),
Harald Welted17e75a2011-01-18 18:25:20 +0100140 % if they are not equal, we will abort here
141 DataOut = Data,
142 io:format("Data is equal~n"),
143 ok = gen_sctp:send(Sock, SndRcvInfo, DataOut).
144
Harald Welted17e75a2011-01-18 18:25:20 +0100145