NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
network-interface-predicate.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
27 
28 #include "config-file.hpp"
29 #include "network-interface.hpp"
30 #include "network.hpp"
31 
32 namespace nfd {
33 
35 {
36  this->clear();
37 }
38 
39 void
41 {
42  m_whitelist = std::set<std::string>{"*"};
43  m_blacklist.clear();
44 }
45 
46 static void
47 parseList(std::set<std::string>& set, const boost::property_tree::ptree& list, const std::string& section)
48 {
49  set.clear();
50 
51  for (const auto& item : list) {
52  if (item.first == "*") {
53  // insert wildcard
54  set.insert(item.first);
55  }
56  else if (item.first == "ifname") {
57  // very basic sanity check for interface names
58  auto name = item.second.get_value<std::string>();
59  if (name.empty()) {
60  BOOST_THROW_EXCEPTION(ConfigFile::Error("Empty interface name in \"" + section + "\" section"));
61  }
62  set.insert(name);
63  }
64  else if (item.first == "ether") {
65  // validate ethernet address
66  auto addr = item.second.get_value<std::string>();
68  BOOST_THROW_EXCEPTION(ConfigFile::Error("Malformed ether address \"" + addr +
69  "\" in \"" + section + "\" section"));
70  }
71  set.insert(addr);
72  }
73  else if (item.first == "subnet") {
74  // example subnet: 10.0.0.0/8
75  auto cidr = item.second.get_value<std::string>();
76  if (!Network::isValidCidr(cidr)) {
77  BOOST_THROW_EXCEPTION(ConfigFile::Error("Malformed subnet declaration \"" + cidr +
78  "\" in \"" + section + "\" section"));
79  }
80  set.insert(cidr);
81  }
82  }
83 }
84 
85 void
86 NetworkInterfacePredicate::parseWhitelist(const boost::property_tree::ptree& list)
87 {
88  parseList(m_whitelist, list, "whitelist");
89 }
90 
91 void
92 NetworkInterfacePredicate::parseBlacklist(const boost::property_tree::ptree& list)
93 {
94  parseList(m_blacklist, list, "blacklist");
95 }
96 
97 static bool
98 doesMatchRule(const NetworkInterfaceInfo& nic, const std::string& rule)
99 {
100  // if '/' is in rule, this is a subnet, check if IP in subnet
101  if (rule.find('/') != std::string::npos) {
102  Network n = boost::lexical_cast<Network>(rule);
103  for (const auto& addr : nic.ipv4Addresses) {
104  if (n.doesContain(addr)) {
105  return true;
106  }
107  }
108  }
109 
110  return rule == "*" ||
111  nic.name == rule ||
112  nic.etherAddress.toString() == rule;
113 }
114 
115 bool
117 {
118  return std::any_of(m_whitelist.begin(), m_whitelist.end(), bind(&doesMatchRule, nic, _1)) &&
119  std::none_of(m_blacklist.begin(), m_blacklist.end(), bind(&doesMatchRule, nic, _1));
120 }
121 
122 } // namespace nfd
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:86
contains information about a network interface
static bool doesMatchRule(const NetworkInterfaceInfo &nic, const std::string &rule)
ethernet::Address etherAddress
static void parseList(std::set< std::string > &set, const boost::property_tree::ptree &list, const std::string &section)
void parseWhitelist(const boost::property_tree::ptree &list)
void clear()
Set the whitelist to "*" and clear the blacklist.
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
bool operator()(const NetworkInterfaceInfo &nic) const
std::vector< boost::asio::ip::address_v4 > ipv4Addresses
static bool isValidCidr(const std::string &cidr)
Definition: network.cpp:61
void parseBlacklist(const boost::property_tree::ptree &list)
bool isNull() const
True if this is a null address (00:00:00:00:00:00)
Definition: ethernet.cpp:66
bool doesContain(const boost::asio::ip::address &address) const
Definition: network.hpp:42
std::string toString(char sep=':') const
Converts the address to a human-readable string.
Definition: ethernet.cpp:72