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