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"))
94 /* Return 0 to suppress a console warning message */
99 ast_rtp_instance_get_remote_address(stream, &sin);
100 snprintf(buf, buflen, "%s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
101 } else if (!strcasecmp(args.param, "rtpsource")) {
102 struct sockaddr_in sin;
103 struct ast_rtp_instance *stream;
105 if (ast_strlen_zero(args.type))
108 if (!strcasecmp(args.type, "audio"))
110 else if (!strcasecmp(args.type, "video"))
112 else if (!strcasecmp(args.type, "text"))
117 /* Return 0 to suppress a console warning message */
122 ast_rtp_instance_get_local_address(stream, &sin);
124 if (!sin.sin_addr.s_addr) {
125 struct sockaddr_in dest_sin;
126 ast_rtp_instance_get_remote_address(stream, &dest_sin);
127 ast_ouraddrfor(&dest_sin.sin_addr, &sin.sin_addr);
130 snprintf(buf, buflen, "%s:%d", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
131 } else if (!strcasecmp(args.param, "rtpqos")) {
132 struct ast_rtp_instance *rtp = NULL;
134 if (ast_strlen_zero(args.type)) {
138 if (!strcasecmp(args.type, "audio")) {
140 } else if (!strcasecmp(args.type, "video")) {
142 } else if (!strcasecmp(args.type, "text")) {
148 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
149 char quality_buf[AST_MAX_USER_FIELD], *quality;
151 if (!(quality = ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
155 ast_copy_string(buf, quality_buf, buflen);
158 struct ast_rtp_instance_stats stats;
162 enum { INT, DBL } type;
168 { "txcount", INT, { .i4 = &stats.txcount, }, },
169 { "rxcount", INT, { .i4 = &stats.rxcount, }, },
170 { "txjitter", DBL, { .d8 = &stats.txjitter, }, },
171 { "rxjitter", DBL, { .d8 = &stats.rxjitter, }, },
172 { "remote_maxjitter", DBL, { .d8 = &stats.remote_maxjitter, }, },
173 { "remote_minjitter", DBL, { .d8 = &stats.remote_minjitter, }, },
174 { "remote_normdevjitter", DBL, { .d8 = &stats.remote_normdevjitter, }, },
175 { "remote_stdevjitter", DBL, { .d8 = &stats.remote_stdevjitter, }, },
176 { "local_maxjitter", DBL, { .d8 = &stats.local_maxjitter, }, },
177 { "local_minjitter", DBL, { .d8 = &stats.local_minjitter, }, },
178 { "local_normdevjitter", DBL, { .d8 = &stats.local_normdevjitter, }, },
179 { "local_stdevjitter", DBL, { .d8 = &stats.local_stdevjitter, }, },
180 { "txploss", INT, { .i4 = &stats.txploss, }, },
181 { "rxploss", INT, { .i4 = &stats.rxploss, }, },
182 { "remote_maxrxploss", DBL, { .d8 = &stats.remote_maxrxploss, }, },
183 { "remote_minrxploss", DBL, { .d8 = &stats.remote_minrxploss, }, },
184 { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
185 { "remote_stdevrxploss", DBL, { .d8 = &stats.remote_stdevrxploss, }, },
186 { "local_maxrxploss", DBL, { .d8 = &stats.local_maxrxploss, }, },
187 { "local_minrxploss", DBL, { .d8 = &stats.local_minrxploss, }, },
188 { "local_normdevrxploss", DBL, { .d8 = &stats.local_normdevrxploss, }, },
189 { "local_stdevrxploss", DBL, { .d8 = &stats.local_stdevrxploss, }, },
190 { "rtt", DBL, { .d8 = &stats.rtt, }, },
191 { "maxrtt", DBL, { .d8 = &stats.maxrtt, }, },
192 { "minrtt", DBL, { .d8 = &stats.minrtt, }, },
193 { "normdevrtt", DBL, { .d8 = &stats.normdevrtt, }, },
194 { "stdevrtt", DBL, { .d8 = &stats.stdevrtt, }, },
195 { "local_ssrc", INT, { .i4 = &stats.local_ssrc, }, },
196 { "remote_ssrc", INT, { .i4 = &stats.remote_ssrc, }, },
200 if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
204 for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
205 if (!strcasecmp(args.field, lookup[i].name)) {
206 if (lookup[i].type == INT) {
207 snprintf(buf, buflen, "%u", *lookup[i].i4);
209 snprintf(buf, buflen, "%f", *lookup[i].d8);
214 ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
223 #ifdef TEST_FRAMEWORK
224 static int test_sip_rtpqos_1_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct sockaddr_in *sin, void *data)
226 /* Needed to pass sanity checks */
227 ast_rtp_instance_set_data(instance, data);
231 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
233 /* Needed to pass sanity checks */
237 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
239 /* Needed to pass sanity checks */
240 return &ast_null_frame;
243 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
245 /* Needed to pass sanity checks */
249 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)
251 struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
252 memcpy(stats, s, sizeof(*stats));
256 AST_TEST_DEFINE(test_sip_rtpqos_1)
258 int i, res = AST_TEST_PASS;
259 struct ast_rtp_engine test_engine = {
261 .new = test_sip_rtpqos_1_new,
262 .destroy = test_sip_rtpqos_1_destroy,
263 .read = test_sip_rtpqos_1_read,
264 .write = test_sip_rtpqos_1_write,
265 .get_stat = test_sip_rtpqos_1_get_stat,
267 struct sockaddr_in sin = { .sin_port = 31337, .sin_addr = { .s_addr = 4 * 16777216 + 3 * 65536 + 2 * 256 + 1 } };
268 struct ast_rtp_instance_stats mine = { 0, };
269 struct sip_pvt *p = NULL;
270 struct ast_channel *chan = NULL;
271 struct ast_str *varstr = NULL, *buffer = NULL;
274 enum { INT, DBL } type;
280 { "txcount", INT, { .i4 = &mine.txcount, }, },
281 { "rxcount", INT, { .i4 = &mine.rxcount, }, },
282 { "txjitter", DBL, { .d8 = &mine.txjitter, }, },
283 { "rxjitter", DBL, { .d8 = &mine.rxjitter, }, },
284 { "remote_maxjitter", DBL, { .d8 = &mine.remote_maxjitter, }, },
285 { "remote_minjitter", DBL, { .d8 = &mine.remote_minjitter, }, },
286 { "remote_normdevjitter", DBL, { .d8 = &mine.remote_normdevjitter, }, },
287 { "remote_stdevjitter", DBL, { .d8 = &mine.remote_stdevjitter, }, },
288 { "local_maxjitter", DBL, { .d8 = &mine.local_maxjitter, }, },
289 { "local_minjitter", DBL, { .d8 = &mine.local_minjitter, }, },
290 { "local_normdevjitter", DBL, { .d8 = &mine.local_normdevjitter, }, },
291 { "local_stdevjitter", DBL, { .d8 = &mine.local_stdevjitter, }, },
292 { "txploss", INT, { .i4 = &mine.txploss, }, },
293 { "rxploss", INT, { .i4 = &mine.rxploss, }, },
294 { "remote_maxrxploss", DBL, { .d8 = &mine.remote_maxrxploss, }, },
295 { "remote_minrxploss", DBL, { .d8 = &mine.remote_minrxploss, }, },
296 { "remote_normdevrxploss", DBL, { .d8 = &mine.remote_normdevrxploss, }, },
297 { "remote_stdevrxploss", DBL, { .d8 = &mine.remote_stdevrxploss, }, },
298 { "local_maxrxploss", DBL, { .d8 = &mine.local_maxrxploss, }, },
299 { "local_minrxploss", DBL, { .d8 = &mine.local_minrxploss, }, },
300 { "local_normdevrxploss", DBL, { .d8 = &mine.local_normdevrxploss, }, },
301 { "local_stdevrxploss", DBL, { .d8 = &mine.local_stdevrxploss, }, },
302 { "rtt", DBL, { .d8 = &mine.rtt, }, },
303 { "maxrtt", DBL, { .d8 = &mine.maxrtt, }, },
304 { "minrtt", DBL, { .d8 = &mine.minrtt, }, },
305 { "normdevrtt", DBL, { .d8 = &mine.normdevrtt, }, },
306 { "stdevrtt", DBL, { .d8 = &mine.stdevrtt, }, },
307 { "local_ssrc", INT, { .i4 = &mine.local_ssrc, }, },
308 { "remote_ssrc", INT, { .i4 = &mine.remote_ssrc, }, },
314 info->name = "test_sip_rtpqos";
315 info->category = "channels/chan_sip/";
316 info->summary = "Test retrieval of SIP RTP QOS stats";
318 "Verify values in the RTP instance structure can be accessed through the dialplan.";
319 return AST_TEST_NOT_RUN;
324 ast_rtp_engine_register2(&test_engine, NULL);
325 /* Have to associate this with a SIP pvt and an ast_channel */
326 if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
327 res = AST_TEST_NOT_RUN;
330 if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
331 res = AST_TEST_NOT_RUN;
334 ast_rtp_instance_set_remote_address(p->rtp, &sin);
335 if (!(chan = ast_dummy_channel_alloc())) {
336 res = AST_TEST_NOT_RUN;
339 chan->tech = &sip_tech;
343 varstr = ast_str_create(16);
344 buffer = ast_str_create(16);
345 if (!varstr || !buffer) {
346 res = AST_TEST_NOT_RUN;
350 /* Populate "mine" with values, then retrieve them with the CHANNEL dialplan function */
351 for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
352 ast_str_set(&varstr, 0, "${CHANNEL(rtpqos,audio,%s)}", lookup[i].name);
353 if (lookup[i].type == INT) {
356 for (j = 1; j < 25; j++) {
358 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
359 snprintf(cmpstr, sizeof(cmpstr), "%d", j);
360 if (strcmp(cmpstr, ast_str_buffer(buffer))) {
362 ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
367 double j, cmpdbl = 0.0;
368 for (j = 1.0; j < 10.0; j += 0.3) {
370 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
371 if (sscanf(ast_str_buffer(buffer), "%lf", &cmpdbl) != 1 || fabs(j - cmpdbl > .05)) {
373 ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
384 /* This unref will take care of destroying the channel, RTP instance, and SIP pvt */
386 dialog_unref(p, "Destroy test object");
388 ast_rtp_engine_unregister(&test_engine);
393 /*! \brief SIP test registration */
394 void sip_dialplan_function_register_tests(void)
396 AST_TEST_REGISTER(test_sip_rtpqos_1);
399 /*! \brief SIP test registration */
400 void sip_dialplan_function_unregister_tests(void)
402 AST_TEST_UNREGISTER(test_sip_rtpqos_1);