Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [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 | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP |
| 27 | #define NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 28 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 29 | #include "core/common.hpp" |
| 30 | |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 31 | #include <boost/operators.hpp> |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 32 | #include <boost/property_tree/ptree_fwd.hpp> |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 33 | |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 34 | #include <ndn-cxx/net/network-interface.hpp> |
| 35 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 36 | #include <set> |
| 37 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | namespace nfd::face { |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 39 | |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 40 | class NetworkPredicateBase : private boost::equality_comparable<NetworkPredicateBase> |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | NetworkPredicateBase(); |
| 44 | |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 45 | /** |
Davide Pesavento | c0df94e | 2023-10-08 19:26:55 -0400 | [diff] [blame] | 46 | * \brief Set the whitelist to "*" and clear the blacklist. |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 47 | */ |
| 48 | void |
| 49 | clear(); |
| 50 | |
| 51 | void |
| 52 | parseWhitelist(const boost::property_tree::ptree& list); |
| 53 | |
| 54 | void |
| 55 | parseBlacklist(const boost::property_tree::ptree& list); |
| 56 | |
| 57 | void |
| 58 | assign(std::initializer_list<std::pair<std::string, std::string>> whitelist, |
| 59 | std::initializer_list<std::pair<std::string, std::string>> blacklist); |
| 60 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 61 | protected: |
| 62 | // Explicitly declare the following four special member functions |
| 63 | // to avoid -Wdeprecated-copy-with-dtor warnings from clang. |
| 64 | |
| 65 | NetworkPredicateBase(const NetworkPredicateBase&) = delete; |
| 66 | |
| 67 | NetworkPredicateBase(NetworkPredicateBase&&) = default; |
| 68 | |
| 69 | NetworkPredicateBase& |
| 70 | operator=(const NetworkPredicateBase&) = delete; |
| 71 | |
| 72 | NetworkPredicateBase& |
| 73 | operator=(NetworkPredicateBase&&) = default; |
| 74 | |
| 75 | // NetworkPredicateBase is not supposed to be used polymorphically, so we make the destructor |
| 76 | // protected to prevent deletion of derived objects through a pointer to the base class, |
| 77 | // which would be UB when the destructor is non-virtual. |
| 78 | ~NetworkPredicateBase() = default; |
| 79 | |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 80 | private: |
| 81 | virtual bool |
| 82 | isRuleSupported(const std::string& key) = 0; |
| 83 | |
| 84 | virtual bool |
| 85 | isRuleValid(const std::string& key, const std::string& value) = 0; |
| 86 | |
| 87 | void |
| 88 | parseList(std::set<std::string>& set, const boost::property_tree::ptree& list, const std::string& section); |
| 89 | |
| 90 | void |
| 91 | parseList(std::set<std::string>& set, std::initializer_list<std::pair<std::string, std::string>> list); |
| 92 | |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 93 | private: // non-member operators (hidden friends) |
| 94 | friend bool |
| 95 | operator==(const NetworkPredicateBase& lhs, const NetworkPredicateBase& rhs) |
| 96 | { |
| 97 | return lhs.m_whitelist == rhs.m_whitelist && |
| 98 | lhs.m_blacklist == rhs.m_blacklist; |
| 99 | } |
| 100 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 101 | NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 102 | std::set<std::string> m_whitelist; |
| 103 | std::set<std::string> m_blacklist; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * \brief Represents a predicate to accept or reject a ndn::net::NetworkInterface. |
| 108 | * |
| 109 | * The predicate consists of a whitelist and a blacklist. Whitelist and blacklist can contain, |
| 110 | * in no particular order, interface names (e.g., `ifname eth0`), MAC addresses (e.g., `ether |
| 111 | * 85:3b:4d:d3:5f:c2`), IPv4 and IPv6 subnets (e.g., `subnet 192.0.2.0/24` or `subnet |
| 112 | * 2001:db8:2::/64`), or a wildcard (`*`) that matches all interfaces. A |
| 113 | * ndn::net::NetworkInterface is accepted if it matches any entry in the whitelist and none of |
| 114 | * the entries in the blacklist. |
| 115 | */ |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 116 | class NetworkInterfacePredicate final : public NetworkPredicateBase |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 117 | { |
| 118 | public: |
| 119 | bool |
| 120 | operator()(const ndn::net::NetworkInterface& netif) const; |
| 121 | |
| 122 | private: |
| 123 | bool |
| 124 | isRuleSupported(const std::string& key) final; |
| 125 | |
| 126 | bool |
| 127 | isRuleValid(const std::string& key, const std::string& value) final; |
| 128 | }; |
| 129 | |
| 130 | /** |
| 131 | * \brief Represents a predicate to accept or reject an IP address. |
| 132 | * |
| 133 | * The predicate consists of a whitelist and a blacklist. Whitelist and blacklist can contain, |
| 134 | * in no particular order, IPv4 and IPv6 subnets (e.g., `subnet 192.0.2.0/24` or `subnet |
| 135 | * 2001:db8:2::/64`) or a wildcard (`*`) that matches all IP addresses. An IP address is |
| 136 | * accepted if it matches any entry in the whitelist and none of the entries in the blacklist. |
| 137 | */ |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 138 | class IpAddressPredicate final : public NetworkPredicateBase |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 139 | { |
| 140 | public: |
| 141 | bool |
| 142 | operator()(const boost::asio::ip::address& address) const; |
| 143 | |
| 144 | private: |
| 145 | bool |
| 146 | isRuleSupported(const std::string& key) final; |
| 147 | |
| 148 | bool |
| 149 | isRuleValid(const std::string& key, const std::string& value) final; |
| 150 | }; |
| 151 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 152 | } // namespace nfd::face |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 153 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 154 | #endif // NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP |