Harald Welte | 876bf9d | 2018-01-21 19:28:59 +0100 | [diff] [blame] | 1 | module 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. |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | import from Osmocom_Types all; |
| 34 | import from MNCC_CodecPort all; |
| 35 | import from MNCC_Types all; |
| 36 | import from UD_Types all; |
| 37 | |
| 38 | /* General "base class" component definition, of which specific implementations |
| 39 | * derive themselves by means of the "extends" feature */ |
| 40 | type component MNCC_ConnHdlr { |
| 41 | /* ports towards MNCC Emulator core / call dispatchar */ |
| 42 | port MNCC_Conn_PT MNCC; |
| 43 | port MNCCEM_PROC_PT MNCC_PROC; |
| 44 | } |
| 45 | |
| 46 | /* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */ |
| 47 | type enumerated MNCC_Conn_Prim { |
| 48 | /* MNCC tell us that connection was released */ |
| 49 | MNCC_CONN_PRIM_DISC_IND, |
| 50 | /* we tell MNCC to release connection */ |
| 51 | MNCC_CONN_PRIM_DISC_REQ |
| 52 | } |
| 53 | |
| 54 | type record MNCC_Conn_Req { |
| 55 | MNCC_PDU mncc |
| 56 | } |
| 57 | |
| 58 | /* port between individual per-connection components and this dispatcher */ |
| 59 | type port MNCC_Conn_PT message { |
| 60 | inout MNCC_PDU, MNCC_Conn_Prim, MNCC_Conn_Req; |
| 61 | } with { extension "internal" }; |
| 62 | |
| 63 | |
| 64 | /* represents a single MNCC call */ |
| 65 | type record ConnectionData { |
| 66 | /* reference to the instance of the per-connection component */ |
| 67 | MNCC_ConnHdlr comp_ref, |
| 68 | integer mncc_call_id |
| 69 | } |
| 70 | |
| 71 | type component MNCC_Emulation_CT { |
| 72 | /* UNIX DOMAIN socket on the bottom side, using primitives */ |
| 73 | port MNCC_CODEC_PT MNCC; |
| 74 | /* MNCC port to the per-connection clients */ |
| 75 | port MNCC_Conn_PT MNCC_CLIENT; |
| 76 | |
| 77 | /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */ |
| 78 | var ConnectionData MnccCallTable[16]; |
| 79 | |
| 80 | /* pending expected incoming connections */ |
| 81 | var ExpectData MnccExpectTable[8]; |
| 82 | /* procedure based port to register for incoming connections */ |
| 83 | port MNCCEM_PROC_PT MNCC_PROC; |
| 84 | |
| 85 | var integer g_mncc_ud_id; |
| 86 | }; |
| 87 | |
| 88 | private function f_call_id_known(uint32_t mncc_call_id) |
| 89 | runs on MNCC_Emulation_CT return boolean { |
| 90 | var integer i; |
| 91 | for (i := 0; i < sizeof(MnccCallTable); i := i+1) { |
| 92 | if (MnccCallTable[i].mncc_call_id == mncc_call_id){ |
| 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | private function f_comp_known(MNCC_ConnHdlr client) |
| 100 | runs on MNCC_Emulation_CT return boolean { |
| 101 | var integer i; |
| 102 | for (i := 0; i < sizeof(MnccCallTable); i := i+1) { |
| 103 | if (MnccCallTable[i].comp_ref == client) { |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | /* resolve component reference by connection ID */ |
| 111 | private function f_comp_by_call_id(uint32_t mncc_call_id) |
| 112 | runs on MNCC_Emulation_CT return MNCC_ConnHdlr { |
| 113 | var integer i; |
| 114 | for (i := 0; i < sizeof(MnccCallTable); i := i+1) { |
| 115 | if (MnccCallTable[i].mncc_call_id == mncc_call_id) { |
| 116 | return MnccCallTable[i].comp_ref; |
| 117 | } |
| 118 | } |
| 119 | log("MNCC Call table not found by MNCC Call ID ", mncc_call_id); |
| 120 | setverdict(fail); |
| 121 | self.stop; |
| 122 | } |
| 123 | |
| 124 | /* resolve connection ID by component reference */ |
| 125 | private function f_call_id_by_comp(MNCC_ConnHdlr client) |
| 126 | runs on MNCC_Emulation_CT return integer { |
| 127 | for (var integer i := 0; i < sizeof(MnccCallTable); i := i+1) { |
| 128 | if (MnccCallTable[i].comp_ref == client) { |
| 129 | return MnccCallTable[i].mncc_call_id; |
| 130 | } |
| 131 | } |
| 132 | log("MNCC Call table not found by component ", client); |
| 133 | setverdict(fail); |
| 134 | self.stop; |
| 135 | } |
| 136 | |
| 137 | private function f_gen_call_id() |
| 138 | runs on MNCC_Emulation_CT return integer { |
| 139 | var uint32_t call_id; |
| 140 | |
| 141 | do { |
| 142 | call_id := float2int(rnd()*4294967296.0); |
| 143 | } while (f_call_id_known(call_id) == true); |
| 144 | |
| 145 | return call_id; |
| 146 | } |
| 147 | |
| 148 | private function f_call_table_init() |
| 149 | runs on MNCC_Emulation_CT { |
| 150 | for (var integer i := 0; i < sizeof(MnccCallTable); i := i+1) { |
| 151 | MnccCallTable[i].comp_ref := null; |
| 152 | MnccCallTable[i].mncc_call_id := -1; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private function f_call_table_add(MNCC_ConnHdlr comp_ref, uint32_t mncc_call_id) |
| 157 | runs on MNCC_Emulation_CT { |
| 158 | for (var integer i := 0; i < sizeof(MnccCallTable); i := i+1) { |
| 159 | if (MnccCallTable[i].mncc_call_id == -1) { |
| 160 | MnccCallTable[i].comp_ref := comp_ref; |
| 161 | MnccCallTable[i].mncc_call_id := mncc_call_id; |
| 162 | log("Added conn table entry ", i, comp_ref, mncc_call_id); |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | log("MNCC Call table full!"); |
| 167 | setverdict(fail); |
| 168 | self.stop; |
| 169 | } |
| 170 | |
| 171 | private function f_call_table_del(uint32_t mncc_call_id) |
| 172 | runs on MNCC_Emulation_CT { |
| 173 | for (var integer i := 0; i < sizeof(MnccCallTable); i := i+1) { |
| 174 | if (MnccCallTable[i].mncc_call_id == mncc_call_id) { |
| 175 | log("Deleted conn table entry ", i, |
| 176 | MnccCallTable[i].comp_ref, mncc_call_id); |
| 177 | MnccCallTable[i].mncc_call_id := -1; |
| 178 | MnccCallTable[i].comp_ref := null; |
| 179 | return |
| 180 | } |
| 181 | } |
| 182 | log("MNCC Call table attempt to delete non-existant ", mncc_call_id); |
| 183 | setverdict(fail); |
| 184 | self.stop; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | function f_connect(charstring sock) runs on MNCC_Emulation_CT { |
| 189 | var UD_connect_result res; |
| 190 | timer T := 5.0; |
| 191 | |
| 192 | T.start; |
| 193 | MNCC.send(UD_connect:{sock, -1}); |
| 194 | alt { |
| 195 | [] MNCC.receive(UD_connect_result:?) -> value res { |
| 196 | if (ispresent(res.result) and ispresent(res.result.result_code) and res.result.result_code == ERROR) { |
| 197 | setverdict(fail, "Error connecting to MNCC socket", res); |
| 198 | self.stop; |
| 199 | } else { |
| 200 | g_mncc_ud_id := res.id; |
| 201 | } |
| 202 | } |
| 203 | [] T.timeout { |
| 204 | setverdict(fail, "Timeout connecting to MNCC socket"); |
| 205 | self.stop; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* call-back type, to be provided by specific implementation; called when new SCCP connection |
| 211 | * arrives */ |
| 212 | type function MnccCreateCallback(MNCC_PDU conn_ind, charstring id) |
| 213 | runs on MNCC_Emulation_CT return MNCC_ConnHdlr; |
| 214 | |
| 215 | type function MnccUnitdataCallback(MNCC_PDU mncc) |
| 216 | runs on MNCC_Emulation_CT return template MNCC_PDU; |
| 217 | |
| 218 | type record MnccOps { |
| 219 | MnccCreateCallback create_cb, |
| 220 | MnccUnitdataCallback unitdata_cb |
| 221 | } |
| 222 | |
| 223 | function main(MnccOps ops, charstring id, charstring sock) runs on MNCC_Emulation_CT { |
| 224 | |
| 225 | f_connect(sock); |
| 226 | f_call_table_init(); |
| 227 | |
| 228 | while (true) { |
| 229 | var MNCC_send_data sd; |
| 230 | var MNCC_Conn_Req creq; |
| 231 | var MNCC_ConnHdlr vc_conn; |
| 232 | var MNCC_PDU mncc; |
| 233 | var MNCC_ConnHdlr vc_hdlr; |
| 234 | var charstring dest_nr; |
| 235 | |
| 236 | alt { |
| 237 | /* MNCC -> Client: UNIT-DATA (connectionless SCCP) from a BSC */ |
| 238 | [] MNCC.receive(t_SD_MNCC_MSGT(g_mncc_ud_id, MNCC_SOCKET_HELLO)) -> value sd { |
| 239 | /* Connectionless Procedures like HELLO */ |
| 240 | var template MNCC_PDU resp; |
| 241 | resp := ops.unitdata_cb.apply(sd.data); |
| 242 | if (isvalue(resp)) { |
| 243 | MNCC.send(t_SD_MNCC(g_mncc_ud_id, resp)); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* MNCC -> Client: Release Indication / confirmation */ |
| 248 | [] MNCC.receive(t_SD_MNCC_MSGT(g_mncc_ud_id, (MNCC_REL_IND, MNCC_REL_CNF))) -> value sd { |
| 249 | var uint32_t call_id := f_mncc_get_call_id(sd.data); |
| 250 | /* forward to respective client */ |
| 251 | vc_conn := f_comp_by_call_id(call_id); |
| 252 | MNCC_CLIENT.send(sd.data) to vc_conn; |
| 253 | /* remove from call table */ |
| 254 | f_call_table_del(call_id); |
| 255 | } |
| 256 | |
| 257 | /* MNCC -> Client: call related messages */ |
| 258 | [] MNCC.receive(t_SD_MNCC_MSGT(g_mncc_ud_id, ?)) -> value sd { |
| 259 | var uint32_t call_id := f_mncc_get_call_id(sd.data); |
| 260 | |
| 261 | if (f_call_id_known(call_id)) { |
| 262 | vc_conn := f_comp_by_call_id(call_id); |
| 263 | MNCC_CLIENT.send(sd.data) to vc_conn; |
| 264 | } else { |
| 265 | /* TODO: Only accept this for SETUP.req? */ |
| 266 | vc_conn := ops.create_cb.apply(sd.data, id) |
| 267 | /* store mapping between client components and SCCP connectionId */ |
| 268 | f_call_table_add(vc_conn, call_id); |
| 269 | /* handle user payload */ |
| 270 | MNCC_CLIENT.send(sd.data) to vc_conn; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* Client -> MNCC Socket: RELEASE.ind or RELEASE.cnf: forward + drop call table entry */ |
| 275 | [] MNCC_CLIENT.receive(MNCC_PDU:{msg_type := (MNCC_REL_IND, MNCC_REL_CNF), u:=?}) -> value mncc sender vc_conn { |
| 276 | var integer call_id := f_call_id_by_comp(vc_conn); |
| 277 | /* forward to MNCC socket */ |
| 278 | MNCC.send(t_SD_MNCC(g_mncc_ud_id, mncc)); |
| 279 | /* remove from call table */ |
| 280 | f_call_table_del(call_id); |
| 281 | } |
| 282 | |
| 283 | /* Client -> MNCC Socket: Normal message */ |
| 284 | [] MNCC_CLIENT.receive(MNCC_PDU:?) -> value mncc sender vc_conn { |
| 285 | /* forward to MNCC socket */ |
| 286 | MNCC.send(t_SD_MNCC(g_mncc_ud_id, mncc)); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* Client -> us: procedure call to register expect */ |
| 291 | [] MNCC_PROC.getcall(MNCCEM_register:{?,?}) -> param(dest_nr, vc_hdlr) { |
| 292 | f_create_expect(dest_nr, vc_hdlr); |
| 293 | MNCC_PROC.reply(MNCCEM_register:{dest_nr, vc_hdlr}); |
| 294 | } |
| 295 | |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | private function f_mgcp_ep_extract_cic(charstring inp) return integer { |
| 301 | var charstring local_part := regexp(inp, "(*)@*", 0); |
| 302 | return hex2int(str2hex(local_part)); |
| 303 | |
| 304 | } |
| 305 | |
| 306 | /*********************************************************************** |
| 307 | * "Expect" Handling (mapping for expected incoming MNCC calls from IUT) |
| 308 | ***********************************************************************/ |
| 309 | |
| 310 | /* data about an expected future incoming connection */ |
| 311 | type record ExpectData { |
| 312 | /* destination number based on which we can match it */ |
| 313 | charstring dest_number optional, |
| 314 | /* component reference for this connection */ |
| 315 | MNCC_ConnHdlr vc_conn |
| 316 | } |
| 317 | |
| 318 | /* procedure based port to register for incoming calls */ |
| 319 | signature MNCCEM_register(in charstring dest_nr, in MNCC_ConnHdlr hdlr); |
| 320 | |
| 321 | type port MNCCEM_PROC_PT procedure { |
| 322 | inout MNCCEM_register; |
| 323 | } with { extension "internal" }; |
| 324 | |
| 325 | /* CreateCallback that can be used as create_cb and will use the expectation table */ |
| 326 | function ExpectedCreateCallback(MNCC_PDU conn_ind, charstring id) |
| 327 | runs on MNCC_Emulation_CT return MNCC_ConnHdlr { |
| 328 | var MNCC_ConnHdlr ret := null; |
| 329 | var charstring dest_number; |
| 330 | var integer i; |
| 331 | |
| 332 | if (not ischosen(conn_ind.u.signal) or conn_ind.msg_type != MNCC_SETUP_IND) { |
| 333 | setverdict(fail, "MNCC ExpectedCreateCallback needs MNCC_SETUP_IND"); |
| 334 | return ret; |
| 335 | } |
| 336 | dest_number := conn_ind.u.signal.called.number; |
| 337 | |
| 338 | for (i := 0; i < sizeof(MnccExpectTable); i:= i+1) { |
| 339 | if (not ispresent(MnccExpectTable[i].dest_number)) { |
| 340 | continue; |
| 341 | } |
| 342 | if (dest_number == MnccExpectTable[i].dest_number) { |
| 343 | ret := MnccExpectTable[i].vc_conn; |
| 344 | /* release this entry to be used again */ |
| 345 | MnccExpectTable[i].dest_number := omit; |
| 346 | MnccExpectTable[i].vc_conn := null; |
| 347 | log("Found MnccExpect[", i, "] for ", dest_number, " handled at ", ret); |
| 348 | /* return the component reference */ |
| 349 | return ret; |
| 350 | } |
| 351 | } |
| 352 | setverdict(fail, "Couldn't find MnccExpect for incoming call ", dest_number); |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | /* server/emulation side function to create expect */ |
| 357 | private function f_create_expect(charstring dest_number, MNCC_ConnHdlr hdlr) |
| 358 | runs on MNCC_Emulation_CT { |
| 359 | var integer i; |
| 360 | for (i := 0; i < sizeof(MnccExpectTable); i := i+1) { |
| 361 | if (not ispresent(MnccExpectTable[i].dest_number)) { |
| 362 | MnccExpectTable[i].dest_number := dest_number; |
| 363 | MnccExpectTable[i].vc_conn := hdlr; |
| 364 | log("Created MnccExpect[", i, "] for ", dest_number, " to be handled at ", hdlr); |
| 365 | return; |
| 366 | } |
| 367 | } |
| 368 | setverdict(fail, "No space left in MnccMnccExpectTable"); |
| 369 | } |
| 370 | |
| 371 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 372 | function f_create_mncc_expect(charstring dest_number) runs on MNCC_ConnHdlr { |
| 373 | MNCC_PROC.call(MNCCEM_register:{dest_number, self}) { |
| 374 | [] MNCC_PROC.getreply(MNCCEM_register:{?,?}) {}; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | function DummyUnitdataCallback(MNCC_PDU mncc) |
| 379 | runs on MNCC_Emulation_CT return template MNCC_PDU { |
| 380 | log("Ignoring MNCC ", mncc); |
| 381 | return omit; |
| 382 | } |
| 383 | |
| 384 | } |