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/06/06 02:32:48 UTC

[3/4] mesos git commit: Windows: Ported the `net::mac` helper.

Windows: Ported the `net::mac` helper.

The Windows implementation is effectively the same as the Posix
implementation, except that the loopback interface does not show
up as a link device on Windows.

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


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

Branch: refs/heads/master
Commit: 0a1e94d8471a7ecee09af51977ae9c5128f19ef8
Parents: 5a7c8d8
Author: Jeff Coffler <je...@taltos.com>
Authored: Mon Jun 5 17:25:06 2017 -0700
Committer: Joseph Wu <jo...@apache.org>
Committed: Mon Jun 5 18:39:46 2017 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/Makefile.am           |  2 +
 3rdparty/stout/include/stout/mac.hpp         | 90 ++++------------------
 3rdparty/stout/include/stout/posix/mac.hpp   | 92 +++++++++++++++++++++++
 3rdparty/stout/include/stout/windows/mac.hpp | 88 ++++++++++++++++++++++
 4 files changed, 197 insertions(+), 75 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/0a1e94d8/3rdparty/stout/include/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/Makefile.am b/3rdparty/stout/include/Makefile.am
index a287226..eeb054b 100644
--- a/3rdparty/stout/include/Makefile.am
+++ b/3rdparty/stout/include/Makefile.am
@@ -181,6 +181,7 @@ nobase_include_HEADERS =			\
   stout/posix/dynamiclibrary.hpp		\
   stout/posix/fs.hpp				\
   stout/posix/ip.hpp				\
+  stout/posix/mac.hpp				\
   stout/posix/net.hpp				\
   stout/posix/os.hpp				\
   stout/recordio.hpp				\
@@ -209,5 +210,6 @@ nobase_include_HEADERS =			\
   stout/windows/format.hpp			\
   stout/windows/fs.hpp				\
   stout/windows/ip.hpp				\
+  stout/windows/mac.hpp				\
   stout/windows/net.hpp				\
   stout/windows/os.hpp

http://git-wip-us.apache.org/repos/asf/mesos/blob/0a1e94d8/3rdparty/stout/include/stout/mac.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/mac.hpp b/3rdparty/stout/include/stout/mac.hpp
index 7ed5f38..298cc14 100644
--- a/3rdparty/stout/include/stout/mac.hpp
+++ b/3rdparty/stout/include/stout/mac.hpp
@@ -13,9 +13,6 @@
 #ifndef __STOUT_MAC_HPP__
 #define __STOUT_MAC_HPP__
 
-#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
-#include <ifaddrs.h>
-#endif
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
@@ -47,13 +44,13 @@
 #include <iostream>
 #include <string>
 
-#include "abort.hpp"
-#include "error.hpp"
-#include "none.hpp"
-#include "result.hpp"
-#include "stringify.hpp"
-#include "strings.hpp"
-#include "try.hpp"
+#include <stout/abort.hpp>
+#include <stout/error.hpp>
+#include <stout/none.hpp>
+#include <stout/result.hpp>
+#include <stout/stringify.hpp>
+#include <stout/strings.hpp>
+#include <stout/try.hpp>
 
 
 // Network utilities.
@@ -176,72 +173,15 @@ inline std::ostream& operator<<(std::ostream& stream, const MAC& mac)
   return stream << buffer;
 }
 
+} // namespace net {
 
