summaryrefslogtreecommitdiffstats
path: root/library/SIP_Emulation.ttcn
blob: 98f73710fe0c9fd0608c8e9ba62d31746949086a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
module SIP_Emulation {

/* SIP Emulation, runs on top of SIPmsg_PT.  It multiplexes/demultiplexes
 * the individual calls, so there can be separate TTCN-3 components handling
 * each of the calls
 *
 * The SIP_Emulation.main() function processes SIP message from the SIPmsg
 * socket via the SIPmsg_PT, and dispatches them to the per-connection components.
 *
 * Outbound SIP calls are initiated by sending a PDU_SIP_Request messages
 * to the component running the SIP_Emulation.main() function.
 *
 * For each new inbound call, the SipOps.create_cb() is called.  It can create
 * or resolve a TTCN-3 component, and returns a component reference to which that inbound
 * call is routed/dispatched.
 *
 * If a pre-existing component wants to register to handle a future inbound call, it can
 * do so by registering an "expect" with the expected destination phone number.  This is e.g. useful
 * if you are simulating MNCC + SIP, and first trigger a connection from MNCC side in a
 * component which then subsequently should also handle the SIP emulation.
 *
 * (C) 2018 by Harald Welte <laforge@gnumonks.org>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 */

import from SIPmsg_Types all;
import from SIPmsg_PortType all;

type component SIP_ConnHdlr {
	/* ports towards SIP Emulation core / call dispatcher */
	port SIP_Conn_PT SIP;
	port SIPEM_PROC_PT SIP_PROC;
}

/* port between individual per-call components and this dispatcher */
type port SIP_Conn_PT message {
	inout PDU_SIP_Request, PDU_SIP_Response;
} with { extension "internal" };

/* represents a single SIP Call */
type record CallData {
	/* reference to the instance of the per-connection component */
	SIP_ConnHdlr	comp_ref,
	CallidString	call_id
}

type component SIP_Emulation_CT {
	/* SIP test port on bottom side */
	port SIPmsg_PT SIP;
	/* SIP port to the per-call clients */
	port SIP_Conn_PT CLIENT;

	var CallData SipCallTable[16];
	var ExpectData SipExpectTable[16];

	/* procedure based port to register for incoming connections */
	port SIPEM_PROC_PT CLIENT_PROC;
};

private function f_sip_init() runs on SIP_Emulation_CT {
	map(self:SIP, system:SIP);
}

template RequestLine tr_ReqLine(template Method method) := {
	method := method,
	requestUri := ?,
	sipVersion := ?
}

private template PDU_SIP_Request tr_SIP_INVITE := {
	requestLine := tr_ReqLine(INVITE_E),
	msgHeader := t_SIP_msgHeader_any,
	messageBody := *,
	payload := *
}


template SipUrl tr_SIP_Url(template charstring user_or_num,
			   template charstring host := *,
			   template integer portField := *) := {
	scheme := "sip",
	userInfo := {
		userOrTelephoneSubscriber := user_or_num,
		password := *
	},
	hostPort := {
		host := host,
		portField := portField
	},
	urlParameters := *,
	headers := *
}
template (value) SipUrl ts_SIP_Url(charstring user_or_num,
			   template (omit) charstring host := omit,
			   template (omit) integer portField := omit) := {
	scheme := "sip",
	userInfo := {
		userOrTelephoneSubscriber := user_or_num,
		password := omit
	},
	hostPort := {
		host := host,
		portField := portField
	},
	urlParameters := omit,
	headers := omit
}

template Addr_Union tr_SIP_Addr(template SipUrl sip_url) := {
	nameAddr := {
		displayName := *,
		addrSpec := sip_url
	}
}
template (value) Addr_Union ts_SIP_Addr(template (value) SipUrl sip_url) := {
	nameAddr := {
		displayName := omit,
		addrSpec := sip_url
	}
}

template To tr_SIP_To(template Addr_Union addr) := {
	fieldName := TO_E,
	addressField := addr,
	toParams := *
}
template (value) To ts_SIP_To(template (value) Addr_Union addr) := {
	fieldName := TO_E,
	addressField := addr,
	toParams := omit
}

/* resolve component reference by connection ID */
private function f_call_id_known(CallidString call_id)
runs on SIP_Emulation_CT return boolean {
	var integer i;
	for (i := 0; i < sizeof(SipCallTable); i := i+1) {
		if (SipCallTable[i].call_id == call_id) {
			return true;
		}
	}
	return false;
}

/* resolve component reference by connection ID */
private function f_comp_by_call_id(CallidString call_id)
runs on SIP_Emulation_CT return SIP_ConnHdlr {
	var integer i;
	for (i := 0; i < sizeof(SipCallTable); i := i+1) {
		if (SipCallTable[i].call_id == call_id) {
			return SipCallTable[i].comp_ref;
		}
	}
	setverdict(fail, "SIP Call table not found by SIP Call ID ", call_id);
	mtc.stop;
}

/* resolve connection ID by component reference */
private function f_call_id_by_comp(SIP_ConnHdlr client)
runs on SIP_Emulation_CT return CallidString {
	for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) {
		if (SipCallTable[i].comp_ref == client) {
			return SipCallTable[i].call_id;
		}
	}
	setverdict(fail, "SIP Call table not found by component ", client);
	mtc.stop;
}

private function f_expect_table_init()
runs on SIP_Emulation_CT {
	for (var integer i := 0; i < sizeof(SipExpectTable); i := i+1) {
		SipExpectTable[i].sip_to := omit;
		SipExpectTable[i].vc_conn := null;
	}
}

private function f_call_table_init()
runs on SIP_Emulation_CT {
	for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) {
		SipCallTable[i].comp_ref := null;
		SipCallTable[i].call_id := "";
	}
}

private function f_call_table_add(SIP_ConnHdlr comp_ref, CallidString call_id)
runs on SIP_Emulation_CT {
	for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) {
		if (SipCallTable[i].call_id == "") {
			SipCallTable[i].comp_ref := comp_ref;
			SipCallTable[i].call_id := call_id;
			log("Added SIP Call Table entry [", i, "] for ", call_id, " at ", comp_ref);
			return;
		}
	}
	testcase.stop("SIP Call table full");
}

private function f_call_table_del(CallidString call_id)
runs on SIP_Emulation_CT {
	for (var integer i := 0; i < sizeof(SipCallTable); i := i+1) {
		if (SipCallTable[i].call_id == call_id) {
			SipCallTable[i].comp_ref := null;
			SipCallTable[i].call_id := "";
			log("Deleted SIP Call Table entry [", i, "] for ", call_id);
			return;
		}
	}
	setverdict(fail, "SIP Call table attempt to delete non-existant ", call_id);
	mtc.stop;
}

/* call-back type, to be provided by specific implementation; called when new call connection
 * arrives */
type function SipCreateCallback(PDU_SIP_Request sip_req, charstring id)
runs on SIP_Emulation_CT return SIP_ConnHdlr;

type record SipOps {
	SipCreateCallback create_cb
};

function f_init_sip(inout SIP_Emulation_CT ct, charstring id) {
	id := id & "-SIP";

	var SipOps ops := {
		create_cb := refers(SIP_Emulation.ExpectedCreateCallback)
	};

	ct := SIP_Emulation_CT.create(id);
	map(ct:SIP, system:SIP);
	ct.start(SIP_Emulation.main(ops, id));
}

function main(SipOps ops, charstring id)
runs on SIP_Emulation_CT {

	f_sip_init();
	f_expect_table_init();
	f_call_table_init();

	while (true) {
		var SIP_ConnHdlr vc_hdlr, vc_conn;
		var PDU_SIP_Request sip_req;
		var PDU_SIP_Response sip_resp;
		var SipUrl sip_to;

		alt {
		/* SIP INVITE was received on SIP socket/port */
		[] SIP.receive(tr_SIP_INVITE) -> value sip_req {
			var CallidString call_id := sip_req.msgHeader.callId.callid;
			if (f_call_id_known(call_id)) {
				/* re-invite? */
				vc_conn := f_comp_by_call_id(call_id);
			} else {
				/* new INVITE: check expect */
				vc_conn := ops.create_cb.apply(sip_req, id);
				f_call_table_add(vc_conn, call_id);
			}
			CLIENT.send(sip_req) to vc_conn;
			}
		/* other SIP request was received on SIP socket/port */
		[] SIP.receive(PDU_SIP_Request:?) -> value sip_req {
			var CallidString call_id := sip_req.msgHeader.callId.callid;
			if (f_call_id_known(call_id)) {
				vc_conn := f_comp_by_call_id(call_id);
				CLIENT.send(sip_req) to vc_conn;
			} else {
				setverdict(fail, "SIP Request for unknown call ", call_id);
				mtc.stop;
			}
			}
		/* SIP response was received on SIP socket/port */
		[] SIP.receive(PDU_SIP_Response:?) -> value sip_resp {
			var CallidString call_id := sip_resp.msgHeader.callId.callid;
			if (f_call_id_known(call_id)) {
				vc_conn := f_comp_by_call_id(call_id);
				CLIENT.send(sip_resp) to vc_conn;
			} else {
				setverdict(fail, "SIP Response for unknown call ", call_id);
				mtc.stop;
			}
			}

		/* a ConnHdlr is sending us a SIP INVITE: Forward to SIP port */
		[] CLIENT.receive(tr_SIP_INVITE) -> value sip_req sender vc_conn {
			var CallidString call_id := sip_req.msgHeader.callId.callid;
			if (f_call_id_known(call_id)) {
				/* re-invite? */
				vc_conn := f_comp_by_call_id(call_id);
			} else {
				/* new INVITE: add to table */
				f_call_table_add(vc_conn, call_id);
			}
			SIP.send(sip_req);
			}
		/* a ConnHdlr is sending us a SIP request: Forward to SIP port */
		[] CLIENT.receive(PDU_SIP_Request:?) -> value sip_req sender vc_conn {
			SIP.send(sip_req);
			}
		/* a ConnHdlr is sending us a SIP request: Forward to SIP port */
		[] CLIENT.receive(PDU_SIP_Response:?) -> value sip_resp sender vc_conn {
			SIP.send(sip_resp);
			}

		[] CLIENT_PROC.getcall(SIPEM_register:{?,?}) -> param(sip_to, vc_hdlr) {
			f_create_expect(sip_to, vc_hdlr);
			CLIENT_PROC.reply(SIPEM_register:{sip_to, vc_hdlr});
			}

		}
	}
}

