NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
filter.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 "filter.hpp"
23 
24 #include "data.hpp"
25 #include "interest.hpp"
26 #include "util/regex.hpp"
28 
29 #include <boost/algorithm/string.hpp>
30 
31 namespace ndn {
32 namespace security {
33 namespace v2 {
34 namespace validator_config {
35 
36 bool
37 Filter::match(uint32_t pktType, const Name& pktName)
38 {
39  BOOST_ASSERT(pktType == tlv::Interest || pktType == tlv::Data);
40 
41  if (pktType == tlv::Interest) {
42  if (pktName.size() < signed_interest::MIN_SIZE)
43  return false;
44 
45  return matchName(pktName.getPrefix(-signed_interest::MIN_SIZE));
46  }
47  else {
48  return matchName(pktName);
49  }
50 }
51 
53  : m_name(name)
54  , m_relation(relation)
55 {
56 }
57 
58 bool
59 RelationNameFilter::matchName(const Name& name)
60 {
61  return checkNameRelation(m_relation, m_name, name);
62 }
63 
65  : m_regex(regex)
66 {
67 }
68 
69 bool
70 RegexNameFilter::matchName(const Name& name)
71 {
72  return m_regex.match(name);
73 }
74 
75 unique_ptr<Filter>
76 Filter::create(const ConfigSection& configSection, const std::string& configFilename)
77 {
78  auto propertyIt = configSection.begin();
79 
80  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) {
81  BOOST_THROW_EXCEPTION(Error("Expect <filter.type>"));
82  }
83 
84  std::string type = propertyIt->second.data();
85 
86  if (boost::iequals(type, "name"))
87  return createNameFilter(configSection, configFilename);
88  else
89  BOOST_THROW_EXCEPTION(Error("Unsupported filter.type: " + type));
90 }
91 
92 unique_ptr<Filter>
93 Filter::createNameFilter(const ConfigSection& configSection, const std::string& configFilename)
94 {
95  auto propertyIt = configSection.begin();
96  propertyIt++;
97 
98  if (propertyIt == configSection.end()) {
99  BOOST_THROW_EXCEPTION(Error("Expect more properties for filter(name)"));
100  }
101 
102  if (boost::iequals(propertyIt->first, "name")) {
103  // Get filter.name
104  Name name;
105  try {
106  name = Name(propertyIt->second.data());
107  }
108  catch (const Name::Error& e) {
109  BOOST_THROW_EXCEPTION(Error("Wrong filter.name: " + propertyIt->second.data()));
110  }
111 
112  propertyIt++;
113 
114  // Get filter.relation
115  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation")) {
116  BOOST_THROW_EXCEPTION(Error("Expect <filter.relation>"));
117  }
118 
119  NameRelation relation = getNameRelationFromString(propertyIt->second.data());
120  propertyIt++;
121 
122  if (propertyIt != configSection.end())
123  BOOST_THROW_EXCEPTION(Error("Expect the end of filter"));
124 
125  return make_unique<RelationNameFilter>(name, relation);
126  }
127  else if (boost::iequals(propertyIt->first, "regex")) {
128  std::string regexString = propertyIt->second.data();
129  propertyIt++;
130 
131  if (propertyIt != configSection.end())
132  BOOST_THROW_EXCEPTION(Error("Expect the end of filter"));
133 
134  try {
135  return make_unique<RegexNameFilter>(regexString);
136  }
137  catch (const Regex::Error& e) {
138  BOOST_THROW_EXCEPTION(Error("Wrong filter.regex: " + regexString));
139  }
140  }
141  else {
142  BOOST_THROW_EXCEPTION(Error("Wrong filter(name) properties"));
143  }
144 }
145 
146 } // namespace validator_config
147 } // namespace v2
148 } // namespace security
149 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
const size_t MIN_SIZE
minimal number of components for Signed Interest
Catch-all error for security policy errors that don&#39;t fit in other categories.
Definition: base.hpp:79
bool match(uint32_t pktType, const Name &pktName)
Definition: filter.cpp:37
bool checkNameRelation(NameRelation relation, const Name &name1, const Name &name2)
check whether name1 and name2 satisfies relation
NameRelation getNameRelationFromString(const std::string &relationString)
convert relationString to NameRelation
RelationNameFilter(const Name &name, NameRelation relation)
Definition: filter.cpp:52
size_t size() const
Get number of components.
Definition: name.hpp:154
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
bool match(const Name &name)
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:210