[PATCHv5 03/13] net/lwip: implement dns cmd

Maxim Uvarov
2023-08-02T14:09:55Z

Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
---
lib/lwip/Makefile | 2 ++
lib/lwip/apps/dns/lwip-dns.c | 46 ++++++++++++++++++++++++++++++++++++
lib/lwip/apps/dns/lwip-dns.h | 3 +++
3 files changed, 51 insertions(+)
create mode 100644 lib/lwip/apps/dns/lwip-dns.c
create mode 100644 lib/lwip/apps/dns/lwip-dns.h

diff --git a/lib/lwip/Makefile b/lib/lwip/Makefile
index 35f34d7afa..2815662093 100644
--- a/lib/lwip/Makefile
+++ b/lib/lwip/Makefile
@@ -64,3 +64,5 @@ obj-$(CONFIG_NET) += $(LWIPDIR)/netif/ethernet.o

obj-$(CONFIG_NET) += port/if.o
obj-$(CONFIG_NET) += port/sys-arch.o
+
+obj-$(CONFIG_CMD_DNS) += apps/dns/lwip-dns.o
diff --git a/lib/lwip/apps/dns/lwip-dns.c b/lib/lwip/apps/dns/lwip-dns.c
new file mode 100644
index 0000000000..04fd53bfcb
--- /dev/null
+++ b/lib/lwip/apps/dns/lwip-dns.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov@linaro.org>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <console.h>
+
+#include <lwip/dns.h>
+#include <lwip/ip_addr.h>
+
+#include "../../../lwip/ulwip.h"
+
+static void dns_found_cb(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
+{
+ char *varname = (char *)callback_arg;
+
+ if (varname)
+ env_set(varname, ip4addr_ntoa(ipaddr));
+
+ printf("resolved %s to %s\n", name, ip4addr_ntoa(ipaddr));
+ ulwip_exit(0);
+}
+
+int ulwip_dns(char *name, char *varname)
+{
+ int err;
+ ip_addr_t ipaddr; /* not used */
+ ip_addr_t dns1;
+ ip_addr_t dns2;
+
+ ipaddr_aton(env_get("dnsip"), &dns1);
+ ipaddr_aton(env_get("dnsip2"), &dns2);
+
+ dns_init();
+ dns_setserver(0, &dns1);
+ dns_setserver(1, &dns2);
+
+ err = dns_gethostbyname(name, &ipaddr, dns_found_cb, varname);
+ if (err == ERR_OK)
+ dns_found_cb(name, &ipaddr, varname);
+
+ return err;
+}
diff --git a/lib/lwip/apps/dns/lwip-dns.h b/lib/lwip/apps/dns/lwip-dns.h
new file mode 100644
index 0000000000..c59f99e099
--- /dev/null
+++ b/lib/lwip/apps/dns/lwip-dns.h
@@ -0,0 +1,3 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+int ulwip_dns(char *name, char *varname);
--
2.30.2