2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
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.
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.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <defaultenabled>yes</defaultenabled>
23 <support_level>core</support_level>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
38 enum pjsip_logging_mode {
39 LOGGING_MODE_DISABLED, /* No logging is enabled */
40 LOGGING_MODE_ENABLED, /* Logging is enabled */
43 static enum pjsip_logging_mode logging_mode;
44 static struct ast_sockaddr log_addr;
46 /*! \brief Return the first entry from ast_sockaddr_resolve filtered by address family
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
51 static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
52 const char* name, int flag, int family)
54 struct ast_sockaddr *addrs;
57 addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
62 ast_debug(1, "Multiple addresses, using the first one only\n");
65 ast_sockaddr_copy(addr, &addrs[0]);
71 /*! \brief See if we pass debug IP filter */
72 static inline int pjsip_log_test_addr(const char *address, int port)
74 struct ast_sockaddr test_addr;
75 if (logging_mode == LOGGING_MODE_DISABLED) {
79 /* A null logging address means we'll debug any address */
80 if (ast_sockaddr_isnull(&log_addr)) {
84 /* A null address was passed in. Just reject it. */
85 if (ast_strlen_zero(address)) {
89 ast_sockaddr_parse(&test_addr, address, PARSE_PORT_IGNORE);
90 ast_sockaddr_set_port(&test_addr, port);
92 /* If no port was specified for a debug address, just compare the
93 * addresses, otherwise compare the address and port
95 if (ast_sockaddr_port(&log_addr)) {
96 return !ast_sockaddr_cmp(&log_addr, &test_addr);
98 return !ast_sockaddr_cmp_addr(&log_addr, &test_addr);
102 static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
104 if (!pjsip_log_test_addr(tdata->tp_info.dst_name, tdata->tp_info.dst_port)) {
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);
118 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
120 if (!pjsip_log_test_addr(rdata->pkt_info.src_name, rdata->pkt_info.src_port)) {
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",
127 rdata->tp_info.transport->type_name,
128 rdata->pkt_info.src_name,
129 rdata->pkt_info.src_port,
130 rdata->pkt_info.packet);
134 static pjsip_module logging_module = {
135 .name = { "Logging Module", 14 },
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,
143 static char *pjsip_enable_logger_host(int fd, const char *arg)
145 if (ast_sockaddr_resolve_first_af(&log_addr, arg, 0, AST_AF_UNSPEC)) {
146 return CLI_SHOWUSAGE;
149 ast_cli(fd, "PJSIP Logging Enabled for host: %s\n", ast_sockaddr_stringify_addr(&log_addr));
150 logging_mode = LOGGING_MODE_ENABLED;
155 static char *pjsip_set_logger(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
159 if (cmd == CLI_INIT) {
160 e->command = "pjsip set logger {on|off|host}";
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"
168 } else if (cmd == CLI_GENERATE) {
172 what = a->argv[e->args - 1]; /* Guaranteed to exist */
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);
180 } else if (!strcasecmp(what, "off")) {
181 logging_mode = LOGGING_MODE_DISABLED;
182 ast_cli(a->fd, "PJSIP Logging disabled\n");
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]);
191 return CLI_SHOWUSAGE;
194 static struct ast_cli_entry cli_pjsip[] = {
195 AST_CLI_DEFINE(pjsip_set_logger, "Enable/Disable PJSIP Logger Output")
198 static void check_debug(void)
200 RAII_VAR(char *, debug, ast_sip_get_debug(), ast_free);
202 if (ast_false(debug)) {
203 logging_mode = LOGGING_MODE_DISABLED;
207 logging_mode = LOGGING_MODE_ENABLED;
209 if (ast_true(debug)) {
210 ast_sockaddr_setnull(&log_addr);
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 "
221 static void global_reloaded(const char *object_type)
226 static const struct ast_sorcery_observer global_observer = {
227 .loaded = global_reloaded
230 static int load_module(void)
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;
239 ast_sip_register_service(&logging_module);
240 ast_cli_register_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
242 return AST_MODULE_LOAD_SUCCESS;
245 static int unload_module(void)
247 ast_cli_unregister_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
248 ast_sip_unregister_service(&logging_module);
250 ast_sorcery_observer_remove(
251 ast_sip_get_sorcery(), "global", &global_observer);
256 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Packet Logger",
258 .unload = unload_module,
259 .load_pri = AST_MODPRI_APP_DEPEND,