susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 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-interface-predicate.hpp" |
| 27 | |
| 28 | #include "config-file.hpp" |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 29 | #include "network.hpp" |
| 30 | |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 31 | #include <fnmatch.h> |
| 32 | |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 33 | namespace nfd { |
| 34 | |
| 35 | NetworkInterfacePredicate::NetworkInterfacePredicate() |
| 36 | { |
| 37 | this->clear(); |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | NetworkInterfacePredicate::clear() |
| 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>(); |
Junxiao Shi | a5765d6 | 2017-08-09 04:07:46 +0000 | [diff] [blame^] | 68 | if (ndn::ethernet::Address::fromString(addr).isNull()) { |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 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 |
susmit | 21f22e6 | 2017-03-25 15:44:45 -0500 | [diff] [blame] | 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 |
Junxiao Shi | a5765d6 | 2017-08-09 04:07:46 +0000 | [diff] [blame^] | 107 | doesMatchRule(const ndn::net::NetworkInterface& netif, const std::string& rule) |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 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 | |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 124 | bool |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 125 | NetworkInterfacePredicate::operator()(const ndn::net::NetworkInterface& netif) const |
| 126 | { |
Junxiao Shi | a5765d6 | 2017-08-09 04:07:46 +0000 | [diff] [blame^] | 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)); |
Junxiao Shi | 2760adc | 2017-07-06 05:44:52 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 132 | NetworkInterfacePredicate::operator==(const NetworkInterfacePredicate& other) const |
| 133 | { |
| 134 | return this->m_whitelist == other.m_whitelist && |
| 135 | this->m_blacklist == other.m_blacklist; |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // namespace nfd |