From 33519a0fb7bc28b6489ae5598e83befb689a712c Mon Sep 17 00:00:00 2001
From: Iryna Antsyferova <iryna.antsyferova@genexis.eu>
Date: Mon, 28 Oct 2024 14:52:03 +0100
Subject: [PATCH] Implement "pjsip show srv_lookups" CLI command, REF 15305

---
 res/res_pjsip/pjsip_resolver.c | 64 ++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/res/res_pjsip/pjsip_resolver.c b/res/res_pjsip/pjsip_resolver.c
index c90f635365..34e7257a15 100644
--- a/res/res_pjsip/pjsip_resolver.c
+++ b/res/res_pjsip/pjsip_resolver.c
@@ -24,6 +24,7 @@
 #include <arpa/nameser.h>
 
 #include "asterisk/astobj2.h"
+#include "asterisk/cli.h"
 #include "asterisk/dns_core.h"
 #include "asterisk/dns_query_set.h"
 #include "asterisk/dns_srv.h"
@@ -950,6 +951,63 @@ static void sip_check_transport(pj_pool_t *pool, pjsip_transport_type_e transpor
 	}
 }
 
+static char *cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	char addr[PJ_INET6_ADDRSTRLEN + 10];
+	struct host_track_entry *host = NULL;
+	char *port = NULL;
+	int idx;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "pjsip show srv_lookups";
+		e->usage = "Usage: pjsip show SRV DNS lookup records\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != 3) {
+		return CLI_SHOWUSAGE;
+	}
+
+	ast_cli(a->fd,
+		"<Host name......................................> "
+		"<IP address.................> <Port> <Transport.....> "
+		"<Weight> <Priority> <Expired at>\n"
+		"============================================================="
+		"============================================================="
+		"======\n");
+
+	AST_LIST_LOCK(&sip_resolve_track);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&sip_resolve_track, host, next) {
+		for (idx = 0; idx < host->addresses.count; ++idx) {
+			pj_sockaddr_print(&host->addresses.entry[idx].addr, addr, sizeof(addr), 3);
+			/* ipv6 is not supported */
+			if (!pj_gethostip(pj_AF_INET(), &host->addresses.entry[idx].addr)) {
+				if ((port = strstr(addr, ":"))) {
+					*port = '\0';
+					port++;
+				}
+			}
+			ast_cli(a->fd, "%-40.40s %-25.25s %-6s %-15.15s %8d %10d %-24s\n",
+				host->target.host, addr, host->target.port ? atoi(host->target.port) : (port ? port : 0),
+				pjsip_transport_get_type_desc(host->target.type),
+				host->addresses.entry[idx].weight,
+				host->addresses.entry[idx].priority,
+				ctime(&host->expiry));
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&sip_resolve_track);
+
+	return CLI_SUCCESS;
+}
+
+static struct ast_cli_entry cli_commands[] = {
+	AST_CLI_DEFINE(cli_show_tasks, "Show pjsip SRV DNS lookup records"),
+};
+
 /*! \brief External resolver implementation for PJSIP */
 static pjsip_ext_resolver ext_resolver = {
 	.resolve = sip_resolve,
@@ -991,6 +1049,7 @@ void ast_sip_initialize_resolver(void)
 {
 	/* Replace the existing PJSIP resolver with our own implementation */
 	ast_sip_push_task_wait_servant(NULL, sip_replace_resolver, NULL);
+	ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
 }
 
 #else
@@ -1002,3 +1061,8 @@ void ast_sip_initialize_resolver(void)
 }
 
 #endif
+
+int ast_sip_destroy_resolver(void)
+{
+	ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
+}
-- 
GitLab