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-2019, 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(), bind(&doesNetifMatchRule, std::cref(netif), _1)) &&
202  std::none_of(m_blacklist.begin(), m_blacklist.end(), bind(&doesNetifMatchRule, std::cref(netif), _1));
203 }
204 
205 static bool
206 doesAddressMatchRule(const boost::asio::ip::address& address, const std::string& rule)
207 {
208  // if '/' is in rule, this is a subnet, check if IP in subnet
209  if (rule.find('/') != std::string::npos) {
210  Network n = boost::lexical_cast<Network>(rule);
211  if (n.doesContain(address)) {
212  return true;
213  }
214  }
215 
216  return rule == "*";
217 }
218 
219 bool
220 IpAddressPredicate::operator()(const boost::asio::ip::address& address) const
221 {
222  return std::any_of(m_whitelist.begin(), m_whitelist.end(), bind(&doesAddressMatchRule, std::cref(address), _1)) &&
223  std::none_of(m_blacklist.begin(), m_blacklist.end(), bind(&doesAddressMatchRule, std::cref(address), _1));
224 }
225 
226 } // namespace face
227 } // namespace nfd
nfd::face::NetworkPredicateBase::parseWhitelist
void parseWhitelist(const boost::property_tree::ptree &list)
Definition: network-predicate.cpp:102
config-file.hpp
nfd::face::NetworkPredicateBase::clear
void clear()
Set the whitelist to "*" and clear the blacklist.
Definition: network-predicate.cpp:43
ndn::net::NetworkInterface
Represents one network interface attached to the host.
Definition: network-interface.hpp:70
nfd::face::doesMatchPattern
static bool doesMatchPattern(const std::string &ifname, const std::string &pattern)
Definition: network-predicate.cpp:173
nfd::Network
Definition: network.hpp:34
ndn::ethernet::Address::fromString
static Address fromString(const std::string &str)
Creates an Address from a string containing an Ethernet address in hexadecimal notation,...
Definition: ethernet.cpp:92
network-predicate.hpp
nfd::face::NetworkPredicateBase::assign
void assign(std::initializer_list< std::pair< std::string, std::string >> whitelist, std::initializer_list< std::pair< std::string, std::string >> blacklist)
Definition: network-predicate.cpp:114
nfd::face::NetworkPredicateBase::operator==
bool operator==(const NetworkPredicateBase &other) const
Definition: network-predicate.cpp:166
nfd::Network::isValidCidr
static bool isValidCidr(const std::string &cidr)
Definition: network.cpp:62
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::ethernet::Address::isNull
bool isNull() const
True if this is a null address (00:00:00:00:00:00)
Definition: ethernet.cpp:72
nfd::face::doesNetifMatchRule
static bool doesNetifMatchRule(const ndn::net::NetworkInterface &netif, const std::string &rule)
Definition: network-predicate.cpp:181
nfd::face::NetworkInterfacePredicate::operator()
bool operator()(const ndn::net::NetworkInterface &netif) const
Definition: network-predicate.cpp:199
ndn::net::NetworkInterface::getNetworkAddresses
const std::set< NetworkAddress > & getNetworkAddresses() const
Returns a list of all network-layer addresses present on the interface.
Definition: network-interface.hpp:156
nfd::face::NetworkPredicateBase::NetworkPredicateBase
NetworkPredicateBase()
Definition: network-predicate.cpp:35
network.hpp
nfd::face::IpAddressPredicate::operator()
bool operator()(const boost::asio::ip::address &address) const
Definition: network-predicate.cpp:220
ndn::net::NetworkInterface::getEthernetAddress
ethernet::Address getEthernetAddress() const
Returns the link-layer (Ethernet) address of the interface.
Definition: network-interface.hpp:140
nfd::face::NetworkPredicateBase
Definition: network-predicate.hpp:37
ndn::ethernet::Address::toString
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:78
nfd::face::NetworkPredicateBase::~NetworkPredicateBase
virtual ~NetworkPredicateBase()
nfd::face::NetworkPredicateBase::m_whitelist
std::set< std::string > m_whitelist
Definition: network-predicate.hpp:83
nfd::ConfigFile::Error
Definition: config-file.hpp:61
ndn::net::NetworkInterface::getName
std::string getName() const
Returns the name of the interface, unique on the system.
Definition: network-interface.hpp:100
nfd::Network::doesContain
bool doesContain(const boost::asio::ip::address &address) const
Definition: network.hpp:42
nfd::face::doesAddressMatchRule
static bool doesAddressMatchRule(const boost::asio::ip::address &address, const std::string &rule)
Definition: network-predicate.cpp:206
nfd::face::NetworkPredicateBase::m_blacklist
std::set< std::string > m_blacklist
Definition: network-predicate.hpp:84
nfd::face::NetworkPredicateBase::parseBlacklist
void parseBlacklist(const boost::property_tree::ptree &list)
Definition: network-predicate.cpp:108