blob: 6ed58087f808fc55fc7b0b74844137990f1bedc4 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu48e8c0c2014-03-19 12:01:55 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070022 */
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
31namespace ndn {
32namespace security {
33namespace conf {
34
35template<class Packet>
36class Rule
37{
38public:
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
109private:
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