Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 1 | % M3UA in accordance with RFC4666 (http://tools.ietf.org/html/rfc4666) |
| 2 | |
| 3 | % (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 4 | % |
| 5 | % All Rights Reserved |
| 6 | % |
| 7 | % This program is free software; you can redistribute it and/or modify |
| 8 | % it under the terms of the GNU Affero General Public License as |
| 9 | % published by the Free Software Foundation; either version 3 of the |
| 10 | % License, or (at your option) any later version. |
| 11 | % |
| 12 | % This program is distributed in the hope that it will be useful, |
| 13 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | % GNU General Public License for more details. |
| 16 | % |
| 17 | % You should have received a copy of the GNU Affero General Public License |
| 18 | % along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | -module(m3ua_core). |
| 21 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 22 | |
| 23 | -include_lib("kernel/include/inet_sctp.hrl"). |
| 24 | -include("sccp.hrl"). |
| 25 | -include("m3ua.hrl"). |
| 26 | |
| 27 | -export([start_link/1]). |
| 28 | |
| 29 | -export([init/1, handle_event/3]). |
| 30 | |
| 31 | % FSM states: |
| 32 | -export([asp_down/2, asp_inactive/2, asp_active/2]). |
| 33 | |
| 34 | -define(T_ACK_TIMEOUT, 2*60*100). |
| 35 | |
| 36 | % Loop Data |
| 37 | -record(m3ua_state, { |
| 38 | role, % asp | sgp |
| 39 | asp_state, % down, inactive, active |
| 40 | t_ack, |
| 41 | user_pid, |
| 42 | sctp_remote_ip, |
| 43 | sctp_remote_port, |
Harald Welte | 8a0ab00 | 2011-04-03 22:16:12 +0200 | [diff] [blame^] | 44 | sctp_local_port, |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 45 | sctp_sock, |
| 46 | sctp_assoc_id |
| 47 | }). |
| 48 | |
| 49 | start_link(InitOpts) -> |
| 50 | gen_fsm:start_link(?MODULE, InitOpts, [{debug, [trace]}]). |
| 51 | |
| 52 | reconnect_sctp(L = #m3ua_state{sctp_remote_ip = Ip, sctp_remote_port = Port, sctp_sock = Sock}) -> |
| 53 | io:format("SCTP Reconnect ~p:~p~n", [Ip, Port]), |
Harald Welte | 8a0ab00 | 2011-04-03 22:16:12 +0200 | [diff] [blame^] | 54 | InitMsg = #sctp_initmsg{num_ostreams = 2, max_instreams = 2}, |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 55 | case gen_sctp:connect(Sock, Ip, Port, [{active, once}, {reuseaddr, true}, |
| 56 | {sctp_initmsg, InitMsg}]) of |
| 57 | {ok, Assoc} -> |
| 58 | L#m3ua_state{sctp_assoc_id = Assoc#sctp_assoc_change.assoc_id}; |
| 59 | {error, Error } -> |
| 60 | reconnect_sctp(L) |
| 61 | end. |
| 62 | |
| 63 | init(InitOpts) -> |
Harald Welte | 8a0ab00 | 2011-04-03 22:16:12 +0200 | [diff] [blame^] | 64 | OpenOptsBase = [{active, once}, {reuseaddr, true}], |
| 65 | LocalPort = proplists:get_value(sctp_local_port, InitOpts), |
| 66 | case LocalPort of |
| 67 | undefined -> |
| 68 | OpenOpts = OpenOptsBase; |
| 69 | _ -> |
| 70 | OpenOpts = OpenOptsBase ++ [{port, LocalPort}] |
| 71 | end, |
| 72 | {ok, SctpSock} = gen_sctp:open(OpenOpts), |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 73 | LoopDat = #m3ua_state{role = asp, sctp_sock = SctpSock, |
| 74 | user_pid = proplists:get_value(user_pid, InitOpts), |
| 75 | sctp_remote_ip = proplists:get_value(sctp_remote_ip, InitOpts), |
Harald Welte | 8a0ab00 | 2011-04-03 22:16:12 +0200 | [diff] [blame^] | 76 | sctp_remote_port = proplists:get_value(sctp_remote_port, InitOpts), |
| 77 | sctp_local_port = LocalPort}, |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 78 | LoopDat2 = reconnect_sctp(LoopDat), |
| 79 | {ok, asp_down, LoopDat2}. |
| 80 | |
| 81 | % Helper function to send data to the SCTP peer |
| 82 | send_sctp_to_peer(LoopDat, PktData) when is_binary(PktData) -> |
| 83 | #m3ua_state{sctp_sock = Sock, sctp_assoc_id = Assoc} = LoopDat, |
| 84 | SndRcvInfo = #sctp_sndrcvinfo{assoc_id = Assoc, ppid = 3, stream = 0}, |
| 85 | gen_sctp:send(Sock, SndRcvInfo, PktData); |
| 86 | |
| 87 | % same as above, but for un-encoded #m3ua_msg{} |
| 88 | send_sctp_to_peer(LoopDat, M3uaMsg) when is_record(M3uaMsg, m3ua_msg) -> |
| 89 | MsgBin = m3ua_codec:encode_m3ua_msg(M3uaMsg), |
| 90 | send_sctp_to_peer(LoopDat, MsgBin). |
| 91 | |
| 92 | % helper to send one of the up/down/act/inact management messages + start timer |
| 93 | send_msg_start_tack(LoopDat, State, MsgClass, MsgType, Params) -> |
| 94 | % generate and send the respective message |
| 95 | Msg = #m3ua_msg{version = 1, msg_class = MsgClass, msg_type = MsgType, payload = Params}, |
| 96 | send_sctp_to_peer(LoopDat, Msg), |
| 97 | % start T(ack) timer and wait for ASP_UP_ACK |
| 98 | Tack = timer:apply_after(?T_ACK_TIMEOUT, gen_fsm, send_event, |
| 99 | [self(), {timer_expired, t_ack, {MsgClass, MsgType, Params}}]), |
| 100 | {next_state, State, LoopDat#m3ua_state{t_ack = Tack}}. |
| 101 | |
| 102 | |
| 103 | handle_event(Msg = #m3ua_msg{msg_class = ?M3UA_MSGC_ASPSM, |
| 104 | msg_type = ?M3UA_MSGT_ASPSM_BEAT}, State, LoopDat) -> |
| 105 | % Send BEAT_ACK using the same payload as the BEAT msg |
| 106 | send_sctp_to_peer(LoopDat, Msg#m3ua_msg{msg_type = ?M3UA_MSGT_ASPSM_BEAT_ACK}), |
| 107 | {next_state, State, LoopDat}; |
| 108 | |
| 109 | handle_event({sctp, Socket, _RemoteIp, _RemotePort, {ANC, SAC}}, |
| 110 | _State, LoopDat) when is_record(SAC, sctp_assoc_change) -> |
| 111 | io:format("SCTP Assoc Change ~p ~p~n", [ANC, SAC]), |
| 112 | #sctp_assoc_change{state = SacState, outbound_streams = OutStreams, |
| 113 | inbound_streams = InStreams, assoc_id = AssocId} = SAC, |
| 114 | case SacState of |
| 115 | comm_up -> |
| 116 | % FIXME: primmitive to the user |
| 117 | LoopDat2 = LoopDat; |
| 118 | comm_lost -> |
| 119 | LoopDat2 = reconnect_sctp(LoopDat); |
| 120 | addr_unreachable -> |
| 121 | LoopDat2 = reconnect_sctp(LoopDat) |
| 122 | end, |
| 123 | inet:setopts(Socket, [{active, once}]), |
| 124 | {next_state, asp_down, LoopDat2}; |
| 125 | |
| 126 | handle_event({sctp, Socket, _RemoteIp, _RemotePort, {[Anc], Data}}, State, LoopDat) -> |
| 127 | io:format("SCTP rx data: ~p ~p~n", [Anc, Data]), |
| 128 | % FIXME: process incoming SCTP data |
| 129 | inet:setopts(Socket, [{active, once}]), |
| 130 | {next_state, State, LoopDat}; |
| 131 | |
| 132 | handle_event({sctp, Socket, RemoteIp, RemotePort, {_Anc, Data}}, _State, LoopDat) |
| 133 | when is_record(Data, sctp_shutdown_event) -> |
| 134 | io:format("SCTP remote ~p:~p shutdown~n", [RemoteIp, RemotePort]), |
| 135 | inet:setopts(Socket, [{active, once}]), |
| 136 | {next_state, asp_down, LoopDat}. |
| 137 | |
| 138 | |
| 139 | |
| 140 | asp_down(#primitive{subsystem = 'M', gen_name = 'ASP_UP', |
| 141 | spec_name = request, parameters = Params}, LoopDat) -> |
| 142 | send_msg_start_tack(LoopDat, asp_down, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPUP, Params); |
| 143 | asp_down({timer_expired, t_ack, {?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPUP, Params}}, LoopDat) -> |
| 144 | send_msg_start_tack(LoopDat, asp_down, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPUP, Params); |
| 145 | |
| 146 | asp_down(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPSM, |
| 147 | msg_type = ?M3UA_MSGT_ASPSM_ASPUP_ACK}, LoopDat) -> |
| 148 | % transition into ASP_INACTIVE |
| 149 | {next_state, asp_inactive, LoopDat}. |
| 150 | |
| 151 | |
| 152 | |
| 153 | asp_inactive(#primitive{subsystem = 'M', gen_name = 'ASP_ACTIVATE', |
| 154 | spec_name = request, parameters = Params}, LoopDat) -> |
| 155 | send_msg_start_tack(LoopDat, asp_inactive, ?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPAC, Params); |
| 156 | |
| 157 | asp_inactive({timer_expired, t_ack, {?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPAC, Params}}, LoopDat) -> |
| 158 | send_msg_start_tack(LoopDat, asp_inactive, ?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPAC, Params); |
| 159 | |
| 160 | asp_inactive(#primitive{subsystem = 'M', gen_name = 'ASP_DOWN', |
| 161 | spec_name = request, parameters = Params}, LoopDat) -> |
| 162 | send_msg_start_tack(LoopDat, asp_inactive, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params); |
| 163 | |
| 164 | asp_inactive({timer_expired, t_ack, {?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params}}, LoopDat) -> |
| 165 | send_msg_start_tack(LoopDat, asp_inactive, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params); |
| 166 | |
| 167 | asp_inactive(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPTM, |
| 168 | msg_type = ?M3UA_MSGT_ASPTM_ASPAC_ACK}, LoopDat) -> |
| 169 | % transition into ASP_ACTIVE |
| 170 | % FIXME: signal this to the user |
| 171 | {next_state, asp_active, LoopDat}; |
| 172 | |
| 173 | asp_inactive(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPSM, |
| 174 | msg_type = ?M3UA_MSGT_ASPSM_ASPDN_ACK}, LoopDat) -> |
| 175 | % transition into ASP_DOWN |
| 176 | % FIXME: signal this to the user |
| 177 | {next_state, asp_down, LoopDat}. |
| 178 | |
| 179 | |
| 180 | |
| 181 | asp_active(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPSM, |
| 182 | msg_type = ?M3UA_MSGT_ASPSM_ASPDN_ACK}, LoopDat) -> |
| 183 | % transition into ASP_DOWN |
| 184 | % FIXME: signal this to the user |
| 185 | {next_state, asp_down, LoopDat}; |
| 186 | |
| 187 | asp_active(#m3ua_msg{msg_class = ?M3UA_MSGC_ASPTM, |
| 188 | msg_type = ?M3UA_MSGT_ASPTM_ASPIA_ACK}, LoopDat) -> |
| 189 | % transition into ASP_INACTIVE |
| 190 | % FIXME: signal this to the user |
| 191 | {next_state, asp_inactive, LoopDat}; |
| 192 | |
| 193 | asp_active(#primitive{subsystem = 'M', gen_name = 'ASP_DOWN', |
| 194 | spec_name = request, parameters = Params}, LoopDat) -> |
| 195 | send_msg_start_tack(LoopDat, asp_active, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params); |
| 196 | |
| 197 | asp_active({timer_expired, t_ack, {?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params}}, LoopDat) -> |
| 198 | send_msg_start_tack(LoopDat, asp_active, ?M3UA_MSGC_ASPSM, ?M3UA_MSGT_ASPSM_ASPDN, Params); |
| 199 | |
| 200 | asp_active(#primitive{subsystem = 'M', gen_name = 'ASP_INACTIVE', |
| 201 | spec_name = request, parameters = Params}, LoopDat) -> |
| 202 | send_msg_start_tack(LoopDat, asp_active, ?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPIA, Params); |
| 203 | |
| 204 | asp_active({timer_expired, t_ack, {?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPIA, Params}}, LoopDat) -> |
| 205 | send_msg_start_tack(LoopDat, asp_active, ?M3UA_MSGC_ASPTM, ?M3UA_MSGT_ASPTM_ASPIA, Params); |
| 206 | |
| 207 | asp_active(#primitive{subsystem = 'MTP', gen_name = 'TRANSFER', |
| 208 | spec_name = request, parameters = Params}, LoopDat) -> |
| 209 | % Send message to remote peer |
| 210 | OptList = [{?M3UA_IEI_PROTOCOL_DATA, Params}], |
| 211 | Msg = #m3ua_msg{version = 1, msg_class = ?M3UA_MSGC_TRANSFER, |
| 212 | msg_type = ?M3UA_MSGT_XFR_DATA, |
| 213 | payload = OptList}, |
| 214 | send_sctp_to_peer(LoopDat, Msg), |
| 215 | {next_state, asp_active, LoopDat}; |
| 216 | asp_active(#m3ua_msg{version = 1, msg_class = ?M3UA_MSGC_TRANSFER, |
| 217 | msg_type = ?M3UA_MSGT_XFR_DATA, payload = Params}, LoopDat) -> |
| 218 | % FIXME: Send primitive to the user |
| 219 | {next_state, asp_active, LoopDat}. |