blob: be087ec783802ad44420900c01c3c84e5ad325dd [file] [log] [blame]
Harald Welte876bf9d2018-01-21 19:28:59 +01001module MNCC_Emulation {
2
3/* MNCC Emulation, runs on top of MNCC_CodecPort. It multiplexes/demultiplexes
4 * the individual calls, so there can be separate TTCN-3 components handling
5 * each of the calls
6 *
7 * The MNCC_Emulation.main() function processes MNCC primitives from the MNCC
8 * socket via the MNCC_CodecPort, and dispatches them to the per-connection components.
9 *
10 * Outbound MNCC connections are initiated by sending a MNCC_Call_Req primitive
11 * to the component running the MNCC_Emulation.main() function.
12 *
13 * For each new inbound connections, the MnccOps.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 + MNCC, and first trigger a connection from BSC side in a
20 * component which then subsequently should also handle the MNCC emulation.
21 *
22 * Inbound Unit Data messages (such as are dispatched to the MnccOps.unitdata_cb() callback,
23 * which is registered with an argument to the main() function below.
24 *
25 * (C) 2018 by Harald Welte <laforge@gnumonks.org>
26 * All rights reserved.
27 *
28 * Released under the terms of GNU General Public License, Version 2 or
29 * (at your option) any later version.
Harald Welte34b5a952019-05-27 11:54:11 +020030 *
31 * SPDX-License-Identifier: GPL-2.0-or-later
Harald Welte876bf9d2018-01-21 19:28:59 +010032 */
33
34
35import from Osmocom_Types all;
36import from MNCC_CodecPort all;
37import from MNCC_Types all;
38import from UD_Types all;
39
Neels Hofmeyr5e3b5d92019-11-28 20:34:57 +010040modulepar {
Neels Hofmeyr1c891f22019-11-28 20:49:23 +010041 int mp_mncc_version := 6;
Neels Hofmeyr5e3b5d92019-11-28 20:34:57 +010042}
43
Harald Welte876bf9d2018-01-21 19:28:59 +010044/* General "base class" component definition, of which specific implementations
45 * derive themselves by means of the "extends" feature */
46type component MNCC_ConnHdlr {
47 /* ports towards MNCC Emulator core / call dispatchar */
48 port MNCC_Conn_PT MNCC;
49 port MNCCEM_PROC_PT MNCC_PROC;
50}
51
52/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
53type enumerated MNCC_Conn_Prim {
54 /* MNCC tell us that connection was released */
55 MNCC_CONN_PRIM_DISC_IND,
56 /* we tell MNCC to release connection */
57 MNCC_CONN_PRIM_DISC_REQ
58}
59
60type record MNCC_Conn_Req {
61 MNCC_PDU mncc
62}
63
64/* port between individual per-connection components and this dispatcher */
65type port MNCC_Conn_PT message {
66 inout MNCC_PDU, MNCC_Conn_Prim, MNCC_Conn_Req;
67} with { extension "internal" };
68
69
70/* represents a single MNCC call */
71type record ConnectionData {
72 /* reference to the instance of the per-connection component */
73 MNCC_ConnHdlr comp_ref,
74 integer mncc_call_id
75}
76
77type component MNCC_Emulation_CT {
78 /* UNIX DOMAIN socket on the bottom side, using primitives */
79 port MNCC_CODEC_PT MNCC;
80 /* MNCC port to the per-connection clients */
81 port MNCC_Conn_PT MNCC_CLIENT;
82
83 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
84 var ConnectionData MnccCallTable[16];
85
86 /* pending expected incoming connections */
87 var ExpectData MnccExpectTable[8];
88 /* procedure based port to register for incoming connections */
89 port MNCCEM_PROC_PT MNCC_PROC;
90
91 var integer g_mncc_ud_id;
92};
93
94private function f_call_id_known(uint32_t mncc_call_id)
95runs on MNCC_Emulation_CT return boolean {
96 var integer i;
97 for (i := 0; i < sizeof(MnccCallTable); i := i+1) {
98 if (MnccCallTable[i].mncc_call_id == mncc_call_id){
99 return true;
100 }
101 }
102 return false;
103}
104
105private function f_comp_known(MNCC_ConnHdlr client)
106runs on MNCC_Emulation_CT return boolean {
107 var integer i;
108 for (i := 0; i < sizeof(MnccCallTable); i := i+1) {
109 if (MnccCallTable[i].comp_ref == client) {
110 return true;
111 }
112 }
113 return false;
114}
115
116/* resolve component reference by connection ID */
117private function f_comp_by_call_id(uint32_t mncc_call_id)
118runs on MNCC_Emulation_CT return MNCC_ConnHdlr {
119 var integer i;
120 for (i := 0; i < sizeof(MnccCallTable); i := i+1) {
121 if (MnccCallTable[i].mncc_call_id == mncc_call_id) {
122 return MnccCallTable[i].comp_ref;
123 }
124 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200125 setverdict(fail, "MNCC Call table not found by MNCC Call ID ", mncc_call_id);
126 mtc.stop;
Harald Welte876bf9d2018-01-21 19:28:59 +0100127}
128
129/* resolve connection ID by component reference */
130private function f_call_id_by_comp(MNCC_ConnHdlr client)
131runs on MNCC_Emulation_CT return integer {
132 for (var integer i := 0; i < sizeof(MnccCallTable); i := i+1) {
133 if (MnccCallTable[i].comp_ref == client) {
134 return MnccCallTable[i].mncc_call_id;
135 }
136 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200137 setverdict(fail, "MNCC Call table not found by component ", client);
138 mtc.stop;
Harald Welte876bf9d2018-01-21 19:28:59 +0100139}
140
141private function f_gen_call_id()
142runs on MNCC_Emulation_CT return integer {
143 var uint32_t call_id;
144
145 do {
146 call_id := float2int(rnd()*4294967296.0);
147 } while (f_call_id_known(call_id) == true);
148
149 return call_id;
150}
151
Daniel Willmannff6abf02018-02-02 18:26:05 +0100152private function f_expect_table_init()
153runs on MNCC_Emulation_CT {
154 for (var integer i := 0; i < sizeof(MnccExpectTable); i := i+1) {
155 MnccExpectTable[i].dest_number := omit;
156 MnccExpectTable[i].vc_conn := null;
157 }
158}
159
Harald Welte876bf9d2018-01-21 19:28:59 +0100160private function f_call_table_init()
161runs on MNCC_Emulation_CT {
162 for (var integer i := 0; i < sizeof(MnccCallTable); i := i+1) {
163 MnccCallTable[i].comp_ref := null;
164 MnccCallTable[i].mncc_call_id := -1;
165 }
166}
167
168private function f_call_table_add(MNCC_ConnHdlr comp_ref, uint32_t mncc_call_id)
169runs on MNCC_Emulation_CT {
170 for (var integer i := 0; i < sizeof(MnccCallTable); i := i+1) {
171 if (MnccCallTable[i].mncc_call_id == -1) {
172 MnccCallTable[i].comp_ref := comp_ref;
173 MnccCallTable[i].mncc_call_id := mncc_call_id;
174 log("Added conn table entry ", i, comp_ref, mncc_call_id);
175 return;
176 }
177 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200178 testcase.stop("MNCC Call table full!");
Harald Welte876bf9d2018-01-21 19:28:59 +0100179}
180
181private function f_call_table_del(uint32_t mncc_call_id)
182runs on MNCC_Emulation_CT {
183 for (var integer i := 0; i < sizeof(MnccCallTable); i := i+1) {
184 if (MnccCallTable[i].mncc_call_id == mncc_call_id) {
185 log("Deleted conn table entry ", i,
186 MnccCallTable[i].comp_ref, mncc_call_id);
187 MnccCallTable[i].mncc_call_id := -1;
188 MnccCallTable[i].comp_ref := null;
189 return
190 }
191 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200192 setverdict(fail, "MNCC Call table attempt to delete non-existant ", mncc_call_id);
193 mtc.stop;
Harald Welte876bf9d2018-01-21 19:28:59 +0100194}
195
196
Harald Welteafec4712018-03-19 22:52:17 +0100197private function f_connect(charstring sock) runs on MNCC_Emulation_CT {
Harald Welte876bf9d2018-01-21 19:28:59 +0100198 var UD_connect_result res;
199 timer T := 5.0;
200
201 T.start;
202 MNCC.send(UD_connect:{sock, -1});
203 alt {
204 [] MNCC.receive(UD_connect_result:?) -> value res {
205 if (ispresent(res.result) and ispresent(res.result.result_code) and res.result.result_code == ERROR) {
206 setverdict(fail, "Error connecting to MNCC socket", res);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200207 mtc.stop;
Harald Welte876bf9d2018-01-21 19:28:59 +0100208 } else {
209 g_mncc_ud_id := res.id;
210 }
211 }
212 [] T.timeout {
213 setverdict(fail, "Timeout connecting to MNCC socket");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200214 mtc.stop;
Harald Welte876bf9d2018-01-21 19:28:59 +0100215 }
216 }
217}
218
Harald Welteafec4712018-03-19 22:52:17 +0100219private function f_listen(charstring sock) runs on MNCC_Emulation_CT {
220 var UD_listen_result res;
221 var UD_connected udc;
222 timer T := 5.0;
223
224 T.start;
225 MNCC.send(UD_listen:{sock});
226 alt {
227 [] MNCC.receive(UD_listen_result:?) -> value res {
228 if (ispresent(res.result) and ispresent(res.result.result_code) and res.result.result_code == ERROR) {
229 setverdict(fail, "Error listening to MNCC socket", res);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200230 mtc.stop;
Harald Welteafec4712018-03-19 22:52:17 +0100231 } else {
232 g_mncc_ud_id := res.id;
233 }
234 }
235 [] T.timeout {
236 setverdict(fail, "Timeout listening to MNCC socket");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200237 mtc.stop;
Harald Welteafec4712018-03-19 22:52:17 +0100238 }
239 }
240
241 T.start;
242 alt {
243 [] MNCC.receive(UD_connected:?) -> value udc {
244 g_mncc_ud_id := res.id;
245 }
246 [] T.timeout {
247 setverdict(fail, "Timeout waiting for MNCC connection");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200248 mtc.stop;
Harald Welteafec4712018-03-19 22:52:17 +0100249 }
250 }
251}
252
Harald Welte876bf9d2018-01-21 19:28:59 +0100253/* call-back type, to be provided by specific implementation; called when new SCCP connection
254 * arrives */
255type function MnccCreateCallback(MNCC_PDU conn_ind, charstring id)
256runs on MNCC_Emulation_CT return MNCC_ConnHdlr;
257
258type function MnccUnitdataCallback(MNCC_PDU mncc)
259runs on MNCC_Emulation_CT return template MNCC_PDU;
260
261type record MnccOps {
262 MnccCreateCallback create_cb,
263 MnccUnitdataCallback unitdata_cb
264}
265
Harald Welteafec4712018-03-19 22:52:17 +0100266function main(MnccOps ops, charstring id, charstring sock, boolean role_server := false)
267runs on MNCC_Emulation_CT {
Harald Welte876bf9d2018-01-21 19:28:59 +0100268
Harald Welteafec4712018-03-19 22:52:17 +0100269 if (role_server) {
270 f_listen(sock);
Neels Hofmeyr5e3b5d92019-11-28 20:34:57 +0100271 MNCC.send(t_SD_MNCC(g_mncc_ud_id, ts_MNCC_HELLO(version := mp_mncc_version)));
Harald Welteafec4712018-03-19 22:52:17 +0100272 } else {
273 f_connect(sock);
274 }
Daniel Willmannff6abf02018-02-02 18:26:05 +0100275 f_expect_table_init();
Harald Welte876bf9d2018-01-21 19:28:59 +0100276 f_call_table_init();
277
278 while (true) {
279 var MNCC_send_data sd;
280 var MNCC_Conn_Req creq;
281 var MNCC_ConnHdlr vc_conn;
282 var MNCC_PDU mncc;
283 var MNCC_ConnHdlr vc_hdlr;
284 var charstring dest_nr;
285
286 alt {
287 /* MNCC -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
288 [] MNCC.receive(t_SD_MNCC_MSGT(g_mncc_ud_id, MNCC_SOCKET_HELLO)) -> value sd {
289 /* Connectionless Procedures like HELLO */
290 var template MNCC_PDU resp;
291 resp := ops.unitdata_cb.apply(sd.data);
292 if (isvalue(resp)) {
293 MNCC.send(t_SD_MNCC(g_mncc_ud_id, resp));
294 }
295 }
296
297 /* MNCC -> Client: Release Indication / confirmation */
298 [] MNCC.receive(t_SD_MNCC_MSGT(g_mncc_ud_id, (MNCC_REL_IND, MNCC_REL_CNF))) -> value sd {
299 var uint32_t call_id := f_mncc_get_call_id(sd.data);
300 /* forward to respective client */
301 vc_conn := f_comp_by_call_id(call_id);
302 MNCC_CLIENT.send(sd.data) to vc_conn;
303 /* remove from call table */
304 f_call_table_del(call_id);
305 }
306
307 /* MNCC -> Client: call related messages */
308 [] MNCC.receive(t_SD_MNCC_MSGT(g_mncc_ud_id, ?)) -> value sd {
309 var uint32_t call_id := f_mncc_get_call_id(sd.data);
310
311 if (f_call_id_known(call_id)) {
312 vc_conn := f_comp_by_call_id(call_id);
313 MNCC_CLIENT.send(sd.data) to vc_conn;
314 } else {
315 /* TODO: Only accept this for SETUP.req? */
316 vc_conn := ops.create_cb.apply(sd.data, id)
317 /* store mapping between client components and SCCP connectionId */
318 f_call_table_add(vc_conn, call_id);
319 /* handle user payload */
320 MNCC_CLIENT.send(sd.data) to vc_conn;
321 }
322 }
323
324 /* Client -> MNCC Socket: RELEASE.ind or RELEASE.cnf: forward + drop call table entry */
325 [] MNCC_CLIENT.receive(MNCC_PDU:{msg_type := (MNCC_REL_IND, MNCC_REL_CNF), u:=?}) -> value mncc sender vc_conn {
326 var integer call_id := f_call_id_by_comp(vc_conn);
327 /* forward to MNCC socket */
328 MNCC.send(t_SD_MNCC(g_mncc_ud_id, mncc));
329 /* remove from call table */
330 f_call_table_del(call_id);
331 }
332
Harald Welteafec4712018-03-19 22:52:17 +0100333 /* Client -> MNCC Socket: Normal message */
334 [] MNCC_CLIENT.receive(MNCC_PDU:?) -> value mncc sender vc_conn {
335 if (mncc.msg_type == MNCC_SETUP_REQ and not role_server) {
336 /* ConnHdlr -> MNCC Server: SETUP.req: add to call table */
337 f_call_table_add(vc_conn, f_mncc_get_call_id(mncc));
338 } else if (mncc.msg_type == MNCC_SETUP_IND and role_server) {
339 /* ConnHdlr -> MNCC Client: SETUP.ind: add to call table */
340 f_call_table_add(vc_conn, f_mncc_get_call_id(mncc));
341 }
Harald Welte812f7a42018-01-27 00:49:18 +0100342 /* forward to MNCC socket */
343 MNCC.send(t_SD_MNCC(g_mncc_ud_id, mncc));
344 }
345
Harald Welte876bf9d2018-01-21 19:28:59 +0100346 [] MNCC_CLIENT.receive(MNCC_PDU:?) -> value mncc sender vc_conn {
347 /* forward to MNCC socket */
348 MNCC.send(t_SD_MNCC(g_mncc_ud_id, mncc));
349 }
350
351
352 /* Client -> us: procedure call to register expect */
353 [] MNCC_PROC.getcall(MNCCEM_register:{?,?}) -> param(dest_nr, vc_hdlr) {
354 f_create_expect(dest_nr, vc_hdlr);
Harald Weltee32ad992018-05-31 22:17:46 +0200355 MNCC_PROC.reply(MNCCEM_register:{dest_nr, vc_hdlr}) to vc_hdlr;
Harald Welte876bf9d2018-01-21 19:28:59 +0100356 }
357
358 }
359 }
360}
361
362private function f_mgcp_ep_extract_cic(charstring inp) return integer {
363 var charstring local_part := regexp(inp, "(*)@*", 0);
364 return hex2int(str2hex(local_part));
365
366}
367
368/***********************************************************************
369 * "Expect" Handling (mapping for expected incoming MNCC calls from IUT)
370 ***********************************************************************/
371
372/* data about an expected future incoming connection */
373type record ExpectData {
374 /* destination number based on which we can match it */
375 charstring dest_number optional,
376 /* component reference for this connection */
377 MNCC_ConnHdlr vc_conn
378}
379
380/* procedure based port to register for incoming calls */
381signature MNCCEM_register(in charstring dest_nr, in MNCC_ConnHdlr hdlr);
382
383type port MNCCEM_PROC_PT procedure {
384 inout MNCCEM_register;
385} with { extension "internal" };
386
387/* CreateCallback that can be used as create_cb and will use the expectation table */
388function ExpectedCreateCallback(MNCC_PDU conn_ind, charstring id)
389runs on MNCC_Emulation_CT return MNCC_ConnHdlr {
390 var MNCC_ConnHdlr ret := null;
391 var charstring dest_number;
392 var integer i;
393
Harald Welte9edea882018-03-24 22:32:20 +0100394 if (not ischosen(conn_ind.u.signal) or
395 (conn_ind.msg_type != MNCC_SETUP_IND and conn_ind.msg_type != MNCC_SETUP_REQ)) {
396 setverdict(fail, "MNCC ExpectedCreateCallback needs MNCC_SETUP_{IND,REQ}");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200397 mtc.stop;
Harald Welte876bf9d2018-01-21 19:28:59 +0100398 return ret;
399 }
400 dest_number := conn_ind.u.signal.called.number;
401
402 for (i := 0; i < sizeof(MnccExpectTable); i:= i+1) {
403 if (not ispresent(MnccExpectTable[i].dest_number)) {
404 continue;
405 }
406 if (dest_number == MnccExpectTable[i].dest_number) {
407 ret := MnccExpectTable[i].vc_conn;
408 /* release this entry to be used again */
409 MnccExpectTable[i].dest_number := omit;
410 MnccExpectTable[i].vc_conn := null;
411 log("Found MnccExpect[", i, "] for ", dest_number, " handled at ", ret);
412 /* return the component reference */
413 return ret;
414 }
415 }
416 setverdict(fail, "Couldn't find MnccExpect for incoming call ", dest_number);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200417 mtc.stop;
Harald Welte876bf9d2018-01-21 19:28:59 +0100418 return ret;
419}
420
421/* server/emulation side function to create expect */
422private function f_create_expect(charstring dest_number, MNCC_ConnHdlr hdlr)
423runs on MNCC_Emulation_CT {
424 var integer i;
425 for (i := 0; i < sizeof(MnccExpectTable); i := i+1) {
426 if (not ispresent(MnccExpectTable[i].dest_number)) {
427 MnccExpectTable[i].dest_number := dest_number;
428 MnccExpectTable[i].vc_conn := hdlr;
429 log("Created MnccExpect[", i, "] for ", dest_number, " to be handled at ", hdlr);
430 return;
431 }
432 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200433 testcase.stop("No space left in MnccMnccExpectTable");
Harald Welte876bf9d2018-01-21 19:28:59 +0100434}
435
436/* client/conn_hdlr side function to use procedure port to create expect in emulation */
437function f_create_mncc_expect(charstring dest_number) runs on MNCC_ConnHdlr {
438 MNCC_PROC.call(MNCCEM_register:{dest_number, self}) {
439 [] MNCC_PROC.getreply(MNCCEM_register:{?,?}) {};
440 }
441}
442
443function DummyUnitdataCallback(MNCC_PDU mncc)
444runs on MNCC_Emulation_CT return template MNCC_PDU {
445 log("Ignoring MNCC ", mncc);
446 return omit;
447}
448
449}