blob: 815afcc5b464a7deab389299f822f9c157026d55 [file] [log] [blame]
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento191a7a22023-05-17 22:40:43 -04003 * Copyright (c) 2014-2023, 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 Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::face {
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040037
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040038class NetworkPredicateBase : private boost::equality_comparable<NetworkPredicateBase>
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040039{
40public:
41 NetworkPredicateBase();
42
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040043 /**
Davide Pesaventoc0df94e2023-10-08 19:26:55 -040044 * \brief Set the whitelist to "*" and clear the blacklist.
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -040045 */
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 Pesavento0a05f7a2023-10-16 20:28:06 -040059protected:
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 Afanasyeve4d745d2018-04-08 17:55:56 -040078private:
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 Pesavento191a7a22023-05-17 22:40:43 -040091private: // 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 Pesavento264af772021-02-09 21:48:24 -050099NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400100 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 Pesavento0a05f7a2023-10-16 20:28:06 -0400114class NetworkInterfacePredicate final : public NetworkPredicateBase
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400115{
116public:
117 bool
118 operator()(const ndn::net::NetworkInterface& netif) const;
119
120private:
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 Pesavento0a05f7a2023-10-16 20:28:06 -0400136class IpAddressPredicate final : public NetworkPredicateBase
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400137{
138public:
139 bool
140 operator()(const boost::asio::ip::address& address) const;
141
142private:
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 Pesaventoe422f9e2022-06-03 01:30:23 -0400150} // namespace nfd::face
Alexander Afanasyeve4d745d2018-04-08 17:55:56 -0400151
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400152#endif // NFD_DAEMON_FACE_NETWORK_PREDICATE_HPP