summaryrefslogtreecommitdiffstats
path: root/library/SIP_Templates.ttcn
blob: 75681a912d52016cee369afb32b26268d1e147ff (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
410
411
412
413
module SIP_Templates {

import from SIPmsg_Types all;

/* wrapper type to encapsulate the Addr_Union + parameter list used in From, To. ... */
type record SipAddr {
	Addr_Union 		addr,
	SemicolonParam_List	params optional
}

const charstring c_SIP_VERSION := "SIP/2.0";

template (value) SipUrl ts_SipUrl(charstring user_or_tel, charstring host, integer portnr) := {
	scheme := "sip",
	userInfo := {
		userOrTelephoneSubscriber := user_or_tel,
		password := omit
	},
	hostPort := {
		host := host,
		portField := portnr
	},
	urlParameters := omit,
	headers := omit
}
template SipUrl tr_SipUrl(template charstring user_or_tel,
			  template charstring host,
			  template integer portnr) := {
	scheme := "sip",
	userInfo := {
		userOrTelephoneSubscriber := user_or_tel,
		password := *
	},
	hostPort := {
		host := host,
		portField := portnr
	},
	urlParameters := *,
	headers := *
}

template (value) SipAddr ts_SipAddr(charstring user_or_tel, charstring host, integer portnr) := {
	addr := {
		nameAddr := {
			displayName := omit,
			addrSpec := ts_SipUrl(user_or_tel, host, portnr)
		}
	},
	params := omit
}
template SipAddr tr_SipAddr(template charstring user_or_tel,
				template charstring host,
				template integer portnr) := {
	addr := {
		nameAddr := {
			displayName := *,
			addrSpec := tr_SipUrl(user_or_tel, host, portnr)
		}
	},
	params := *
}

/* build a receive template from a value: substitute '*' for omit */
function tr_SipAddr_from_val(SipAddr tin) return template SipAddr {
	var template SipAddr ret := tin;
	if (tin.addr.nameAddr.displayName == omit) {
		ret.addr.nameAddr.displayName := *;
	}
	if (tin.addr.nameAddr.addrSpec.userInfo.password == omit) {
		ret.addr.nameAddr.addrSpec.userInfo.password := *;
	}
	if (tin.params == omit) {
		ret.params := *;
	}
	return ret;
}


function tr_HostPort(template HostPort hp) return template HostPort {
	var template HostPort hpout := hp;
	/* if the port number is 5060, it may be omitted */
	if (isvalue(hp.portField) and valueof(hp.portField) == 5060) {
		hpout.portField := 5060 ifpresent;
	}
	return hpout;
}

template (value) RequestLine ts_SIP_ReqLine(Method method, template (value) SipUrl uri,
					    charstring ver := c_SIP_VERSION) := {
	method := method,
	requestUri := uri,
	sipVersion := ver
}
template RequestLine tr_SIP_ReqLine(template Method method,
				    template SipUrl uri,
				    template charstring ver := c_SIP_VERSION) := {
	method := method,
	requestUri := uri,
	sipVersion := ver
}

template (value) StatusLine ts_SIP_StatusLine(integer status_code, charstring reason) := {
	sipVersion := "SIP/2.0",
	statusCode := status_code,
	reasonPhrase := reason
}
template StatusLine tr_SIP_StatusLine(template integer status_code, template charstring reason) := {
	sipVersion := "SIP/2.0",
	statusCode := status_code,
	reasonPhrase := reason
}


template (value) PDU_SIP_Request ts_SIP_req(template (value) RequestLine rl) := {
	requestLine := rl,
	msgHeader := c_SIP_msgHeader_empty,
	messageBody := omit,
	payload := omit
}

const Method_List c_SIP_defaultMethods := {
	"INVITE", "ACK", "BYE", "CANCEL", "OPTIONS", "PRACK", "MESSAGE", "SUBSCRIBE",
	"NOTIFY", "REFER", "UPDATE" };

private function f_ContentTypeOrOmit(template (omit) ContentType ct, template (omit) charstring body)
return template (omit) ContentType {
	/* if user explicitly stated no content type */
	if (istemplatekind(ct, "omit")) {
		return omit;
	}
	/* if there's no body, then there's no content-type either */
	if (istemplatekind(body, "omit")) {
		return omit;
	}
	return ct;
}

template (value) ContentType ts_CT_SDP := {
	fieldName := CONTENT_TYPE_E,
	mediaType := "application/sdp"
};

template (value) Via ts_Via_from(SipAddr from_addr) := {
	fieldName := VIA_E,
	viaBody := {
		{
			sentProtocol := { "SIP", "2.0", "UDP" },
			sentBy := from_addr.addr.nameAddr.addrSpec.hostPort,
			viaParams := omit
		}
	}
}

template (value) MessageHeader ts_SIP_msgHeader_empty :=c_SIP_msgHeader_empty;
template (value) MessageHeader ts_SIP_msgh_std( CallidString call_id,
						SipAddr from_addr,
						SipAddr to_addr,
						template (omit) SipAddr contact_addr,
						charstring method,
						integer seq_nr,
						template (value) Via via,
						template (omit) ContentType content_type := omit,
						Method_List allow_methods := c_SIP_defaultMethods
					) modifies ts_SIP_msgHeader_empty := {
	allow := {
		fieldName := ALLOW_E,
		methods := allow_methods
	},
	callId := {
		fieldName := CALL_ID_E,
		callid := call_id
	},
	contact := ts_Contact(contact_addr),
	contentType := content_type,
	cSeq := {
		fieldName := CSEQ_E,
		seqNumber := seq_nr,
		method := method
	},
	fromField := {
		fieldName := FROM_E,
		addressField := from_addr.addr,
		fromParams := from_addr.params
	},
	toField := {
		fieldName := TO_E,
		addressField := to_addr.addr,
		toParams := to_addr.params
	},
	userAgent := {
		fieldName := USER_AGENT_E,
		userAgentBody := {
			"osmo-ttcn3-hacks/0.23"
		}
	},
	via := via
}

private function tr_Contact(template SipAddr contact_addr) return template Contact
{
	if (istemplatekind(contact_addr, "omit")) {
		return omit;
	} else if (istemplatekind(contact_addr, "*")) {
		return *;
	} else if (istemplatekind(contact_addr, "?")) {
		return ?;
	}
	var template Contact ret := {
		fieldName := CONTACT_E,
		contactBody := {
			contactAddresses := {
				{
					addressField := contact_addr.addr,
					contactParams := contact_addr.params
				}
			}
		}
	};
	return ret;
}

private function ts_Contact(template (omit) SipAddr contact_addr) return template (omit) Contact
{
	if (istemplatekind(contact_addr, "omit")) {
		return omit;
	}
	var template (omit) Contact ret := {
		fieldName := CONTACT_E,
		contactBody := {
			contactAddresses := {
				{
					addressField := contact_addr.addr,
					contactParams := contact_addr.params
				}
			}
		}
	};
	return ret;
}


function tr_AllowMethods(template Method_List allow_methods) return template Allow {
	if (istemplatekind(allow_methods, "omit")) {
		return omit;
	} else if (istemplatekind(allow_methods, "*")) {
		return *;
	} else if (istemplatekind(allow_methods, "?")) {
		return ?;
	}
	var template Allow ret := {
		fieldName := ALLOW_E,
		methods := allow_methods
	}
	return ret
}

template MessageHeader tr_SIP_msgh_std( template CallidString call_id,
					template SipAddr from_addr,
					template SipAddr to_addr,
					template SipAddr contact_addr,
					template charstring method,
					template ContentType content_type := *,
					template integer seq_nr := ?,
					template Method_List allow_methods := *
				) modifies t_SIP_msgHeader_any := {
	allow := tr_AllowMethods(allow_methods),
	callId := {
		fieldName := CALL_ID_E,
		callid := call_id
	},
	contact := tr_Contact(contact_addr),
	contentType := content_type,
	cSeq := {
		fieldName := CSEQ_E,
		seqNumber := seq_nr,
		method := method
	},
	fromField := {
		fieldName := FROM_E,
		addressField := from_addr.addr,
		fromParams := from_addr.params
	},
	toField := {
		fieldName := TO_E,
		addressField := to_addr.addr,
		toParams := to_addr.params
	},
	userAgent := *,
	via := {
		fieldName := VIA_E,
		viaBody := {
			{
				sentProtocol := { "SIP", "2.0", "UDP" },
				sentBy := tr_HostPort(from_addr.addr.nameAddr.addrSpec.hostPort),
				viaParams := *
			}
		}
	}
}


template (value) PDU_SIP_Request ts_SIP_INVITE( CallidString call_id,
						SipAddr from_addr,
						SipAddr to_addr,
						integer seq_nr,
						template (omit) charstring body
						) := {
	requestLine := ts_SIP_ReqLine(INVITE_E, to_addr.addr.nameAddr.addrSpec),
	msgHeader := ts_SIP_msgh_std(call_id, from_addr, to_addr, from_addr, "INVITE", seq_nr,
				     ts_Via_from(from_addr), f_ContentTypeOrOmit(ts_CT_SDP, body)),
	messageBody := body,
	payload := omit
}
template PDU_SIP_Request tr_SIP_INVITE( template CallidString call_id,
					template SipAddr from_addr,
					template SipAddr to_addr,
					template integer seq_nr,
					template charstring body
						) := {
	requestLine := tr_SIP_ReqLine(INVITE_E, to_addr.addr.nameAddr.addrSpec),
	msgHeader := tr_SIP_msgh_std(call_id, from_addr, to_addr, ?, "INVITE", *, seq_nr),
	messageBody := body,
	payload := omit
}

template (value) PDU_SIP_Request ts_SIP_BYE( CallidString call_id,
					     SipAddr from_addr,
					     SipAddr to_addr,
					     integer seq_nr,
					     template (omit) charstring body
						) := {
	requestLine := ts_SIP_ReqLine(BYE_E, to_addr.addr.nameAddr.addrSpec),
	msgHeader := ts_SIP_msgh_std(call_id, from_addr, to_addr, omit, "BYE", seq_nr,
				     ts_Via_from(from_addr), f_ContentTypeOrOmit(ts_CT_SDP, body)),
	messageBody := body,
	payload := omit
}

template PDU_SIP_Request tr_SIP_BYE( template CallidString call_id,
					template SipAddr from_addr,
					template SipAddr to_addr,
					template integer seq_nr,
					template charstring body
						) := {
	requestLine := tr_SIP_ReqLine(BYE_E, to_addr.addr.nameAddr.addrSpec),
	msgHeader := tr_SIP_msgh_std(call_id, from_addr, to_addr, omit, "BYE", *, seq_nr),
	messageBody := body,
	payload := omit
}


template (value) PDU_SIP_Request ts_SIP_ACK( CallidString call_id,
						SipAddr from_addr,
						SipAddr to_addr,
						integer seq_nr,
						template (omit) charstring body
						) := {
	requestLine := ts_SIP_ReqLine(ACK_E, to_addr.addr.nameAddr.addrSpec),
	msgHeader := ts_SIP_msgh_std(call_id, from_addr, to_addr, from_addr, "ACK", seq_nr,
				     ts_Via_from(from_addr), f_ContentTypeOrOmit(ts_CT_SDP, body)),
	messageBody := body,
	payload := omit
}
template PDU_SIP_Request tr_SIP_ACK( template CallidString call_id,
				     template SipAddr from_addr,
				     template SipAddr to_addr,
				     template integer seq_nr,
				     template charstring body
						) := {
	requestLine := tr_SIP_ReqLine(ACK_E, to_addr.addr.nameAddr.addrSpec),
	msgHeader := tr_SIP_msgh_std(call_id, from_addr, to_addr, *, "ACK", *, seq_nr),
	messageBody := body,
	payload := omit
}



template (value) PDU_SIP_Response ts_SIP_Response( CallidString call_id,
						   SipAddr from_addr,
						   SipAddr to_addr,
						   charstring method,
						   integer status_code,
						   integer seq_nr,
						   charstring reason,
						   Via via,
						   template (omit) charstring body := omit
							) := {
	statusLine := ts_SIP_StatusLine(status_code, reason),
	msgHeader := ts_SIP_msgh_std(call_id, from_addr, to_addr, omit, method, seq_nr,
				     via, f_ContentTypeOrOmit(ts_CT_SDP, body)),
	messageBody := body,
	payload := omit
}

template PDU_SIP_Response tr_SIP_Response( template CallidString call_id,
					   template SipAddr from_addr,
					   template SipAddr to_addr,
					   template SipAddr contact_addr,
					   template charstring method,
					   template integer status_code,
					   template integer seq_nr := ?,
					   template charstring reason := ?,
					   template charstring body := ?
						) := {
	statusLine := tr_SIP_StatusLine(status_code, reason),
	msgHeader := tr_SIP_msgh_std(call_id, from_addr, to_addr, contact_addr, method, *, seq_nr),
	messageBody := body,
	payload := omit
}



}