Debian Codegraph @

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