mavtables  0.2.1
MAVLink router and firewall.
Accept.cpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #include <memory>
19 #include <optional>
20 #include <ostream>
21 #include <typeinfo>
22 #include <utility>
23 
24 #include "Accept.hpp"
25 #include "Action.hpp"
26 #include "MAVAddress.hpp"
27 #include "Packet.hpp"
28 #include "Rule.hpp"
29 
30 
31 /** Construct an accept rule, without a priority.
32  *
33  * An accept rule is used to accept packet/address combinations that match the
34  * condition of the rule.
35  *
36  * \param condition The condition used to determine the rule matches a
37  * particular packet/address combination given to the \ref action method.
38  * The default is {} which indicates the rule matches any packet/address
39  * combination.
40  * \sa action
41  */
42 Accept::Accept(std::optional<If> condition)
43  : Rule(std::move(condition))
44 {
45 }
46 
47 
48 /** Construct an accept rule, with a priority.
49  *
50  * An accept rule is used to accept packet/address combinations that match the
51  * condition of the rule.
52  *
53  * \param condition The condition used to determine the rule matches a
54  * particular packet/address combination given to the \ref action method.
55  * The default is {} which indicates the rule matches any packet/address
56  * combination.
57  * \param priority The priority to accept packets with. A higher number is
58  * more important and will be routed first.
59  * \sa action
60  */
61 Accept::Accept(int priority, std::optional<If> condition)
62  : Rule(std::move(condition)), priority_(priority)
63 {
64 }
65 
66 
67 /** \copydoc Rule::print_(std::ostream &os)const
68  *
69  * Prints `"reject <Priority> <If Statement>"` where the priority and if
70  * statement are only printed if the priority or condition is set,
71  * respectively.
72  *
73  * \param os The output stream to print to.
74  */
75 std::ostream &Accept::print_(std::ostream &os) const
76 {
77  os << "accept";
78 
79  if (priority_)
80  {
81  os << " with priority " << priority_.value();
82  }
83 
84  if (condition_)
85  {
86  os << " " << condition_.value();
87  }
88 
89  return os;
90 }
91 
92 
93 /** \copydoc Rule::action(const Packet&,const MAVAddress&)const
94  *
95  * %If the condition has not been set or it matches the given packet/address
96  * combination then it will return the accept \ref Action object (with optional
97  * priority), otherwise it will return the continue \ref Action object.
98  */
100  const Packet &packet, const MAVAddress &address) const
101 {
102  if (!condition_ || condition_->check(packet, address))
103  {
104  return Action::make_accept(priority_);
105  }
106 
107  return Action::make_continue();
108 }
109 
110 
111 std::unique_ptr<Rule> Accept::clone() const
112 {
113  if (priority_)
114  {
115  return std::make_unique<Accept>(priority_.value(), condition_);
116  }
117 
118  return std::make_unique<Accept>(condition_);
119 }
120 
121 
122 /** \copydoc Rule::operator==(const Rule&)const
123  *
124  * Compares the priority (if set) associated with the rule as well.
125  */
126 bool Accept::operator==(const Rule &other) const
127 {
128  return typeid(*this) == typeid(other) &&
129  priority_ == static_cast<const Accept &>(other).priority_ &&
130  condition_ == static_cast<const Accept &>(other).condition_;
131 }
132 
133 
134 /** \copydoc Rule::operator!=(const Rule&)const
135  *
136  * Compares the priority (if set) associated with the rule as well.
137  */
138 bool Accept::operator!=(const Rule &other) const
139 {
140  return typeid(*this) != typeid(other) ||
141  priority_ != static_cast<const Accept &>(other).priority_ ||
142  condition_ != static_cast<const Accept &>(other).condition_;
143 }
Accept(std::optional< If > condition={})
Definition: Accept.cpp:42
STL namespace.
static Action make_accept(std::optional< int > priority={})
Definition: Action.cpp:100
virtual Action action(const Packet &packet, const MAVAddress &address) const
Definition: Accept.cpp:99
virtual bool operator!=(const Rule &other) const
Definition: Accept.cpp:138
static Action make_continue()
Definition: Action.cpp:126
virtual std::ostream & print_(std::ostream &os) const
Definition: Accept.cpp:75
Definition: Rule.hpp:38
virtual std::unique_ptr< Rule > clone() const
Definition: Accept.cpp:111
std::optional< If > condition_
Definition: Rule.hpp:91
virtual bool operator==(const Rule &other) const
Definition: Accept.cpp:126