From patchwork Mon Jun 17 15:33:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 804883 Delivered-To: patch@linaro.org Received: by 2002:adf:fb90:0:b0:360:93e7:1765 with SMTP id a16csp611611wrr; Mon, 17 Jun 2024 08:35:00 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW29MK6oiDYgfVZIPfGshyuhBv2GeIM4iPRZddyE9VNn+VwO/U/ZwyAAvEuWPT+LHH+FrrUYjn6jjOLEddRtzyV X-Google-Smtp-Source: AGHT+IGsPhPOng/vYVMq/Kwwur3W/WF3eNqmrb+q+/HwsUSlag806xSlVSF685H4sBabGEaZKwYm X-Received: by 2002:a50:8d45:0:b0:57c:6ba4:3d8a with SMTP id 4fb4d7f45d1cf-57cbd66341fmr6326978a12.10.1718638499867; Mon, 17 Jun 2024 08:34:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1718638499; cv=none; d=google.com; s=arc-20160816; b=Vn5T2dQPpW1t7iYgVIiX6smTyrz1ad9c9uAb90e2dbfHZiiDbNGa2QHyucCAa4cOB2 kagmUVh7n2ebebLsCEThO2zUd/NQx7BD85yRaRs1d+GiYKi8ipkZw+vKq42w+7IPw63A bIHL17B9G1Tl749T8RLM0uEcJPQVIQFkdhFZZxgAasa+X7Wu0z8uLBWLGRyEVPe1Wve6 gEdHo5j3vmdLNRQyG8S6Mkk/WY3P5lOGORiF4m0R24p0l3uF1yZLy2QRvm/NVW5CaIY1 LSqERhp99jLBBW24H5T/K/bQ5DkQ5/OZ1a1qGrBCve8iT120GCw6Vwt0E3BToN/UY8q/ ywHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2DjkQ5FcX0Lcg8810jjl75vE1EMobjhdgNckOlMDd4M=; fh=2NZSi0YJbXUV2aVmrYzP40c+UfdtOxyfPSK7nbHv47o=; b=yBeqVoZNrORR72pXWjvThbtp1zbVBJdz1LjYAwRS0/2ymz0DeXWKXwIZtZPY+Mo6Hs LL46VH8zvCsd/8Iy1YC2lo9E/13Gxb264wtjH5gBQWbKPmvx27uDwCMyqHElzE5gndzi qNhPRyaSekHmCJ2d09rHXqNbGCqbR0SErzEkFacn7twqDKANVvVtUqqtoAZA054DRs9B UCpYUOj1ACdCy1gOdCVv5FpJMIYpeww68kEH35AcIEjfIBE7O2JU0oQ/GHwfqG0/Z2fz fDM4PNaNQQBTZTFIAZHANw4lt0ZSj0il2dmo3dREo23FRNmfjtpvcFMmJmZfJfgYONH6 QECQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=js3x7Ql8; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57cb742ce42si4482938a12.336.2024.06.17.08.34.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jun 2024 08:34:59 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=js3x7Ql8; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DED2D8839C; Mon, 17 Jun 2024 17:33:48 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="js3x7Ql8"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BDC39883DA; Mon, 17 Jun 2024 17:33:47 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id B64A3883AE for ; Mon, 17 Jun 2024 17:33:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-4217a96de38so32812195e9.1 for ; Mon, 17 Jun 2024 08:33:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718638425; x=1719243225; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2DjkQ5FcX0Lcg8810jjl75vE1EMobjhdgNckOlMDd4M=; b=js3x7Ql8RprFUZP3+0uXneAVm2UhJjeZhfs31rDPpatKGS2hz1n4bOhmZ3VY2B0bYk 1+MpCN15t7X/Olrqo77AJfkhZ6Fhbv8YlvumzCHEZM32uINxMtzPF8gIkWVIT2zf5Cnk uwv0V3GHOZQsSm7+KYKmf9PbLKZwQNI39JNZiZ+rEfuXF3BXuoSr+DjidcL17UFL9y/n Ag6Qz3IbhAIL0IO82J1t+3MZ9dR6NgOu6reQIxutr0a1hcRsIcWbpGxo+HWRIuSlkSc3 mGKzQTjlB58E0hfWVkdkj8w/gviRiePviFK1syOOPQpFK3gShzqpHh3kDMOB9hvdPnkv DJRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718638425; x=1719243225; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2DjkQ5FcX0Lcg8810jjl75vE1EMobjhdgNckOlMDd4M=; b=UTFi1VkVErAGz2aiHKxE9TdQds6YS7PxwDNpFL3ZSKwBSe9df0jwBQESElG1CzXyPJ hBj8ceoOyJFVopWPJIsDNUiGAvT3zL8nQzSj/gC+R8BSOamA1QWHr9g7riErplTB1T8j m0up8RB944cELNlVoxUgu2szJaAPugPuueSBEkqqf003uB9Y65ULr6LU5YeO9AgoM7/m XquEwYbwTw7tHJBlnybcBN2OqktxT9I92eONXdHdxGvwykhMf//suUULJY8ti9Mtk8/J kjEJ+jdeZVb2Hcu1MDEUBHlLn+h0ekE2OM2Ym//EaAUrZg+oEcI7BbOpa4JIOlg3KN4s l3qw== X-Gm-Message-State: AOJu0YwtN4djXxgFzEbBc9zO1LC+e0kwHnEROWkNsqJ3H+6e8up6jjzX u9e6cja+ZIYR7CZbW3o4jG7onAIxHRVnLe6MlFIVryKsavnOzKL2oAkBKn6thcUhKRhwlJTpQDg yS5E= X-Received: by 2002:a05:600c:45cd:b0:421:7eff:efb8 with SMTP id 5b1f17b1804b1-423056e01dbmr82123945e9.31.1718638425071; Mon, 17 Jun 2024 08:33:45 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:6e75:be8f:56ae:2b5e]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-422f602f620sm161350895e9.19.2024.06.17.08.33.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jun 2024 08:33:44 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Javier Tia , Maxim Uvarov , Jerome Forissier , Tom Rini , Simon Glass , Mattijs Korpershoek , AKASHI Takahiro , Michal Simek , Francis Laniel , Peter Robinson Subject: [PATCH v4 08/14] net-lwip: add dns command Date: Mon, 17 Jun 2024 17:33:00 +0200 Message-Id: <481f1b6d4ca8925060af62186ad2cb6b25f7e4f8.1718638104.git.jerome.forissier@linaro.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add CMD_DNS when NET_LWIP is enabled to provide the dns command using lwIP. Signed-off-by: Jerome Forissier --- cmd/Kconfig | 6 +++ cmd/net-lwip.c | 8 ++++ include/net-lwip.h | 1 + net-lwip/Makefile | 1 + net-lwip/dns.c | 117 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 net-lwip/dns.c diff --git a/cmd/Kconfig b/cmd/Kconfig index 07cfe824e3f..6ef0b52cd34 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -2098,6 +2098,12 @@ config CMD_DHCP help Boot image via network using DHCP/TFTP protocol +config CMD_DNS + bool "dns" + select PROT_DNS_LWIP + help + Lookup the IP of a hostname + config CMD_PING bool "ping" select PROT_RAW_LWIP diff --git a/cmd/net-lwip.c b/cmd/net-lwip.c index feed719beef..c021da6a674 100644 --- a/cmd/net-lwip.c +++ b/cmd/net-lwip.c @@ -27,3 +27,11 @@ U_BOOT_CMD( "[loadAddress] [[hostIPaddr:]bootfilename]" ); #endif + +#if defined(CONFIG_CMD_DNS) +U_BOOT_CMD( + dns, 3, 1, do_dns, + "lookup the IP of a hostname", + "hostname [envvar]" +); +#endif diff --git a/include/net-lwip.h b/include/net-lwip.h index b34f53b4891..4d41b0208a3 100644 --- a/include/net-lwip.h +++ b/include/net-lwip.h @@ -12,6 +12,7 @@ void net_lwip_remove_netif(struct netif *netif); struct netif *net_lwip_get_netif(void); int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); +int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); int do_ping(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]); #endif /* __NET_LWIP_H__ */ diff --git a/net-lwip/Makefile b/net-lwip/Makefile index e68d4e24197..aa247859483 100644 --- a/net-lwip/Makefile +++ b/net-lwip/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_$(SPL_)DM_ETH) += ../net/eth_common.o obj-$(CONFIG_$(SPL_)DM_ETH) += ../net/eth-uclass.o obj-$(CONFIG_$(SPL_)DM_ETH) += net-lwip.o obj-$(CONFIG_CMD_DHCP) += dhcp.o +obj-$(CONFIG_CMD_DNS) += dns.o obj-$(CONFIG_CMD_PING) += ping.o obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o diff --git a/net-lwip/dns.c b/net-lwip/dns.c new file mode 100644 index 00000000000..081f0933715 --- /dev/null +++ b/net-lwip/dns.c @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Copyright (C) 2024 Linaro Ltd. */ + +#include +#include +#include +#include +#include +#include + +#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; + } + + if (dns_cb_arg->var) + env_set(dns_cb_arg->var, ipstr); + + printf("%s\n", ipstr); +} + +int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) +{ + struct dns_cb_arg dns_cb_arg = { }; + bool has_server = false; + struct netif *netif; + ip_addr_t ipaddr; + ip_addr_t ns; + ulong start; + char *nsenv; + char *name; + int ret; + + if (argc == 1 || argc > 3) + return CMD_RET_USAGE; + + if (argc >= 2) + name = argv[1]; + + if (argc == 3) + dns_cb_arg.var = argv[2]; + + netif = net_lwip_new_netif(); + if (!netif) + return -1; + + dns_init(); + + nsenv = env_get("dnsip"); + if (nsenv && ipaddr_aton(nsenv, &ns)) { + dns_setserver(0, &ns); + has_server = true; + } + + nsenv = env_get("dnsip2"); + if (nsenv && ipaddr_aton(nsenv, &ns)) { + dns_setserver(1, &ns); + has_server = true; + } + + if (!has_server) { + log_err("No valid name server (dnsip/dnsip2)\n"); + 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 { + eth_rx(); + if (dns_cb_arg.done) + break; + sys_check_timeouts(); + 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) + return CMD_RET_SUCCESS; + + return CMD_RET_FAILURE; +}