blob: c3a2acbb27010ac624304e5e1e37d28970c21a8c [file] [log] [blame]
btolsch50905ba2018-07-25 14:12:32 -07001// 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 Wiitala82edd202018-10-31 18:42:58 -07008#include <string>
btolsch50905ba2018-07-25 14:12:32 -07009#include <vector>
10
Jordan Baylesf1e4bb72019-05-01 12:38:39 -070011#include "osp_base/ip_address.h"
btolsch50905ba2018-07-25 14:12:32 -070012
13namespace openscreen {
14namespace platform {
15
Yuri Wiitala82edd202018-10-31 18:42:58 -070016// 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 Baylesf2278612019-01-22 13:19:34 -080020using NetworkInterfaceIndex = int64_t;
21enum : NetworkInterfaceIndex { kInvalidNetworkInterfaceIndex = -1 };
Yuri Wiitala82edd202018-10-31 18:42:58 -070022
btolsch50905ba2018-07-25 14:12:32 -070023struct InterfaceInfo {
24 enum class Type {
25 kEthernet = 0,
26 kWifi,
27 kOther,
28 };
29
btolsch79998fe2018-09-18 14:58:09 -070030 // TODO(btolsch): Only needed until c++14.
31 InterfaceInfo();
Jordan Baylesf2278612019-01-22 13:19:34 -080032 InterfaceInfo(NetworkInterfaceIndex index,
btolsch79998fe2018-09-18 14:58:09 -070033 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
btolschbd9ca072018-08-30 15:57:32 -070041 void CopyHardwareAddressTo(uint8_t x[6]) const;
42
btolsch50905ba2018-07-25 14:12:32 -070043 // Interface index, typically as specified by the operating system,
44 // identifying this interface on the host machine.
Jordan Baylesf2278612019-01-22 13:19:34 -080045 NetworkInterfaceIndex index = kInvalidNetworkInterfaceIndex;
btolsch50905ba2018-07-25 14:12:32 -070046
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
btolsch56aa52a2018-09-10 11:40:12 -070057struct IPSubnet {
btolsch79998fe2018-09-18 14:58:09 -070058 // TODO(btolsch): Only needed until c++14.
59 IPSubnet();
60 IPSubnet(const IPAddress& address, uint8_t prefix_length);
61 ~IPSubnet();
62
btolsch56aa52a2018-09-10 11:40:12 -070063 IPAddress address;
btolsch50905ba2018-07-25 14:12:32 -070064
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
btolsch50905ba2018-07-25 14:12:32 -070072struct InterfaceAddresses {
73 InterfaceInfo info = {};
74
btolsch56aa52a2018-09-10 11:40:12 -070075 // All IP addresses associated with the interface identified by |info|.
76 std::vector<IPSubnet> addresses;
btolsch50905ba2018-07-25 14:12:32 -070077};
78
79// Returns at most one InterfaceAddresses per interface, so there will be no
80// duplicate InterfaceInfo entries.
81std::vector<InterfaceAddresses> GetInterfaceAddresses();
82
Yuri Wiitala82edd202018-10-31 18:42:58 -070083// Output, for logging.
84std::ostream& operator<<(std::ostream& out, const InterfaceInfo& info);
85std::ostream& operator<<(std::ostream& out, const IPSubnet& subnet);
86std::ostream& operator<<(std::ostream& out, const InterfaceAddresses& ifas);
87
btolsch50905ba2018-07-25 14:12:32 -070088} // namespace platform
89} // namespace openscreen
90
91#endif // PLATFORM_API_NETWORK_INTERFACE_H_