[Asterisk-code-review] res_pjsip_logger: Add method-based logging option. (asterisk[master])

N A asteriskteam at digium.com
Wed Jul 20 19:07:00 CDT 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/18824 )


Change subject: res_pjsip_logger: Add method-based logging option.
......................................................................

res_pjsip_logger: Add method-based logging option.

Expands the pjsip logger to support the ability to filter
by SIP message type (INVITE, CANCEL, ACK, BYE, REGISTER,
and OPTION). This can make certain types of SIP debugging
easier by only logging messages of particular method(s).

ASTERISK-30146 #close

Change-Id: Idd03bd9b466b40e4bca7769437d52ac13a957cf9
---
A doc/CHANGES-staging/res_pjsip_logger_method.txt
M res/res_pjsip_logger.c
2 files changed, 105 insertions(+), 6 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/24/18824/1

diff --git a/doc/CHANGES-staging/res_pjsip_logger_method.txt b/doc/CHANGES-staging/res_pjsip_logger_method.txt
new file mode 100644
index 0000000..4add8a3
--- /dev/null
+++ b/doc/CHANGES-staging/res_pjsip_logger_method.txt
@@ -0,0 +1,5 @@
+Subject: res_pjsip_logger
+
+SIP messages can now be filtered by SIP method,
+allowing for more granular debugging to be done
+in the CLI.
diff --git a/res/res_pjsip_logger.c b/res/res_pjsip_logger.c
index 957020f..9f17dc8 100644
--- a/res/res_pjsip_logger.c
+++ b/res/res_pjsip_logger.c
@@ -99,6 +99,22 @@
 	uint16_t checksum;	/*! \brief Packet checksum, left uncalculated for our purposes */
 };
 
+/*! \brief PJSIP Method Logging */
+struct pjsip_method_logger {
+	/*! \brief Whether to log INVITEs */
+	unsigned int log_method_invite:1;
+	/*! \brief Whether to log CANCELs */
+	unsigned int log_method_cancel:1;
+	/*! \brief Whether to log ACKs */
+	unsigned int log_method_ack:1;
+	/*! \brief Whether to log BYEs */
+	unsigned int log_method_bye:1;
+	/*! \brief Whether to log REGISTERs */
+	unsigned int log_method_register:1;
+	/*! \brief Whether to log OPTIONs */
+	unsigned int log_method_option:1;
+};
+
 /*! \brief PJSIP Logging Session */
 struct pjsip_logger_session {
 	/*! \brief Explicit addresses or ranges being logged */
@@ -115,6 +131,8 @@
 	unsigned int log_to_verbose:1;
 	/*! \brief Whether to log to pcap or not */
 	unsigned int log_to_pcap:1;
+	/*! \brief Whether to log specific SIP methods */
+	struct pjsip_method_logger log_methods;
 };
 
 /*! \brief The default logger session */
@@ -149,7 +167,7 @@
 }
 
 /*! \brief See if we pass debug IP filter */
-static inline int pjsip_log_test_addr(const struct pjsip_logger_session *session, const char *address, int port)
+static inline int pjsip_log_test_addr(const struct pjsip_logger_session *session, const char *address, int port, const pjsip_method *method)
 {
 	struct ast_sockaddr test_addr;
 
@@ -161,6 +179,26 @@
 		return 1;
 	}
 
