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