net: lwip: move dns init to common function

move the dns init including setting the dns servers from env vars to a
common function as other commands that support hostname lookups will
need this.

Signed-off-by: Tim Harvey <tharvey@gateworks.com>
[jf: add CMD_DNS conditional to support NET_LWIP && !CMD_DNS]
Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
diff --git a/net/lwip/net-lwip.c b/net/lwip/net-lwip.c
index ff4d634..040b51e 100644
--- a/net/lwip/net-lwip.c
+++ b/net/lwip/net-lwip.c
@@ -8,6 +8,7 @@
 #include <dm/uclass.h>
 #include <hexdump.h>
 #include <lwip/ip4_addr.h>
+#include <lwip/dns.h>
 #include <lwip/err.h>
 #include <lwip/netif.h>
 #include <lwip/pbuf.h>
@@ -141,6 +142,40 @@
 }
 
 /*
+ * Initialize DNS via env
+ */
+int net_lwip_dns_init(void)
+{
+#if CONFIG_IS_ENABLED(CMD_DNS)
+	bool has_server = false;
+	ip_addr_t ns;
+	char *nsenv;
+
+	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");
+		return -EINVAL;
+	}
+
+	return 0;
+#else
+	log_err("DNS disabled\n");
+	return -EINVAL;
+#endif
+}
+
+/*
  * Initialize the network stack if needed and start the current device if valid
  */
 int net_lwip_eth_start(void)