You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by jo...@apache.org on 2017/07/11 01:39:24 UTC

[38/50] mesos git commit: Windows: Made `net.hpp` use Unicode.

Windows: Made `net.hpp` use Unicode.

This commit moves most of the shared `os::net` code into similar but
separate POSIX and Windows implementations, where the latter use the
explicit wide/Unicode Windows APIs.

Review: https://reviews.apache.org/r/60335/


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/498c5302
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/498c5302
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/498c5302

Branch: refs/heads/master
Commit: 498c530269f8574937cd33658c92102e7a9b05af
Parents: 52adff1
Author: Andrew Schwartzmeyer <an...@schwartzmeyer.com>
Authored: Mon Jul 10 14:56:52 2017 -0700
Committer: Joseph Wu <jo...@apache.org>
Committed: Mon Jul 10 17:15:38 2017 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/net.hpp         | 106 --------------------
 3rdparty/stout/include/stout/posix/net.hpp   | 108 ++++++++++++++++++++-
 3rdparty/stout/include/stout/windows/net.hpp | 113 ++++++++++++++++++++++
 3 files changed, 219 insertions(+), 108 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/498c5302/3rdparty/stout/include/stout/net.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/net.hpp b/3rdparty/stout/include/stout/net.hpp
index f9fbd51..7b6557d 100644
--- a/3rdparty/stout/include/stout/net.hpp
+++ b/3rdparty/stout/include/stout/net.hpp
@@ -193,112 +193,6 @@ inline Try<int> download(const std::string& url, const std::string& path)
   return Try<int>::some(code);
 }
 
-
-inline struct addrinfo createAddrInfo(int socktype, int family, int flags)
-{
-  struct addrinfo addr;
-  memset(&addr, 0, sizeof(addr));
-  addr.ai_socktype = socktype;
-  addr.ai_family = family;
-  addr.ai_flags |= flags;
-
-  return addr;
-}
-
-
-// Returns a Try of the hostname for the provided IP. If the hostname
-// cannot be resolved, then a string version of the IP address is
-// returned.
-//
-// TODO(benh): Merge with `net::hostname`.
-inline Try<std::string> getHostname(const IP& ip)
-{
-  struct sockaddr_storage storage;
-  memset(&storage, 0, sizeof(storage));
-
-  switch (ip.family()) {
-    case AF_INET: {
-      struct sockaddr_in addr;
-      memset(&addr, 0, sizeof(addr));
-      addr.sin_family = AF_INET;
-      addr.sin_addr = ip.in().get();
-      addr.sin_port = 0;
-
-      memcpy(&storage, &addr, sizeof(addr));
-      break;
-    }
-    case AF_INET6: {
-      struct sockaddr_in6 addr;
-      memset(&addr, 0, sizeof(addr));
-      addr.sin6_family = AF_INET6;
-      addr.sin6_addr = ip.in6().get();
-      addr.sin6_port = 0;
-
-      memcpy(&storage, &addr, sizeof(addr));
-      break;
-    }
-    default: {
-      ABORT("Unsupported family type: " + stringify(ip.family()));
-    }
-  }
-
-  char hostname[MAXHOSTNAMELEN];
-  socklen_t length;
-
-  if (ip.family() == AF_INET) {
-    length = sizeof(struct sockaddr_in);
-  } else if (ip.family() == AF_INET6) {
-    length = sizeof(struct sockaddr_in6);
-  } else {
-    return Error("Unknown address family: " + stringify(ip.family()));
-  }
-
-  int error = getnameinfo(
-      (struct sockaddr*) &storage,
-      length,
-      hostname,
-      MAXHOSTNAMELEN,
-      nullptr,
-      0,
-      0);
-
-  if (error != 0) {
-    return Error(std::string(gai_strerror(error)));
-  }
-
-  return std::string(hostname);
-}
-
-
-// Returns a Try of the IP for the provided hostname or an error if no IP is
-// obtained.
-inline Try<IP> getIP(const std::string& hostname, int family = AF_UNSPEC)
-{
-  struct addrinfo hints = createAddrInfo(SOCK_STREAM, family, 0);
-  struct addrinfo* result = nullptr;
-
-  int error = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
-
-  if (error != 0) {
-    return Error(gai_strerror(error));
-  }
-
-  if (result->ai_addr == nullptr) {
-    freeaddrinfo(result);
-    return Error("No addresses found");
-  }
-
-  Try<IP> ip = IP::create(*result->ai_addr);
-
-  if (ip.isError()) {
-    freeaddrinfo(result);
-    return Error("Unsupported family type");
-  }
-
-  freeaddrinfo(result);
-  return ip.get();
-}
-
 } // namespace net {
 
 #endif // __STOUT_NET_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/498c5302/3rdparty/stout/include/stout/posix/net.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/posix/net.hpp b/3rdparty/stout/include/stout/posix/net.hpp
