net: introduce CONFIG_DNS
authorJerome Forissier <jerome.forissier@linaro.org>
Fri, 18 Jul 2025 10:48:48 +0000 (12:48 +0200)
committerJerome Forissier <jerome.forissier@linaro.org>
Mon, 18 Aug 2025 13:47:58 +0000 (15:47 +0200)
Introduce the DNS Kconfig symbol so that various network commands may
use host names without the dns command (CMD_DNS) being selected.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
CC: E Shattow <e@freeshell.de>
12 files changed:
cmd/Kconfig
cmd/lwip/dns.c
doc/usage/cmd/sntp.rst
doc/usage/cmd/wget.rst
include/net-legacy.h
net/Kconfig
net/Makefile
net/lwip/Makefile
net/lwip/dns.c [new file with mode: 0644]
net/lwip/net-lwip.c
net/net.c
net/wget.c

index 1a7dba2..29de857 100644 (file)
@@ -2117,7 +2117,7 @@ config CMD_DHCP
 
 config CMD_DNS
        bool "dns"
-       select PROT_DNS_LWIP if NET_LWIP
+       select DNS
        help
          Lookup the IP of a hostname
 
index b5fccc7..3eb698b 100644 (file)
@@ -2,115 +2,7 @@
 /* Copyright (C) 2024 Linaro Ltd. */
 
 #include <command.h>
-#include <console.h>
-#include <env.h>
-#include <lwip/dns.h>
-#include <lwip/timeouts.h>
 #include <net.h>
-#include <time.h>
 
 U_BOOT_CMD(dns, 3, 1, do_dns, "lookup the IP of a hostname",
           "hostname [envvar]");
-
-#define DNS_RESEND_MS 1000
-#define DNS_TIMEOUT_MS 10000
-
-struct dns_cb_arg {
-       ip_addr_t host_ipaddr;
-       const char *var;
-       bool done;
-};
-
-static void do_dns_tmr(void *arg)
-{
-       dns_tmr();
-}
-
-static void dns_cb(const char *name, const ip_addr_t *ipaddr, void *arg)
-{
-       struct dns_cb_arg *dns_cb_arg = arg;
-       char *ipstr = ip4addr_ntoa(ipaddr);
-
-       dns_cb_arg->done = true;
-
-       if (!ipaddr) {
-               printf("DNS: host not found\n");
-               dns_cb_arg->host_ipaddr.addr = 0;
-               return;
-       }
-
-       dns_cb_arg->host_ipaddr.addr = ipaddr->addr;
-
-       if (dns_cb_arg->var)
-               env_set(dns_cb_arg->var, ipstr);
-}
-
-static int dns_loop(struct udevice *udev, const char *name, const char *var)
-{
-       struct dns_cb_arg dns_cb_arg = { };
-       struct netif *netif;
-       ip_addr_t ipaddr;
-       ulong start;
-       int ret;
-
-       dns_cb_arg.var = var;
-
-       netif = net_lwip_new_netif(udev);
-       if (!netif)
-               return CMD_RET_FAILURE;
-
-       if (net_lwip_dns_init()) {
-               net_lwip_remove_netif(netif);
-               return CMD_RET_FAILURE;
-       }
-
-       dns_cb_arg.done = false;
-
-       ret = dns_gethostbyname(name, &ipaddr, dns_cb, &dns_cb_arg);
-
-       if (ret == ERR_OK) {
-               dns_cb(name, &ipaddr, &dns_cb_arg);
-       } else if (ret == ERR_INPROGRESS) {
-               start = get_timer(0);
-               sys_timeout(DNS_RESEND_MS, do_dns_tmr, NULL);
-               do {
-                       net_lwip_rx(udev, netif);
-                       if (dns_cb_arg.done)
-                               break;
-                       if (ctrlc()) {
-                               printf("\nAbort\n");
-                               break;
-                       }
-               } while (get_timer(start) < DNS_TIMEOUT_MS);
-               sys_untimeout(do_dns_tmr, NULL);
-       }
-
-       net_lwip_remove_netif(netif);
-
-       if (dns_cb_arg.done && dns_cb_arg.host_ipaddr.addr != 0) {
-               if (!var)
-                       printf("%s\n", ipaddr_ntoa(&ipaddr));
-               return CMD_RET_SUCCESS;
-       }
-
-       return CMD_RET_FAILURE;
-}
-
-int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
-{
-       char *name;
-       char *var = NULL;
-
-       if (argc == 1 || argc > 3)
-               return CMD_RET_USAGE;
-
-       name = argv[1];
-
-       if (argc == 3)
-               var = argv[2];
-
-       if (net_lwip_eth_start() < 0)
-               return CMD_RET_FAILURE;
-
-       return dns_loop(eth_get_dev(), name, var);
-}
index d97f830..2046828 100644 (file)
@@ -12,7 +12,7 @@ Synopsis
 ::
 
     sntp [serverip]
-    sntp [servername]  # NET_LWIP=y && CMD_DNS=y only
+    sntp [servername]  # NET_LWIP=y && DNS=y only
 
 
 Description
