res_pjproject.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Bridge PJPROJECT logging to Asterisk logging.
  21. * \author David M. Lee, II <dlee@digium.com>
  22. *
  23. * PJPROJECT logging doesn't exactly match Asterisk logging, but mapping the two is
  24. * not too bad. PJPROJECT log levels are identified by a single int. Limits are
  25. * not specified by PJPROJECT, but their implementation used 1 through 6.
  26. *
  27. * The mapping is as follows:
  28. * - 0: LOG_ERROR
  29. * - 1: LOG_ERROR
  30. * - 2: LOG_WARNING
  31. * - 3 and above: equivalent to ast_debug(level, ...) for res_pjproject.so
  32. */
  33. /*** MODULEINFO
  34. <depend>pjproject</depend>
  35. <support_level>core</support_level>
  36. ***/
  37. /*** DOCUMENTATION
  38. <configInfo name="res_pjproject" language="en_US">
  39. <synopsis>pjproject common configuration</synopsis>
  40. <configFile name="pjproject.conf">
  41. <configObject name="startup">
  42. <synopsis>Asterisk startup time options for PJPROJECT</synopsis>
  43. <description>
  44. <note><para>The id of this object, as well as its type, must be
  45. 'startup' or it won't be found.</para></note>
  46. </description>
  47. <configOption name="type">
  48. <synopsis>Must be of type 'startup'.</synopsis>
  49. </configOption>
  50. <configOption name="log_level" default="2">
  51. <synopsis>Initial maximum pjproject logging level to log.</synopsis>
  52. <description>
  53. <para>Valid values are: 0-6, and default</para>
  54. <note><para>
  55. This option is needed very early in the startup process
  56. so it can only be read from config files because the
  57. modules for other methods have not been loaded yet.
  58. </para></note>
  59. </description>
  60. </configOption>
  61. </configObject>
  62. <configObject name="log_mappings">
  63. <synopsis>PJPROJECT to Asterisk Log Level Mapping</synopsis>
  64. <description><para>Warnings and errors in the pjproject libraries are generally handled
  65. by Asterisk. In many cases, Asterisk wouldn't even consider them to
  66. be warnings or errors so the messages emitted by pjproject directly
  67. are either superfluous or misleading. The 'log_mappings'
  68. object allows mapping the pjproject levels to Asterisk levels, or nothing.
  69. </para>
  70. <note><para>The id of this object, as well as its type, must be
  71. 'log_mappings' or it won't be found.</para></note>
  72. </description>
  73. <configOption name="type">
  74. <synopsis>Must be of type 'log_mappings'.</synopsis>
  75. </configOption>
  76. <configOption name="asterisk_error" default="0,1">
  77. <synopsis>A comma separated list of pjproject log levels to map to Asterisk LOG_ERROR.</synopsis>
  78. </configOption>
  79. <configOption name="asterisk_warning" default="2">
  80. <synopsis>A comma separated list of pjproject log levels to map to Asterisk LOG_WARNING.</synopsis>
  81. </configOption>
  82. <configOption name="asterisk_notice" default="">
  83. <synopsis>A comma separated list of pjproject log levels to map to Asterisk LOG_NOTICE.</synopsis>
  84. </configOption>
  85. <configOption name="asterisk_debug" default="3,4,5,6">
  86. <synopsis>A comma separated list of pjproject log levels to map to Asterisk LOG_DEBUG.</synopsis>
  87. </configOption>
  88. <configOption name="asterisk_verbose" default="">
  89. <synopsis>A comma separated list of pjproject log levels to map to Asterisk LOG_VERBOSE.</synopsis>
  90. </configOption>
  91. </configObject>
  92. </configFile>
  93. </configInfo>
  94. ***/
  95. #include "asterisk.h"
  96. ASTERISK_REGISTER_FILE()
  97. #include <stdarg.h>
  98. #include <pjlib.h>
  99. #include <pjsip.h>
  100. #include <pj/log.h>
  101. #include "asterisk/options.h"
  102. #include "asterisk/logger.h"
  103. #include "asterisk/module.h"
  104. #include "asterisk/cli.h"
  105. #include "asterisk/res_pjproject.h"
  106. #include "asterisk/vector.h"
  107. #include "asterisk/sorcery.h"
  108. #include "asterisk/test.h"
  109. #include "asterisk/netsock2.h"
  110. static struct ast_sorcery *pjproject_sorcery;
  111. static pj_log_func *log_cb_orig;
  112. static unsigned decor_orig;
  113. static AST_VECTOR(buildopts, char *) buildopts;
  114. /*! Protection from other log intercept instances. There can be only one at a time. */
  115. AST_MUTEX_DEFINE_STATIC(pjproject_log_intercept_lock);
  116. struct pjproject_log_intercept_data {
  117. pthread_t thread;
  118. int fd;
  119. };
  120. static struct pjproject_log_intercept_data pjproject_log_intercept = {
  121. .thread = AST_PTHREADT_NULL,
  122. .fd = -1,
  123. };
  124. struct log_mappings {
  125. /*! Sorcery object details */
  126. SORCERY_OBJECT(details);
  127. /*! These are all comma-separated lists of pjproject log levels */
  128. AST_DECLARE_STRING_FIELDS(
  129. /*! pjproject log levels mapped to Asterisk ERROR */
  130. AST_STRING_FIELD(asterisk_error);
  131. /*! pjproject log levels mapped to Asterisk WARNING */
  132. AST_STRING_FIELD(asterisk_warning);
  133. /*! pjproject log levels mapped to Asterisk NOTICE */
  134. AST_STRING_FIELD(asterisk_notice);
  135. /*! pjproject log levels mapped to Asterisk VERBOSE */
  136. AST_STRING_FIELD(asterisk_verbose);
  137. /*! pjproject log levels mapped to Asterisk DEBUG */
  138. AST_STRING_FIELD(asterisk_debug);
  139. );
  140. };
  141. static struct log_mappings *default_log_mappings;
  142. static struct log_mappings *get_log_mappings(void)
  143. {
  144. struct log_mappings *mappings;
  145. mappings = ast_sorcery_retrieve_by_id(pjproject_sorcery, "log_mappings", "log_mappings");
  146. if (!mappings) {
  147. return ao2_bump(default_log_mappings);
  148. }
  149. return mappings;
  150. }
  151. #define __LOG_SUPPRESS -1
  152. static int get_log_level(int pj_level)
  153. {
  154. int mapped_level;
  155. unsigned char l;
  156. struct log_mappings *mappings;
  157. mappings = get_log_mappings();
  158. if (!mappings) {
  159. return __LOG_ERROR;
  160. }
  161. l = '0' + fmin(pj_level, 9);
  162. if (strchr(mappings->asterisk_error, l)) {
  163. mapped_level = __LOG_ERROR;
  164. } else if (strchr(mappings->asterisk_warning, l)) {
  165. mapped_level = __LOG_WARNING;
  166. } else if (strchr(mappings->asterisk_notice, l)) {
  167. mapped_level = __LOG_NOTICE;
  168. } else if (strchr(mappings->asterisk_verbose, l)) {
  169. mapped_level = __LOG_VERBOSE;
  170. } else if (strchr(mappings->asterisk_debug, l)) {
  171. mapped_level = __LOG_DEBUG;
  172. } else {
  173. mapped_level = __LOG_SUPPRESS;
  174. }
  175. ao2_ref(mappings, -1);
  176. return mapped_level;
  177. }
  178. static void log_forwarder(int level, const char *data, int len)
  179. {
  180. int ast_level;
  181. /* PJPROJECT doesn't provide much in the way of source info */
  182. const char * log_source = "pjproject";
  183. int log_line = 0;
  184. const char *log_func = "<?>";
  185. if (pjproject_log_intercept.fd != -1
  186. && pjproject_log_intercept.thread == pthread_self()) {
  187. /*
  188. * We are handling a CLI command intercepting PJPROJECT
  189. * log output.
  190. */
  191. ast_cli(pjproject_log_intercept.fd, "%s\n", data);
  192. return;
  193. }
  194. ast_level = get_log_level(level);
  195. if (ast_level == __LOG_SUPPRESS) {
  196. return;
  197. }
  198. /* PJPROJECT uses indention to indicate function call depth. We'll prepend
  199. * log statements with a tab so they'll have a better shot at lining
  200. * up */
  201. ast_log(ast_level, log_source, log_line, log_func, "\t%s\n", data);
  202. }
  203. static void capture_buildopts_cb(int level, const char *data, int len)
  204. {
  205. char *dup;
  206. if (strstr(data, "Teluu") || strstr(data, "Dumping")) {
  207. return;
  208. }
  209. dup = ast_strdup(ast_skip_blanks(data));
  210. if (dup && AST_VECTOR_ADD_SORTED(&buildopts, dup, strcmp)) {
  211. ast_free(dup);
  212. }
  213. }
  214. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  215. int ast_pjproject_get_buildopt(char *option, char *format_string, ...)
  216. {
  217. int res = 0;
  218. char *format_temp;
  219. int i;
  220. format_temp = ast_alloca(strlen(option) + strlen(" : ") + strlen(format_string) + 1);
  221. sprintf(format_temp, "%s : %s", option, format_string);
  222. for (i = 0; i < AST_VECTOR_SIZE(&buildopts); i++) {
  223. va_list arg_ptr;
  224. va_start(arg_ptr, format_string);
  225. res = vsscanf(AST_VECTOR_GET(&buildopts, i), format_temp, arg_ptr);
  226. va_end(arg_ptr);
  227. if (res) {
  228. break;
  229. }
  230. }
  231. return res;
  232. }
  233. #pragma GCC diagnostic warning "-Wformat-nonliteral"
  234. void ast_pjproject_log_intercept_begin(int fd)
  235. {
  236. /* Protect from other CLI instances trying to do this at the same time. */
  237. ast_mutex_lock(&pjproject_log_intercept_lock);
  238. pjproject_log_intercept.thread = pthread_self();
  239. pjproject_log_intercept.fd = fd;
  240. }
  241. void ast_pjproject_log_intercept_end(void)
  242. {
  243. pjproject_log_intercept.fd = -1;
  244. pjproject_log_intercept.thread = AST_PTHREADT_NULL;
  245. ast_mutex_unlock(&pjproject_log_intercept_lock);
  246. }
  247. void ast_pjproject_ref(void)
  248. {
  249. ast_module_ref(ast_module_info->self);
  250. }
  251. void ast_pjproject_unref(void)
  252. {
  253. ast_module_unref(ast_module_info->self);
  254. }
  255. static char *handle_pjproject_show_buildopts(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  256. {
  257. int i;
  258. switch (cmd) {
  259. case CLI_INIT:
  260. e->command = "pjproject show buildopts";
  261. e->usage =
  262. "Usage: pjproject show buildopts\n"
  263. " Show the compile time config of the pjproject that Asterisk is\n"
  264. " running against.\n";
  265. return NULL;
  266. case CLI_GENERATE:
  267. return NULL;
  268. }
  269. ast_cli(a->fd, "PJPROJECT compile time config currently running against:\n");
  270. for (i = 0; i < AST_VECTOR_SIZE(&buildopts); i++) {
  271. ast_cli(a->fd, "%s\n", AST_VECTOR_GET(&buildopts, i));
  272. }
  273. return CLI_SUCCESS;
  274. }
  275. static void mapping_destroy(void *object)
  276. {
  277. struct log_mappings *mappings = object;
  278. ast_string_field_free_memory(mappings);
  279. }
  280. static void *mapping_alloc(const char *name)
  281. {
  282. struct log_mappings *mappings = ast_sorcery_generic_alloc(sizeof(*mappings), mapping_destroy);
  283. if (!mappings) {
  284. return NULL;
  285. }
  286. ast_string_field_init(mappings, 128);
  287. return mappings;
  288. }
  289. static char *handle_pjproject_show_log_mappings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  290. {
  291. struct ast_variable *objset;
  292. struct ast_variable *i;
  293. struct log_mappings *mappings;
  294. switch (cmd) {
  295. case CLI_INIT:
  296. e->command = "pjproject show log mappings";
  297. e->usage =
  298. "Usage: pjproject show log mappings\n"
  299. " Show pjproject to Asterisk log mappings\n";
  300. return NULL;
  301. case CLI_GENERATE:
  302. return NULL;
  303. }
  304. ast_cli(a->fd, "PJPROJECT to Asterisk log mappings:\n");
  305. ast_cli(a->fd, "Asterisk Level : PJPROJECT log levels\n");
  306. mappings = get_log_mappings();
  307. if (!mappings) {
  308. ast_log(LOG_ERROR, "Unable to retrieve pjproject log_mappings\n");
  309. return CLI_SUCCESS;
  310. }
  311. objset = ast_sorcery_objectset_create(pjproject_sorcery, mappings);
  312. if (!objset) {
  313. ao2_ref(mappings, -1);
  314. return CLI_SUCCESS;
  315. }
  316. for (i = objset; i; i = i->next) {
  317. ast_cli(a->fd, "%-16s : %s\n", i->name, i->value);
  318. }
  319. ast_variables_destroy(objset);
  320. ao2_ref(mappings, -1);
  321. return CLI_SUCCESS;
  322. }
  323. struct max_pjproject_log_level_check {
  324. /*!
  325. * Compile time sanity check to determine if
  326. * MAX_PJ_LOG_MAX_LEVEL matches CLI syntax.
  327. */
  328. char check[1 / (6 == MAX_PJ_LOG_MAX_LEVEL)];
  329. };
  330. static char *handle_pjproject_set_log_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  331. {
  332. int level_new;
  333. int level_old;
  334. switch (cmd) {
  335. case CLI_INIT:
  336. e->command = "pjproject set log level {default|0|1|2|3|4|5|6}";
  337. e->usage =
  338. "Usage: pjproject set log level {default|<level>}\n"
  339. "\n"
  340. " Set the maximum active pjproject logging level.\n"
  341. " See pjproject.conf.sample for additional information\n"
  342. " about the various levels pjproject uses.\n";
  343. return NULL;
  344. case CLI_GENERATE:
  345. return NULL;
  346. }
  347. if (a->argc != 5) {
  348. return CLI_SHOWUSAGE;
  349. }
  350. if (!strcasecmp(a->argv[4], "default")) {
  351. level_new = DEFAULT_PJ_LOG_MAX_LEVEL;
  352. } else {
  353. if (sscanf(a->argv[4], "%30d", &level_new) != 1
  354. || level_new < 0 || MAX_PJ_LOG_MAX_LEVEL < level_new) {
  355. return CLI_SHOWUSAGE;
  356. }
  357. }
  358. /* Update pjproject logging level */
  359. if (ast_pjproject_max_log_level < level_new) {
  360. level_new = ast_pjproject_max_log_level;
  361. ast_cli(a->fd,
  362. "Asterisk built or linked with pjproject PJ_LOG_MAX_LEVEL=%d.\n"
  363. "Lowering request to the max supported level.\n",
  364. ast_pjproject_max_log_level);
  365. }
  366. level_old = ast_option_pjproject_log_level;
  367. if (level_old == level_new) {
  368. ast_cli(a->fd, "pjproject log level is still %d.\n", level_old);
  369. } else {
  370. ast_cli(a->fd, "pjproject log level was %d and is now %d.\n",
  371. level_old, level_new);
  372. ast_option_pjproject_log_level = level_new;
  373. pj_log_set_level(level_new);
  374. }
  375. return CLI_SUCCESS;
  376. }
  377. static char *handle_pjproject_show_log_level(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  378. {
  379. switch (cmd) {
  380. case CLI_INIT:
  381. e->command = "pjproject show log level";
  382. e->usage =
  383. "Usage: pjproject show log level\n"
  384. "\n"
  385. " Show the current maximum active pjproject logging level.\n"
  386. " See pjproject.conf.sample for additional information\n"
  387. " about the various levels pjproject uses.\n";
  388. return NULL;
  389. case CLI_GENERATE:
  390. return NULL;
  391. }
  392. if (a->argc != 4) {
  393. return CLI_SHOWUSAGE;
  394. }
  395. ast_cli(a->fd, "pjproject log level is %d.%s\n",
  396. ast_option_pjproject_log_level,
  397. ast_option_pjproject_log_level == DEFAULT_PJ_LOG_MAX_LEVEL ? " (default)" : "");
  398. return CLI_SUCCESS;
  399. }
  400. static struct ast_cli_entry pjproject_cli[] = {
  401. AST_CLI_DEFINE(handle_pjproject_set_log_level, "Set the maximum active pjproject logging level"),
  402. AST_CLI_DEFINE(handle_pjproject_show_buildopts, "Show the compiled config of the pjproject in use"),
  403. AST_CLI_DEFINE(handle_pjproject_show_log_mappings, "Show pjproject to Asterisk log mappings"),
  404. AST_CLI_DEFINE(handle_pjproject_show_log_level, "Show the maximum active pjproject logging level"),
  405. };
  406. void ast_pjproject_caching_pool_init(pj_caching_pool *cp,
  407. const pj_pool_factory_policy *policy, pj_size_t max_capacity)
  408. {
  409. /* Passing a max_capacity of zero disables caching pools */
  410. pj_caching_pool_init(cp, policy, ast_option_pjproject_cache_pools ? max_capacity : 0);
  411. }
  412. void ast_pjproject_caching_pool_destroy(pj_caching_pool *cp)
  413. {
  414. pj_caching_pool_destroy(cp);
  415. }
  416. int ast_sockaddr_to_pj_sockaddr(const struct ast_sockaddr *addr, pj_sockaddr *pjaddr)
  417. {
  418. if (addr->ss.ss_family == AF_INET) {
  419. struct sockaddr_in *sin = (struct sockaddr_in *) &addr->ss;
  420. pjaddr->ipv4.sin_family = pj_AF_INET();
  421. #ifdef HAVE_PJPROJECT_BUNDLED
  422. pjaddr->ipv4.sin_addr = sin->sin_addr;
  423. #else
  424. pjaddr->ipv4.sin_addr.s_addr = sin->sin_addr.s_addr;
  425. #endif
  426. pjaddr->ipv4.sin_port = sin->sin_port;
  427. } else if (addr->ss.ss_family == AF_INET6) {
  428. struct sockaddr_in6 *sin = (struct sockaddr_in6 *) &addr->ss;
  429. pjaddr->ipv6.sin6_family = pj_AF_INET6();
  430. pjaddr->ipv6.sin6_port = sin->sin6_port;
  431. pjaddr->ipv6.sin6_flowinfo = sin->sin6_flowinfo;
  432. pjaddr->ipv6.sin6_scope_id = sin->sin6_scope_id;
  433. memcpy(&pjaddr->ipv6.sin6_addr, &sin->sin6_addr, sizeof(pjaddr->ipv6.sin6_addr));
  434. } else {
  435. memset(pjaddr, 0, sizeof(*pjaddr));
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. int ast_sockaddr_from_pj_sockaddr(struct ast_sockaddr *addr, const pj_sockaddr *pjaddr)
  441. {
  442. if (pjaddr->addr.sa_family == pj_AF_INET()) {
  443. struct sockaddr_in *sin = (struct sockaddr_in *) &addr->ss;
  444. sin->sin_family = AF_INET;
  445. #ifdef HAVE_PJPROJECT_BUNDLED
  446. sin->sin_addr = pjaddr->ipv4.sin_addr;
  447. #else
  448. sin->sin_addr.s_addr = pjaddr->ipv4.sin_addr.s_addr;
  449. #endif
  450. sin->sin_port = pjaddr->ipv4.sin_port;
  451. addr->len = sizeof(struct sockaddr_in);
  452. } else if (pjaddr->addr.sa_family == pj_AF_INET6()) {
  453. struct sockaddr_in6 *sin = (struct sockaddr_in6 *) &addr->ss;
  454. sin->sin6_family = AF_INET6;
  455. sin->sin6_port = pjaddr->ipv6.sin6_port;
  456. sin->sin6_flowinfo = pjaddr->ipv6.sin6_flowinfo;
  457. sin->sin6_scope_id = pjaddr->ipv6.sin6_scope_id;
  458. memcpy(&sin->sin6_addr, &pjaddr->ipv6.sin6_addr, sizeof(sin->sin6_addr));
  459. addr->len = sizeof(struct sockaddr_in6);
  460. } else {
  461. memset(addr, 0, sizeof(*addr));
  462. return -1;
  463. }
  464. return 0;
  465. }
  466. #ifdef TEST_FRAMEWORK
  467. static void fill_with_garbage(void *x, ssize_t len)
  468. {
  469. unsigned char *w = x;
  470. while (len > 0) {
  471. int r = ast_random();
  472. memcpy(w, &r, len > sizeof(r) ? sizeof(r) : len);
  473. w += sizeof(r);
  474. len -= sizeof(r);
  475. }
  476. }
  477. AST_TEST_DEFINE(ast_sockaddr_to_pj_sockaddr_test)
  478. {
  479. char *candidates[] = {
  480. "127.0.0.1:5555",
  481. "[::]:4444",
  482. "192.168.0.100:0",
  483. "[fec0::1:80]:0",
  484. "[fec0::1]:80",
  485. NULL,
  486. }, **candidate = candidates;
  487. switch (cmd) {
  488. case TEST_INIT:
  489. info->name = "ast_sockaddr_to_pj_sockaddr_test";
  490. info->category = "/res/res_pjproject/";
  491. info->summary = "Validate conversions from an ast_sockaddr to a pj_sockaddr";
  492. info->description = "This test converts an ast_sockaddr to a pj_sockaddr and validates\n"
  493. "that the two evaluate to the same string when formatted.";
  494. return AST_TEST_NOT_RUN;
  495. case TEST_EXECUTE:
  496. break;
  497. }
  498. while (*candidate) {
  499. struct ast_sockaddr addr = {{0,}};
  500. pj_sockaddr pjaddr;
  501. char buffer[512];
  502. fill_with_garbage(&pjaddr, sizeof(pj_sockaddr));
  503. if (!ast_sockaddr_parse(&addr, *candidate, 0)) {
  504. ast_test_status_update(test, "Failed to parse candidate IP: %s\n", *candidate);
  505. return AST_TEST_FAIL;
  506. }
  507. if (ast_sockaddr_to_pj_sockaddr(&addr, &pjaddr)) {
  508. ast_test_status_update(test, "Failed to convert ast_sockaddr to pj_sockaddr: %s\n", *candidate);
  509. return AST_TEST_FAIL;
  510. }
  511. pj_sockaddr_print(&pjaddr, buffer, sizeof(buffer), 1 | 2);
  512. if (strcmp(*candidate, buffer)) {
  513. ast_test_status_update(test, "Converted sockaddrs do not match: \"%s\" and \"%s\"\n",
  514. *candidate,
  515. buffer);
  516. return AST_TEST_FAIL;
  517. }
  518. candidate++;
  519. }
  520. return AST_TEST_PASS;
  521. }
  522. AST_TEST_DEFINE(ast_sockaddr_from_pj_sockaddr_test)
  523. {
  524. char *candidates[] = {
  525. "127.0.0.1:5555",
  526. "[::]:4444",
  527. "192.168.0.100:0",
  528. "[fec0::1:80]:0",
  529. "[fec0::1]:80",
  530. NULL,
  531. }, **candidate = candidates;
  532. switch (cmd) {
  533. case TEST_INIT:
  534. info->name = "ast_sockaddr_from_pj_sockaddr_test";
  535. info->category = "/res/res_pjproject/";
  536. info->summary = "Validate conversions from a pj_sockaddr to an ast_sockaddr";
  537. info->description = "This test converts a pj_sockaddr to an ast_sockaddr and validates\n"
  538. "that the two evaluate to the same string when formatted.";
  539. return AST_TEST_NOT_RUN;
  540. case TEST_EXECUTE:
  541. break;
  542. }
  543. while (*candidate) {
  544. struct ast_sockaddr addr = {{0,}};
  545. pj_sockaddr pjaddr;
  546. pj_str_t t;
  547. char buffer[512];
  548. fill_with_garbage(&addr, sizeof(addr));
  549. pj_strset(&t, *candidate, strlen(*candidate));
  550. if (pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &t, &pjaddr) != PJ_SUCCESS) {
  551. ast_test_status_update(test, "Failed to parse candidate IP: %s\n", *candidate);
  552. return AST_TEST_FAIL;
  553. }
  554. if (ast_sockaddr_from_pj_sockaddr(&addr, &pjaddr)) {
  555. ast_test_status_update(test, "Failed to convert pj_sockaddr to ast_sockaddr: %s\n", *candidate);
  556. return AST_TEST_FAIL;
  557. }
  558. snprintf(buffer, sizeof(buffer), "%s", ast_sockaddr_stringify(&addr));
  559. if (strcmp(*candidate, buffer)) {
  560. ast_test_status_update(test, "Converted sockaddrs do not match: \"%s\" and \"%s\"\n",
  561. *candidate,
  562. buffer);
  563. return AST_TEST_FAIL;
  564. }
  565. candidate++;
  566. }
  567. return AST_TEST_PASS;
  568. }
  569. #endif
  570. static int load_module(void)
  571. {
  572. ast_debug(3, "Starting PJPROJECT logging to Asterisk logger\n");
  573. if (!(pjproject_sorcery = ast_sorcery_open())) {
  574. ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
  575. return AST_MODULE_LOAD_DECLINE;
  576. }
  577. ast_sorcery_apply_default(pjproject_sorcery, "log_mappings", "config", "pjproject.conf,criteria=type=log_mappings");
  578. if (ast_sorcery_object_register(pjproject_sorcery, "log_mappings", mapping_alloc, NULL, NULL)) {
  579. ast_log(LOG_WARNING, "Failed to register pjproject log_mappings object with sorcery\n");
  580. ast_sorcery_unref(pjproject_sorcery);
  581. pjproject_sorcery = NULL;
  582. return AST_MODULE_LOAD_DECLINE;
  583. }
  584. ast_sorcery_object_field_register(pjproject_sorcery, "log_mappings", "type", "", OPT_NOOP_T, 0, 0);
  585. ast_sorcery_object_field_register(pjproject_sorcery, "log_mappings", "asterisk_debug", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct log_mappings, asterisk_debug));
  586. ast_sorcery_object_field_register(pjproject_sorcery, "log_mappings", "asterisk_error", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct log_mappings, asterisk_error));
  587. ast_sorcery_object_field_register(pjproject_sorcery, "log_mappings", "asterisk_warning", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct log_mappings, asterisk_warning));
  588. ast_sorcery_object_field_register(pjproject_sorcery, "log_mappings", "asterisk_notice", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct log_mappings, asterisk_notice));
  589. ast_sorcery_object_field_register(pjproject_sorcery, "log_mappings", "asterisk_verbose", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct log_mappings, asterisk_verbose));
  590. default_log_mappings = ast_sorcery_alloc(pjproject_sorcery, "log_mappings", "log_mappings");
  591. if (!default_log_mappings) {
  592. ast_log(LOG_ERROR, "Unable to allocate memory for pjproject log_mappings\n");
  593. return AST_MODULE_LOAD_DECLINE;
  594. }
  595. ast_string_field_set(default_log_mappings, asterisk_error, "0,1");
  596. ast_string_field_set(default_log_mappings, asterisk_warning, "2");
  597. ast_string_field_set(default_log_mappings, asterisk_debug, "3,4,5,6");
  598. ast_sorcery_load(pjproject_sorcery);
  599. AST_PJPROJECT_INIT_LOG_LEVEL();
  600. pj_init();
  601. decor_orig = pj_log_get_decor();
  602. log_cb_orig = pj_log_get_log_func();
  603. if (AST_VECTOR_INIT(&buildopts, 64)) {
  604. return AST_MODULE_LOAD_DECLINE;
  605. }
  606. /*
  607. * On startup, we want to capture the dump once and store it.
  608. */
  609. pj_log_set_log_func(capture_buildopts_cb);
  610. pj_log_set_decor(0);
  611. pj_log_set_level(MAX_PJ_LOG_MAX_LEVEL);/* Set level to guarantee the dump output. */
  612. pj_dump_config();
  613. pj_log_set_decor(PJ_LOG_HAS_SENDER | PJ_LOG_HAS_INDENT);
  614. pj_log_set_log_func(log_forwarder);
  615. if (ast_pjproject_max_log_level < ast_option_pjproject_log_level) {
  616. ast_log(LOG_WARNING,
  617. "Asterisk built or linked with pjproject PJ_LOG_MAX_LEVEL=%d which is too low for startup level: %d.\n",
  618. ast_pjproject_max_log_level, ast_option_pjproject_log_level);
  619. ast_option_pjproject_log_level = ast_pjproject_max_log_level;
  620. }
  621. pj_log_set_level(ast_option_pjproject_log_level);
  622. if (!AST_VECTOR_SIZE(&buildopts)) {
  623. ast_log(LOG_NOTICE,
  624. "Asterisk built or linked with pjproject PJ_LOG_MAX_LEVEL=%d which is too low to get buildopts.\n",
  625. ast_pjproject_max_log_level);
  626. }
  627. ast_cli_register_multiple(pjproject_cli, ARRAY_LEN(pjproject_cli));
  628. AST_TEST_REGISTER(ast_sockaddr_to_pj_sockaddr_test);
  629. AST_TEST_REGISTER(ast_sockaddr_from_pj_sockaddr_test);
  630. return AST_MODULE_LOAD_SUCCESS;
  631. }
  632. #define NOT_EQUALS(a, b) (a != b)
  633. static int unload_module(void)
  634. {
  635. ast_cli_unregister_multiple(pjproject_cli, ARRAY_LEN(pjproject_cli));
  636. pj_log_set_log_func(log_cb_orig);
  637. pj_log_set_decor(decor_orig);
  638. AST_VECTOR_CALLBACK_VOID(&buildopts, ast_free);
  639. AST_VECTOR_FREE(&buildopts);
  640. ast_debug(3, "Stopped PJPROJECT logging to Asterisk logger\n");
  641. pj_shutdown();
  642. ao2_cleanup(default_log_mappings);
  643. default_log_mappings = NULL;
  644. ast_sorcery_unref(pjproject_sorcery);
  645. AST_TEST_UNREGISTER(ast_sockaddr_to_pj_sockaddr_test);
  646. AST_TEST_UNREGISTER(ast_sockaddr_from_pj_sockaddr_test);
  647. return 0;
  648. }
  649. static int reload_module(void)
  650. {
  651. if (pjproject_sorcery) {
  652. ast_sorcery_reload(pjproject_sorcery);
  653. }
  654. return AST_MODULE_LOAD_SUCCESS;
  655. }
  656. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJPROJECT Log and Utility Support",
  657. .support_level = AST_MODULE_SUPPORT_CORE,
  658. .load = load_module,
  659. .unload = unload_module,
  660. .reload = reload_module,
  661. .load_pri = AST_MODPRI_CHANNEL_DEPEND - 6,
  662. );