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 #include "platform/base/interface_info.h"
6
7 #include <algorithm>
8 #include <utility>
9
10 namespace openscreen {
11
12 InterfaceInfo::InterfaceInfo() = default;
InterfaceInfo(NetworkInterfaceIndex index,const uint8_t hardware_address[6],std::string name,Type type,std::vector<IPSubnet> addresses)13 InterfaceInfo::InterfaceInfo(NetworkInterfaceIndex index,
14 const uint8_t hardware_address[6],
15 std::string name,
16 Type type,
17 std::vector<IPSubnet> addresses)
18 : index(index),
19 hardware_address{hardware_address[0], hardware_address[1],
20 hardware_address[2], hardware_address[3],
21 hardware_address[4], hardware_address[5]},
22 name(std::move(name)),
23 type(type),
24 addresses(std::move(addresses)) {}
25 InterfaceInfo::~InterfaceInfo() = default;
26
27 IPSubnet::IPSubnet() = default;
IPSubnet(IPAddress address,uint8_t prefix_length)28 IPSubnet::IPSubnet(IPAddress address, uint8_t prefix_length)
29 : address(std::move(address)), prefix_length(prefix_length) {}
30 IPSubnet::~IPSubnet() = default;
31
GetIpAddressV4() const32 IPAddress InterfaceInfo::GetIpAddressV4() const {
33 for (const auto& address : addresses) {
34 if (address.address.IsV4()) {
35 return address.address;
36 }
37 }
38 return IPAddress{};
39 }
40
GetIpAddressV6() const41 IPAddress InterfaceInfo::GetIpAddressV6() const {
42 for (const auto& address : addresses) {
43 if (address.address.IsV6()) {
44 return address.address;
45 }
46 }
47 return IPAddress{};
48 }
49
HasHardwareAddress() const50 bool InterfaceInfo::HasHardwareAddress() const {
51 return std::any_of(hardware_address.begin(), hardware_address.end(),
52 [](uint8_t e) { return e != 0; });
53 }
54
operator <<(std::ostream & out,const IPSubnet & subnet)55 std::ostream& operator<<(std::ostream& out, const IPSubnet& subnet) {
56 if (subnet.address.IsV6()) {
57 out << '[';
58 }
59 out << subnet.address;
60 if (subnet.address.IsV6()) {
61 out << ']';
62 }
63 return out << '/' << std::dec << static_cast<int>(subnet.prefix_length);
64 }
65
operator <<(std::ostream & out,InterfaceInfo::Type type)66 std::ostream& operator<<(std::ostream& out, InterfaceInfo::Type type) {
67 switch (type) {
68 case InterfaceInfo::Type::kEthernet:
69 out << "Ethernet";
70 break;
71 case InterfaceInfo::Type::kWifi:
72 out << "Wifi";
73 break;
74 case InterfaceInfo::Type::kLoopback:
75 out << "Loopback";
76 break;
77 case InterfaceInfo::Type::kOther:
78 out << "Other";
79 break;
80 }
81
82 return out;
83 }
84
operator <<(std::ostream & out,const InterfaceInfo & info)85 std::ostream& operator<<(std::ostream& out, const InterfaceInfo& info) {
86 out << '{' << info.index << " (a.k.a. " << info.name
87 << "); media_type=" << info.type << "; MAC=" << std::hex
88 << static_cast<int>(info.hardware_address[0]);
89 for (size_t i = 1; i < info.hardware_address.size(); ++i) {
90 out << ':' << static_cast<int>(info.hardware_address[i]);
91 }
92 for (const IPSubnet& ip : info.addresses) {
93 out << "; " << ip;
94 }
95 return out << '}';
96 }
97
98 } // namespace openscreen
99