mavtables  0.2.1
MAVLink router and firewall.
Action.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 <optional>
19 #include <ostream>
20 #include <utility>
21 
22 #include "Action.hpp"
23 
24 
25 /** Construct an Action.
26  *
27  * \note Private constructor, use \ref make_accept, \ref make_reject, \ref
28  * make_continue, or \ref make_default.
29  *
30  * \param action The action this result represents.
31  * \param priority The priority, only used with Action::ACCEPT.
32  * \sa Action::Option
33  */
35  Action::Option action, std::optional<int> priority)
36  : action_(action), priority_(std::move(priority))
37 {
38 }
39 
40 
41 /** Return the action that has been chosen.
42  *
43  * \returns The Action::Option enum associated with this action.
44  */
46 {
47  return action_;
48 }
49 
50 
51 /** Set the priority of the action.
52  *
53  * This only has an effect if the \ref action is Action::ACCEPT and the
54  * priority has never been set before.
55  *
56  * The default priority is 0. A higher priority will result in the
57  * packet being prioritized over other packets while a lower (negative)
58  * priority will de-prioritize the packet.
59  *
60  * \param priority The priority to apply to the accept action.
61  */
62 void Action::priority(int priority)
63 {
64  if (action_ == Action::ACCEPT)
65  {
66  if (!priority_)
67  {
68  priority_ = priority;
69  }
70  }
71 }
72 
73 
74 /** Return the priority if it exists.
75  *
76  * \returns The priority of the action. This will always be 0 if the \ref
77  * action is not Action::ACCEPT. It will also be 0 (the default priority)
78  * if the priority has never been set.
79  */
80 int Action::priority() const
81 {
82  if (priority_)
83  {
84  return priority_.value();
85  }
86 
87  return 0;
88 }
89 
90 
91 /** Make a new action result with the Action::ACCEPT action.
92  *
93  * An accept action indicates that the packet/address combination this action
94  * is the response to should be accepted without any further processing.
95  *
96  * \param priority The priority to accept the packet with. The default is to
97  * not apply a priority.
98  * \returns The new 'accept' action.
99  */
100 Action Action::make_accept(std::optional<int> priority)
101 {
102  return Action(Action::ACCEPT, priority);
103 }
104 
105 
106 /** Make a new action result with the Action::REJECT action.
107  *
108  * A reject action indicates that the packet/address combination this action is
109  * the response to should be rejected without any further processing.
110  *
111  * \returns The new 'reject' action.
112  */
114 {
115  return Action(Action::REJECT);
116 }
117 
118 
119 /** Make a new action result with the Action::CONTINUE action.
120  *
121  * A continue action indicates that filtering of the packet/address combination
122  * this action is the response should continue with the next \ref Rule.
123  *
124  * \returns The new 'continue' action.
125  */
127 {
128  return Action(Action::CONTINUE);
129 }
130 
131 
132 /** Make a new action result with the Action::DEFAULT action.
133  *
134  * A default action indicates that the default action (defined in \ref Filter)
135  * should be taken for the packet/address combination this action is the
136  * response to.
137  *
138  * \returns The new 'default' action.
139  */
141 {
142  return Action(Action::DEFAULT);
143 }
144 
145 
146 /** Equality comparison.
147  *
148  * \relates Action
149  * \param lhs The left hand side action.
150  * \param rhs The right hand side action.
151  * \retval true if \p lhs is the same as rhs.
152  * \retval false if \p lhs is not the same as rhs.
153  */
154 bool operator==(const Action &lhs, const Action &rhs)
155 {
156  return (lhs.action() == rhs.action()) && (lhs.priority() == rhs.priority());
157 }
158 
159 
160 /** Inequality comparison.
161  *
162  * \relates Action
163  * \param lhs The left hand side action.
164  * \param rhs The right hand side action.
165  * \retval true if \p lhs is not the same as rhs.
166  * \retval false if \p lhs is the same as rhs.
167  */
168 bool operator!=(const Action &lhs, const Action &rhs)
169 {
170  return (lhs.action() != rhs.action()) || (lhs.priority() != rhs.priority());
171 }
172 
173 
174 /** Print the action to the given output stream.
175  *
176  * Some examples are:
177  * - `accept`
178  * - `accept with priority 3`
179  * - `reject`
180  * - `continue`
181  * - `default`
182  *
183  * \relates Action
184  * \param os The output stream to print to.
185  * \param action The action result to print.
186  * \returns The output stream.
187  */
188 std::ostream &operator<<(std::ostream &os, const Action &action)
189 {
190  switch (action.action())
191  {
192  case Action::ACCEPT:
193  os << "accept";
194 
195  if (action.priority() != 0)
196  {
197  os << " with priority " << action.priority();
198  }
199 
200  break;
201 
202  case Action::REJECT:
203  os << "reject";
204  break;
205 
206  case Action::CONTINUE:
207  os << "continue";
208  break;
209 
210  case Action::DEFAULT:
211  os << "default";
212  break;
213  }
214 
215  return os;
216 }
static Action make_reject()
Definition: Action.cpp:113
The packet has been accepted, possibly with priority.
Definition: Action.hpp:37
int priority() const
Definition: Action.cpp:80
Continue evaluating rules.
Definition: Action.hpp:39
STL namespace.
static Action make_accept(std::optional< int > priority={})
Definition: Action.cpp:100
static Action make_continue()
Definition: Action.cpp:126
std::ostream & operator<<(std::ostream &os, const Action &action)
Definition: Action.cpp:188
bool operator==(const Action &lhs, const Action &rhs)
Definition: Action.cpp:154
static Action make_default()
Definition: Action.cpp:140
Action::Option action() const
Definition: Action.cpp:45
bool operator!=(const Action &lhs, const Action &rhs)
Definition: Action.cpp:168
Action(const Action &other)=default
Use the default rule.
Definition: Action.hpp:40
The packet has been rejected.
Definition: Action.hpp:38
void priority(int priority)
Definition: Action.cpp:62
Option
Definition: Action.hpp:35