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>
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"
36 enum pjsip_logging_mode {
37 LOGGING_MODE_DISABLED, /* No logging is enabled */
38 LOGGING_MODE_ENABLED, /* Logging is enabled */
41 static enum pjsip_logging_mode logging_mode;
42 static struct ast_sockaddr log_addr;
44 /*! \brief Return the first entry from ast_sockaddr_resolve filtered by address family
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
49 static int ast_sockaddr_resolve_first_af(struct ast_sockaddr *addr,
50 const char* name, int flag, int family)
52 struct ast_sockaddr *addrs;
55 addrs_cnt = ast_sockaddr_resolve(&addrs, name, flag, family);
60 ast_debug(1, "Multiple addresses, using the first one only\n");
63 ast_sockaddr_copy(addr, &addrs[0]);
69 /*! \brief See if we pass debug IP filter */
70 static inline int pjsip_log_test_addr(const char *address, int port)
72 struct ast_sockaddr test_addr;
73 if (logging_mode == LOGGING_MODE_DISABLED) {
77 /* A null logging address means we'll debug any address */
78 if (ast_sockaddr_isnull(&log_addr)) {
82 /* A null address was passed in. Just reject it. */
83 if (ast_strlen_zero(address)) {
87 ast_sockaddr_parse(&test_addr, address, PARSE_PORT_IGNORE);
88 ast_sockaddr_set_port(&test_addr, port);
90 /* If no port was specified for a debug address, just compare the
91 * addresses, otherwise compare the address and port
93 if (ast_sockaddr_port(&log_addr)) {
94 return !ast_sockaddr_cmp(&log_addr, &test_addr);
96 return !ast_sockaddr_cmp_addr(&log_addr, &test_addr);
100 static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
102 if (!pjsip_log_test_addr(tdata->tp_info.dst_name, tdata->tp_info.dst_port)) {
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);
116 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
118 if (!pjsip_log_test_addr(rdata->pkt_info.src_name, rdata->pkt_info.src_port)) {
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",
125 rdata->tp_info.transport->type_name,
126 rdata->pkt_info.src_name,
127 rdata->pkt_info.src_port,
128 rdata->pkt_info.packet);
132 static pjsip_module logging_module = {
133 .name = { "Logging Module", 14 },
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,
141 static char *pjsip_enable_logger_host(int fd, const char *arg)
143 if (ast_sockaddr_resolve_first_af(&log_addr, arg, 0, AST_AF_UNSPEC)) {
144 return CLI_SHOWUSAGE;
147 ast_cli(fd, "PJSIP Logging Enabled for host: %s\n", ast_sockaddr_stringify_addr(&log_addr));
148 logging_mode = LOGGING_MODE_ENABLED;
153 static char *pjsip_set_logger(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
157 if (cmd == CLI_INIT) {
158 e->command = "pjsip set logger {on|off|host}";
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"
166 } else if (cmd == CLI_GENERATE) {
170 what = a->argv[e->args - 1]; /* Guaranteed to exist */
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);
178 } else if (!strcasecmp(what, "off")) {
179 logging_mode = LOGGING_MODE_DISABLED;
180 ast_cli(a->fd, "PJSIP Logging disabled\n");
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]);
189 return CLI_SHOWUSAGE;
192 static struct ast_cli_entry cli_pjsip[] = {
193 AST_CLI_DEFINE(pjsip_set_logger, "Enable/Disable PJSIP Logger Output")
196 static int load_module(void)
198 ast_sip_register_service(&logging_module);
199 ast_cli_register_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
200 return AST_MODULE_LOAD_SUCCESS;
203 static int unload_module(void)
205 ast_cli_unregister_multiple(cli_pjsip, ARRAY_LEN(cli_pjsip));
206 ast_sip_unregister_service(&logging_module);
210 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Packet Logger",
212 .unload = unload_module,
213 .load_pri = AST_MODPRI_APP_DEPEND,