bbc846fe9275c3fa173bc12c6b414aac3a651122
[asterisk/asterisk.git] / channels / sip / dialplan_functions.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
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.
11  *
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.
15  */
16
17 /*!
18  * \file
19  * \brief sip channel dialplan functions and unit tests
20  */
21
22 #include "asterisk.h"
23
24 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
25
26 #include <math.h>
27
28 #include "asterisk/channel.h"
29 #include "asterisk/rtp_engine.h"
30 #include "asterisk/pbx.h"
31 #include "asterisk/acl.h"
32
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"
38
39
40 int sip_acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
41 {
42         struct sip_pvt *p = chan->tech_pvt;
43         char *parse = ast_strdupa(preparse);
44         int res = 0;
45         AST_DECLARE_APP_ARGS(args,
46                 AST_APP_ARG(param);
47                 AST_APP_ARG(type);
48                 AST_APP_ARG(field);
49         );
50         AST_STANDARD_APP_ARGS(args, parse);
51
52         /* Sanity check */
53         if (!IS_SIP_TECH(chan->tech)) {
54                 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
55                 return 0;
56         }
57
58         memset(buf, 0, buflen);
59
60         if (p == NULL) {
61                 return -1;
62         }
63
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;
81
82                 if (ast_strlen_zero(args.type))
83                         args.type = "audio";
84
85                 if (!strcasecmp(args.type, "audio"))
86                         stream = p->rtp;
87                 else if (!strcasecmp(args.type, "video"))
88                         stream = p->vrtp;
89                 else if (!strcasecmp(args.type, "text"))
90                         stream = p->trtp;
91                 else
92                         return -1;
93
94                 /* Return 0 to suppress a console warning message */
95                 if (!stream) {
96                         return 0;
97                 }
98
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;
104
105                 if (ast_strlen_zero(args.type))
106                         args.type = "audio";
107
108                 if (!strcasecmp(args.type, "audio"))
109                         stream = p->rtp;
110                 else if (!strcasecmp(args.type, "video"))
111                         stream = p->vrtp;
112                 else if (!strcasecmp(args.type, "text"))
113                         stream = p->trtp;
114                 else
115                         return -1;
116
117                 /* Return 0 to suppress a console warning message */
118                 if (!stream) {
119                         return 0;
120                 }
121
122                 ast_rtp_instance_get_local_address(stream, &sin);
123
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);
128                 }
129
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;
133
134                 if (ast_strlen_zero(args.type)) {
135                         args.type = "audio";
136                 }
137
138                 if (!strcasecmp(args.type, "audio")) {
139                         rtp = p->rtp;
140                 } else if (!strcasecmp(args.type, "video")) {
141                         rtp = p->vrtp;
142                 } else if (!strcasecmp(args.type, "text")) {
143                         rtp = p->trtp;
144                 } else {
145                         return -1;
146                 }
147
148                 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
149                         char quality_buf[AST_MAX_USER_FIELD], *quality;
150
151                         if (!(quality = ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
152                                 return -1;
153                         }
154
155                         ast_copy_string(buf, quality_buf, buflen);
156                         return res;
157                 } else {
158                         struct ast_rtp_instance_stats stats;
159                         int i;
160                         struct {
161                                 const char *name;
162                                 enum { INT, DBL } type;
163                                 union {
164                                         unsigned int *i4;
165                                         double *d8;
166                                 };
167                         } lookup[] = {
168                                 { "txcount",               INT, { .i4 = &stats.txcount, }, },
169                                 { "rxcount",               INT, { .i4 = &stats.rxcount, }, },
170                                 { "txjitter",              INT, { .i4 = &stats.txjitter, }, },
171                                 { "rxjitter",              INT, { .i4 = &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",                   INT, { .i4 = &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, }, },
197                                 { NULL, },
198                         };
199
200                         if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
201                                 return -1;
202                         }
203
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);
208                                         } else {
209                                                 snprintf(buf, buflen, "%f", *lookup[i].d8);
210                                         }
211                                         return 0;
212                                 }
213                         }
214                         ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
215                         return -1;
216                 }
217         } else {
218                 res = -1;
219         }
220         return res;
221 }
222
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)
225 {
226         /* Needed to pass sanity checks */
227         ast_rtp_instance_set_data(instance, data);
228         return 0;
229 }
230
231 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
232 {
233         /* Needed to pass sanity checks */
234         return 0;
235 }
236
237 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
238 {
239         /* Needed to pass sanity checks */
240         return &ast_null_frame;
241 }
242
243 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
244 {
245         /* Needed to pass sanity checks */
246         return 0;
247 }
248
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)
250 {
251         struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
252         memcpy(stats, s, sizeof(*stats));
253         return 0;
254 }
255
256 AST_TEST_DEFINE(test_sip_rtpqos_1)
257 {
258         int i, res = AST_TEST_PASS;
259         struct ast_rtp_engine test_engine = {
260                 .name = "test",
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,
266         };
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;
272         struct {
273                 const char *name;
274                 enum { INT, DBL } type;
275                 union {
276                         unsigned int *i4;
277                         double *d8;
278                 };
279         } lookup[] = {
280                 { "txcount",               INT, { .i4 = &mine.txcount, }, },
281                 { "rxcount",               INT, { .i4 = &mine.rxcount, }, },
282                 { "txjitter",              INT, { .i4 = &mine.txjitter, }, },
283                 { "rxjitter",              INT, { .i4 = &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",                   INT, { .i4 = &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, }, },
309                 { NULL, },
310         };
311
312         switch (cmd) {
313         case TEST_INIT:
314                 info->name = "test_sip_rtpqos";
315                 info->category = "channels/chan_sip/";
316                 info->summary = "Test retrieval of SIP RTP QOS stats";
317                 info->description =
318                         "Verify values in the RTP instance structure can be accessed through the dialplan.";
319                 return AST_TEST_NOT_RUN;
320         case TEST_EXECUTE:
321                 break;
322         }
323
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;
328                 goto done;
329         }
330         if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
331                 res = AST_TEST_NOT_RUN;
332                 goto done;
333         }
334         ast_rtp_instance_set_remote_address(p->rtp, &sin);
335         if (!(chan = ast_dummy_channel_alloc())) {
336                 res = AST_TEST_NOT_RUN;
337                 goto done;
338         }
339         chan->tech = &sip_tech;
340         chan->tech_pvt = p;
341         p->owner = chan;
342
343         varstr = ast_str_create(16);
344         buffer = ast_str_create(16);
345         if (!varstr || !buffer) {
346                 res = AST_TEST_NOT_RUN;
347                 goto done;
348         }
349
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) {
354                         int j;
355                         char cmpstr[256];
356                         for (j = 1; j < 25; j++) {
357                                 *lookup[i].i4 = 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))) {
361                                         res = AST_TEST_FAIL;
362                                         ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
363                                         break;
364                                 }
365                         }
366                 } else {
367                         double j, cmpdbl = 0.0;
368                         for (j = 1.0; j < 10.0; j += 0.3) {
369                                 *lookup[i].d8 = j;
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)) {
372                                         res = AST_TEST_FAIL;
373                                         ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
374                                         break;
375                                 }
376                         }
377                 }
378         }
379
380 done:
381         ast_free(varstr);
382         ast_free(buffer);
383
384         /* This unref will take care of destroying the channel, RTP instance, and SIP pvt */
385         if (p) {
386                 dialog_unref(p, "Destroy test object");
387         }
388         ast_rtp_engine_unregister(&test_engine);
389         return res;
390 }
391 #endif
392
393 /*! \brief SIP test registration */
394 void sip_dialplan_function_register_tests(void)
395 {
396         AST_TEST_REGISTER(test_sip_rtpqos_1);
397 }
398
399 /*! \brief SIP test registration */
400 void sip_dialplan_function_unregister_tests(void)
401 {
402         AST_TEST_UNREGISTER(test_sip_rtpqos_1);
403 }
404