summaryrefslogtreecommitdiffstats
path: root/src/ss7_link_ipa_client.erl
blob: e4db2ab433688b3a05a5c6b885690a1ce1cacb6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
% Osmocom adaptor to interface the IPA core with osmo_ss7

% (C) 2011 by Harald Welte <laforge@gnumonks.org>
%
% All Rights Reserved
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU Affero General Public License as
% published by the Free Software Foundation; either version 3 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU Affero General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.

-module(ss7_link_ipa_client).
-author('Harald Welte <laforge@gnumonks.org>').
-behavior(gen_server).

-include_lib("osmo_ss7/include/osmo_util.hrl").
%-include_lib("osmo_ss7/include/ipa.hrl").
-include_lib("osmo_ss7/include/sccp.hrl").
-include_lib("osmo_ss7/include/osmo_ss7.hrl").
-include_lib("osmo_ss7/include/mtp3.hrl").

-export([start_link/1, init/1]).

-export([handle_cast/2, handle_info/2]).

-record(loop_dat, {
	 ipa_pid,
	 socket,
	 link
	}).

start_link(Args) ->
	gen_server:start_link(?MODULE, Args, []).

init(L = #sigtran_link{type = ipa_client, name = Name, linkset_name = LinksetName,
			sls = Sls}) ->
	% start the IPA link to the SG
	ok = ss7_links:register_link(LinksetName, Sls, Name),
	{ok, LoopDat2} = reconnect(#loop_dat{link = L}),
	{ok, LoopDat2}.

handle_info({ipa_closed, {_Sock, _Stream}}, LoopDat) ->
	set_link_state(LoopDat, down),
	{ok, LoopDat2} = reconnect(LoopDat),
	{noreply, LoopDat2}.

handle_cast(#primitive{subsystem='MTP', gen_name='TRANSFER', spec_name=request,
		       parameters = #mtp3_msg{service_ind = ?MTP3_SERV_SCCP,
		       			    payload = Data}}, LoopDat) ->
	#loop_dat{socket = Socket, ipa_pid = Pid} = LoopDat,
	Pid ! {ipa_send, Socket, 253, Data},
	{noreply, LoopDat}.

reconnect(LoopDat = #loop_dat{link=Link}) ->
	#sigtran_link{local = Local, remote = Remote} = Link,
	#sigtran_peer{ip = LocalIp, port = LocalPort, point_code = LocalPc} = Local,
	#sigtran_peer{ip = RemoteIp, port = RemotePort, point_code = RemotePc} = Remote,
	case ipa_proto:connect(RemoteIp, RemotePort, [], 10000) of
		{ok, {Socket, IpaPid}} ->
			set_link_state(LoopDat, up),
			Mtp3Label = #mtp3_routing_label{sig_link_sel=0, origin_pc = RemotePc, dest_pc = LocalPc},
			ipa_proto:register_stream(Socket, 253, {callback_fn, fun ipa_tx_to_sccp/4, [Mtp3Label]}),
			set_link_state(LoopDat, active),
			ipa_proto:unblock(Socket),
			{ok, LoopDat#loop_dat{ipa_pid=IpaPid, socket=Socket}};
		{error, Reason} ->
			io:format("Reconnecting TCP (~w)~n", [Reason]),
			reconnect(LoopDat)
	end.

set_link_state(#loop_dat{link = #sigtran_link{linkset_name = LinksetName, sls = Sls}}, State) ->
	ss7_links:set_link_state(LinksetName, Sls, State).

% Callback that we pass to the ipa_proto, which it will call when it wants to
% send a primitive up the stack to SCCP
ipa_tx_to_sccp(_Socket, 253, Data, [Mtp3Label]) ->
	ss7_links:mtp3_rx(#mtp3_msg{service_ind=?MTP3_SERV_SCCP,
				    routing_label=Mtp3Label, payload=Data}).