297f9f5dab4eeb40f804adc60a7650fa8c83fcd9
[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                 if (!stream) {
95                         return -1;
96                 }
97
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;
103
104                 if (ast_strlen_zero(args.type))
105                         args.type = "audio";
106
107                 if (!strcasecmp(args.type, "audio"))
108                         stream = p->rtp;
109                 else if (!strcasecmp(args.type, "video"))
110                         stream = p->vrtp;
111                 else if (!strcasecmp(args.type, "text"))
112                         stream = p->trtp;
113                 else
114                         return -1;
115
116                 if (!stream) {
117                         return -1;
118                 }
119
120                 ast_rtp_instance_get_local_address(stream, &sin);
121
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);
126                 }
127
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;
131
132                 if (ast_strlen_zero(args.type)) {
133                         args.type = "audio";
134                 }
135
136                 if (!strcasecmp(args.type, "audio")) {
137                         rtp = p->rtp;
138                 } else if (!strcasecmp(args.type, "video")) {
139                         rtp = p->vrtp;
140                 } else if (!strcasecmp(args.type, "text")) {
141                         rtp = p->trtp;
142                 } else {
143                         return -1;
144                 }
145
146                 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
147                         char quality_buf[AST_MAX_USER_FIELD], *quality;
148
149                         if (!(quality = ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
150                                 return -1;
151                         }
152
153                         ast_copy_string(buf, quality_buf, buflen);
154                         return res;
155                 } else {
156                         struct ast_rtp_instance_stats stats;
157                         int i;
158                         struct {
159                                 const char *name;
160                                 enum { INT, DBL } type;
161                                 union {
162                                         unsigned int *i4;
163                                         double *d8;
164                                 };
165                         } lookup[] = {
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, }, },
195                                 { NULL, },
196                         };
197
198                         if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
199                                 return -1;
200                         }
201
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);
206                                         } else {
207                                                 snprintf(buf, buflen, "%f", *lookup[i].d8);
208                                         }
209                                         return 0;
210                                 }
211                         }
212                         ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
213                         return -1;
214                 }
215         } else {
216                 res = -1;
217         }
218         return res;
219 }
220
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)
223 {
224         /* Needed to pass sanity checks */
225         ast_rtp_instance_set_data(instance, data);
226         return 0;
227 }
228
229 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
230 {
231         /* Needed to pass sanity checks */
232         return 0;
233 }
234
235 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
236 {
237         /* Needed to pass sanity checks */
238         return &ast_null_frame;
239 }
240
241 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
242 {
243         /* Needed to pass sanity checks */
244         return 0;
245 }
246
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)
248 {
249         struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
250         memcpy(stats, s, sizeof(*stats));
251         return 0;
252 }
253
254 AST_TEST_DEFINE(test_sip_rtpqos_1)
255 {
256         int i, res = AST_TEST_PASS;
257         struct ast_rtp_engine test_engine = {
258                 .name = "test",
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,
264         };
265         struct sockaddr_in sin = { .sin_port = 31337, .sin_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;
270         struct {
271                 const char *name;
272                 enum { INT, DBL } type;
273                 union {
274                         unsigned int *i4;
275                         double *d8;
276                 };
277         } lookup[] = {
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, }, },
307                 { NULL, },
308         };
309
310         switch (cmd) {
311         case TEST_INIT:
312                 info->name = "test_sip_rtpqos";
313                 info->category = "channels/chan_sip/";
314                 info->summary = "Test retrieval of SIP RTP QOS stats";
315                 info->description =
316                         "Verify values in the RTP instance structure can be accessed through the dialplan.";
317                 return AST_TEST_NOT_RUN;
318         case TEST_EXECUTE:
319                 break;
320         }
321
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;
326                 goto done;
327         }
328         if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
329                 res = AST_TEST_NOT_RUN;
330                 goto done;
331         }
332         ast_rtp_instance_set_remote_address(p->rtp, &sin);
333         if (!(chan = ast_dummy_channel_alloc())) {
334                 res = AST_TEST_NOT_RUN;
335                 goto done;
336         }
337         chan->tech = &sip_tech;
338         chan->tech_pvt = p;
339         p->owner = chan;
340
341         varstr = ast_str_create(16);
342         buffer = ast_str_create(16);
343         if (!varstr || !buffer) {
344                 res = AST_TEST_NOT_RUN;
345                 goto done;
346         }
347
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) {
352                         int j;
353                         char cmpstr[256];
354                         for (j = 1; j < 25; j++) {
355                                 *lookup[i].i4 = 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))) {
359                                         res = AST_TEST_FAIL;
360                                         ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
361                                         break;
362                                 }
363                         }
364                 } else {
365                         double j, cmpdbl = 0.0;
366                         for (j = 1.0; j < 10.0; j += 0.3) {
367                                 *lookup[i].d8 = j;
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)) {
370                                         res = AST_TEST_FAIL;
371                                         ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
372                                         break;
373                                 }
374                         }
375                 }
376         }
377
378 done:
379         ast_free(varstr);
380         ast_free(buffer);
381
382         /* This unref will take care of destroying the channel, RTP instance, and SIP pvt */
383         if (p) {
384                 dialog_unref(p, "Destroy test object");
385         }
386         ast_rtp_engine_unregister(&test_engine);
387         return res;
388 }
389 #endif
390
391 /*! \brief SIP test registration */
392 void sip_dialplan_function_register_tests(void)
393 {
394         AST_TEST_REGISTER(test_sip_rtpqos_1);
395 }
396
397 /*! \brief SIP test registration */
398 void sip_dialplan_function_unregister_tests(void)
399 {
400         AST_TEST_UNREGISTER(test_sip_rtpqos_1);
401 }
402