Skip to content

Commit f7f912e

Browse files
committed
net: shell: dns: Print DNS server with network interface
If network interface is specified in the DNS server, then send the queries to the server via the network interface. Print this information in the server list. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 924eb67 commit f7f912e

File tree

1 file changed

+34
-7
lines changed
  • subsys/net/lib/shell

1 file changed

+34
-7
lines changed

subsys/net/lib/shell/dns.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <zephyr/logging/log.h>
99
LOG_MODULE_DECLARE(net_shell);
1010

11+
#include <zephyr/net/socket.h>
1112
#include <zephyr/net/dns_resolve.h>
1213

1314
#include "net_shell_private.h"
@@ -59,29 +60,55 @@ static void dns_result_cb(enum dns_resolve_status status,
5960
PR_WARNING("dns: Unhandled status %d received\n", status);
6061
}
6162

63+
static const char *printable_iface(const char *iface_name,
64+
const char *found,
65+
const char *not_found)
66+
{
67+
if (iface_name[0] != '\0') {
68+
return found;
69+
}
70+
71+
return not_found;
72+
}
73+
6274
static void print_dns_info(const struct shell *sh,
6375
struct dns_resolve_context *ctx)
6476
{
65-
int i;
77+
int i, ret;
6678

6779
PR("DNS servers:\n");
6880

6981
for (i = 0; i < CONFIG_DNS_RESOLVER_MAX_SERVERS +
7082
DNS_MAX_MCAST_SERVERS; i++) {
83+
char iface_name[IFNAMSIZ] = { 0 };
84+
85+
if (ctx->servers[i].if_index > 0) {
86+
ret = net_if_get_name(
87+
net_if_get_by_index(ctx->servers[i].if_index),
88+
iface_name, sizeof(iface_name));
89+
if (ret < 0) {
90+
snprintk(iface_name, sizeof(iface_name), "%d",
91+
ctx->servers[i].if_index);
92+
}
93+
}
94+
7195
if (ctx->servers[i].dns_server.sa_family == AF_INET) {
72-
PR("\t%s:%u\n",
96+
PR("\t%s:%u%s%s\n",
7397
net_sprint_ipv4_addr(
7498
&net_sin(&ctx->servers[i].dns_server)->
7599
sin_addr),
76-
ntohs(net_sin(
77-
&ctx->servers[i].dns_server)->sin_port));
100+
ntohs(net_sin(&ctx->servers[i].dns_server)->sin_port),
101+
printable_iface(iface_name, " via ", ""),
102+
printable_iface(iface_name, iface_name, ""));
103+
78104
} else if (ctx->servers[i].dns_server.sa_family == AF_INET6) {
79-
PR("\t[%s]:%u\n",
105+
PR("\t[%s]:%u%s%s\n",
80106
net_sprint_ipv6_addr(
81107
&net_sin6(&ctx->servers[i].dns_server)->
82108
sin6_addr),
83-
ntohs(net_sin6(
84-
&ctx->servers[i].dns_server)->sin6_port));
109+
ntohs(net_sin6(&ctx->servers[i].dns_server)->sin6_port),
110+
printable_iface(iface_name, " via ", ""),
111+
printable_iface(iface_name, iface_name, ""));
85112
}
86113
}
87114

0 commit comments

Comments
 (0)