mavtables  0.2.1
MAVLink router and firewall.
Filter.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 <utility>
20 
21 #include "Action.hpp"
22 #include "Chain.hpp"
23 #include "Filter.hpp"
24 #include "MAVAddress.hpp"
25 #include "Packet.hpp"
26 
27 
28 /** Construct a new packet filter.
29  *
30  * \param default_chain The \ref Chain that all filtering begins with.
31  * \param accept_by_default Whether to accept (true) or reject (false) packets
32  * that don't match any rules in the default chain or any chains called by
33  * the default chain. The default value is false and thus to reject
34  * unmatched packets.
35  */
36 Filter::Filter(Chain default_chain, bool accept_by_default)
37  : default_chain_(std::move(default_chain)),
38  accept_by_default_(accept_by_default)
39 {
40 }
41 
42 
43 /** Determine whether to accept or reject a packet/address combination.
44  *
45  * \param packet The packet to determine whether to allow or not.
46  * \param address The address the \p packet will be sent out on if the
47  * action allows it.
48  * \returns A pair (tuple) with the first value being whether to accept the
49  * packet or not and the second being the priority to use when sending the
50  * packet. The second value is only defined if the first value is true
51  * (accept).
52  */
53 std::pair<bool, int> Filter::will_accept(
54  const Packet &packet, const MAVAddress &address)
55 {
56  Action result = default_chain_.action(packet, address);
57 
58  switch (result.action())
59  {
60  case Action::ACCEPT:
61  return {true, result.priority()};
62  case Action::REJECT:
63  return {false, 0};
64  case Action::CONTINUE:
65  break;
66 
67  case Action::DEFAULT:
68  break;
69  }
70 
71  return {accept_by_default_, 0};
72 }
73 
74 
75 /** Equality comparison.
76  *
77  * The default chain and default action are compared.
78  *
79  * \relates Filter
80  * \param lhs The left hand side packet filter.
81  * \param rhs The right hand side packet filter.
82  * \retval true if \p lhs is the same as rhs.
83  * \retval false if \p lhs is not the same as rhs.
84  */
85 bool operator==(const Filter &lhs, const Filter &rhs)
86 {
87  return (lhs.default_chain_ == rhs.default_chain_) &&
88  (lhs.accept_by_default_ == rhs.accept_by_default_);
89 }
90 
91 
92 /** Inequality comparison.
93  *
94  * The default chain and default action are compared.
95  *
96  * \relates Filter
97  * \param lhs The left hand side packet filter.
98  * \param rhs The right hand side packet filter.
99  * \retval true if \p lhs is not the same as rhs.
100  * \retval false if \p lhs is the same as rhs.
101  */
102 bool operator!=(const Filter &lhs, const Filter &rhs)
103 {
104  return (lhs.default_chain_ != rhs.default_chain_) ||
105  (lhs.accept_by_default_ != rhs.accept_by_default_);
106 }
The packet has been accepted, possibly with priority.
Definition: Action.hpp:37
Continue evaluating rules.
Definition: Action.hpp:39
STL namespace.
Filter(const Filter &other)=default
TEST_VIRTUAL std::pair< bool, int > will_accept(const Packet &packet, const MAVAddress &address)
Definition: Filter.cpp:53
TEST_VIRTUAL Action action(const Packet &packet, const MAVAddress &address)
Definition: Chain.cpp:90
bool operator==(const Action &lhs, const Action &rhs)
Definition: Action.cpp:154
Action::Option action() const
Definition: Action.cpp:45
bool operator!=(const Action &lhs, const Action &rhs)
Definition: Action.cpp:168
Use the default rule.
Definition: Action.hpp:40
Definition: Chain.hpp:37
The packet has been rejected.
Definition: Action.hpp:38
void priority(int priority)
Definition: Action.cpp:62