NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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-2018 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"
27 #include "util/regex.hpp"
28 
29 #include <boost/algorithm/string/predicate.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("Expecting <filter.type>"));
82  }
83 
84  std::string type = propertyIt->second.data();
85  if (boost::iequals(type, "name"))
86  return createNameFilter(configSection, configFilename);
87  else
88  BOOST_THROW_EXCEPTION(Error("Unrecognized <filter.type>: " + type));
89 }
90 
91 unique_ptr<Filter>
92 Filter::createNameFilter(const ConfigSection& configSection, const std::string& configFilename)
93 {
94  auto propertyIt = configSection.begin();
95  propertyIt++;
96 
97  if (propertyIt == configSection.end())
98  BOOST_THROW_EXCEPTION(Error("Unexpected end of <filter>"));
99 
100  if (boost::iequals(propertyIt->first, "name")) {
101  // Get filter.name
102  Name name;
103  try {
104  name = Name(propertyIt->second.data());
105  }
106  catch (const Name::Error&) {
107  BOOST_THROW_EXCEPTION(Error("Invalid <filter.name>: " + propertyIt->second.data()));
108  }
109 
110  propertyIt++;
111 
112  // Get filter.relation
113  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation")) {
114  BOOST_THROW_EXCEPTION(Error("Expecting <filter.relation>"));
115  }
116 
117  NameRelation relation = getNameRelationFromString(propertyIt->second.data());
118  propertyIt++;
119 
120  if (propertyIt != configSection.end())
121  BOOST_THROW_EXCEPTION(Error("Expecting end of <filter>"));
122 
123  return make_unique<RelationNameFilter>(name, relation);
124  }
125  else if (boost::iequals(propertyIt->first, "regex")) {
126  std::string regexString = propertyIt->second.data();
127  propertyIt++;
128 
129  if (propertyIt != configSection.end())
130  BOOST_THROW_EXCEPTION(Error("Expecting end of <filter>"));
131 
132  try {
133  return make_unique<RegexNameFilter>(Regex(regexString));
134  }
135  catch (const Regex::Error&) {
136  BOOST_THROW_EXCEPTION(Error("Invalid <filter.regex>: " + regexString));
137  }
138  }
139  else {
140  BOOST_THROW_EXCEPTION(Error("Unrecognized <filter> property: " + propertyIt->first));
141  }
142 }
143 
144 } // namespace validator_config
145 } // namespace v2
146 } // namespace security
147 } // namespace ndn
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
Definition: name.hpp:210
Copyright (c) 2011-2015 Regents of the University of California.
const size_t MIN_SIZE
minimal number of components for Signed Interest
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
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
size_t size() const
Get number of components.
Definition: name.hpp:154
boost::property_tree::ptree ConfigSection
Definition: common.hpp:35
bool match(const Name &name)
RegexTopMatcher Regex
Definition: regex.hpp:31