res_pjsip: Updates and adds more PJSIP CLI commands.
[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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <pjsip.h>
31
32 #include "asterisk/res_pjsip.h"
33 #include "asterisk/module.h"
34 #include "asterisk/logger.h"
35 #include "asterisk/cli.h"
36 #include "asterisk/netsock2.h"
37
38 enum pjsip_logging_mode {
39         LOGGING_MODE_DISABLED,    /* No logging is enabled */
40         LOGGING_MODE_ENABLED,     /* Logging is enabled */
41 };
42
43 static enum pjsip_logging_mode logging_mode;
44 static struct ast_sockaddr log_addr;
45
46 /*! \brief  Return the first entry from ast_sockaddr_resolve filtered by address family
47  *
48  * \warning Using this function probably means you have a faulty design.
49  * \note This function was taken from the function of the same name in chan_sip.c
50  */
51 static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
52                                       const char* name, int flag, int family)
53 {
54         struct ast_sockaddr *addrs;
55         int addrs_cnt;
56
57         addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
58         if (addrs_cnt <= 0) {
59                 return 1;
60         }
61         if (addrs_cnt > 1) {
62                 ast_debug(1, "Multiple addresses, using the first one only\n");
63         }
64
65         ast_sockaddr_copy(addr, &addrs[0]);
66
67         ast_free(addrs);
68         return 0;
69 }
70
71 /*! \brief See if we pass debug IP filter */
72 static inline int pjsip_log_test_addr(const char *address, int port)
73 {
74         struct ast_sockaddr test_addr;
75         if (logging_mode == LOGGING_MODE_DISABLED) {
76                 return 0;
77         }
78
79         /* A null logging address means we'll debug any address */
80         if (ast_sockaddr_isnull(&log_addr)) {
81                 return 1;
82         }
83
84         /* A null address was passed in. Just reject it. */
85         if (ast_strlen_zero(address)) {
86                 return 0;
87         }
88
89         ast_sockaddr_parse(&test_addr, address, PARSE_PORT_IGNORE);
90         ast_sockaddr_set_port(&test_addr, port);
91
92         /* If no port was specified for a debug address, just compare the
93          * addresses, otherwise compare the address and port
94          */
95         if (ast_sockaddr_port(&log_addr)) {
96                 return !ast_sockaddr_cmp(&log_addr, &test_addr);
97         } else {
98                 return !ast_sockaddr_cmp_addr(&log_addr, &test_addr);
99         }
100 }
101
102 static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
103 {
104         if (!pjsip_log_test_addr(tdata->tp_info.dst_name, tdata->tp_info.dst_port)) {
105                 return PJ_SUCCESS;
106         }
107
108         ast_verbose("<--- Transmitting SIP %s (%d bytes) to %s:%s:%d --->\n%.*s\n",
109                     tdata->msg->type == PJSIP_REQUEST_MSG ? "request" : "response",
110                     (int) (tdata->buf.cur - tdata->buf.start),
111                     tdata->tp_info.transport->type_name,
112                     tdata->tp_info.dst_name,
113                     tdata->tp_info.dst_port,
114                     (int) (tdata->buf.end - tdata->buf.start), tdata->buf.start);
115         return PJ_SUCCESS;
116 }
117
118 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
119 {
120         if (!pjsip_log_test_addr(rdata->pkt_info.src_name, rdata->pkt_info.src_port)) {
121                 return PJ_FALSE;
122         }
123
124         ast_verbose("<--- Received SIP %s (%d bytes) from %s:%s:%d --->\n%s\n",
125                     rdata->msg_info.msg->type == PJSIP_REQUEST_MSG ? "request" : "response",
126                     rdata->msg_info.len,
127                     rdata->tp_info.transport->type_name,
128                     rdata->pkt_info.src_name,
129                     rdata->pkt_info.src_port,
130                     rdata->pkt_info.packet);
131         return PJ_FALSE;
132 }
133
134 static pjsip_module logging_module = {
135         .name = { "Logging Module", 14 },
136         .priority = 0,
137         .on_rx_request = logging_on_rx_msg,
138         .on_rx_response = logging_on_rx_msg,
139         .on_tx_request = logging_on_tx_msg,
140         .on_tx_response = logging_on_tx_msg,
141 };
142
143 static char *pjsip_enable_logger_host(int fd, const char *arg)
144 {
145         if (ast_sockaddr_resolve_first_af(&log_addr, arg, 0, AST_AF_UNSPEC)) {
146                 return CLI_SHOWUSAGE;
147         }
148
149         ast_cli(fd, "PJSIP Logging Enabled for host: %s\n", ast_sockaddr_stringify_addr(&log_addr));
150         logging_mode = LOGGING_MODE_ENABLED;
151
152         return CLI_SUCCESS;
153 }
154
155 static char *pjsip_set_logger(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
156 {
157         const char *what;
158
159         if (cmd == CLI_INIT) {
160                 e->command = "pjsip set logger {on|off|host}";
161                 e->usage =
162                         "Usage: pjsip set logger {on|off|host <name>}\n"
163                         "       Enables or disabling logging of SIP packets\n"
164                         "       read on ports bound to PJSIP transports either\n"
165                         "       globally or enables logging for an individual\n"
166                         "       host.\n";
167                 return NULL;
168         } else if (cmd == CLI_GENERATE) {
169                 return NULL;
170         }
171
172         what = a->argv[e->args - 1];     /* Guaranteed to exist */
173
174         if (a->argc == e->args) {        /* on/off */
175                 if (!strcasecmp(what, "on")) {
176                         logging_mode = LOGGING_MODE_ENABLED;
177                         ast_cli(a->fd, "PJSIP Logging enabled\n");
178                         ast_sockaddr_setnull(&log_addr);
179                         return CLI_SUCCESS;
180                 } else if (!strcasecmp(what, "off")) {
181                         logging_mode = LOGGING_MODE_DISABLED;
182                         ast_cli(a->fd, "PJSIP Logging disabled\n");
183                         return CLI_SUCCESS;
184                 }
185         } else if (a->argc == e->args + 1) {
186                 if (!strcasecmp(what, "host")) {
187                         return pjsip_enable_logger_host(a->fd, a->argv[e->args]);
188                 }
189         }
190
191         return CLI_SHOWUSAGE;
192 }
193
194 static struct ast_cli_entry cli_pjsip[] = {
195         AST_CLI_DEFINE(pjsip_set_logger, "Enable/Disable PJSIP Logger Output")
196 };
197
198 static void check_debug(void)
199 {
200         RAII_VAR(char *, debug, ast_sip_get_debug(), ast_free);
201
202         if (ast_false(debug)) {
203                 logging_mode = LOGGING_MODE_DISABLED;
204                 return;
205         }
206
207         logging_mode = LOGGING_MODE_ENABLED;
208
209         if (ast_true(debug)) {
210                 ast_sockaddr_setnull(&log_addr);
211                 return;
212         }
213
214         /* assume host */
215         if (ast_sockaddr_resolve_first_af(&log_addr, debug, 0, AST_AF_UNSPEC)) {
216                 ast_log(LOG_WARNING, "Could not resolve host %s for debug "
217                         "logging\n", debug);
218         }
219 }
220
221 static void global_reloaded(const char *object_type)
222 {
223         check_debug();
224 }
225
226 static const struct ast_sorcery_observer global_observer = {
227         .loaded = global_reloaded
228 };
229
230 static int load_module(void)
231 {
232         if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &global_observer)) {
233                 ast_log(LOG_WARNING, "Unable to add global observer\n");
234                 return AST_MODULE_LOAD_DECLINE;
235         }
236
237         check_debug();
238
239         ast_sip_register_service(&logging_module);
240         ast_cli_register_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
241
242         return AST_MODULE_LOAD_SUCCESS;
243 }
244
245 static int unload_module(void)
246 {
247         ast_cli_unregister_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
248         ast_sip_unregister_service(&logging_module);
249
250         ast_sorcery_observer_remove(
251                 ast_sip_get_sorcery(), "global", &global_observer);
252
253         return 0;
254 }
255
256 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Packet Logger",
257                 .load = load_module,
258                 .unload = unload_module,
259                 .load_pri = AST_MODPRI_APP_DEPEND,
260                );