res_hep_pjsip.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if ((dlg = pjsip_ua_find_dialog(call_id, local_tag, remote_tag, PJ_FALSE))
  48. && (session = ast_sip_dialog_get_session(dlg))
  49. && (session->channel)) {
  50. uuid = ast_strdup(ast_channel_name(session->channel));
  51. } else {
  52. uuid = ast_malloc(pj_strlen(call_id) + 1);
  53. if (uuid) {
  54. ast_copy_pj_str(uuid, call_id, pj_strlen(call_id) + 1);
  55. }
  56. }
  57. return uuid;
  58. }
  59. static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
  60. {
  61. char local_buf[256];
  62. char remote_buf[256];
  63. char *uuid;
  64. struct hepv3_capture_info *capture_info;
  65. pjsip_cid_hdr *cid_hdr;
  66. pjsip_from_hdr *from_hdr;
  67. pjsip_to_hdr *to_hdr;
  68. capture_info = hepv3_create_capture_info(tdata->buf.start, (size_t)(tdata->buf.cur - tdata->buf.start));
  69. if (!capture_info) {
  70. return PJ_SUCCESS;
  71. }
  72. pj_sockaddr_print(&tdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
  73. pj_sockaddr_print(&tdata->tp_info.dst_addr, remote_buf, sizeof(remote_buf), 3);
  74. cid_hdr = PJSIP_MSG_CID_HDR(tdata->msg);
  75. from_hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
  76. to_hdr = PJSIP_MSG_TO_HDR(tdata->msg);
  77. uuid = assign_uuid(&cid_hdr->id, &to_hdr->tag, &from_hdr->tag);
  78. if (!uuid) {
  79. ao2_ref(capture_info, -1);
  80. return PJ_SUCCESS;
  81. }
  82. ast_sockaddr_parse(&capture_info->src_addr, local_buf, PARSE_PORT_REQUIRE);
  83. ast_sockaddr_parse(&capture_info->dst_addr, remote_buf, PARSE_PORT_REQUIRE);
  84. capture_info->capture_time = ast_tvnow();
  85. capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
  86. capture_info->uuid = uuid;
  87. capture_info->zipped = 0;
  88. hepv3_send_packet(capture_info);
  89. return PJ_SUCCESS;
  90. }
  91. static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
  92. {
  93. char local_buf[256];
  94. char remote_buf[256];
  95. char *uuid;
  96. struct hepv3_capture_info *capture_info;
  97. capture_info = hepv3_create_capture_info(&rdata->pkt_info.packet, rdata->pkt_info.len);
  98. if (!capture_info) {
  99. return PJ_SUCCESS;
  100. }
  101. if (rdata->tp_info.transport->addr_len) {
  102. pj_sockaddr_print(&rdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
  103. }
  104. if (rdata->pkt_info.src_addr_len) {
  105. pj_sockaddr_print(&rdata->pkt_info.src_addr, remote_buf, sizeof(remote_buf), 3);
  106. }
  107. uuid = assign_uuid(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag);
  108. if (!uuid) {
  109. ao2_ref(capture_info, -1);
  110. return PJ_SUCCESS;
  111. }
  112. ast_sockaddr_parse(&capture_info->src_addr, remote_buf, PARSE_PORT_REQUIRE);
  113. ast_sockaddr_parse(&capture_info->dst_addr, local_buf, PARSE_PORT_REQUIRE);
  114. capture_info->capture_time.tv_sec = rdata->pkt_info.timestamp.sec;
  115. capture_info->capture_time.tv_usec = rdata->pkt_info.timestamp.msec * 1000;
  116. capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
  117. capture_info->uuid = uuid;
  118. capture_info->zipped = 0;
  119. hepv3_send_packet(capture_info);
  120. return PJ_FALSE;
  121. }
  122. static pjsip_module logging_module = {
  123. .name = { "HEPv3 Logging Module", 20 },
  124. .priority = 0,
  125. .on_rx_request = logging_on_rx_msg,
  126. .on_rx_response = logging_on_rx_msg,
  127. .on_tx_request = logging_on_tx_msg,
  128. .on_tx_response = logging_on_tx_msg,
  129. };
  130. static int load_module(void)
  131. {
  132. CHECK_PJSIP_MODULE_LOADED();
  133. ast_sip_register_service(&logging_module);
  134. return AST_MODULE_LOAD_SUCCESS;
  135. }
  136. static int unload_module(void)
  137. {
  138. ast_sip_unregister_service(&logging_module);
  139. return 0;
  140. }
  141. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP HEPv3 Logger",
  142. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  143. .load = load_module,
  144. .unload = unload_module,
  145. .load_pri = AST_MODPRI_DEFAULT,
  146. );