blob: 3471ba3adc3de2baf02547c037311a1d15cc2649 [file] [log] [blame]
Junxiao Shi84d62cb2017-07-12 16:15:18 +00001/* -*- 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
30namespace nfd {
31namespace tests {
32
33std::vector<shared_ptr<const NetworkInterface>>
34collectNetworkInterfaces(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
59namespace {
60
61template<typename E>
62bool
63matchTristate(E e, bool b)
64{
65 return (e == E::DontCare) ||
66 (e == E::Yes && b) ||
67 (e == E::No && !b);
68}
69
70template<AddressFamily AF>
71typename IpAddressFromFamily<AF>::type
72fromIpAddress(const boost::asio::ip::address& a);
73
74template<>
75IpAddressFromFamily<AddressFamily::V4>::type
76fromIpAddress<AddressFamily::V4>(const boost::asio::ip::address& a)
77{
78 return a.to_v4();
79}
80
81template<>
82IpAddressFromFamily<AddressFamily::V6>::type
83fromIpAddress<AddressFamily::V6>(const boost::asio::ip::address& a)
84{
85 return a.to_v6();
86}
87
88} // unnamed namespace
89
90template<AddressFamily AF>
91typename IpAddressFromFamily<AF>::type
92getTestIp(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
113template
114IpAddressFromFamily<AddressFamily::V4>::type
115getTestIp<AddressFamily::V4>(LoopbackAddress loopback, MulticastInterface mcast);
116
117template
118IpAddressFromFamily<AddressFamily::V6>::type
119getTestIp<AddressFamily::V6>(LoopbackAddress loopback, MulticastInterface mcast);
120
121} // namespace tests
122} // namespace nfd