res_hep_pjsip.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2014, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@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. /*!
  19. * \file
  20. * \brief PJSIP logging with Homer
  21. *
  22. * \author Matt Jordan <mjordan@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>pjproject</depend>
  27. <depend>res_pjsip</depend>
  28. <depend>res_pjsip_session</depend>
  29. <depend>res_hep</depend>
  30. <support_level>extended</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include <pjsip.h>
  35. #include <pjsip_ua.h>
  36. #include <pjlib.h>
  37. #include "asterisk/res_pjsip.h"
  38. #include "asterisk/res_pjsip_session.h"
  39. #include "asterisk/res_hep.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/netsock2.h"
  42. static char *assign_uuid(const pj_str_t *call_id, const pj_str_t *local_tag, const pj_str_t *remote_tag)
  43. {
  44. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  45. pjsip_dialog *dlg;
  46. char *uuid = NULL;
  47. enum hep_uuid_type uuid_type = hepv3_get_uuid_type();
  48. if ((uuid_type == HEP_UUID_TYPE_CHANNEL)
  49. && (dlg = pjsip_ua_find_dialog(call_id, local_tag, remote_tag, PJ_FALSE))
  50. && (session = ast_sip_dialog_get_session(dlg))
  51. && (session->channel)) {
  52. uuid = ast_strdup(ast_channel_name(session->channel));
  53. }
  54. /* If we couldn't get the channel or we never wanted it, default to the call-id */
  55. if (!uuid) {
  56. uuid = ast_malloc(pj_strlen(call_id) + 1);
  57. if (uuid) {
  58. ast_copy_pj_str(uuid, call_id, pj_strlen(call_id) + 1);
  59. }
  60. }
  61. return uuid;
  62. }
  63. static int transport_to_protocol_id(pjsip_transport *tp)
  64. {
  65. /* XXX If we ever add SCTP support, we'll need to revisit */
  66. if (tp->flag & PJSIP_TRANSPORT_RELIABLE) {
  67. return IPPROTO_TCP;
  68. }
  69. return IPPROTO_UDP;
  70. }
  71. static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
  72. {
  73. char local_buf[256];
  74. char remote_buf[256];
  75. char *uuid;
  76. struct hepv3_capture_info *capture_info;
  77. pjsip_cid_hdr *cid_hdr;
  78. pjsip_from_hdr *from_hdr;
  79. pjsip_to_hdr *to_hdr;
  80. capture_info = hepv3_create_capture_info(tdata->buf.start, (size_t)(tdata->buf.cur - tdata->buf.start));
  81. if (!capture_info) {
  82. return PJ_SUCCESS;
  83. }
  84. if (!(tdata->tp_info.transport->flag & PJSIP_TRANSPORT_RELIABLE)) {
  85. pjsip_tpmgr_fla2_param prm;
  86. /* Attempt to determine what IP address will we send this packet out of */
  87. pjsip_tpmgr_fla2_param_default(&prm);
  88. prm.tp_type = tdata->tp_info.transport->key.type;
  89. pj_strset2(&prm.dst_host, tdata->tp_info.dst_name);
  90. prm.local_if = PJ_TRUE;
  91. /* If we can't get the local address use what we have already */
  92. if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), tdata->pool, &prm) != PJ_SUCCESS) {
  93. pj_sockaddr_print(&tdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
  94. } else {
  95. if (prm.tp_type & PJSIP_TRANSPORT_IPV6) {
  96. snprintf(local_buf, sizeof(local_buf), "[%.*s]:%hu",
  97. (int)pj_strlen(&prm.ret_addr),
  98. pj_strbuf(&prm.ret_addr),
  99. prm.ret_port);
  100. } else {
  101. snprintf(local_buf, sizeof(local_buf), "%.*s:%hu",
  102. (int)pj_strlen(&prm.ret_addr),
  103. pj_strbuf(&prm.ret_addr),
  104. prm.ret_port);
  105. }
  106. }
  107. } else {
  108. /* For reliable transports they can only ever come from the transport
  109. * local address.
  110. */
  111. pj_sockaddr_print(&tdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
  112. }
  113. pj_sockaddr_print(&tdata->tp_info.dst_addr, remote_buf, sizeof(remote_buf), 3);
  114. cid_hdr = PJSIP_MSG_CID_HDR(tdata->msg);
  115. from_hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
  116. to_hdr = PJSIP_MSG_TO_HDR(tdata->msg);
  117. uuid = assign_uuid(&cid_hdr->id, &to_hdr->tag, &from_hdr->tag);
  118. if (!uuid) {
  119. ao2_ref(capture_info, -1);
  120. return PJ_SUCCESS;
  121. }
  122. ast_sockaddr_parse(&capture_info->src_addr, local_buf, PARSE_PORT_REQUIRE);
  123. ast_sockaddr_parse(&capture_info->dst_addr, remote_buf, PARSE_PORT_REQUIRE);
  124. capture_info->protocol_id = transport_to_protocol_id(tdata->tp_info.transport);
  125. capture_info->capture_time = ast_tvnow();
  126. capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
  127. capture_info->uuid = uuid;
  128. capture_info->zipped = 0;
  129. hepv3_send_packet(capture_info);
  130. return PJ_SUCCESS;
  131. }
  132. static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
  133. {
  134. char local_buf[256];
  135. char remote_buf[256];
  136. char *uuid;
  137. struct hepv3_capture_info *capture_info;
  138. capture_info = hepv3_create_capture_info(&rdata->pkt_info.packet, rdata->pkt_info.len);
  139. if (!capture_info) {
  140. return PJ_SUCCESS;
  141. }
  142. if (!rdata->pkt_info.src_addr_len) {
  143. return PJ_SUCCESS;
  144. }
  145. pj_sockaddr_print(&rdata->pkt_info.src_addr, remote_buf, sizeof(remote_buf), 3);
  146. if (!(rdata->tp_info.transport->flag & PJSIP_TRANSPORT_RELIABLE)) {
  147. pjsip_tpmgr_fla2_param prm;
  148. /* Attempt to determine what IP address we probably received this packet on */
  149. pjsip_tpmgr_fla2_param_default(&prm);
  150. prm.tp_type = rdata->tp_info.transport->key.type;
  151. pj_strset2(&prm.dst_host, rdata->pkt_info.src_name);
  152. prm.local_if = PJ_TRUE;
  153. /* If we can't get the local address use what we have already */
  154. if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), rdata->tp_info.pool, &prm) != PJ_SUCCESS) {
  155. pj_sockaddr_print(&rdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
  156. } else {
  157. if (prm.tp_type & PJSIP_TRANSPORT_IPV6) {
  158. snprintf(local_buf, sizeof(local_buf), "[%.*s]:%hu",
  159. (int)pj_strlen(&prm.ret_addr),
  160. pj_strbuf(&prm.ret_addr),
  161. prm.ret_port);
  162. } else {
  163. snprintf(local_buf, sizeof(local_buf), "%.*s:%hu",
  164. (int)pj_strlen(&prm.ret_addr),
  165. pj_strbuf(&prm.ret_addr),
  166. prm.ret_port);
  167. }
  168. }
  169. } else {
  170. pj_sockaddr_print(&rdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
  171. }
  172. uuid = assign_uuid(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag);
  173. if (!uuid) {
  174. ao2_ref(capture_info, -1);
  175. return PJ_SUCCESS;
  176. }
  177. ast_sockaddr_parse(&capture_info->src_addr, remote_buf, PARSE_PORT_REQUIRE);
  178. ast_sockaddr_parse(&capture_info->dst_addr, local_buf, PARSE_PORT_REQUIRE);
  179. capture_info->protocol_id = transport_to_protocol_id(rdata->tp_info.transport);
  180. capture_info->capture_time.tv_sec = rdata->pkt_info.timestamp.sec;
  181. capture_info->capture_time.tv_usec = rdata->pkt_info.timestamp.msec * 1000;
  182. capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
  183. capture_info->uuid = uuid;
  184. capture_info->zipped = 0;
  185. hepv3_send_packet(capture_info);
  186. return PJ_FALSE;
  187. }
  188. static pjsip_module logging_module = {
  189. .name = { "HEPv3 Logging Module", 20 },
  190. .priority = 0,
  191. .on_rx_request = logging_on_rx_msg,
  192. .on_rx_response = logging_on_rx_msg,
  193. .on_tx_request = logging_on_tx_msg,
  194. .on_tx_response = logging_on_tx_msg,
  195. };
  196. static int load_module(void)
  197. {
  198. CHECK_PJSIP_MODULE_LOADED();
  199. if (!ast_module_check("res_hep.so") || !hepv3_is_loaded()) {
  200. ast_log(AST_LOG_WARNING, "res_hep is not loaded or running; declining module load\n");
  201. return AST_MODULE_LOAD_DECLINE;
  202. }
  203. ast_sip_register_service(&logging_module);
  204. return AST_MODULE_LOAD_SUCCESS;
  205. }
  206. static int unload_module(void)
  207. {
  208. ast_sip_unregister_service(&logging_module);
  209. return 0;
  210. }
  211. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PJSIP HEPv3 Logger",
  212. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  213. .load = load_module,
  214. .unload = unload_module,
  215. );