152f7caa4e890c5a3ec10dd3402d764c9821af83
[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 = ast_channel_tech_pvt(chan);
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                 
51         /* Check for zero arguments */
52         if (ast_strlen_zero(parse)) {
53                 ast_log(LOG_ERROR, "Cannot call %s without arguments\n", funcname);
54                 return -1;
55         }
56
57         AST_STANDARD_APP_ARGS(args, parse);
58
59         /* Sanity check */
60         if (!IS_SIP_TECH(ast_channel_tech(chan))) {
61                 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
62                 return 0;
63         }
64
65         memset(buf, 0, buflen);
66
67         if (p == NULL) {
68                 return -1;
69         }
70
71         if (!strcasecmp(args.param, "peerip")) {
72                 ast_copy_string(buf, ast_sockaddr_isnull(&p->sa) ? "" : ast_sockaddr_stringify_addr(&p->sa), buflen);
73         } else if (!strcasecmp(args.param, "recvip")) {
74                 ast_copy_string(buf, ast_sockaddr_isnull(&p->recv) ? "" : ast_sockaddr_stringify_addr(&p->recv), buflen);
75         } else if (!strcasecmp(args.param, "from")) {
76                 ast_copy_string(buf, p->from, buflen);
77         } else if (!strcasecmp(args.param, "uri")) {
78                 ast_copy_string(buf, p->uri, buflen);
79         } else if (!strcasecmp(args.param, "useragent")) {
80                 ast_copy_string(buf, p->useragent, buflen);
81         } else if (!strcasecmp(args.param, "peername")) {
82                 ast_copy_string(buf, p->peername, buflen);
83         } else if (!strcasecmp(args.param, "t38passthrough")) {
84                 ast_copy_string(buf, (p->t38.state == T38_DISABLED) ? "0" : "1", buflen);
85         } else if (!strcasecmp(args.param, "rtpdest")) {
86                 struct ast_sockaddr addr;
87                 struct ast_rtp_instance *stream;
88
89                 if (ast_strlen_zero(args.type))
90                         args.type = "audio";
91
92                 if (!strcasecmp(args.type, "audio"))
93                         stream = p->rtp;
94                 else if (!strcasecmp(args.type, "video"))
95                         stream = p->vrtp;
96                 else if (!strcasecmp(args.type, "text"))
97                         stream = p->trtp;
98                 else
99                         return -1;
100
101                 /* Return 0 to suppress a console warning message */
102                 if (!stream) {
103                         return 0;
104                 }
105
106                 ast_rtp_instance_get_remote_address(stream, &addr);
107                 snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&addr));
108         } else if (!strcasecmp(args.param, "rtpsource")) {
109                 struct ast_sockaddr sa;
110                 struct ast_rtp_instance *stream;
111
112                 if (ast_strlen_zero(args.type))
113                         args.type = "audio";
114
115                 if (!strcasecmp(args.type, "audio"))
116                         stream = p->rtp;
117                 else if (!strcasecmp(args.type, "video"))
118                         stream = p->vrtp;
119                 else if (!strcasecmp(args.type, "text"))
120                         stream = p->trtp;
121                 else
122                         return -1;
123
124                 /* Return 0 to suppress a console warning message */
125                 if (!stream) {
126                         return 0;
127                 }
128
129                 ast_rtp_instance_get_local_address(stream, &sa);
130
131                 if (ast_sockaddr_isnull(&sa)) {
132                         struct ast_sockaddr dest_sa;
133                         ast_rtp_instance_get_remote_address(stream, &dest_sa);
134                         ast_ouraddrfor(&dest_sa, &sa);
135                 }
136
137                 snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&sa));
138         } else if (!strcasecmp(args.param, "rtpqos")) {
139                 struct ast_rtp_instance *rtp = NULL;
140
141                 if (ast_strlen_zero(args.type)) {
142                         args.type = "audio";
143                 }
144
145                 if (!strcasecmp(args.type, "audio")) {
146                         rtp = p->rtp;
147                 } else if (!strcasecmp(args.type, "video")) {
148                         rtp = p->vrtp;
149                 } else if (!strcasecmp(args.type, "text")) {
150                         rtp = p->trtp;
151                 } else {
152                         return -1;
153                 }
154
155                 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
156                         char quality_buf[AST_MAX_USER_FIELD], *quality;
157
158                         if (!(quality = ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
159                                 return -1;
160                         }
161
162                         ast_copy_string(buf, quality_buf, buflen);
163                         return res;
164                 } else {
165                         struct ast_rtp_instance_stats stats;
166                         int i;
167                         struct {
168                                 const char *name;
169                                 enum { INT, DBL } type;
170                                 union {
171                                         unsigned int *i4;
172                                         double *d8;
173                                 };
174                         } lookup[] = {
175                                 { "txcount",               INT, { .i4 = &stats.txcount, }, },
176                                 { "rxcount",               INT, { .i4 = &stats.rxcount, }, },
177                                 { "txjitter",              DBL, { .d8 = &stats.txjitter, }, },
178                                 { "rxjitter",              DBL, { .d8 = &stats.rxjitter, }, },
179                                 { "remote_maxjitter",      DBL, { .d8 = &stats.remote_maxjitter, }, },
180                                 { "remote_minjitter",      DBL, { .d8 = &stats.remote_minjitter, }, },
181                                 { "remote_normdevjitter",  DBL, { .d8 = &stats.remote_normdevjitter, }, },
182                                 { "remote_stdevjitter",    DBL, { .d8 = &stats.remote_stdevjitter, }, },
183                                 { "local_maxjitter",       DBL, { .d8 = &stats.local_maxjitter, }, },
184                                 { "local_minjitter",       DBL, { .d8 = &stats.local_minjitter, }, },
185                                 { "local_normdevjitter",   DBL, { .d8 = &stats.local_normdevjitter, }, },
186                                 { "local_stdevjitter",     DBL, { .d8 = &stats.local_stdevjitter, }, },
187                                 { "txploss",               INT, { .i4 = &stats.txploss, }, },
188                                 { "rxploss",               INT, { .i4 = &stats.rxploss, }, },
189                                 { "remote_maxrxploss",     DBL, { .d8 = &stats.remote_maxrxploss, }, },
190                                 { "remote_minrxploss",     DBL, { .d8 = &stats.remote_minrxploss, }, },
191                                 { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
192                                 { "remote_stdevrxploss",   DBL, { .d8 = &stats.remote_stdevrxploss, }, },
193                                 { "local_maxrxploss",      DBL, { .d8 = &stats.local_maxrxploss, }, },
194                                 { "local_minrxploss",      DBL, { .d8 = &stats.local_minrxploss, }, },
195                                 { "local_normdevrxploss",  DBL, { .d8 = &stats.local_normdevrxploss, }, },
196                                 { "local_stdevrxploss",    DBL, { .d8 = &stats.local_stdevrxploss, }, },
197                                 { "rtt",                   DBL, { .d8 = &stats.rtt, }, },
198                                 { "maxrtt",                DBL, { .d8 = &stats.maxrtt, }, },
199                                 { "minrtt",                DBL, { .d8 = &stats.minrtt, }, },
200                                 { "normdevrtt",            DBL, { .d8 = &stats.normdevrtt, }, },
201                                 { "stdevrtt",              DBL, { .d8 = &stats.stdevrtt, }, },
202                                 { "local_ssrc",            INT, { .i4 = &stats.local_ssrc, }, },
203                                 { "remote_ssrc",           INT, { .i4 = &stats.remote_ssrc, }, },
204                                 { NULL, },
205                         };
206
207                         if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
208                                 return -1;
209                         }
210
211                         for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
212                                 if (!strcasecmp(args.field, lookup[i].name)) {
213                                         if (lookup[i].type == INT) {
214                                                 snprintf(buf, buflen, "%u", *lookup[i].i4);
215                                         } else {
216                                                 snprintf(buf, buflen, "%f", *lookup[i].d8);
217                                         }
218                                         return 0;
219                                 }
220                         }
221                         ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
222                         return -1;
223                 }
224         } else if (!strcasecmp(args.param, "secure_signaling")) {
225                 snprintf(buf, buflen, "%s", p->socket.type == SIP_TRANSPORT_TLS ? "1" : "");
226         } else if (!strcasecmp(args.param, "secure_media")) {
227                 snprintf(buf, buflen, "%s", p->srtp ? "1" : "");
228         } else {
229                 res = -1;
230         }
231         return res;
232 }
233
234 #ifdef TEST_FRAMEWORK
235 static int test_sip_rtpqos_1_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
236 {
237         /* Needed to pass sanity checks */
238         ast_rtp_instance_set_data(instance, data);
239         return 0;
240 }
241
242 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
243 {
244         /* Needed to pass sanity checks */
245         return 0;
246 }
247
248 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
249 {
250         /* Needed to pass sanity checks */
251         return &ast_null_frame;
252 }
253
254 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
255 {
256         /* Needed to pass sanity checks */
257         return 0;
258 }
259
260 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)
261 {
262         struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
263         memcpy(stats, s, sizeof(*stats));
264         return 0;
265 }
266
267 AST_TEST_DEFINE(test_sip_rtpqos_1)
268 {
269         int i, res = AST_TEST_PASS;
270         struct ast_rtp_engine test_engine = {
271                 .name = "test",
272                 .new = test_sip_rtpqos_1_new,
273                 .destroy = test_sip_rtpqos_1_destroy,
274                 .read = test_sip_rtpqos_1_read,
275                 .write = test_sip_rtpqos_1_write,
276                 .get_stat = test_sip_rtpqos_1_get_stat,
277         };
278         struct ast_sockaddr sa = { {0, } };
279         struct ast_rtp_instance_stats mine = { 0, };
280         struct sip_pvt *p = NULL;
281         struct ast_channel *chan = NULL;
282         struct ast_str *varstr = NULL, *buffer = NULL;
283         struct {
284                 const char *name;
285                 enum { INT, DBL } type;
286                 union {
287                         unsigned int *i4;
288                         double *d8;
289                 };
290         } lookup[] = {
291                 { "txcount",               INT, { .i4 = &mine.txcount, }, },
292                 { "rxcount",               INT, { .i4 = &mine.rxcount, }, },
293                 { "txjitter",              DBL, { .d8 = &mine.txjitter, }, },
294                 { "rxjitter",              DBL, { .d8 = &mine.rxjitter, }, },
295                 { "remote_maxjitter",      DBL, { .d8 = &mine.remote_maxjitter, }, },
296                 { "remote_minjitter",      DBL, { .d8 = &mine.remote_minjitter, }, },
297                 { "remote_normdevjitter",  DBL, { .d8 = &mine.remote_normdevjitter, }, },
298                 { "remote_stdevjitter",    DBL, { .d8 = &mine.remote_stdevjitter, }, },
299                 { "local_maxjitter",       DBL, { .d8 = &mine.local_maxjitter, }, },
300                 { "local_minjitter",       DBL, { .d8 = &mine.local_minjitter, }, },
301                 { "local_normdevjitter",   DBL, { .d8 = &mine.local_normdevjitter, }, },
302                 { "local_stdevjitter",     DBL, { .d8 = &mine.local_stdevjitter, }, },
303                 { "txploss",               INT, { .i4 = &mine.txploss, }, },
304                 { "rxploss",               INT, { .i4 = &mine.rxploss, }, },
305                 { "remote_maxrxploss",     DBL, { .d8 = &mine.remote_maxrxploss, }, },
306                 { "remote_minrxploss",     DBL, { .d8 = &mine.remote_minrxploss, }, },
307                 { "remote_normdevrxploss", DBL, { .d8 = &mine.remote_normdevrxploss, }, },
308                 { "remote_stdevrxploss",   DBL, { .d8 = &mine.remote_stdevrxploss, }, },
309                 { "local_maxrxploss",      DBL, { .d8 = &mine.local_maxrxploss, }, },
310                 { "local_minrxploss",      DBL, { .d8 = &mine.local_minrxploss, }, },
311                 { "local_normdevrxploss",  DBL, { .d8 = &mine.local_normdevrxploss, }, },
312                 { "local_stdevrxploss",    DBL, { .d8 = &mine.local_stdevrxploss, }, },
313                 { "rtt",                   DBL, { .d8 = &mine.rtt, }, },
314                 { "maxrtt",                DBL, { .d8 = &mine.maxrtt, }, },
315                 { "minrtt",                DBL, { .d8 = &mine.minrtt, }, },
316                 { "normdevrtt",            DBL, { .d8 = &mine.normdevrtt, }, },
317                 { "stdevrtt",              DBL, { .d8 = &mine.stdevrtt, }, },
318                 { "local_ssrc",            INT, { .i4 = &mine.local_ssrc, }, },
319                 { "remote_ssrc",           INT, { .i4 = &mine.remote_ssrc, }, },
320                 { NULL, },
321         };
322
323         switch (cmd) {
324         case TEST_INIT:
325                 info->name = "test_sip_rtpqos";
326                 info->category = "/channels/chan_sip/";
327                 info->summary = "Test retrieval of SIP RTP QOS stats";
328                 info->description =
329                         "Verify values in the RTP instance structure can be accessed through the dialplan.";
330                 return AST_TEST_NOT_RUN;
331         case TEST_EXECUTE:
332                 break;
333         }
334
335         ast_rtp_engine_register2(&test_engine, NULL);
336         /* Have to associate this with a SIP pvt and an ast_channel */
337         if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
338                 res = AST_TEST_NOT_RUN;
339                 goto done;
340         }
341
342         if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
343                 res = AST_TEST_NOT_RUN;
344                 goto done;
345         }
346         ast_rtp_instance_set_remote_address(p->rtp, &sa);
347         if (!(chan = ast_dummy_channel_alloc())) {
348                 res = AST_TEST_NOT_RUN;
349                 goto done;
350         }
351         ast_channel_tech_set(chan, &sip_tech);
352         ast_channel_tech_pvt_set(chan, p);
353         p->owner = chan;
354
355         varstr = ast_str_create(16);
356         buffer = ast_str_create(16);
357         if (!varstr || !buffer) {
358                 res = AST_TEST_NOT_RUN;
359                 goto done;
360         }
361
362         /* Populate "mine" with values, then retrieve them with the CHANNEL dialplan function */
363         for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
364                 ast_str_set(&varstr, 0, "${CHANNEL(rtpqos,audio,%s)}", lookup[i].name);
365                 if (lookup[i].type == INT) {
366                         int j;
367                         char cmpstr[256];
368                         for (j = 1; j < 25; j++) {
369                                 *lookup[i].i4 = j;
370                                 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
371                                 snprintf(cmpstr, sizeof(cmpstr), "%d", j);
372                                 if (strcmp(cmpstr, ast_str_buffer(buffer))) {
373                                         res = AST_TEST_FAIL;
374                                         ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
375                                         break;
376                                 }
377                         }
378                 } else {
379                         double j, cmpdbl = 0.0;
380                         for (j = 1.0; j < 10.0; j += 0.3) {
381                                 *lookup[i].d8 = j;
382                                 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
383                                 if (sscanf(ast_str_buffer(buffer), "%lf", &cmpdbl) != 1 || fabs(j - cmpdbl > .05)) {
384                                         res = AST_TEST_FAIL;
385                                         ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
386                                         break;
387                                 }
388                         }
389                 }
390         }
391
392 done:
393         ast_free(varstr);
394         ast_free(buffer);
395
396         /* This unref will take care of destroying the channel, RTP instance, and SIP pvt */
397         if (p) {
398                 dialog_unref(p, "Destroy test object");
399         }
400         ast_rtp_engine_unregister(&test_engine);
401         return res;
402 }
403 #endif
404
405 /*! \brief SIP test registration */
406 void sip_dialplan_function_register_tests(void)
407 {
408         AST_TEST_REGISTER(test_sip_rtpqos_1);
409 }
410
411 /*! \brief SIP test registration */
412 void sip_dialplan_function_unregister_tests(void)
413 {
414         AST_TEST_UNREGISTER(test_sip_rtpqos_1);
415 }
416