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 |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame^] | 45 | * \param needMulticast true if the address must be chosen from a multicast-capable interface |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 46 | * \return an IP address |
| 47 | * \retval default-constructed A, if no address is available |
| 48 | */ |
| 49 | template<typename A> |
| 50 | A |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame^] | 51 | getAvailableInterfaceIp(bool needMulticast = false); |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 52 | |
| 53 | template<> |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 54 | inline boost::asio::ip::address_v4 |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame^] | 55 | getAvailableInterfaceIp(bool needMulticast) |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 56 | { |
| 57 | for (const auto& interface : listNetworkInterfaces()) { |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame^] | 58 | if (interface.isUp() && |
| 59 | !interface.isLoopback() && |
| 60 | (!needMulticast || interface.isMulticastCapable())) { |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 61 | for (const auto& address : interface.ipv4Addresses) { |
| 62 | if (!address.is_unspecified() && |
| 63 | !address.is_loopback()) { |
| 64 | return address; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 69 | return {}; |
| 70 | } |
| 71 | |
| 72 | template<> |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 73 | inline boost::asio::ip::address_v6 |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame^] | 74 | getAvailableInterfaceIp(bool needMulticast) |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 75 | { |
| 76 | for (const auto& interface : listNetworkInterfaces()) { |
Eric Newberry | 6d8ee7a | 2015-12-21 16:37:52 -0700 | [diff] [blame^] | 77 | if (interface.isUp() && |
| 78 | !interface.isLoopback() && |
| 79 | (!needMulticast || interface.isMulticastCapable())) { |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 80 | 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 Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 89 | return {}; |
| 90 | } |
| 91 | |
| 92 | } // namespace tests |
| 93 | } // namespace face |
| 94 | } // namespace nfd |
| 95 | |
| 96 | #endif // NFD_TESTS_DAEMON_FACE_GET_AVAILABLE_INTERFACE_IP_HPP |