sip/dialplan_functions.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2010, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*!
00018  * \file
00019  * \brief sip channel dialplan functions and unit tests
00020  */
00021 
00022 /*** MODULEINFO
00023    <support_level>extended</support_level>
00024  ***/
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 433818 $")
00029 
00030 #include <math.h>
00031 
00032 #include "asterisk/channel.h"
00033 #include "asterisk/rtp_engine.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/acl.h"
00036 
00037 #include "include/sip.h"
00038 #include "include/globals.h"
00039 #include "include/dialog.h"
00040 #include "include/dialplan_functions.h"
00041 #include "include/sip_utils.h"
00042 
00043 
00044 int sip_acf_channel_read(struct ast_channel *chan, const char *funcname, char *preparse, char *buf, size_t buflen)
00045 {
00046    struct sip_pvt *p = ast_channel_tech_pvt(chan);
00047    char *parse = ast_strdupa(preparse);
00048    int res = 0;
00049    AST_DECLARE_APP_ARGS(args,
00050       AST_APP_ARG(param);
00051       AST_APP_ARG(type);
00052       AST_APP_ARG(field);
00053    );
00054       
00055    /* Check for zero arguments */
00056    if (ast_strlen_zero(parse)) {
00057       ast_log(LOG_ERROR, "Cannot call %s without arguments\n", funcname);
00058       return -1;
00059    }
00060 
00061    AST_STANDARD_APP_ARGS(args, parse);
00062 
00063    /* Sanity check */
00064    if (!IS_SIP_TECH(ast_channel_tech(chan))) {
00065       ast_log(LOG_ERROR, "Cannot call %s on a non-SIP channel\n", funcname);
00066       return 0;
00067    }
00068 
00069    memset(buf, 0, buflen);
00070 
00071    if (p == NULL) {
00072       return -1;
00073    }
00074 
00075    if (!strcasecmp(args.param, "peerip")) {
00076       ast_copy_string(buf, ast_sockaddr_isnull(&p->sa) ? "" : ast_sockaddr_stringify_addr(&p->sa), buflen);
00077    } else if (!strcasecmp(args.param, "recvip")) {
00078       ast_copy_string(buf, ast_sockaddr_isnull(&p->recv) ? "" : ast_sockaddr_stringify_addr(&p->recv), buflen);
00079    } else if (!strcasecmp(args.param, "recvport")) {
00080       ast_copy_string(buf, ast_sockaddr_isnull(&p->recv) ? "" : ast_sockaddr_stringify_port(&p->recv), buflen);
00081    } else if (!strcasecmp(args.param, "from")) {
00082       ast_copy_string(buf, p->from, buflen);
00083    } else if (!strcasecmp(args.param, "uri")) {
00084       ast_copy_string(buf, p->uri, buflen);
00085    } else if (!strcasecmp(args.param, "useragent")) {
00086       ast_copy_string(buf, p->useragent, buflen);
00087    } else if (!strcasecmp(args.param, "peername")) {
00088       ast_copy_string(buf, p->peername, buflen);
00089    } else if (!strcasecmp(args.param, "t38passthrough")) {
00090       ast_copy_string(buf, (p->t38.state == T38_DISABLED) ? "0" : "1", buflen);
00091    } else if (!strcasecmp(args.param, "rtpdest")) {
00092       struct ast_sockaddr addr;
00093       struct ast_rtp_instance *stream;
00094 
00095       if (ast_strlen_zero(args.type))
00096          args.type = "audio";
00097 
00098       if (!strcasecmp(args.type, "audio"))
00099          stream = p->rtp;
00100       else if (!strcasecmp(args.type, "video"))
00101          stream = p->vrtp;
00102       else if (!strcasecmp(args.type, "text"))
00103          stream = p->trtp;
00104       else
00105          return -1;
00106 
00107       /* Return 0 to suppress a console warning message */
00108       if (!stream) {
00109          return 0;
00110       }
00111 
00112       ast_rtp_instance_get_remote_address(stream, &addr);
00113       snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&addr));
00114    } else if (!strcasecmp(args.param, "rtpsource")) {
00115       struct ast_sockaddr sa;
00116       struct ast_rtp_instance *stream;
00117 
00118       if (ast_strlen_zero(args.type))
00119          args.type = "audio";
00120 
00121       if (!strcasecmp(args.type, "audio"))
00122          stream = p->rtp;
00123       else if (!strcasecmp(args.type, "video"))
00124          stream = p->vrtp;
00125       else if (!strcasecmp(args.type, "text"))
00126          stream = p->trtp;
00127       else
00128          return -1;
00129 
00130       /* Return 0 to suppress a console warning message */
00131       if (!stream) {
00132          return 0;
00133       }
00134 
00135       ast_rtp_instance_get_local_address(stream, &sa);
00136 
00137       if (ast_sockaddr_isnull(&sa)) {
00138          struct ast_sockaddr dest_sa;
00139          ast_rtp_instance_get_remote_address(stream, &dest_sa);
00140          ast_ouraddrfor(&dest_sa, &sa);
00141       }
00142 
00143       snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&sa));
00144    } else if (!strcasecmp(args.param, "rtpqos")) {
00145       struct ast_rtp_instance *rtp = NULL;
00146 
00147       if (ast_strlen_zero(args.type)) {
00148          args.type = "audio";
00149       }
00150 
00151       if (!strcasecmp(args.type, "audio")) {
00152          rtp = p->rtp;
00153       } else if (!strcasecmp(args.type, "video")) {
00154          rtp = p->vrtp;
00155       } else if (!strcasecmp(args.type, "text")) {
00156          rtp = p->trtp;
00157       } else {
00158          return -1;
00159       }
00160 
00161       if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
00162          char quality_buf[AST_MAX_USER_FIELD];
00163 
00164          if (!ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf))) {
00165             return -1;
00166          }
00167 
00168          ast_copy_string(buf, quality_buf, buflen);
00169          return res;
00170       } else {
00171          struct ast_rtp_instance_stats stats;
00172          int i;
00173          struct {
00174             const char *name;
00175             enum { INT, DBL } type;
00176             union {
00177                unsigned int *i4;
00178                double *d8;
00179             };
00180          } lookup[] = {
00181             { "txcount",               INT, { .i4 = &stats.txcount, }, },
00182             { "rxcount",               INT, { .i4 = &stats.rxcount, }, },
00183             { "txjitter",              DBL, { .d8 = &stats.txjitter, }, },
00184             { "rxjitter",              DBL, { .d8 = &stats.rxjitter, }, },
00185             { "remote_maxjitter",      DBL, { .d8 = &stats.remote_maxjitter, }, },
00186             { "remote_minjitter",      DBL, { .d8 = &stats.remote_minjitter, }, },
00187             { "remote_normdevjitter",  DBL, { .d8 = &stats.remote_normdevjitter, }, },
00188             { "remote_stdevjitter",    DBL, { .d8 = &stats.remote_stdevjitter, }, },
00189             { "local_maxjitter",       DBL, { .d8 = &stats.local_maxjitter, }, },
00190             { "local_minjitter",       DBL, { .d8 = &stats.local_minjitter, }, },
00191             { "local_normdevjitter",   DBL, { .d8 = &stats.local_normdevjitter, }, },
00192             { "local_stdevjitter",     DBL, { .d8 = &stats.local_stdevjitter, }, },
00193             { "txploss",               INT, { .i4 = &stats.txploss, }, },
00194             { "rxploss",               INT, { .i4 = &stats.rxploss, }, },
00195             { "remote_maxrxploss",     DBL, { .d8 = &stats.remote_maxrxploss, }, },
00196             { "remote_minrxploss",     DBL, { .d8 = &stats.remote_minrxploss, }, },
00197             { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
00198             { "remote_stdevrxploss",   DBL, { .d8 = &stats.remote_stdevrxploss, }, },
00199             { "local_maxrxploss",      DBL, { .d8 = &stats.local_maxrxploss, }, },
00200             { "local_minrxploss",      DBL, { .d8 = &stats.local_minrxploss, }, },
00201             { "local_normdevrxploss",  DBL, { .d8 = &stats.local_normdevrxploss, }, },
00202             { "local_stdevrxploss",    DBL, { .d8 = &stats.local_stdevrxploss, }, },
00203             { "rtt",                   DBL, { .d8 = &stats.rtt, }, },
00204             { "maxrtt",                DBL, { .d8 = &stats.maxrtt, }, },
00205             { "minrtt",                DBL, { .d8 = &stats.minrtt, }, },
00206             { "normdevrtt",            DBL, { .d8 = &stats.normdevrtt, }, },
00207             { "stdevrtt",              DBL, { .d8 = &stats.stdevrtt, }, },
00208             { "local_ssrc",            INT, { .i4 = &stats.local_ssrc, }, },
00209             { "remote_ssrc",           INT, { .i4 = &stats.remote_ssrc, }, },
00210             { NULL, },
00211          };
00212 
00213          if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
00214             return -1;
00215          }
00216 
00217          for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
00218             if (!strcasecmp(args.field, lookup[i].name)) {
00219                if (lookup[i].type == INT) {
00220                   snprintf(buf, buflen, "%u", *lookup[i].i4);
00221                } else {
00222                   snprintf(buf, buflen, "%f", *lookup[i].d8);
00223                }
00224                return 0;
00225             }
00226          }
00227          ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
00228          return -1;
00229       }
00230    } else if (!strcasecmp(args.param, "secure_signaling")) {
00231       snprintf(buf, buflen, "%s", p->socket.type == AST_TRANSPORT_TLS ? "1" : "");
00232    } else if (!strcasecmp(args.param, "secure_media")) {
00233       snprintf(buf, buflen, "%s", p->srtp ? "1" : "");
00234    } else {
00235       res = -1;
00236    }
00237    return res;
00238 }
00239 
00240 #ifdef TEST_FRAMEWORK
00241 static int test_sip_rtpqos_1_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
00242 {
00243    /* Needed to pass sanity checks */
00244    ast_rtp_instance_set_data(instance, data);
00245    return 0;
00246 }
00247 
00248 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
00249 {
00250    /* Needed to pass sanity checks */
00251    return 0;
00252 }
00253 
00254 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
00255 {
00256    /* Needed to pass sanity checks */
00257    return &ast_null_frame;
00258 }
00259 
00260 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
00261 {
00262    /* Needed to pass sanity checks */
00263    return 0;
00264 }
00265 
00266 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)
00267 {
00268    struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
00269    memcpy(stats, s, sizeof(*stats));
00270    return 0;
00271 }
00272 
00273 AST_TEST_DEFINE(test_sip_rtpqos_1)
00274 {
00275    int i, res = AST_TEST_PASS;
00276    struct ast_rtp_engine test_engine = {
00277       .name = "test",
00278       .new = test_sip_rtpqos_1_new,
00279       .destroy = test_sip_rtpqos_1_destroy,
00280       .read = test_sip_rtpqos_1_read,
00281       .write = test_sip_rtpqos_1_write,
00282       .get_stat = test_sip_rtpqos_1_get_stat,
00283    };
00284    struct ast_sockaddr sa = { {0, } };
00285    struct ast_rtp_instance_stats mine = { 0, };
00286    struct sip_pvt *p = NULL;
00287    struct ast_channel *chan = NULL;
00288    struct ast_str *varstr = NULL, *buffer = NULL;
00289    struct {
00290       const char *name;
00291       enum { INT, DBL } type;
00292       union {
00293          unsigned int *i4;
00294          double *d8;
00295       };
00296    } lookup[] = {
00297       { "txcount",               INT, { .i4 = &mine.txcount, }, },
00298       { "rxcount",               INT, { .i4 = &mine.rxcount, }, },
00299       { "txjitter",              DBL, { .d8 = &mine.txjitter, }, },
00300       { "rxjitter",              DBL, { .d8 = &mine.rxjitter, }, },
00301       { "remote_maxjitter",      DBL, { .d8 = &mine.remote_maxjitter, }, },
00302       { "remote_minjitter",      DBL, { .d8 = &mine.remote_minjitter, }, },
00303       { "remote_normdevjitter",  DBL, { .d8 = &mine.remote_normdevjitter, }, },
00304       { "remote_stdevjitter",    DBL, { .d8 = &mine.remote_stdevjitter, }, },
00305       { "local_maxjitter",       DBL, { .d8 = &mine.local_maxjitter, }, },
00306       { "local_minjitter",       DBL, { .d8 = &mine.local_minjitter, }, },
00307       { "local_normdevjitter",   DBL, { .d8 = &mine.local_normdevjitter, }, },
00308       { "local_stdevjitter",     DBL, { .d8 = &mine.local_stdevjitter, }, },
00309       { "txploss",               INT, { .i4 = &mine.txploss, }, },
00310       { "rxploss",               INT, { .i4 = &mine.rxploss, }, },
00311       { "remote_maxrxploss",     DBL, { .d8 = &mine.remote_maxrxploss, }, },
00312       { "remote_minrxploss",     DBL, { .d8 = &mine.remote_minrxploss, }, },
00313       { "remote_normdevrxploss", DBL, { .d8 = &mine.remote_normdevrxploss, }, },
00314       { "remote_stdevrxploss",   DBL, { .d8 = &mine.remote_stdevrxploss, }, },
00315       { "local_maxrxploss",      DBL, { .d8 = &mine.local_maxrxploss, }, },
00316       { "local_minrxploss",      DBL, { .d8 = &mine.local_minrxploss, }, },
00317       { "local_normdevrxploss",  DBL, { .d8 = &mine.local_normdevrxploss, }, },
00318       { "local_stdevrxploss",    DBL, { .d8 = &mine.local_stdevrxploss, }, },
00319       { "rtt",                   DBL, { .d8 = &mine.rtt, }, },
00320       { "maxrtt",                DBL, { .d8 = &mine.maxrtt, }, },
00321       { "minrtt",                DBL, { .d8 = &mine.minrtt, }, },
00322       { "normdevrtt",            DBL, { .d8 = &mine.normdevrtt, }, },
00323       { "stdevrtt",              DBL, { .d8 = &mine.stdevrtt, }, },
00324       { "local_ssrc",            INT, { .i4 = &mine.local_ssrc, }, },
00325       { "remote_ssrc",           INT, { .i4 = &mine.remote_ssrc, }, },
00326       { NULL, },
00327    };
00328 
00329    switch (cmd) {
00330    case TEST_INIT:
00331       info->name = "test_sip_rtpqos";
00332       info->category = "/channels/chan_sip/";
00333       info->summary = "Test retrieval of SIP RTP QOS stats";
00334       info->description =
00335          "Verify values in the RTP instance structure can be accessed through the dialplan.";
00336       return AST_TEST_NOT_RUN;
00337    case TEST_EXECUTE:
00338       break;
00339    }
00340 
00341    ast_rtp_engine_register2(&test_engine, NULL);
00342    /* Have to associate this with a SIP pvt and an ast_channel */
00343    if (!(p = sip_alloc(0, NULL, 0, SIP_NOTIFY, NULL, 0))) {
00344       res = AST_TEST_NOT_RUN;
00345       goto done;
00346    }
00347 
00348    if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
00349       res = AST_TEST_NOT_RUN;
00350       goto done;
00351    }
00352    ast_rtp_instance_set_remote_address(p->rtp, &sa);
00353    if (!(chan = ast_dummy_channel_alloc())) {
00354       res = AST_TEST_NOT_RUN;
00355       goto done;
00356    }
00357    ast_channel_tech_set(chan, &sip_tech);
00358    ast_channel_tech_pvt_set(chan, dialog_ref(p, "Give the owner channel a reference to the dialog"));
00359    p->owner = chan;
00360 
00361    varstr = ast_str_create(16);
00362    buffer = ast_str_create(16);
00363    if (!varstr || !buffer) {
00364       res = AST_TEST_NOT_RUN;
00365       goto done;
00366    }
00367 
00368    /* Populate "mine" with values, then retrieve them with the CHANNEL dialplan function */
00369    for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
00370       ast_str_set(&varstr, 0, "${CHANNEL(rtpqos,audio,%s)}", lookup[i].name);
00371       if (lookup[i].type == INT) {
00372          int j;
00373          char cmpstr[256];
00374          for (j = 1; j < 25; j++) {
00375             *lookup[i].i4 = j;
00376             ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
00377             snprintf(cmpstr, sizeof(cmpstr), "%d", j);
00378             if (strcmp(cmpstr, ast_str_buffer(buffer))) {
00379                res = AST_TEST_FAIL;
00380                ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
00381                break;
00382             }
00383          }
00384       } else {
00385          double j, cmpdbl = 0.0;
00386          for (j = 1.0; j < 10.0; j += 0.3) {
00387             *lookup[i].d8 = j;
00388             ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
00389             if (sscanf(ast_str_buffer(buffer), "%lf", &cmpdbl) != 1 || fabs(j - cmpdbl) > .05) {
00390                res = AST_TEST_FAIL;
00391                ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
00392                break;
00393             }
00394          }
00395       }
00396    }
00397 
00398 done:
00399    ast_free(varstr);
00400    ast_free(buffer);
00401 
00402    /* This unlink and unref will take care of destroying the channel, RTP instance, and SIP pvt */
00403    if (p) {
00404       dialog_unlink_all(p);
00405       dialog_unref(p, "Destroy test object");
00406    }
00407    ast_rtp_engine_unregister(&test_engine);
00408    return res;
00409 }
00410 #endif
00411 
00412 /*! \brief SIP test registration */
00413 void sip_dialplan_function_register_tests(void)
00414 {
00415    AST_TEST_REGISTER(test_sip_rtpqos_1);
00416 }
00417 
00418 /*! \brief SIP test registration */
00419 void sip_dialplan_function_unregister_tests(void)
00420 {
00421    AST_TEST_UNREGISTER(test_sip_rtpqos_1);
00422 }
00423 

Generated on Thu Apr 16 06:27:33 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6