Merge "res_pjsip/res_pjsip_callerid: NULL check on caller id name string"
[asterisk/asterisk.git] / res / res_hep_rtcp.c
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 /*!
20  * \file
21  * \brief RTCP logging with Homer
22  *
23  * \author Matt Jordan <mjordan@digium.com>
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 && ast_begins_with(channel_name, "PJSIP")) {
57                 struct ast_channel *chan = ast_channel_get_by_name(channel_name);
58                 char buf[128];
59
60                 if (chan && !ast_func_read(chan, "CHANNEL(pjsip,call-id)", buf, sizeof(buf))) {
61                         uuid = ast_strdup(buf);
62                 }
63
64                 ast_channel_cleanup(chan);
65         }
66
67         /* If we couldn't get the call-id or didn't want it, just use the channel name */
68         if (!uuid) {
69                 uuid = ast_strdup(channel_name);
70         }
71
72         return uuid;
73 }
74
75 static void rtcp_message_handler(struct stasis_message *message)
76 {
77
78         RAII_VAR(struct ast_json *, json_payload, NULL, ast_json_unref);
79         RAII_VAR(char *,  payload, NULL, ast_json_free);
80         struct ast_json *json_blob;
81         struct ast_json *json_channel;
82         struct ast_json *json_rtcp;
83         struct hepv3_capture_info *capture_info;
84         struct ast_json *from;
85         struct ast_json *to;
86         struct timeval current_time = ast_tvnow();
87
88         json_payload = stasis_message_to_json(message, NULL);
89         if (!json_payload) {
90                 return;
91         }
92
93         json_blob = ast_json_object_get(json_payload, "blob");
94         if (!json_blob) {
95                 return;
96         }
97
98         json_channel = ast_json_object_get(json_payload, "channel");
99         if (!json_channel) {
100                 return;
101         }
102
103         json_rtcp = ast_json_object_get(json_payload, "rtcp_report");
104         if (!json_rtcp) {
105                 return;
106         }
107
108         from = ast_json_object_get(json_blob, "from");
109         to = ast_json_object_get(json_blob, "to");
110         if (!from || !to) {
111                 return;
112         }
113
114         payload = ast_json_dump_string(json_rtcp);
115         if (ast_strlen_zero(payload)) {
116                 return;
117         }
118
119         capture_info = hepv3_create_capture_info(payload, strlen(payload));
120         if (!capture_info) {
121                 return;
122         }
123         ast_sockaddr_parse(&capture_info->src_addr, ast_json_string_get(from), PARSE_PORT_REQUIRE);
124         ast_sockaddr_parse(&capture_info->dst_addr, ast_json_string_get(to), PARSE_PORT_REQUIRE);
125
126         capture_info->uuid = assign_uuid(json_channel);
127         if (!capture_info->uuid) {
128                 ao2_ref(capture_info, -1);
129                 return;
130         }
131         capture_info->capture_time = current_time;
132         capture_info->capture_type = HEPV3_CAPTURE_TYPE_RTCP;
133         capture_info->zipped = 0;
134
135         hepv3_send_packet(capture_info);
136 }
137
138 static void rtp_topic_handler(void *data, struct stasis_subscription *sub, struct stasis_message *message)
139 {
140         struct stasis_message_type *message_type = stasis_message_type(message);
141
142         if ((message_type == ast_rtp_rtcp_sent_type()) ||
143                 (message_type == ast_rtp_rtcp_received_type())) {
144                 rtcp_message_handler(message);
145         }
146 }
147
148 static int load_module(void)
149 {
150         if (!ast_module_check("res_hep.so") || !hepv3_is_loaded()) {
151                 ast_log(AST_LOG_WARNING, "res_hep is not loaded or running; declining module load\n");
152                 return AST_MODULE_LOAD_DECLINE;
153         }
154
155         stasis_rtp_subscription = stasis_subscribe(ast_rtp_topic(),
156                 rtp_topic_handler, NULL);
157         if (!stasis_rtp_subscription) {
158                 return AST_MODULE_LOAD_DECLINE;
159         }
160
161         return AST_MODULE_LOAD_SUCCESS;
162 }
163
164 static int unload_module(void)
165 {
166         if (stasis_rtp_subscription) {
167                 stasis_rtp_subscription = stasis_unsubscribe_and_join(stasis_rtp_subscription);
168         }
169
170         return 0;
171 }
172
173 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "RTCP HEPv3 Logger",
174         .load = load_module,
175         .unload = unload_module,
176         .load_pri = AST_MODPRI_DEFAULT,
177 );