/***********************************************************************
 * "Expect" Handling (mapping for expected incoming SIP callds from IUT)
 ***********************************************************************/

/* data about an expected future incoming connection */
type record ExpectData {
	/* SIP "To" (destination number) based on which we can match */
	SipUrl sip_to optional,
	/* component reference registered for the connection */
	SIP_ConnHdlr vc_conn
}

/* procedure based port to register for incoming calls */
signature SIPEM_register(SipUrl sip_to, SIP_ConnHdlr vc_conn);

type port SIPEM_PROC_PT procedure {
	inout SIPEM_register;
} with { extension "internal" };


/* CreateCallback that can be used as create_cb and will use the expect table */
function ExpectedCreateCallback(PDU_SIP_Request sip_req, charstring id)
runs on SIP_Emulation_CT return SIP_ConnHdlr {
	var SIP_ConnHdlr ret := null;
	var SipUrl sip_to;
	var integer i;

	if (sip_req.requestLine.method != INVITE_E) {
		setverdict(fail, "SIP ExpectedCreateCallback needs INVITE");
		mtc.stop
		return ret;
	}
	sip_to := sip_req.msgHeader.toField.addressField.nameAddr.addrSpec;

	for (i := 0; i < sizeof(SipExpectTable); i := i+1) {
		if (not ispresent(SipExpectTable[i].sip_to)) {
			continue;
		}
		/* build a template, use '*' for all 'omit' values */
		var template SipUrl t_exp := SipExpectTable[i].sip_to;
		if (not ispresent(t_exp.hostPort.host)) {
			t_exp.hostPort.host := *;
		}
		if (not ispresent(t_exp.hostPort.portField)) {
			t_exp.hostPort.portField := *;
		}
		if (not ispresent(t_exp.urlParameters)) {
			t_exp.urlParameters := *;
		}
		if (not ispresent(t_exp.headers)) {
			t_exp.headers := *;
		}
		/* match against the constructed template */
		if (match(sip_to, t_exp)) {
			ret := SipExpectTable[i].vc_conn;
			/* release this entry to be used again */
			SipExpectTable[i].sip_to := omit;
			SipExpectTable[i].vc_conn := null;
			log("Found SipExpect[", i, "] for ", sip_to, " handled at ", ret);
			return ret;
		}
	}

	setverdict(fail, "Couldn't find SipExpect for incoming call ", sip_to);
	mtc.stop
	return ret;
}

/* server/emulation side function to create expect */
private function f_create_expect(SipUrl sip_to, SIP_ConnHdlr hdlr)
runs on SIP_Emulation_CT {
	var integer i;
	for (i := 0; i < sizeof(SipExpectTable); i := i+1) {
		if (not ispresent(SipExpectTable[i].sip_to)) {
			SipExpectTable[i].sip_to := sip_to;
			SipExpectTable[i].vc_conn := hdlr;
			log("Created SipExpect[", i, "] for ", sip_to, " to be handled at ", hdlr);
			return;
		}
	}
	testcase.stop("No space left in SipExpectTable");
}

/* client/conn_hdlr side function to use procedure port to create expect in emulation */
function f_create_sip_expect(SipUrl sip_to) runs on SIP_ConnHdlr {
	SIP_PROC.call(SIPEM_register:{sip_to, self}) {
		[] SIP_PROC.getreply(SIPEM_register:{?,?}) {};
	}
}



}