index 841d9ca..b8ea601 100644
--- a/3rdparty/stout/include/stout/posix/net.hpp
+++ b/3rdparty/stout/include/stout/posix/net.hpp
@@ -25,6 +25,112 @@
 
 namespace net {
 
+inline struct addrinfo createAddrInfo(int socktype, int family, int flags)
+{
+  struct addrinfo addr;
+  memset(&addr, 0, sizeof(addr));
+  addr.ai_socktype = socktype;
+  addr.ai_family = family;
+  addr.ai_flags |= flags;
+
+  return addr;
+}
+
+
+// Returns a Try of the hostname for the provided IP. If the hostname
+// cannot be resolved, then a string version of the IP address is
+// returned.
+//
+// TODO(benh): Merge with `net::hostname`.
+inline Try<std::string> getHostname(const IP& ip)
+{
+  struct sockaddr_storage storage;
+  memset(&storage, 0, sizeof(storage));
+
+  switch (ip.family()) {
+    case AF_INET: {
+      struct sockaddr_in addr;
+      memset(&addr, 0, sizeof(addr));
+      addr.sin_family = AF_INET;
+      addr.sin_addr = ip.in().get();
+      addr.sin_port = 0;
+
+      memcpy(&storage, &addr, sizeof(addr));
+      break;
+    }
+    case AF_INET6: {
+      struct sockaddr_in6 addr;
+      memset(&addr, 0, sizeof(addr));
+      addr.sin6_family = AF_INET6;
+      addr.sin6_addr = ip.in6().get();
+      addr.sin6_port = 0;
+
+      memcpy(&storage, &addr, sizeof(addr));
+      break;
+    }
+    default: {
+      ABORT("Unsupported family type: " + stringify(ip.family()));
+    }
+  }
+
+  char hostname[MAXHOSTNAMELEN];
+  socklen_t length;
+
+  if (ip.family() == AF_INET) {
+    length = sizeof(struct sockaddr_in);
+  } else if (ip.family() == AF_INET6) {
+    length = sizeof(struct sockaddr_in6);
+  } else {
+    return Error("Unknown address family: " + stringify(ip.family()));
+  }
+
+  int error = getnameinfo(
+      (struct sockaddr*) &storage,
+      length,
+      hostname,
+      MAXHOSTNAMELEN,
+      nullptr,
+      0,
+      0);
+
+  if (error != 0) {
+    return Error(std::string(gai_strerror(error)));
+  }
+
+  return std::string(hostname);
+}
+
+
+// Returns a Try of the IP for the provided hostname or an error if no IP is
+// obtained.
+inline Try<IP> getIP(const std::string& hostname, int family = AF_UNSPEC)
+{
+  struct addrinfo hints = createAddrInfo(SOCK_STREAM, family, 0);
+  struct addrinfo* result = nullptr;
+
+  int error = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
+
+  if (error != 0) {
+    return Error(gai_strerror(error));
+  }
+
+  if (result->ai_addr == nullptr) {
+    freeaddrinfo(result);
+    return Error("No addresses found");
+  }
+
+  Try<IP> ip = IP::create(*result->ai_addr);
+
+  if (ip.isError()) {
+    freeaddrinfo(result);
+    return Error("Unsupported family type");
+  }
+
+  freeaddrinfo(result);
+  return ip.get();
+}
+
+
 // Returns the names of all the link devices in the system.
 inline Try<std::set<std::string>> links()
 {
@@ -45,8 +151,6 @@ inline Try<std::set<std::string>> links()
 }
 
 
-inline struct addrinfo createAddrInfo(int socktype, int family, int flags);
-
 inline Try<std::string> hostname()
 {
   char host[512];

http://git-wip-us.apache.org/repos/asf/mesos/blob/498c5302/3rdparty/stout/include/stout/windows/net.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/windows/net.hpp b/3rdparty/stout/include/stout/windows/net.hpp
index 6df4916..1418b5c 100644
--- a/3rdparty/stout/include/stout/windows/net.hpp
+++ b/3rdparty/stout/include/stout/windows/net.hpp
@@ -31,6 +31,119 @@
 
 namespace net {
 
+inline struct addrinfoW createAddrInfo(int socktype, int family, int flags)
+{
+  struct addrinfoW addr;
+  memset(&addr, 0, sizeof(addr));
+  addr.ai_socktype = socktype;
+  addr.ai_family = family;
+  addr.ai_flags |= flags;
+
+  return addr;
+}
+
+
+inline Error GaiError(int error)
+{
+  return Error(stringify(std::wstring(gai_strerrorW(error))));
+}
+
+
+// Returns a Try of the hostname for the provided IP. If the hostname
+// cannot be resolved, then a string version of the IP address is
+// returned.
+//
+// TODO(benh): Merge with `net::hostname`.
+inline Try<std::string> getHostname(const IP& ip)
+{
+  struct sockaddr_storage storage;
+  memset(&storage, 0, sizeof(storage));
+
+  switch (ip.family()) {
+    case AF_INET: {
+      struct sockaddr_in addr;
+      memset(&addr, 0, sizeof(addr));
+      addr.sin_family = AF_INET;
+      addr.sin_addr = ip.in().get();
+      addr.sin_port = 0;
+
+      memcpy(&storage, &addr, sizeof(addr));
+      break;
+    }
+    case AF_INET6: {
+      struct sockaddr_in6 addr;
+      memset(&addr, 0, sizeof(addr));
+      addr.sin6_family = AF_INET6;
+      addr.sin6_addr = ip.in6().get();
+      addr.sin6_port = 0;
+
+      memcpy(&storage, &addr, sizeof(addr));
+      break;
+    }
+    default: {
+      ABORT("Unsupported family type: " + stringify(ip.family()));
+    }
+  }
+
+  wchar_t hostname[MAXHOSTNAMELEN];
+  socklen_t length;
+
+  if (ip.family() == AF_INET) {
+    length = sizeof(struct sockaddr_in);
+  } else if (ip.family() == AF_INET6) {
+    length = sizeof(struct sockaddr_in6);
+  } else {
+    return Error("Unknown address family: " + stringify(ip.family()));
+  }
+
+  int error = GetNameInfoW(
+      (struct sockaddr*) &storage,
+      length,
+      hostname,
+      MAXHOSTNAMELEN,
+      nullptr,
+      0,
+      0);
+
+  if (error != 0) {
+    return GaiError(error);
+  }
+
+  return stringify(std::wstring(hostname));
+}
+
+
+// Returns a Try of the IP for the provided hostname or an error if no IP is
+// obtained.
+inline Try<IP> getIP(const std::string& hostname, int family = AF_UNSPEC)
+{
+  struct addrinfoW hints = createAddrInfo(SOCK_STREAM, family, 0);
+  struct addrinfoW* result = nullptr;
+
+  int error =
+      GetAddrInfoW(wide_stringify(hostname).data(), nullptr, &hints, &result);
+
+  if (error != 0) {
+    return GaiError(error);
+  }
+
+  if (result->ai_addr == nullptr) {
+    FreeAddrInfoW(result);
+    return Error("No addresses found");
+  }
+
+  Try<IP> ip = IP::create(*result->ai_addr);
+
+  if (ip.isError()) {
+    FreeAddrInfoW(result);
+    return Error("Unsupported family type");
+  }
+
+  FreeAddrInfoW(result);
+  return ip.get();
+}
+
+
 // Returns the names of all the link devices in the system.
 //
 // NOTE: On Windows, the device names are GUID's which are not easily