Debian Codegraph @

res/res_hep_rtcp.c

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