Apache Mesos
mac.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_WINDOWS_MAC_HPP__
14 #define __STOUT_WINDOWS_MAC_HPP__
15 
16 #include <algorithm>
17 #include <vector>
18 
19 #include <stout/error.hpp>
20 #include <stout/none.hpp>
21 #include <stout/result.hpp>
22 #include <stout/stringify.hpp>
23 #include <stout/windows.hpp> // For `iphlpapi.h`.
24 
25 
26 // Network utilities.
27 namespace net {
28 
29 // Returns the MAC address of a given link device. The link device is
30 // specified using its name. Returns an error if the link device is not found.
31 inline Result<MAC> mac(const std::string& name)
32 {
33  DWORD result;
34  ULONG size = 0;
35 
36  // Indicates whether the link device is found or not.
37  bool found = false;
38 
39  // Make an initial call to GetAdaptersInfo to get structure size.
40  if (GetAdaptersInfo(nullptr, &size) != ERROR_BUFFER_OVERFLOW) {
41  return WindowsError("Calling GetAdaptersInfo returned unexpected result");
42  }
43 
44  std::vector<IP_ADAPTER_INFO> adapterInfo(size / sizeof(IP_ADAPTER_INFO));
45  result = GetAdaptersInfo(
46  static_cast<PIP_ADAPTER_INFO>(adapterInfo.data()),
47  &size);
48 
49  if (result != NO_ERROR) {
50  return WindowsError(result, "GetAdaptersInfo failed");
51  }
52 
53  foreach (const IP_ADAPTER_INFO& ip_adapter, adapterInfo) {
54  if (!strcmp(ip_adapter.AdapterName, name.c_str())) {
55  found = true;
56 
57  if (ip_adapter.AddressLength == 6) {
58  // NOTE: Although the `AddressLength` is 6, the size of the
59  // `Address` byte buffer is usually larger than required, which
60  // means we need to copy the bytes before the `MAC` constructor
61  // will accept them.
62  uint8_t mac_buffer[6];
63  std::copy(ip_adapter.Address, ip_adapter.Address + 6, mac_buffer);
64  MAC mac(mac_buffer);
65 
66  // Ignore if the address is 0 so that the results are
67  // consistent across all platforms.
68  if (stringify(mac) == "00:00:00:00:00:00") {
69  continue;
70  }
71 
72  return mac;
73  }
74  }
75  }
76 
77  if (!found) {
78  return Error("Cannot find the link device");
79  }
80 
81  return None();
82 }
83 
84 } // namespace net {
85 
86 #endif // __STOUT_WINDOWS_MAC_HPP__
Definition: errorbase.hpp:36
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:121
Definition: error.hpp:108
Definition: check.hpp:30
Definition: ip.hpp:70
Result< MAC > mac(const std::string &name)
Definition: mac.hpp:33
Definition: none.hpp:27
std::string stringify(int flags)
T copy(const T &t)
Definition: utils.hpp:21
constexpr const char * name
Definition: shell.hpp:43