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
24 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
28 #include "asterisk/channel.h"
29 #include "asterisk/rtp_engine.h"
30 #include "asterisk/pbx.h"
31 #include "asterisk/acl.h"
33 #include "include/sip.h"
34 #include "include/globals.h"
35 #include "include/dialog.h"
36 #include "include/dialplan_functions.h"
37 #include "include/sip_utils.h"
40 int sip_acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
42 struct sip_pvt *p = chan->tech_pvt;
43 char *parse = ast_strdupa(preparse);
45 AST_DECLARE_APP_ARGS(args,
50 AST_STANDARD_APP_ARGS(args, parse);
53 if (!IS_SIP_TECH(chan->tech)) {
54 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
58 memset(buf, 0, buflen);
64 if (!strcasecmp(args.param, "peerip")) {
65 ast_copy_string(buf, p->sa.sin_addr.s_addr ? ast_inet_ntoa(p->sa.sin_addr) : "", buflen);
66 } else if (!strcasecmp(args.param, "recvip")) {
67 ast_copy_string(buf, p->recv.sin_addr.s_addr ? ast_inet_ntoa(p->recv.sin_addr) : "", buflen);
68 } else if (!strcasecmp(args.param, "from")) {
69 ast_copy_string(buf, p->from, buflen);
70 } else if (!strcasecmp(args.param, "uri")) {
71 ast_copy_string(buf, p->uri, buflen);
72 } else if (!strcasecmp(args.param, "useragent")) {
73 ast_copy_string(buf, p->useragent, buflen);
74 } else if (!strcasecmp(args.param, "peername")) {
75 ast_copy_string(buf, p->peername, buflen);
76 } else if (!strcasecmp(args.param, "t38passthrough")) {
77 ast_copy_string(buf, (p->t38.state == T38_DISABLED) ? "0" : "1", buflen);
78 } else if (!strcasecmp(args.param, "rtpdest")) {
79 struct sockaddr_in sin;
80 struct ast_rtp_instance *stream;
82 if (ast_strlen_zero(args.type))
85 if (!strcasecmp(args.type, "audio"))
87 else if (!strcasecmp(args.type, "video"))
89 else if (!strcasecmp(args.type, "text"))
98 ast_rtp_instance_get_remote_address(stream, &sin);
99 snprintf(buf, buflen, "%s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
100 } else if (!strcasecmp(args.param, "rtpsource")) {
101 struct sockaddr_in sin;
102 struct ast_rtp_instance *stream;
104 if (ast_strlen_zero(args.type))
107 if (!strcasecmp(args.type, "audio"))
109 else if (!strcasecmp(args.type, "video"))
111 else if (!strcasecmp(args.type, "text"))
120 ast_rtp_instance_get_local_address(stream, &sin);
122 if (!sin.sin_addr.s_addr) {
123 struct sockaddr_in dest_sin;
124 ast_rtp_instance_get_remote_address(stream, &dest_sin);
125 ast_ouraddrfor(&dest_sin.sin_addr, &sin.sin_addr);
128 snprintf(buf, buflen, "%s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
129 } else if (!strcasecmp(args.param, "rtpqos")) {
130 struct ast_rtp_instance *rtp = NULL;
132 if (ast_strlen_zero(args.type)) {
136 if (!strcasecmp(args.type, "audio")) {
138 } else if (!strcasecmp(args.type, "video")) {
140 } else if (!strcasecmp(args.type, "text")) {
146 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
147 char quality_buf[AST_MAX_USER_FIELD], *quality;
149 if (!(quality = ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
153 ast_copy_string(buf, quality_buf, buflen);
156 struct ast_rtp_instance_stats stats;
160 enum { INT, DBL } type;
166 { "txcount", INT, { .i4 = &stats.txcount, }, },
167 { "rxcount", INT, { .i4 = &stats.rxcount, }, },
168 { "txjitter", INT, { .i4 = &stats.txjitter, }, },
169 { "rxjitter", INT, { .i4 = &stats.rxjitter, }, },
170 { "remote_maxjitter", DBL, { .d8 = &stats.remote_maxjitter, }, },
171 { "remote_minjitter", DBL, { .d8 = &stats.remote_minjitter, }, },
172 { "remote_normdevjitter", DBL, { .d8 = &stats.remote_normdevjitter, }, },
173 { "remote_stdevjitter", DBL, { .d8 = &stats.remote_stdevjitter, }, },
174 { "local_maxjitter", DBL, { .d8 = &stats.local_maxjitter, }, },
175 { "local_minjitter", DBL, { .d8 = &stats.local_minjitter, }, },
176 { "local_normdevjitter", DBL, { .d8 = &stats.local_normdevjitter, }, },
177 { "local_stdevjitter", DBL, { .d8 = &stats.local_stdevjitter, }, },
178 { "txploss", INT, { .i4 = &stats.txploss, }, },
179 { "rxploss", INT, { .i4 = &stats.rxploss, }, },
180 { "remote_maxrxploss", DBL, { .d8 = &stats.remote_maxrxploss, }, },
181 { "remote_minrxploss", DBL, { .d8 = &stats.remote_minrxploss, }, },
182 { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
183 { "remote_stdevrxploss", DBL, { .d8 = &stats.remote_stdevrxploss, }, },
184 { "local_maxrxploss", DBL, { .d8 = &stats.local_maxrxploss, }, },
185 { "local_minrxploss", DBL, { .d8 = &stats.local_minrxploss, }, },
186 { "local_normdevrxploss", DBL, { .d8 = &stats.local_normdevrxploss, }, },
187 { "local_stdevrxploss", DBL, { .d8 = &stats.local_stdevrxploss, }, },
188 { "rtt", INT, { .i4 = &stats.rtt, }, },
189 { "maxrtt", DBL, { .d8 = &stats.maxrtt, }, },
190 { "minrtt", DBL, { .d8 = &stats.minrtt, }, },
191 { "normdevrtt", DBL, { .d8 = &stats.normdevrtt, }, },
192 { "stdevrtt", DBL, { .d8 = &stats.stdevrtt, }, },
193 { "local_ssrc", INT, { .i4 = &stats.local_ssrc, }, },
194 { "remote_ssrc", INT, { .i4 = &stats.remote_ssrc, }, },
198 if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
202 for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
203 if (!strcasecmp(args.field, lookup[i].name)) {
204 if (lookup[i].type == INT) {
205 snprintf(buf, buflen, "%u", *lookup[i].i4);
207 snprintf(buf, buflen, "%f", *lookup[i].d8);
212 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
221 #ifdef TEST_FRAMEWORK
222 static int test_sip_rtpqos_1_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct sockaddr_in *sin, void *data)
224 /* Needed to pass sanity checks */
225 ast_rtp_instance_set_data(instance, data);
229 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
231 /* Needed to pass sanity checks */
235 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
237 /* Needed to pass sanity checks */
238 return &ast_null_frame;
241 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
243 /* Needed to pass sanity checks */
247 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)
249 struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
250 memcpy(stats, s, sizeof(*stats));
254 AST_TEST_DEFINE(test_sip_rtpqos_1)
256 int i, res = AST_TEST_PASS;
257 struct ast_rtp_engine test_engine = {
259 .new = test_sip_rtpqos_1_new,
260 .destroy = test_sip_rtpqos_1_destroy,
261 .read = test_sip_rtpqos_1_read,
262 .write = test_sip_rtpqos_1_write,
263 .get_stat = test_sip_rtpqos_1_get_stat,
265 struct sockaddr_in sin = { .sin_port = 31337, .sin_addr = { .s_addr = 4 * 16777216 + 3 * 65536 + 2 * 256 + 1 } };
266 struct ast_rtp_instance_stats mine = { 0, };
267 struct sip_pvt *p = NULL;
268 struct ast_channel *chan = NULL;
269 struct ast_str *varstr = NULL, *buffer = NULL;
272 enum { INT, DBL } type;
278 { "txcount", INT, { .i4 = &mine.txcount, }, },
279 { "rxcount", INT, { .i4 = &mine.rxcount, }, },
280 { "txjitter", INT, { .i4 = &mine.txjitter, }, },
281 { "rxjitter", INT, { .i4 = &mine.rxjitter, }, },
282 { "remote_maxjitter", DBL, { .d8 = &mine.remote_maxjitter, }, },
283 { "remote_minjitter", DBL, { .d8 = &mine.remote_minjitter, }, },
284 { "remote_normdevjitter", DBL, { .d8 = &mine.remote_normdevjitter, }, },
285 { "remote_stdevjitter", DBL, { .d8 = &mine.remote_stdevjitter, }, },
286 { "local_maxjitter", DBL, { .d8 = &mine.local_maxjitter, }, },
287 { "local_minjitter", DBL, { .d8 = &mine.local_minjitter, }, },
288 { "local_normdevjitter", DBL, { .d8 = &mine.local_normdevjitter, }, },
289 { "local_stdevjitter", DBL, { .d8 = &mine.local_stdevjitter, }, },
290 { "txploss", INT, { .i4 = &mine.txploss, }, },
291 { "rxploss", INT, { .i4 = &mine.rxploss, }, },
292 { "remote_maxrxploss", DBL, { .d8 = &mine.remote_maxrxploss, }, },
293 { "remote_minrxploss", DBL, { .d8 = &mine.remote_minrxploss, }, },
294 { "remote_normdevrxploss", DBL, { .d8 = &mine.remote_normdevrxploss, }, },
295 { "remote_stdevrxploss", DBL, { .d8 = &mine.remote_stdevrxploss, }, },
296 { "local_maxrxploss", DBL, { .d8 = &mine.local_maxrxploss, }, },
297 { "local_minrxploss", DBL, { .d8 = &mine.local_minrxploss, }, },
298 { "local_normdevrxploss", DBL, { .d8 = &mine.local_normdevrxploss, }, },
299 { "local_stdevrxploss", DBL, { .d8 = &mine.local_stdevrxploss, }, },
300 { "rtt", INT, { .i4 = &mine.rtt, }, },
301 { "maxrtt", DBL, { .d8 = &mine.maxrtt, }, },
302 { "minrtt", DBL, { .d8 = &mine.minrtt, }, },
303 { "normdevrtt", DBL, { .d8 = &mine.normdevrtt, }, },
304 { "stdevrtt", DBL, { .d8 = &mine.stdevrtt, }, },
305 { "local_ssrc", INT, { .i4 = &mine.local_ssrc, }, },
306 { "remote_ssrc", INT, { .i4 = &mine.remote_ssrc, }, },
312 info->name = "test_sip_rtpqos";
313 info->category = "channels/chan_sip/";
314 info->summary = "Test retrieval of SIP RTP QOS stats";
316 "Verify values in the RTP instance structure can be accessed through the dialplan.";
317 return AST_TEST_NOT_RUN;
322 ast_rtp_engine_register2(&test_engine, NULL);
323 /* Have to associate this with a SIP pvt and an ast_channel */
324 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
325 res = AST_TEST_NOT_RUN;
328 if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
329 res = AST_TEST_NOT_RUN;
332 ast_rtp_instance_set_remote_address(p->rtp, &sin);
333 if (!(chan = ast_dummy_channel_alloc())) {
334 res = AST_TEST_NOT_RUN;
337 chan->tech = &sip_tech;
341 varstr = ast_str_create(16);
342 buffer = ast_str_create(16);
343 if (!varstr || !buffer) {
344 res = AST_TEST_NOT_RUN;
348 /* Populate "mine" with values, then retrieve them with the CHANNEL dialplan function */
349 for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
350 ast_str_set(&varstr, 0, "${CHANNEL(rtpqos,audio,%s)}", lookup[i].name);
351 if (lookup[i].type == INT) {
354 for (j = 1; j < 25; j++) {
356 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
357 snprintf(cmpstr, sizeof(cmpstr), "%d", j);
358 if (strcmp(cmpstr, ast_str_buffer(buffer))) {
360 ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
365 double j, cmpdbl = 0.0;
366 for (j = 1.0; j < 10.0; j += 0.3) {
368 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
369 if (sscanf(ast_str_buffer(buffer), "%lf", &cmpdbl) != 1 || fabs(j - cmpdbl > .05)) {
371 ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
382 /* This unref will take care of destroying the channel, RTP instance, and SIP pvt */
384 dialog_unref(p, "Destroy test object");
386 ast_rtp_engine_unregister(&test_engine);
391 /*! \brief SIP test registration */
392 void sip_dialplan_function_register_tests(void)
394 AST_TEST_REGISTER(test_sip_rtpqos_1);
397 /*! \brief SIP test registration */
398 void sip_dialplan_function_unregister_tests(void)
400 AST_TEST_UNREGISTER(test_sip_rtpqos_1);