blob: 3990b731bf3466d4660b77db967392af3282eb54 [file] [log] [blame]
Harald Welte0e5aad22018-01-21 14:00:41 +01001module GSUP_Emulation {
2
3/* GSUP Emulation, runs on top of IPA_Emulation. It multiplexes/demultiplexes
4 * the individual calls, so there can be separate TTCN-3 components handling
5 * each of the calls
6 *
7 * The GSUP_Emulation.main() function processes GSUP primitives from the IPA/GSUP
8 * socket via the IPA_Emulation, and dispatches them to the per-connection components.
9 *
10 * Outbound GSUP connections are initiated by sending a FIXME primitive
11 * to the component running the GSUP_Emulation.main() function.
12 *
13 * For each new inbound connections, the GsupOps.create_cb() is called. It can create
14 * or resolve a TTCN-3 component, and returns a component reference to which that inbound
15 * connection is routed/dispatched.
16 *
17 * If a pre-existing component wants to register to handle a future inbound call, it can
18 * do so by registering an "expect" with the expected destination phone number. This is e.g. useful
19 * if you are simulating BSC + HUL, and first trigger a connection from BSC side in a
20 * component which then subsequently should also handle the GSUP emulation.
21 *
22 * (C) 2018 by Harald Welte <laforge@gnumonks.org>
23 * All rights reserved.
24 *
25 * Released under the terms of GNU General Public License, Version 2 or
26 * (at your option) any later version.
27 */
28
29
30import from Osmocom_Types all;
31import from GSUP_Types all;
32import from IPA_Emulation all;
33
34/* General "base class" component definition, of which specific implementations
35 * derive themselves by means of the "extends" feature */
36type component GSUP_ConnHdlr {
37 /* ports towards GSUP Emulator core / call dispatchar */
38 port GSUP_Conn_PT GSUP;
39 port GSUPEM_PROC_PT GSUP_PROC;
40}
41
42/* port between individual per-connection components and this dispatcher */
43type port GSUP_Conn_PT message {
44 inout GSUP_PDU;
45} with { extension "internal" };
46
47
48/* represents a single GSUP call */
49type record ConnectionData {
50 /* reference to the instance of the per-connection component */
51 GSUP_ConnHdlr comp_ref,
52 charstring imsi
53}
54
55type component GSUP_Emulation_CT {
56 /* UNIX DOMAIN socket on the bottom side, using primitives */
57 port IPA_GSUP_PT GSUP;
58 /* GSUP port to the per-connection clients */
59 port GSUP_Conn_PT GSUP_CLIENT;
60
61 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
62 var ConnectionData GsupImsiTable[16];
63
64 /* pending expected incoming connections */
65 var ExpectData GsupExpectTable[8];
66 /* procedure based port to register for incoming connections */
67 port GSUPEM_PROC_PT GSUP_PROC;
68};
69
70private function f_imsi_known(charstring imsi)
71runs on GSUP_Emulation_CT return boolean {
72 var integer i;
73 for (i := 0; i < sizeof(GsupImsiTable); i := i+1) {
74 if (GsupImsiTable[i].imsi == imsi) {
75 return true;
76 }
77 }
78 return false;
79}
80
81private function f_comp_known(GSUP_ConnHdlr client)
82runs on GSUP_Emulation_CT return boolean {
83 var integer i;
84 for (i := 0; i < sizeof(GsupImsiTable); i := i+1) {
85 if (GsupImsiTable[i].comp_ref == client) {
86 return true;
87 }
88 }
89 return false;
90}
91
92/* resolve component reference by connection ID */
93private function f_comp_by_imsi(charstring imsi)
94runs on GSUP_Emulation_CT return GSUP_ConnHdlr {
95 var integer i;
96 for (i := 0; i < sizeof(GsupImsiTable); i := i+1) {
97 if (GsupImsiTable[i].imsi == imsi) {
98 return GsupImsiTable[i].comp_ref;
99 }
100 }
101 log("GSUP IMSI table not found by IMSI ", imsi);
102 setverdict(fail);
103 self.stop;
104}
105
106/* resolve connection ID by component reference */
107private function f_imsi_by_comp(GSUP_ConnHdlr client)
108runs on GSUP_Emulation_CT return charstring {
109 for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) {
110 if (GsupImsiTable[i].comp_ref == client) {
111 return GsupImsiTable[i].imsi;
112 }
113 }
114 log("GSUP IMSI table not found by component ", client);
115 setverdict(fail);
116 self.stop;
117}
118
119private function f_imsi_table_init()
120runs on GSUP_Emulation_CT {
121 for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) {
122 GsupImsiTable[i].comp_ref := null;
123 GsupImsiTable[i].imsi := "";
124 }
125}
126
Harald Welted95da562018-01-27 21:46:53 +0100127private function f_expect_table_init()
128runs on GSUP_Emulation_CT {
129 for (var integer i := 0; i < sizeof(GsupExpectTable); i := i+1) {
130 GsupExpectTable[i].vc_conn := null;
131 GsupExpectTable[i].imsi := omit;
132 }
133}
134
Harald Welte0e5aad22018-01-21 14:00:41 +0100135private function f_imsi_table_add(GSUP_ConnHdlr comp_ref, charstring imsi)
136runs on GSUP_Emulation_CT {
137 for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) {
138 if (GsupImsiTable[i].imsi == "") {
139 GsupImsiTable[i].comp_ref := comp_ref;
140 GsupImsiTable[i].imsi := imsi;
141 log("Added IMSI table entry ", i, comp_ref, imsi);
142 return;
143 }
144 }
145 log("GSUP IMSI table full!");
146 setverdict(fail);
147 self.stop;
148}
149
150private function f_imsi_table_del(charstring imsi)
151runs on GSUP_Emulation_CT {
152 for (var integer i := 0; i < sizeof(GsupImsiTable); i := i+1) {
153 if (GsupImsiTable[i].imsi == imsi) {
154 log("Deleted GSUP IMSI table entry ", i,
155 GsupImsiTable[i].comp_ref, imsi);
156 GsupImsiTable[i].imsi := "";
157 GsupImsiTable[i].comp_ref := null;
158 return
159 }
160 }
161 log("GSUP IMSI table attempt to delete non-existant ", imsi);
162 setverdict(fail);
163 self.stop;
164}
165
166
167/* call-back type, to be provided by specific implementation; called when new SCCP connection
168 * arrives */
169type function GsupCreateCallback(GSUP_PDU gsup, charstring id)
170runs on GSUP_Emulation_CT return GSUP_ConnHdlr;
171
172type record GsupOps {
173 GsupCreateCallback create_cb
174}
175
176function main(GsupOps ops, charstring id) runs on GSUP_Emulation_CT {
177
178 f_imsi_table_init();
Harald Welted95da562018-01-27 21:46:53 +0100179 f_expect_table_init();
Harald Welte0e5aad22018-01-21 14:00:41 +0100180
181 while (true) {
182 var GSUP_ConnHdlr vc_conn;
183 var GSUP_ConnHdlr vc_hdlr;
184 var GSUP_PDU gsup;
185 var charstring imsi;
186
187 alt {
188
189 [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_ID_ACK}) { repeat; }
190 [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_UP}) { repeat; }
191 [] GSUP.receive(ASP_IPA_Event:{up_down:=ASP_IPA_EVENT_DOWN}) {
192 setverdict(fail, "GSUP Connection Lost");
193 self.stop;
194 }
195
196 /* GSUP -> Client: call related messages */
197 [] GSUP.receive(GSUP_PDU:?) -> value gsup {
198 imsi := hex2str(gsup.ies[0].val.imsi);
199
200 if (f_imsi_known(imsi)) {
201 vc_conn := f_comp_by_imsi(imsi);
202 GSUP_CLIENT.send(gsup) to vc_conn;
203 } else {
204 /* TODO: Only accept this for SETUP.req? */
205 vc_conn := ops.create_cb.apply(gsup, id)
206 /* store mapping between client components and SCCP connectionId */
207 f_imsi_table_add(vc_conn, imsi);
208 /* handle user payload */
209 GSUP_CLIENT.send(gsup) to vc_conn;
210 }
211 }
212
213 [] GSUP.receive { repeat; }
214
215 /* Client -> GSUP Socket: Normal message */
216 [] GSUP_CLIENT.receive(GSUP_PDU:?) -> value gsup sender vc_conn {
217 /* forward to GSUP socket */
218 GSUP.send(gsup);
219 }
220
221
222 /* Client -> us: procedure call to register expect */
223 [] GSUP_PROC.getcall(GSUPEM_register:{?,?}) -> param(imsi, vc_hdlr) {
224 f_create_expect(imsi, vc_hdlr);
225 GSUP_PROC.reply(GSUPEM_register:{imsi, vc_hdlr});
226 }
227
228 }
229 }
230}
231
232/***********************************************************************
233 * "Expect" Handling (mapping for expected incoming GSUP calls from IUT)
234 ***********************************************************************/
235
236/* data about an expected future incoming connection */
237type record ExpectData {
238 /* destination number based on which we can match it */
239 charstring imsi optional,
240 /* component reference for this connection */
241 GSUP_ConnHdlr vc_conn
242}
243
244/* procedure based port to register for incoming calls */
245signature GSUPEM_register(in charstring imsi, in GSUP_ConnHdlr hdlr);
246
247type port GSUPEM_PROC_PT procedure {
248 inout GSUPEM_register;
249} with { extension "internal" };
250
251/* CreateCallback that can be used as create_cb and will use the expectation table */
252function ExpectedCreateCallback(GSUP_PDU gsup, charstring id)
253runs on GSUP_Emulation_CT return GSUP_ConnHdlr {
254 var GSUP_ConnHdlr ret := null;
255 var charstring imsi;
256 var integer i;
257
258 imsi := hex2str(gsup.ies[0].val.imsi);
259
260 for (i := 0; i < sizeof(GsupExpectTable); i:= i+1) {
261 if (not ispresent(GsupExpectTable[i].imsi)) {
262 continue;
263 }
264 if (imsi == GsupExpectTable[i].imsi) {
265 ret := GsupExpectTable[i].vc_conn;
266 /* release this entry to be used again */
267 GsupExpectTable[i].imsi := omit;
268 GsupExpectTable[i].vc_conn := null;
269 log("Found GsupExpect[", i, "] for ", imsi, " handled at ", ret);
270 /* return the component reference */
271 return ret;
272 }
273 }
274 setverdict(fail, "Couldn't find GsupExpect for incoming imsi ", imsi);
275 return ret;
276}
277
278/* server/emulation side function to create expect */
279private function f_create_expect(charstring imsi, GSUP_ConnHdlr hdlr)
280runs on GSUP_Emulation_CT {
281 var integer i;
282 for (i := 0; i < sizeof(GsupExpectTable); i := i+1) {
283 if (not ispresent(GsupExpectTable[i].imsi)) {
284 GsupExpectTable[i].imsi := imsi;
285 GsupExpectTable[i].vc_conn := hdlr;
286 log("Created GsupExpect[", i, "] for ", imsi, " to be handled at ", hdlr);
287 return;
288 }
289 }
290 setverdict(fail, "No space left in GsupExpectTable");
291}
292
293/* client/conn_hdlr side function to use procedure port to create expect in emulation */
294function f_create_gsup_expect(charstring imsi) runs on GSUP_ConnHdlr {
295 GSUP_PROC.call(GSUPEM_register:{imsi, self}) {
296 [] GSUP_PROC.getreply(GSUPEM_register:{?,?}) {};
297 }
298}
299
300}