Merge "funcs/func_devstate: Remove new line in Device field of during module load"
[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>extended</support_level>
24  ***/
25
26 /*** DOCUMENTATION
27 <info name="CHANNEL" language="en_US" tech="SIP">
28         <enumlist>
29                 <enum name="peerip">
30                         <para>R/O Get the IP address of the peer.</para>
31                 </enum>
32                 <enum name="recvip">
33                         <para>R/O Get the source IP address of the peer.</para>
34                 </enum>
35                 <enum name="recvport">
36                         <para>R/O Get the source port of the peer.</para>
37                 </enum>
38                 <enum name="from">
39                         <para>R/O Get the URI from the From: header.</para>
40                 </enum>
41                 <enum name="uri">
42                         <para>R/O Get the URI from the Contact: header.</para>
43                 </enum>
44                 <enum name="useragent">
45                         <para>R/O Get the useragent.</para>
46                 </enum>
47                 <enum name="peername">
48                         <para>R/O Get the name of the peer.</para>
49                 </enum>
50                 <enum name="t38passthrough">
51                         <para>R/O <literal>1</literal> if T38 is offered or enabled in this channel,
52                         otherwise <literal>0</literal></para>
53                 </enum>
54                 <enum name="rtpqos">
55                         <para>R/O Get QOS information about the RTP stream</para>
56                         <para>    This option takes two additional arguments:</para>
57                         <para>    Argument 1:</para>
58                         <para>     <literal>audio</literal>             Get data about the audio stream</para>
59                         <para>     <literal>video</literal>             Get data about the video stream</para>
60                         <para>     <literal>text</literal>              Get data about the text stream</para>
61                         <para>    Argument 2:</para>
62                         <para>     <literal>local_ssrc</literal>        Local SSRC (stream ID)</para>
63                         <para>     <literal>local_lostpackets</literal> Local lost packets</para>
64                         <para>     <literal>local_jitter</literal>      Local calculated jitter</para>
65                         <para>     <literal>local_maxjitter</literal>   Local calculated jitter (maximum)</para>
66                         <para>     <literal>local_minjitter</literal>   Local calculated jitter (minimum)</para>
67                         <para>     <literal>local_normdevjitter</literal>Local calculated jitter (normal deviation)</para>
68                         <para>     <literal>local_stdevjitter</literal> Local calculated jitter (standard deviation)</para>
69                         <para>     <literal>local_count</literal>       Number of received packets</para>
70                         <para>     <literal>remote_ssrc</literal>       Remote SSRC (stream ID)</para>
71                         <para>     <literal>remote_lostpackets</literal>Remote lost packets</para>
72                         <para>     <literal>remote_jitter</literal>     Remote reported jitter</para>
73                         <para>     <literal>remote_maxjitter</literal>  Remote calculated jitter (maximum)</para>
74                         <para>     <literal>remote_minjitter</literal>  Remote calculated jitter (minimum)</para>
75                         <para>     <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
76                         <para>     <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
77                         <para>     <literal>remote_count</literal>      Number of transmitted packets</para>
78                         <para>     <literal>rtt</literal>               Round trip time</para>
79                         <para>     <literal>maxrtt</literal>            Round trip time (maximum)</para>
80                         <para>     <literal>minrtt</literal>            Round trip time (minimum)</para>
81                         <para>     <literal>normdevrtt</literal>        Round trip time (normal deviation)</para>
82                         <para>     <literal>stdevrtt</literal>          Round trip time (standard deviation)</para>
83                         <para>     <literal>all</literal>               All statistics (in a form suited to logging,
84                         but not for parsing)</para>
85                 </enum>
86                 <enum name="rtpdest">
87                         <para>R/O Get remote RTP destination information.</para>
88                         <para>   This option takes one additional argument:</para>
89                         <para>    Argument 1:</para>
90                         <para>     <literal>audio</literal>             Get audio destination</para>
91                         <para>     <literal>video</literal>             Get video destination</para>
92                         <para>     <literal>text</literal>              Get text destination</para>
93                         <para>   Defaults to <literal>audio</literal> if unspecified.</para>
94                 </enum>
95                 <enum name="rtpsource">
96                         <para>R/O Get source RTP destination information.</para>
97                         <para>   This option takes one additional argument:</para>
98                         <para>    Argument 1:</para>
99                         <para>     <literal>audio</literal>             Get audio destination</para>
100                         <para>     <literal>video</literal>             Get video destination</para>
101                         <para>     <literal>text</literal>              Get text destination</para>
102                         <para>   Defaults to <literal>audio</literal> if unspecified.</para>
103                 </enum>
104         </enumlist>
105 </info>
106  ***/
107
108 #include "asterisk.h"
109
110 #include <math.h>
111
112 #include "asterisk/channel.h"
113 #include "asterisk/rtp_engine.h"
114 #include "asterisk/pbx.h"
115 #include "asterisk/acl.h"
116
117 #include "include/sip.h"
118 #include "include/globals.h"
119 #include "include/dialog.h"
120 #include "include/dialplan_functions.h"
121 #include "include/sip_utils.h"
122
123
124 int sip_acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
125 {
126         struct sip_pvt *p = ast_channel_tech_pvt(chan);
127         char *parse = ast_strdupa(preparse);
128         int res = 0;
129         AST_DECLARE_APP_ARGS(args,
130                 AST_APP_ARG(param);
131                 AST_APP_ARG(type);
132                 AST_APP_ARG(field);
133         );
134                 
135         /* Check for zero arguments */
136         if (ast_strlen_zero(parse)) {
137                 ast_log(LOG_ERROR, "Cannot call %s without arguments\n", funcname);
138                 return -1;
139         }
140
141         AST_STANDARD_APP_ARGS(args, parse);
142
143         /* Sanity check */
144         if (!IS_SIP_TECH(ast_channel_tech(chan))) {
145                 ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
146                 return 0;
147         }
148
149         memset(buf, 0, buflen);
150
151         if (p == NULL) {
152                 return -1;
153         }
154
155         if (!strcasecmp(args.param, "peerip")) {
156                 ast_copy_string(buf, ast_sockaddr_isnull(&p->sa) ? "" : ast_sockaddr_stringify_addr(&p->sa), buflen);
157         } else if (!strcasecmp(args.param, "recvip")) {
158                 ast_copy_string(buf, ast_sockaddr_isnull(&p->recv) ? "" : ast_sockaddr_stringify_addr(&p->recv), buflen);
159         } else if (!strcasecmp(args.param, "recvport")) {
160                 ast_copy_string(buf, ast_sockaddr_isnull(&p->recv) ? "" : ast_sockaddr_stringify_port(&p->recv), buflen);
161         } else if (!strcasecmp(args.param, "from")) {
162                 ast_copy_string(buf, p->from, buflen);
163         } else if (!strcasecmp(args.param, "uri")) {
164                 ast_copy_string(buf, p->uri, buflen);
165         } else if (!strcasecmp(args.param, "useragent")) {
166                 ast_copy_string(buf, p->useragent, buflen);
167         } else if (!strcasecmp(args.param, "peername")) {
168                 ast_copy_string(buf, p->peername, buflen);
169         } else if (!strcasecmp(args.param, "t38passthrough")) {
170                 ast_copy_string(buf, (p->t38.state == T38_DISABLED) ? "0" : "1", buflen);
171         } else if (!strcasecmp(args.param, "rtpdest")) {
172                 struct ast_sockaddr addr;
173                 struct ast_rtp_instance *stream;
174
175                 if (ast_strlen_zero(args.type))
176                         args.type = "audio";
177
178                 if (!strcasecmp(args.type, "audio"))
179                         stream = p->rtp;
180                 else if (!strcasecmp(args.type, "video"))
181                         stream = p->vrtp;
182                 else if (!strcasecmp(args.type, "text"))
183                         stream = p->trtp;
184                 else
185                         return -1;
186
187                 /* Return 0 to suppress a console warning message */
188                 if (!stream) {
189                         return 0;
190                 }
191
192                 ast_rtp_instance_get_remote_address(stream, &addr);
193                 snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&addr));
194         } else if (!strcasecmp(args.param, "rtpsource")) {
195                 struct ast_sockaddr sa;
196                 struct ast_rtp_instance *stream;
197
198                 if (ast_strlen_zero(args.type))
199                         args.type = "audio";
200
201                 if (!strcasecmp(args.type, "audio"))
202                         stream = p->rtp;
203                 else if (!strcasecmp(args.type, "video"))
204                         stream = p->vrtp;
205                 else if (!strcasecmp(args.type, "text"))
206                         stream = p->trtp;
207                 else
208                         return -1;
209
210                 /* Return 0 to suppress a console warning message */
211                 if (!stream) {
212                         return 0;
213                 }
214
215                 ast_rtp_instance_get_local_address(stream, &sa);
216
217                 if (ast_sockaddr_isnull(&sa)) {
218                         struct ast_sockaddr dest_sa;
219                         ast_rtp_instance_get_remote_address(stream, &dest_sa);
220                         ast_ouraddrfor(&dest_sa, &sa);
221                 }
222
223                 snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&sa));
224         } else if (!strcasecmp(args.param, "rtpqos")) {
225                 struct ast_rtp_instance *rtp = NULL;
226
227                 if (ast_strlen_zero(args.type)) {
228                         args.type = "audio";
229                 }
230
231                 if (!strcasecmp(args.type, "audio")) {
232                         rtp = p->rtp;
233                 } else if (!strcasecmp(args.type, "video")) {
234                         rtp = p->vrtp;
235                 } else if (!strcasecmp(args.type, "text")) {
236                         rtp = p->trtp;
237                 } else {
238                         return -1;
239                 }
240
241                 if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
242                         char quality_buf[AST_MAX_USER_FIELD];
243
244                         if (!ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf))) {
245                                 return -1;
246                         }
247
248                         ast_copy_string(buf, quality_buf, buflen);
249                         return res;
250                 } else {
251                         struct ast_rtp_instance_stats stats;
252                         int i;
253                         struct {
254                                 const char *name;
255                                 enum { INT, DBL } type;
256                                 union {
257                                         unsigned int *i4;
258                                         double *d8;
259                                 };
260                         } lookup[] = {
261                                 { "txcount",               INT, { .i4 = &stats.txcount, }, },
262                                 { "rxcount",               INT, { .i4 = &stats.rxcount, }, },
263                                 { "txjitter",              DBL, { .d8 = &stats.txjitter, }, },
264                                 { "rxjitter",              DBL, { .d8 = &stats.rxjitter, }, },
265                                 { "remote_maxjitter",      DBL, { .d8 = &stats.remote_maxjitter, }, },
266                                 { "remote_minjitter",      DBL, { .d8 = &stats.remote_minjitter, }, },
267                                 { "remote_normdevjitter",  DBL, { .d8 = &stats.remote_normdevjitter, }, },
268                                 { "remote_stdevjitter",    DBL, { .d8 = &stats.remote_stdevjitter, }, },
269                                 { "local_maxjitter",       DBL, { .d8 = &stats.local_maxjitter, }, },
270                                 { "local_minjitter",       DBL, { .d8 = &stats.local_minjitter, }, },
271                                 { "local_normdevjitter",   DBL, { .d8 = &stats.local_normdevjitter, }, },
272                                 { "local_stdevjitter",     DBL, { .d8 = &stats.local_stdevjitter, }, },
273                                 { "txploss",               INT, { .i4 = &stats.txploss, }, },
274                                 { "rxploss",               INT, { .i4 = &stats.rxploss, }, },
275                                 { "remote_maxrxploss",     DBL, { .d8 = &stats.remote_maxrxploss, }, },
276                                 { "remote_minrxploss",     DBL, { .d8 = &stats.remote_minrxploss, }, },
277                                 { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
278                                 { "remote_stdevrxploss",   DBL, { .d8 = &stats.remote_stdevrxploss, }, },
279                                 { "local_maxrxploss",      DBL, { .d8 = &stats.local_maxrxploss, }, },
280                                 { "local_minrxploss",      DBL, { .d8 = &stats.local_minrxploss, }, },
281                                 { "local_normdevrxploss",  DBL, { .d8 = &stats.local_normdevrxploss, }, },
282                                 { "local_stdevrxploss",    DBL, { .d8 = &stats.local_stdevrxploss, }, },
283                                 { "rtt",                   DBL, { .d8 = &stats.rtt, }, },
284                                 { "maxrtt",                DBL, { .d8 = &stats.maxrtt, }, },
285                                 { "minrtt",                DBL, { .d8 = &stats.minrtt, }, },
286                                 { "normdevrtt",            DBL, { .d8 = &stats.normdevrtt, }, },
287                                 { "stdevrtt",              DBL, { .d8 = &stats.stdevrtt, }, },
288                                 { "local_ssrc",            INT, { .i4 = &stats.local_ssrc, }, },
289                                 { "remote_ssrc",           INT, { .i4 = &stats.remote_ssrc, }, },
290                                 { NULL, },
291                         };
292
293                         if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
294                                 return -1;
295                         }
296
297                         for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
298                                 if (!strcasecmp(args.field, lookup[i].name)) {
299                                         if (lookup[i].type == INT) {
300                                                 snprintf(buf, buflen, "%u", *lookup[i].i4);
301                                         } else {
302                                                 snprintf(buf, buflen, "%f", *lookup[i].d8);
303                                         }
304                                         return 0;
305                                 }
306                         }
307                         ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
308                         return -1;
309                 }
310         } else if (!strcasecmp(args.param, "secure_signaling")) {
311                 snprintf(buf, buflen, "%s", p->socket.type == AST_TRANSPORT_TLS ? "1" : "");
312         } else if (!strcasecmp(args.param, "secure_media")) {
313                 snprintf(buf, buflen, "%s", p->srtp ? "1" : "");
314         } else {
315                 res = -1;
316         }
317         return res;
318 }
319
320 #ifdef TEST_FRAMEWORK
321 static int test_sip_rtpqos_1_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
322 {
323         /* Needed to pass sanity checks */
324         ast_rtp_instance_set_data(instance, data);
325         return 0;
326 }
327
328 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
329 {
330         /* Needed to pass sanity checks */
331         return 0;
332 }
333
334 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
335 {
336         /* Needed to pass sanity checks */
337         return &ast_null_frame;
338 }
339
340 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
341 {
342         /* Needed to pass sanity checks */
343         return 0;
344 }
345
346 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)
347 {
348         struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
349         memcpy(stats, s, sizeof(*stats));
350         return 0;
351 }
352
353 AST_TEST_DEFINE(test_sip_rtpqos_1)
354 {
355         int i, res = AST_TEST_PASS;
356         static struct ast_rtp_engine test_engine = {
357                 .name = "test",
358                 .new = test_sip_rtpqos_1_new,
359                 .destroy = test_sip_rtpqos_1_destroy,
360                 .read = test_sip_rtpqos_1_read,
361                 .write = test_sip_rtpqos_1_write,
362                 .get_stat = test_sip_rtpqos_1_get_stat,
363         };
364         struct ast_sockaddr sa = { {0, } };
365         struct ast_rtp_instance_stats mine = { 0, };
366         struct sip_pvt *p = NULL;
367         struct ast_channel *chan = NULL;
368         struct ast_str *varstr = NULL, *buffer = NULL;
369         struct {
370                 const char *name;
371                 enum { INT, DBL } type;
372                 union {
373                         unsigned int *i4;
374                         double *d8;
375                 };
376         } lookup[] = {
377                 { "txcount",               INT, { .i4 = &mine.txcount, }, },
378                 { "rxcount",               INT, { .i4 = &mine.rxcount, }, },
379                 { "txjitter",              DBL, { .d8 = &mine.txjitter, }, },
380                 { "rxjitter",              DBL, { .d8 = &mine.rxjitter, }, },
381                 { "remote_maxjitter",      DBL, { .d8 = &mine.remote_maxjitter, }, },
382                 { "remote_minjitter",      DBL, { .d8 = &mine.remote_minjitter, }, },
383                 { "remote_normdevjitter",  DBL, { .d8 = &mine.remote_normdevjitter, }, },
384                 { "remote_stdevjitter",    DBL, { .d8 = &mine.remote_stdevjitter, }, },
385                 { "local_maxjitter",       DBL, { .d8 = &mine.local_maxjitter, }, },
386                 { "local_minjitter",       DBL, { .d8 = &mine.local_minjitter, }, },
387                 { "local_normdevjitter",   DBL, { .d8 = &mine.local_normdevjitter, }, },
388                 { "local_stdevjitter",     DBL, { .d8 = &mine.local_stdevjitter, }, },
389                 { "txploss",               INT, { .i4 = &mine.txploss, }, },
390                 { "rxploss",               INT, { .i4 = &mine.rxploss, }, },
391                 { "remote_maxrxploss",     DBL, { .d8 = &mine.remote_maxrxploss, }, },
392                 { "remote_minrxploss",     DBL, { .d8 = &mine.remote_minrxploss, }, },
393                 { "remote_normdevrxploss", DBL, { .d8 = &mine.remote_normdevrxploss, }, },
394                 { "remote_stdevrxploss",   DBL, { .d8 = &mine.remote_stdevrxploss, }, },
395                 { "local_maxrxploss",      DBL, { .d8 = &mine.local_maxrxploss, }, },
396                 { "local_minrxploss",      DBL, { .d8 = &mine.local_minrxploss, }, },
397                 { "local_normdevrxploss",  DBL, { .d8 = &mine.local_normdevrxploss, }, },
398                 { "local_stdevrxploss",    DBL, { .d8 = &mine.local_stdevrxploss, }, },
399                 { "rtt",                   DBL, { .d8 = &mine.rtt, }, },
400                 { "maxrtt",                DBL, { .d8 = &mine.maxrtt, }, },
401                 { "minrtt",                DBL, { .d8 = &mine.minrtt, }, },
402                 { "normdevrtt",            DBL, { .d8 = &mine.normdevrtt, }, },
403                 { "stdevrtt",              DBL, { .d8 = &mine.stdevrtt, }, },
404                 { "local_ssrc",            INT, { .i4 = &mine.local_ssrc, }, },
405                 { "remote_ssrc",           INT, { .i4 = &mine.remote_ssrc, }, },
406                 { NULL, },
407         };
408
409         switch (cmd) {
410         case TEST_INIT:
411                 info->name = "test_sip_rtpqos";
412                 info->category = "/channels/chan_sip/";
413                 info->summary = "Test retrieval of SIP RTP QOS stats";
414                 info->description =
415                         "Verify values in the RTP instance structure can be accessed through the dialplan.";
416                 return AST_TEST_NOT_RUN;
417         case TEST_EXECUTE:
418                 break;
419         }
420
421         ast_rtp_engine_register(&test_engine);
422         /* Have to associate this with a SIP pvt and an ast_channel */
423         if (!(p = sip_alloc(0, NULL, 0, SIP_NOTIFY, NULL, 0))) {
424                 res = AST_TEST_NOT_RUN;
425                 goto done;
426         }
427
428         if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
429                 res = AST_TEST_NOT_RUN;
430                 goto done;
431         }
432         ast_rtp_instance_set_remote_address(p->rtp, &sa);
433         if (!(chan = ast_dummy_channel_alloc())) {
434                 res = AST_TEST_NOT_RUN;
435                 goto done;
436         }
437         ast_channel_tech_set(chan, &sip_tech);
438         ast_channel_tech_pvt_set(chan, dialog_ref(p, "Give the owner channel a reference to the dialog"));
439         p->owner = chan;
440
441         varstr = ast_str_create(16);
442         buffer = ast_str_create(16);
443         if (!varstr || !buffer) {
444                 res = AST_TEST_NOT_RUN;
445                 goto done;
446         }
447
448         /* Populate "mine" with values, then retrieve them with the CHANNEL dialplan function */
449         for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
450                 ast_str_set(&varstr, 0, "${CHANNEL(rtpqos,audio,%s)}", lookup[i].name);
451                 if (lookup[i].type == INT) {
452                         int j;
453                         char cmpstr[256];
454                         for (j = 1; j < 25; j++) {
455                                 *lookup[i].i4 = j;
456                                 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
457                                 snprintf(cmpstr, sizeof(cmpstr), "%d", j);
458                                 if (strcmp(cmpstr, ast_str_buffer(buffer))) {
459                                         res = AST_TEST_FAIL;
460                                         ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
461                                         break;
462                                 }
463                         }
464                 } else {
465                         double j, cmpdbl = 0.0;
466                         for (j = 1.0; j < 10.0; j += 0.3) {
467                                 *lookup[i].d8 = j;
468                                 ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
469                                 if (sscanf(ast_str_buffer(buffer), "%lf", &cmpdbl) != 1 || fabs(j - cmpdbl) > .05) {
470                                         res = AST_TEST_FAIL;
471                                         ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
472                                         break;
473                                 }
474                         }
475                 }
476         }
477
478 done:
479         ast_free(varstr);
480         ast_free(buffer);
481
482         /* This unlink and unref will take care of destroying the channel, RTP instance, and SIP pvt */
483         if (p) {
484                 dialog_unlink_all(p);
485                 dialog_unref(p, "Destroy test object");
486         }
487         ast_rtp_engine_unregister(&test_engine);
488         return res;
489 }
490 #endif
491
492 /*! \brief SIP test registration */
493 void sip_dialplan_function_register_tests(void)
494 {
495         AST_TEST_REGISTER(test_sip_rtpqos_1);
496 }
497
498 /*! \brief SIP test registration */
499 void sip_dialplan_function_unregister_tests(void)
500 {
501         AST_TEST_UNREGISTER(test_sip_rtpqos_1);
502 }
503