Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "test-netif-ip.hpp" |
| 27 | #include "core/global-io.hpp" |
| 28 | #include <ndn-cxx/net/network-monitor.hpp> |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace tests { |
| 32 | |
| 33 | std::vector<shared_ptr<const NetworkInterface>> |
| 34 | collectNetworkInterfaces(bool allowCached) |
| 35 | { |
| 36 | using ndn::net::NetworkMonitor; |
| 37 | |
| 38 | static std::vector<shared_ptr<const NetworkInterface>> cached; |
| 39 | // cached.empty() indicates there's no cached list of netifs. |
| 40 | // Although it could also mean a system without any network interface, this situation is rare |
| 41 | // because the loopback interface is present on almost all systems. |
| 42 | |
| 43 | if (!allowCached || cached.empty()) { |
| 44 | NetworkMonitor netmon(getGlobalIoService()); |
| 45 | if ((netmon.getCapabilities() & NetworkMonitor::CAP_ENUM) == 0) { |
| 46 | BOOST_THROW_EXCEPTION(NetworkMonitor::Error("NetworkMonitor::CAP_ENUM is unavailable")); |
| 47 | } |
| 48 | |
| 49 | netmon.onEnumerationCompleted.connect([] { getGlobalIoService().stop(); }); |
| 50 | getGlobalIoService().run(); |
| 51 | getGlobalIoService().reset(); |
| 52 | |
| 53 | cached = netmon.listNetworkInterfaces(); |
| 54 | } |
| 55 | |
| 56 | return cached; |
| 57 | } |
| 58 | |
| 59 | namespace { |
| 60 | |
| 61 | template<typename E> |
| 62 | bool |
| 63 | matchTristate(E e, bool b) |
| 64 | { |
| 65 | return (e == E::DontCare) || |
| 66 | (e == E::Yes && b) || |
| 67 | (e == E::No && !b); |
| 68 | } |
| 69 | |
| 70 | template<AddressFamily AF> |
| 71 | typename IpAddressFromFamily<AF>::type |
| 72 | fromIpAddress(const boost::asio::ip::address& a); |
| 73 | |
| 74 | template<> |
| 75 | IpAddressFromFamily<AddressFamily::V4>::type |
| 76 | fromIpAddress<AddressFamily::V4>(const boost::asio::ip::address& a) |
| 77 | { |
| 78 | return a.to_v4(); |
| 79 | } |
| 80 | |
| 81 | template<> |
| 82 | IpAddressFromFamily<AddressFamily::V6>::type |
| 83 | fromIpAddress<AddressFamily::V6>(const boost::asio::ip::address& a) |
| 84 | { |
| 85 | return a.to_v6(); |
| 86 | } |
| 87 | |
| 88 | } // unnamed namespace |
| 89 | |
| 90 | template<AddressFamily AF> |
| 91 | typename IpAddressFromFamily<AF>::type |
| 92 | getTestIp(LoopbackAddress loopback, MulticastInterface mcast) |
| 93 | { |
| 94 | for (const auto& interface : collectNetworkInterfaces()) { |
| 95 | if (!interface->isUp() || |
| 96 | !matchTristate(loopback, interface->isLoopback()) || |
| 97 | !matchTristate(mcast, interface->canMulticast())) { |
| 98 | continue; |
| 99 | } |
| 100 | for (const auto& address : interface->getNetworkAddresses()) { |
| 101 | if (isAddressFamily<AF>(address) && |
| 102 | !address.getIp().is_unspecified() && |
| 103 | address.getScope() != ndn::net::AddressScope::NOWHERE && |
| 104 | address.getScope() != ndn::net::AddressScope::LINK && // link-local addresses are not supported yet (#1428) |
| 105 | matchTristate(loopback, address.getIp().is_loopback())) { |
| 106 | return fromIpAddress<AF>(address.getIp()); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return {}; |
| 111 | } |
| 112 | |
| 113 | template |
| 114 | IpAddressFromFamily<AddressFamily::V4>::type |
| 115 | getTestIp<AddressFamily::V4>(LoopbackAddress loopback, MulticastInterface mcast); |
| 116 | |
| 117 | template |
| 118 | IpAddressFromFamily<AddressFamily::V6>::type |
| 119 | getTestIp<AddressFamily::V6>(LoopbackAddress loopback, MulticastInterface mcast); |
| 120 | |
| 121 | } // namespace tests |
| 122 | } // namespace nfd |