res_hep_rtcp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 RTCP logging with Homer
  21. *
  22. * \author Matt Jordan <mjordan@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>res_hep</depend>
  27. <support_level>extended</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/res_hep.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/netsock2.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/stasis.h"
  37. #include "asterisk/rtp_engine.h"
  38. #include "asterisk/json.h"
  39. #include "asterisk/config.h"
  40. static struct stasis_subscription *stasis_rtp_subscription;
  41. static char *assign_uuid(struct ast_json *json_channel)
  42. {
  43. const char *channel_name = ast_json_string_get(ast_json_object_get(json_channel, "name"));
  44. enum hep_uuid_type uuid_type = hepv3_get_uuid_type();
  45. char *uuid = NULL;
  46. if (!channel_name) {
  47. return NULL;
  48. }
  49. if (uuid_type == HEP_UUID_TYPE_CALL_ID) {
  50. struct ast_channel *chan = NULL;
  51. char buf[128];
  52. if (ast_begins_with(channel_name, "PJSIP")) {
  53. chan = ast_channel_get_by_name(channel_name);
  54. if (chan && !ast_func_read(chan, "CHANNEL(pjsip,call-id)", buf, sizeof(buf))) {
  55. uuid = ast_strdup(buf);
  56. }
  57. } else if (ast_begins_with(channel_name, "SIP")) {
  58. chan = ast_channel_get_by_name(channel_name);
  59. if (chan && !ast_func_read(chan, "SIP_HEADER(call-id)", buf, sizeof(buf))) {
  60. uuid = ast_strdup(buf);
  61. }
  62. }
  63. ast_channel_cleanup(chan);
  64. }
  65. /* If we couldn't get the call-id or didn't want it, just use the channel name */
  66. if (!uuid) {
  67. uuid = ast_strdup(channel_name);
  68. }
  69. return uuid;
  70. }
  71. static void rtcp_message_handler(struct stasis_message *message)
  72. {
  73. RAII_VAR(struct ast_json *, json_payload, NULL, ast_json_unref);
  74. RAII_VAR(char *, payload, NULL, ast_json_free);
  75. struct ast_json *json_blob;
  76. struct ast_json *json_channel;
  77. struct ast_json *json_rtcp;
  78. struct hepv3_capture_info *capture_info;
  79. struct ast_json *from;
  80. struct ast_json *to;
  81. struct timeval current_time = ast_tvnow();
  82. json_payload = stasis_message_to_json(message, NULL);
  83. if (!json_payload) {
  84. return;
  85. }
  86. json_blob = ast_json_object_get(json_payload, "blob");
  87. if (!json_blob) {
  88. return;
  89. }
  90. json_channel = ast_json_object_get(json_payload, "channel");
  91. if (!json_channel) {
  92. return;
  93. }
  94. json_rtcp = ast_json_object_get(json_payload, "rtcp_report");
  95. if (!json_rtcp) {
  96. return;
  97. }
  98. from = ast_json_object_get(json_blob, "from");
  99. to = ast_json_object_get(json_blob, "to");
  100. if (!from || !to) {
  101. return;
  102. }
  103. payload = ast_json_dump_string(json_rtcp);
  104. if (ast_strlen_zero(payload)) {
  105. return;
  106. }
  107. capture_info = hepv3_create_capture_info(payload, strlen(payload));
  108. if (!capture_info) {
  109. return;
  110. }
  111. ast_sockaddr_parse(&capture_info->src_addr, ast_json_string_get(from), PARSE_PORT_REQUIRE);
  112. ast_sockaddr_parse(&capture_info->dst_addr, ast_json_string_get(to), PARSE_PORT_REQUIRE);
  113. capture_info->uuid = assign_uuid(json_channel);
  114. if (!capture_info->uuid) {
  115. ao2_ref(capture_info, -1);
  116. return;
  117. }
  118. capture_info->capture_time = current_time;
  119. capture_info->capture_type = HEPV3_CAPTURE_TYPE_RTCP;
  120. capture_info->zipped = 0;
  121. hepv3_send_packet(capture_info);
  122. }
  123. static void rtp_topic_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  124. {
  125. struct stasis_message_type *message_type = stasis_message_type(message);
  126. if ((message_type == ast_rtp_rtcp_sent_type()) ||
  127. (message_type == ast_rtp_rtcp_received_type())) {
  128. rtcp_message_handler(message);
  129. }
  130. }
  131. static int load_module(void)
  132. {
  133. if (!ast_module_check("res_hep.so") || !hepv3_is_loaded()) {
  134. ast_log(AST_LOG_WARNING, "res_hep is not loaded or running; declining module load\n");
  135. return AST_MODULE_LOAD_DECLINE;
  136. }
  137. stasis_rtp_subscription = stasis_subscribe(ast_rtp_topic(),
  138. rtp_topic_handler, NULL);
  139. if (!stasis_rtp_subscription) {
  140. return AST_MODULE_LOAD_DECLINE;
  141. }
  142. stasis_subscription_accept_message_type(stasis_rtp_subscription, ast_rtp_rtcp_sent_type());
  143. stasis_subscription_accept_message_type(stasis_rtp_subscription, ast_rtp_rtcp_received_type());
  144. stasis_subscription_set_filter(stasis_rtp_subscription, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  145. return AST_MODULE_LOAD_SUCCESS;
  146. }
  147. static int unload_module(void)
  148. {
  149. if (stasis_rtp_subscription) {
  150. stasis_rtp_subscription = stasis_unsubscribe_and_join(stasis_rtp_subscription);
  151. }
  152. return 0;
  153. }
  154. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RTCP HEPv3 Logger",
  155. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  156. .load = load_module,
  157. .unload = unload_module,
  158. );