blob: 153fb86aa09b4bfbe1fc511d59ba40eb7f002e08 [file] [log] [blame]
Harald Welte365f4ed2017-11-23 00:00:43 +01001module BSSMAP_Emulation {
2
Harald Welte35bb7162018-01-03 21:07:52 +01003/* BSSMAP Emulation, runs on top of BSSAP_CodecPort. It multiplexes/demultiplexes
4 * the individual connections, so there can be separate TTCN-3 components handling
5 * each of the connections.
6 *
7 * The BSSMAP_Emulation.main() function processes SCCP primitives from the SCCP
8 * stack via the BSSAP_CodecPort, and dispatches them to the per-connection components.
9 *
10 * Outbound BSSAP/SCCP connections are initiated by sending a BSSAP_Conn_Req primitive
11 * to the component running the BSSMAP_Emulation.main() function.
12 *
13 * For each new inbound connections, the BssmapOps.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 connection, it can
18 * do so by registering an "expect" with the expected Layer 3 (DTAP) payload. This is e.g. useful
19 * if you are simulating BTS + MSC, and first trigger a connection from BTS/RSL side in a
20 * component which then subsequently should also handle the MSC emulation.
21 *
22 * Inbound Unit Data messages (such as are dispatched to the BssmapOps.unitdata_cb() callback,
23 * which is registered with an argument to the main() function below.
24 *
25 * (C) 2017 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.
30 */
31
32
Harald Welteb3414b22017-11-23 18:22:10 +010033import from SCCP_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010034import from SCCPasp_Types all;
35import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +010036import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +010037import from BSSMAP_Templates all;
Harald Weltec82eef42017-11-24 20:40:12 +010038import from MGCP_Types all;
39import from MGCP_Templates all;
40import from IPA_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010041
42/* General "base class" component definition, of which specific implementations
43 * derive themselves by means of the "extends" feature */
44type component BSSAP_ConnHdlr {
45 /* port towards MSC Emulator core / SCCP connection dispatchar */
46 port BSSAP_Conn_PT BSSAP;
47}
48
49/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
50type enumerated BSSAP_Conn_Prim {
51 /* SCCP tell us that connection was released */
52 MSC_CONN_PRIM_DISC_IND,
53 /* we tell SCCP to release connection */
54 MSC_CONN_PRIM_DISC_REQ
55}
56
Harald Welteb3414b22017-11-23 18:22:10 +010057type record BSSAP_Conn_Req {
58 SCCP_PAR_Address addr_peer,
59 SCCP_PAR_Address addr_own,
60 PDU_BSSAP bssap
61}
62
Harald Welte365f4ed2017-11-23 00:00:43 +010063/* port between individual per-connection components and this dispatcher */
64type port BSSAP_Conn_PT message {
Harald Weltec82eef42017-11-24 20:40:12 +010065 inout PDU_BSSAP, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +010066} with { extension "internal" };
67
68
69/* represents a single BSSAP connection over SCCP */
70type record ConnectionData {
71 /* reference to the instance of the per-connection component */
72 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +010073 integer sccp_conn_id,
74 /* most recent MGCP transaction ID (Used on MSC side) */
75 MgcpTransId mgcp_trans_id optional,
76 /* CIC that has been used for voice of this channel (BSC side) */
77 integer cic optional
Harald Welte365f4ed2017-11-23 00:00:43 +010078}
79
80type component BSSMAP_Emulation_CT {
81 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +010082 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +010083 /* BSSAP port to the per-connection clients */
84 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +010085 /* MGCP port */
86 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +010087
88 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
89 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +010090
Harald Welte624f9632017-12-16 19:26:04 +010091 /* pending expected incoming connections */
92 var ExpectData ExpectTable[8];
93 /* procedure based port to register for incoming connections */
94 port BSSMAPEM_PROC_PT PROC;
95
Harald Weltebe620f62017-11-25 00:23:54 +010096 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +010097 var integer g_next_e1_ts := 1;
Harald Welte365f4ed2017-11-23 00:00:43 +010098};
99
Harald Welteb3414b22017-11-23 18:22:10 +0100100private function f_conn_id_known(integer sccp_conn_id)
101runs on BSSMAP_Emulation_CT return boolean {
102 var integer i;
103 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
104 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
105 return true;
106 }
107 }
108 return false;
109}
110
111private function f_comp_known(BSSAP_ConnHdlr client)
112runs on BSSMAP_Emulation_CT return boolean {
113 var integer i;
114 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
115 if (ConnectionTable[i].comp_ref == client) {
116 return true;
117 }
118 }
119 return false;
120}
Harald Welte365f4ed2017-11-23 00:00:43 +0100121
Harald Weltee98bb2e2017-11-29 12:09:48 +0100122private function f_cic_known(integer cic)
123runs on BSSMAP_Emulation_CT return boolean {
124 var integer i;
125 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
126 if (ConnectionTable[i].cic == cic) {
127 return true;
128 }
129 }
130 return false;
131}
132
Harald Welte365f4ed2017-11-23 00:00:43 +0100133/* resolve component reference by connection ID */
134private function f_comp_by_conn_id(integer sccp_conn_id)
135runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
136 var integer i;
137 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
138 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
139 return ConnectionTable[i].comp_ref;
140 }
141 }
142 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100143 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100144 self.stop;
145}
146
Harald Weltec82eef42017-11-24 20:40:12 +0100147/* resolve component reference by CIC */
148private function f_comp_by_mgcp_tid(MgcpTransId tid)
149runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
150 var integer i;
151 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
152 if (ConnectionTable[i].mgcp_trans_id == tid) {
153 return ConnectionTable[i].comp_ref;
154 }
155 }
156 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100157 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100158 self.stop;
159}
160
161private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
162runs on BSSMAP_Emulation_CT {
163 var integer i;
164 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
165 if (ConnectionTable[i].comp_ref == client) {
166 ConnectionTable[i].mgcp_trans_id := tid;
167 return;
168 }
169 }
170 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100171 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100172 self.stop;
173}
174
175private function f_comp_by_cic(integer cic)
176runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
177 var integer i;
178 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
179 if (ConnectionTable[i].cic == cic) {
180 return ConnectionTable[i].comp_ref;
181 }
182 }
183 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100184 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100185 self.stop;
186}
187
188private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
189runs on BSSMAP_Emulation_CT {
190 var integer i;
191 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
192 if (ConnectionTable[i].comp_ref == client) {
193 ConnectionTable[i].cic := cic;
194 return;
195 }
196 }
197 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100198 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100199}
200
Harald Welte365f4ed2017-11-23 00:00:43 +0100201/* resolve connection ID by component reference */
202private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
203runs on BSSMAP_Emulation_CT return integer {
204 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
205 if (ConnectionTable[i].comp_ref == client) {
206 return ConnectionTable[i].sccp_conn_id;
207 }
208 }
209 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100210 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100211 self.stop;
212}
213
Harald Welteb3414b22017-11-23 18:22:10 +0100214private function f_gen_conn_id()
215runs on BSSMAP_Emulation_CT return integer {
216 var integer conn_id;
217
218 do {
219 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
220 } while (f_conn_id_known(conn_id) == true);
221
222 return conn_id;
223}
224
Harald Welte365f4ed2017-11-23 00:00:43 +0100225private function f_conn_table_init()
226runs on BSSMAP_Emulation_CT {
227 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
228 ConnectionTable[i].comp_ref := null;
229 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100230 ConnectionTable[i].mgcp_trans_id := omit;
231 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100232 }
233}
234
235private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
236runs on BSSMAP_Emulation_CT {
237 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
238 if (ConnectionTable[i].sccp_conn_id == -1) {
239 ConnectionTable[i].comp_ref := comp_ref;
240 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100241 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100242 return;
243 }
244 }
245 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100246 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100247 self.stop;
248}
249
250private function f_conn_table_del(integer sccp_conn_id)
251runs on BSSMAP_Emulation_CT {
252 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
253 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100254 log("Deleted conn table entry ", i,
255 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100256 ConnectionTable[i].sccp_conn_id := -1;
257 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100258 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100259 }
260 }
261 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100262 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100263 self.stop;
264}
265
266/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100267private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100268runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100269 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100270
271 /* BSC Side: If this is an assignment command, store CIC */
272 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
273 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
274 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
275 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
276 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
277 f_comp_store_cic(client, cic);
278 }
279
Harald Welte365f4ed2017-11-23 00:00:43 +0100280 CLIENT.send(bssap) to client;
281}
282
283/* call-back type, to be provided by specific implementation; called when new SCCP connection
284 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100285type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100286runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
287
288type function BssmapUnitdataCallback(PDU_BSSAP bssap)
289runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
290
291type record BssmapOps {
292 BssmapCreateCallback create_cb,
293 BssmapUnitdataCallback unitdata_cb
294}
295
Harald Weltebe620f62017-11-25 00:23:54 +0100296function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100297
Harald Weltebe620f62017-11-25 00:23:54 +0100298 g_bssmap_id := id;
Harald Welte365f4ed2017-11-23 00:00:43 +0100299 f_conn_table_init();
300
301 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100302 var BSSAP_N_UNITDATA_ind ud_ind;
303 var BSSAP_N_CONNECT_ind conn_ind;
304 var BSSAP_N_CONNECT_cfm conn_cfm;
305 var BSSAP_N_DATA_ind data_ind;
306 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100307 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100308 var BSSAP_ConnHdlr vc_conn;
309 var PDU_BSSAP bssap;
Harald Weltec82eef42017-11-24 20:40:12 +0100310 var MgcpCommand mgcp_req;
311 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100312 var BSSAP_ConnHdlr vc_hdlr;
313 var octetstring l3_info;
Harald Welte365f4ed2017-11-23 00:00:43 +0100314
315 alt {
316 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100317 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100318 /* Connectionless Procedures like RESET */
319 var template PDU_BSSAP resp;
Harald Welte004f5fb2017-12-16 17:54:40 +0100320 resp := ops.unitdata_cb.apply(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100321 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100322 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
323 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100324 }
325 }
326
327 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100328 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100329 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100330 /* store mapping between client components and SCCP connectionId */
331 f_conn_table_add(vc_conn, conn_ind.connectionId);
332 /* handle user payload */
333 f_handle_userData(vc_conn, conn_ind.userData);
334 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100335 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100336 }
337
338 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100339 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100340 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100341 if (ispresent(data_ind.userData)) {
342 f_handle_userData(vc_conn, data_ind.userData);
343 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100344 }
345
346 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100347 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100348 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100349 if (ispresent(disc_ind.userData)) {
350 f_handle_userData(vc_conn, disc_ind.userData);
351 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100352 /* notify client about termination */
353 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
354 CLIENT.send(prim) to vc_conn;
355 f_conn_table_del(disc_ind.connectionId);
356 /* TOOD: return confirm to other side? */
357 }
358
Harald Welteb3414b22017-11-23 18:22:10 +0100359 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100360 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welteb3414b22017-11-23 18:22:10 +0100361 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100362 if (ispresent(conn_cfm.userData)) {
363 f_handle_userData(vc_conn, conn_cfm.userData);
364 }
Harald Welteb3414b22017-11-23 18:22:10 +0100365 }
366
Harald Welte365f4ed2017-11-23 00:00:43 +0100367 /* Disconnect request client -> SCCP */
368 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
369 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100370 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100371 f_conn_table_del(conn_id);
372 }
373
374 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100375 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
376 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100377 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100378
379 if (f_comp_known(vc_conn) == false) {
380 /* unknown client, create new connection */
381 conn_id := f_gen_conn_id();
382
383 /* store mapping between client components and SCCP connectionId */
384 f_conn_table_add(vc_conn, conn_id);
385
Harald Welte004f5fb2017-12-16 17:54:40 +0100386 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
387 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100388 } else {
389 /* known client, send via existing connection */
390 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100391 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100392 }
393
394 }
395
Harald Welte365f4ed2017-11-23 00:00:43 +0100396 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
397 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100398 /* send it to dispatcher */
399 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100400 }
401
Harald Weltec82eef42017-11-24 20:40:12 +0100402 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
403 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
404 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
405 * is printed as hex string, e.g. a@mgw for CIC 10 */
406
407 /* CLIENT -> MGCP */
408 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
409 /* MGCP request from Handler (we're MSC) */
410 /* store the transaction ID we've seen */
411 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
412 /* simply forward any MGCP from the client to the port */
413 MGCP.send(mgcp_req);
414 }
415 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
416 /* MGCP response from Handler (we're BSC/MGW) */
417 /* simply forward any MGCP from the client to the port */
418 MGCP.send(mgcp_resp);
419 }
420
421 /* MGCP -> CLIENT */
422 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
423 /* MGCP request from network side (we're BSC/MGW) */
424 /* Extract CIC from local part of endpoint name */
425 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100426 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
427 /* ignore RSIP for unknown CIC */
428 } else {
429 /* Resolve the vc_conn by the CIC */
430 vc_conn := f_comp_by_cic(cic);
431 CLIENT.send(mgcp_req) to vc_conn;
432 }
Harald Weltec82eef42017-11-24 20:40:12 +0100433 }
434 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
435 /* MGCP response from network side (we're MSC) */
436 /* Resolve the vc_conn by the transaction ID */
437 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
438 CLIENT.send(mgcp_resp) to vc_conn;
439 }
440
Harald Welte624f9632017-12-16 19:26:04 +0100441
442 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
443 f_create_expect(l3_info, vc_hdlr);
444 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
445 }
446
Harald Welte365f4ed2017-11-23 00:00:43 +0100447 }
448 }
449}
450
Harald Weltec82eef42017-11-24 20:40:12 +0100451private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100452 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100453 return hex2int(str2hex(local_part));
454
455}
Harald Welte365f4ed2017-11-23 00:00:43 +0100456
Harald Welte624f9632017-12-16 19:26:04 +0100457/***********************************************************************
458 * "Expect" Handling (mapping for expected incoming SCCP connections)
459 ***********************************************************************/
460
461/* data about an expected future incoming connection */
462type record ExpectData {
463 /* L3 payload based on which we can match it */
464 octetstring l3_payload optional,
465 /* component reference for this connection */
466 BSSAP_ConnHdlr vc_conn
467}
468
469/* procedure based port to register for incoming connections */
470signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
471
472type port BSSMAPEM_PROC_PT procedure {
473 inout BSSMAPEM_register;
474} with { extension "internal" };
475
476/* CreateCallback that can be used as create_cb and will use the expectation table */
477function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
478runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
479 var BSSAP_ConnHdlr ret := null;
480 var octetstring l3_info;
481 var integer i;
482
483 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
484 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
485 return ret;
486 }
487 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
488
489 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
490 if (not ispresent(ExpectTable[i].l3_payload)) {
491 continue;
492 }
493 if (l3_info == ExpectTable[i].l3_payload) {
494 ret := ExpectTable[i].vc_conn;
495 /* release this entry to be used again */
496 ExpectTable[i].l3_payload := omit;
497 ExpectTable[i].vc_conn := null;
498 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
499 /* return the component reference */
500 return ret;
501 }
502 }
503 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
504 return ret;
505}
506
507private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
508runs on BSSMAP_Emulation_CT {
509 var integer i;
510 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
511 if (not ispresent(ExpectTable[i].l3_payload)) {
512 ExpectTable[i].l3_payload := l3;
513 ExpectTable[i].vc_conn := hdlr;
514 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
515 return;
516 }
517 }
518 setverdict(fail, "No space left in ExpectTable");
519}
520
521
Harald Welte365f4ed2017-11-23 00:00:43 +0100522}