NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
filter.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #ifndef NDN_SECURITY_CONF_FILTER_HPP
25 #define NDN_SECURITY_CONF_FILTER_HPP
26 
27 #include "../../common.hpp"
28 #include "../../data.hpp"
29 #include "../../interest.hpp"
30 #include "../../util/regex.hpp"
31 #include "../security-common.hpp"
32 #include <boost/algorithm/string.hpp>
33 
34 #include "common.hpp"
35 
36 namespace ndn {
37 namespace security {
38 namespace conf {
39 
48 class Filter
49 {
50 public:
51 
52  virtual
54  {
55  }
56 
57  bool
58  match(const Data& data)
59  {
60  return matchName(data.getName());
61  }
62 
63  bool
64  match(const Interest& interest)
65  {
66  if (interest.getName().size() < signed_interest::MIN_LENGTH)
67  return false;
68 
69  Name unsignedName = interest.getName().getPrefix(-signed_interest::MIN_LENGTH);
70  return matchName(unsignedName);
71  }
72 
73 protected:
74  virtual bool
75  matchName(const Name& name) = 0;
76 };
77 
78 class RelationNameFilter : public Filter
79 {
80 public:
81  enum Relation
82  {
85  RELATION_IS_STRICT_PREFIX_OF
86  };
87 
89  : m_name(name)
90  , m_relation(relation)
91  {
92  }
93 
94  virtual
96  {
97  }
98 
99 protected:
100  virtual bool
102  {
103  switch (m_relation)
104  {
105  case RELATION_EQUAL:
106  return (name == m_name);
107  case RELATION_IS_PREFIX_OF:
108  return m_name.isPrefixOf(name);
109  case RELATION_IS_STRICT_PREFIX_OF:
110  return (m_name.isPrefixOf(name) && m_name.size() < name.size());
111  default:
112  return false;
113  }
114  }
115 
116 private:
117  Name m_name;
118  Relation m_relation;
119 };
120 
121 class RegexNameFilter : public Filter
122 {
123 public:
124  explicit
125  RegexNameFilter(const Regex& regex)
126  : m_regex(regex)
127  {
128  }
129 
130  virtual
132  {
133  }
134 
135 protected:
136  virtual bool
138  {
139  return m_regex.match(name);
140  }
141 
142 private:
143  Regex m_regex;
144 };
145 
147 {
148 public:
149  static shared_ptr<Filter>
150  create(const ConfigSection& configSection)
151  {
152  ConfigSection::const_iterator propertyIt = configSection.begin();
153 
154  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type"))
155  BOOST_THROW_EXCEPTION(Error("Expect <filter.type>!"));
156 
157  std::string type = propertyIt->second.data();
158 
159  if (boost::iequals(type, "name"))
160  return createNameFilter(configSection);
161  else
162  BOOST_THROW_EXCEPTION(Error("Unsupported filter.type: " + type));
163  }
164 private:
165  static shared_ptr<Filter>
166  createNameFilter(const ConfigSection& configSection)
167  {
168  ConfigSection::const_iterator propertyIt = configSection.begin();
169  propertyIt++;
170 
171  if (propertyIt == configSection.end())
172  BOOST_THROW_EXCEPTION(Error("Expect more properties for filter(name)"));
173 
174  if (boost::iequals(propertyIt->first, "name"))
175  {
176  // Get filter.name
177  Name name;
178  try
179  {
180  name = Name(propertyIt->second.data());
181  }
182  catch (Name::Error& e)
183  {
184  BOOST_THROW_EXCEPTION(Error("Wrong filter.name: " + propertyIt->second.data()));
185  }
186 
187  propertyIt++;
188 
189  // Get filter.relation
190  if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation"))
191  BOOST_THROW_EXCEPTION(Error("Expect <filter.relation>!"));
192 
193  std::string relationString = propertyIt->second.data();
194  propertyIt++;
195 
197  if (boost::iequals(relationString, "equal"))
199  else if (boost::iequals(relationString, "is-prefix-of"))
201  else if (boost::iequals(relationString, "is-strict-prefix-of"))
203  else
204  BOOST_THROW_EXCEPTION(Error("Unsupported relation: " + relationString));
205 
206 
207  if (propertyIt != configSection.end())
208  BOOST_THROW_EXCEPTION(Error("Expect the end of filter!"));
209 
210  return make_shared<RelationNameFilter>(name, relation);
211  }
212  else if (boost::iequals(propertyIt->first, "regex"))
213  {
214  std::string regexString = propertyIt->second.data();
215  propertyIt++;
216 
217  if (propertyIt != configSection.end())
218  BOOST_THROW_EXCEPTION(Error("Expect the end of filter!"));
219 
220  try
221  {
222  return shared_ptr<RegexNameFilter>(new RegexNameFilter(regexString));
223  }
224  catch (Regex::Error& e)
225  {
226  BOOST_THROW_EXCEPTION(Error("Wrong filter.regex: " + regexString));
227  }
228  }
229  else
230  BOOST_THROW_EXCEPTION(Error("Wrong filter(name) properties"));
231  }
232 };
233 
234 } // namespace conf
235 } // namespace security
236 } // namespace ndn
237 
238 #endif // NDN_SECURITY_CONF_FILTER_HPP
const Name & getName() const
Definition: interest.hpp:216
Copyright (c) 2011-2015 Regents of the University of California.
virtual bool matchName(const Name &name)
Definition: filter.hpp:101
RegexNameFilter(const Regex &regex)
Definition: filter.hpp:125
represents an Interest packet
Definition: interest.hpp:45
Copyright (c) 2013-2014 Regents of the University of California.
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:343
Error that can be thrown from Name.
Definition: name.hpp:52
bool match(const Interest &interest)
Definition: filter.hpp:64
virtual bool matchName(const Name &name)=0
virtual bool matchName(const Name &name)
Definition: filter.hpp:137
bool match(const Data &data)
Definition: filter.hpp:58
size_t size() const
Get the number of components.
Definition: name.hpp:408
Name abstraction to represent an absolute name.
Definition: name.hpp:46
Filter is one of the classes used by ValidatorConfig.
Definition: filter.hpp:48
boost::property_tree::ptree ConfigSection
Definition: common.hpp:35
RelationNameFilter(const Name &name, Relation relation)
Definition: filter.hpp:88
static shared_ptr< Filter > create(const ConfigSection &configSection)
Definition: filter.hpp:150
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:249
represents a Data packet
Definition: data.hpp:39