blob: 3e75d8cba11bb12867b3efedd72d278ebe4ffbda [file] [log] [blame]
Junxiao Shi84d62cb2017-07-12 16:15:18 +00001/* -*- 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 Pesavento22fba352017-10-17 15:53:51 -040026#include "test-ip.hpp"
27#include "test-netif.hpp"
Junxiao Shi84d62cb2017-07-12 16:15:18 +000028
29namespace nfd {
Davide Pesavento22fba352017-10-17 15:53:51 -040030namespace face {
Junxiao Shi84d62cb2017-07-12 16:15:18 +000031namespace tests {
32
Davide Pesavento22fba352017-10-17 15:53:51 -040033std::ostream&
34operator<<(std::ostream& os, AddressFamily family)
Junxiao Shi84d62cb2017-07-12 16:15:18 +000035{
Davide Pesavento22fba352017-10-17 15:53:51 -040036 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 Shi84d62cb2017-07-12 16:15:18 +000043 }
Davide Pesavento22fba352017-10-17 15:53:51 -040044 return os << '?';
45}
Junxiao Shi84d62cb2017-07-12 16:15:18 +000046
Davide Pesavento22fba352017-10-17 15:53:51 -040047std::ostream&
48operator<<(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
63std::ostream&
64operator<<(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 Shi84d62cb2017-07-12 16:15:18 +000075}
76
Junxiao Shi84d62cb2017-07-12 16:15:18 +000077template<typename E>
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040078static bool
Junxiao Shi84d62cb2017-07-12 16:15:18 +000079matchTristate(E e, bool b)
80{
Davide Pesavento22fba352017-10-17 15:53:51 -040081 return (e == E::Any) ||
Junxiao Shi84d62cb2017-07-12 16:15:18 +000082 (e == E::Yes && b) ||
83 (e == E::No && !b);
84}
85
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040086boost::asio::ip::address
87getTestIp(AddressFamily family, AddressScope scope, MulticastInterface mcast)
Junxiao Shi84d62cb2017-07-12 16:15:18 +000088{
89 for (const auto& interface : collectNetworkInterfaces()) {
90 if (!interface->isUp() ||
Junxiao Shi84d62cb2017-07-12 16:15:18 +000091 !matchTristate(mcast, interface->canMulticast())) {
92 continue;
93 }
94 for (const auto& address : interface->getNetworkAddresses()) {
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040095 if (!address.getIp().is_unspecified() &&
Davide Pesavento22fba352017-10-17 15:53:51 -040096 (family == AddressFamily::Any ||
97 static_cast<int>(family) == static_cast<int>(address.getFamily())) &&
98 (scope == AddressScope::Any ||
Davide Pesaventob8d1fc72017-10-08 02:05:05 -040099 static_cast<int>(scope) == static_cast<int>(address.getScope())) &&
100 (scope != AddressScope::Loopback ||
101 address.getIp().is_loopback())) {
102 return address.getIp();
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000103 }
104 }
105 }
106 return {};
107}
108
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000109} // namespace tests
Davide Pesavento22fba352017-10-17 15:53:51 -0400110} // namespace face
Junxiao Shi84d62cb2017-07-12 16:15:18 +0000111} // namespace nfd