Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 1 | module SIP_Emulation { |
| 2 | |
| 3 | /* SIP Emulation, runs on top of SIPmsg_PT. It multiplexes/demultiplexes |
| 4 | * the individual calls, so there can be separate TTCN-3 components handling |
| 5 | * each of the calls |
| 6 | * |
| 7 | * The SIP_Emulation.main() function processes SIP message from the SIPmsg |
| 8 | * socket via the SIPmsg_PT, and dispatches them to the per-connection components. |
| 9 | * |
| 10 | * Outbound SIP calls are initiated by sending a PDU_SIP_Request messages |
| 11 | * to the component running the SIP_Emulation.main() function. |
| 12 | * |
| 13 | * For each new inbound call, the SipOps.create_cb() is called. It can create |
| 14 | * or resolve a TTCN-3 component, and returns a component reference to which that inbound |
| 15 | * call 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 MNCC + SIP, and first trigger a connection from MNCC side in a |
| 20 | * component which then subsequently should also handle the SIP 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. |
Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 27 | * |
| 28 | * SPDX-License-Identifier: GPL-2.0-or-later |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | import from SIPmsg_Types all; |
| 32 | import from SIPmsg_PortType all; |
| 33 | |
| 34 | type component SIP_ConnHdlr { |
| 35 | /* ports towards SIP Emulation core / call dispatcher */ |
| 36 | port SIP_Conn_PT SIP; |
| 37 | port SIPEM_PROC_PT SIP_PROC; |
| 38 | } |
| 39 | |
| 40 | /* port between individual per-call components and this dispatcher */ |
| 41 | type port SIP_Conn_PT message { |
| 42 | inout PDU_SIP_Request, PDU_SIP_Response; |
| 43 | } with { extension "internal" }; |
| 44 | |
| 45 | /* represents a single SIP Call */ |
| 46 | type record CallData { |
| 47 | /* reference to the instance of the per-connection component */ |
| 48 | SIP_ConnHdlr comp_ref, |
| 49 | CallidString call_id |
| 50 | } |
| 51 | |
| 52 | type component SIP_Emulation_CT { |
| 53 | /* SIP test port on bottom side */ |
| 54 | port SIPmsg_PT SIP; |
| 55 | /* SIP port to the per-call clients */ |
| 56 | port SIP_Conn_PT CLIENT; |
| 57 | |
| 58 | var CallData SipCallTable[16]; |
| 59 | var ExpectData SipExpectTable[16]; |
| 60 | |
| 61 | /* procedure based port to register for incoming connections */ |
| 62 | port SIPEM_PROC_PT CLIENT_PROC; |
| 63 | }; |
| 64 | |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 65 | template RequestLine tr_ReqLine(template Method method) := { |
| 66 | method := method, |
| 67 | requestUri := ?, |
| 68 | sipVersion := ? |
| 69 | } |
| 70 | |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 71 | private template PDU_SIP_Request tr_SIP_REGISTER := { |
| 72 | requestLine := tr_ReqLine(REGISTER_E), |
| 73 | msgHeader := t_SIP_msgHeader_any, |
| 74 | messageBody := *, |
| 75 | payload := * |
| 76 | } |
| 77 | |
Harald Welte | b0d9360 | 2018-03-20 18:09:34 +0100 | [diff] [blame] | 78 | private template PDU_SIP_Request tr_SIP_INVITE := { |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 79 | requestLine := tr_ReqLine(INVITE_E), |
| 80 | msgHeader := t_SIP_msgHeader_any, |
| 81 | messageBody := *, |
| 82 | payload := * |
| 83 | } |
| 84 | |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 85 | template SipUrl tr_SIP_Url(template charstring user_or_num, |
| 86 | template charstring host := *, |
| 87 | template integer portField := *) := { |
| 88 | scheme := "sip", |
| 89 | userInfo := { |
| 90 | userOrTelephoneSubscriber := user_or_num, |
| 91 | password := * |
| 92 | }, |
| 93 | hostPort := { |
| 94 | host := host, |
| 95 | portField := portField |
| 96 | }, |
| 97 | urlParameters := *, |
| 98 | headers := * |
| 99 | } |
| 100 | template (value) SipUrl ts_SIP_Url(charstring user_or_num, |
| 101 | template (omit) charstring host := omit, |
| 102 | template (omit) integer portField := omit) := { |
| 103 | scheme := "sip", |
| 104 | userInfo := { |
| 105 | userOrTelephoneSubscriber := user_or_num, |
| 106 | password := omit |
| 107 | }, |
| 108 | hostPort := { |
| 109 | host := host, |
| 110 | portField := portField |
| 111 | }, |
| 112 | urlParameters := omit, |
| 113 | headers := omit |
| 114 | } |
| 115 | |
| 116 | template Addr_Union tr_SIP_Addr(template SipUrl sip_url) := { |
| 117 | nameAddr := { |
| 118 | displayName := *, |
| 119 | addrSpec := sip_url |
| 120 | } |
| 121 | } |
| 122 | template (value) Addr_Union ts_SIP_Addr(template (value) SipUrl sip_url) := { |
| 123 | nameAddr := { |
| 124 | displayName := omit, |
| 125 | addrSpec := sip_url |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | template To tr_SIP_To(template Addr_Union addr) := { |
| 130 | fieldName := TO_E, |
| 131 | addressField := addr, |
| 132 | toParams := * |
| 133 | } |
| 134 | template (value) To ts_SIP_To(template (value) Addr_Union addr) := { |
| 135 | fieldName := TO_E, |
| 136 | addressField := addr, |
| 137 | toParams := omit |
| 138 | } |
| 139 | |
| 140 | /* resolve component reference by connection ID */ |
| 141 | private function f_call_id_known(CallidString call_id) |
| 142 | runs on SIP_Emulation_CT return boolean { |
| 143 | var integer i; |
| 144 | for (i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 145 | if (SipCallTable[i].call_id == call_id) { |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | /* resolve component reference by connection ID */ |
| 153 | private function f_comp_by_call_id(CallidString call_id) |
| 154 | runs on SIP_Emulation_CT return SIP_ConnHdlr { |
| 155 | var integer i; |
| 156 | for (i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 157 | if (SipCallTable[i].call_id == call_id) { |
| 158 | return SipCallTable[i].comp_ref; |
| 159 | } |
| 160 | } |
| 161 | setverdict(fail, "SIP Call table not found by SIP Call ID ", call_id); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 162 | mtc.stop; |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* resolve connection ID by component reference */ |
| 166 | private function f_call_id_by_comp(SIP_ConnHdlr client) |
| 167 | runs on SIP_Emulation_CT return CallidString { |
| 168 | for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 169 | if (SipCallTable[i].comp_ref == client) { |
| 170 | return SipCallTable[i].call_id; |
| 171 | } |
| 172 | } |
| 173 | setverdict(fail, "SIP Call table not found by component ", client); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 174 | mtc.stop; |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | private function f_expect_table_init() |
| 178 | runs on SIP_Emulation_CT { |
| 179 | for (var integer i := 0; i < sizeof(SipExpectTable); i := i+1) { |
| 180 | SipExpectTable[i].sip_to := omit; |
| 181 | SipExpectTable[i].vc_conn := null; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | private function f_call_table_init() |
| 186 | runs on SIP_Emulation_CT { |
| 187 | for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 188 | SipCallTable[i].comp_ref := null; |
| 189 | SipCallTable[i].call_id := ""; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private function f_call_table_add(SIP_ConnHdlr comp_ref, CallidString call_id) |
| 194 | runs on SIP_Emulation_CT { |
| 195 | for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 196 | if (SipCallTable[i].call_id == "") { |
| 197 | SipCallTable[i].comp_ref := comp_ref; |
| 198 | SipCallTable[i].call_id := call_id; |
| 199 | log("Added SIP Call Table entry [", i, "] for ", call_id, " at ", comp_ref); |
| 200 | return; |
| 201 | } |
| 202 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 203 | testcase.stop("SIP Call table full"); |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | private function f_call_table_del(CallidString call_id) |
| 207 | runs on SIP_Emulation_CT { |
| 208 | for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 209 | if (SipCallTable[i].call_id == call_id) { |
| 210 | SipCallTable[i].comp_ref := null; |
| 211 | SipCallTable[i].call_id := ""; |
| 212 | log("Deleted SIP Call Table entry [", i, "] for ", call_id); |
| 213 | return; |
| 214 | } |
| 215 | } |
| 216 | setverdict(fail, "SIP Call table attempt to delete non-existant ", call_id); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 217 | mtc.stop; |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* call-back type, to be provided by specific implementation; called when new call connection |
| 221 | * arrives */ |
| 222 | type function SipCreateCallback(PDU_SIP_Request sip_req, charstring id) |
| 223 | runs on SIP_Emulation_CT return SIP_ConnHdlr; |
| 224 | |
| 225 | type record SipOps { |
| 226 | SipCreateCallback create_cb |
| 227 | }; |
| 228 | |
Pau Espin Pedrol | 1a77134 | 2024-04-25 14:42:50 +0200 | [diff] [blame^] | 229 | function f_init_sip(inout SIP_Emulation_CT ct, charstring id := "SIP_EMU") { |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 230 | var SipOps ops := { |
| 231 | create_cb := refers(SIP_Emulation.ExpectedCreateCallback) |
| 232 | }; |
| 233 | |
Pau Espin Pedrol | e94a648 | 2024-04-10 13:37:55 +0200 | [diff] [blame] | 234 | ct := SIP_Emulation_CT.create(id) alive; |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 235 | map(ct:SIP, system:SIP); |
| 236 | ct.start(SIP_Emulation.main(ops, id)); |
| 237 | } |
| 238 | |
| 239 | function main(SipOps ops, charstring id) |
| 240 | runs on SIP_Emulation_CT { |
| 241 | |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 242 | f_expect_table_init(); |
| 243 | f_call_table_init(); |
| 244 | |
| 245 | while (true) { |
| 246 | var SIP_ConnHdlr vc_hdlr, vc_conn; |
| 247 | var PDU_SIP_Request sip_req; |
| 248 | var PDU_SIP_Response sip_resp; |
| 249 | var SipUrl sip_to; |
| 250 | |
| 251 | alt { |
| 252 | /* SIP INVITE was received on SIP socket/port */ |
| 253 | [] SIP.receive(tr_SIP_INVITE) -> value sip_req { |
| 254 | var CallidString call_id := sip_req.msgHeader.callId.callid; |
| 255 | if (f_call_id_known(call_id)) { |
| 256 | /* re-invite? */ |
| 257 | vc_conn := f_comp_by_call_id(call_id); |
| 258 | } else { |
| 259 | /* new INVITE: check expect */ |
| 260 | vc_conn := ops.create_cb.apply(sip_req, id); |
| 261 | f_call_table_add(vc_conn, call_id); |
| 262 | } |
| 263 | CLIENT.send(sip_req) to vc_conn; |
| 264 | } |
| 265 | /* other SIP request was received on SIP socket/port */ |
| 266 | [] SIP.receive(PDU_SIP_Request:?) -> value sip_req { |
| 267 | var CallidString call_id := sip_req.msgHeader.callId.callid; |
| 268 | if (f_call_id_known(call_id)) { |
| 269 | vc_conn := f_comp_by_call_id(call_id); |
| 270 | CLIENT.send(sip_req) to vc_conn; |
| 271 | } else { |
| 272 | setverdict(fail, "SIP Request for unknown call ", call_id); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 273 | mtc.stop; |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | /* SIP response was received on SIP socket/port */ |
| 277 | [] SIP.receive(PDU_SIP_Response:?) -> value sip_resp { |
| 278 | var CallidString call_id := sip_resp.msgHeader.callId.callid; |
| 279 | if (f_call_id_known(call_id)) { |
| 280 | vc_conn := f_comp_by_call_id(call_id); |
| 281 | CLIENT.send(sip_resp) to vc_conn; |
| 282 | } else { |
| 283 | setverdict(fail, "SIP Response for unknown call ", call_id); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 284 | mtc.stop; |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Pau Espin Pedrol | 37ee0ed | 2024-03-28 21:17:12 +0100 | [diff] [blame] | 288 | /* a ConnHdlr is sending us a SIP REGISTER: Forward to SIP port */ |
| 289 | [] CLIENT.receive(tr_SIP_REGISTER) -> value sip_req sender vc_conn { |
| 290 | var CallidString call_id := sip_req.msgHeader.callId.callid; |
| 291 | if (f_call_id_known(call_id)) { |
| 292 | /* re-register */ |
| 293 | vc_conn := f_comp_by_call_id(call_id); |
| 294 | } else { |
| 295 | /* new REGISTER: add to table */ |
| 296 | f_call_table_add(vc_conn, call_id); |
| 297 | } |
| 298 | SIP.send(sip_req); |
| 299 | } |
| 300 | |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 301 | /* a ConnHdlr is sending us a SIP INVITE: Forward to SIP port */ |
| 302 | [] CLIENT.receive(tr_SIP_INVITE) -> value sip_req sender vc_conn { |
| 303 | var CallidString call_id := sip_req.msgHeader.callId.callid; |
| 304 | if (f_call_id_known(call_id)) { |
| 305 | /* re-invite? */ |
| 306 | vc_conn := f_comp_by_call_id(call_id); |
| 307 | } else { |
| 308 | /* new INVITE: add to table */ |
| 309 | f_call_table_add(vc_conn, call_id); |
| 310 | } |
| 311 | SIP.send(sip_req); |
| 312 | } |
| 313 | /* a ConnHdlr is sending us a SIP request: Forward to SIP port */ |
| 314 | [] CLIENT.receive(PDU_SIP_Request:?) -> value sip_req sender vc_conn { |
| 315 | SIP.send(sip_req); |
| 316 | } |
| 317 | /* a ConnHdlr is sending us a SIP request: Forward to SIP port */ |
| 318 | [] CLIENT.receive(PDU_SIP_Response:?) -> value sip_resp sender vc_conn { |
| 319 | SIP.send(sip_resp); |
| 320 | } |
| 321 | |
Pau Espin Pedrol | 0169e60 | 2024-04-08 20:59:19 +0200 | [diff] [blame] | 322 | [] CLIENT_PROC.getcall(SIPEM_register:{?,?}) -> param(sip_to, vc_hdlr) sender vc_conn { |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 323 | f_create_expect(sip_to, vc_hdlr); |
Pau Espin Pedrol | 0169e60 | 2024-04-08 20:59:19 +0200 | [diff] [blame] | 324 | CLIENT_PROC.reply(SIPEM_register:{sip_to, vc_hdlr}) to vc_conn; |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /*********************************************************************** |
| 332 | * "Expect" Handling (mapping for expected incoming SIP callds from IUT) |
| 333 | ***********************************************************************/ |
| 334 | |
| 335 | /* data about an expected future incoming connection */ |
| 336 | type record ExpectData { |
| 337 | /* SIP "To" (destination number) based on which we can match */ |
| 338 | SipUrl sip_to optional, |
| 339 | /* component reference registered for the connection */ |
| 340 | SIP_ConnHdlr vc_conn |
| 341 | } |
| 342 | |
| 343 | /* procedure based port to register for incoming calls */ |
| 344 | signature SIPEM_register(SipUrl sip_to, SIP_ConnHdlr vc_conn); |
| 345 | |
| 346 | type port SIPEM_PROC_PT procedure { |
| 347 | inout SIPEM_register; |
| 348 | } with { extension "internal" }; |
| 349 | |
| 350 | |
| 351 | /* CreateCallback that can be used as create_cb and will use the expect table */ |
| 352 | function ExpectedCreateCallback(PDU_SIP_Request sip_req, charstring id) |
| 353 | runs on SIP_Emulation_CT return SIP_ConnHdlr { |
| 354 | var SIP_ConnHdlr ret := null; |
| 355 | var SipUrl sip_to; |
| 356 | var integer i; |
| 357 | |
| 358 | if (sip_req.requestLine.method != INVITE_E) { |
| 359 | setverdict(fail, "SIP ExpectedCreateCallback needs INVITE"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 360 | mtc.stop |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 361 | return ret; |
| 362 | } |
| 363 | sip_to := sip_req.msgHeader.toField.addressField.nameAddr.addrSpec; |
| 364 | |
| 365 | for (i := 0; i < sizeof(SipExpectTable); i := i+1) { |
| 366 | if (not ispresent(SipExpectTable[i].sip_to)) { |
| 367 | continue; |
| 368 | } |
| 369 | /* build a template, use '*' for all 'omit' values */ |
| 370 | var template SipUrl t_exp := SipExpectTable[i].sip_to; |
| 371 | if (not ispresent(t_exp.hostPort.host)) { |
| 372 | t_exp.hostPort.host := *; |
| 373 | } |
| 374 | if (not ispresent(t_exp.hostPort.portField)) { |
| 375 | t_exp.hostPort.portField := *; |
Pau Espin Pedrol | 6331f5c | 2024-04-08 21:32:35 +0200 | [diff] [blame] | 376 | } else if (valueof(t_exp.hostPort.portField) == 5060) { |
| 377 | /* if the port number is 5060, it may be omitted */ |
| 378 | t_exp.hostPort.portField := 5060 ifpresent; |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 379 | } |
| 380 | if (not ispresent(t_exp.urlParameters)) { |
| 381 | t_exp.urlParameters := *; |
| 382 | } |
| 383 | if (not ispresent(t_exp.headers)) { |
| 384 | t_exp.headers := *; |
| 385 | } |
| 386 | /* match against the constructed template */ |
| 387 | if (match(sip_to, t_exp)) { |
| 388 | ret := SipExpectTable[i].vc_conn; |
| 389 | /* release this entry to be used again */ |
| 390 | SipExpectTable[i].sip_to := omit; |
| 391 | SipExpectTable[i].vc_conn := null; |
| 392 | log("Found SipExpect[", i, "] for ", sip_to, " handled at ", ret); |
| 393 | return ret; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | setverdict(fail, "Couldn't find SipExpect for incoming call ", sip_to); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 398 | mtc.stop |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | /* server/emulation side function to create expect */ |
| 403 | private function f_create_expect(SipUrl sip_to, SIP_ConnHdlr hdlr) |
| 404 | runs on SIP_Emulation_CT { |
| 405 | var integer i; |
| 406 | for (i := 0; i < sizeof(SipExpectTable); i := i+1) { |
| 407 | if (not ispresent(SipExpectTable[i].sip_to)) { |
| 408 | SipExpectTable[i].sip_to := sip_to; |
| 409 | SipExpectTable[i].vc_conn := hdlr; |
| 410 | log("Created SipExpect[", i, "] for ", sip_to, " to be handled at ", hdlr); |
| 411 | return; |
| 412 | } |
| 413 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 414 | testcase.stop("No space left in SipExpectTable"); |
Harald Welte | afec471 | 2018-03-19 22:52:17 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 418 | function f_create_sip_expect(SipUrl sip_to) runs on SIP_ConnHdlr { |
| 419 | SIP_PROC.call(SIPEM_register:{sip_to, self}) { |
| 420 | [] SIP_PROC.getreply(SIPEM_register:{?,?}) {}; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | |
| 425 | |
| 426 | } |