res_pjsip_endpoint_identifier_anonymous.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_in_use(void *obj, void *arg, int flags)
  40. {
  41. struct ast_sip_transport *transport = obj;
  42. pjsip_rx_data *rdata = arg;
  43. if ((transport->state->transport == rdata->tp_info.transport) ||
  44. (transport->state->factory && !pj_strcmp(&transport->state->factory->addr_name.host, &rdata->tp_info.transport->local_name.host) &&
  45. transport->state->factory->addr_name.port == rdata->tp_info.transport->local_name.port)) {
  46. return CMP_MATCH | CMP_STOP;
  47. }
  48. return 0;
  49. }
  50. static struct ast_sip_endpoint *anonymous_identify(pjsip_rx_data *rdata)
  51. {
  52. char domain_name[64], id[AST_UUID_STR_LEN];
  53. struct ast_sip_endpoint *endpoint;
  54. RAII_VAR(struct ast_sip_domain_alias *, alias, NULL, ao2_cleanup);
  55. RAII_VAR(struct ao2_container *, transports, NULL, ao2_cleanup);
  56. RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
  57. if (get_endpoint_details(rdata, domain_name, sizeof(domain_name))) {
  58. return NULL;
  59. }
  60. /* Attempt to find the endpoint given the name and domain provided */
  61. snprintf(id, sizeof(id), "anonymous@%s", domain_name);
  62. if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
  63. goto done;
  64. }
  65. /* See if an alias exists for the domain provided */
  66. if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
  67. snprintf(id, sizeof(id), "anonymous@%s", alias->domain);
  68. if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
  69. goto done;
  70. }
  71. }
  72. /* See if the transport this came in on has a provided domain */
  73. if ((transports = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL)) &&
  74. (transport = ao2_callback(transports, 0, find_transport_in_use, rdata)) &&
  75. !ast_strlen_zero(transport->domain)) {
  76. snprintf(id, sizeof(id), "anonymous@%s", transport->domain);
  77. if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
  78. goto done;
  79. }
  80. }
  81. /* Fall back to no domain */
  82. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", "anonymous");
  83. done:
  84. if (endpoint) {
  85. ast_debug(3, "Retrieved anonymous endpoint '%s'\n", ast_sorcery_object_get_id(endpoint));
  86. }
  87. return endpoint;
  88. }
  89. static struct ast_sip_endpoint_identifier anonymous_identifier = {
  90. .identify_endpoint = anonymous_identify,
  91. };
  92. static int load_module(void)
  93. {
  94. CHECK_PJSIP_MODULE_LOADED();
  95. ast_sip_register_endpoint_identifier(&anonymous_identifier);
  96. return AST_MODULE_LOAD_SUCCESS;
  97. }
  98. static int unload_module(void)
  99. {
  100. ast_sip_unregister_endpoint_identifier(&anonymous_identifier);
  101. return 0;
  102. }
  103. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Anonymous endpoint identifier",
  104. .support_level = AST_MODULE_SUPPORT_CORE,
  105. .load = load_module,
  106. .unload = unload_module,
  107. .load_pri = AST_MODPRI_DEFAULT,
  108. );