Davide Pesavento | eee53aa | 2016-04-11 17:20:21 +0200 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, 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-ip.hpp" |
| 27 | |
| 28 | #include "core/network-interface.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace tests { |
| 32 | namespace { |
| 33 | |
| 34 | template<typename E> |
| 35 | bool |
| 36 | matchTristate(E e, bool b) |
| 37 | { |
| 38 | return (e == E::DontCare) || |
| 39 | (e == E::Yes && b) || |
| 40 | (e == E::No && !b); |
| 41 | } |
| 42 | |
| 43 | } // unnamed namespace |
| 44 | |
| 45 | template<> |
| 46 | boost::asio::ip::address_v4 |
| 47 | getTestIp(LoopbackAddress loopback, MulticastInterface mcast) |
| 48 | { |
| 49 | for (const auto& interface : listNetworkInterfaces()) { |
| 50 | if (interface.isUp() && |
| 51 | matchTristate(loopback, interface.isLoopback()) && |
| 52 | matchTristate(mcast, interface.isMulticastCapable())) { |
| 53 | for (const auto& address : interface.ipv4Addresses) { |
| 54 | if (!address.is_unspecified() && |
| 55 | matchTristate(loopback, address.is_loopback())) { |
| 56 | return address; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | return {}; |
| 62 | } |
| 63 | |
| 64 | template<> |
| 65 | boost::asio::ip::address_v6 |
| 66 | getTestIp(LoopbackAddress loopback, MulticastInterface mcast) |
| 67 | { |
| 68 | for (const auto& interface : listNetworkInterfaces()) { |
| 69 | if (interface.isUp() && |
| 70 | matchTristate(loopback, interface.isLoopback()) && |
| 71 | matchTristate(mcast, interface.isMulticastCapable())) { |
| 72 | for (const auto& address : interface.ipv6Addresses) { |
| 73 | if (!address.is_unspecified() && |
| 74 | !address.is_link_local() && // see #1428 |
| 75 | matchTristate(loopback, address.is_loopback())) { |
| 76 | return address; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return {}; |
| 82 | } |
| 83 | |
| 84 | } // namespace tests |
| 85 | } // namespace nfd |