blob: 2cf01c95dc2f051c5b0febe5f9acbe10d9e3a025 [file] [log] [blame]
Junxiao Shi84d62cb2017-07-12 16:15:18 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi84d62cb2017-07-12 16:15:18 +00004 * 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 Pesavento22fba352017-10-17 15:53:51 -040026#include "test-ip.hpp"
27#include "test-netif.hpp"
Junxiao Shi84d62cb2017-07-12 16:15:18 +000028
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040029namespace nfd::tests {
Junxiao Shi84d62cb2017-07-12 16:15:18 +000030
Davide Pesavento22fba352017-10-17 15:53:51 -040031std::ostream&
32operator<<(std::ostream& os, AddressFamily family)
Junxiao Shi84d62cb2017-07-12 16:15:18 +000033{
Davide Pesavento22fba352017-10-17 15:53:51 -040034 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 Shi84d62cb2017-07-12 16:15:18 +000041 }
Davide Pesavento22fba352017-10-17 15:53:51 -040042 return os << '?';
43}
Junxiao Shi84d62cb2017-07-12 16:15:18 +000044
Davide Pesavento22fba352017-10-17 15:53:51 -040045std::ostream&
46operator<<(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
61std::ostream&
62operator<<(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 Shi84d62cb2017-07-12 16:15:18 +000073}
74
Junxiao Shi84d62cb2017-07-12 16:15:18 +000075template<typename E>
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040076static bool
Junxiao Shi84d62cb2017-07-12 16:15:18 +000077matchTristate(E e, bool b)
78{
Davide Pesavento22fba352017-10-17 15:53:51 -040079 return (e == E::Any) ||
Junxiao Shi84d62cb2017-07-12 16:15:18 +000080 (e == E::Yes && b) ||
81 (e == E::No && !b);
82}
83
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040084boost::asio::ip::address
85getTestIp(AddressFamily family, AddressScope scope, MulticastInterface mcast)
Junxiao Shi84d62cb2017-07-12 16:15:18 +000086{
Davide Pesavento279af1c2022-08-29 20:18:32 -040087 return std::get<boost::asio::ip::address>(getTestInterfaceAndIp(family, scope, mcast));
88}
89
90std::tuple<shared_ptr<const ndn::net::NetworkInterface>, boost::asio::ip::address>
91getTestInterfaceAndIp(AddressFamily family, AddressScope scope, MulticastInterface mcast)
92{
Junxiao Shi84d62cb2017-07-12 16:15:18 +000093 for (const auto& interface : collectNetworkInterfaces()) {
94 if (!interface->isUp() ||
Junxiao Shi84d62cb2017-07-12 16:15:18 +000095 !matchTristate(mcast, interface->canMulticast())) {
96 continue;
97 }
98 for (const auto& address : interface->getNetworkAddresses()) {
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040099 if (!address.getIp().is_unspecified() &&
Davide Pesavento22fba352017-10-17 15:53:51 -0400100 (family == AddressFamily::Any ||
101 static_cast<int>(family) == static_cast<int>(address.getFamily())) &&
102 (scope == AddressScope::Any ||
Davide Pesaventob8d1fc72017-10-08 02:05:05 -0400103 static_cast<int>(scope) == static_cast<int>(address.getScope())) &&
104 (scope != AddressScope::Loopback ||
105 address.getIp().is_loopback())) {
Davide Pesavento279af1c2022-08-29 20:18:32 -0400106 return {interface, address.getIp()};
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000107 }
108 }
109 }
110 return {};
111}
112
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400113} // namespace nfd::tests