NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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 
22 #include "rule.hpp"
23 #include "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  BOOST_THROW_EXCEPTION(Error("Invalid packet type supplied (" +
58  to_string(pktType) + " != " + 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, const shared_ptr<ValidationState>& state) const
77 {
78  NDN_LOG_TRACE("Trying to check " << pktName << " with keyLocator " << klName);
79 
80  if (pktType != m_pktType) {
81  BOOST_THROW_EXCEPTION(Error("Invalid packet type supplied (" +
82  to_string(pktType) + " != " + to_string(m_pktType) + ")"));
83  }
84 
85  bool hasPendingResult = false;
86  for (const auto& checker : m_checkers) {
87  bool result = checker->check(pktType, pktName, klName, state);
88  if (!result) {
89  return result;
90  }
91  hasPendingResult = true;
92  }
93 
94  return hasPendingResult;
95 }
96 
97 unique_ptr<Rule>
98 Rule::create(const ConfigSection& configSection, const std::string& configFilename)
99 {
100  auto propertyIt = configSection.begin();
101 
102  // Get rule.id
103  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "id")) {
104  BOOST_THROW_EXCEPTION(Error("Expecting <rule.id>"));
105  }
106 
107  std::string ruleId = propertyIt->second.data();
108  propertyIt++;
109 
110  // Get rule.for
111  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "for")) {
112  BOOST_THROW_EXCEPTION(Error("Expecting <rule.for> in rule: " + ruleId));
113  }
114 
115  std::string usage = propertyIt->second.data();
116  propertyIt++;
117 
118  bool isForData = false;
119  if (boost::iequals(usage, "data")) {
120  isForData = true;
121  }
122  else if (boost::iequals(usage, "interest")) {
123  isForData = false;
124  }
125  else {
126  BOOST_THROW_EXCEPTION(Error("Unrecognized <rule.for>: " + usage + " in rule: " + ruleId));
127  }
128 
129  auto rule = make_unique<Rule>(ruleId, isForData ? tlv::Data : tlv::Interest);
130 
131  // Get rule.filter(s)
132  for (; propertyIt != configSection.end(); propertyIt++) {
133  if (!boost::iequals(propertyIt->first, "filter")) {
134  if (boost::iequals(propertyIt->first, "checker")) {
135  break;
136  }
137  BOOST_THROW_EXCEPTION(Error("Expecting <rule.filter> in rule: " + ruleId));
138  }
139 
140  rule->addFilter(Filter::create(propertyIt->second, configFilename));
141  }
142 
143  // Get rule.checker(s)
144  bool hasCheckers = false;
145  for (; propertyIt != configSection.end(); propertyIt++) {
146  if (!boost::iequals(propertyIt->first, "checker")) {
147  BOOST_THROW_EXCEPTION(Error("Expecting <rule.checker> in rule: " + ruleId));
148  }
149 
150  rule->addChecker(Checker::create(propertyIt->second, configFilename));
151  hasCheckers = true;
152  }
153 
154  // Check other stuff
155  if (propertyIt != configSection.end()) {
156  BOOST_THROW_EXCEPTION(Error("Expecting the end of rule: " + ruleId));
157  }
158 
159  if (!hasCheckers) {
160  BOOST_THROW_EXCEPTION(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
Rule(const std::string &id, uint32_t pktType)
Definition: rule.cpp:34
Copyright (c) 2011-2015 Regents of the University of California.
static unique_ptr< Rule > create(const ConfigSection &configSection, const std::string &configFilename)
create a rule from configuration section
Definition: rule.cpp:98
Catch-all error for security policy errors that don&#39;t fit in other categories.
Definition: base.hpp:79
#define NDN_LOG_INIT(name)
declare a log module
Definition: logger.hpp:32
std::vector< unique_ptr< Checker > > m_checkers
Definition: rule.hpp:105
void addFilter(unique_ptr< Filter > filter)
Definition: rule.cpp:41
Represents an absolute name.
Definition: name.hpp:42
static unique_ptr< Filter > create(const ConfigSection &configSection, const std::string &configFilename)
Create a filter from the configuration section.
Definition: filter.cpp:76
boost::property_tree::ptree ConfigSection
Definition: common.hpp:35
void addChecker(unique_ptr< Checker > checker)
Definition: rule.cpp:47
std::string to_string(const V &v)
Definition: backports.hpp:84
bool check(uint32_t pktType, const Name &pktName, const Name &klName, const shared_ptr< ValidationState > &state) const
check if packet satisfies rule&#39;s condition
Definition: rule.cpp:76
#define NDN_LOG_TRACE(expression)
Definition: logger.hpp:34
std::vector< unique_ptr< Filter > > m_filters
Definition: rule.hpp:104
static unique_ptr< Checker > create(const ConfigSection &configSection, const std::string &configFilename)
create a checker from configuration section
Definition: checker.cpp:126
bool match(uint32_t pktType, const Name &pktName) const
check if the packet name matches rule&#39;s filter
Definition: rule.cpp:53