32 #include <boost/lexical_cast.hpp>
51 NetworkPredicateBase::parseList(std::set<std::string>& set,
52 const boost::property_tree::ptree& list,
53 const std::string& section)
57 for (
const auto& item : list) {
58 if (item.first ==
"*") {
60 set.insert(item.first);
63 if (!isRuleSupported(item.first)) {
65 "' in section '" + section +
"'"));
68 auto value = item.second.get_value<std::string>();
69 if (!isRuleValid(item.first, value)) {
70 NDN_THROW(ConfigFile::Error(
"Malformed " + item.first +
" '" + value +
71 "' in section '" + section +
"'"));
79 NetworkPredicateBase::parseList(std::set<std::string>& set,
80 std::initializer_list<std::pair<std::string, std::string>> list)
84 for (
const auto& item : list) {
85 if (item.first ==
"*") {
87 set.insert(item.first);
90 if (!isRuleSupported(item.first)) {
91 NDN_THROW(std::runtime_error(
"Unrecognized rule '" + item.first +
"'"));
94 if (!isRuleValid(item.first, item.second)) {
95 NDN_THROW(std::runtime_error(
"Malformed " + item.first +
" '" + item.second +
"'"));
97 set.insert(item.second);
116 std::initializer_list<std::pair<std::string, std::string>> blacklist)
123 NetworkInterfacePredicate::isRuleSupported(
const std::string& key)
125 return key ==
"ifname" || key ==
"ether" || key ==
"subnet";
129 NetworkInterfacePredicate::isRuleValid(
const std::string& key,
const std::string& value)
131 if (key ==
"ifname") {
133 return !value.empty();
135 else if (key ==
"ether") {
137 return !ndn::ethernet::Address::fromString(value).isNull();
139 else if (key ==
"subnet") {
144 NDN_THROW(std::logic_error(
"Only supported rules are expected"));
149 IpAddressPredicate::isRuleSupported(
const std::string& key)
151 return key ==
"subnet";
155 IpAddressPredicate::isRuleValid(
const std::string& key,
const std::string& value)
157 if (key ==
"subnet") {
162 NDN_THROW(std::logic_error(
"Only supported rules are expected"));
178 return ::fnmatch(pattern.data(), ifname.data(), 0) == 0;
185 if (rule.find(
'/') != std::string::npos) {
186 Network n = boost::lexical_cast<Network>(rule);
187 for (
const auto& addr : netif.getNetworkAddresses()) {
194 return rule ==
"*" ||
196 netif.getEthernetAddress().toString() == rule;
203 [&netif] (
const auto& rule) { return doesNetifMatchRule(netif, rule); }) &&
205 [&netif] (
const auto& rule) { return doesNetifMatchRule(netif, rule); });
212 if (rule.find(
'/') != std::string::npos) {
213 Network n = boost::lexical_cast<Network>(rule);
226 [&address] (
const auto& rule) { return doesAddressMatchRule(address, rule); }) &&
228 [&address] (
const auto& rule) { return doesAddressMatchRule(address, rule); });
bool doesContain(const boost::asio::ip::address &address) const
static bool isValidCidr(std::string_view cidr)
bool operator()(const boost::asio::ip::address &address) const
bool operator()(const ndn::net::NetworkInterface &netif) const
virtual ~NetworkPredicateBase()
void clear()
Set the whitelist to "*" and clear the blacklist.
bool operator==(const NetworkPredicateBase &other) const
void parseWhitelist(const boost::property_tree::ptree &list)
std::set< std::string > m_whitelist
void parseBlacklist(const boost::property_tree::ptree &list)
void assign(std::initializer_list< std::pair< std::string, std::string >> whitelist, std::initializer_list< std::pair< std::string, std::string >> blacklist)
std::set< std::string > m_blacklist
static bool doesNetifMatchRule(const ndn::net::NetworkInterface &netif, const std::string &rule)
static bool doesAddressMatchRule(const boost::asio::ip::address &address, const std::string &rule)
static bool doesMatchPattern(const std::string &ifname, const std::string &pattern)