blob: 66bbfe574b384e0b96726b176c471ec08113e5db [file] [log] [blame]
Yingdi Yu48e8c0c2014-03-19 12:01:55 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Yu48e8c0c2014-03-19 12:01:55 -070013 */
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
22namespace ndn {
23namespace security {
24namespace conf {
25
26template<class Packet>
27class Rule
28{
29public:
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
100private:
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