res_pjsip_multihomed.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include <pjsip.h>
  25. #include <pjsip_ua.h>
  26. #include "asterisk/res_pjsip.h"
  27. #include "asterisk/module.h"
  28. /*! \brief Local host address for IPv4 */
  29. static char host_ipv4[PJ_INET_ADDRSTRLEN + 2];
  30. /*! \brief Local host address for IPv6 */
  31. static char host_ipv6[PJ_INET6_ADDRSTRLEN + 2];
  32. /*! \brief Helper function which returns a UDP transport bound to the given address and port */
  33. static pjsip_transport *multihomed_get_udp_transport(pj_str_t *address, int port)
  34. {
  35. struct ao2_container *transports = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport",
  36. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  37. struct ast_sip_transport *transport;
  38. struct ao2_iterator iter;
  39. pjsip_transport *sip_transport = NULL;
  40. if (!transports) {
  41. return NULL;
  42. }
  43. for (iter = ao2_iterator_init(transports, 0); (transport = ao2_iterator_next(&iter)); ao2_ref(transport, -1)) {
  44. if ((transport->type != AST_TRANSPORT_UDP) ||
  45. (pj_strcmp(&transport->state->transport->local_name.host, address)) ||
  46. (transport->state->transport->local_name.port != port)) {
  47. continue;
  48. }
  49. sip_transport = transport->state->transport;
  50. ao2_ref(transport, -1);
  51. break;
  52. }
  53. ao2_iterator_destroy(&iter);
  54. ao2_ref(transports, -1);
  55. return sip_transport;
  56. }
  57. /*! \brief Helper function which determines if the address within SDP should be rewritten */
  58. static int multihomed_rewrite_sdp(struct pjmedia_sdp_session *sdp)
  59. {
  60. if (!sdp->conn) {
  61. return 0;
  62. }
  63. /* If the host address is used in the SDP replace it with the address of what this is going out on */
  64. if ((!pj_strcmp2(&sdp->conn->addr_type, "IP4") && !pj_strcmp2(&sdp->conn->addr, host_ipv4)) ||
  65. (!pj_strcmp2(&sdp->conn->addr_type, "IP6") && !pj_strcmp2(&sdp->conn->addr, host_ipv6))) {
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. /*! \brief Helper function which determines if the existing address has priority over new one */
  71. static int multihomed_rewrite_header(pj_str_t *source, pjsip_transport *transport)
  72. {
  73. pj_uint32_t loop6[4] = {0, 0, 0, 0};
  74. /* If the transport is bound to any it should always rewrite */
  75. if ((transport->local_addr.addr.sa_family == pj_AF_INET() &&
  76. transport->local_addr.ipv4.sin_addr.s_addr == PJ_INADDR_ANY) ||
  77. (transport->local_addr.addr.sa_family == pj_AF_INET6() &&
  78. !pj_memcmp(&transport->local_addr.ipv6.sin6_addr, loop6, sizeof(loop6)))) {
  79. return 1;
  80. }
  81. /* If the transport is explicitly bound but the determined source differs favor the transport */
  82. if (!pj_strcmp(source, &transport->local_name.host)) {
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. static pj_status_t multihomed_on_tx_message(pjsip_tx_data *tdata)
  88. {
  89. pjsip_tpmgr_fla2_param prm;
  90. pjsip_transport *transport = NULL;
  91. pjsip_cseq_hdr *cseq;
  92. pjsip_via_hdr *via;
  93. /* Use the destination information to determine what local interface this message will go out on */
  94. pjsip_tpmgr_fla2_param_default(&prm);
  95. prm.tp_type = tdata->tp_info.transport->key.type;
  96. pj_strset2(&prm.dst_host, tdata->tp_info.dst_name);
  97. prm.local_if = PJ_TRUE;
  98. /* If we can't get the local address use best effort and let it pass */
  99. if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), tdata->pool, &prm) != PJ_SUCCESS) {
  100. return PJ_SUCCESS;
  101. }
  102. /* If the transport it is going out on is different reflect it in the message */
  103. if (tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP ||
  104. tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP6) {
  105. transport = multihomed_get_udp_transport(&prm.ret_addr, prm.ret_port);
  106. }
  107. /* If no new transport use the one provided by the message */
  108. if (!transport) {
  109. transport = tdata->tp_info.transport;
  110. }
  111. /* If the message should not be rewritten then abort early */
  112. if (!multihomed_rewrite_header(&prm.ret_addr, transport)) {
  113. return PJ_SUCCESS;
  114. }
  115. /* Update the transport in case it has changed - we do this now in case we don't want to touch the message above */
  116. tdata->tp_info.transport = transport;
  117. /* If the message needs to be updated with new address do so */
  118. if (tdata->msg->type == PJSIP_REQUEST_MSG || !(cseq = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL)) ||
  119. pj_strcmp2(&cseq->method.name, "REGISTER")) {
  120. pjsip_contact_hdr *contact = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, NULL);
  121. if (contact && (PJSIP_URI_SCHEME_IS_SIP(contact->uri) || PJSIP_URI_SCHEME_IS_SIPS(contact->uri))) {
  122. pjsip_sip_uri *uri = pjsip_uri_get_uri(contact->uri);
  123. /* prm.ret_addr is allocated from the tdata pool so it is perfectly fine to just do an assignment like this */
  124. pj_strassign(&uri->host, &prm.ret_addr);
  125. uri->port = prm.ret_port;
  126. pjsip_tx_data_invalidate_msg(tdata);
  127. }
  128. }
  129. if (tdata->msg->type == PJSIP_REQUEST_MSG && (via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL))) {
  130. pj_strassign(&via->sent_by.host, &prm.ret_addr);
  131. via->sent_by.port = prm.ret_port;
  132. pjsip_tx_data_invalidate_msg(tdata);
  133. }
  134. /* Update the SDP if it is present */
  135. if (tdata->msg->body && ast_sip_is_content_type(&tdata->msg->body->content_type, "application", "sdp") &&
  136. multihomed_rewrite_sdp(tdata->msg->body->data)) {
  137. struct pjmedia_sdp_session *sdp = tdata->msg->body->data;
  138. int stream;
  139. pj_strassign(&sdp->conn->addr, &prm.ret_addr);
  140. for (stream = 0; stream < sdp->media_count; ++stream) {
  141. if (sdp->media[stream]->conn) {
  142. pj_strassign(&sdp->media[stream]->conn->addr, &prm.ret_addr);
  143. }
  144. }
  145. pjsip_tx_data_invalidate_msg(tdata);
  146. }
  147. return PJ_SUCCESS;
  148. }
  149. static pjsip_module multihomed_module = {
  150. .name = { "Multihomed Routing", 18 },
  151. .id = -1,
  152. .priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 1,
  153. .on_tx_request = multihomed_on_tx_message,
  154. .on_tx_response = multihomed_on_tx_message,
  155. };
  156. static int unload_module(void)
  157. {
  158. ast_sip_unregister_service(&multihomed_module);
  159. return 0;
  160. }
  161. static int load_module(void)
  162. {
  163. pj_sockaddr addr;
  164. CHECK_PJSIP_MODULE_LOADED();
  165. if (!pj_gethostip(pj_AF_INET(), &addr)) {
  166. pj_sockaddr_print(&addr, host_ipv4, sizeof(host_ipv4), 2);
  167. }
  168. if (!pj_gethostip(pj_AF_INET6(), &addr)) {
  169. pj_sockaddr_print(&addr, host_ipv6, sizeof(host_ipv6), 2);
  170. }
  171. if (ast_sip_register_service(&multihomed_module)) {
  172. ast_log(LOG_ERROR, "Could not register multihomed module for incoming and outgoing requests\n");
  173. return AST_MODULE_LOAD_FAILURE;
  174. }
  175. return AST_MODULE_LOAD_SUCCESS;
  176. }
  177. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Multihomed Routing Support",
  178. .support_level = AST_MODULE_SUPPORT_CORE,
  179. .load = load_module,
  180. .unload = unload_module,
  181. .load_pri = AST_MODPRI_APP_DEPEND,
  182. );