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-2020 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 
24 #include "ndn-cxx/data.hpp"
25 #include "ndn-cxx/interest.hpp"
28 #include "ndn-cxx/util/regex.hpp"
29 
30 #include <boost/algorithm/string/predicate.hpp>
31 
32 namespace ndn {
33 namespace security {
34 inline namespace v2 {
35 namespace validator_config {
36 
37 bool
38 Filter::match(uint32_t pktType, const Name& pktName, const shared_ptr<ValidationState>& state)
39 {
40  BOOST_ASSERT(pktType == tlv::Interest || pktType == tlv::Data);
41 
42  if (pktType == tlv::Interest) {
43  auto fmt = state->getTag<SignedInterestFormatTag>();
44  BOOST_ASSERT(fmt);
45 
46  if (*fmt == SignedInterestFormat::V03) {
47  // This check is redundant if parameter digest checking is enabled. However, the parameter
48  // digest checking can be disabled in API.
49  if (pktName.size() == 0 || pktName[-1].type() != tlv::ParametersSha256DigestComponent) {
50  return false;
51  }
52 
53  return matchName(pktName.getPrefix(-1));
54  }
55  else {
56  if (pktName.size() < signed_interest::MIN_SIZE)
57  return false;
58 
59  return matchName(pktName.getPrefix(-signed_interest::MIN_SIZE));
60  }
61  }
62  else {
63  return matchName(pktName);
64  }
65 }
66 
68  : m_name(name)
69  , m_relation(relation)
70 {
71 }
72 
73 bool
74 RelationNameFilter::matchName(const Name& name)
75 {
76  return checkNameRelation(m_relation, m_name, name);
77 }
78 
80  : m_regex(regex)
81 {
82 }
83 
84 bool
85 RegexNameFilter::matchName(const Name& name)
86 {
87  return m_regex.match(name);
88 }
89 
90 unique_ptr<Filter>
91 Filter::create(const ConfigSection& configSection, const std::string& configFilename)
92 {
93  auto propertyIt = configSection.begin();
94 
95  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) {
96  NDN_THROW(Error("Expecting <filter.type>"));
97  }
98 
99  std::string type = propertyIt->second.data();
100  if (boost::iequals(type, "name"))
101  return createNameFilter(configSection, configFilename);
102  else
103  NDN_THROW(Error("Unrecognized <filter.type>: " + type));
104 }
105 
106 unique_ptr<Filter>
107 Filter::createNameFilter(const ConfigSection& configSection, const std::string& configFilename)
108 {
109  auto propertyIt = configSection.begin();
110  propertyIt++;
111 
112  if (propertyIt == configSection.end())
113  NDN_THROW(Error("Unexpected end of <filter>"));
114 
115  if (boost::iequals(propertyIt->first, "name")) {
116  // Get filter.name
117  Name name;
118  try {
119  name = Name(propertyIt->second.data());
120  }
121  catch (const Name::Error&) {
122  NDN_THROW_NESTED(Error("Invalid <filter.name>: " + propertyIt->second.data()));
123  }
124 
125  propertyIt++;
126 
127  // Get filter.relation
128  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation")) {
129  NDN_THROW(Error("Expecting <filter.relation>"));
130  }
131 
132  NameRelation relation = getNameRelationFromString(propertyIt->second.data());
133  propertyIt++;
134 
135  if (propertyIt != configSection.end())
136  NDN_THROW(Error("Expecting end of <filter>"));
137 
138  return make_unique<RelationNameFilter>(name, relation);
139  }
140  else if (boost::iequals(propertyIt->first, "regex")) {
141  std::string regexString = propertyIt->second.data();
142  propertyIt++;
143 
144  if (propertyIt != configSection.end())
145  NDN_THROW(Error("Expecting end of <filter>"));
146 
147  try {
148  return make_unique<RegexNameFilter>(Regex(regexString));
149  }
150  catch (const Regex::Error&) {
151  NDN_THROW_NESTED(Error("Invalid <filter.regex>: " + regexString));
152  }
153  }
154  else {
155  NDN_THROW(Error("Unrecognized <filter> property: " + propertyIt->first));
156  }
157 }
158 
159 } // namespace validator_config
160 } // inline namespace v2
161 } // namespace security
162 } // namespace ndn
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
bool match(uint32_t pktType, const Name &pktName, const shared_ptr< ValidationState > &state)
Definition: filter.cpp:38
Sign Interest using Packet Specification v0.3 semantics.
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:209
Copyright (c) 2011-2015 Regents of the University of California.
const size_t MIN_SIZE
minimal number of components for Signed Interest
#define NDN_THROW(e)
Definition: exception.hpp:61
provides a tag type for simple types
Definition: tag.hpp:58
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:67
Represents an absolute name.
Definition: name.hpp:41
static unique_ptr< Filter > create(const ConfigSection &configSection, const std::string &configFilename)
Create a filter from the configuration section.
Definition: filter.cpp:91
size_t size() const
Returns the number of components.
Definition: name.hpp:151
boost::property_tree::ptree ConfigSection
Definition: common.hpp:36
bool match(const Name &name)
RegexTopMatcher Regex
Definition: regex.hpp:31