Skip to content

Commit d394b74

Browse files
jukkardkalowsk
authored andcommitted
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>
1 parent ff805ae commit d394b74

File tree

1 file changed

+51
-0
lines changed
  • subsys/net/lib/shell

1 file changed

+51
-0
lines changed

subsys/net/lib/shell/dns.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ LOG_MODULE_DECLARE(net_shell);
1010

1111
#include <zephyr/net/socket.h>
1212
#include <zephyr/net/dns_resolve.h>
13+
#include <zephyr/net/dns_sd.h>
14+
#include "dns/dns_sd.h"
1315

1416
#include "net_shell_private.h"
1517

@@ -256,13 +258,62 @@ static int cmd_net_dns(const struct shell *sh, size_t argc, char *argv[])
256258
return 0;
257259
}
258260

261+
static int cmd_net_dns_list(const struct shell *sh, size_t argc, char *argv[])
262+
{
263+
#if defined(CONFIG_DNS_SD)
264+
#define MAX_PORT_LEN 6
265+
char buf[MAX_PORT_LEN];
266+
int n_records = 0;
267+
268+
DNS_SD_FOREACH(record) {
269+
if (!dns_sd_rec_is_valid(record)) {
270+
continue;
271+
}
272+
273+
if (n_records == 0) {
274+
PR(" DNS service records\n");
275+
}
276+
277+
++n_records;
278+
279+
if (record->port != NULL) {
280+
snprintk(buf, sizeof(buf), "%u", ntohs(*record->port));
281+
}
282+
283+
PR("[%2d] %s.%s%s%s%s%s%s%s\n",
284+
n_records,
285+
record->instance != NULL ? record->instance : "",
286+
record->service != NULL ? record->service : "",
287+
record->proto != NULL ? "." : "",
288+
record->proto != NULL ? record->proto : "",
289+
record->domain != NULL ? "." : "",
290+
record->domain != NULL ? record->domain : "",
291+
record->port != NULL ? ":" : "",
292+
record->port != NULL ? buf : "");
293+
}
294+
295+
if (n_records == 0) {
296+
PR("No DNS service records found.\n");
297+
return 0;
298+
}
299+
#else
300+
PR_INFO("DNS service discovery not supported. Set CONFIG_DNS_SD to "
301+
"enable it.\n");
302+
#endif
303+
304+
return 0;
305+
}
306+
259307
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_dns,
260308
SHELL_CMD(cancel, NULL, "Cancel all pending requests.",
261309
cmd_net_dns_cancel),
262310
SHELL_CMD(query, NULL,
263311
"'net dns <hostname> [A or AAAA]' queries IPv4 address "
264312
"(default) or IPv6 address for a host name.",
265313
cmd_net_dns_query),
314+
SHELL_CMD(list, NULL,
315+
"List local DNS service records.",
316+
cmd_net_dns_list),
266317
SHELL_SUBCMD_SET_END
267318
);
268319

0 commit comments

Comments
 (0)