Named ACLs: Introduces a system for creating and sharing ACLs
[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 /*** MODULEINFO
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <math.h>
31
32 #include "asterisk/channel.h"
33 #include "asterisk/rtp_engine.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/acl.h"
36
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"
42
43
44 int sip_acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
45 {
46         struct sip_pvt *p = ast_channel_tech_pvt(chan);
47         char *parse = ast_strdupa(preparse);
48         int res = 0;
49         AST_DECLARE_APP_ARGS(args,
50                 AST_APP_ARG(param);
51                 AST_APP_ARG(type);
52                 AST_APP_ARG(field);
53         );
54                 
55         /* Check for zero arguments */
56         if (ast_strlen_zero(parse)) {
57                 ast_log(LOG_ERROR, "Cannot call %s without arguments\n", funcname);
58                 return -1;
59         }
60
61         AST_STANDARD_APP_ARGS(args, parse);
62
63         /* Sanity check */
64         if (!IS_SIP_TECH(ast_channel_tech(chan))) {
65                 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
66                 return 0;
67         }
68
69         memset(buf, 0, buflen);
70
71         if (p == NULL) {
72                 return -1;
73         }
74
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;
92
93                 if (ast_strlen_zero(args.type))
94                         args.type = "audio";
95
96                 if (!strcasecmp(args.type, "audio"))
97                         stream = p->rtp;
98                 else if (!strcasecmp(args.type, "video"))
99                         stream = p->vrtp;
100                 else if (!strcasecmp(args.type, "text"))
101                         stream = p->trtp;
102                 else
103                         return -1;
104
105                 /* Return 0 to suppress a console warning message */
106                 if (!stream) {
107                         return 0;
108                 }
109
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;
115
116                 if (ast_strlen_zero(args.type))
117                         args.type = "audio";
118
119                 if (!strcasecmp(args.type, "audio"))
120                         stream = p->rtp;
121                 else if (!strcasecmp(args.type, "video"))
122                         stream = p->vrtp;
123                 else if (!strcasecmp(args.type, "text"))
124                         stream = p->trtp;
125                 else
126                         return -1;
127
128                 /* Return 0 to suppress a console warning message */
129                 if (!stream) {
130                         return 0;
131                 }
132
133                 ast_rtp_instance_get_local_address(stream, &sa);
134
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);
139                 }
140
141                 snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&sa));
142         } else if (!strcasecmp(args.param, "rtpqos")) {
143                 struct ast_rtp_instance *rtp = NULL;
144
145                 if (ast_strlen_zero(args.type)) {
146                         args.type = "audio";
147                 }
148
149                 if (!strcasecmp(args.type, "audio")) {
150                         rtp = p->rtp;
151                 } else if (!strcasecmp(args.type, "video")) {
152                         rtp = p->vrtp;
153                 } else if (!strcasecmp(args.type, "text")) {
154                         rtp = p->trtp;
155                 } else {
156                         return -1;
157                 }
158
159                 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
160                         char quality_buf[AST_MAX_USER_FIELD];
161
162                         if (!ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf))) {
163                                 return -1;
164                         }
165
166                         ast_copy_string(buf, quality_buf, buflen);
167                         return res;
168                 } else {
169                         struct ast_rtp_instance_stats stats;
170                         int i;
171                         struct {
172                                 const char *name;
173                                 enum { INT, DBL } type;
174                                 union {
175                                         unsigned int *i4;
176                                         double *d8;
177                                 };
178                         } lookup[] = {
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, }, },
208                                 { NULL, },
209                         };
210
211                         if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
212                                 return -1;
213                         }
214
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);
219                                         } else {
220                                                 snprintf(buf, buflen, "%f", *lookup[i].d8);
221                                         }
222                                         return 0;
223                                 }
224                         }
225                         ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
226                         return -1;
227                 }
228         } else if (!strcasecmp(args.param, "secure_signaling")) {
229                 snprintf(buf, buflen, "%s", p->socket.type == SIP_TRANSPORT_TLS ? "1" : "");
230         } else if (!strcasecmp(args.param, "secure_media")) {
231                 snprintf(buf, buflen, "%s", p->srtp ? "1" : "");
232         } else {
233                 res = -1;
234         }
235         return res;
236 }
237
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)
240 {
241         /* Needed to pass sanity checks */
242         ast_rtp_instance_set_data(instance, data);
243         return 0;
244 }
245
246 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
247 {
248         /* Needed to pass sanity checks */
249         return 0;
250 }
251
252 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
253 {
254         /* Needed to pass sanity checks */
255         return &ast_null_frame;
256 }
257
258 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
259 {
260         /* Needed to pass sanity checks */
261         return 0;
262 }
263
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)
265 {
266         struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
267         memcpy(stats, s, sizeof(*stats));
268         return 0;
269 }
270
271 AST_TEST_DEFINE(test_sip_rtpqos_1)
272 {
273         int i, res = AST_TEST_PASS;
274         struct ast_rtp_engine test_engine = {
275                 .name = "test",
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,
281         };
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;
287         struct {
288                 const char *name;
289                 enum { INT, DBL } type;
290                 union {
291                         unsigned int *i4;
292                         double *d8;
293                 };
294         } lookup[] = {
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, }, },
324                 { NULL, },
325         };
326
327         switch (cmd) {
328         case TEST_INIT:
329                 info->name = "test_sip_rtpqos";
330                 info->category = "/channels/chan_sip/";
331                 info->summary = "Test retrieval of SIP RTP QOS stats";
332                 info->description =
333                         "Verify values in the RTP instance structure can be accessed through the dialplan.";
334                 return AST_TEST_NOT_RUN;
335         case TEST_EXECUTE:
336                 break;
337         }
338
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;
343                 goto done;
344         }
345
346         if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
347                 res = AST_TEST_NOT_RUN;
348                 goto done;
349         }
350         ast_rtp_instance_set_remote_address(p->rtp, &sa);
351         if (!(chan = ast_dummy_channel_alloc())) {
352                 res = AST_TEST_NOT_RUN;
353                 goto done;
354         }
355         ast_channel_tech_set(chan, &sip_tech);
356         ast_channel_tech_pvt_set(chan, p);
357         p->owner = chan;
358
359         varstr = ast_str_create(16);
360         buffer = ast_str_create(16);
361         if (!varstr || !buffer) {
362                 res = AST_TEST_NOT_RUN;
363                 goto done;
364         }
365
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) {
370                         int j;
371                         char cmpstr[256];
372                         for (j = 1; j < 25; j++) {
373                                 *lookup[i].i4 = 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))) {
377                                         res = AST_TEST_FAIL;
378                                         ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
379                                         break;
380                                 }
381                         }
382                 } else {
383                         double j, cmpdbl = 0.0;
384                         for (j = 1.0; j < 10.0; j += 0.3) {
385                                 *lookup[i].d8 = j;
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)) {
388                                         res = AST_TEST_FAIL;
389                                         ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
390                                         break;
391                                 }
392                         }
393                 }
394         }
395
396 done:
397         ast_free(varstr);
398         ast_free(buffer);
399
400         /* This unref will take care of destroying the channel, RTP instance, and SIP pvt */
401         if (p) {
402                 dialog_unref(p, "Destroy test object");
403         }
404         ast_rtp_engine_unregister(&test_engine);
405         return res;
406 }
407 #endif
408
409 /*! \brief SIP test registration */
410 void sip_dialplan_function_register_tests(void)
411 {
412         AST_TEST_REGISTER(test_sip_rtpqos_1);
413 }
414
415 /*! \brief SIP test registration */
416 void sip_dialplan_function_unregister_tests(void)
417 {
418         AST_TEST_UNREGISTER(test_sip_rtpqos_1);
419 }
420