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