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