+	/* If logging specific SIP methods, do so. */
+	if (session->log_methods.log_method_invite && !pjsip_method_cmp(method, &pjsip_invite_method)) {
+		return 1;
+	}
+	if (session->log_methods.log_method_cancel && !pjsip_method_cmp(method, &pjsip_cancel_method)) {
+		return 1;
+	}
+	if (session->log_methods.log_method_ack && !pjsip_method_cmp(method, &pjsip_ack_method)) {
+		return 1;
+	}
+	if (session->log_methods.log_method_bye && !pjsip_method_cmp(method, &pjsip_bye_method)) {
+		return 1;
+	}
+	if (session->log_methods.log_method_register && !pjsip_method_cmp(method, &pjsip_register_method)) {
+		return 1;
+	}
+	if (session->log_methods.log_method_option && !pjsip_method_cmp(method, &pjsip_options_method)) {
+		return 1;
+	}
+
 	/* A null address was passed in or no explicit matches. Just reject it. */
 	if (ast_strlen_zero(address) || !session->matches) {
 		return 0;
@@ -270,7 +308,7 @@
 	char buffer[AST_SOCKADDR_BUFLEN];
 
 	ao2_rdlock(default_logger);
-	if (!pjsip_log_test_addr(default_logger, tdata->tp_info.dst_name, tdata->tp_info.dst_port)) {
+	if (!pjsip_log_test_addr(default_logger, tdata->tp_info.dst_name, tdata->tp_info.dst_port, &tdata->msg->line.req.method)) {
 		ao2_unlock(default_logger);
 		return PJ_SUCCESS;
 	}
@@ -302,7 +340,7 @@
 	}
 
 	ao2_rdlock(default_logger);
-	if (!pjsip_log_test_addr(default_logger, rdata->pkt_info.src_name, rdata->pkt_info.src_port)) {
+	if (!pjsip_log_test_addr(default_logger, rdata->pkt_info.src_name, rdata->pkt_info.src_port, &rdata->msg_info.msg->line.req.method)) {
 		ao2_unlock(default_logger);
 		return PJ_FALSE;
 	}
@@ -393,6 +431,59 @@
 	return CLI_SUCCESS;
 }
 
+static char *pjsip_enable_logger_method(int fd, const char *arg)
+{
+	int added = 0;
+	const char *methods = arg;
+
+	ao2_wrlock(default_logger);
+	default_logger->enabled = 1;
+
+	/* Remove what already exists */
+	memset(&default_logger->log_methods, 0, sizeof(default_logger->log_methods));
+
+	if (strcasestr(methods, "INVITE")) {
+		default_logger->log_methods.log_method_invite = 1;
+		added++;
+	}
+	if (strcasestr(methods, "CANCEL")) {
+		default_logger->log_methods.log_method_cancel = 1;
+		added++;
+	}
+	if (strcasestr(methods, "ACK")) {
+		default_logger->log_methods.log_method_ack = 1;
+		added++;
+	}
+	if (strcasestr(methods, "BYE")) {
+		default_logger->log_methods.log_method_bye = 1;
+		added++;
+	}
+	if (strcasestr(methods, "REGISTER")) {
+		default_logger->log_methods.log_method_register = 1;
+		added++;
+	}
+	if (strcasestr(methods, "OPTION")) {
+		default_logger->log_methods.log_method_option = 1;
+		added++;
+	}
+
+	if (!added) {
+		if (fd >= 0) {
+			ast_cli(fd, "Failed to add method '%s' for logging\n", methods);
+		}
+		ao2_unlock(default_logger);
+		return CLI_SUCCESS;
+	}
+
+	ao2_unlock(default_logger);
+
+	if (fd >= 0) {
+		ast_cli(fd, "PJSIP Logging Enabled for %d SIP Method%s\n", added, ESS(added));
+	}
+
+	return CLI_SUCCESS;
+}
+
 static char *pjsip_disable_logger(int fd)
 {
 	ao2_wrlock(default_logger);
@@ -403,6 +494,7 @@
 	default_logger->pcap_filename[0] = '\0';
 	default_logger->log_to_verbose = 1;
 	default_logger->log_to_pcap = 0;
+	memset(&default_logger->log_methods, 0, sizeof(default_logger->log_methods));
 
 	/* Stop logging to the PCAP file if active */
 	if (default_logger->pcap_file) {
@@ -472,13 +564,13 @@
 	const char *what;
 
 	if (cmd == CLI_INIT) {
-		e->command = "pjsip set logger {on|off|host|add|verbose|pcap}";
+		e->command = "pjsip set logger {on|off|host|add|method|verbose|pcap}";
 		e->usage =
-			"Usage: pjsip set logger {on|off|host <name/subnet>|add <name/subnet>|verbose <on/off>|pcap <filename>}\n"
+			"Usage: pjsip set logger {on|off|host <name/subnet>|add <name/subnet>|method <method[|method2...]>|verbose <on/off>|pcap <filename>}\n"
 			"       Enables or disabling logging of SIP packets\n"
 			"       read on ports bound to PJSIP transports either\n"
 			"       globally or enables logging for an individual\n"
-			"       host.\n";
+			"       host or particular SIP method(s).\n";
 		return NULL;
 	} else if (cmd == CLI_GENERATE) {
 		return NULL;
@@ -497,6 +589,8 @@
 			return pjsip_enable_logger_host(a->fd, a->argv[e->args], 0);
 		} else if (!strcasecmp(what, "add")) {
 			return pjsip_enable_logger_host(a->fd, a->argv[e->args], 1);
+		} else if (!strcasecmp(what, "method")) {
+			return pjsip_enable_logger_method(a->fd, a->argv[e->args]);
 		} else if (!strcasecmp(what, "verbose")) {
 			return pjsip_set_logger_verbose(a->fd, a->argv[e->args]);
 		} else if (!strcasecmp(what, "pcap")) {

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/18824
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Idd03bd9b466b40e4bca7769437d52ac13a957cf9
Gerrit-Change-Number: 18824
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220720/c6d765c5/attachment-0001.html>


More information about the asterisk-code-review mailing list