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-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 
24 #include "ndn-cxx/data.hpp"
25 #include "ndn-cxx/interest.hpp"
27 #include "ndn-cxx/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  NDN_THROW(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  NDN_THROW(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  NDN_THROW(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  NDN_THROW_NESTED(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  NDN_THROW(Error("Expecting <filter.relation>"));
115  }
116 
117  NameRelation relation = getNameRelationFromString(propertyIt->second.data());
118  propertyIt++;
119 
120  if (propertyIt != configSection.end())
121  NDN_THROW(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  NDN_THROW(Error("Expecting end of <filter>"));
131 
132  try {
133  return make_unique<RegexNameFilter>(Regex(regexString));
134  }
135  catch (const Regex::Error&) {
136  NDN_THROW_NESTED(Error("Invalid <filter.regex>: " + regexString));
137  }
138  }
139  else {
140  NDN_THROW(Error("Unrecognized <filter> property: " + propertyIt->first));
141  }
142 }
143 
144 } // namespace validator_config
145 } // namespace v2
146 } // namespace security
147 } // namespace ndn
filter.hpp
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
ndn::RegexTopMatcher
Definition: regex-top-matcher.hpp:35
ndn::Regex
RegexTopMatcher Regex
Definition: regex.hpp:31
ndn::security::v2::validator_config::checkNameRelation
bool checkNameRelation(NameRelation relation, const Name &name1, const Name &name2)
check whether name1 and name2 satisfies relation
Definition: name-relation.cpp:46
ndn::tlv::Interest
@ Interest
Definition: tlv.hpp:65
security-common.hpp
ndn::Name::size
size_t size() const
Returns the number of components.
Definition: name.hpp:153
ndn::security::v2::validator_config::getNameRelationFromString
NameRelation getNameRelationFromString(const std::string &relationString)
convert relationString to NameRelation
Definition: name-relation.cpp:60
NDN_THROW_NESTED
#define NDN_THROW_NESTED(e)
Definition: exception.hpp:71
ndn::security::v2::validator_config::Filter::match
bool match(uint32_t pktType, const Name &pktName)
Definition: filter.cpp:37
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::Name::getPrefix
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:211
ndn::RegexTopMatcher::match
bool match(const Name &name)
Definition: regex-top-matcher.cpp:65
ndn::name::Component::Error
Definition: name-component.hpp:97
regex.hpp
interest.hpp
ndn::security::v2::validator_config::NameRelation
NameRelation
Definition: name-relation.hpp:33
ndn::tlv::Data
@ Data
Definition: tlv.hpp:66
data.hpp
ndn::name
Definition: name-component-types.hpp:33
ndn::signed_interest::MIN_SIZE
const size_t MIN_SIZE
minimal number of components for Signed Interest
Definition: security-common.hpp:37
ndn::security::v2::validator_config::RegexNameFilter::RegexNameFilter
RegexNameFilter(const Regex &regex)
Definition: filter.cpp:64
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
ndn::security::v2::validator_config::RelationNameFilter::RelationNameFilter
RelationNameFilter(const Name &name, NameRelation relation)
Definition: filter.cpp:52