-// Returns the MAC address of a given link device. The link device is
-// specified using its name (e.g., eth0). Returns error if the link
-// device is not found. Returns none if the link device is found, but
-// does not have a MAC address (e.g., loopback).
-inline Result<MAC> mac(const std::string& name)
-{
-#if !defined(__linux__) && !defined(__APPLE__) && !defined(__FreeBSD__)
-  return Error("Not implemented");
-#else
-  struct ifaddrs* ifaddr = nullptr;
-  if (getifaddrs(&ifaddr) == -1) {
-    return ErrnoError();
-  }
-
-  // Indicates whether the link device is found or not.
-  bool found = false;
-
-  for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
-    if (ifa->ifa_name != nullptr && !strcmp(ifa->ifa_name, name.c_str())) {
-      found = true;
-
-# if defined(__linux__)
-     if (ifa->ifa_addr != nullptr && ifa->ifa_addr->sa_family == AF_PACKET) {
-        struct sockaddr_ll* link = (struct sockaddr_ll*) ifa->ifa_addr;
-
-        if (link->sll_halen == 6) {
-          struct ether_addr* addr = (struct ether_addr*) link->sll_addr;
-          MAC mac(addr->ether_addr_octet);
-
-          // Ignore if the address is 0 so that the results are
-          // consistent on both OSX and Linux.
-          if (stringify(mac) == "00:00:00:00:00:00") {
-            continue;
-          }
-
-          freeifaddrs(ifaddr);
-          return mac;
-        }
-      }
-# elif defined(__APPLE__)
-      if (ifa->ifa_addr != nullptr && ifa->ifa_addr->sa_family == AF_LINK) {
-        struct sockaddr_dl* link = (struct sockaddr_dl*) ifa->ifa_addr;
-
-        if (link->sdl_type == IFT_ETHER && link->sdl_alen == 6) {
-          struct ether_addr* addr = (struct ether_addr*) LLADDR(link);
-          MAC mac(addr->octet);
-
-          freeifaddrs(ifaddr);
-          return mac;
-        }
-      }
-# endif
-    }
-  }
-
-  freeifaddrs(ifaddr);
-
-  if (!found) {
-    return Error("Cannot find the link device");
-  }
-
-  return None();
-#endif
-}
 
-} // namespace net {
+// NOTE: These headers are placed here because the platform specific code
+// requires classes defined in this file.
+#ifdef __WINDOWS__
+#include <stout/windows/mac.hpp>
+#else
+#include <stout/posix/mac.hpp>
+#endif // __WINDOWS__
 
 #endif // __STOUT_MAC_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/0a1e94d8/3rdparty/stout/include/stout/posix/mac.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/posix/mac.hpp b/3rdparty/stout/include/stout/posix/mac.hpp
