network-interface-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-2017, 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 
27 
28 #include "config-file.hpp"
29 #include "network.hpp"
30 
31 #include <fnmatch.h>
32 
33 namespace nfd {
34 
36 {
37  this->clear();
38 }
39 
40 void
42 {
43  m_whitelist = std::set<std::string>{"*"};
44  m_blacklist.clear();
45 }
46 
47 static void
48 parseList(std::set<std::string>& set, const boost::property_tree::ptree& list, const std::string& section)
49 {
50  set.clear();
51 
52  for (const auto& item : list) {
53  if (item.first == "*") {
54  // insert wildcard
55  set.insert(item.first);
56  }
57  else if (item.first == "ifname") {
58  // very basic sanity check for interface names
59  auto name = item.second.get_value<std::string>();
60  if (name.empty()) {
61  BOOST_THROW_EXCEPTION(ConfigFile::Error("Empty interface name in \"" + section + "\" section"));
62  }
63  set.insert(name);
64  }
65  else if (item.first == "ether") {
66  // validate ethernet address
67  auto addr = item.second.get_value<std::string>();
68  if (ndn::ethernet::Address::fromString(addr).isNull()) {
69  BOOST_THROW_EXCEPTION(ConfigFile::Error("Malformed ether address \"" + addr +
70  "\" in \"" + section + "\" section"));
71  }
72  set.insert(addr);
73  }
74  else if (item.first == "subnet") {
75  // example subnet: 10.0.0.0/8
76  auto cidr = item.second.get_value<std::string>();
77  if (!Network::isValidCidr(cidr)) {
78  BOOST_THROW_EXCEPTION(ConfigFile::Error("Malformed subnet declaration \"" + cidr +
79  "\" in \"" + section + "\" section"));
80  }
81  set.insert(cidr);
82  }
83  }
84 }
85 
86 void
87 NetworkInterfacePredicate::parseWhitelist(const boost::property_tree::ptree& list)
88 {
89  parseList(m_whitelist, list, "whitelist");
90 }
91 
92 void
93 NetworkInterfacePredicate::parseBlacklist(const boost::property_tree::ptree& list)
94 {
95  parseList(m_blacklist, list, "blacklist");
96 }
97 
98 static bool
99 doesMatchPattern(const std::string& ifname, const std::string& pattern)
100 {
101  // use fnmatch(3) to provide unix glob-style matching for interface names
102  // fnmatch returns 0 if there is a match
103  return ::fnmatch(pattern.data(), ifname.data(), 0) == 0;
104 }
105 
106 static bool
107 doesMatchRule(const ndn::net::NetworkInterface& netif, const std::string& rule)
108 {
109  // if '/' is in rule, this is a subnet, check if IP in subnet
110  if (rule.find('/') != std::string::npos) {
111  Network n = boost::lexical_cast<Network>(rule);
112  for (const auto& addr : netif.getNetworkAddresses()) {
113  if (n.doesContain(addr.getIp())) {
114  return true;
115  }
116  }
117  }
118 
119  return rule == "*" ||
120  doesMatchPattern(netif.getName(), rule) ||
121  netif.getEthernetAddress().toString() == rule;
122 }
123 
124 bool
125 NetworkInterfacePredicate::operator()(const ndn::net::NetworkInterface& netif) const
126 {
127  return std::any_of(m_whitelist.begin(), m_whitelist.end(), bind(&doesMatchRule, cref(netif), _1)) &&
128  std::none_of(m_blacklist.begin(), m_blacklist.end(), bind(&doesMatchRule, cref(netif), _1));
129 }
130 
131 bool
133 {
134  return this->m_whitelist == other.m_whitelist &&
135  this->m_blacklist == other.m_blacklist;
136 }
137 
138 } // namespace nfd
void parseWhitelist(const boost::property_tree::ptree &list)
bool operator==(const NetworkInterfacePredicate &other) const
static void parseList(std::set< std::string > &set, const boost::property_tree::ptree &list, const std::string &section)
static bool doesMatchPattern(const std::string &ifname, const std::string &pattern)
static bool isValidCidr(const std::string &cidr)
Definition: network.cpp:62
static bool doesMatchRule(const ndn::net::NetworkInterface &netif, const std::string &rule)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void parseBlacklist(const boost::property_tree::ptree &list)
bool operator()(const ndn::net::NetworkInterface &netif) const
void clear()
Set the whitelist to "*" and clear the blacklist.
bool doesContain(const boost::asio::ip::address &address) const
Definition: network.hpp:42
Represents a predicate to accept or reject a NetworkInterfaceInfo.