2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2010, Digium, Inc.
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief sip channel dialplan functions and unit tests
23 <support_level>core</support_level>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/channel.h"
33 #include "asterisk/rtp_engine.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/acl.h"
37 #include "include/sip.h"
38 #include "include/globals.h"
39 #include "include/dialog.h"
40 #include "include/dialplan_functions.h"
41 #include "include/sip_utils.h"
44 int sip_acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
46 struct sip_pvt *p = ast_channel_tech_pvt(chan);
47 char *parse = ast_strdupa(preparse);
49 AST_DECLARE_APP_ARGS(args,
55 /* Check for zero arguments */
56 if (ast_strlen_zero(parse)) {
57 ast_log(LOG_ERROR, "Cannot call %s without arguments\n", funcname);
61 AST_STANDARD_APP_ARGS(args, parse);
64 if (!IS_SIP_TECH(ast_channel_tech(chan))) {
65 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
69 memset(buf, 0, buflen);
75 if (!strcasecmp(args.param, "peerip")) {
76 ast_copy_string(buf, ast_sockaddr_isnull(&p->sa) ? "" : ast_sockaddr_stringify_addr(&p->sa), buflen);
77 } else if (!strcasecmp(args.param, "recvip")) {
78 ast_copy_string(buf, ast_sockaddr_isnull(&p->recv) ? "" : ast_sockaddr_stringify_addr(&p->recv), buflen);
79 } else if (!strcasecmp(args.param, "from")) {
80 ast_copy_string(buf, p->from, buflen);
81 } else if (!strcasecmp(args.param, "uri")) {
82 ast_copy_string(buf, p->uri, buflen);
83 } else if (!strcasecmp(args.param, "useragent")) {
84 ast_copy_string(buf, p->useragent, buflen);
85 } else if (!strcasecmp(args.param, "peername")) {
86 ast_copy_string(buf, p->peername, buflen);
87 } else if (!strcasecmp(args.param, "t38passthrough")) {
88 ast_copy_string(buf, (p->t38.state == T38_DISABLED) ? "0" : "1", buflen);
89 } else if (!strcasecmp(args.param, "rtpdest")) {
90 struct ast_sockaddr addr;
91 struct ast_rtp_instance *stream;
93 if (ast_strlen_zero(args.type))
96 if (!strcasecmp(args.type, "audio"))
98 else if (!strcasecmp(args.type, "video"))
100 else if (!strcasecmp(args.type, "text"))
105 /* Return 0 to suppress a console warning message */
110 ast_rtp_instance_get_remote_address(stream, &addr);
111 snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&addr));
112 } else if (!strcasecmp(args.param, "rtpsource")) {
113 struct ast_sockaddr sa;
114 struct ast_rtp_instance *stream;
116 if (ast_strlen_zero(args.type))
119 if (!strcasecmp(args.type, "audio"))
121 else if (!strcasecmp(args.type, "video"))
123 else if (!strcasecmp(args.type, "text"))
128 /* Return 0 to suppress a console warning message */
133 ast_rtp_instance_get_local_address(stream, &sa);
135 if (ast_sockaddr_isnull(&sa)) {
136 struct ast_sockaddr dest_sa;
137 ast_rtp_instance_get_remote_address(stream, &dest_sa);
138 ast_ouraddrfor(&dest_sa, &sa);
141 snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&sa));
142 } else if (!strcasecmp(args.param, "rtpqos")) {
143 struct ast_rtp_instance *rtp = NULL;
145 if (ast_strlen_zero(args.type)) {
149 if (!strcasecmp(args.type, "audio")) {
151 } else if (!strcasecmp(args.type, "video")) {
153 } else if (!strcasecmp(args.type, "text")) {
159 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
160 char quality_buf[AST_MAX_USER_FIELD];
162 if (!ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf))) {
166 ast_copy_string(buf, quality_buf, buflen);
169 struct ast_rtp_instance_stats stats;
173 enum { INT, DBL } type;
179 { "txcount", INT, { .i4 = &stats.txcount, }, },
180 { "rxcount", INT, { .i4 = &stats.rxcount, }, },
181 { "txjitter", DBL, { .d8 = &stats.txjitter, }, },
182 { "rxjitter", DBL, { .d8 = &stats.rxjitter, }, },
183 { "remote_maxjitter", DBL, { .d8 = &stats.remote_maxjitter, }, },
184 { "remote_minjitter", DBL, { .d8 = &stats.remote_minjitter, }, },
185 { "remote_normdevjitter", DBL, { .d8 = &stats.remote_normdevjitter, }, },
186 { "remote_stdevjitter", DBL, { .d8 = &stats.remote_stdevjitter, }, },
187 { "local_maxjitter", DBL, { .d8 = &stats.local_maxjitter, }, },
188 { "local_minjitter", DBL, { .d8 = &stats.local_minjitter, }, },
189 { "local_normdevjitter", DBL, { .d8 = &stats.local_normdevjitter, }, },
190 { "local_stdevjitter", DBL, { .d8 = &stats.local_stdevjitter, }, },
191 { "txploss", INT, { .i4 = &stats.txploss, }, },
192 { "rxploss", INT, { .i4 = &stats.rxploss, }, },
193 { "remote_maxrxploss", DBL, { .d8 = &stats.remote_maxrxploss, }, },
194 { "remote_minrxploss", DBL, { .d8 = &stats.remote_minrxploss, }, },
195 { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
196 { "remote_stdevrxploss", DBL, { .d8 = &stats.remote_stdevrxploss, }, },
197 { "local_maxrxploss", DBL, { .d8 = &stats.local_maxrxploss, }, },
198 { "local_minrxploss", DBL, { .d8 = &stats.local_minrxploss, }, },
199 { "local_normdevrxploss", DBL, { .d8 = &stats.local_normdevrxploss, }, },
200 { "local_stdevrxploss", DBL, { .d8 = &stats.local_stdevrxploss, }, },
201 { "rtt", DBL, { .d8 = &stats.rtt, }, },
202 { "maxrtt", DBL, { .d8 = &stats.maxrtt, }, },
203 { "minrtt", DBL, { .d8 = &stats.minrtt, }, },
204 { "normdevrtt", DBL, { .d8 = &stats.normdevrtt, }, },
205 { "stdevrtt", DBL, { .d8 = &stats.stdevrtt, }, },
206 { "local_ssrc", INT, { .i4 = &stats.local_ssrc, }, },
207 { "remote_ssrc", INT, { .i4 = &stats.remote_ssrc, }, },
211 if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
215 for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
216 if (!strcasecmp(args.field, lookup[i].name)) {
217 if (lookup[i].type == INT) {
218 snprintf(buf, buflen, "%u", *lookup[i].i4);
220 snprintf(buf, buflen, "%f", *lookup[i].d8);
225 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
228 } else if (!strcasecmp(args.param, "secure_signaling")) {
229 snprintf(buf, buflen, "%s", p->socket.type == AST_TRANSPORT_TLS ? "1" : "");
230 } else if (!strcasecmp(args.param, "secure_media")) {
231 snprintf(buf, buflen, "%s", p->srtp ? "1" : "");
238 #ifdef TEST_FRAMEWORK
239 static int test_sip_rtpqos_1_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
241 /* Needed to pass sanity checks */
242 ast_rtp_instance_set_data(instance, data);
246 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
248 /* Needed to pass sanity checks */
252 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
254 /* Needed to pass sanity checks */
255 return &ast_null_frame;
258 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
260 /* Needed to pass sanity checks */
264 static int test_sip_rtpqos_1_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
266 struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
267 memcpy(stats, s, sizeof(*stats));
271 AST_TEST_DEFINE(test_sip_rtpqos_1)
273 int i, res = AST_TEST_PASS;
274 struct ast_rtp_engine test_engine = {
276 .new = test_sip_rtpqos_1_new,
277 .destroy = test_sip_rtpqos_1_destroy,
278 .read = test_sip_rtpqos_1_read,
279 .write = test_sip_rtpqos_1_write,
280 .get_stat = test_sip_rtpqos_1_get_stat,
282 struct ast_sockaddr sa = { {0, } };
283 struct ast_rtp_instance_stats mine = { 0, };
284 struct sip_pvt *p = NULL;
285 struct ast_channel *chan = NULL;
286 struct ast_str *varstr = NULL, *buffer = NULL;
289 enum { INT, DBL } type;
295 { "txcount", INT, { .i4 = &mine.txcount, }, },
296 { "rxcount", INT, { .i4 = &mine.rxcount, }, },
297 { "txjitter", DBL, { .d8 = &mine.txjitter, }, },
298 { "rxjitter", DBL, { .d8 = &mine.rxjitter, }, },
299 { "remote_maxjitter", DBL, { .d8 = &mine.remote_maxjitter, }, },
300 { "remote_minjitter", DBL, { .d8 = &mine.remote_minjitter, }, },
301 { "remote_normdevjitter", DBL, { .d8 = &mine.remote_normdevjitter, }, },
302 { "remote_stdevjitter", DBL, { .d8 = &mine.remote_stdevjitter, }, },
303 { "local_maxjitter", DBL, { .d8 = &mine.local_maxjitter, }, },
304 { "local_minjitter", DBL, { .d8 = &mine.local_minjitter, }, },
305 { "local_normdevjitter", DBL, { .d8 = &mine.local_normdevjitter, }, },
306 { "local_stdevjitter", DBL, { .d8 = &mine.local_stdevjitter, }, },
307 { "txploss", INT, { .i4 = &mine.txploss, }, },
308 { "rxploss", INT, { .i4 = &mine.rxploss, }, },
309 { "remote_maxrxploss", DBL, { .d8 = &mine.remote_maxrxploss, }, },
310 { "remote_minrxploss", DBL, { .d8 = &mine.remote_minrxploss, }, },
311 { "remote_normdevrxploss", DBL, { .d8 = &mine.remote_normdevrxploss, }, },
312 { "remote_stdevrxploss", DBL, { .d8 = &mine.remote_stdevrxploss, }, },
313 { "local_maxrxploss", DBL, { .d8 = &mine.local_maxrxploss, }, },
314 { "local_minrxploss", DBL, { .d8 = &mine.local_minrxploss, }, },
315 { "local_normdevrxploss", DBL, { .d8 = &mine.local_normdevrxploss, }, },
316 { "local_stdevrxploss", DBL, { .d8 = &mine.local_stdevrxploss, }, },
317 { "rtt", DBL, { .d8 = &mine.rtt, }, },
318 { "maxrtt", DBL, { .d8 = &mine.maxrtt, }, },
319 { "minrtt", DBL, { .d8 = &mine.minrtt, }, },
320 { "normdevrtt", DBL, { .d8 = &mine.normdevrtt, }, },
321 { "stdevrtt", DBL, { .d8 = &mine.stdevrtt, }, },
322 { "local_ssrc", INT, { .i4 = &mine.local_ssrc, }, },
323 { "remote_ssrc", INT, { .i4 = &mine.remote_ssrc, }, },
329 info->name = "test_sip_rtpqos";
330 info->category = "/channels/chan_sip/";
331 info->summary = "Test retrieval of SIP RTP QOS stats";
333 "Verify values in the RTP instance structure can be accessed through the dialplan.";
334 return AST_TEST_NOT_RUN;
339 ast_rtp_engine_register2(&test_engine, NULL);
340 /* Have to associate this with a SIP pvt and an ast_channel */
341 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL, NULL))) {
342 res = AST_TEST_NOT_RUN;
346 if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
347 res = AST_TEST_NOT_RUN;
350 ast_rtp_instance_set_remote_address(p->rtp, &sa);
351 if (!(chan = ast_dummy_channel_alloc())) {
352 res = AST_TEST_NOT_RUN;
355 ast_channel_tech_set(chan, &sip_tech);
356 ast_channel_tech_pvt_set(chan, dialog_ref(p, "Give the owner channel a reference to the dialog"));
359 varstr = ast_str_create(16);
360 buffer = ast_str_create(16);
361 if (!varstr || !buffer) {
362 res = AST_TEST_NOT_RUN;
366 /* Populate "mine" with values, then retrieve them with the CHANNEL dialplan function */
367 for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
368 ast_str_set(&varstr, 0, "${CHANNEL(rtpqos,audio,%s)}", lookup[i].name);
369 if (lookup[i].type == INT) {
372 for (j = 1; j < 25; j++) {
374 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
375 snprintf(cmpstr, sizeof(cmpstr), "%d", j);
376 if (strcmp(cmpstr, ast_str_buffer(buffer))) {
378 ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
383 double j, cmpdbl = 0.0;
384 for (j = 1.0; j < 10.0; j += 0.3) {
386 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
387 if (sscanf(ast_str_buffer(buffer), "%lf", &cmpdbl) != 1 || fabs(j - cmpdbl > .05)) {
389 ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
400 /* This unlink and unref will take care of destroying the channel, RTP instance, and SIP pvt */
402 dialog_unlink_all(p);
403 dialog_unref(p, "Destroy test object");
405 ast_rtp_engine_unregister(&test_engine);
410 /*! \brief SIP test registration */
411 void sip_dialplan_function_register_tests(void)
413 AST_TEST_REGISTER(test_sip_rtpqos_1);
416 /*! \brief SIP test registration */
417 void sip_dialplan_function_unregister_tests(void)
419 AST_TEST_UNREGISTER(test_sip_rtpqos_1);