git migration: Refactor the ASTERISK_FILE_VERSION macro
[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
55         if ((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         } else {
61
62                 uuid = ast_malloc(pj_strlen(call_id) + 1);
63                 if (uuid) {
64                         ast_copy_pj_str(uuid, call_id, pj_strlen(call_id) + 1);
65                 }
66         }
67
68         return uuid;
69 }
70
71 static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
72 {
73         char local_buf[256];
74         char remote_buf[256];
75         char *uuid;
76         struct hepv3_capture_info *capture_info;
77         pjsip_cid_hdr *cid_hdr;
78         pjsip_from_hdr *from_hdr;
79         pjsip_to_hdr *to_hdr;
80
81         capture_info = hepv3_create_capture_info(tdata->buf.start, (size_t)(tdata->buf.cur - tdata->buf.start));
82         if (!capture_info) {
83                 return PJ_SUCCESS;
84         }
85
86         pj_sockaddr_print(&tdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
87         pj_sockaddr_print(&tdata->tp_info.dst_addr, remote_buf, sizeof(remote_buf), 3);
88
89         cid_hdr = PJSIP_MSG_CID_HDR(tdata->msg);
90         from_hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
91         to_hdr = PJSIP_MSG_TO_HDR(tdata->msg);
92
93         uuid = assign_uuid(&cid_hdr->id, &to_hdr->tag, &from_hdr->tag);
94         if (!uuid) {
95                 ao2_ref(capture_info, -1);
96                 return PJ_SUCCESS;
97         }
98
99         ast_sockaddr_parse(&capture_info->src_addr, local_buf, PARSE_PORT_REQUIRE);
100         ast_sockaddr_parse(&capture_info->dst_addr, remote_buf, PARSE_PORT_REQUIRE);
101
102         capture_info->capture_time = ast_tvnow();
103         capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
104         capture_info->uuid = uuid;
105         capture_info->zipped = 0;
106
107         hepv3_send_packet(capture_info);
108
109         return PJ_SUCCESS;
110 }
111
112 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
113 {
114         char local_buf[256];
115         char remote_buf[256];
116         char *uuid;
117         struct hepv3_capture_info *capture_info;
118
119         capture_info = hepv3_create_capture_info(&rdata->pkt_info.packet, rdata->pkt_info.len);
120         if (!capture_info) {
121                 return PJ_SUCCESS;
122         }
123
124         if (rdata->tp_info.transport->addr_len) {
125                 pj_sockaddr_print(&rdata->tp_info.transport->local_addr, local_buf, sizeof(local_buf), 3);
126         }
127         if (rdata->pkt_info.src_addr_len) {
128                 pj_sockaddr_print(&rdata->pkt_info.src_addr, remote_buf, sizeof(remote_buf), 3);
129         }
130
131         uuid = assign_uuid(&rdata->msg_info.cid->id, &rdata->msg_info.to->tag, &rdata->msg_info.from->tag);
132         if (!uuid) {
133                 ao2_ref(capture_info, -1);
134                 return PJ_SUCCESS;
135         }
136
137         ast_sockaddr_parse(&capture_info->src_addr, remote_buf, PARSE_PORT_REQUIRE);
138         ast_sockaddr_parse(&capture_info->dst_addr, local_buf, PARSE_PORT_REQUIRE);
139         capture_info->capture_time.tv_sec = rdata->pkt_info.timestamp.sec;
140         capture_info->capture_time.tv_usec = rdata->pkt_info.timestamp.msec * 1000;
141         capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP;
142         capture_info->uuid = uuid;
143         capture_info->zipped = 0;
144
145         hepv3_send_packet(capture_info);
146
147         return PJ_FALSE;
148 }
149
150 static pjsip_module logging_module = {
151         .name = { "HEPv3 Logging Module", 20 },
152         .priority = 0,
153         .on_rx_request = logging_on_rx_msg,
154         .on_rx_response = logging_on_rx_msg,
155         .on_tx_request = logging_on_tx_msg,
156         .on_tx_response = logging_on_tx_msg,
157 };
158
159
160 static int load_module(void)
161 {
162         CHECK_PJSIP_MODULE_LOADED();
163
164         ast_sip_register_service(&logging_module);
165         return AST_MODULE_LOAD_SUCCESS;
166 }
167
168 static int unload_module(void)
169 {
170         ast_sip_unregister_service(&logging_module);
171         return 0;
172 }
173
174 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP HEPv3 Logger",
175                 .support_level = AST_MODULE_SUPPORT_EXTENDED,
176                 .load = load_module,
177                 .unload = unload_module,
178                 .load_pri = AST_MODPRI_DEFAULT,
179                );