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