mavtables  0.2.1
MAVLink router and firewall.
Rule.hpp
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 #ifndef RULE_HPP_
19 #define RULE_HPP_
20 
21 
22 #include <memory>
23 #include <optional>
24 #include <ostream>
25 
26 #include "Action.hpp"
27 #include "If.hpp"
28 #include "MAVAddress.hpp"
29 #include "Packet.hpp"
30 
31 
32 /** Base class of all rules, used in filter \ref Chain's.
33  *
34  * \ref Rule's are used to determine an \ref Action to take with a packet based
35  * on its type, source address, and destination address. They are very much
36  * like the rules found in a typical software defined firewalls.
37  */
38 class Rule
39 {
40  public:
41  Rule(std::optional<If> condition = {});
42  virtual ~Rule(); // Clang does not like pure virtual destructors.
43  /** Decide what to do with a \ref Packet.
44  *
45  * Determine what action to take with the given \p packet sent to the
46  * given \p address. The possible actions are documented in the \ref
47  * Action class. The continue object is always returned if the
48  * condition was set and does not match the packet/address combination.
49  *
50  * \param packet The packet to determine whether to allow or not.
51  * \param address The address the \p packet will be sent out on if the
52  * action dictates it.
53  * \returns The action to take with the packet. %If this is the accept
54  * object, it may also contain a priority for the packet.
55  */
56  virtual Action action(
57  const Packet &packet, const MAVAddress &address) const = 0;
58  /** Return a copy of the Rule polymorphically.
59  *
60  * This allows Rule's to be copied without knowing the derived type.
61  *
62  * \returns A pointer to a new object with base type \ref Rule which
63  * is an exact copy of this one.
64  */
65  virtual std::unique_ptr<Rule> clone() const = 0;
66  /** Equality comparison.
67  *
68  * Compares the type of the \ref Rule and the condition (\ref If) if
69  * set.
70  *
71  * \param other The other rule to compare this to.
72  * \retval true if this rule is the same as \p other.
73  * \retval false if this rule is not the same as \p other.
74  */
75  virtual bool operator==(const Rule &other) const = 0;
76  /** Inequality comparison.
77  *
78  * Compares the type of the \ref Rule and the condition (\ref If) if
79  * set.
80  *
81  * \param other The other rule to compare this to.
82  * \retval true if this rule is not the same as \p other.
83  * \retval false if this rule is the same as \p other.
84  */
85  virtual bool operator!=(const Rule &other) const = 0;
86 
87  friend std::ostream &operator<<(std::ostream &os, const Rule &action);
88 
89  protected:
90  // Variables
91  std::optional<If> condition_;
92  // Methods
93  /** Print the rule to the given output stream.
94  *
95  * \param os The output stream to print to.
96  * \returns The output stream.
97  */
98  virtual std::ostream &print_(std::ostream &os) const = 0;
99 };
100 
101 
102 std::ostream &operator<<(std::ostream &os, const Rule &rule);
103 
104 
105 #endif // RULE_HPP_
virtual bool operator==(const Rule &other) const =0
virtual std::unique_ptr< Rule > clone() const =0
friend std::ostream & operator<<(std::ostream &os, const Rule &action)
Definition: Rule.cpp:65
std::ostream & operator<<(std::ostream &os, const Action &action)
Definition: Action.cpp:188
Definition: Rule.hpp:38
virtual std::ostream & print_(std::ostream &os) const =0
virtual ~Rule()
Definition: Rule.cpp:42
Rule(std::optional< If > condition={})
Definition: Rule.cpp:33
std::optional< If > condition_
Definition: Rule.hpp:91
Rule & rule
Definition: test_Call.cpp:231
virtual bool operator!=(const Rule &other) const =0
virtual Action action(const Packet &packet, const MAVAddress &address) const =0