dialplan_functions.c 14 KB

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