res_pjsip_endpoint_identifier_anonymous.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@digium.com>
  7. * Joshua Colp <jcolp@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*** MODULEINFO
  20. <depend>pjproject</depend>
  21. <depend>res_pjsip</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. #include "asterisk.h"
  25. #include <pjsip.h>
  26. #include "asterisk/res_pjsip.h"
  27. #include "asterisk/module.h"
  28. static int get_endpoint_details(pjsip_rx_data *rdata, char *domain, size_t domain_size)
  29. {
  30. pjsip_uri *from = rdata->msg_info.from->uri;
  31. pjsip_sip_uri *sip_from;
  32. if (!PJSIP_URI_SCHEME_IS_SIP(from) && !PJSIP_URI_SCHEME_IS_SIPS(from)) {
  33. return -1;
  34. }
  35. sip_from = (pjsip_sip_uri *) pjsip_uri_get_uri(from);
  36. ast_copy_pj_str(domain, &sip_from->host, domain_size);
  37. return 0;
  38. }
  39. static int find_transport_state_in_use(void *obj, void *arg, int flags)
  40. {
  41. struct ast_sip_transport_state *transport_state = obj;
  42. pjsip_rx_data *rdata = arg;
  43. if (transport_state->transport == rdata->tp_info.transport
  44. || (transport_state->factory
  45. && !pj_strcmp(&transport_state->factory->addr_name.host, &rdata->tp_info.transport->local_name.host)
  46. && transport_state->factory->addr_name.port == rdata->tp_info.transport->local_name.port)) {
  47. return CMP_MATCH;
  48. }
  49. return 0;
  50. }
  51. #define DOMAIN_NAME_LEN 255
  52. static struct ast_sip_endpoint *anonymous_identify(pjsip_rx_data *rdata)
  53. {
  54. char domain_name[DOMAIN_NAME_LEN + 1];
  55. struct ast_sip_endpoint *endpoint;
  56. if (get_endpoint_details(rdata, domain_name, sizeof(domain_name))) {
  57. return NULL;
  58. }
  59. if (!ast_sip_get_disable_multi_domain()) {
  60. struct ast_sip_domain_alias *alias;
  61. struct ao2_container *transport_states;
  62. struct ast_sip_transport_state *transport_state = NULL;
  63. struct ast_sip_transport *transport = NULL;
  64. char id[sizeof("anonymous@") + DOMAIN_NAME_LEN];
  65. /* Attempt to find the endpoint given the name and domain provided */
  66. snprintf(id, sizeof(id), "anonymous@%s", domain_name);
  67. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
  68. if (endpoint) {
  69. goto done;
  70. }
  71. /* See if an alias exists for the domain provided */
  72. alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias",
  73. domain_name);
  74. if (alias) {
  75. snprintf(id, sizeof(id), "anonymous@%s", alias->domain);
  76. ao2_ref(alias, -1);
  77. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
  78. if (endpoint) {
  79. goto done;
  80. }
  81. }
  82. /* See if the transport this came in on has a provided domain */
  83. if ((transport_states = ast_sip_get_transport_states())
  84. && (transport_state = ao2_callback(transport_states, 0, find_transport_state_in_use, rdata))
  85. && (transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_state->id))
  86. && !ast_strlen_zero(transport->domain)) {
  87. snprintf(id, sizeof(id), "anonymous@%s", transport->domain);
  88. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
  89. }
  90. ao2_cleanup(transport);
  91. ao2_cleanup(transport_state);
  92. ao2_cleanup(transport_states);
  93. if (endpoint) {
  94. goto done;
  95. }
  96. }
  97. /* Fall back to no domain */
  98. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", "anonymous");
  99. done:
  100. if (endpoint) {
  101. ast_debug(3, "Retrieved anonymous endpoint '%s'\n", ast_sorcery_object_get_id(endpoint));
  102. }
  103. return endpoint;
  104. }
  105. static struct ast_sip_endpoint_identifier anonymous_identifier = {
  106. .identify_endpoint = anonymous_identify,
  107. };
  108. static int load_module(void)
  109. {
  110. CHECK_PJSIP_MODULE_LOADED();
  111. ast_sip_register_endpoint_identifier_with_name(&anonymous_identifier, "anonymous");
  112. return AST_MODULE_LOAD_SUCCESS;
  113. }
  114. static int unload_module(void)
  115. {
  116. ast_sip_unregister_endpoint_identifier(&anonymous_identifier);
  117. return 0;
  118. }
  119. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PJSIP Anonymous endpoint identifier",
  120. .support_level = AST_MODULE_SUPPORT_CORE,
  121. .load = load_module,
  122. .unload = unload_module,
  123. );