13efbfa6afa1d4cdc9d8842e17437b28eefcdfc3
[asterisk/asterisk.git] / res / res_hep_pjsip.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 PJSIP logging with Homer
22  *
23  * \author Matt Jordan <mjordan@digium.com>
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         pjsip_tpmgr_fla2_param prm;
93
94         capture_info = hepv3_create_capture_info(tdata->buf.start, (size_t)(tdata->buf.cur - tdata->buf.start));
95         if (!capture_info) {
96                 return PJ_SUCCESS;
97         }
98
99         /* Attempt to determine what IP address will we send this packet out of */
100         pjsip_tpmgr_fla2_param_default(&prm);
101         prm.tp_type = tdata->tp_info.transport->key.type;
102         pj_strset2(&prm.dst_host, tdata->tp_info.dst_name);
103         prm.local_if = PJ_TRUE;
104
105         /* If we can't get the local address use what we have already */
106         if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), tdata->pool, &prm) != PJ_SUCCESS) {
107                 pj_sockaddr_print(&tdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
108         } else {
109                 if (prm.tp_type & PJSIP_TRANSPORT_IPV6) {
110                         snprintf(local_buf, sizeof(local_buf), "[%.*s]:%hu",
111                                 (int)pj_strlen(&prm.ret_addr),
112                                 pj_strbuf(&prm.ret_addr),
113                                 prm.ret_port);
114                 } else {
115                         snprintf(local_buf, sizeof(local_buf), "%.*s:%hu",
116                                 (int)pj_strlen(&prm.ret_addr),
117                                 pj_strbuf(&prm.ret_addr),
118                                 prm.ret_port);
119                 }
120         }
121         pj_sockaddr_print(&tdata->tp_info.dst_addr, remote_buf, sizeof(remote_buf), 3);
122
123         cid_hdr = PJSIP_MSG_CID_HDR(tdata->msg);
124         from_hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
125         to_hdr = PJSIP_MSG_TO_HDR(tdata->msg);
126
127         uuid = assign_uuid(&cid_hdr->id, &to_hdr->tag, &from_hdr->tag);
128         if (!uuid) {
129                 ao2_ref(capture_info, -1);
130                 return PJ_SUCCESS;
131         }
132
133         ast_sockaddr_parse(&capture_info->src_addr, local_buf, PARSE_PORT_REQUIRE);
134         ast_sockaddr_parse(&capture_info->dst_addr, remote_buf, PARSE_PORT_REQUIRE);
135
136         capture_info->protocol_id = transport_to_protocol_id(tdata->tp_info.transport);
137         capture_info->capture_time = ast_tvnow();
138         capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
139         capture_info->uuid = uuid;
140         capture_info->zipped = 0;
141
142         hepv3_send_packet(capture_info);
143
144         return PJ_SUCCESS;
145 }
146
147 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
148 {
149         char local_buf[256];
150         char remote_buf[256];
151         char *uuid;
152         struct hepv3_capture_info *capture_info;
153         pjsip_tpmgr_fla2_param prm;
154
155         capture_info = hepv3_create_capture_info(&rdata->pkt_info.packet, rdata->pkt_info.len);
156         if (!capture_info) {
157                 return PJ_SUCCESS;
158         }
159
160         if (!rdata->pkt_info.src_addr_len) {
161                 return PJ_SUCCESS;
162         }
163         pj_sockaddr_print(&rdata->pkt_info.src_addr, remote_buf, sizeof(remote_buf), 3);
164
165         /* Attempt to determine what IP address we probably received this packet on */
166         pjsip_tpmgr_fla2_param_default(&prm);
167         prm.tp_type = rdata->tp_info.transport->key.type;
168         pj_strset2(&prm.dst_host, rdata->pkt_info.src_name);
169         prm.local_if = PJ_TRUE;
170
171         /* If we can't get the local address use what we have already */
172         if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), rdata->tp_info.pool, &prm) != PJ_SUCCESS) {
173                 pj_sockaddr_print(&rdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
174         } else {
175                 if (prm.tp_type & PJSIP_TRANSPORT_IPV6) {
176                         snprintf(local_buf, sizeof(local_buf), "[%.*s]:%hu",
177                                 (int)pj_strlen(&prm.ret_addr),
178                                 pj_strbuf(&prm.ret_addr),
179                                 prm.ret_port);
180                 } else {
181                         snprintf(local_buf, sizeof(local_buf), "%.*s:%hu",
182                                 (int)pj_strlen(&prm.ret_addr),
183                                 pj_strbuf(&prm.ret_addr),
184                                 prm.ret_port);
185                 }
186         }
187
188         uuid = assign_uuid(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag);
189         if (!uuid) {
190                 ao2_ref(capture_info, -1);
191                 return PJ_SUCCESS;
192         }
193
194         ast_sockaddr_parse(&capture_info->src_addr, remote_buf, PARSE_PORT_REQUIRE);
195         ast_sockaddr_parse(&capture_info->dst_addr, local_buf, PARSE_PORT_REQUIRE);
196
197         capture_info->protocol_id = transport_to_protocol_id(rdata->tp_info.transport);
198         capture_info->capture_time.tv_sec = rdata->pkt_info.timestamp.sec;
199         capture_info->capture_time.tv_usec = rdata->pkt_info.timestamp.msec * 1000;
200         capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
201         capture_info->uuid = uuid;
202         capture_info->zipped = 0;
203
204         hepv3_send_packet(capture_info);
205
206         return PJ_FALSE;
207 }
208
209 static pjsip_module logging_module = {
210         .name = { "HEPv3 Logging Module", 20 },
211         .priority = 0,
212         .on_rx_request = logging_on_rx_msg,
213         .on_rx_response = logging_on_rx_msg,
214         .on_tx_request = logging_on_tx_msg,
215         .on_tx_response = logging_on_tx_msg,
216 };
217
218
219 static int load_module(void)
220 {
221         CHECK_PJSIP_MODULE_LOADED();
222
223         if (!ast_module_check("res_hep.so") || !hepv3_is_loaded()) {
224                 ast_log(AST_LOG_WARNING, "res_hep is not loaded or running; declining module load\n");
225                 return AST_MODULE_LOAD_DECLINE;
226         }
227
228         ast_sip_register_service(&logging_module);
229         return AST_MODULE_LOAD_SUCCESS;
230 }
231
232 static int unload_module(void)
233 {
234         ast_sip_unregister_service(&logging_module);
235         return 0;
236 }
237
238 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP HEPv3 Logger",
239         .support_level = AST_MODULE_SUPPORT_EXTENDED,
240         .load = load_module,
241         .unload = unload_module,
242         .load_pri = AST_MODPRI_DEFAULT,
243 );