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. |
| 27 | */ |
| 28 | |
| 29 | import from SIPmsg_Types all; |
| 30 | import from SIPmsg_PortType all; |
| 31 | |
| 32 | type component SIP_ConnHdlr { |
| 33 | /* ports towards SIP Emulation core / call dispatcher */ |
| 34 | port SIP_Conn_PT SIP; |
| 35 | port SIPEM_PROC_PT SIP_PROC; |
| 36 | } |
| 37 | |
| 38 | /* port between individual per-call components and this dispatcher */ |
| 39 | type port SIP_Conn_PT message { |
| 40 | inout PDU_SIP_Request, PDU_SIP_Response; |
| 41 | } with { extension "internal" }; |
| 42 | |
| 43 | /* represents a single SIP Call */ |
| 44 | type record CallData { |
| 45 | /* reference to the instance of the per-connection component */ |
| 46 | SIP_ConnHdlr comp_ref, |
| 47 | CallidString call_id |
| 48 | } |
| 49 | |
| 50 | type component SIP_Emulation_CT { |
| 51 | /* SIP test port on bottom side */ |
| 52 | port SIPmsg_PT SIP; |
| 53 | /* SIP port to the per-call clients */ |
| 54 | port SIP_Conn_PT CLIENT; |
| 55 | |
| 56 | var CallData SipCallTable[16]; |
| 57 | var ExpectData SipExpectTable[16]; |
| 58 | |
| 59 | /* procedure based port to register for incoming connections */ |
| 60 | port SIPEM_PROC_PT CLIENT_PROC; |
| 61 | }; |
| 62 | |
| 63 | private function f_sip_init() runs on SIP_Emulation_CT { |
| 64 | map(self:SIP, system:SIP); |
| 65 | } |
| 66 | |
| 67 | template RequestLine tr_ReqLine(template Method method) := { |
| 68 | method := method, |
| 69 | requestUri := ?, |
| 70 | sipVersion := ? |
| 71 | } |
| 72 | |
| 73 | template PDU_SIP_Request tr_SIP_INVITE := { |
| 74 | requestLine := tr_ReqLine(INVITE_E), |
| 75 | msgHeader := t_SIP_msgHeader_any, |
| 76 | messageBody := *, |
| 77 | payload := * |
| 78 | } |
| 79 | |
| 80 | |
| 81 | template SipUrl tr_SIP_Url(template charstring user_or_num, |
| 82 | template charstring host := *, |
| 83 | template integer portField := *) := { |
| 84 | scheme := "sip", |
| 85 | userInfo := { |
| 86 | userOrTelephoneSubscriber := user_or_num, |
| 87 | password := * |
| 88 | }, |
| 89 | hostPort := { |
| 90 | host := host, |
| 91 | portField := portField |
| 92 | }, |
| 93 | urlParameters := *, |
| 94 | headers := * |
| 95 | } |
| 96 | template (value) SipUrl ts_SIP_Url(charstring user_or_num, |
| 97 | template (omit) charstring host := omit, |
| 98 | template (omit) integer portField := omit) := { |
| 99 | scheme := "sip", |
| 100 | userInfo := { |
| 101 | userOrTelephoneSubscriber := user_or_num, |
| 102 | password := omit |
| 103 | }, |
| 104 | hostPort := { |
| 105 | host := host, |
| 106 | portField := portField |
| 107 | }, |
| 108 | urlParameters := omit, |
| 109 | headers := omit |
| 110 | } |
| 111 | |
| 112 | template Addr_Union tr_SIP_Addr(template SipUrl sip_url) := { |
| 113 | nameAddr := { |
| 114 | displayName := *, |
| 115 | addrSpec := sip_url |
| 116 | } |
| 117 | } |
| 118 | template (value) Addr_Union ts_SIP_Addr(template (value) SipUrl sip_url) := { |
| 119 | nameAddr := { |
| 120 | displayName := omit, |
| 121 | addrSpec := sip_url |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | template To tr_SIP_To(template Addr_Union addr) := { |
| 126 | fieldName := TO_E, |
| 127 | addressField := addr, |
| 128 | toParams := * |
| 129 | } |
| 130 | template (value) To ts_SIP_To(template (value) Addr_Union addr) := { |
| 131 | fieldName := TO_E, |
| 132 | addressField := addr, |
| 133 | toParams := omit |
| 134 | } |
| 135 | |
| 136 | /* resolve component reference by connection ID */ |
| 137 | private function f_call_id_known(CallidString call_id) |
| 138 | runs on SIP_Emulation_CT return boolean { |
| 139 | var integer i; |
| 140 | for (i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 141 | if (SipCallTable[i].call_id == call_id) { |
| 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | /* resolve component reference by connection ID */ |
| 149 | private function f_comp_by_call_id(CallidString call_id) |
| 150 | runs on SIP_Emulation_CT return SIP_ConnHdlr { |
| 151 | var integer i; |
| 152 | for (i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 153 | if (SipCallTable[i].call_id == call_id) { |
| 154 | return SipCallTable[i].comp_ref; |
| 155 | } |
| 156 | } |
| 157 | setverdict(fail, "SIP Call table not found by SIP Call ID ", call_id); |
| 158 | self.stop; |
| 159 | } |
| 160 | |
| 161 | /* resolve connection ID by component reference */ |
| 162 | private function f_call_id_by_comp(SIP_ConnHdlr client) |
| 163 | runs on SIP_Emulation_CT return CallidString { |
| 164 | for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 165 | if (SipCallTable[i].comp_ref == client) { |
| 166 | return SipCallTable[i].call_id; |
| 167 | } |
| 168 | } |
| 169 | setverdict(fail, "SIP Call table not found by component ", client); |
| 170 | self.stop; |
| 171 | } |
| 172 | |
| 173 | private function f_expect_table_init() |
| 174 | runs on SIP_Emulation_CT { |
| 175 | for (var integer i := 0; i < sizeof(SipExpectTable); i := i+1) { |
| 176 | SipExpectTable[i].sip_to := omit; |
| 177 | SipExpectTable[i].vc_conn := null; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private function f_call_table_init() |
| 182 | runs on SIP_Emulation_CT { |
| 183 | for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 184 | SipCallTable[i].comp_ref := null; |
| 185 | SipCallTable[i].call_id := ""; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | private function f_call_table_add(SIP_ConnHdlr comp_ref, CallidString call_id) |
| 190 | runs on SIP_Emulation_CT { |
| 191 | for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 192 | if (SipCallTable[i].call_id == "") { |
| 193 | SipCallTable[i].comp_ref := comp_ref; |
| 194 | SipCallTable[i].call_id := call_id; |
| 195 | log("Added SIP Call Table entry [", i, "] for ", call_id, " at ", comp_ref); |
| 196 | return; |
| 197 | } |
| 198 | } |
| 199 | setverdict(fail, "SIP Call table full"); |
| 200 | self.stop; |
| 201 | } |
| 202 | |
| 203 | private function f_call_table_del(CallidString call_id) |
| 204 | runs on SIP_Emulation_CT { |
| 205 | for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) { |
| 206 | if (SipCallTable[i].call_id == call_id) { |
| 207 | SipCallTable[i].comp_ref := null; |
| 208 | SipCallTable[i].call_id := ""; |
| 209 | log("Deleted SIP Call Table entry [", i, "] for ", call_id); |
| 210 | return; |
| 211 | } |
| 212 | } |
| 213 | setverdict(fail, "SIP Call table attempt to delete non-existant ", call_id); |
| 214 | self.stop; |
| 215 | } |
| 216 | |
| 217 | /* call-back type, to be provided by specific implementation; called when new call connection |
| 218 | * arrives */ |
| 219 | type function SipCreateCallback(PDU_SIP_Request sip_req, charstring id) |
| 220 | runs on SIP_Emulation_CT return SIP_ConnHdlr; |
| 221 | |
| 222 | type record SipOps { |
| 223 | SipCreateCallback create_cb |
| 224 | }; |
| 225 | |
| 226 | function f_init_sip(inout SIP_Emulation_CT ct, charstring id) { |
| 227 | id := id & "-SIP"; |
| 228 | |
| 229 | var SipOps ops := { |
| 230 | create_cb := refers(SIP_Emulation.ExpectedCreateCallback) |
| 231 | }; |
| 232 | |
| 233 | ct := SIP_Emulation_CT.create(id); |
| 234 | map(ct:SIP, system:SIP); |
| 235 | ct.start(SIP_Emulation.main(ops, id)); |
| 236 | } |
| 237 | |
| 238 | function main(SipOps ops, charstring id) |
| 239 | runs on SIP_Emulation_CT { |
| 240 | |
| 241 | f_sip_init(); |
| 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); |
| 273 | self.stop; |
| 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); |
| 284 | self.stop; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* a ConnHdlr is sending us a SIP INVITE: Forward to SIP port */ |
| 289 | [] CLIENT.receive(tr_SIP_INVITE) -> 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-invite? */ |
| 293 | vc_conn := f_comp_by_call_id(call_id); |
| 294 | } else { |
| 295 | /* new INVITE: add to table */ |
| 296 | f_call_table_add(vc_conn, call_id); |
| 297 | } |
| 298 | SIP.send(sip_req); |
| 299 | } |
| 300 | /* a ConnHdlr is sending us a SIP request: Forward to SIP port */ |
| 301 | [] CLIENT.receive(PDU_SIP_Request:?) -> value sip_req sender vc_conn { |
| 302 | SIP.send(sip_req); |
| 303 | } |
| 304 | /* a ConnHdlr is sending us a SIP request: Forward to SIP port */ |
| 305 | [] CLIENT.receive(PDU_SIP_Response:?) -> value sip_resp sender vc_conn { |
| 306 | SIP.send(sip_resp); |
| 307 | } |
| 308 | |
| 309 | [] CLIENT_PROC.getcall(SIPEM_register:{?,?}) -> param(sip_to, vc_hdlr) { |
| 310 | f_create_expect(sip_to, vc_hdlr); |
| 311 | CLIENT_PROC.reply(SIPEM_register:{sip_to, vc_hdlr}); |
| 312 | } |
| 313 | |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /*********************************************************************** |
| 319 | * "Expect" Handling (mapping for expected incoming SIP callds from IUT) |
| 320 | ***********************************************************************/ |
| 321 | |
| 322 | /* data about an expected future incoming connection */ |
| 323 | type record ExpectData { |
| 324 | /* SIP "To" (destination number) based on which we can match */ |
| 325 | SipUrl sip_to optional, |
| 326 | /* component reference registered for the connection */ |
| 327 | SIP_ConnHdlr vc_conn |
| 328 | } |
| 329 | |
| 330 | /* procedure based port to register for incoming calls */ |
| 331 | signature SIPEM_register(SipUrl sip_to, SIP_ConnHdlr vc_conn); |
| 332 | |
| 333 | type port SIPEM_PROC_PT procedure { |
| 334 | inout SIPEM_register; |
| 335 | } with { extension "internal" }; |
| 336 | |
| 337 | |
| 338 | /* CreateCallback that can be used as create_cb and will use the expect table */ |
| 339 | function ExpectedCreateCallback(PDU_SIP_Request sip_req, charstring id) |
| 340 | runs on SIP_Emulation_CT return SIP_ConnHdlr { |
| 341 | var SIP_ConnHdlr ret := null; |
| 342 | var SipUrl sip_to; |
| 343 | var integer i; |
| 344 | |
| 345 | if (sip_req.requestLine.method != INVITE_E) { |
| 346 | setverdict(fail, "SIP ExpectedCreateCallback needs INVITE"); |
| 347 | return ret; |
| 348 | } |
| 349 | sip_to := sip_req.msgHeader.toField.addressField.nameAddr.addrSpec; |
| 350 | |
| 351 | for (i := 0; i < sizeof(SipExpectTable); i := i+1) { |
| 352 | if (not ispresent(SipExpectTable[i].sip_to)) { |
| 353 | continue; |
| 354 | } |
| 355 | /* build a template, use '*' for all 'omit' values */ |
| 356 | var template SipUrl t_exp := SipExpectTable[i].sip_to; |
| 357 | if (not ispresent(t_exp.hostPort.host)) { |
| 358 | t_exp.hostPort.host := *; |
| 359 | } |
| 360 | if (not ispresent(t_exp.hostPort.portField)) { |
| 361 | t_exp.hostPort.portField := *; |
| 362 | } |
| 363 | if (not ispresent(t_exp.urlParameters)) { |
| 364 | t_exp.urlParameters := *; |
| 365 | } |
| 366 | if (not ispresent(t_exp.headers)) { |
| 367 | t_exp.headers := *; |
| 368 | } |
| 369 | /* match against the constructed template */ |
| 370 | if (match(sip_to, t_exp)) { |
| 371 | ret := SipExpectTable[i].vc_conn; |
| 372 | /* release this entry to be used again */ |
| 373 | SipExpectTable[i].sip_to := omit; |
| 374 | SipExpectTable[i].vc_conn := null; |
| 375 | log("Found SipExpect[", i, "] for ", sip_to, " handled at ", ret); |
| 376 | return ret; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | setverdict(fail, "Couldn't find SipExpect for incoming call ", sip_to); |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | /* server/emulation side function to create expect */ |
| 385 | private function f_create_expect(SipUrl sip_to, SIP_ConnHdlr hdlr) |
| 386 | runs on SIP_Emulation_CT { |
| 387 | var integer i; |
| 388 | for (i := 0; i < sizeof(SipExpectTable); i := i+1) { |
| 389 | if (not ispresent(SipExpectTable[i].sip_to)) { |
| 390 | SipExpectTable[i].sip_to := sip_to; |
| 391 | SipExpectTable[i].vc_conn := hdlr; |
| 392 | log("Created SipExpect[", i, "] for ", sip_to, " to be handled at ", hdlr); |
| 393 | return; |
| 394 | } |
| 395 | } |
| 396 | setverdict(fail, "No space left in SipExpectTable"); |
| 397 | } |
| 398 | |
| 399 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 400 | function f_create_sip_expect(SipUrl sip_to) runs on SIP_ConnHdlr { |
| 401 | SIP_PROC.call(SIPEM_register:{sip_to, self}) { |
| 402 | [] SIP_PROC.getreply(SIPEM_register:{?,?}) {}; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | |
| 407 | |
| 408 | } |