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_RULE_HPP |
| 25 | #define NDN_SECURITY_CONF_RULE_HPP |
| 26 | |
| 27 | #include "filter.hpp" |
| 28 | #include "checker.hpp" |
| 29 | |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace security { |
| 33 | namespace conf { |
| 34 | |
| 35 | template<class Packet> |
| 36 | class Rule |
| 37 | { |
| 38 | public: |
Alexander Afanasyev | a4297a6 | 2014-06-19 13:29:34 -0700 | [diff] [blame] | 39 | explicit |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 40 | Rule(const std::string& id) |
| 41 | : m_id(id) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | virtual |
| 46 | ~Rule() |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | const std::string& |
| 51 | getId() |
| 52 | { |
| 53 | return m_id; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | addFilter(const shared_ptr<Filter>& filter) |
| 58 | { |
| 59 | m_filters.push_back(filter); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | addChecker(const shared_ptr<Checker>& checker) |
| 64 | { |
| 65 | m_checkers.push_back(checker); |
| 66 | } |
| 67 | |
| 68 | bool |
| 69 | match(const Packet& packet) |
| 70 | { |
| 71 | if (m_filters.empty()) |
| 72 | return true; |
| 73 | |
| 74 | for (FilterList::iterator it = m_filters.begin(); |
| 75 | it != m_filters.end(); it++) |
| 76 | { |
| 77 | if (!(*it)->match(packet)) |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @brief check if packet satisfies certain condition |
| 86 | * |
| 87 | * @param packet The packet |
| 88 | * @param onValidated Callback function which is called when packet is immediately valid |
| 89 | * @param onValidationFailed Call function which is called when packet is immediately invalid |
| 90 | * @return -1 if packet is immediately invalid (onValidationFailed has been called) |
| 91 | * 1 if packet is immediately valid (onValidated has been called) |
| 92 | * 0 if further signature verification is needed. |
| 93 | */ |
| 94 | template<class ValidatedCallback, class ValidationFailureCallback> |
| 95 | int8_t |
| 96 | check(const Packet& packet, |
| 97 | const ValidatedCallback& onValidated, |
| 98 | const ValidationFailureCallback& onValidationFailed) |
| 99 | { |
| 100 | for (CheckerList::iterator it = m_checkers.begin(); |
| 101 | it != m_checkers.end(); it++) |
| 102 | { |
| 103 | int8_t result = (*it)->check(packet, onValidated, onValidationFailed); |
| 104 | if (result >= 0) |
| 105 | return result; |
| 106 | } |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | typedef std::vector<shared_ptr<Filter> > FilterList; |
| 112 | typedef std::vector<shared_ptr<Checker> > CheckerList; |
| 113 | |
| 114 | std::string m_id; |
| 115 | FilterList m_filters; |
| 116 | CheckerList m_checkers; |
| 117 | }; |
| 118 | |
| 119 | } // namespace conf |
| 120 | } // namespace security |
| 121 | } // namespace ndn |
| 122 | |
| 123 | #endif // NDN_SECURITY_CONF_RULE_HPP |