summaryrefslogtreecommitdiffstats
path: root/library/MGCP_Templates.ttcn
blob: 9d6dea66fcd0c1ba3d11f7a505b6e4a6e41c914b (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
module MGCP_Templates {

/* MGCP Templates, building on top of MGCP_Types (Osmocom) and SDP_Types from Ericsson.
 *
 * (C) 2017 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 MGCP_Types all;
	import from SDP_Types all;

	function f_mgcp_par_append(inout template MgcpParameterList list, template MgcpParameter par) {
		var integer len := lengthof(list);
		list[len] := par;
	}

	/* 3.2.2.6 Connection Mode (sendonly, recvonly, sendrecv, confrnce, inactive, loopback,
	 * conttest, netwloop, netwtest) */
	template MgcpParameter t_MgcpParConnMode(template MgcpConnectionMode mode) := { "M", mode };

	/* 3.2.2.2 CallId: maximum 32 hex chars */
	template MgcpParameter ts_MgcpParCallId(MgcpCallId cid) := {
		code := "C",
		val := hex2str(cid)
	};

	/* 3.2.2.18 RequestIdentifier: Maximum 32 hex chars */
	template MgcpParameter ts_MgcpParReqId(MgcpRequestId rid) := {
		code := "X",
		val := hex2str(rid)
	};

	/* 3.2.1.3 SpecificEndpointId */
	template MgcpParameter ts_MgcpParSpecEP(MgcpEndpoint ep) := {
		code := "Z",
		val := ep
	};

	/* 3.2.2.10: LocalConnectionOptions (codec, packetization, bandwidth, ToS, eco, gain, silence, ...) */
	template MgcpParameter t_MgcpParLocConnOpt(template charstring lco) := { "L", lco };

	/* 3.2.2.5: ConnectionId: maximum 32 hex chars */
	template MgcpParameter ts_MgcpParConnectionId(MgcpConnectionId cid) := {
		code := "I",
		val := hex2str(cid)
	};

	/* osmo-bsc_mgcp implements L/C/M/X only, osmo-mgw adds 'I' */
	/* SDP: osmo-bsc_mgcp implements Tx of v,o,s,c,t,m,a */

	template MgcpResponse tr_MgcpResp_Err(template MgcpResponseCode code) := {
		line := {
			code := code,
			trans_id := ?,
			string := ?
		},
		params := {},
		sdp := omit
	}

	template MgcpCommandLine t_MgcpCmdLine(template charstring verb, template MgcpTransId trans_id, template charstring ep) := {
		verb := verb,
		trans_id := trans_id,
		ep := ep,
		ver := "1.0"
	};

	template MgcpCommand ts_CRCX(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, template SDP_Message sdp := omit) := {
		line := t_MgcpCmdLine("CRCX", trans_id, ep),
		params := {
			t_MgcpParConnMode(mode),
			ts_MgcpParCallId(call_id),
			//t_MgcpParReqId(omit),
			t_MgcpParLocConnOpt("p:20, a:AMR")
		},
		sdp := sdp
	}

	template MgcpCommand ts_CRCX_no_lco(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, template SDP_Message sdp := omit) := {
		line := t_MgcpCmdLine("CRCX", trans_id, ep),
		params := {
			t_MgcpParConnMode(mode),
			ts_MgcpParCallId(call_id)
		},
		sdp := sdp
	}

	template MgcpCommand tr_CRCX(template MgcpEndpoint ep := ?) := {
		line := t_MgcpCmdLine("CRCX", ?, ep),
		params := *,
		sdp := *
	}

	template MgcpResponse tr_CRCX_ACK := {
		line := {
			code := "200",
			trans_id := ?,
			string := "OK"
		},
		params:= { { "I", ? }, *},
		sdp := ?
	}

	template MgcpResponse ts_CRCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := {
		line := {
			code := "200",
			trans_id := trans_id,
			string := "OK"
		},
		params:= { ts_MgcpParConnectionId(conn_id) },
		sdp := sdp
	}

	template MgcpCommand ts_MDCX(MgcpTransId trans_id, charstring ep, MgcpConnectionMode mode, MgcpCallId call_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := {
		line := t_MgcpCmdLine("MDCX", trans_id, ep),
		params := {
			t_MgcpParConnMode(mode),
			ts_MgcpParCallId(call_id),
			ts_MgcpParConnectionId(conn_id),
			//t_MgcpParReqId(omit),
			t_MgcpParLocConnOpt("p:20, a:AMR")
		},
		sdp := sdp
	}

	template MgcpCommand tr_MDCX := {
		line := t_MgcpCmdLine("MDCX", ?, ?),
		params := *,
		sdp := *
	}

	template MgcpResponse tr_MDCX_ACK := {
		line := {
			code := "200",
			trans_id := ?,
			string := "OK"
		},
		params := *,
		sdp := ?
	}

	template MgcpResponse ts_MDCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := ts_CRCX_ACK(trans_id, conn_id, sdp);

	/* have a function that generates a template, rather than a template in order to handle
	 * optional parameters */
	function ts_DLCX(MgcpTransId trans_id, charstring ep, template MgcpCallId call_id := omit,
			 template MgcpConnectionId conn_id := omit) return template MgcpCommand {
		var template MgcpCommand cmd;
		cmd.line := t_MgcpCmdLine("DLCX", trans_id, ep);
		cmd.params := {};
		cmd.sdp := omit;
		if (isvalue(call_id)) {
			f_mgcp_par_append(cmd.params, ts_MgcpParCallId(valueof(call_id)));
			if (isvalue(conn_id)) {
				f_mgcp_par_append(cmd.params, ts_MgcpParConnectionId(valueof(conn_id)));
			}
		}
		return cmd;
	}

	template MgcpCommand tr_DLCX(template MgcpEndpoint ep := ?) := {
		line := t_MgcpCmdLine("DLCX", ?, ep),
		params := *,
		sdp := *
	}

	template MgcpResponse tr_DLCX_ACK := {
		line := {
			code := ("200", "250"),
			trans_id := ?,
			string := "OK"
		},
		params:= *,
		sdp := *
	}

	template MgcpResponse ts_DLCX_ACK2(MgcpTransId trans_id) := {
		line := {
			code := "250",
			trans_id := trans_id,
			string := "OK"
		},
		params:= { /* list of ConnectionIDs */ },
		sdp := omit
	}



	template MgcpResponse ts_DLCX_ACK(MgcpTransId trans_id, MgcpConnectionId conn_id, template SDP_Message sdp := omit) := ts_CRCX_ACK(trans_id, conn_id, sdp);

	template MgcpCommand tr_RSIP := {
		line := t_MgcpCmdLine("RSIP", ?, ?),
		params := *,
		sdp := *
	}

	/* SDP Templates */
	template SDP_Origin ts_SDP_origin(charstring addr, charstring session_id,
					  charstring session_version := "1",
					  charstring addr_type := "IP4",
					  charstring user_name := "-") := {
		user_name := user_name,
		session_id := session_id,
		session_version := session_version,
		net_type := "IN",
		addr_type := addr_type,
		addr := addr
	}

	template SDP_connection ts_SDP_connection_IP(charstring addr, charstring addr_type := "IP4",
						     template integer ttl := omit,
						     template integer num_of_addr := omit) :={
		net_type := "IN",
		addr_type := addr_type,
		conn_addr := {
			addr := addr,
			ttl := ttl,
			num_of_addr := num_of_addr
		}
	}

	template SDP_time ts_SDP_time(charstring beg, charstring end) := {
		time_field := {
			start_time := beg,
			stop_time := end
		},
		time_repeat := omit
	}

	template SDP_media_desc ts_SDP_media_desc(integer port_number, SDP_fmt_list fmts,
						  SDP_attribute_list attributes) := {
		media_field := {
			media := "audio",
			ports := {
				port_number := port_number,
				num_of_ports := omit
			},
			transport := "RTP/AVP",
			fmts := fmts
		},
		information := omit,
		connections := omit,
		bandwidth := omit,
		key := omit,
		attributes := attributes
	}

	/* master template for generating SDP based in template arguments */
	template SDP_Message ts_SDP(charstring local_addr, charstring remote_addr,
				    charstring session_id, charstring session_version,
				    integer rtp_port, SDP_fmt_list fmts,
				    SDP_attribute_list attributes) := {
		protocol_version := 0,
		origin := ts_SDP_origin(local_addr, session_id, session_version),
		session_name := "-",
		information := omit,
		uri := omit,
		emails := omit,
		phone_numbers := omit,
		connection := ts_SDP_connection_IP(remote_addr),
		bandwidth := omit,
		times := { ts_SDP_time("0","0") },
		timezone_adjustments := omit,
		key := omit,
		attributes := omit,
		media_list := { ts_SDP_media_desc(rtp_port, fmts, attributes) }
	}

	template SDP_attribute ts_SDP_rtpmap(integer fmt, charstring val) := {
		rtpmap := {
			attr_value := int2str(fmt) & " " & val
		}
	}
	template SDP_attribute ts_SDP_ptime(integer p) := {
		ptime := {
			attr_value := int2str(p)
		}
	}

	function f_mgcp_extract_par(MgcpMessage msg, MgcpInfoCode code) return charstring {
		var MgcpParameterList pars;
		if (ischosen(msg.command)) {
			pars := msg.command.params;
		} else {
			pars := msg.response.params;
		}
		for (var integer i := 0; i < lengthof(pars); i := i + 1) {
			var MgcpParameter par := pars[i];
			if (par.code == code) {
				return par.val;
			}
		}
		setverdict(fail);
		return "";
	}

	function f_MgcpResp_extract_par(MgcpResponse resp, MgcpInfoCode code) return charstring {
		var MgcpMessage msg := {
			response := resp
		}
		return f_mgcp_extract_par(msg, code);
	}

	function f_MgcpCmd_extract_par(MgcpCommand cmd, MgcpInfoCode code) return charstring {
		var MgcpMessage msg := {
			command := cmd
		}
		return f_mgcp_extract_par(msg, code);
	}

	function f_MgcpResp_extract_conn_id(MgcpResponse resp) return MgcpConnectionId {
		return str2hex(f_MgcpResp_extract_par(resp, "I"));
	}

	function f_MgcpCmd_extract_call_id(MgcpCommand cmd) return MgcpCallId {
		return str2hex(f_MgcpCmd_extract_par(cmd, "C"));
	}

	function f_MgcpCmd_extract_conn_id(MgcpCommand cmd) return MgcpConnectionId {
		return str2hex(f_MgcpCmd_extract_par(cmd, "I"));
	}


	function f_mgcp_alloc_tid() return MgcpTransId {
		return int2str(float2int(rnd()*2147483647.0));
	}

	function f_mgcp_alloc_call_id() return MgcpCallId {
		return int2hex(float2int(rnd()*2147483647.0), 8);
	}

	function f_mgcp_alloc_conn_id() return MgcpConnectionId {
		return int2hex(float2int(rnd()*2147483647.0), 8);
	}

	/* those verbs that related to a connection (and hence have ConnectionId) */
	template MgcpVerb tr_MgcpVerb_ConnectionOriented := ("CRCX", "MDCX", "DLCX", "AUCX");
	/* entire command template matching only connection oriented verbs */
	template MgcpCommand tr_MgcpCommand_CO := {
		line := {
			verb := tr_MgcpVerb_ConnectionOriented,
			trans_id := ?,
			ep := ?,
			ver := ?
		},
		params := *,
		sdp := *
	}

	function f_mgcp_find_param(MgcpMessage msg, MgcpInfoCode code, out charstring ret)
	return boolean {
		var MgcpParameterList pars;
		if (ischosen(msg.command)) {
			pars := msg.command.params;
		} else {
			pars := msg.response.params;
		}
		for (var integer i := 0; i < sizeof(pars); i := i+1) {
			if (pars[i].code == code) {
				ret := pars[i].val;
				return true;
			}
		}
		return false;
	}

	/* template to determine if a MGCP endpoint is a wildcard endpoint */
	template charstring t_MGCP_EP_wildcard := (pattern "\*@*", pattern "rtpbridge/\*@*");


}