@@ -27,8 +27,8 @@ The address of the NTP server does not need to be given if the DHCP server
 provides one. The legacy network stack (`CONFIG_NET=y`) can only use the
 first NTP server provided in the `ntp-servers` DHCP option.
 
-When the network stack is lwIP (`CONFIG_NET_LWIP=y`) and the dns command
-is enabled (`CONFIG_CMD_DNS=y`), then the sntp command accepts a server
+When the network stack is lwIP (`CONFIG_NET_LWIP=y`) and DNS resolution
+is enabled (`CONFIG_DNS=y`), then the sntp command accepts a server
 name as an argument.
 
 The network time is sent as UTC. So, if you want to set the RTC to any local
@@ -61,7 +61,7 @@ Examples
     => date
     Date: 2025-06-16 (Monday)    Time: 17:19:57
 
-With `CONFIG_NET_LWIP=y` and `CONFIG_CMD_DNS=y`:
+With `CONFIG_NET_LWIP=y` and `CONFIG_DNS=y`:
 
 ::
 
index 06df284..8feda02 100644 (file)
@@ -38,7 +38,7 @@ address
     memory address for the data downloaded
 
 host
-    IP address (or host name if `CONFIG_CMD_DNS` is enabled) of the HTTP
+    IP address (or host name if `CONFIG_DNS` is enabled) of the HTTP
     server, defaults to the value of environment variable *serverip*.
 
 path
index 050fc35..9564e97 100644 (file)
@@ -315,7 +315,7 @@ extern u32  net_boot_file_size;
 /* Boot file size in blocks as reported by the DHCP server */
 extern u32     net_boot_file_expected_size_in_blocks;
 
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
 extern char *net_dns_resolve;          /* The host to resolve  */
 extern char *net_dns_env_var;          /* the env var to put the ip into */
 #endif
index 2450802..40ec6bb 100644 (file)
@@ -244,11 +244,17 @@ config NET_RANDOM_ETHADDR
          generated. It will be saved to the appropriate environment variable,
          too.
 
+config DNS
+       bool "Enable DNS resolutions"
+       select PROT_DNS_LWIP if NET_LWIP
+       help
+         Selecting this will allow the network stack to use server names
+         in addition to IP addresses.
+
 config WGET
        bool "Enable wget"
        select PROT_TCP if NET
        select PROT_TCP_LWIP if NET_LWIP
-       select PROT_DNS_LWIP if NET_LWIP
        help
          Selecting this will enable wget, an interface to send HTTP requests
          via the network stack.
index d63f62b..4688201 100644 (file)
@@ -10,7 +10,7 @@ ifeq ($(CONFIG_NET),y)
 obj-$(CONFIG_NET)      += arp.o
 obj-$(CONFIG_CMD_BOOTP) += bootp.o
 obj-$(CONFIG_CMD_CDP)  += cdp.o
-obj-$(CONFIG_CMD_DNS)  += dns.o
+obj-$(CONFIG_DNS)  += dns.o
 obj-$(CONFIG_CMD_LINK_LOCAL) += link_local.o
 obj-$(CONFIG_IPV6)     += ndisc.o
 obj-$(CONFIG_$(PHASE_)DM_ETH) += net.o
index 09c3b35..1b48ae4 100644 (file)
@@ -2,6 +2,7 @@ ccflags-y += -I$(srctree)/lib/lwip/lwip/src/include -I$(srctree)/lib/lwip/u-boot
 
 obj-$(CONFIG_$(PHASE_)DM_ETH) += net-lwip.o
 obj-$(CONFIG_CMD_DHCP) += dhcp.o
+obj-$(CONFIG_DNS) += dns.o
 obj-$(CONFIG_LWIP_ICMP_SHOW_UNREACH) += icmp_unreach.o
 obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o
 obj-$(CONFIG_WGET) += wget.o
