libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
pdu_cacher.h
1 /*
2  * Copyright (c) 2016, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_PDU_CACHER_H
31 #define TINS_PDU_CACHER_H
32 
33 #include <algorithm>
34 #include "pdu.h"
35 #include "macros.h"
36 
37 namespace Tins {
55 template <typename T>
56 class PDUCacher : public PDU {
57 public:
61  typedef T cached_type;
62 
66  static const PDU::PDUType pdu_flag = cached_type::pdu_flag;
67 
72  : cached_size_() {}
73 
78  PDUCacher(const cached_type& pdu)
79  : cached_(pdu), cached_size_() {}
80 
86  uint32_t header_size() const {
87  if (cached_serialization_.empty()) {
88  cached_size_ = cached_.size();
89  }
90  return cached_size_;
91  }
92 
98  PDUCacher* clone() const {
99  return new PDUCacher<T>(*this);
100  }
101 
107  void send(PacketSender& sender, const NetworkInterface& iface) {
108  cached_.send(sender, iface);
109  }
110 
116  PDU* recv_response(PacketSender& sender, const NetworkInterface& iface) {
117  return cached_.recv_response(sender, iface);
118  }
119 
125  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const {
126  return cached_.matches_response(ptr, total_sz);
127  }
128 
134  bool matches_flag(PDUType flag) const {
135  return cached_.matches_flag(flag);
136  }
137 
143  PDUType pdu_type() const {
144  return cached_.pdu_type();
145  }
146 private:
147  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
148  if (cached_serialization_.size() != total_sz) {
149  cached_serialization_ = cached_.serialize();
150  }
151  std::copy(cached_serialization_.begin(), cached_serialization_.end(), buffer);
152  }
153 
154  cached_type cached_;
155  PDU::serialization_type cached_serialization_;
156  mutable uint32_t cached_size_;
157 };
158 }
159 
160 #endif // TINS_PDU_CACHER_H
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
T cached_type
Definition: pdu_cacher.h:61
PDUCacher(const cached_type &pdu)
Definition: pdu_cacher.h:78
Sends packets through a network interface.
Definition: packet_sender.h:117
PDUCacher()
Definition: pdu_cacher.h:71
bool matches_response(const uint8_t *ptr, uint32_t total_sz) const
Definition: pdu_cacher.h:125
uint32_t header_size() const
Definition: pdu_cacher.h:86
Abstraction of a network interface.
Definition: network_interface.h:47
PDUType pdu_type() const
Definition: pdu_cacher.h:143
bool matches_flag(PDUType flag) const
Definition: pdu_cacher.h:134
byte_array serialization_type
Definition: pdu.h:113
PDU wrapper that caches the result of the wrapped PDU's serialization.
Definition: pdu_cacher.h:56
Base class for protocol data units.
Definition: pdu.h:108
PDU * recv_response(PacketSender &sender, const NetworkInterface &iface)
Definition: pdu_cacher.h:116
void send(PacketSender &sender, const NetworkInterface &iface)
Definition: pdu_cacher.h:107
PDUCacher * clone() const
Definition: pdu_cacher.h:98