btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PLATFORM_API_NETWORK_INTERFACE_H_ |
| 6 | #define PLATFORM_API_NETWORK_INTERFACE_H_ |
| 7 | |
Yuri Wiitala | 82edd20 | 2018-10-31 18:42:58 -0700 | [diff] [blame] | 8 | #include <string> |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Jordan Bayles | f1e4bb7 | 2019-05-01 12:38:39 -0700 | [diff] [blame^] | 11 | #include "osp_base/ip_address.h" |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 12 | |
| 13 | namespace openscreen { |
| 14 | namespace platform { |
| 15 | |
Yuri Wiitala | 82edd20 | 2018-10-31 18:42:58 -0700 | [diff] [blame] | 16 | // On Linux, these are a signed 32-bit integer. However, on Mac, they are an |
| 17 | // unsigned 32-bit integer. Thus, for cross-platform compatibility, and to |
| 18 | // provide a special "invalid/not set" value, define the type as a 64-bit signed |
| 19 | // integer. |
Jordan Bayles | f227861 | 2019-01-22 13:19:34 -0800 | [diff] [blame] | 20 | using NetworkInterfaceIndex = int64_t; |
| 21 | enum : NetworkInterfaceIndex { kInvalidNetworkInterfaceIndex = -1 }; |
Yuri Wiitala | 82edd20 | 2018-10-31 18:42:58 -0700 | [diff] [blame] | 22 | |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 23 | struct InterfaceInfo { |
| 24 | enum class Type { |
| 25 | kEthernet = 0, |
| 26 | kWifi, |
| 27 | kOther, |
| 28 | }; |
| 29 | |
btolsch | 79998fe | 2018-09-18 14:58:09 -0700 | [diff] [blame] | 30 | // TODO(btolsch): Only needed until c++14. |
| 31 | InterfaceInfo(); |
Jordan Bayles | f227861 | 2019-01-22 13:19:34 -0800 | [diff] [blame] | 32 | InterfaceInfo(NetworkInterfaceIndex index, |
btolsch | 79998fe | 2018-09-18 14:58:09 -0700 | [diff] [blame] | 33 | const uint8_t hardware_address[6], |
| 34 | const std::string& name, |
| 35 | Type type); |
| 36 | ~InterfaceInfo(); |
| 37 | |
| 38 | bool operator==(const InterfaceInfo& other) const; |
| 39 | bool operator!=(const InterfaceInfo& other) const; |
| 40 | |
btolsch | bd9ca07 | 2018-08-30 15:57:32 -0700 | [diff] [blame] | 41 | void CopyHardwareAddressTo(uint8_t x[6]) const; |
| 42 | |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 43 | // Interface index, typically as specified by the operating system, |
| 44 | // identifying this interface on the host machine. |
Jordan Bayles | f227861 | 2019-01-22 13:19:34 -0800 | [diff] [blame] | 45 | NetworkInterfaceIndex index = kInvalidNetworkInterfaceIndex; |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 46 | |
| 47 | // MAC address of the interface. All 0s if unavailable. |
| 48 | uint8_t hardware_address[6] = {}; |
| 49 | |
| 50 | // Interface name (e.g. eth0) if available. |
| 51 | std::string name; |
| 52 | |
| 53 | // Hardware type of the interface. |
| 54 | Type type = Type::kOther; |
| 55 | }; |
| 56 | |
btolsch | 56aa52a | 2018-09-10 11:40:12 -0700 | [diff] [blame] | 57 | struct IPSubnet { |
btolsch | 79998fe | 2018-09-18 14:58:09 -0700 | [diff] [blame] | 58 | // TODO(btolsch): Only needed until c++14. |
| 59 | IPSubnet(); |
| 60 | IPSubnet(const IPAddress& address, uint8_t prefix_length); |
| 61 | ~IPSubnet(); |
| 62 | |
btolsch | 56aa52a | 2018-09-10 11:40:12 -0700 | [diff] [blame] | 63 | IPAddress address; |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 64 | |
| 65 | // Prefix length of |address|, which is another way of specifying a subnet |
| 66 | // mask. For example, 192.168.0.10/24 is a common representation of the |
| 67 | // address 192.168.0.10 with a 24-bit prefix, and therefore a subnet mask of |
| 68 | // 255.255.255.0. |
| 69 | uint8_t prefix_length = 0; |
| 70 | }; |
| 71 | |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 72 | struct InterfaceAddresses { |
| 73 | InterfaceInfo info = {}; |
| 74 | |
btolsch | 56aa52a | 2018-09-10 11:40:12 -0700 | [diff] [blame] | 75 | // All IP addresses associated with the interface identified by |info|. |
| 76 | std::vector<IPSubnet> addresses; |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | // Returns at most one InterfaceAddresses per interface, so there will be no |
| 80 | // duplicate InterfaceInfo entries. |
| 81 | std::vector<InterfaceAddresses> GetInterfaceAddresses(); |
| 82 | |
Yuri Wiitala | 82edd20 | 2018-10-31 18:42:58 -0700 | [diff] [blame] | 83 | // Output, for logging. |
| 84 | std::ostream& operator<<(std::ostream& out, const InterfaceInfo& info); |
| 85 | std::ostream& operator<<(std::ostream& out, const IPSubnet& subnet); |
| 86 | std::ostream& operator<<(std::ostream& out, const InterfaceAddresses& ifas); |
| 87 | |
btolsch | 50905ba | 2018-07-25 14:12:32 -0700 | [diff] [blame] | 88 | } // namespace platform |
| 89 | } // namespace openscreen |
| 90 | |
| 91 | #endif // PLATFORM_API_NETWORK_INTERFACE_H_ |