blob: 0147d78743b5ceff1eca9b8ddb3976236427a3ca [file] [log] [blame]
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -04004 * 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 Pesavento2cae8ca2019-04-18 20:48:05 -040026#ifndef NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP
27#define NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040028
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040029#include "core/common.hpp"
30
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040031#include <boost/operators.hpp>
Davide Pesaventoa9b09b62022-06-04 14:07:25 -040032#include <boost/property_tree/ptree_fwd.hpp>
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040033
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040034#include <ndn-cxx/net/network-interface.hpp>
35
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050036#include <set>
37
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038namespace nfd::face {
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040039
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040040class NetworkPredicateBase : private boost::equality_comparable<NetworkPredicateBase>
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040041{
42public:
43 NetworkPredicateBase();
44
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040045 /**
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040046 * \brief Set the whitelist to "*" and clear the blacklist.
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040047 */
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 Pesavento0a05f7a2023-10-16 20:28:06 -040061protected:
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 Afanasyeve4d745d2018-04-08 17:55:56 -040080private:
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 Pesavento191a7a22023-05-17 22:40:43 -040093private: // 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 Pesavento264af772021-02-09 21:48:24 -0500101NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400102 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 Pesavento0a05f7a2023-10-16 20:28:06 -0400116class NetworkInterfacePredicate final : public NetworkPredicateBase
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400117{
118public:
119 bool
120 operator()(const ndn::net::NetworkInterface& netif) const;
121
122private:
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 Pesavento0a05f7a2023-10-16 20:28:06 -0400138class IpAddressPredicate final : public NetworkPredicateBase
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400139{
140public:
141 bool
142 operator()(const boost::asio::ip::address& address) const;
143
144private:
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 Pesaventoe422f9e2022-06-03 01:30:23 -0400152} // namespace nfd::face
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400153
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400154#endif // NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP