1 // Copyright 2019 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_BASE_INTERFACE_INFO_H_ 6 #define PLATFORM_BASE_INTERFACE_INFO_H_ 7 8 #include <stdint.h> 9 10 #include <string> 11 #include <vector> 12 13 #include "platform/base/ip_address.h" 14 15 namespace openscreen { 16 17 // Unique identifier, usually provided by the operating system, for identifying 18 // a specific network interface. This value is used with UdpSocket to join 19 // multicast groups, or to make multicast broadcasts. An implementation may 20 // choose to make these values anything its UdpSocket implementation will 21 // recognize. 22 using NetworkInterfaceIndex = int64_t; 23 enum : NetworkInterfaceIndex { kInvalidNetworkInterfaceIndex = -1 }; 24 25 struct IPSubnet { 26 IPAddress address; 27 28 // Prefix length of |address|, which is another way of specifying a subnet 29 // mask. For example, 192.168.0.10/24 is a common representation of the 30 // address 192.168.0.10 with a 24-bit prefix (this describes a range of IPv4 31 // addresses from 192.168.0.0 through 192.168.0.255). Likewise, for IPv6 32 // addresses such as 2001:db8::/96, the concept is the same (this specifies 33 // the range of addresses having the same leading 96 bits). 34 uint8_t prefix_length = 0; 35 36 IPSubnet(); 37 IPSubnet(IPAddress address, uint8_t prefix); 38 ~IPSubnet(); 39 }; 40 41 struct InterfaceInfo { 42 enum class Type : uint32_t { kEthernet = 0, kWifi, kLoopback, kOther }; 43 44 // Interface index, typically as specified by the operating system, 45 // identifying this interface on the host machine. 46 NetworkInterfaceIndex index = kInvalidNetworkInterfaceIndex; 47 48 // MAC address of the interface. All 0s if unavailable. 49 std::array<uint8_t, 6> hardware_address = {}; 50 51 // Interface name (e.g. eth0) if available. 52 std::string name; 53 54 // Hardware type of the interface. 55 Type type = Type::kOther; 56 57 // All IP addresses associated with the interface. 58 std::vector<IPSubnet> addresses; 59 60 // Returns an IPAddress of the given type associated with this network 61 // interface, or the false IPAddress if the associated address family is not 62 // supported on this interface. 63 IPAddress GetIpAddressV4() const; 64 IPAddress GetIpAddressV6() const; 65 66 InterfaceInfo(); 67 InterfaceInfo(NetworkInterfaceIndex index, 68 const uint8_t hardware_address[6], 69 std::string name, 70 Type type, 71 std::vector<IPSubnet> addresses); 72 ~InterfaceInfo(); 73 }; 74 75 // Human-readable output (e.g., for logging). 76 std::ostream& operator<<(std::ostream& out, InterfaceInfo::Type type); 77 std::ostream& operator<<(std::ostream& out, const IPSubnet& subnet); 78 std::ostream& operator<<(std::ostream& out, const InterfaceInfo& info); 79 80 } // namespace openscreen 81 82 #endif // PLATFORM_BASE_INTERFACE_INFO_H_ 83