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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
% SCCP routing control procedures (SCRC)
% (C) 2010-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(sccp_scrc).
-behaviour(gen_fsm).
-export([start_link/1, init/1, terminate/3, idle/2]).
-include_lib("osmo_ss7/include/osmo_util.hrl").
-include_lib("osmo_ss7/include/sccp.hrl").
-include_lib("osmo_ss7/include/mtp3.hrl").
-record(scrc_state, {
scoc_conn_ets,
next_local_ref,
user_pid % pid() of the user process
}).
% TODO: Integrate with proper SCCP routing / GTT implementation
tx_prim_to_local_ref(Prim, LocalRef) ->
% determine the Pid to which the primitive must be sent
ConnTable = get(scoc_by_ref),
case ets:lookup(ConnTable, LocalRef) of
[{LocalRef, ScocPid}] ->
gen_fsm:send_event(ScocPid, Prim);
_ ->
io:format("Primitive ~p for unknown local reference ~p~n",
[Prim, LocalRef])
end.
start_link(InitData) ->
% make sure to store the Pid of the caller in the scrc_state
gen_fsm:start_link({local, sccp_scrc}, sccp_scrc,
[{user_pid,self()}|InitData], [{debug, [trace]}]).
% gen_fsm init callback, called by start_link()
init(InitPropList) ->
io:format("SCRC Init PropList~p ~n", [InitPropList]),
UserPid = proplists:get_value(user_pid, InitPropList),
LoopData = #scrc_state{user_pid = UserPid, next_local_ref = 0},
TableRef = ets:new(scoc_by_ref, [set]),
put(scoc_by_ref, TableRef),
{ok, idle, LoopData}.
terminate(Reason, _State, _LoopDat) ->
io:format("SCRC: Terminating with reason ~p~n", [Reason]),
Tref = get(scoc_by_ref),
ets:delete(Tref),
ok.
% helper function to create new SCOC instance
spawn_new_scoc(LoopDat) ->
% create new SCOC instance
UserPid = LoopDat#scrc_state.user_pid,
% Compute the new local reference
LocalRef = LoopDat#scrc_state.next_local_ref + 1,
LoopDat1 = LoopDat#scrc_state{next_local_ref = LocalRef},
% generate proplist for SCRC initialization
ScocPropList = [{scrc_pid, self()}, {user_pid, UserPid}, {local_reference, LocalRef}],
{ok, ScocPid} = sccp_scoc:start_link(ScocPropList),
% insert SCOC instance in connection table
ConnTable = get(scoc_by_ref),
ets:insert_new(ConnTable, {LocalRef, ScocPid}),
{LoopDat1, ScocPid}.
is_cr_or_connless(SccpMsg) when is_record(SccpMsg, sccp_msg) ->
case SccpMsg of
#sccp_msg{msg_type = ?SCCP_MSGT_CR} ->
true;
_ ->
sccp_codec:is_connectionless(SccpMsg)
end.
% deliver message to local SCOC or SCLC
deliver_to_scoc_sclc(LoopDat, Msg) when is_record(Msg, sccp_msg) ->
case Msg of
% special handling for CR message here in SCRC
#sccp_msg{msg_type = ?SCCP_MSGT_CR} ->
% spawn a new SCOC instance/process
{LoopDat1, ScocPid} = spawn_new_scoc(LoopDat),
% send a RCOC-CONNECTING.ind primitive to the new SCOC fsm
UserPrim = osmo_util:make_prim('RCOC','CONNECTION', indication, Msg#sccp_msg.parameters),
io:format("Sending ~p to ~p~n", [UserPrim, ScocPid]),
gen_fsm:send_event(ScocPid, UserPrim),
LoopDat1;
% T(ias) expired on the other end of the connection
%#sccp_msg{msg_type = ?SCCP_MSGT_IT} ->
_ ->
IsConnLess = sccp_codec:is_connectionless(Msg),
case IsConnLess of
true ->
% it would be more proper to send them via SCLC ??
%gen_fsm:send(sccp_sclc, ??
UserPid = LoopDat#scrc_state.user_pid,
% FIXME: N-NOTICE.ind for NOTICE
UserPrim = osmo_util:make_prim('N','UNITDATA', indication, Msg),
UserPid ! {sccp, UserPrim};
false ->
% connection oriented messages need to go via SCOC instance
#sccp_msg{parameters = Opts} = Msg,
LocalRef = proplists:get_value(dst_local_ref, Opts),
ScocPrim = osmo_util:make_prim('RCOC', 'CONNECTION-MSG', indication, Msg),
case LocalRef of
undefined ->
% FIXME: send SCCP_MSGT_ERR
io:format("Conn-Msg to undefined ref ~p~n", [Msg]);
_ ->
tx_prim_to_local_ref(ScocPrim, LocalRef)
end
end,
LoopDat
end.
% N-CONNECT.req from user: spawn new SCOC and deliver primitive to it
idle(P = #primitive{subsystem = 'N', gen_name = 'CONNECT',
spec_name = request, parameters = Params}, LoopDat) ->
% Start new SCOC instance
{LoopDat1, ScocPid} = spawn_new_scoc(LoopDat),
% Deliver primitive to new SCOC instance
gen_fsm:send_event(ScocPid, P),
{next_state, idle, LoopDat1};
% N-UNITDATA.req from user (normally this is SCLC, but we don't have SCLC)
idle(P= #primitive{subsystem = 'N', gen_name = 'UNITDATA',
spec_name = request, parameters = Params}, LoopDat) ->
% User needs to specify: Protocol Class, Called Party, Calling Party, Data
% FIXME: implement XUDT / LUDT support
% encode the actual SCCP message
EncMsg = sccp_codec:encode_sccp_msgt(?SCCP_MSGT_UDT, Params),
% generate a MTP-TRANSFER.req primitive to the lower layer
send_mtp_transfer_down(LoopDat, EncMsg),
{next_state, idle, LoopDat};
% MTP-TRANSFER.ind from lower layer is passed into SCRC
idle(#primitive{subsystem = 'MTP', gen_name = 'TRANSFER',
spec_name = indication, parameters = Params}, LoopDat) ->
case sccp_routing:route_mtp3_sccp_in(Params) of
{remote} ->
% routing has taken care of it
LoopDat1 = LoopDat;
{local, SccpMsg, _} ->
LoopDat1 = deliver_to_scoc_sclc(LoopDat, SccpMsg)
end,
{next_state, idle, LoopDat1};
idle({sclc_scrc_connless_msg, SccpMsg}, LoopDat) ->
case sccp_routing:route_local_out(SccpMsg) of
{remote, SccpMsg2} ->
% FIXME: get to MTP-TRANSFER.req
LoopDat1 = LoopDat;
{error, _} ->
LoopDat1 = LoopDat;
{local, SccpMsg2} ->
LoopDat1 = deliver_to_scoc_sclc(LoopDat, SccpMsg2)
end,
{next_state, idle, LoopDat};
% connection oriented messages like N-DATA.req from user
idle(#primitive{subsystem = 'OCRC', gen_name = 'CONNECTION-MSG',
spec_name = request, parameters = Msg}, LoopDat) ->
% encode the actual SCCP message
EncMsg = sccp_codec:encode_sccp_msg(Msg),
% generate a MTP-TRANSFER.req primitive to the lower layer
send_mtp_transfer_down(LoopDat, EncMsg),
{next_state, idle, LoopDat};
% SCOC has received confirmation about new incoming connection from user
idle(#primitive{subsystem = 'OCRC', gen_name = 'CONNECTION',
spec_name = confirm, parameters = Params}, LoopDat) ->
% encode the actual SCCP message
EncMsg = sccp_codec:encode_sccp_msgt(?SCCP_MSGT_CC, Params),
% generate a MTP-TRANSFER.req primitive to the lower layer
send_mtp_transfer_down(LoopDat, EncMsg),
{next_state, idle, LoopDat};
% triggered by N-CONNECT.req from user to SCOC:
idle(#primitive{subsystem = 'OCRC', gen_name = 'CONNECTION',
spec_name = indication, parameters = Params}, LoopDat) ->
% encode the actual SCCP message
EncMsg = sccp_codec:encode_sccp_msgt(?SCCP_MSGT_CR, Params),
% generate a MTP-TRANSFER.req primitive to the lower layer
send_mtp_transfer_down(LoopDat, EncMsg),
{next_state, idle, LoopDat}.
send_mtp_transfer_down(LoopDat, EncMsg) ->
Rlbl = #mtp3_routing_label{sig_link_sel = 0, origin_pc = 123, dest_pc = 456},
Mtp3 = #mtp3_msg{network_ind = ?MTP3_NETIND_INTERNATIONAL,
service_ind = ?MTP3_SERV_SCCP,
routing_label = Rlbl, payload = EncMsg},
MtpPrim = #primitive{subsystem = 'MTP', gen_name = 'TRANSFER',
spec_name = request, parameters = Mtp3},
sccp_links:mtp3_tx(Mtp3).
|