blob: 31d81f70d0cdcfcca47f057c929d19f64ac5795c [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:
Alexander Afanasyeva4297a62014-06-19 13:29:34 -070039 explicit
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070040 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
110private:
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