From 86c87875a78efc84b123cf1661a31cb06f240f59 Mon Sep 17 00:00:00 2001
From: Adam Ierymenko <adam.ierymenko@gmail.com>
Date: Mon, 20 Apr 2015 16:07:38 -0700
Subject: [PATCH] OSUtils::resolve()

---
 osdep/OSUtils.cpp | 30 ++++++++++++++++++++++++++++++
 osdep/OSUtils.hpp | 11 +++++++++++
 selftest.cpp      | 14 +++++++++++++-
 3 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp
index a8639a12f..0ff7bfc46 100644
--- a/osdep/OSUtils.cpp
+++ b/osdep/OSUtils.cpp
@@ -38,9 +38,11 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/uio.h>
 #include <dirent.h>
+#include <netdb.h>
 #endif
 
 #ifdef __WINDOWS__
@@ -174,6 +176,34 @@ int64_t OSUtils::getFileSize(const char *path)
 	return -1;
 }
 
+std::vector<InetAddress> OSUtils::resolve(const char *name)
+{
+	std::vector<InetAddress> r;
+	std::vector<InetAddress>::iterator i;
+	InetAddress tmp;
+	struct addrinfo *ai = (struct addrinfo *)0,*p;
+	if (!getaddrinfo(name,(const char *)0,(const struct addrinfo *)0,&ai)) {
+		try {
+			p = ai;
+			while (p) {
+				if ((p->ai_addr)&&((p->ai_addr->sa_family == AF_INET)||(p->ai_addr->sa_family == AF_INET6))) {
+					tmp = *(p->ai_addr);
+					for(i=r.begin();i!=r.end();++i) {
+						if (i->ipsEqual(tmp))
+							goto skip_add_inetaddr;
+					}
+					r.push_back(tmp);
+				}
+skip_add_inetaddr:
+				p = p->ai_next;
+			}
+		} catch ( ... ) {}
+		freeaddrinfo(ai);
+	}
+	std::sort(r.begin(),r.end());
+	return r;
+}
+
 bool OSUtils::readFile(const char *path,std::string &buf)
 {
 	char tmp[4096];
diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp
index 0cf4916b6..4422ab7d1 100644
--- a/osdep/OSUtils.hpp
+++ b/osdep/OSUtils.hpp
@@ -40,6 +40,7 @@
 #include <map>
 
 #include "../node/Constants.hpp"
+#include "../node/InetAddress.hpp"
 
 #ifdef __WINDOWS__
 #include <WinSock2.h>
@@ -146,6 +147,16 @@ public:
 	 */
 	static int64_t getFileSize(const char *path);
 
+	/**
+	 * Get IP (v4 and/or v6) addresses for a given host
+	 *
+	 * This is a blocking resolver.
+	 *
+	 * @param name Host name
+	 * @return IP addresses in InetAddress sort order or empty vector if not found
+	 */
+	static std::vector<InetAddress> resolve(const char *name);
+
 	/**
 	 * @return Current time in milliseconds since epoch
 	 */
diff --git a/selftest.cpp b/selftest.cpp
index 21e75924f..ea90813e5 100644
--- a/selftest.cpp
+++ b/selftest.cpp
@@ -758,7 +758,19 @@ static int testHttp()
 	std::map<std::string,std::string> requestHeaders,responseHeaders;
 	std::string responseBody;
 
-	InetAddress downloadZerotierDotCom("142.4.214.72/80");
+	InetAddress downloadZerotierDotCom;
+	std::vector<InetAddress> rr(OSUtils::resolve("download.zerotier.com"));
+	if (rr.empty()) {
+		std::cout << "[http] Resolve of download.zerotier.com failed, skipping." << std::endl;
+		return 0;
+	} else {
+		for(std::vector<InetAddress>::iterator r(rr.begin());r!=rr.end();++r) {
+			std::cout << "[http] download.zerotier.com: " << r->toString() << std::endl;
+			if (r->isV4())
+				downloadZerotierDotCom = *r;
+		}
+	}
+	downloadZerotierDotCom.setPort(80);
 
 	std::cout << "[http] GET http://download.zerotier.com/dev/1k @" << downloadZerotierDotCom.toString() << " ... "; std::cout.flush();
 	requestHeaders["Host"] = "download.zerotier.com";