All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
filter.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #ifndef NDN_SECURITY_CONF_FILTER_HPP
9 #define NDN_SECURITY_CONF_FILTER_HPP
10 
11 #include "../../common.hpp"
12 #include "../../data.hpp"
13 #include "../../interest.hpp"
14 #include "../../util/regex.hpp"
15 #include <boost/algorithm/string.hpp>
16 
17 #include "common.hpp"
18 
19 namespace ndn {
20 namespace security {
21 namespace conf {
22 
23 class Filter
24 {
25 public:
26  virtual
28  {
29  }
30 
31  virtual bool
32  match(const Data& data) = 0;
33 
34  virtual bool
35  match(const Interest& interest) = 0;
36 };
37 
38 class RelationNameFilter : public Filter
39 {
40 public:
41  enum Relation
42  {
46  };
47 
48  RelationNameFilter(const Name& name, Relation relation)
49  : m_name(name)
50  , m_relation(relation)
51  {
52  }
53 
54  virtual
56  {
57  }
58 
59  virtual bool
60  match(const Data& data)
61  {
62  return match(data.getName());
63  }
64 
65  virtual bool
66  match(const Interest& interest)
67  {
68  if (interest.getName().size() < 2)
69  return false;
70 
71  Name signedName = interest.getName().getPrefix(-2);
72  return match(signedName);
73  }
74 
75 private:
76  bool
77  match(const Name& name)
78  {
79  switch (m_relation)
80  {
81  case RELATION_EQUAL:
82  return (name == m_name);
84  return m_name.isPrefixOf(name);
86  return (m_name.isPrefixOf(name) && m_name != name);
87  default:
88  return false;
89  }
90  }
91 
92 private:
93  Name m_name;
94  Relation m_relation;
95 };
96 
97 class RegexNameFilter : public Filter
98 {
99 public:
100  explicit
101  RegexNameFilter(const Regex& regex)
102  : m_regex(regex)
103  {
104  }
105 
106  virtual
108  {
109  }
110 
111  virtual bool
112  match(const Data& data)
113  {
114  return m_regex.match(data.getName());
115  }
116 
117  virtual bool
118  match(const Interest& interest)
119  {
120  if (interest.getName().size() < 2)
121  return false;
122 
123  Name signedName = interest.getName().getPrefix(-2);
124  return m_regex.match(signedName);
125  }
126 
127 private:
128  Regex m_regex;
129 };
130 
132 {
133 public:
134  static shared_ptr<Filter>
135  create(const ConfigSection& configSection)
136  {
137  ConfigSection::const_iterator propertyIt = configSection.begin();
138 
139  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type"))
140  throw Error("Expect <filter.type>!");
141 
142  std::string type = propertyIt->second.data();
143 
144  if (boost::iequals(type, "name"))
145  return createNameFilter(configSection);
146  else
147  throw Error("Unsupported filter.type: " + type);
148  }
149 private:
150  static shared_ptr<Filter>
151  createNameFilter(const ConfigSection& configSection)
152  {
153  ConfigSection::const_iterator propertyIt = configSection.begin();
154  propertyIt++;
155 
156  if (propertyIt == configSection.end())
157  throw Error("Expect more properties for filter(name)");
158 
159  if (boost::iequals(propertyIt->first, "name"))
160  {
161  // Get filter.name
162  Name name;
163  try
164  {
165  name = Name(propertyIt->second.data());
166  }
167  catch (Name::Error& e)
168  {
169  throw Error("Wrong filter.name: " + propertyIt->second.data());
170  }
171 
172  propertyIt++;
173 
174  // Get filter.relation
175  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation"))
176  throw Error("Expect <filter.relation>!");
177 
178  std::string relationString = propertyIt->second.data();
179  propertyIt++;
180 
182  if (boost::iequals(relationString, "equal"))
184  else if (boost::iequals(relationString, "is-prefix-of"))
186  else if (boost::iequals(relationString, "is-strict-prefix-of"))
188  else
189  throw Error("Unsupported relation: " + relationString);
190 
191 
192  if (propertyIt != configSection.end())
193  throw Error("Expect the end of filter!");
194 
195  return make_shared<RelationNameFilter>(boost::cref(name),
196  boost::cref(relation));
197  }
198  else if (boost::iequals(propertyIt->first, "regex"))
199  {
200  std::string regexString = propertyIt->second.data();
201  propertyIt++;
202 
203  if (propertyIt != configSection.end())
204  throw Error("Expect the end of filter!");
205 
206  try
207  {
208  return shared_ptr<RegexNameFilter>(new RegexNameFilter(regexString));
209  }
210  catch (Regex::Error& e)
211  {
212  throw Error("Wrong filter.regex: " + regexString);
213  }
214  }
215  else
216  throw Error("Wrong filter(name) properties");
217  }
218 };
219 
220 } // namespace conf
221 } // namespace security
222 } // namespace ndn
223 
224 #endif // NDN_SECURITY_CONF_FILTER_HPP
const Name & getName() const
Definition: interest.hpp:182
Name getPrefix(int nComponents) const
Return a new Name with the first nComponents components of this Name.
Definition: name.hpp:240
RegexNameFilter(const Regex &regex)
Definition: filter.hpp:101
virtual bool match(const Data &data)=0
An Interest holds a Name and other fields for an interest.
Definition: interest.hpp:24
const Name & getName() const
Definition: data.hpp:346
Error that can be thrown from Name.
Definition: name.hpp:30
virtual bool match(const Data &data)
Definition: filter.hpp:60
size_t size() const
Get the number of components.
Definition: name.hpp:329
virtual bool match(const Interest &interest)
Definition: filter.hpp:66
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
boost::property_tree::ptree ConfigSection
RelationNameFilter(const Name &name, Relation relation)
Definition: filter.hpp:48
bool match(const Name &name)
virtual bool match(const Interest &interest)
Definition: filter.hpp:118
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.hpp:669
static shared_ptr< Filter > create(const ConfigSection &configSection)
Definition: filter.hpp:135
virtual bool match(const Data &data)
Definition: filter.hpp:112