90cb06fb898f820f58665dc5702561643a380c0c
[asterisk/asterisk.git] / res / res_pjsip_logger.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@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 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <defaultenabled>yes</defaultenabled>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29
30 #include "asterisk/res_pjsip.h"
31 #include "asterisk/module.h"
32 #include "asterisk/logger.h"
33 #include "asterisk/cli.h"
34 #include "asterisk/netsock2.h"
35
36 enum pjsip_logging_mode {
37         LOGGING_MODE_DISABLED,    /* No logging is enabled */
38         LOGGING_MODE_ENABLED,     /* Logging is enabled */
39 };
40
41 static enum pjsip_logging_mode logging_mode;
42 static struct ast_sockaddr log_addr;
43
44 /*! \brief  Return the first entry from ast_sockaddr_resolve filtered by address family
45  *
46  * \warning Using this function probably means you have a faulty design.
47  * \note This function was taken from the function of the same name in chan_sip.c
48  */
49 static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
50                                       const char* name, int flag, int family)
51 {
52         struct ast_sockaddr *addrs;
53         int addrs_cnt;
54
55         addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
56         if (addrs_cnt <= 0) {
57                 return 1;
58         }
59         if (addrs_cnt > 1) {
60                 ast_debug(1, "Multiple addresses, using the first one only\n");
61         }
62
63         ast_sockaddr_copy(addr, &addrs[0]);
64
65         ast_free(addrs);
66         return 0;
67 }
68
69 /*! \brief See if we pass debug IP filter */
70 static inline int pjsip_log_test_addr(const char *address, int port)
71 {
72         struct ast_sockaddr test_addr;
73         if (logging_mode == LOGGING_MODE_DISABLED) {
74                 return 0;
75         }
76
77         /* A null logging address means we'll debug any address */
78         if (ast_sockaddr_isnull(&log_addr)) {
79                 return 1;
80         }
81
82         /* A null address was passed in. Just reject it. */
83         if (ast_strlen_zero(address)) {
84                 return 0;
85         }
86
87         ast_sockaddr_parse(&test_addr, address, PARSE_PORT_IGNORE);
88         ast_sockaddr_set_port(&test_addr, port);
89
90         /* If no port was specified for a debug address, just compare the
91          * addresses, otherwise compare the address and port
92          */
93         if (ast_sockaddr_port(&log_addr)) {
94                 return !ast_sockaddr_cmp(&log_addr, &test_addr);
95         } else {
96                 return !ast_sockaddr_cmp_addr(&log_addr, &test_addr);
97         }
98 }
99
100 static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
101 {
102         if (!pjsip_log_test_addr(tdata->tp_info.dst_name, tdata->tp_info.dst_port)) {
103                 return PJ_SUCCESS;
104         }
105
106         ast_verbose("<--- Transmitting SIP %s (%d bytes) to %s:%s:%d --->\n%.*s\n",
107                     tdata->msg->type == PJSIP_REQUEST_MSG ? "request" : "response",
108                     (int) (tdata->buf.cur - tdata->buf.start),
109                     tdata->tp_info.transport->type_name,
110                     tdata->tp_info.dst_name,
111                     tdata->tp_info.dst_port,
112                     (int) (tdata->buf.end - tdata->buf.start), tdata->buf.start);
113         return PJ_SUCCESS;
114 }
115
116 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
117 {
118         if (!pjsip_log_test_addr(rdata->pkt_info.src_name, rdata->pkt_info.src_port)) {
119                 return PJ_FALSE;
120         }
121
122         ast_verbose("<--- Received SIP %s (%d bytes) from %s:%s:%d --->\n%s\n",
123                     rdata->msg_info.msg->type == PJSIP_REQUEST_MSG ? "request" : "response",
124                     rdata->msg_info.len,
125                     rdata->tp_info.transport->type_name,
126                     rdata->pkt_info.src_name,
127                     rdata->pkt_info.src_port,
128                     rdata->pkt_info.packet);
129         return PJ_FALSE;
130 }
131
132 static pjsip_module logging_module = {
133         .name = { "Logging Module", 14 },
134         .priority = 0,
135         .on_rx_request = logging_on_rx_msg,
136         .on_rx_response = logging_on_rx_msg,
137         .on_tx_request = logging_on_tx_msg,
138         .on_tx_response = logging_on_tx_msg,
139 };
140
141 static char *pjsip_enable_logger_host(int fd, const char *arg)
142 {
143         if (ast_sockaddr_resolve_first_af(&log_addr, arg, 0, AST_AF_UNSPEC)) {
144                 return CLI_SHOWUSAGE;
145         }
146
147         ast_cli(fd, "PJSIP Logging Enabled for host: %s\n", ast_sockaddr_stringify_addr(&log_addr));
148         logging_mode = LOGGING_MODE_ENABLED;
149
150         return CLI_SUCCESS;
151 }
152
153 static char *pjsip_set_logger(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
154 {
155         const char *what;
156
157         if (cmd == CLI_INIT) {
158                 e->command = "pjsip set logger {on|off|host}";
159                 e->usage =
160                         "Usage: pjsip set logger {on|off}\n"
161                         "       Enables or disabling logging of SIP packets\n"
162                         "       read on ports bound to PJSIP transports either\n"
163                         "       globally or enables logging for an individual\n"
164                         "       host.\n";
165                 return NULL;
166         } else if (cmd == CLI_GENERATE) {
167                 return NULL;
168         }
169
170         what = a->argv[e->args - 1];     /* Guaranteed to exist */
171
172         if (a->argc == e->args) {        /* on/off */
173                 if (!strcasecmp(what, "on")) {
174                         logging_mode = LOGGING_MODE_ENABLED;
175                         ast_cli(a->fd, "PJSIP Logging enabled\n");
176                         ast_sockaddr_setnull(&log_addr);
177                         return CLI_SUCCESS;
178                 } else if (!strcasecmp(what, "off")) {
179                         logging_mode = LOGGING_MODE_DISABLED;
180                         ast_cli(a->fd, "PJSIP Logging disabled\n");
181                         return CLI_SUCCESS;
182                 }
183         } else if (a->argc == e->args + 1) {
184                 if (!strcasecmp(what, "host")) {
185                         return pjsip_enable_logger_host(a->fd, a->argv[e->args]);
186                 }
187         }
188
189         return CLI_SHOWUSAGE;
190 }
191
192 static struct ast_cli_entry cli_pjsip[] = {
193         AST_CLI_DEFINE(pjsip_set_logger, "Enable/Disable PJSIP Logger Output")
194 };
195
196 static int load_module(void)
197 {
198         ast_sip_register_service(&logging_module);
199         ast_cli_register_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
200         return AST_MODULE_LOAD_SUCCESS;
201 }
202
203 static int unload_module(void)
204 {
205         ast_cli_unregister_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
206         ast_sip_unregister_service(&logging_module);
207         return 0;
208 }
209
210 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Packet Logger",
211                 .load = load_module,
212                 .unload = unload_module,
213                 .load_pri = AST_MODPRI_APP_DEPEND,
214                );