mavtables  0.2.1
MAVLink router and firewall.
Reject.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 "Action.hpp"
25 #include "MAVAddress.hpp"
26 #include "Packet.hpp"
27 #include "Reject.hpp"
28 #include "Rule.hpp"
29 
30 
31 /** Construct a reject rule.
32  *
33  * A reject rule is used to reject 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 Reject::Reject(std::optional<If> condition)
43  : Rule(std::move(condition))
44 {
45 }
46 
47 
48 /** \copydoc Rule::print_(std::ostream &os) const
49  *
50  * Prints `"reject"` or `"reject <If Statement>"` if the rule's condition was
51  * set.
52  */
53 std::ostream &Reject::print_(std::ostream &os) const
54 {
55  os << "reject";
56 
57  if (condition_)
58  {
59  os << " " << condition_.value();
60  }
61 
62  return os;
63 }
64 
65 
66 /** \copydoc Rule::action(const Packet&,const MAVAddress&)const
67  *
68  * %If the condition has not been set or it matches the given packet/address
69  * combination then it will return the reject \ref Action object, otherwise it
70  * will return the continue \ref Action object.
71  */
73  const Packet &packet, const MAVAddress &address) const
74 {
75  if (!condition_ || condition_->check(packet, address))
76  {
77  return Action::make_reject();
78  }
79 
80  return Action::make_continue();
81 }
82 
83 
84 std::unique_ptr<Rule> Reject::clone() const
85 {
86  return std::make_unique<Reject>(condition_);
87 }
88 
89 
90 bool Reject::operator==(const Rule &other) const
91 {
92  return typeid(*this) == typeid(other) &&
93  condition_ == static_cast<const Reject &>(other).condition_;
94 }
95 
96 
97 bool Reject::operator!=(const Rule &other) const
98 {
99  return typeid(*this) != typeid(other) ||
100  condition_ != static_cast<const Reject &>(other).condition_;
101 }
static Action make_reject()
Definition: Action.cpp:113
STL namespace.
virtual std::ostream & print_(std::ostream &os) const
Definition: Reject.cpp:53
virtual Action action(const Packet &packet, const MAVAddress &address) const
Definition: Reject.cpp:72
static Action make_continue()
Definition: Action.cpp:126
virtual bool operator==(const Rule &other) const
Definition: Reject.cpp:90
virtual std::unique_ptr< Rule > clone() const
Definition: Reject.cpp:84
Definition: Rule.hpp:38
Reject(std::optional< If > condition={})
Definition: Reject.cpp:42
std::optional< If > condition_
Definition: Rule.hpp:91
virtual bool operator!=(const Rule &other) const
Definition: Reject.cpp:97