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