NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
rule.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
23 #include "ndn-cxx/util/logger.hpp"
24 
25 #include <boost/algorithm/string/predicate.hpp>
26 
27 NDN_LOG_INIT(ndn.security.validator_config.Rule);
28 
29 namespace ndn {
30 namespace security {
31 namespace v2 {
32 namespace validator_config {
33 
34 Rule::Rule(const std::string& id, uint32_t pktType)
35  : m_id(id)
36  , m_pktType(pktType)
37 {
38 }
39 
40 void
41 Rule::addFilter(unique_ptr<Filter> filter)
42 {
43  m_filters.push_back(std::move(filter));
44 }
45 
46 void
47 Rule::addChecker(unique_ptr<Checker> checker)
48 {
49  m_checkers.push_back(std::move(checker));
50 }
51 
52 bool
53 Rule::match(uint32_t pktType, const Name& pktName) const
54 {
55  NDN_LOG_TRACE("Trying to match " << pktName);
56  if (pktType != m_pktType) {
57  NDN_THROW(Error("Invalid packet type supplied (" + to_string(pktType) +
58  " != " + to_string(m_pktType) + ")"));
59  }
60 
61  if (m_filters.empty()) {
62  return true;
63  }
64 
65  bool retval = false;
66  for (const auto& filter : m_filters) {
67  retval |= filter->match(pktType, pktName);
68  if (retval) {
69  break;
70  }
71  }
72  return retval;
73 }
74 
75 bool
76 Rule::check(uint32_t pktType, const Name& pktName, const Name& klName,
77  const shared_ptr<ValidationState>& state) const
78 {
79  NDN_LOG_TRACE("Trying to check " << pktName << " with keyLocator " << klName);
80 
81  if (pktType != m_pktType) {
82  NDN_THROW(Error("Invalid packet type supplied (" + to_string(pktType) +
83  " != " + to_string(m_pktType) + ")"));
84  }
85 
86  bool hasPendingResult = false;
87  for (const auto& checker : m_checkers) {
88  bool result = checker->check(pktType, pktName, klName, state);
89  if (!result) {
90  return result;
91  }
92  hasPendingResult = true;
93  }
94 
95  return hasPendingResult;
96 }
97 
98 unique_ptr<Rule>
99 Rule::create(const ConfigSection& configSection, const std::string& configFilename)
100 {
101  auto propertyIt = configSection.begin();
102 
103  // Get rule.id
104  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "id")) {
105  NDN_THROW(Error("Expecting <rule.id>"));
106  }
107 
108  std::string ruleId = propertyIt->second.data();
109  propertyIt++;
110 
111  // Get rule.for
112  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "for")) {
113  NDN_THROW(Error("Expecting <rule.for> in rule: " + ruleId));
114  }
115 
116  std::string usage = propertyIt->second.data();
117  propertyIt++;
118 
119  bool isForData = false;
120  if (boost::iequals(usage, "data")) {
121  isForData = true;
122  }
123  else if (boost::iequals(usage, "interest")) {
124  isForData = false;
125  }
126  else {
127  NDN_THROW(Error("Unrecognized <rule.for>: " + usage + " in rule: " + ruleId));
128  }
129 
130  auto rule = make_unique<Rule>(ruleId, isForData ? tlv::Data : tlv::Interest);
131 
132  // Get rule.filter(s)
133  for (; propertyIt != configSection.end(); propertyIt++) {
134  if (!boost::iequals(propertyIt->first, "filter")) {
135  if (boost::iequals(propertyIt->first, "checker")) {
136  break;
137  }
138  NDN_THROW(Error("Expecting <rule.filter> in rule: " + ruleId));
139  }
140 
141  rule->addFilter(Filter::create(propertyIt->second, configFilename));
142  }
143 
144  // Get rule.checker(s)
145  bool hasCheckers = false;
146  for (; propertyIt != configSection.end(); propertyIt++) {
147  if (!boost::iequals(propertyIt->first, "checker")) {
148  NDN_THROW(Error("Expecting <rule.checker> in rule: " + ruleId));
149  }
150 
151  rule->addChecker(Checker::create(propertyIt->second, configFilename));
152  hasCheckers = true;
153  }
154 
155  if (propertyIt != configSection.end()) {
156  NDN_THROW(Error("Expecting end of <rule>: " + ruleId));
157  }
158 
159  if (!hasCheckers) {
160  NDN_THROW(Error("No <rule.checker> is specified in rule: " + ruleId));
161  }
162 
163  return rule;
164 }
165 
166 } // namespace validator_config
167 } // namespace v2
168 } // namespace security
169 } // namespace ndn
NDN_LOG_INIT
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:81
ndn::security::v2::validator_config::Filter::create
static unique_ptr< Filter > create(const ConfigSection &configSection, const std::string &configFilename)
Create a filter from the configuration section.
Definition: filter.cpp:76
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
ndn::tlv::Interest
@ Interest
Definition: tlv.hpp:65
ndn::security::v2::validator_config::Rule::addChecker
void addChecker(unique_ptr< Checker > checker)
Definition: rule.cpp:47
ndn::security::v2::validator_config::Rule::m_checkers
std::vector< unique_ptr< Checker > > m_checkers
Definition: rule.hpp:105
ndn::security::v2::validator_config::Rule::check
bool check(uint32_t pktType, const Name &pktName, const Name &klName, const shared_ptr< ValidationState > &state) const
check if packet satisfies rule's condition
Definition: rule.cpp:76
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::security::v2::validator_config::Rule::m_filters
std::vector< unique_ptr< Filter > > m_filters
Definition: rule.hpp:104
ndn::security::v2::validator_config::Rule::m_pktType
uint32_t m_pktType
Definition: rule.hpp:103
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::security::v2::validator_config::Rule::create
static unique_ptr< Rule > create(const ConfigSection &configSection, const std::string &configFilename)
create a rule from configuration section
Definition: rule.cpp:99
logger.hpp
ndn::tlv::Data
@ Data
Definition: tlv.hpp:66
ndn::security
Definition: dummy-keychain.cpp:28
ndn::security::v2::validator_config::Rule::Rule
Rule(const std::string &id, uint32_t pktType)
Definition: rule.cpp:34
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::security::v2::validator_config::Checker::create
static unique_ptr< Checker > create(const ConfigSection &configSection, const std::string &configFilename)
create a checker from configuration section
Definition: checker.cpp:125
ndn::security::v2::validator_config::Rule::match
bool match(uint32_t pktType, const Name &pktName) const
check if the packet name matches rule's filter
Definition: rule.cpp:53
rule.hpp
NDN_LOG_TRACE
#define NDN_LOG_TRACE(expression)
Definition: logger.hpp:98
ndn::security::v2::validator_config::Rule::addFilter
void addFilter(unique_ptr< Filter > filter)
Definition: rule.cpp:41
ndn::security::v2::validator_config::Error
Definition: common.hpp:39
ndn::security::v2::validator_config::ConfigSection
boost::property_tree::ptree ConfigSection
Definition: common.hpp:36
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34