NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2021, 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 namespace nfd {
33 namespace face {
34 
36 {
37  this->clear();
38 }
39 
41 
42 void
44 {
45  m_whitelist = std::set<std::string>{"*"};
46  m_blacklist.clear();
47 }
48 
49 void
50 NetworkPredicateBase::parseList(std::set<std::string>& set,
51  const boost::property_tree::ptree& list,
52  const std::string& section)
53 {
54  set.clear();
55 
56  for (const auto& item : list) {
57  if (item.first == "*") {
58  // insert wildcard
59  set.insert(item.first);
60  }
61  else {
62  if (!isRuleSupported(item.first)) {
63  NDN_THROW(ConfigFile::Error("Unrecognized rule '" + item.first +
64  "' in section '" + section + "'"));
65  }
66 
67  auto value = item.second.get_value<std::string>();
68  if (!isRuleValid(item.first, value)) {
69  NDN_THROW(ConfigFile::Error("Malformed " + item.first + " '" + value +
70  "' in section '" + section + "'"));
71  }
72  set.insert(value);
73  }
74  }
75 }
76 
77 void
78 NetworkPredicateBase::parseList(std::set<std::string>& set,
79  std::initializer_list<std::pair<std::string, std::string>> list)
80 {
81  set.clear();
82 
83  for (const auto& item : list) {
84  if (item.first == "*") {
85  // insert wildcard
86  set.insert(item.first);
87  }
88  else {
89  if (!isRuleSupported(item.first)) {
90  NDN_THROW(std::runtime_error("Unrecognized rule '" + item.first + "'"));
91  }
92 
93  if (!isRuleValid(item.first, item.second)) {
94  NDN_THROW(std::runtime_error("Malformed " + item.first + " '" + item.second + "'"));
95  }
96  set.insert(item.second);
97  }
98  }
99 }
100 
101 void
102 NetworkPredicateBase::parseWhitelist(const boost::property_tree::ptree& list)
103 {
104  parseList(m_whitelist, list, "whitelist");
105 }
106 
107 void
108 NetworkPredicateBase::parseBlacklist(const boost::property_tree::ptree& list)
109 {
110  parseList(m_blacklist, list, "blacklist");
111 }
112 
113 void
114 NetworkPredicateBase::assign(std::initializer_list<std::pair<std::string, std::string>> whitelist,
115  std::initializer_list<std::pair<std::string, std::string>> blacklist)
116 {
117  parseList(m_whitelist, whitelist);
118  parseList(m_blacklist, blacklist);
119 }
120 
121 bool
122 NetworkInterfacePredicate::isRuleSupported(const std::string& key)
123 {
124  return key == "ifname" || key == "ether" || key == "subnet";
125 }
126 
127 bool
128 NetworkInterfacePredicate::isRuleValid(const std::string& key, const std::string& value)
129 {
130  if (key == "ifname") {
131  // very basic sanity check for interface names
132  return !value.empty();
133  }
134  else if (key == "ether") {
135  // validate ethernet address
137  }
138  else if (key == "subnet") {
139  // example subnet: 10.0.0.0/8
140  return Network::isValidCidr(value);
141  }
142  else {
143  NDN_THROW(std::logic_error("Only supported rules are expected"));
144  }
145 }
146 
147 bool
148 IpAddressPredicate::isRuleSupported(const std::string& key)
149 {
150  return key == "subnet";
151 }
152 
153 bool
154 IpAddressPredicate::isRuleValid(const std::string& key, const std::string& value)
155 {
156  if (key == "subnet") {
157  // example subnet: 10.0.0.0/8
158  return Network::isValidCidr(value);
159  }
160  else {
161  NDN_THROW(std::logic_error("Only supported rules are expected"));
162  }
163 }
164 
165 bool
167 {
168  return this->m_whitelist == other.m_whitelist &&
169  this->m_blacklist == other.m_blacklist;
170 }
171 
172 static bool
173 doesMatchPattern(const std::string& ifname, const std::string& pattern)
174 {
175  // use fnmatch(3) to provide unix glob-style matching for interface names
176  // fnmatch returns 0 if there is a match
177  return ::fnmatch(pattern.data(), ifname.data(), 0) == 0;
178 }
179 
180 static bool
181 doesNetifMatchRule(const ndn::net::NetworkInterface& netif, const std::string& rule)
182 {
183  // if '/' is in rule, this is a subnet, check if IP in subnet
184  if (rule.find('/') != std::string::npos) {
185  Network n = boost::lexical_cast<Network>(rule);
186  for (const auto& addr : netif.getNetworkAddresses()) {
187  if (n.doesContain(addr.getIp())) {
188  return true;
189  }
190  }
191  }
192 
193  return rule == "*" ||
194  doesMatchPattern(netif.getName(), rule) ||
195  netif.getEthernetAddress().toString() == rule;
196 }
197 
198 bool
200 {
201  return std::any_of(m_whitelist.begin(), m_whitelist.end(),
202  [&netif] (const auto& rule) { return doesNetifMatchRule(netif, rule); }) &&
203  std::none_of(m_blacklist.begin(), m_blacklist.end(),
204  [&netif] (const auto& rule) { return doesNetifMatchRule(netif, rule); });
205 }
206 
207 static bool
208 doesAddressMatchRule(const boost::asio::ip::address& address, const std::string& rule)
209 {
210  // if '/' is in rule, this is a subnet, check if IP in subnet
211  if (rule.find('/') != std::string::npos) {
212  Network n = boost::lexical_cast<Network>(rule);
213  if (n.doesContain(address)) {
214  return true;
215  }
216  }
217 
218  return rule == "*";
219 }
220 
221 bool
222 IpAddressPredicate::operator()(const boost::asio::ip::address& address) const
223 {
224  return std::any_of(m_whitelist.begin(), m_whitelist.end(),
225  [&address] (const auto& rule) { return doesAddressMatchRule(address, rule); }) &&
226  std::none_of(m_blacklist.begin(), m_blacklist.end(),
227  [&address] (const auto& rule) { return doesAddressMatchRule(address, rule); });
228 }
229 
230 } // namespace face
231 } // namespace nfd
bool operator()(const boost::asio::ip::address &address) const
static Address fromString(const std::string &str)
Creates an Address from a string containing an Ethernet address in hexadecimal notation, with colons or hyphens as separators.
Definition: ethernet.cpp:90
std::string getName() const
Returns the name of the interface, unique on the system.
bool isNull() const
True if this is a null address (00:00:00:00:00:00)
Definition: ethernet.cpp:72
static bool doesNetifMatchRule(const ndn::net::NetworkInterface &netif, const std::string &rule)
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:78
#define NDN_THROW(e)
Definition: exception.hpp:61
static bool doesAddressMatchRule(const boost::asio::ip::address &address, const std::string &rule)
Represents one network interface attached to the host.
static bool doesMatchPattern(const std::string &ifname, const std::string &pattern)
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
void clear()
Set the whitelist to "*" and clear the blacklist.
static bool isValidCidr(const std::string &cidr)
Definition: network.cpp:62
void assign(std::initializer_list< std::pair< std::string, std::string >> whitelist, std::initializer_list< std::pair< std::string, std::string >> blacklist)
void parseBlacklist(const boost::property_tree::ptree &list)
bool operator==(const NetworkPredicateBase &other) const
void parseWhitelist(const boost::property_tree::ptree &list)
ethernet::Address getEthernetAddress() const
Returns the link-layer (Ethernet) address of the interface.
bool doesContain(const boost::asio::ip::address &address) const
Definition: network.hpp:42
const std::set< NetworkAddress > & getNetworkAddresses() const
Returns a list of all network-layer addresses present on the interface.
bool operator()(const ndn::net::NetworkInterface &netif) const