config_global.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "include/res_pjsip_private.h"
  23. #include "asterisk/sorcery.h"
  24. #include "asterisk/ast_version.h"
  25. #define DEFAULT_MAX_FORWARDS 70
  26. #define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
  27. #define DEFAULT_OUTBOUND_ENDPOINT "default_outbound_endpoint"
  28. static char default_useragent[128];
  29. struct global_config {
  30. SORCERY_OBJECT(details);
  31. AST_DECLARE_STRING_FIELDS(
  32. AST_STRING_FIELD(useragent);
  33. AST_STRING_FIELD(default_outbound_endpoint);
  34. /*! Debug logging yes|no|host */
  35. AST_STRING_FIELD(debug);
  36. );
  37. /* Value to put in Max-Forwards header */
  38. unsigned int max_forwards;
  39. };
  40. static void global_destructor(void *obj)
  41. {
  42. struct global_config *cfg = obj;
  43. ast_string_field_free_memory(cfg);
  44. }
  45. static void *global_alloc(const char *name)
  46. {
  47. struct global_config *cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
  48. if (!cfg || ast_string_field_init(cfg, 80)) {
  49. return NULL;
  50. }
  51. return cfg;
  52. }
  53. static int global_apply(const struct ast_sorcery *sorcery, void *obj)
  54. {
  55. struct global_config *cfg = obj;
  56. char max_forwards[10];
  57. snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
  58. ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
  59. ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
  60. ast_sip_add_global_response_header("Server", cfg->useragent, 1);
  61. return 0;
  62. }
  63. static struct global_config *get_global_cfg(void)
  64. {
  65. RAII_VAR(struct ao2_container *, globals, ast_sorcery_retrieve_by_fields(
  66. ast_sip_get_sorcery(), "global", AST_RETRIEVE_FLAG_MULTIPLE,
  67. NULL), ao2_cleanup);
  68. if (!globals) {
  69. return NULL;
  70. }
  71. return ao2_find(globals, NULL, 0);
  72. }
  73. char *ast_sip_global_default_outbound_endpoint(void)
  74. {
  75. RAII_VAR(struct global_config *, cfg, get_global_cfg(), ao2_cleanup);
  76. if (!cfg) {
  77. return NULL;
  78. }
  79. return ast_strdup(cfg->default_outbound_endpoint);
  80. }
  81. char *ast_sip_get_debug(void)
  82. {
  83. char *res;
  84. struct global_config *cfg = get_global_cfg();
  85. if (!cfg) {
  86. return ast_strdup("no");
  87. }
  88. res = ast_strdup(cfg->debug);
  89. ao2_ref(cfg, -1);
  90. return res;
  91. }
  92. int ast_sip_initialize_sorcery_global(void)
  93. {
  94. struct ast_sorcery *sorcery = ast_sip_get_sorcery();
  95. snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version());
  96. ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
  97. if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
  98. return -1;
  99. }
  100. ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
  101. ast_sorcery_object_field_register(sorcery, "global", "max_forwards", __stringify(DEFAULT_MAX_FORWARDS),
  102. OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
  103. ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
  104. OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
  105. ast_sorcery_object_field_register(sorcery, "global", "default_outbound_endpoint", DEFAULT_OUTBOUND_ENDPOINT,
  106. OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
  107. ast_sorcery_object_field_register(sorcery, "global", "debug", "no",
  108. OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, debug));
  109. return 0;
  110. }