Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 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" |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 31 | #include "../security-common.hpp" |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 32 | #include <boost/algorithm/string.hpp> |
| 33 | |
| 34 | #include "common.hpp" |
| 35 | |
| 36 | namespace ndn { |
| 37 | namespace security { |
| 38 | namespace conf { |
| 39 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 40 | /** |
| 41 | * @brief Filter is one of the classes used by ValidatorConfig. |
| 42 | * |
| 43 | * The ValidatorConfig class consists of a set of rules. |
| 44 | * The Filter class is a part of a rule and is used to match packet. |
| 45 | * Matched packets will be checked against the checkers defined in the rule. |
| 46 | */ |
| 47 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 48 | class Filter |
| 49 | { |
| 50 | public: |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 51 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 52 | virtual |
| 53 | ~Filter() |
| 54 | { |
| 55 | } |
| 56 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 57 | bool |
| 58 | match(const Data& data) |
| 59 | { |
| 60 | return matchName(data.getName()); |
| 61 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 62 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 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: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 74 | virtual bool |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 75 | matchName(const Name& name) = 0; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | class RelationNameFilter : public Filter |
| 79 | { |
| 80 | public: |
| 81 | enum Relation |
| 82 | { |
| 83 | RELATION_EQUAL, |
| 84 | RELATION_IS_PREFIX_OF, |
Alexander Afanasyev | b78bc4d | 2014-04-09 21:20:52 -0700 | [diff] [blame] | 85 | RELATION_IS_STRICT_PREFIX_OF |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | RelationNameFilter(const Name& name, Relation relation) |
| 89 | : m_name(name) |
| 90 | , m_relation(relation) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | virtual |
| 95 | ~RelationNameFilter() |
| 96 | { |
| 97 | } |
| 98 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 99 | protected: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 100 | virtual bool |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 101 | matchName(const Name& name) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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: |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 110 | return (m_name.isPrefixOf(name) && m_name.size() < name.size()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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 |
| 131 | ~RegexNameFilter() |
| 132 | { |
| 133 | } |
| 134 | |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 135 | protected: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 136 | virtual bool |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 137 | matchName(const Name& name) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 138 | { |
Yingdi Yu | 0f5fb69 | 2014-06-10 12:07:28 -0700 | [diff] [blame^] | 139 | return m_regex.match(name); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | private: |
| 143 | Regex m_regex; |
| 144 | }; |
| 145 | |
| 146 | class FilterFactory |
| 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 | throw 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 | throw 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 | throw 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 | throw 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 | throw Error("Expect <filter.relation>!"); |
| 192 | |
| 193 | std::string relationString = propertyIt->second.data(); |
| 194 | propertyIt++; |
| 195 | |
| 196 | RelationNameFilter::Relation relation; |
| 197 | if (boost::iequals(relationString, "equal")) |
| 198 | relation = RelationNameFilter::RELATION_EQUAL; |
| 199 | else if (boost::iequals(relationString, "is-prefix-of")) |
| 200 | relation = RelationNameFilter::RELATION_IS_PREFIX_OF; |
| 201 | else if (boost::iequals(relationString, "is-strict-prefix-of")) |
| 202 | relation = RelationNameFilter::RELATION_IS_STRICT_PREFIX_OF; |
| 203 | else |
| 204 | throw Error("Unsupported relation: " + relationString); |
| 205 | |
| 206 | |
| 207 | if (propertyIt != configSection.end()) |
| 208 | throw Error("Expect the end of filter!"); |
| 209 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 210 | return make_shared<RelationNameFilter>(name, relation); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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 | throw Error("Expect the end of filter!"); |
| 219 | |
| 220 | try |
| 221 | { |
| 222 | return shared_ptr<RegexNameFilter>(new RegexNameFilter(regexString)); |
| 223 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 224 | catch (Regex::Error& e) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 225 | { |
| 226 | throw Error("Wrong filter.regex: " + regexString); |
| 227 | } |
| 228 | } |
| 229 | else |
| 230 | throw 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 |