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