res_pjsip_multihomed.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 a transport is bound to any */
  71. static int multihomed_bound_any(pjsip_transport *transport)
  72. {
  73. pj_uint32_t loop6[4] = {0, 0, 0, 0};
  74. if ((transport->local_addr.addr.sa_family == pj_AF_INET() &&
  75. transport->local_addr.ipv4.sin_addr.s_addr == PJ_INADDR_ANY) ||
  76. (transport->local_addr.addr.sa_family == pj_AF_INET6() &&
  77. !pj_memcmp(&transport->local_addr.ipv6.sin6_addr, loop6, sizeof(loop6)))) {
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static pj_status_t multihomed_on_tx_message(pjsip_tx_data *tdata)
  83. {
  84. pjsip_tpmgr_fla2_param prm;
  85. pjsip_cseq_hdr *cseq;
  86. pjsip_via_hdr *via;
  87. /* Use the destination information to determine what local interface this message will go out on */
  88. pjsip_tpmgr_fla2_param_default(&prm);
  89. prm.tp_type = tdata->tp_info.transport->key.type;
  90. pj_strset2(&prm.dst_host, tdata->tp_info.dst_name);
  91. prm.local_if = PJ_TRUE;
  92. /* If we can't get the local address use best effort and let it pass */
  93. if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), tdata->pool, &prm) != PJ_SUCCESS) {
  94. return PJ_SUCCESS;
  95. }
  96. /* The port in the message should always be that of the original transport */
  97. prm.ret_port = tdata->tp_info.transport->local_name.port;
  98. /* If the IP source differs from the existing transport see if we need to update it */
  99. if (pj_strcmp(&prm.ret_addr, &tdata->tp_info.transport->local_name.host)) {
  100. /* If the transport it is going out on is different reflect it in the message */
  101. if (tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP ||
  102. tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP6) {
  103. pjsip_transport *transport;
  104. transport = multihomed_get_udp_transport(&prm.ret_addr, prm.ret_port);
  105. if (transport) {
  106. tdata->tp_info.transport = transport;
  107. }
  108. }
  109. /* If the chosen transport is not bound to any we can't use the source address as it won't get back to us */
  110. if (!multihomed_bound_any(tdata->tp_info.transport)) {
  111. pj_strassign(&prm.ret_addr, &tdata->tp_info.transport->local_name.host);
  112. }
  113. } else {
  114. /* The transport chosen will deliver this but ensure it is updated with the right information */
  115. pj_strassign(&prm.ret_addr, &tdata->tp_info.transport->local_name.host);
  116. }
  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. && !(tdata->msg->type == PJSIP_RESPONSE_MSG && tdata->msg->line.status.code / 100 == 3)) {
  123. pjsip_sip_uri *uri = pjsip_uri_get_uri(contact->uri);
  124. /* prm.ret_addr is allocated from the tdata pool OR the transport so it is perfectly fine to just do an assignment like this */
  125. pj_strassign(&uri->host, &prm.ret_addr);
  126. uri->port = prm.ret_port;
  127. ast_debug(4, "Re-wrote Contact URI host/port to %.*s:%d\n",
  128. (int)pj_strlen(&uri->host), pj_strbuf(&uri->host), uri->port);
  129. pjsip_tx_data_invalidate_msg(tdata);
  130. }
  131. }
  132. if (tdata->msg->type == PJSIP_REQUEST_MSG && (via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL))) {
  133. pj_strassign(&via->sent_by.host, &prm.ret_addr);
  134. via->sent_by.port = prm.ret_port;
  135. pjsip_tx_data_invalidate_msg(tdata);
  136. }
  137. /* Update the SDP if it is present */
  138. if (tdata->msg->body && ast_sip_is_content_type(&tdata->msg->body->content_type, "application", "sdp") &&
  139. multihomed_rewrite_sdp(tdata->msg->body->data)) {
  140. struct pjmedia_sdp_session *sdp = tdata->msg->body->data;
  141. int stream;
  142. pj_strassign(&sdp->conn->addr, &prm.ret_addr);
  143. for (stream = 0; stream < sdp->media_count; ++stream) {
  144. if (sdp->media[stream]->conn) {
  145. pj_strassign(&sdp->media[stream]->conn->addr, &prm.ret_addr);
  146. }
  147. }
  148. pjsip_tx_data_invalidate_msg(tdata);
  149. }
  150. return PJ_SUCCESS;
  151. }
  152. static pjsip_module multihomed_module = {
  153. .name = { "Multihomed Routing", 18 },
  154. .id = -1,
  155. .priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 1,
  156. .on_tx_request = multihomed_on_tx_message,
  157. .on_tx_response = multihomed_on_tx_message,
  158. };
  159. static int unload_module(void)
  160. {
  161. ast_sip_unregister_service(&multihomed_module);
  162. return 0;
  163. }
  164. static int load_module(void)
  165. {
  166. char hostname[MAXHOSTNAMELEN] = "";
  167. pj_sockaddr addr;
  168. CHECK_PJSIP_MODULE_LOADED();
  169. if (!gethostname(hostname, sizeof(hostname) - 1)) {
  170. ast_verb(2, "Performing DNS resolution of local hostname '%s' to get local IPv4 and IPv6 address\n",
  171. hostname);
  172. }
  173. if (!pj_gethostip(pj_AF_INET(), &addr)) {
  174. pj_sockaddr_print(&addr, host_ipv4, sizeof(host_ipv4), 2);
  175. ast_verb(3, "Local IPv4 address determined to be: %s\n", host_ipv4);
  176. }
  177. if (!pj_gethostip(pj_AF_INET6(), &addr)) {
  178. pj_sockaddr_print(&addr, host_ipv6, sizeof(host_ipv6), 2);
  179. ast_verb(3, "Local IPv6 address determined to be: %s\n", host_ipv6);
  180. }
  181. if (ast_sip_register_service(&multihomed_module)) {
  182. ast_log(LOG_ERROR, "Could not register multihomed module for incoming and outgoing requests\n");
  183. return AST_MODULE_LOAD_FAILURE;
  184. }
  185. return AST_MODULE_LOAD_SUCCESS;
  186. }
  187. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Multihomed Routing Support",
  188. .support_level = AST_MODULE_SUPPORT_CORE,
  189. .load = load_module,
  190. .unload = unload_module,
  191. .load_pri = AST_MODPRI_APP_DEPEND,
  192. );