1 // Copyright 2020 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/impl/network_interface.h" 6 7 namespace openscreen { 8 GetNetworkInterfaces()9std::vector<InterfaceInfo> GetNetworkInterfaces() { 10 std::vector<InterfaceInfo> interfaces = GetAllInterfaces(); 11 12 const auto new_end = std::remove_if( 13 interfaces.begin(), interfaces.end(), [](const InterfaceInfo& info) { 14 return info.type != InterfaceInfo::Type::kEthernet && 15 info.type != InterfaceInfo::Type::kWifi && 16 info.type != InterfaceInfo::Type::kOther; 17 }); 18 interfaces.erase(new_end, interfaces.end()); 19 20 return interfaces; 21 } 22 23 // Returns an InterfaceInfo associated with the system's loopback interface. GetLoopbackInterfaceForTesting()24absl::optional<InterfaceInfo> GetLoopbackInterfaceForTesting() { 25 const std::vector<InterfaceInfo> interfaces = GetAllInterfaces(); 26 auto it = std::find_if( 27 interfaces.begin(), interfaces.end(), [](const InterfaceInfo& info) { 28 return info.type == InterfaceInfo::Type::kLoopback && 29 std::find_if( 30 info.addresses.begin(), info.addresses.end(), 31 [](const IPSubnet& subnet) { 32 return subnet.address == IPAddress::kV4LoopbackAddress() || 33 subnet.address == IPAddress::kV6LoopbackAddress(); 34 }) != info.addresses.end(); 35 }); 36 37 if (it == interfaces.end()) { 38 return absl::nullopt; 39 } else { 40 return *it; 41 } 42 } 43 44 } // namespace openscreen 45