network-predicate.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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 
26 #ifndef NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP
27 #define NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP
28 
29 #include "core/common.hpp"
30 
31 #include <boost/operators.hpp>
32 #include <boost/property_tree/ptree_fwd.hpp>
33 
34 #include <ndn-cxx/net/network-interface.hpp>
35 
36 #include <set>
37 
38 namespace nfd::face {
39 
40 class NetworkPredicateBase : private boost::equality_comparable<NetworkPredicateBase>
41 {
42 public:
44 
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 
61 protected:
62  // Explicitly declare the following four special member functions
63  // to avoid -Wdeprecated-copy-with-dtor warnings from clang.
64 
66 
68 
70  operator=(const NetworkPredicateBase&) = delete;
71 
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 
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 
93 private: // non-member operators (hidden friends)
94  friend bool
96  {
97  return lhs.m_whitelist == rhs.m_whitelist &&
98  lhs.m_blacklist == rhs.m_blacklist;
99  }
100 
102  std::set<std::string> m_whitelist;
103  std::set<std::string> m_blacklist;
104 };
105 
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 
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 
152 } // namespace nfd::face
153 
154 #endif // NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP
Represents a predicate to accept or reject an IP address.
bool operator()(const boost::asio::ip::address &address) const
Represents a predicate to accept or reject a ndn::net::NetworkInterface.
bool operator()(const ndn::net::NetworkInterface &netif) const
NetworkPredicateBase & operator=(const NetworkPredicateBase &)=delete
NetworkPredicateBase(const NetworkPredicateBase &)=delete
void clear()
Set the whitelist to "*" and clear the blacklist.
void parseWhitelist(const boost::property_tree::ptree &list)
NetworkPredicateBase(NetworkPredicateBase &&)=default
friend bool operator==(const NetworkPredicateBase &lhs, const NetworkPredicateBase &rhs)
std::set< std::string > m_whitelist
NetworkPredicateBase & operator=(NetworkPredicateBase &&)=default
void parseBlacklist(const boost::property_tree::ptree &list)
void assign(std::initializer_list< std::pair< std::string, std::string >> whitelist, std::initializer_list< std::pair< std::string, std::string >> blacklist)
std::set< std::string > m_blacklist
#define NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:40