Wed Aug 7 17:15:41 2019

Asterisk developer's documentation


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>core</support_level>
00024  ***/
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 397112 $")
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 = chan->tech_pvt;
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(chan->tech)) {
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, "from")) {
00080       ast_copy_string(buf, p->from, buflen);
00081    } else if (!strcasecmp(args.param, "uri")) {
00082       ast_copy_string(buf, p->uri, buflen);
00083    } else if (!strcasecmp(args.param, "useragent")) {
00084       ast_copy_string(buf, p->useragent, buflen);
00085    } else if (!strcasecmp(args.param, "peername")) {
00086       ast_copy_string(buf, p->peername, buflen);
00087    } else if (!strcasecmp(args.param, "t38passthrough")) {
00088       ast_copy_string(buf, (p->t38.state == T38_DISABLED) ? "0" : "1", buflen);
00089    } else if (!strcasecmp(args.param, "rtpdest")) {
00090       struct ast_sockaddr addr;
00091       struct ast_rtp_instance *stream;
00092 
00093       if (ast_strlen_zero(args.type))
00094          args.type = "audio";
00095 
00096       if (!strcasecmp(args.type, "audio"))
00097          stream = p->rtp;
00098       else if (!strcasecmp(args.type, "video"))
00099          stream = p->vrtp;
00100       else if (!strcasecmp(args.type, "text"))
00101          stream = p->trtp;
00102       else
00103          return -1;
00104 
00105       /* Return 0 to suppress a console warning message */
00106       if (!stream) {
00107          return 0;
00108       }
00109 
00110       ast_rtp_instance_get_remote_address(stream, &addr);
00111       snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&addr));
00112    } else if (!strcasecmp(args.param, "rtpsource")) {
00113       struct ast_sockaddr sa;
00114       struct ast_rtp_instance *stream;
00115 
00116       if (ast_strlen_zero(args.type))
00117          args.type = "audio";
00118 
00119       if (!strcasecmp(args.type, "audio"))
00120          stream = p->rtp;
00121       else if (!strcasecmp(args.type, "video"))
00122          stream = p->vrtp;
00123       else if (!strcasecmp(args.type, "text"))
00124          stream = p->trtp;
00125       else
00126          return -1;
00127 
00128       /* Return 0 to suppress a console warning message */
00129       if (!stream) {
00130          return 0;
00131       }
00132 
00133       ast_rtp_instance_get_local_address(stream, &sa);
00134 
00135       if (ast_sockaddr_isnull(&sa)) {
00136          struct ast_sockaddr dest_sa;
00137          ast_rtp_instance_get_remote_address(stream, &dest_sa);
00138          ast_ouraddrfor(&dest_sa, &sa);
00139       }
00140 
00141       snprintf(buf, buflen, "%s", ast_sockaddr_stringify(&sa));
00142    } else if (!strcasecmp(args.param, "rtpqos")) {
00143       struct ast_rtp_instance *rtp = NULL;
00144 
00145       if (ast_strlen_zero(args.type)) {
00146          args.type = "audio";
00147       }
00148 
00149       if (!strcasecmp(args.type, "audio")) {
00150          rtp = p->rtp;
00151       } else if (!strcasecmp(args.type, "video")) {
00152          rtp = p->vrtp;
00153       } else if (!strcasecmp(args.type, "text")) {
00154          rtp = p->trtp;
00155       } else {
00156          return -1;
00157       }
00158 
00159       if (ast_strlen_zero(args.field) || !strcasecmp(args.field, "all")) {
00160          char quality_buf[AST_MAX_USER_FIELD];
00161 
00162          if (!ast_rtp_instance_get_quality(rtp, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf))) {
00163             return -1;
00164          }
00165 
00166          ast_copy_string(buf, quality_buf, buflen);
00167          return res;
00168       } else {
00169          struct ast_rtp_instance_stats stats;
00170          int i;
00171          struct {
00172             const char *name;
00173             enum { INT, DBL } type;
00174             union {
00175                unsigned int *i4;
00176                double *d8;
00177             };
00178          } lookup[] = {
00179             { "txcount",               INT, { .i4 = &stats.txcount, }, },
00180             { "rxcount",               INT, { .i4 = &stats.rxcount, }, },
00181             { "txjitter",              DBL, { .d8 = &stats.txjitter, }, },
00182             { "rxjitter",              DBL, { .d8 = &stats.rxjitter, }, },
00183             { "remote_maxjitter",      DBL, { .d8 = &stats.remote_maxjitter, }, },
00184             { "remote_minjitter",      DBL, { .d8 = &stats.remote_minjitter, }, },
00185             { "remote_normdevjitter",  DBL, { .d8 = &stats.remote_normdevjitter, }, },
00186             { "remote_stdevjitter",    DBL, { .d8 = &stats.remote_stdevjitter, }, },
00187             { "local_maxjitter",       DBL, { .d8 = &stats.local_maxjitter, }, },
00188             { "local_minjitter",       DBL, { .d8 = &stats.local_minjitter, }, },
00189             { "local_normdevjitter",   DBL, { .d8 = &stats.local_normdevjitter, }, },
00190             { "local_stdevjitter",     DBL, { .d8 = &stats.local_stdevjitter, }, },
00191             { "txploss",               INT, { .i4 = &stats.txploss, }, },
00192             { "rxploss",               INT, { .i4 = &stats.rxploss, }, },
00193             { "remote_maxrxploss",     DBL, { .d8 = &stats.remote_maxrxploss, }, },
00194             { "remote_minrxploss",     DBL, { .d8 = &stats.remote_minrxploss, }, },
00195             { "remote_normdevrxploss", DBL, { .d8 = &stats.remote_normdevrxploss, }, },
00196             { "remote_stdevrxploss",   DBL, { .d8 = &stats.remote_stdevrxploss, }, },
00197             { "local_maxrxploss",      DBL, { .d8 = &stats.local_maxrxploss, }, },
00198             { "local_minrxploss",      DBL, { .d8 = &stats.local_minrxploss, }, },
00199             { "local_normdevrxploss",  DBL, { .d8 = &stats.local_normdevrxploss, }, },
00200             { "local_stdevrxploss",    DBL, { .d8 = &stats.local_stdevrxploss, }, },
00201             { "rtt",                   DBL, { .d8 = &stats.rtt, }, },
00202             { "maxrtt",                DBL, { .d8 = &stats.maxrtt, }, },
00203             { "minrtt",                DBL, { .d8 = &stats.minrtt, }, },
00204             { "normdevrtt",            DBL, { .d8 = &stats.normdevrtt, }, },
00205             { "stdevrtt",              DBL, { .d8 = &stats.stdevrtt, }, },
00206             { "local_ssrc",            INT, { .i4 = &stats.local_ssrc, }, },
00207             { "remote_ssrc",           INT, { .i4 = &stats.remote_ssrc, }, },
00208             { NULL, },
00209          };
00210 
00211          if (ast_rtp_instance_get_stats(rtp, &stats, AST_RTP_INSTANCE_STAT_ALL)) {
00212             return -1;
00213          }
00214 
00215          for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
00216             if (!strcasecmp(args.field, lookup[i].name)) {
00217                if (lookup[i].type == INT) {
00218                   snprintf(buf, buflen, "%u", *lookup[i].i4);
00219                } else {
00220                   snprintf(buf, buflen, "%f", *lookup[i].d8);
00221                }
00222                return 0;
00223             }
00224          }
00225          ast_log(LOG_WARNING, "Unrecognized argument '%s' to %s\n", preparse, funcname);
00226          return -1;
00227       }
00228    } else if (!strcasecmp(args.param, "secure_signaling")) {
00229       snprintf(buf, buflen, "%s", p->socket.type == SIP_TRANSPORT_TLS ? "1" : "");
00230    } else if (!strcasecmp(args.param, "secure_media")) {
00231       snprintf(buf, buflen, "%s", p->srtp ? "1" : "");
00232    } else {
00233       res = -1;
00234    }
00235    return res;
00236 }
00237 
00238 #ifdef TEST_FRAMEWORK
00239 static int test_sip_rtpqos_1_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct ast_sockaddr *addr, void *data)
00240 {
00241    /* Needed to pass sanity checks */
00242    ast_rtp_instance_set_data(instance, data);
00243    return 0;
00244 }
00245 
00246 static int test_sip_rtpqos_1_destroy(struct ast_rtp_instance *instance)
00247 {
00248    /* Needed to pass sanity checks */
00249    return 0;
00250 }
00251 
00252 static struct ast_frame *test_sip_rtpqos_1_read(struct ast_rtp_instance *instance, int rtcp)
00253 {
00254    /* Needed to pass sanity checks */
00255    return &ast_null_frame;
00256 }
00257 
00258 static int test_sip_rtpqos_1_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
00259 {
00260    /* Needed to pass sanity checks */
00261    return 0;
00262 }
00263 
00264 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)
00265 {
00266    struct ast_rtp_instance_stats *s = ast_rtp_instance_get_data(instance);
00267    memcpy(stats, s, sizeof(*stats));
00268    return 0;
00269 }
00270 
00271 AST_TEST_DEFINE(test_sip_rtpqos_1)
00272 {
00273    int i, res = AST_TEST_PASS;
00274    struct ast_rtp_engine test_engine = {
00275       .name = "test",
00276       .new = test_sip_rtpqos_1_new,
00277       .destroy = test_sip_rtpqos_1_destroy,
00278       .read = test_sip_rtpqos_1_read,
00279       .write = test_sip_rtpqos_1_write,
00280       .get_stat = test_sip_rtpqos_1_get_stat,
00281    };
00282    struct ast_sockaddr sa = { {0, } };
00283    struct ast_rtp_instance_stats mine = { 0, };
00284    struct sip_pvt *p = NULL;
00285    struct ast_channel *chan = NULL;
00286    struct ast_str *varstr = NULL, *buffer = NULL;
00287    struct {
00288       const char *name;
00289       enum { INT, DBL } type;
00290       union {
00291          unsigned int *i4;
00292          double *d8;
00293       };
00294    } lookup[] = {
00295       { "txcount",               INT, { .i4 = &mine.txcount, }, },
00296       { "rxcount",               INT, { .i4 = &mine.rxcount, }, },
00297       { "txjitter",              DBL, { .d8 = &mine.txjitter, }, },
00298       { "rxjitter",              DBL, { .d8 = &mine.rxjitter, }, },
00299       { "remote_maxjitter",      DBL, { .d8 = &mine.remote_maxjitter, }, },
00300       { "remote_minjitter",      DBL, { .d8 = &mine.remote_minjitter, }, },
00301       { "remote_normdevjitter",  DBL, { .d8 = &mine.remote_normdevjitter, }, },
00302       { "remote_stdevjitter",    DBL, { .d8 = &mine.remote_stdevjitter, }, },
00303       { "local_maxjitter",       DBL, { .d8 = &mine.local_maxjitter, }, },
00304       { "local_minjitter",       DBL, { .d8 = &mine.local_minjitter, }, },
00305       { "local_normdevjitter",   DBL, { .d8 = &mine.local_normdevjitter, }, },
00306       { "local_stdevjitter",     DBL, { .d8 = &mine.local_stdevjitter, }, },
00307       { "txploss",               INT, { .i4 = &mine.txploss, }, },
00308       { "rxploss",               INT, { .i4 = &mine.rxploss, }, },
00309       { "remote_maxrxploss",     DBL, { .d8 = &mine.remote_maxrxploss, }, },
00310       { "remote_minrxploss",     DBL, { .d8 = &mine.remote_minrxploss, }, },
00311       { "remote_normdevrxploss", DBL, { .d8 = &mine.remote_normdevrxploss, }, },
00312       { "remote_stdevrxploss",   DBL, { .d8 = &mine.remote_stdevrxploss, }, },
00313       { "local_maxrxploss",      DBL, { .d8 = &mine.local_maxrxploss, }, },
00314       { "local_minrxploss",      DBL, { .d8 = &mine.local_minrxploss, }, },
00315       { "local_normdevrxploss",  DBL, { .d8 = &mine.local_normdevrxploss, }, },
00316       { "local_stdevrxploss",    DBL, { .d8 = &mine.local_stdevrxploss, }, },
00317       { "rtt",                   DBL, { .d8 = &mine.rtt, }, },
00318       { "maxrtt",                DBL, { .d8 = &mine.maxrtt, }, },
00319       { "minrtt",                DBL, { .d8 = &mine.minrtt, }, },
00320       { "normdevrtt",            DBL, { .d8 = &mine.normdevrtt, }, },
00321       { "stdevrtt",              DBL, { .d8 = &mine.stdevrtt, }, },
00322       { "local_ssrc",            INT, { .i4 = &mine.local_ssrc, }, },
00323       { "remote_ssrc",           INT, { .i4 = &mine.remote_ssrc, }, },
00324       { NULL, },
00325    };
00326 
00327    switch (cmd) {
00328    case TEST_INIT:
00329       info->name = "test_sip_rtpqos";
00330       info->category = "/channels/chan_sip/";
00331       info->summary = "Test retrieval of SIP RTP QOS stats";
00332       info->description =
00333          "Verify values in the RTP instance structure can be accessed through the dialplan.";
00334       return AST_TEST_NOT_RUN;
00335    case TEST_EXECUTE:
00336       break;
00337    }
00338 
00339    ast_rtp_engine_register2(&test_engine, NULL);
00340    /* Have to associate this with a SIP pvt and an ast_channel */
00341    if (!(p = sip_alloc(NULL, NULL, 0, SIP_NOTIFY, NULL))) {
00342       res = AST_TEST_NOT_RUN;
00343       goto done;
00344    }
00345 
00346    if (!(p->rtp = ast_rtp_instance_new("test", sched, &bindaddr, &mine))) {
00347       res = AST_TEST_NOT_RUN;
00348       goto done;
00349    }
00350    ast_rtp_instance_set_remote_address(p->rtp, &sa);
00351    if (!(chan = ast_dummy_channel_alloc())) {
00352       res = AST_TEST_NOT_RUN;
00353       goto done;
00354    }
00355    chan->tech = &sip_tech;
00356    chan->tech_pvt = dialog_ref(p, "Give the owner channel a reference to the dialog");
00357    p->owner = chan;
00358 
00359    varstr = ast_str_create(16);
00360    buffer = ast_str_create(16);
00361    if (!varstr || !buffer) {
00362       res = AST_TEST_NOT_RUN;
00363       goto done;
00364    }
00365 
00366    /* Populate "mine" with values, then retrieve them with the CHANNEL dialplan function */
00367    for (i = 0; !ast_strlen_zero(lookup[i].name); i++) {
00368       ast_str_set(&varstr, 0, "${CHANNEL(rtpqos,audio,%s)}", lookup[i].name);
00369       if (lookup[i].type == INT) {
00370          int j;
00371          char cmpstr[256];
00372          for (j = 1; j < 25; j++) {
00373             *lookup[i].i4 = j;
00374             ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
00375             snprintf(cmpstr, sizeof(cmpstr), "%d", j);
00376             if (strcmp(cmpstr, ast_str_buffer(buffer))) {
00377                res = AST_TEST_FAIL;
00378                ast_test_status_update(test, "%s != %s != %s\n", ast_str_buffer(varstr), cmpstr, ast_str_buffer(buffer));
00379                break;
00380             }
00381          }
00382       } else {
00383          double j, cmpdbl = 0.0;
00384          for (j = 1.0; j < 10.0; j += 0.3) {
00385             *lookup[i].d8 = j;
00386             ast_str_substitute_variables(&buffer, 0, chan, ast_str_buffer(varstr));
00387             if (sscanf(ast_str_buffer(buffer), "%lf", &cmpdbl) != 1 || fabs(j - cmpdbl > .05)) {
00388                res = AST_TEST_FAIL;
00389                ast_test_status_update(test, "%s != %f != %s\n", ast_str_buffer(varstr), j, ast_str_buffer(buffer));
00390                break;
00391             }
00392          }
00393       }
00394    }
00395 
00396 done:
00397    ast_free(varstr);
00398    ast_free(buffer);
00399 
00400    /* This unlink and unref will take care of destroying the channel, RTP instance, and SIP pvt */
00401    if (p) {
00402       dialog_unlink_all(p);
00403       dialog_unref(p, "Destroy test object");
00404    }
00405    ast_rtp_engine_unregister(&test_engine);
00406    return res;
00407 }
00408 #endif
00409 
00410 /*! \brief SIP test registration */
00411 void sip_dialplan_function_register_tests(void)
00412 {
00413    AST_TEST_REGISTER(test_sip_rtpqos_1);
00414 }
00415 
00416 /*! \brief SIP test registration */
00417 void sip_dialplan_function_unregister_tests(void)
00418 {
00419    AST_TEST_UNREGISTER(test_sip_rtpqos_1);
00420 }
00421 

Generated on 7 Aug 2019 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1