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 <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2025-06-10 15:29:53 +03:00 committed by Dan Kalowsky
commit d394b74a41

View file

@ -10,6 +10,8 @@ LOG_MODULE_DECLARE(net_shell);
#include <zephyr/net/socket.h>
#include <zephyr/net/dns_resolve.h>
#include <zephyr/net/dns_sd.h>
#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 <hostname> [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
);