blob: 03af661f4a709dee4b2cd4f06ce8f2994f5c2bd0 [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/**
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -07003 * Copyright (c) 2013-2016 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/>
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -070022 * @author Zhiyi Zhang <zhangzhiyi1919@gmail.com>
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070023 */
24
25#ifndef NDN_SECURITY_CONF_RULE_HPP
26#define NDN_SECURITY_CONF_RULE_HPP
27
28#include "filter.hpp"
29#include "checker.hpp"
30
31
32namespace ndn {
33namespace security {
34namespace conf {
35
36template<class Packet>
37class Rule
38{
39public:
Alexander Afanasyeva4297a62014-06-19 13:29:34 -070040 explicit
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070041 Rule(const std::string& id)
42 : m_id(id)
43 {
44 }
45
46 virtual
47 ~Rule()
48 {
49 }
50
51 const std::string&
52 getId()
53 {
54 return m_id;
55 }
56
57 void
58 addFilter(const shared_ptr<Filter>& filter)
59 {
60 m_filters.push_back(filter);
61 }
62
63 void
64 addChecker(const shared_ptr<Checker>& checker)
65 {
66 m_checkers.push_back(checker);
67 }
68
69 bool
70 match(const Packet& packet)
71 {
72 if (m_filters.empty())
73 return true;
74
75 for (FilterList::iterator it = m_filters.begin();
76 it != m_filters.end(); it++)
77 {
78 if (!(*it)->match(packet))
79 return false;
80 }
81
82 return true;
83 }
84
85 /**
86 * @brief check if packet satisfies certain condition
87 *
88 * @param packet The packet
89 * @param onValidated Callback function which is called when packet is immediately valid
90 * @param onValidationFailed Call function which is called when packet is immediately invalid
91 * @return -1 if packet is immediately invalid (onValidationFailed has been called)
92 * 1 if packet is immediately valid (onValidated has been called)
93 * 0 if further signature verification is needed.
94 */
95 template<class ValidatedCallback, class ValidationFailureCallback>
96 int8_t
97 check(const Packet& packet,
98 const ValidatedCallback& onValidated,
99 const ValidationFailureCallback& onValidationFailed)
100 {
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700101 bool hasPendingResult = false;
102 for (CheckerList::iterator it = m_checkers.begin(); it != m_checkers.end(); it++) {
103 int8_t result = (*it)->check(packet);
104 if (result > 0) {
105 onValidated(packet.shared_from_this());
106 return result;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700107 }
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700108 else if (result == 0)
109 hasPendingResult = true;
110 }
111 if (hasPendingResult) {
112 return 0;
113 }
114 else {
115 onValidationFailed(packet.shared_from_this(), "Packet cannot pass any checkers.");
116 return -1;
117 }
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700118 }
119
Zhiyi Zhang044bb7e2016-06-10 00:02:37 -0700120NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
121 typedef std::vector<shared_ptr<Filter>> FilterList;
122 typedef std::vector<shared_ptr<Checker>> CheckerList;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700123
124 std::string m_id;
125 FilterList m_filters;
126 CheckerList m_checkers;
127};
128
129} // namespace conf
130} // namespace security
131} // namespace ndn
132
133#endif // NDN_SECURITY_CONF_RULE_HPP