Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-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 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 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 26 | #include "test-ip.hpp" |
| 27 | #include "test-netif.hpp" |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 30 | namespace face { |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 31 | namespace tests { |
| 32 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 33 | std::ostream& |
| 34 | operator<<(std::ostream& os, AddressFamily family) |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 35 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 36 | switch (family) { |
| 37 | case AddressFamily::V4: |
| 38 | return os << "IPv4"; |
| 39 | case AddressFamily::V6: |
| 40 | return os << "IPv6"; |
| 41 | case AddressFamily::Any: |
| 42 | return os << "Any"; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 43 | } |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 44 | return os << '?'; |
| 45 | } |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 46 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 47 | std::ostream& |
| 48 | operator<<(std::ostream& os, AddressScope scope) |
| 49 | { |
| 50 | switch (scope) { |
| 51 | case AddressScope::Loopback: |
| 52 | return os << "Loopback"; |
| 53 | case AddressScope::LinkLocal: |
| 54 | return os << "LinkLocal"; |
| 55 | case AddressScope::Global: |
| 56 | return os << "Global"; |
| 57 | case AddressScope::Any: |
| 58 | return os << "Any"; |
| 59 | } |
| 60 | return os << '?'; |
| 61 | } |
| 62 | |
| 63 | std::ostream& |
| 64 | operator<<(std::ostream& os, MulticastInterface mcast) |
| 65 | { |
| 66 | switch (mcast) { |
| 67 | case MulticastInterface::No: |
| 68 | return os << "No"; |
| 69 | case MulticastInterface::Yes: |
| 70 | return os << "Yes"; |
| 71 | case MulticastInterface::Any: |
| 72 | return os << "Any"; |
| 73 | } |
| 74 | return os << '?'; |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 77 | template<typename E> |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 78 | static bool |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 79 | matchTristate(E e, bool b) |
| 80 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 81 | return (e == E::Any) || |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 82 | (e == E::Yes && b) || |
| 83 | (e == E::No && !b); |
| 84 | } |
| 85 | |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 86 | boost::asio::ip::address |
| 87 | getTestIp(AddressFamily family, AddressScope scope, MulticastInterface mcast) |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 88 | { |
| 89 | for (const auto& interface : collectNetworkInterfaces()) { |
| 90 | if (!interface->isUp() || |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 91 | !matchTristate(mcast, interface->canMulticast())) { |
| 92 | continue; |
| 93 | } |
| 94 | for (const auto& address : interface->getNetworkAddresses()) { |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 95 | if (!address.getIp().is_unspecified() && |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 96 | (family == AddressFamily::Any || |
| 97 | static_cast<int>(family) == static_cast<int>(address.getFamily())) && |
| 98 | (scope == AddressScope::Any || |
Davide Pesavento | b8d1fc7 | 2017-10-08 02:05:05 -0400 | [diff] [blame] | 99 | static_cast<int>(scope) == static_cast<int>(address.getScope())) && |
| 100 | (scope != AddressScope::Loopback || |
| 101 | address.getIp().is_loopback())) { |
| 102 | return address.getIp(); |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | return {}; |
| 107 | } |
| 108 | |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 109 | } // namespace tests |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 110 | } // namespace face |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 111 | } // namespace nfd |