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