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 | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 31 | #include <boost/property_tree/ptree_fwd.hpp> |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 32 | #include <ndn-cxx/net/network-interface.hpp> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::face { |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 35 | |
| 36 | class NetworkPredicateBase |
| 37 | { |
| 38 | public: |
| 39 | NetworkPredicateBase(); |
| 40 | |
| 41 | virtual |
| 42 | ~NetworkPredicateBase(); |
| 43 | |
| 44 | /** |
| 45 | * \brief Set the whitelist to "*" and clear the blacklist |
| 46 | */ |
| 47 | void |
| 48 | clear(); |
| 49 | |
| 50 | void |
| 51 | parseWhitelist(const boost::property_tree::ptree& list); |
| 52 | |
| 53 | void |
| 54 | parseBlacklist(const boost::property_tree::ptree& list); |
| 55 | |
| 56 | void |
| 57 | assign(std::initializer_list<std::pair<std::string, std::string>> whitelist, |
| 58 | std::initializer_list<std::pair<std::string, std::string>> blacklist); |
| 59 | |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 60 | private: |
| 61 | virtual bool |
| 62 | isRuleSupported(const std::string& key) = 0; |
| 63 | |
| 64 | virtual bool |
| 65 | isRuleValid(const std::string& key, const std::string& value) = 0; |
| 66 | |
| 67 | void |
| 68 | parseList(std::set<std::string>& set, const boost::property_tree::ptree& list, const std::string& section); |
| 69 | |
| 70 | void |
| 71 | parseList(std::set<std::string>& set, std::initializer_list<std::pair<std::string, std::string>> list); |
| 72 | |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 73 | private: // non-member operators (hidden friends) |
| 74 | friend bool |
| 75 | operator==(const NetworkPredicateBase& lhs, const NetworkPredicateBase& rhs) |
| 76 | { |
| 77 | return lhs.m_whitelist == rhs.m_whitelist && |
| 78 | lhs.m_blacklist == rhs.m_blacklist; |
| 79 | } |
| 80 | |
| 81 | friend bool |
| 82 | operator!=(const NetworkPredicateBase& lhs, const NetworkPredicateBase& rhs) |
| 83 | { |
| 84 | return !(lhs == rhs); |
| 85 | } |
| 86 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 87 | NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 88 | std::set<std::string> m_whitelist; |
| 89 | std::set<std::string> m_blacklist; |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * \brief Represents a predicate to accept or reject a ndn::net::NetworkInterface. |
| 94 | * |
| 95 | * The predicate consists of a whitelist and a blacklist. Whitelist and blacklist can contain, |
| 96 | * in no particular order, interface names (e.g., `ifname eth0`), MAC addresses (e.g., `ether |
| 97 | * 85:3b:4d:d3:5f:c2`), IPv4 and IPv6 subnets (e.g., `subnet 192.0.2.0/24` or `subnet |
| 98 | * 2001:db8:2::/64`), or a wildcard (`*`) that matches all interfaces. A |
| 99 | * ndn::net::NetworkInterface is accepted if it matches any entry in the whitelist and none of |
| 100 | * the entries in the blacklist. |
| 101 | */ |
| 102 | class NetworkInterfacePredicate : public NetworkPredicateBase |
| 103 | { |
| 104 | public: |
| 105 | bool |
| 106 | operator()(const ndn::net::NetworkInterface& netif) const; |
| 107 | |
| 108 | private: |
| 109 | bool |
| 110 | isRuleSupported(const std::string& key) final; |
| 111 | |
| 112 | bool |
| 113 | isRuleValid(const std::string& key, const std::string& value) final; |
| 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * \brief Represents a predicate to accept or reject an IP address. |
| 118 | * |
| 119 | * The predicate consists of a whitelist and a blacklist. Whitelist and blacklist can contain, |
| 120 | * in no particular order, IPv4 and IPv6 subnets (e.g., `subnet 192.0.2.0/24` or `subnet |
| 121 | * 2001:db8:2::/64`) or a wildcard (`*`) that matches all IP addresses. An IP address is |
| 122 | * accepted if it matches any entry in the whitelist and none of the entries in the blacklist. |
| 123 | */ |
| 124 | class IpAddressPredicate : public NetworkPredicateBase |
| 125 | { |
| 126 | public: |
| 127 | bool |
| 128 | operator()(const boost::asio::ip::address& address) const; |
| 129 | |
| 130 | private: |
| 131 | bool |
| 132 | isRuleSupported(const std::string& key) final; |
| 133 | |
| 134 | bool |
| 135 | isRuleValid(const std::string& key, const std::string& value) final; |
| 136 | }; |
| 137 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 138 | } // namespace nfd::face |
Alexander Afanasyev | e4d745d | 2018-04-08 17:55:56 -0400 | [diff] [blame] | 139 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 140 | #endif // NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP |