32 #include <boost/lexical_cast.hpp>
49 NetworkPredicateBase::parseList(std::set<std::string>& set,
50 const boost::property_tree::ptree& list,
51 const std::string& section)
55 for (
const auto& item : list) {
56 if (item.first ==
"*") {
58 set.insert(item.first);
61 if (!isRuleSupported(item.first)) {
63 "' in section '" + section +
"'"));
66 auto value = item.second.get_value<std::string>();
67 if (!isRuleValid(item.first, value)) {
68 NDN_THROW(ConfigFile::Error(
"Malformed " + item.first +
" '" + value +
69 "' in section '" + section +
"'"));
77 NetworkPredicateBase::parseList(std::set<std::string>& set,
78 std::initializer_list<std::pair<std::string, std::string>> list)
82 for (
const auto& item : list) {
83 if (item.first ==
"*") {
85 set.insert(item.first);
88 if (!isRuleSupported(item.first)) {
89 NDN_THROW(std::runtime_error(
"Unrecognized rule '" + item.first +
"'"));
92 if (!isRuleValid(item.first, item.second)) {
93 NDN_THROW(std::runtime_error(
"Malformed " + item.first +
" '" + item.second +
"'"));
95 set.insert(item.second);
114 std::initializer_list<std::pair<std::string, std::string>> blacklist)
121 NetworkInterfacePredicate::isRuleSupported(
const std::string& key)
123 return key ==
"ifname" || key ==
"ether" || key ==
"subnet";
127 NetworkInterfacePredicate::isRuleValid(
const std::string& key,
const std::string& value)
129 if (key ==
"ifname") {
131 return !value.empty();
133 else if (key ==
"ether") {
135 return !ndn::ethernet::Address::fromString(value).isNull();
137 else if (key ==
"subnet") {
142 NDN_THROW(std::logic_error(
"Only supported rules are expected"));
147 IpAddressPredicate::isRuleSupported(
const std::string& key)
149 return key ==
"subnet";
153 IpAddressPredicate::isRuleValid(
const std::string& key,
const std::string& value)
155 if (key ==
"subnet") {
160 NDN_THROW(std::logic_error(
"Only supported rules are expected"));
169 return ::fnmatch(pattern.data(), ifname.data(), 0) == 0;
176 if (rule.find(
'/') != std::string::npos) {
177 Network n = boost::lexical_cast<Network>(rule);
178 for (
const auto& addr : netif.getNetworkAddresses()) {
185 return rule ==
"*" ||
187 netif.getEthernetAddress().toString() == rule;
194 [&netif] (
const auto& rule) { return doesNetifMatchRule(netif, rule); }) &&
196 [&netif] (
const auto& rule) { return doesNetifMatchRule(netif, rule); });
203 if (rule.find(
'/') != std::string::npos) {
204 Network n = boost::lexical_cast<Network>(rule);
217 [&address] (
const auto& rule) { return doesAddressMatchRule(address, rule); }) &&
219 [&address] (
const auto& rule) { return doesAddressMatchRule(address, rule); });
static bool isValidCidr(std::string_view cidr) noexcept
bool doesContain(const boost::asio::ip::address &address) const noexcept
bool operator()(const boost::asio::ip::address &address) const
bool operator()(const ndn::net::NetworkInterface &netif) const
void clear()
Set the whitelist to "*" and clear the blacklist.
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)