diff --git a/net/lwip/dns.c b/net/lwip/dns.c
new file mode 100644 (file)
index 0000000..9964003
--- /dev/null
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Copyright (C) 2024 Linaro Ltd. */
+
+#include <command.h>
+#include <console.h>
+#include <env.h>
+#include <lwip/dns.h>
+#include <lwip/timeouts.h>
+#include <net.h>
+#include <time.h>
+
+#define DNS_RESEND_MS 1000
+#define DNS_TIMEOUT_MS 10000
+
+struct dns_cb_arg {
+       ip_addr_t host_ipaddr;
+       const char *var;
+       bool done;
+};
+
+static void do_dns_tmr(void *arg)
+{
+       dns_tmr();
+}
+
+static void dns_cb(const char *name, const ip_addr_t *ipaddr, void *arg)
+{
+       struct dns_cb_arg *dns_cb_arg = arg;
+       char *ipstr = ip4addr_ntoa(ipaddr);
+
+       dns_cb_arg->done = true;
+
+       if (!ipaddr) {
+               printf("DNS: host not found\n");
+               dns_cb_arg->host_ipaddr.addr = 0;
+               return;
+       }
+
+       dns_cb_arg->host_ipaddr.addr = ipaddr->addr;
+
+       if (dns_cb_arg->var)
+               env_set(dns_cb_arg->var, ipstr);
+}
+
+static int dns_loop(struct udevice *udev, const char *name, const char *var)
+{
+       struct dns_cb_arg dns_cb_arg = { };
+       struct netif *netif;
+       ip_addr_t ipaddr;
+       ulong start;
+       int ret;
+
+       dns_cb_arg.var = var;
+
+       netif = net_lwip_new_netif(udev);
+       if (!netif)
+               return CMD_RET_FAILURE;
+
+       if (net_lwip_dns_init()) {
+               net_lwip_remove_netif(netif);
+               return CMD_RET_FAILURE;
+       }
+
+       dns_cb_arg.done = false;
+
+       ret = dns_gethostbyname(name, &ipaddr, dns_cb, &dns_cb_arg);
+
+       if (ret == ERR_OK) {
+               dns_cb(name, &ipaddr, &dns_cb_arg);
+       } else if (ret == ERR_INPROGRESS) {
+               start = get_timer(0);
+               sys_timeout(DNS_RESEND_MS, do_dns_tmr, NULL);
+               do {
+                       net_lwip_rx(udev, netif);
+                       if (dns_cb_arg.done)
+                               break;
+                       if (ctrlc()) {
+                               printf("\nAbort\n");
+                               break;
+                       }
+               } while (get_timer(start) < DNS_TIMEOUT_MS);
+               sys_untimeout(do_dns_tmr, NULL);
+       }
+
+       net_lwip_remove_netif(netif);
+
+       if (dns_cb_arg.done && dns_cb_arg.host_ipaddr.addr != 0) {
+               if (!var)
+                       printf("%s\n", ipaddr_ntoa(&ipaddr));
+               return CMD_RET_SUCCESS;
+       }
+
+       return CMD_RET_FAILURE;
+}
+
+int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+       char *name;
+       char *var = NULL;
+
+       if (argc == 1 || argc > 3)
+               return CMD_RET_USAGE;
+
+       name = argv[1];
+
+       if (argc == 3)
+               var = argv[2];
+
+       if (net_lwip_eth_start() < 0)
+               return CMD_RET_FAILURE;
+
+       return dns_loop(eth_get_dev(), name, var);
+}
index 660ceb1..74cbc7e 100644 (file)
@@ -147,7 +147,7 @@ static int get_udev_ipv4_info(struct udevice *dev, ip4_addr_t *ip,
  */
 int net_lwip_dns_init(void)
 {
-#if CONFIG_IS_ENABLED(CMD_DNS)
+#if CONFIG_IS_ENABLED(DNS)
        bool has_server = false;
        ip_addr_t ns;
        char *nsenv;
@@ -364,7 +364,7 @@ int net_lwip_rx(struct udevice *udev, struct netif *netif)
  */
 int net_lwip_dns_resolve(char *name_or_ip, ip_addr_t *ip)
 {
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
        char *var = "_dnsres";
        char *argv[] = { "dns", name_or_ip, var, NULL };
        int argc = ARRAY_SIZE(argv) - 1;
@@ -373,7 +373,7 @@ int net_lwip_dns_resolve(char *name_or_ip, ip_addr_t *ip)
        if (ipaddr_aton(name_or_ip, ip))
                return 0;
 
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
        if (do_dns(NULL, 0, argc, argv) != CMD_RET_SUCCESS)
                return -1;
 
index ac9b65c..f579f6a 100644 (file)
--- a/net/net.c
+++ b/net/net.c
 #include "bootp.h"
 #include "cdp.h"
 #include "dhcpv6.h"
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
 #include "dns.h"
 #endif
 #include "link_local.h"
@@ -287,7 +287,7 @@ static int on_vlan(const char *name, const char *value, enum env_op op,
 }
 U_BOOT_ENV_CALLBACK(vlan, on_vlan);
 
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
 static int on_dnsip(const char *name, const char *value, enum env_op op,
        int flags)
 {
@@ -581,7 +581,7 @@ restart:
                        nc_start();
                        break;
 #endif
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
                case DNS:
                        dns_start();
                        break;
@@ -1506,7 +1506,7 @@ static int net_check_prereq(enum proto_t protocol)
                }
                goto common;
 #endif
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
        case DNS:
                if (net_dns_server.s_addr == 0) {
                        puts("*** ERROR: DNS server address not given\n");
@@ -1539,7 +1539,7 @@ static int net_check_prereq(enum proto_t protocol)
                        return 1;
                }
 #if    defined(CONFIG_CMD_PING) || \
-       defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
+       defined(CONFIG_DNS) || defined(CONFIG_PROT_UDP)
 common:
 #endif
                /* Fall through */
index 428ee07..d364295 100644 (file)
@@ -393,7 +393,7 @@ int wget_do_request(ulong dst_addr, char *uri)
        if (string_to_ip(host_name).s_addr) {
                s = host_name;
        } else {
-#if IS_ENABLED(CONFIG_CMD_DNS)
+#if IS_ENABLED(CONFIG_DNS)
                net_dns_resolve = host_name;
                net_dns_env_var = "httpserverip";
                if (net_loop(DNS) < 0) {