config_system.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@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. #include "asterisk.h"
  19. #include <pjsip.h>
  20. #include <pjlib.h>
  21. #include "asterisk/res_pjsip.h"
  22. #include "asterisk/sorcery.h"
  23. #include "include/res_pjsip_private.h"
  24. #include "asterisk/threadpool.h"
  25. #include "asterisk/dns.h"
  26. #include "asterisk/res_pjsip_cli.h"
  27. #define TIMER_T1_MIN 100
  28. #define DEFAULT_TIMER_T1 500
  29. #define DEFAULT_TIMER_B 32000
  30. struct system_config {
  31. SORCERY_OBJECT(details);
  32. /*! Transaction Timer T1 value */
  33. unsigned int timert1;
  34. /*! Transaction Timer B value */
  35. unsigned int timerb;
  36. /*! Should we use short forms for headers? */
  37. unsigned int compactheaders;
  38. struct {
  39. /*! Initial number of threads in the threadpool */
  40. int initial_size;
  41. /*! The amount by which the number of threads is incremented when necessary */
  42. int auto_increment;
  43. /*! Thread idle timeout in seconds */
  44. int idle_timeout;
  45. /*! Maxumum number of threads in the threadpool */
  46. int max_size;
  47. } threadpool;
  48. /*! Nonzero to disable switching from UDP to TCP transport */
  49. unsigned int disable_tcp_switch;
  50. /*!
  51. * Although early media is enabled in pjproject by default, it's only
  52. * enabled when the To tags are different. These options allow turning
  53. * on or off the feature for different tags and same tags.
  54. */
  55. unsigned int follow_early_media_fork;
  56. unsigned int accept_multiple_sdp_answers;
  57. };
  58. static struct ast_threadpool_options sip_threadpool_options = {
  59. .version = AST_THREADPOOL_OPTIONS_VERSION,
  60. };
  61. void sip_get_threadpool_options(struct ast_threadpool_options *threadpool_options)
  62. {
  63. *threadpool_options = sip_threadpool_options;
  64. }
  65. static struct ast_sorcery *system_sorcery;
  66. static void *system_alloc(const char *name)
  67. {
  68. struct system_config *system = ast_sorcery_generic_alloc(sizeof(*system), NULL);
  69. if (!system) {
  70. return NULL;
  71. }
  72. return system;
  73. }
  74. static int system_apply(const struct ast_sorcery *system_sorcery, void *obj)
  75. {
  76. struct system_config *system = obj;
  77. int min_timerb;
  78. if (system->timert1 < TIMER_T1_MIN) {
  79. ast_log(LOG_WARNING, "Timer T1 setting is too low. Setting to %d\n", TIMER_T1_MIN);
  80. system->timert1 = TIMER_T1_MIN;
  81. }
  82. min_timerb = 64 * system->timert1;
  83. if (system->timerb < min_timerb) {
  84. ast_log(LOG_WARNING, "Timer B setting is too low. Setting to %d\n", min_timerb);
  85. system->timerb = min_timerb;
  86. }
  87. pjsip_cfg()->tsx.t1 = system->timert1;
  88. pjsip_cfg()->tsx.td = system->timerb;
  89. pjsip_cfg()->endpt.follow_early_media_fork = system->follow_early_media_fork;
  90. #ifdef HAVE_PJSIP_INV_ACCEPT_MULTIPLE_SDP_ANSWERS
  91. pjsip_cfg()->endpt.accept_multiple_sdp_answers = system->accept_multiple_sdp_answers;
  92. #else
  93. if (system->accept_multiple_sdp_answers) {
  94. ast_log(LOG_WARNING,
  95. "The accept_multiple_sdp_answers flag is not supported in this version of pjproject. Ignoring\n");
  96. }
  97. #endif
  98. if (system->compactheaders) {
  99. #ifdef HAVE_PJSIP_ENDPOINT_COMPACT_FORM
  100. pjsip_cfg()->endpt.use_compact_form = PJ_TRUE;
  101. #else
  102. extern pj_bool_t pjsip_use_compact_form;
  103. pjsip_use_compact_form = PJ_TRUE;
  104. #endif
  105. }
  106. sip_threadpool_options.initial_size = system->threadpool.initial_size;
  107. sip_threadpool_options.auto_increment = system->threadpool.auto_increment;
  108. sip_threadpool_options.idle_timeout = system->threadpool.idle_timeout;
  109. sip_threadpool_options.max_size = system->threadpool.max_size;
  110. pjsip_cfg()->endpt.disable_tcp_switch =
  111. system->disable_tcp_switch ? PJ_TRUE : PJ_FALSE;
  112. return 0;
  113. }
  114. static struct system_config *get_system_cfg(void)
  115. {
  116. struct system_config *cfg;
  117. struct ao2_container *systems;
  118. systems = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
  119. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  120. if (!systems) {
  121. return NULL;
  122. }
  123. cfg = ao2_find(systems, NULL, 0);
  124. ao2_ref(systems, -1);
  125. return cfg;
  126. }
  127. int sip_cli_print_system(struct ast_sip_cli_context *context)
  128. {
  129. struct system_config *cfg = get_system_cfg();
  130. if (!cfg) {
  131. cfg = ast_sorcery_alloc(system_sorcery, "system", NULL);
  132. if (!cfg) {
  133. return -1;
  134. }
  135. }
  136. ast_str_append(&context->output_buffer, 0, "\nSystem Settings:\n\n");
  137. ast_sip_cli_print_sorcery_objectset(cfg, context, 0);
  138. ao2_ref(cfg, -1);
  139. return 0;
  140. }
  141. int ast_sip_initialize_system(void)
  142. {
  143. RAII_VAR(struct ao2_container *, system_configs, NULL, ao2_cleanup);
  144. RAII_VAR(struct system_config *, system, NULL, ao2_cleanup);
  145. system_sorcery = ast_sorcery_open();
  146. if (!system_sorcery) {
  147. ast_log(LOG_ERROR, "Failed to open SIP system sorcery\n");
  148. return -1;
  149. }
  150. ast_sorcery_apply_default(system_sorcery, "system", "config", "pjsip.conf,criteria=type=system,single_object=yes,explicit_name=system");
  151. if (ast_sorcery_object_register_no_reload(system_sorcery, "system", system_alloc, NULL, system_apply)) {
  152. ast_log(LOG_ERROR, "Failed to register with sorcery (is res_sorcery_config loaded?)\n");
  153. ast_sorcery_unref(system_sorcery);
  154. system_sorcery = NULL;
  155. return -1;
  156. }
  157. ast_sorcery_object_field_register(system_sorcery, "system", "type", "", OPT_NOOP_T, 0, 0);
  158. ast_sorcery_object_field_register(system_sorcery, "system", "timer_t1", __stringify(DEFAULT_TIMER_T1),
  159. OPT_UINT_T, 0, FLDSET(struct system_config, timert1));
  160. ast_sorcery_object_field_register(system_sorcery, "system", "timer_b", __stringify(DEFAULT_TIMER_B),
  161. OPT_UINT_T, 0, FLDSET(struct system_config, timerb));
  162. ast_sorcery_object_field_register(system_sorcery, "system", "compact_headers", "no",
  163. OPT_BOOL_T, 1, FLDSET(struct system_config, compactheaders));
  164. ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_initial_size", "0",
  165. OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.initial_size));
  166. ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_auto_increment", "5",
  167. OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.auto_increment));
  168. ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_idle_timeout", "60",
  169. OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.idle_timeout));
  170. ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_max_size", "50",
  171. OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.max_size));
  172. ast_sorcery_object_field_register(system_sorcery, "system", "disable_tcp_switch", "yes",
  173. OPT_BOOL_T, 1, FLDSET(struct system_config, disable_tcp_switch));
  174. ast_sorcery_object_field_register(system_sorcery, "system", "follow_early_media_fork", "yes",
  175. OPT_BOOL_T, 1, FLDSET(struct system_config, follow_early_media_fork));
  176. ast_sorcery_object_field_register(system_sorcery, "system", "accept_multiple_sdp_answers", "no",
  177. OPT_BOOL_T, 1, FLDSET(struct system_config, accept_multiple_sdp_answers));
  178. ast_sorcery_load(system_sorcery);
  179. system_configs = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
  180. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  181. if (ao2_container_count(system_configs)) {
  182. return 0;
  183. }
  184. /* No config present, allocate one and apply defaults */
  185. system = ast_sorcery_alloc(system_sorcery, "system", NULL);
  186. if (!system) {
  187. ast_log(LOG_ERROR, "Unable to allocate default system config.\n");
  188. ast_sorcery_unref(system_sorcery);
  189. return -1;
  190. }
  191. if (system_apply(system_sorcery, system)) {
  192. ast_log(LOG_ERROR, "Failed to apply default system config.\n");
  193. ast_sorcery_unref(system_sorcery);
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. void ast_sip_destroy_system(void)
  199. {
  200. ast_sorcery_unref(system_sorcery);
  201. }
  202. static int system_create_resolver_and_set_nameservers(void *data)
  203. {
  204. struct ao2_container *discovered_nameservers;
  205. struct ao2_iterator it_nameservers;
  206. char *nameserver;
  207. pj_status_t status;
  208. pj_dns_resolver *resolver;
  209. pj_str_t nameservers[PJ_DNS_RESOLVER_MAX_NS];
  210. unsigned int count = 0;
  211. discovered_nameservers = ast_dns_get_nameservers();
  212. if (!discovered_nameservers) {
  213. ast_log(LOG_ERROR, "Could not retrieve local system nameservers, resorting to system resolution\n");
  214. return 0;
  215. }
  216. if (!ao2_container_count(discovered_nameservers)) {
  217. ast_log(LOG_ERROR, "There are no local system nameservers configured, resorting to system resolution\n");
  218. ao2_ref(discovered_nameservers, -1);
  219. return -1;
  220. }
  221. if (!(resolver = pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint()))) {
  222. status = pjsip_endpt_create_resolver(ast_sip_get_pjsip_endpoint(), &resolver);
  223. if (status != PJ_SUCCESS) {
  224. ast_log(LOG_ERROR, "Could not create DNS resolver(%d), resorting to system resolution\n", status);
  225. ao2_ref(discovered_nameservers, -1);
  226. return 0;
  227. }
  228. }
  229. it_nameservers = ao2_iterator_init(discovered_nameservers, 0);
  230. while ((nameserver = ao2_iterator_next(&it_nameservers))) {
  231. pj_strset2(&nameservers[count++], nameserver);
  232. ao2_ref(nameserver, -1);
  233. if (count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
  234. break;
  235. }
  236. }
  237. ao2_iterator_destroy(&it_nameservers);
  238. status = pj_dns_resolver_set_ns(resolver, count, nameservers, NULL);
  239. /* Since we no longer need the nameservers we can drop the list of them */
  240. ao2_ref(discovered_nameservers, -1);
  241. if (status != PJ_SUCCESS) {
  242. ast_log(LOG_ERROR, "Could not set nameservers on DNS resolver in PJSIP(%d), resorting to system resolution\n",
  243. status);
  244. return 0;
  245. }
  246. if (!pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint())) {
  247. status = pjsip_endpt_set_resolver(ast_sip_get_pjsip_endpoint(), resolver);
  248. if (status != PJ_SUCCESS) {
  249. ast_log(LOG_ERROR, "Could not set DNS resolver in PJSIP(%d), resorting to system resolution\n", status);
  250. return 0;
  251. }
  252. }
  253. return 0;
  254. }
  255. void ast_sip_initialize_dns(void)
  256. {
  257. ast_sip_push_task_wait_servant(NULL, system_create_resolver_and_set_nameservers, NULL);
  258. }