Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 39 | namespace nfd { |
| 40 | namespace face { |
| 41 | namespace 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 |
| 45 | * \return an IP address |
| 46 | * \retval default-constructed A, if no address is available |
| 47 | */ |
| 48 | template<typename A> |
| 49 | A |
| 50 | getAvailableInterfaceIp(); |
| 51 | |
| 52 | template<> |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame^] | 53 | inline boost::asio::ip::address_v4 |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 54 | getAvailableInterfaceIp() |
| 55 | { |
| 56 | for (const auto& interface : listNetworkInterfaces()) { |
| 57 | if (interface.isUp() && !interface.isLoopback()) { |
| 58 | for (const auto& address : interface.ipv4Addresses) { |
| 59 | if (!address.is_unspecified() && |
| 60 | !address.is_loopback()) { |
| 61 | return address; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return {}; |
| 68 | } |
| 69 | |
| 70 | template<> |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame^] | 71 | inline boost::asio::ip::address_v6 |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 72 | getAvailableInterfaceIp() |
| 73 | { |
| 74 | for (const auto& interface : listNetworkInterfaces()) { |
| 75 | if (interface.isUp() && !interface.isLoopback()) { |
| 76 | for (const auto& address : interface.ipv6Addresses) { |
| 77 | if (!address.is_unspecified() && |
| 78 | !address.is_link_local() && // see #1428 |
| 79 | !address.is_loopback()) { |
| 80 | return address; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return {}; |
| 87 | } |
| 88 | |
| 89 | } // namespace tests |
| 90 | } // namespace face |
| 91 | } // namespace nfd |
| 92 | |
| 93 | #endif // NFD_TESTS_DAEMON_FACE_GET_AVAILABLE_INTERFACE_IP_HPP |