Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
| 11 | * |
| 12 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NDN_SECURITY_CONF_RULE_HPP |
| 16 | #define NDN_SECURITY_CONF_RULE_HPP |
| 17 | |
| 18 | #include "filter.hpp" |
| 19 | #include "checker.hpp" |
| 20 | |
| 21 | |
| 22 | namespace ndn { |
| 23 | namespace security { |
| 24 | namespace conf { |
| 25 | |
| 26 | template<class Packet> |
| 27 | class Rule |
| 28 | { |
| 29 | public: |
| 30 | Rule(const std::string& id) |
| 31 | : m_id(id) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | virtual |
| 36 | ~Rule() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | const std::string& |
| 41 | getId() |
| 42 | { |
| 43 | return m_id; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | addFilter(const shared_ptr<Filter>& filter) |
| 48 | { |
| 49 | m_filters.push_back(filter); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | addChecker(const shared_ptr<Checker>& checker) |
| 54 | { |
| 55 | m_checkers.push_back(checker); |
| 56 | } |
| 57 | |
| 58 | bool |
| 59 | match(const Packet& packet) |
| 60 | { |
| 61 | if (m_filters.empty()) |
| 62 | return true; |
| 63 | |
| 64 | for (FilterList::iterator it = m_filters.begin(); |
| 65 | it != m_filters.end(); it++) |
| 66 | { |
| 67 | if (!(*it)->match(packet)) |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @brief check if packet satisfies certain condition |
| 76 | * |
| 77 | * @param packet The packet |
| 78 | * @param onValidated Callback function which is called when packet is immediately valid |
| 79 | * @param onValidationFailed Call function which is called when packet is immediately invalid |
| 80 | * @return -1 if packet is immediately invalid (onValidationFailed has been called) |
| 81 | * 1 if packet is immediately valid (onValidated has been called) |
| 82 | * 0 if further signature verification is needed. |
| 83 | */ |
| 84 | template<class ValidatedCallback, class ValidationFailureCallback> |
| 85 | int8_t |
| 86 | check(const Packet& packet, |
| 87 | const ValidatedCallback& onValidated, |
| 88 | const ValidationFailureCallback& onValidationFailed) |
| 89 | { |
| 90 | for (CheckerList::iterator it = m_checkers.begin(); |
| 91 | it != m_checkers.end(); it++) |
| 92 | { |
| 93 | int8_t result = (*it)->check(packet, onValidated, onValidationFailed); |
| 94 | if (result >= 0) |
| 95 | return result; |
| 96 | } |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | typedef std::vector<shared_ptr<Filter> > FilterList; |
| 102 | typedef std::vector<shared_ptr<Checker> > CheckerList; |
| 103 | |
| 104 | std::string m_id; |
| 105 | FilterList m_filters; |
| 106 | CheckerList m_checkers; |
| 107 | }; |
| 108 | |
| 109 | } // namespace conf |
| 110 | } // namespace security |
| 111 | } // namespace ndn |
| 112 | |
| 113 | #endif // NDN_SECURITY_CONF_RULE_HPP |