blob: ba7e27c3e4ad4080e6c569b8be1635344521408c [file] [log] [blame]
Harald Welte365f4ed2017-11-23 00:00:43 +01001module BSSMAP_Emulation {
2
Harald Welteb3414b22017-11-23 18:22:10 +01003import from SCCP_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +01004import from SCCPasp_Types all;
5import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +01006import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +01007import from BSSMAP_Templates all;
Harald Weltec82eef42017-11-24 20:40:12 +01008import from MGCP_Types all;
9import from MGCP_Templates all;
10import from IPA_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +010011
12/* General "base class" component definition, of which specific implementations
13 * derive themselves by means of the "extends" feature */
14type component BSSAP_ConnHdlr {
15 /* port towards MSC Emulator core / SCCP connection dispatchar */
16 port BSSAP_Conn_PT BSSAP;
17}
18
19/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
20type enumerated BSSAP_Conn_Prim {
21 /* SCCP tell us that connection was released */
22 MSC_CONN_PRIM_DISC_IND,
23 /* we tell SCCP to release connection */
24 MSC_CONN_PRIM_DISC_REQ
25}
26
Harald Welteb3414b22017-11-23 18:22:10 +010027type record BSSAP_Conn_Req {
28 SCCP_PAR_Address addr_peer,
29 SCCP_PAR_Address addr_own,
30 PDU_BSSAP bssap
31}
32
Harald Welte365f4ed2017-11-23 00:00:43 +010033/* port between individual per-connection components and this dispatcher */
34type port BSSAP_Conn_PT message {
Harald Weltec82eef42017-11-24 20:40:12 +010035 inout PDU_BSSAP, BSSAP_Conn_Prim, BSSAP_Conn_Req, MgcpCommand, MgcpResponse;
Harald Welte365f4ed2017-11-23 00:00:43 +010036} with { extension "internal" };
37
38
39/* represents a single BSSAP connection over SCCP */
40type record ConnectionData {
41 /* reference to the instance of the per-connection component */
42 BSSAP_ConnHdlr comp_ref,
Harald Weltec82eef42017-11-24 20:40:12 +010043 integer sccp_conn_id,
44 /* most recent MGCP transaction ID (Used on MSC side) */
45 MgcpTransId mgcp_trans_id optional,
46 /* CIC that has been used for voice of this channel (BSC side) */
47 integer cic optional
Harald Welte365f4ed2017-11-23 00:00:43 +010048}
49
50type component BSSMAP_Emulation_CT {
51 /* SCCP port on the bottom side, using ASP primitives */
Harald Welte004f5fb2017-12-16 17:54:40 +010052 port BSSAP_CODEC_PT BSSAP;
Harald Welte365f4ed2017-11-23 00:00:43 +010053 /* BSSAP port to the per-connection clients */
54 port BSSAP_Conn_PT CLIENT;
Harald Weltec82eef42017-11-24 20:40:12 +010055 /* MGCP port */
56 port IPA_MGCP_PT MGCP;
Harald Welte365f4ed2017-11-23 00:00:43 +010057
58 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
59 var ConnectionData ConnectionTable[16];
Harald Welte66fecd42017-11-24 23:53:23 +010060
Harald Welte624f9632017-12-16 19:26:04 +010061 /* pending expected incoming connections */
62 var ExpectData ExpectTable[8];
63 /* procedure based port to register for incoming connections */
64 port BSSMAPEM_PROC_PT PROC;
65
Harald Weltebe620f62017-11-25 00:23:54 +010066 var charstring g_bssmap_id;
Harald Welte66fecd42017-11-24 23:53:23 +010067 var integer g_next_e1_ts := 1;
Harald Welte365f4ed2017-11-23 00:00:43 +010068};
69
Harald Welteb3414b22017-11-23 18:22:10 +010070private function f_conn_id_known(integer sccp_conn_id)
71runs on BSSMAP_Emulation_CT return boolean {
72 var integer i;
73 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
74 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
75 return true;
76 }
77 }
78 return false;
79}
80
81private function f_comp_known(BSSAP_ConnHdlr client)
82runs on BSSMAP_Emulation_CT return boolean {
83 var integer i;
84 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
85 if (ConnectionTable[i].comp_ref == client) {
86 return true;
87 }
88 }
89 return false;
90}
Harald Welte365f4ed2017-11-23 00:00:43 +010091
Harald Weltee98bb2e2017-11-29 12:09:48 +010092private function f_cic_known(integer cic)
93runs on BSSMAP_Emulation_CT return boolean {
94 var integer i;
95 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
96 if (ConnectionTable[i].cic == cic) {
97 return true;
98 }
99 }
100 return false;
101}
102
Harald Welte365f4ed2017-11-23 00:00:43 +0100103/* resolve component reference by connection ID */
104private function f_comp_by_conn_id(integer sccp_conn_id)
105runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
106 var integer i;
107 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
108 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
109 return ConnectionTable[i].comp_ref;
110 }
111 }
112 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100113 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100114 self.stop;
115}
116
Harald Weltec82eef42017-11-24 20:40:12 +0100117/* resolve component reference by CIC */
118private function f_comp_by_mgcp_tid(MgcpTransId tid)
119runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
120 var integer i;
121 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
122 if (ConnectionTable[i].mgcp_trans_id == tid) {
123 return ConnectionTable[i].comp_ref;
124 }
125 }
126 log("BSSMAP Connection table not found by MGCP Transaction ID ", tid);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100127 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100128 self.stop;
129}
130
131private function f_comp_store_mgcp_tid(BSSAP_ConnHdlr client, MgcpTransId tid)
132runs on BSSMAP_Emulation_CT {
133 var integer i;
134 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
135 if (ConnectionTable[i].comp_ref == client) {
136 ConnectionTable[i].mgcp_trans_id := tid;
137 return;
138 }
139 }
140 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100141 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100142 self.stop;
143}
144
145private function f_comp_by_cic(integer cic)
146runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
147 var integer i;
148 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
149 if (ConnectionTable[i].cic == cic) {
150 return ConnectionTable[i].comp_ref;
151 }
152 }
153 log("BSSMAP Connection table not found by CIC ", cic);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100154 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100155 self.stop;
156}
157
158private function f_comp_store_cic(BSSAP_ConnHdlr client, integer cic)
159runs on BSSMAP_Emulation_CT {
160 var integer i;
161 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
162 if (ConnectionTable[i].comp_ref == client) {
163 ConnectionTable[i].cic := cic;
164 return;
165 }
166 }
167 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100168 setverdict(fail);
Harald Weltec82eef42017-11-24 20:40:12 +0100169}
170
Harald Welte365f4ed2017-11-23 00:00:43 +0100171/* resolve connection ID by component reference */
172private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
173runs on BSSMAP_Emulation_CT return integer {
174 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
175 if (ConnectionTable[i].comp_ref == client) {
176 return ConnectionTable[i].sccp_conn_id;
177 }
178 }
179 log("BSSMAP Connection table not found by component ", client);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100180 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100181 self.stop;
182}
183
Harald Welteb3414b22017-11-23 18:22:10 +0100184private function f_gen_conn_id()
185runs on BSSMAP_Emulation_CT return integer {
186 var integer conn_id;
187
188 do {
189 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
190 } while (f_conn_id_known(conn_id) == true);
191
192 return conn_id;
193}
194
Harald Welte365f4ed2017-11-23 00:00:43 +0100195private function f_conn_table_init()
196runs on BSSMAP_Emulation_CT {
197 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
198 ConnectionTable[i].comp_ref := null;
199 ConnectionTable[i].sccp_conn_id := -1;
Harald Weltec82eef42017-11-24 20:40:12 +0100200 ConnectionTable[i].mgcp_trans_id := omit;
201 ConnectionTable[i].cic := omit;
Harald Welte365f4ed2017-11-23 00:00:43 +0100202 }
203}
204
205private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
206runs on BSSMAP_Emulation_CT {
207 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
208 if (ConnectionTable[i].sccp_conn_id == -1) {
209 ConnectionTable[i].comp_ref := comp_ref;
210 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100211 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100212 return;
213 }
214 }
215 log("BSSMAP Connection table full!");
Harald Welte9ca9eb12017-11-25 00:50:43 +0100216 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100217 self.stop;
218}
219
220private function f_conn_table_del(integer sccp_conn_id)
221runs on BSSMAP_Emulation_CT {
222 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
223 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100224 log("Deleted conn table entry ", i,
225 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100226 ConnectionTable[i].sccp_conn_id := -1;
227 ConnectionTable[i].comp_ref := null;
Harald Welte0a4317a2017-11-25 00:32:46 +0100228 return
Harald Welte365f4ed2017-11-23 00:00:43 +0100229 }
230 }
231 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
Harald Welte9ca9eb12017-11-25 00:50:43 +0100232 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100233 self.stop;
234}
235
236/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte004f5fb2017-12-16 17:54:40 +0100237private function f_handle_userData(BSSAP_ConnHdlr client, PDU_BSSAP bssap)
Harald Welte365f4ed2017-11-23 00:00:43 +0100238runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100239 /* decode + send decoded BSSAP to client */
Harald Welte1b2748e2017-11-24 23:40:16 +0100240
241 /* BSC Side: If this is an assignment command, store CIC */
242 if (ischosen(bssap.pdu.bssmap.assignmentRequest) and
243 ispresent(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)) {
244 var BSSMAP_IE_CircuitIdentityCode cic_ie :=
245 bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode;
246 var integer cic := (oct2int(cic_ie.cicHigh) * 256) + oct2int(cic_ie.cicLow);
247 f_comp_store_cic(client, cic);
248 }
249
Harald Welte365f4ed2017-11-23 00:00:43 +0100250 CLIENT.send(bssap) to client;
251}
252
253/* call-back type, to be provided by specific implementation; called when new SCCP connection
254 * arrives */
Harald Welte004f5fb2017-12-16 17:54:40 +0100255type function BssmapCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +0100256runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
257
258type function BssmapUnitdataCallback(PDU_BSSAP bssap)
259runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
260
261type record BssmapOps {
262 BssmapCreateCallback create_cb,
263 BssmapUnitdataCallback unitdata_cb
264}
265
Harald Weltebe620f62017-11-25 00:23:54 +0100266function main(BssmapOps ops, charstring id) runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100267
Harald Weltebe620f62017-11-25 00:23:54 +0100268 g_bssmap_id := id;
Harald Welte365f4ed2017-11-23 00:00:43 +0100269 f_conn_table_init();
270
271 while (true) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100272 var BSSAP_N_UNITDATA_ind ud_ind;
273 var BSSAP_N_CONNECT_ind conn_ind;
274 var BSSAP_N_CONNECT_cfm conn_cfm;
275 var BSSAP_N_DATA_ind data_ind;
276 var BSSAP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100277 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100278 var BSSAP_ConnHdlr vc_conn;
279 var PDU_BSSAP bssap;
Harald Weltec82eef42017-11-24 20:40:12 +0100280 var MgcpCommand mgcp_req;
281 var MgcpResponse mgcp_resp;
Harald Welte624f9632017-12-16 19:26:04 +0100282 var BSSAP_ConnHdlr vc_hdlr;
283 var octetstring l3_info;
Harald Welte365f4ed2017-11-23 00:00:43 +0100284
285 alt {
286 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100287 [] BSSAP.receive(BSSAP_N_UNITDATA_ind:?) -> value ud_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100288 /* Connectionless Procedures like RESET */
289 var template PDU_BSSAP resp;
Harald Welte004f5fb2017-12-16 17:54:40 +0100290 resp := ops.unitdata_cb.apply(ud_ind.userData);
Harald Welte365f4ed2017-11-23 00:00:43 +0100291 if (isvalue(resp)) {
Harald Welte004f5fb2017-12-16 17:54:40 +0100292 BSSAP.send(ts_BSSAP_UNITDATA_req(ud_ind.callingAddress,
293 ud_ind.calledAddress, resp));
Harald Welte365f4ed2017-11-23 00:00:43 +0100294 }
295 }
296
297 /* SCCP -> Client: new connection from BSC */
Harald Welte004f5fb2017-12-16 17:54:40 +0100298 [] BSSAP.receive(BSSAP_N_CONNECT_ind:?) -> value conn_ind {
Harald Weltebe620f62017-11-25 00:23:54 +0100299 vc_conn := ops.create_cb.apply(conn_ind, id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100300 /* store mapping between client components and SCCP connectionId */
301 f_conn_table_add(vc_conn, conn_ind.connectionId);
302 /* handle user payload */
303 f_handle_userData(vc_conn, conn_ind.userData);
304 /* confirm connection establishment */
Harald Welte004f5fb2017-12-16 17:54:40 +0100305 BSSAP.send(ts_BSSAP_CONNECT_res(conn_ind.connectionId, omit));
Harald Welte365f4ed2017-11-23 00:00:43 +0100306 }
307
308 /* SCCP -> Client: connection-oriented data in existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100309 [] BSSAP.receive(BSSAP_N_DATA_ind:?) -> value data_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100310 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100311 if (ispresent(data_ind.userData)) {
312 f_handle_userData(vc_conn, data_ind.userData);
313 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100314 }
315
316 /* SCCP -> Client: disconnect of an existing connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100317 [] BSSAP.receive(BSSAP_N_DISCONNECT_ind:?) -> value disc_ind {
Harald Welte365f4ed2017-11-23 00:00:43 +0100318 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100319 if (ispresent(disc_ind.userData)) {
320 f_handle_userData(vc_conn, disc_ind.userData);
321 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100322 /* notify client about termination */
323 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
324 CLIENT.send(prim) to vc_conn;
325 f_conn_table_del(disc_ind.connectionId);
326 /* TOOD: return confirm to other side? */
327 }
328
Harald Welteb3414b22017-11-23 18:22:10 +0100329 /* SCCP -> Client: connection confirm for outbound connection */
Harald Welte004f5fb2017-12-16 17:54:40 +0100330 [] BSSAP.receive(BSSAP_N_CONNECT_cfm:?) -> value conn_cfm {
Harald Welteb3414b22017-11-23 18:22:10 +0100331 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100332 if (ispresent(conn_cfm.userData)) {
333 f_handle_userData(vc_conn, conn_cfm.userData);
334 }
Harald Welteb3414b22017-11-23 18:22:10 +0100335 }
336
Harald Welte365f4ed2017-11-23 00:00:43 +0100337 /* Disconnect request client -> SCCP */
338 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
339 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100340 BSSAP.send(ts_BSSAP_DISC_req(conn_id, 0));
Harald Welte365f4ed2017-11-23 00:00:43 +0100341 f_conn_table_del(conn_id);
342 }
343
344 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100345 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
346 var integer conn_id;
Harald Welte004f5fb2017-12-16 17:54:40 +0100347 /* send to dispatcher */
Harald Welteb3414b22017-11-23 18:22:10 +0100348
349 if (f_comp_known(vc_conn) == false) {
350 /* unknown client, create new connection */
351 conn_id := f_gen_conn_id();
352
353 /* store mapping between client components and SCCP connectionId */
354 f_conn_table_add(vc_conn, conn_id);
355
Harald Welte004f5fb2017-12-16 17:54:40 +0100356 BSSAP.send(ts_BSSAP_CONNECT_req(creq.addr_peer, creq.addr_own, conn_id,
357 creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100358 } else {
359 /* known client, send via existing connection */
360 conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100361 BSSAP.send(ts_BSSAP_DATA_req(conn_id, creq.bssap));
Harald Welteb3414b22017-11-23 18:22:10 +0100362 }
363
364 }
365
Harald Welte365f4ed2017-11-23 00:00:43 +0100366 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
367 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welte004f5fb2017-12-16 17:54:40 +0100368 /* send it to dispatcher */
369 BSSAP.send(ts_BSSAP_DATA_req(conn_id, bssap));
Harald Welte365f4ed2017-11-23 00:00:43 +0100370 }
371
Harald Weltec82eef42017-11-24 20:40:12 +0100372 /* Handling of MGCP in IPA SCCPLite case. This predates 3GPP AoIP
373 * and uses a MGCP session in parallel to BSSAP. BSSAP uses CIC
374 * as usual, and MGCP uses "CIC@mgw" endpoint naming, where CIC
375 * is printed as hex string, e.g. a@mgw for CIC 10 */
376
377 /* CLIENT -> MGCP */
378 [] CLIENT.receive(MgcpCommand:?) -> value mgcp_req sender vc_conn {
379 /* MGCP request from Handler (we're MSC) */
380 /* store the transaction ID we've seen */
381 f_comp_store_mgcp_tid(vc_conn, mgcp_req.line.trans_id);
382 /* simply forward any MGCP from the client to the port */
383 MGCP.send(mgcp_req);
384 }
385 [] CLIENT.receive(MgcpResponse:?) -> value mgcp_resp sender vc_conn {
386 /* MGCP response from Handler (we're BSC/MGW) */
387 /* simply forward any MGCP from the client to the port */
388 MGCP.send(mgcp_resp);
389 }
390
391 /* MGCP -> CLIENT */
392 [] MGCP.receive(MgcpCommand:?) -> value mgcp_req {
393 /* MGCP request from network side (we're BSC/MGW) */
394 /* Extract CIC from local part of endpoint name */
395 var integer cic := f_mgcp_ep_extract_cic(mgcp_req.line.ep);
Harald Weltee98bb2e2017-11-29 12:09:48 +0100396 if (match(mgcp_req, tr_RSIP) and f_cic_known(cic) == false) {
397 /* ignore RSIP for unknown CIC */
398 } else {
399 /* Resolve the vc_conn by the CIC */
400 vc_conn := f_comp_by_cic(cic);
401 CLIENT.send(mgcp_req) to vc_conn;
402 }
Harald Weltec82eef42017-11-24 20:40:12 +0100403 }
404 [] MGCP.receive(MgcpResponse:?) -> value mgcp_resp {
405 /* MGCP response from network side (we're MSC) */
406 /* Resolve the vc_conn by the transaction ID */
407 vc_conn := f_comp_by_mgcp_tid(mgcp_resp.line.trans_id);
408 CLIENT.send(mgcp_resp) to vc_conn;
409 }
410
Harald Welte624f9632017-12-16 19:26:04 +0100411
412 [] PROC.getcall(BSSMAPEM_register:{?,?}) -> param(l3_info, vc_hdlr) {
413 f_create_expect(l3_info, vc_hdlr);
414 PROC.reply(BSSMAPEM_register:{l3_info, vc_hdlr});
415 }
416
Harald Welte365f4ed2017-11-23 00:00:43 +0100417 }
418 }
419}
420
Harald Weltec82eef42017-11-24 20:40:12 +0100421private function f_mgcp_ep_extract_cic(charstring inp) return integer {
Harald Welte525a9c12017-11-24 23:40:41 +0100422 var charstring local_part := regexp(inp, "(*)@*", 0);
Harald Weltec82eef42017-11-24 20:40:12 +0100423 return hex2int(str2hex(local_part));
424
425}
Harald Welte365f4ed2017-11-23 00:00:43 +0100426
Harald Welte624f9632017-12-16 19:26:04 +0100427/***********************************************************************
428 * "Expect" Handling (mapping for expected incoming SCCP connections)
429 ***********************************************************************/
430
431/* data about an expected future incoming connection */
432type record ExpectData {
433 /* L3 payload based on which we can match it */
434 octetstring l3_payload optional,
435 /* component reference for this connection */
436 BSSAP_ConnHdlr vc_conn
437}
438
439/* procedure based port to register for incoming connections */
440signature BSSMAPEM_register(in octetstring l3, in BSSAP_ConnHdlr hdlr);
441
442type port BSSMAPEM_PROC_PT procedure {
443 inout BSSMAPEM_register;
444} with { extension "internal" };
445
446/* CreateCallback that can be used as create_cb and will use the expectation table */
447function ExpectedCreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
448runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
449 var BSSAP_ConnHdlr ret := null;
450 var octetstring l3_info;
451 var integer i;
452
453 if (not ischosen(conn_ind.userData.pdu.bssmap.completeLayer3Information)) {
454 setverdict(fail, "N-CONNECT.ind with L3 != COMPLETE L3");
455 return ret;
456 }
457 l3_info := conn_ind.userData.pdu.bssmap.completeLayer3Information.layer3Information.layer3info;
458
459 for (i := 0; i < sizeof(ExpectTable); i:= i+1) {
460 if (not ispresent(ExpectTable[i].l3_payload)) {
461 continue;
462 }
463 if (l3_info == ExpectTable[i].l3_payload) {
464 ret := ExpectTable[i].vc_conn;
465 /* release this entry to be used again */
466 ExpectTable[i].l3_payload := omit;
467 ExpectTable[i].vc_conn := null;
468 log("Found Expect[", i, "] for ", l3_info, " handled at ", ret);
469 /* return the component reference */
470 return ret;
471 }
472 }
473 setverdict(fail, "Couldn't find Expect for incoming connection ", conn_ind);
474 return ret;
475}
476
477private function f_create_expect(octetstring l3, BSSAP_ConnHdlr hdlr)
478runs on BSSMAP_Emulation_CT {
479 var integer i;
480 for (i := 0; i < sizeof(ExpectTable); i := i+1) {
481 if (not ispresent(ExpectTable[i].l3_payload)) {
482 ExpectTable[i].l3_payload := l3;
483 ExpectTable[i].vc_conn := hdlr;
484 log("Created Expect[", i, "] for ", l3, " to be handled at ", hdlr);
485 return;
486 }
487 }
488 setverdict(fail, "No space left in ExpectTable");
489}
490
491
Harald Welte365f4ed2017-11-23 00:00:43 +0100492}