network-predicate.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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 #include "network-predicate.hpp"
27 #include "common/config-file.hpp"
28 #include "core/network.hpp"
29 
30 #include <fnmatch.h>
31 
32 #include <boost/lexical_cast.hpp>
33 
34 namespace nfd::face {
35 
37 {
38  this->clear();
39 }
40 
42 
43 void
45 {
46  m_whitelist = std::set<std::string>{"*"};
47  m_blacklist.clear();
48 }
49 
50 void
51 NetworkPredicateBase::parseList(std::set<std::string>& set,
52  const boost::property_tree::ptree& list,
53  const std::string& section)
54 {
55  set.clear();
56 
57  for (const auto& item : list) {
58  if (item.first == "*") {
59  // insert wildcard
60  set.insert(item.first);
61  }
62  else {
63  if (!isRuleSupported(item.first)) {
64  NDN_THROW(ConfigFile::Error("Unrecognized rule '" + item.first +
65  "' in section '" + section + "'"));
66  }
67 
68  auto value = item.second.get_value<std::string>();
69  if (!isRuleValid(item.first, value)) {
70  NDN_THROW(ConfigFile::Error("Malformed " + item.first + " '" + value +
71  "' in section '" + section + "'"));
72  }
73  set.insert(value);
74  }
75  }
76 }
77 
78 void
79 NetworkPredicateBase::parseList(std::set<std::string>& set,
80  std::initializer_list<std::pair<std::string, std::string>> list)
81 {
82  set.clear();
83 
84  for (const auto& item : list) {
85  if (item.first == "*") {
86  // insert wildcard
87  set.insert(item.first);
88  }
89  else {
90  if (!isRuleSupported(item.first)) {
91  NDN_THROW(std::runtime_error("Unrecognized rule '" + item.first + "'"));
92  }
93 
94  if (!isRuleValid(item.first, item.second)) {
95  NDN_THROW(std::runtime_error("Malformed " + item.first + " '" + item.second + "'"));
96  }
97  set.insert(item.second);
98  }
99  }
100 }
101 
102 void
103 NetworkPredicateBase::parseWhitelist(const boost::property_tree::ptree& list)
104 {
105  parseList(m_whitelist, list, "whitelist");
106 }
107 
108 void
109 NetworkPredicateBase::parseBlacklist(const boost::property_tree::ptree& list)
110 {
111  parseList(m_blacklist, list, "blacklist");
112 }
113 
114 void
115 NetworkPredicateBase::assign(std::initializer_list<std::pair<std::string, std::string>> whitelist,
116  std::initializer_list<std::pair<std::string, std::string>> blacklist)
117 {
118  parseList(m_whitelist, whitelist);
119  parseList(m_blacklist, blacklist);
120 }
121 
122 bool
123 NetworkInterfacePredicate::isRuleSupported(const std::string& key)
124 {
125  return key == "ifname" || key == "ether" || key == "subnet";
126 }
127 
128 bool
129 NetworkInterfacePredicate::isRuleValid(const std::string& key, const std::string& value)
130 {
131  if (key == "ifname") {
132  // very basic sanity check for interface names
133  return !value.empty();
134  }
135  else if (key == "ether") {
136  // validate ethernet address
137  return !ndn::ethernet::Address::fromString(value).isNull();
138  }
139  else if (key == "subnet") {
140  // example subnet: 10.0.0.0/8
141  return Network::isValidCidr(value);
142  }
143  else {
144  NDN_THROW(std::logic_error("Only supported rules are expected"));
145  }
146 }
147 
148 bool
149 IpAddressPredicate::isRuleSupported(const std::string& key)
150 {
151  return key == "subnet";
152 }
153 
154 bool
155 IpAddressPredicate::isRuleValid(const std::string& key, const std::string& value)
156 {
157  if (key == "subnet") {
158  // example subnet: 10.0.0.0/8
159  return Network::isValidCidr(value);
160  }
161  else {
162  NDN_THROW(std::logic_error("Only supported rules are expected"));
163  }
164 }
165 
166 bool
168 {
169  return this->m_whitelist == other.m_whitelist &&
170  this->m_blacklist == other.m_blacklist;
171 }
172 
173 static bool
174 doesMatchPattern(const std::string& ifname, const std::string& pattern)
175 {
176  // use fnmatch(3) to provide unix glob-style matching for interface names
177  // fnmatch returns 0 if there is a match
178  return ::fnmatch(pattern.data(), ifname.data(), 0) == 0;
179 }
180 
181 static bool
182 doesNetifMatchRule(const ndn::net::NetworkInterface& netif, const std::string& rule)
183 {
184  // if '/' is in rule, this is a subnet, check if IP in subnet
185  if (rule.find('/') != std::string::npos) {
186  Network n = boost::lexical_cast<Network>(rule);
187  for (const auto& addr : netif.getNetworkAddresses()) {
188  if (n.doesContain(addr.getIp())) {
189  return true;
190  }
191  }
192  }
193 
194  return rule == "*" ||
195  doesMatchPattern(netif.getName(), rule) ||
196  netif.getEthernetAddress().toString() == rule;
197 }
198 
199 bool
200 NetworkInterfacePredicate::operator()(const ndn::net::NetworkInterface& netif) const
201 {
202  return std::any_of(m_whitelist.begin(), m_whitelist.end(),
203  [&netif] (const auto& rule) { return doesNetifMatchRule(netif, rule); }) &&
204  std::none_of(m_blacklist.begin(), m_blacklist.end(),
205  [&netif] (const auto& rule) { return doesNetifMatchRule(netif, rule); });
206 }
207 
208 static bool
209 doesAddressMatchRule(const boost::asio::ip::address& address, const std::string& rule)
210 {
211  // if '/' is in rule, this is a subnet, check if IP in subnet
212  if (rule.find('/') != std::string::npos) {
213  Network n = boost::lexical_cast<Network>(rule);
214  if (n.doesContain(address)) {
215  return true;
216  }
217  }
218 
219  return rule == "*";
220 }
221 
222 bool
223 IpAddressPredicate::operator()(const boost::asio::ip::address& address) const
224 {
225  return std::any_of(m_whitelist.begin(), m_whitelist.end(),
226  [&address] (const auto& rule) { return doesAddressMatchRule(address, rule); }) &&
227  std::none_of(m_blacklist.begin(), m_blacklist.end(),
228  [&address] (const auto& rule) { return doesAddressMatchRule(address, rule); });
229 }
230 
231 } // namespace nfd::face
bool doesContain(const boost::asio::ip::address &address) const
Definition: network.hpp:43
static bool isValidCidr(std::string_view cidr)
Definition: network.cpp:66
bool operator()(const boost::asio::ip::address &address) const
bool operator()(const ndn::net::NetworkInterface &netif) const
void clear()
Set the whitelist to "*" and clear the blacklist.
bool operator==(const NetworkPredicateBase &other) const
void parseWhitelist(const boost::property_tree::ptree &list)
std::set< std::string > m_whitelist
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
static bool doesNetifMatchRule(const ndn::net::NetworkInterface &netif, const std::string &rule)
static bool doesAddressMatchRule(const boost::asio::ip::address &address, const std::string &rule)
static bool doesMatchPattern(const std::string &ifname, const std::string &pattern)