new file mode 100644
index 0000000..462d9d9
--- /dev/null
+++ b/3rdparty/stout/include/stout/posix/mac.hpp
@@ -0,0 +1,92 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//  http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_POSIX_MAC_HPP__
+#define __STOUT_POSIX_MAC_HPP__
+
+#include <ifaddrs.h>
+
+#include <sys/types.h>
+
+#include <stout/error.hpp>
+#include <stout/none.hpp>
+#include <stout/result.hpp>
+#include <stout/stringify.hpp>
+
+
+// Network utilities.
+namespace net {
+
+// Returns the MAC address of a given link device. The link device is
+// specified using its name (e.g., eth0). Returns error if the link
+// device is not found. Returns none if the link device is found, but
+// does not have a MAC address (e.g., loopback).
+inline Result<MAC> mac(const std::string& name)
+{
+  struct ifaddrs* ifaddr = nullptr;
+  if (getifaddrs(&ifaddr) == -1) {
+    return ErrnoError();
+  }
+
+  // Indicates whether the link device is found or not.
+  bool found = false;
+
+  for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
+    if (ifa->ifa_name != nullptr && !strcmp(ifa->ifa_name, name.c_str())) {
+      found = true;
+
+# if defined(__linux__)
+      if (ifa->ifa_addr != nullptr && ifa->ifa_addr->sa_family == AF_PACKET) {
+        struct sockaddr_ll* link = (struct sockaddr_ll*) ifa->ifa_addr;
+
+        if (link->sll_halen == 6) {
+          struct ether_addr* addr = (struct ether_addr*) link->sll_addr;
+          MAC mac(addr->ether_addr_octet);
+
+          // Ignore if the address is 0 so that the results are
+          // consistent on both OSX and Linux.
+          if (stringify(mac) == "00:00:00:00:00:00") {
+            continue;
+          }
+
+          freeifaddrs(ifaddr);
+          return mac;
+        }
+      }
+# elif defined(__APPLE__)
+      if (ifa->ifa_addr != nullptr && ifa->ifa_addr->sa_family == AF_LINK) {
+        struct sockaddr_dl* link = (struct sockaddr_dl*) ifa->ifa_addr;
+
+        if (link->sdl_type == IFT_ETHER && link->sdl_alen == 6) {
+          struct ether_addr* addr = (struct ether_addr*) LLADDR(link);
+          MAC mac(addr->octet);
+
+          freeifaddrs(ifaddr);
+          return mac;
+        }
+      }
+# endif
+    }
+  }
+
+  freeifaddrs(ifaddr);
+
+  if (!found) {
+    return Error("Cannot find the link device");
+  }
+
+  return None();
+}
+
+} // namespace net {
+
+#endif // __STOUT_POSIX_MAC_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/0a1e94d8/3rdparty/stout/include/stout/windows/mac.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/windows/mac.hpp b/3rdparty/stout/include/stout/windows/mac.hpp
new file mode 100644
index 0000000..09c4c96
--- /dev/null
+++ b/3rdparty/stout/include/stout/windows/mac.hpp
@@ -0,0 +1,88 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//  http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_WINDOWS_MAC_HPP__
+#define __STOUT_WINDOWS_MAC_HPP__
+
+#include <iphlpapi.h>
+#pragma comment(lib, "IPHLPAPI.lib")
+
+#include <algorithm>
+#include <vector>
+
+#include <stout/error.hpp>
+#include <stout/none.hpp>
+#include <stout/result.hpp>
+#include <stout/stringify.hpp>
+
+
+// Network utilities.
+namespace net {
+
+// Returns the MAC address of a given link device. The link device is
+// specified using its name. Returns an error if the link device is not found.
+inline Result<MAC> mac(const std::string& name)
+{
+  DWORD result;
+  ULONG size = 0;
+
+  // Indicates whether the link device is found or not.
+  bool found = false;
+
+  // Make an initial call to GetAdaptersInfo to get structure size.
+  if (GetAdaptersInfo(nullptr, &size) != ERROR_BUFFER_OVERFLOW) {
+    return WindowsError("Calling GetAdaptersInfo returned unexpected result");
+  }
+
+  std::vector<IP_ADAPTER_INFO> adapterInfo(size / sizeof(IP_ADAPTER_INFO));
+  result = GetAdaptersInfo(
+      static_cast<PIP_ADAPTER_INFO>(adapterInfo.data()),
+      &size);
+
+  if (result != NO_ERROR) {
+    return WindowsError(result, "GetAdaptersInfo failed");
+  }
+
+  foreach (const IP_ADAPTER_INFO& ip_adapter, adapterInfo) {
+    if (!strcmp(ip_adapter.AdapterName, name.c_str())) {
+      found = true;
+
+      if (ip_adapter.AddressLength == 6) {
+        // NOTE: Although the `AddressLength` is 6, the size of the
+        // `Address` byte buffer is usually larger than required, which
+        // means we need to copy the bytes before the `MAC` constructor
+        // will accept them.
+        uint8_t mac_buffer[6];
+        std::copy(ip_adapter.Address, ip_adapter.Address + 6, mac_buffer);
+        MAC mac(mac_buffer);
+
+        // Ignore if the address is 0 so that the results are
+        // consistent across all platforms.
+        if (stringify(mac) == "00:00:00:00:00:00") {
+          continue;
+        }
+
+        return mac;
+      }
+    }
+  }
+
+  if (!found) {
+    return Error("Cannot find the link device");
+  }
+
+  return None();
+}
+
+} // namespace net {
+
+#endif // __STOUT_WINDOWS_MAC_HPP__