Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 1 | % Osmocom adaptor to interface the IPA core with osmo_ss7 |
Harald Welte | c6e466e | 2011-10-10 14:03:50 +0200 | [diff] [blame] | 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(ss7_link_ipa_client). |
| 21 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 22 | -behavior(gen_server). |
| 23 | |
| 24 | -include_lib("osmo_ss7/include/osmo_util.hrl"). |
| 25 | %-include_lib("osmo_ss7/include/ipa.hrl"). |
| 26 | -include_lib("osmo_ss7/include/sccp.hrl"). |
| 27 | -include_lib("osmo_ss7/include/osmo_ss7.hrl"). |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 28 | -include_lib("osmo_ss7/include/mtp3.hrl"). |
Harald Welte | c6e466e | 2011-10-10 14:03:50 +0200 | [diff] [blame] | 29 | |
| 30 | -export([start_link/1, init/1]). |
| 31 | |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 32 | -export([handle_cast/2, handle_info/2]). |
Harald Welte | c6e466e | 2011-10-10 14:03:50 +0200 | [diff] [blame] | 33 | |
| 34 | -record(loop_dat, { |
| 35 | ipa_pid, |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 36 | socket, |
Harald Welte | c6e466e | 2011-10-10 14:03:50 +0200 | [diff] [blame] | 37 | link |
| 38 | }). |
| 39 | |
| 40 | start_link(Args) -> |
| 41 | gen_server:start_link(?MODULE, Args, []). |
| 42 | |
| 43 | init(L = #sigtran_link{type = ipa_client, name = Name, linkset_name = LinksetName, |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 44 | sls = Sls}) -> |
| 45 | % start the IPA link to the SG |
| 46 | ok = ss7_links:register_link(LinksetName, Sls, Name), |
| 47 | {ok, LoopDat2} = reconnect(#loop_dat{link = L}), |
| 48 | {ok, LoopDat2}. |
| 49 | |
| 50 | handle_info({ipa_closed, {_Sock, _Stream}}, LoopDat) -> |
| 51 | set_link_state(LoopDat, down), |
| 52 | {ok, LoopDat2} = reconnect(LoopDat), |
| 53 | {no_reply, LoopDat2}. |
| 54 | |
| 55 | handle_cast(#primitive{subsystem='MTP', gen_name='TRANSFER', spec_name=request, |
| 56 | parameters = #mtp3_msg{service_ind = ?MTP3_SERV_SCCP, |
| 57 | payload = Data}}, LoopDat) -> |
| 58 | #loop_dat{socket = Socket, ipa_pid = Pid} = LoopDat, |
| 59 | Pid ! {ipa_send, Socket, 253, Data}, |
| 60 | {no_reply, LoopDat}. |
| 61 | |
| 62 | reconnect(LoopDat = #loop_dat{link=Link}) -> |
| 63 | #sigtran_link{local = Local, remote = Remote} = Link, |
Harald Welte | c6e466e | 2011-10-10 14:03:50 +0200 | [diff] [blame] | 64 | #sigtran_peer{ip = LocalIp, port = LocalPort} = Local, |
| 65 | #sigtran_peer{ip = RemoteIp, port = RemotePort} = Remote, |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 66 | case ipa_proto:connect(RemoteIp, RemotePort, [], 10000) of |
| 67 | {ok, {Socket, IpaPid}} -> |
| 68 | set_link_state(LoopDat, up), |
| 69 | ipa_proto:register_stream(Socket, 253, {callback_fn, fun ipa_tx_to_sccp/4, []}), |
Harald Welte | 2968ef8 | 2012-01-23 23:57:21 +0100 | [diff] [blame^] | 70 | set_link_state(LoopDat, active), |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 71 | ipa_proto:unblock(Socket), |
| 72 | {ok, LoopDat#loop_dat{ipa_pid=IpaPid, socket=Socket}}; |
| 73 | {error, Reason} -> |
| 74 | io:format("Reconnecting TCP (~w)~n", [Reason]), |
| 75 | reconnect(LoopDat) |
| 76 | end. |
Harald Welte | c6e466e | 2011-10-10 14:03:50 +0200 | [diff] [blame] | 77 | |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 78 | set_link_state(#loop_dat{link = #sigtran_link{linkset_name = LinksetName, sls = Sls}}, State) -> |
| 79 | ss7_links:set_link_state(LinksetName, Sls, State). |
Harald Welte | c6e466e | 2011-10-10 14:03:50 +0200 | [diff] [blame] | 80 | |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 81 | % Callback that we pass to the ipa_proto, which it will call when it wants to |
Harald Welte | c6e466e | 2011-10-10 14:03:50 +0200 | [diff] [blame] | 82 | % send a primitive up the stack to SCCP |
Harald Welte | 5cde762 | 2012-01-23 23:18:28 +0100 | [diff] [blame] | 83 | ipa_tx_to_sccp(_Socket, 253, Data, _Args) -> |
| 84 | osmo_ss7:mtp3_rx(#mtp3_msg{service_ind=?MTP3_SERV_SCCP, payload=Data}). |