From d394b74a4106aa3efdec763b8aa765c26c27f003 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Tue, 10 Jun 2025 15:29:53 +0300 Subject: [PATCH] net: shell: dns: Add list command to show service records Add "net dns list" command that will show DNS SD records defined in the system. Signed-off-by: Jukka Rissanen --- subsys/net/lib/shell/dns.c | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/subsys/net/lib/shell/dns.c b/subsys/net/lib/shell/dns.c index 6c6285990d8..21ed509f953 100644 --- a/subsys/net/lib/shell/dns.c +++ b/subsys/net/lib/shell/dns.c @@ -10,6 +10,8 @@ LOG_MODULE_DECLARE(net_shell); #include #include +#include +#include "dns/dns_sd.h" #include "net_shell_private.h" @@ -256,6 +258,52 @@ static int cmd_net_dns(const struct shell *sh, size_t argc, char *argv[]) return 0; } +static int cmd_net_dns_list(const struct shell *sh, size_t argc, char *argv[]) +{ +#if defined(CONFIG_DNS_SD) +#define MAX_PORT_LEN 6 + char buf[MAX_PORT_LEN]; + int n_records = 0; + + DNS_SD_FOREACH(record) { + if (!dns_sd_rec_is_valid(record)) { + continue; + } + + if (n_records == 0) { + PR(" DNS service records\n"); + } + + ++n_records; + + if (record->port != NULL) { + snprintk(buf, sizeof(buf), "%u", ntohs(*record->port)); + } + + PR("[%2d] %s.%s%s%s%s%s%s%s\n", + n_records, + record->instance != NULL ? record->instance : "", + record->service != NULL ? record->service : "", + record->proto != NULL ? "." : "", + record->proto != NULL ? record->proto : "", + record->domain != NULL ? "." : "", + record->domain != NULL ? record->domain : "", + record->port != NULL ? ":" : "", + record->port != NULL ? buf : ""); + } + + if (n_records == 0) { + PR("No DNS service records found.\n"); + return 0; + } +#else + PR_INFO("DNS service discovery not supported. Set CONFIG_DNS_SD to " + "enable it.\n"); +#endif + + return 0; +} + SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dns, SHELL_CMD(cancel, NULL, "Cancel all pending requests.", cmd_net_dns_cancel), @@ -263,6 +311,9 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dns, "'net dns [A or AAAA]' queries IPv4 address " "(default) or IPv6 address for a host name.", cmd_net_dns_query), + SHELL_CMD(list, NULL, + "List local DNS service records.", + cmd_net_dns_list), SHELL_SUBCMD_SET_END );