Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 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 | |
| 22 | #include "filter.hpp" |
| 23 | |
| 24 | #include "data.hpp" |
| 25 | #include "interest.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 26 | #include "security/security-common.hpp" |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 27 | #include "util/regex.hpp" |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 29 | #include <boost/algorithm/string/predicate.hpp> |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 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 | |
| 52 | RelationNameFilter::RelationNameFilter(const Name& name, NameRelation relation) |
| 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 | |
| 64 | RegexNameFilter::RegexNameFilter(const Regex& regex) |
| 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")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 81 | BOOST_THROW_EXCEPTION(Error("Expecting <filter.type>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | std::string type = propertyIt->second.data(); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 85 | if (boost::iequals(type, "name")) |
| 86 | return createNameFilter(configSection, configFilename); |
| 87 | else |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 88 | BOOST_THROW_EXCEPTION(Error("Unrecognized <filter.type>: " + type)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 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 | |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 97 | if (propertyIt == configSection.end()) |
| 98 | BOOST_THROW_EXCEPTION(Error("Unexpected end of <filter>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 99 | |
| 100 | if (boost::iequals(propertyIt->first, "name")) { |
| 101 | // Get filter.name |
| 102 | Name name; |
| 103 | try { |
| 104 | name = Name(propertyIt->second.data()); |
| 105 | } |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 106 | catch (const Name::Error&) { |
| 107 | BOOST_THROW_EXCEPTION(Error("Invalid <filter.name>: " + propertyIt->second.data())); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | propertyIt++; |
| 111 | |
| 112 | // Get filter.relation |
| 113 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "relation")) { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 114 | BOOST_THROW_EXCEPTION(Error("Expecting <filter.relation>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | NameRelation relation = getNameRelationFromString(propertyIt->second.data()); |
| 118 | propertyIt++; |
| 119 | |
| 120 | if (propertyIt != configSection.end()) |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 121 | BOOST_THROW_EXCEPTION(Error("Expecting end of <filter>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 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()) |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 130 | BOOST_THROW_EXCEPTION(Error("Expecting end of <filter>")); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 131 | |
| 132 | try { |
Davide Pesavento | 45ab9a9 | 2017-11-05 19:34:31 -0500 | [diff] [blame] | 133 | return make_unique<RegexNameFilter>(Regex(regexString)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 134 | } |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 135 | catch (const Regex::Error&) { |
| 136 | BOOST_THROW_EXCEPTION(Error("Invalid <filter.regex>: " + regexString)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | else { |
Davide Pesavento | 5df42a8 | 2018-03-08 20:06:51 -0500 | [diff] [blame] | 140 | BOOST_THROW_EXCEPTION(Error("Unrecognized <filter> property: " + propertyIt->first)); |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | } // namespace validator_config |
| 145 | } // namespace v2 |
| 146 | } // namespace security |
| 147 | } // namespace ndn |