blob: e7088a80020c1d420c74cceeb5403655e7d30931 [file] [log] [blame]
Eric Newberry8717e872015-11-23 12:41:50 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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#ifndef NFD_TESTS_DAEMON_FACE_GET_AVAILABLE_INTERFACE_IP_HPP
27#define NFD_TESTS_DAEMON_FACE_GET_AVAILABLE_INTERFACE_IP_HPP
28
29#include "core/network-interface.hpp"
30
31#define SKIP_IF_IP_UNAVAILABLE(address) \
32 do { \
33 if (address.is_unspecified()) { \
34 BOOST_WARN_MESSAGE(false, "skipping assertions that require a valid IP address"); \
35 return; \
36 } \
37 } while (false)
38
39namespace nfd {
40namespace face {
41namespace tests {
42
43/** \brief get a non-local IP address from any available network interface
44 * \tparam A the address type, either boost::asio::ip::address_v4 or boost::asio::ip::address_v6
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070045 * \param needMulticast true if the address must be chosen from a multicast-capable interface
Eric Newberry8717e872015-11-23 12:41:50 -070046 * \return an IP address
47 * \retval default-constructed A, if no address is available
48 */
49template<typename A>
50A
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070051getAvailableInterfaceIp(bool needMulticast = false);
Eric Newberry8717e872015-11-23 12:41:50 -070052
53template<>
Eric Newberryf68f3782015-12-11 00:31:32 -070054inline boost::asio::ip::address_v4
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070055getAvailableInterfaceIp(bool needMulticast)
Eric Newberry8717e872015-11-23 12:41:50 -070056{
57 for (const auto& interface : listNetworkInterfaces()) {
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070058 if (interface.isUp() &&
59 !interface.isLoopback() &&
60 (!needMulticast || interface.isMulticastCapable())) {
Eric Newberry8717e872015-11-23 12:41:50 -070061 for (const auto& address : interface.ipv4Addresses) {
62 if (!address.is_unspecified() &&
63 !address.is_loopback()) {
64 return address;
65 }
66 }
67 }
68 }
Eric Newberry8717e872015-11-23 12:41:50 -070069 return {};
70}
71
72template<>
Eric Newberryf68f3782015-12-11 00:31:32 -070073inline boost::asio::ip::address_v6
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070074getAvailableInterfaceIp(bool needMulticast)
Eric Newberry8717e872015-11-23 12:41:50 -070075{
76 for (const auto& interface : listNetworkInterfaces()) {
Eric Newberry6d8ee7a2015-12-21 16:37:52 -070077 if (interface.isUp() &&
78 !interface.isLoopback() &&
79 (!needMulticast || interface.isMulticastCapable())) {
Eric Newberry8717e872015-11-23 12:41:50 -070080 for (const auto& address : interface.ipv6Addresses) {
81 if (!address.is_unspecified() &&
82 !address.is_link_local() && // see #1428
83 !address.is_loopback()) {
84 return address;
85 }
86 }
87 }
88 }
Eric Newberry8717e872015-11-23 12:41:50 -070089 return {};
90}
91
92} // namespace tests
93} // namespace face
94} // namespace nfd
95
96#endif // NFD_TESTS_DAEMON_FACE_GET_AVAILABLE_INTERFACE_IP_HPP