blob: 1b357b7e118e93b3d0ec21486b2586a06ee84b44 [file] [log] [blame]
Harald Welteafec4712018-03-19 22:52:17 +01001module 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 Welte34b5a952019-05-27 11:54:11 +020027 *
28 * SPDX-License-Identifier: GPL-2.0-or-later
Harald Welteafec4712018-03-19 22:52:17 +010029 */
30
31import from SIPmsg_Types all;
32import from SIPmsg_PortType all;
33
34type 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 */
41type port SIP_Conn_PT message {
42 inout PDU_SIP_Request, PDU_SIP_Response;
43} with { extension "internal" };
44
45/* represents a single SIP Call */
46type record CallData {
47 /* reference to the instance of the per-connection component */
48 SIP_ConnHdlr comp_ref,
49 CallidString call_id
50}
51
52type 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 Welteafec4712018-03-19 22:52:17 +010065template RequestLine tr_ReqLine(template Method method) := {
66 method := method,
67 requestUri := ?,
68 sipVersion := ?
69}
70
Pau Espin Pedrol37ee0ed2024-03-28 21:17:12 +010071private 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 Welteb0d93602018-03-20 18:09:34 +010078private template PDU_SIP_Request tr_SIP_INVITE := {
Harald Welteafec4712018-03-19 22:52:17 +010079 requestLine := tr_ReqLine(INVITE_E),
80 msgHeader := t_SIP_msgHeader_any,
81 messageBody := *,
82 payload := *
83}
84
Harald Welteafec4712018-03-19 22:52:17 +010085template 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}
100template (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
116template Addr_Union tr_SIP_Addr(template SipUrl sip_url) := {
117 nameAddr := {
118 displayName := *,
119 addrSpec := sip_url
120 }
121}
122template (value) Addr_Union ts_SIP_Addr(template (value) SipUrl sip_url) := {
123 nameAddr := {
124 displayName := omit,
125 addrSpec := sip_url
126 }
127}
128
129template To tr_SIP_To(template Addr_Union addr) := {
130 fieldName := TO_E,
131 addressField := addr,
132 toParams := *
133}
134template (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 */
141private function f_call_id_known(CallidString call_id)
142runs 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 */
153private function f_comp_by_call_id(CallidString call_id)
154runs 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 Willmanne4ff5372018-07-05 17:35:03 +0200162 mtc.stop;
Harald Welteafec4712018-03-19 22:52:17 +0100163}
164
165/* resolve connection ID by component reference */
166private function f_call_id_by_comp(SIP_ConnHdlr client)
167runs 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 Willmanne4ff5372018-07-05 17:35:03 +0200174 mtc.stop;
Harald Welteafec4712018-03-19 22:52:17 +0100175}
176
177private function f_expect_table_init()
178runs 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
185private function f_call_table_init()
186runs 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
193private function f_call_table_add(SIP_ConnHdlr comp_ref, CallidString call_id)
194runs 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 Willmanne4ff5372018-07-05 17:35:03 +0200203 testcase.stop("SIP Call table full");
Harald Welteafec4712018-03-19 22:52:17 +0100204}
205
206private function f_call_table_del(CallidString call_id)
207runs 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 Willmanne4ff5372018-07-05 17:35:03 +0200217 mtc.stop;
Harald Welteafec4712018-03-19 22:52:17 +0100218}
219
220/* call-back type, to be provided by specific implementation; called when new call connection
221 * arrives */
222type function SipCreateCallback(PDU_SIP_Request sip_req, charstring id)
223runs on SIP_Emulation_CT return SIP_ConnHdlr;
224
225type record SipOps {
226 SipCreateCallback create_cb
227};
228
229function f_init_sip(inout SIP_Emulation_CT ct, charstring id) {
230 id := id & "-SIP";
231
232 var SipOps ops := {
233 create_cb := refers(SIP_Emulation.ExpectedCreateCallback)
234 };
235
Pau Espin Pedrole94a6482024-04-10 13:37:55 +0200236 ct := SIP_Emulation_CT.create(id) alive;
Harald Welteafec4712018-03-19 22:52:17 +0100237 map(ct:SIP, system:SIP);
238 ct.start(SIP_Emulation.main(ops, id));
239}
240
241function main(SipOps ops, charstring id)
242runs on SIP_Emulation_CT {
243
Harald Welteafec4712018-03-19 22:52:17 +0100244 f_expect_table_init();
245 f_call_table_init();
246
247 while (true) {
248 var SIP_ConnHdlr vc_hdlr, vc_conn;
249 var PDU_SIP_Request sip_req;
250 var PDU_SIP_Response sip_resp;
251 var SipUrl sip_to;
252
253 alt {
254 /* SIP INVITE was received on SIP socket/port */
255 [] SIP.receive(tr_SIP_INVITE) -> value sip_req {
256 var CallidString call_id := sip_req.msgHeader.callId.callid;
257 if (f_call_id_known(call_id)) {
258 /* re-invite? */
259 vc_conn := f_comp_by_call_id(call_id);
260 } else {
261 /* new INVITE: check expect */
262 vc_conn := ops.create_cb.apply(sip_req, id);
263 f_call_table_add(vc_conn, call_id);
264 }
265 CLIENT.send(sip_req) to vc_conn;
266 }
267 /* other SIP request was received on SIP socket/port */
268 [] SIP.receive(PDU_SIP_Request:?) -> value sip_req {
269 var CallidString call_id := sip_req.msgHeader.callId.callid;
270 if (f_call_id_known(call_id)) {
271 vc_conn := f_comp_by_call_id(call_id);
272 CLIENT.send(sip_req) to vc_conn;
273 } else {
274 setverdict(fail, "SIP Request for unknown call ", call_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200275 mtc.stop;
Harald Welteafec4712018-03-19 22:52:17 +0100276 }
277 }
278 /* SIP response was received on SIP socket/port */
279 [] SIP.receive(PDU_SIP_Response:?) -> value sip_resp {
280 var CallidString call_id := sip_resp.msgHeader.callId.callid;
281 if (f_call_id_known(call_id)) {
282 vc_conn := f_comp_by_call_id(call_id);
283 CLIENT.send(sip_resp) to vc_conn;
284 } else {
285 setverdict(fail, "SIP Response for unknown call ", call_id);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200286 mtc.stop;
Harald Welteafec4712018-03-19 22:52:17 +0100287 }
288 }
289
Pau Espin Pedrol37ee0ed2024-03-28 21:17:12 +0100290 /* a ConnHdlr is sending us a SIP REGISTER: Forward to SIP port */
291 [] CLIENT.receive(tr_SIP_REGISTER) -> value sip_req sender vc_conn {
292 var CallidString call_id := sip_req.msgHeader.callId.callid;
293 if (f_call_id_known(call_id)) {
294 /* re-register */
295 vc_conn := f_comp_by_call_id(call_id);
296 } else {
297 /* new REGISTER: add to table */
298 f_call_table_add(vc_conn, call_id);
299 }
300 SIP.send(sip_req);
301 }
302
Harald Welteafec4712018-03-19 22:52:17 +0100303 /* a ConnHdlr is sending us a SIP INVITE: Forward to SIP port */
304 [] CLIENT.receive(tr_SIP_INVITE) -> value sip_req sender vc_conn {
305 var CallidString call_id := sip_req.msgHeader.callId.callid;
306 if (f_call_id_known(call_id)) {
307 /* re-invite? */
308 vc_conn := f_comp_by_call_id(call_id);
309 } else {
310 /* new INVITE: add to table */
311 f_call_table_add(vc_conn, call_id);
312 }
313 SIP.send(sip_req);
314 }
315 /* a ConnHdlr is sending us a SIP request: Forward to SIP port */
316 [] CLIENT.receive(PDU_SIP_Request:?) -> value sip_req sender vc_conn {
317 SIP.send(sip_req);
318 }
319 /* a ConnHdlr is sending us a SIP request: Forward to SIP port */
320 [] CLIENT.receive(PDU_SIP_Response:?) -> value sip_resp sender vc_conn {
321 SIP.send(sip_resp);
322 }
323
Pau Espin Pedrol0169e602024-04-08 20:59:19 +0200324 [] CLIENT_PROC.getcall(SIPEM_register:{?,?}) -> param(sip_to, vc_hdlr) sender vc_conn {
Harald Welteafec4712018-03-19 22:52:17 +0100325 f_create_expect(sip_to, vc_hdlr);
Pau Espin Pedrol0169e602024-04-08 20:59:19 +0200326 CLIENT_PROC.reply(SIPEM_register:{sip_to, vc_hdlr}) to vc_conn;
Harald Welteafec4712018-03-19 22:52:17 +0100327 }
328
329 }
330 }
331}
332
333/***********************************************************************
334 * "Expect" Handling (mapping for expected incoming SIP callds from IUT)
335 ***********************************************************************/
336
337/* data about an expected future incoming connection */
338type record ExpectData {
339 /* SIP "To" (destination number) based on which we can match */
340 SipUrl sip_to optional,
341 /* component reference registered for the connection */
342 SIP_ConnHdlr vc_conn
343}
344
345/* procedure based port to register for incoming calls */
346signature SIPEM_register(SipUrl sip_to, SIP_ConnHdlr vc_conn);
347
348type port SIPEM_PROC_PT procedure {
349 inout SIPEM_register;
350} with { extension "internal" };
351
352
353/* CreateCallback that can be used as create_cb and will use the expect table */
354function ExpectedCreateCallback(PDU_SIP_Request sip_req, charstring id)
355runs on SIP_Emulation_CT return SIP_ConnHdlr {
356 var SIP_ConnHdlr ret := null;
357 var SipUrl sip_to;
358 var integer i;
359
360 if (sip_req.requestLine.method != INVITE_E) {
361 setverdict(fail, "SIP ExpectedCreateCallback needs INVITE");
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200362 mtc.stop
Harald Welteafec4712018-03-19 22:52:17 +0100363 return ret;
364 }
365 sip_to := sip_req.msgHeader.toField.addressField.nameAddr.addrSpec;
366
367 for (i := 0; i < sizeof(SipExpectTable); i := i+1) {
368 if (not ispresent(SipExpectTable[i].sip_to)) {
369 continue;
370 }
371 /* build a template, use '*' for all 'omit' values */
372 var template SipUrl t_exp := SipExpectTable[i].sip_to;
373 if (not ispresent(t_exp.hostPort.host)) {
374 t_exp.hostPort.host := *;
375 }
376 if (not ispresent(t_exp.hostPort.portField)) {
377 t_exp.hostPort.portField := *;
Pau Espin Pedrol6331f5c2024-04-08 21:32:35 +0200378 } else if (valueof(t_exp.hostPort.portField) == 5060) {
379 /* if the port number is 5060, it may be omitted */
380 t_exp.hostPort.portField := 5060 ifpresent;
Harald Welteafec4712018-03-19 22:52:17 +0100381 }
382 if (not ispresent(t_exp.urlParameters)) {
383 t_exp.urlParameters := *;
384 }
385 if (not ispresent(t_exp.headers)) {
386 t_exp.headers := *;
387 }
388 /* match against the constructed template */
389 if (match(sip_to, t_exp)) {
390 ret := SipExpectTable[i].vc_conn;
391 /* release this entry to be used again */
392 SipExpectTable[i].sip_to := omit;
393 SipExpectTable[i].vc_conn := null;
394 log("Found SipExpect[", i, "] for ", sip_to, " handled at ", ret);
395 return ret;
396 }
397 }
398
399 setverdict(fail, "Couldn't find SipExpect for incoming call ", sip_to);
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200400 mtc.stop
Harald Welteafec4712018-03-19 22:52:17 +0100401 return ret;
402}
403
404/* server/emulation side function to create expect */
405private function f_create_expect(SipUrl sip_to, SIP_ConnHdlr hdlr)
406runs on SIP_Emulation_CT {
407 var integer i;
408 for (i := 0; i < sizeof(SipExpectTable); i := i+1) {
409 if (not ispresent(SipExpectTable[i].sip_to)) {
410 SipExpectTable[i].sip_to := sip_to;
411 SipExpectTable[i].vc_conn := hdlr;
412 log("Created SipExpect[", i, "] for ", sip_to, " to be handled at ", hdlr);
413 return;
414 }
415 }
Daniel Willmanne4ff5372018-07-05 17:35:03 +0200416 testcase.stop("No space left in SipExpectTable");
Harald Welteafec4712018-03-19 22:52:17 +0100417}
418
419/* client/conn_hdlr side function to use procedure port to create expect in emulation */
420function f_create_sip_expect(SipUrl sip_to) runs on SIP_ConnHdlr {
421 SIP_PROC.call(SIPEM_register:{sip_to, self}) {
422 [] SIP_PROC.getreply(SIPEM_register:{?,?}) {};
423 }
424}
425
426
427
428}