blob: 51bd71ef11c88a9412c172fbbde1c5a4cff411d7 [file] [log] [blame]
Yanbiao Liac8b4ca2017-07-10 02:27:50 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-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 ndn-cxx library (NDN C++ library with eXperimental eXtensions).
12 *
13 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
14 * terms of the GNU Lesser General Public License as published by the Free Software
15 * Foundation, either version 3 of the License, or (at your option) any later version.
16 *
17 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 *
21 * You should have received copies of the GNU General Public License and GNU Lesser
22 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
23 * <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
26 */
27
28#include "collect-netifs.hpp"
29#include "net/network-monitor.hpp"
30
31#include <boost/asio/io_service.hpp>
32
33namespace ndn {
34namespace net {
35namespace tests {
36
37std::vector<shared_ptr<const NetworkInterface>>
38collectNetworkInterfaces(bool allowCached)
39{
40 static std::vector<shared_ptr<const NetworkInterface>> cached;
41 // cached.empty() indicates there's no cached list of netifs.
42 // Although it could also mean a system without any network interface, this situation is rare
43 // because the loopback interface is present on almost all systems.
44
45 if (!allowCached || cached.empty()) {
46 boost::asio::io_service io;
47 NetworkMonitor netmon(io);
48 if ((netmon.getCapabilities() & NetworkMonitor::CAP_ENUM) == 0) {
49 BOOST_THROW_EXCEPTION(NetworkMonitor::Error("NetworkMonitor::CAP_ENUM is unavailable"));
50 }
51
52 netmon.onEnumerationCompleted.connect([&io] { io.stop(); });
53 io.run();
54 io.reset();
55
56 cached = netmon.listNetworkInterfaces();
57 }
58
59 return cached;
60}
61
62} // namespace tests
63} // namespace net
64} // namespace ndn