libtins  4.0
packet.h
1 /*
2  * Copyright (c) 2017, 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_PACKET_H
31 #define TINS_PACKET_H
32 
33 #include <tins/cxxstd.h>
34 #include <tins/pdu.h>
35 #include <tins/timestamp.h>
36 
40 namespace Tins {
41 
42 template<typename WrappedType, typename TimestampType>
44 
45 
50 
55 
67 template<typename PDUType, typename TimestampType>
68 class PacketWrapper {
69 public:
70  typedef PDUType pdu_type;
71  typedef TimestampType timestamp_type;
72 
80  operator pdu_type() {
81  return pdu_;
82  }
83 
87  pdu_type pdu() {
88  return pdu_;
89  }
90 
94  const pdu_type pdu() const {
95  return pdu_;
96  }
97 
104  const Timestamp& timestamp() const {
105  return ts_;
106  }
107 private:
108  friend class BaseSniffer;
109  friend class SnifferIterator;
110 
111  PacketWrapper(pdu_type pdu, const Timestamp& ts)
112  : pdu_(pdu), ts_(ts) {}
113 
115  PacketWrapper& operator=(const PacketWrapper&);
116  void* operator new (size_t size);
117  void operator delete (void* p);
118 
119  pdu_type pdu_;
120  timestamp_type ts_;
121 };
122 
130 class Packet {
131 public:
135  struct own_pdu {
136 
137  };
138 
145  : pdu_(0) { }
146 
152  Packet(const PDU* apdu, const Timestamp& tstamp)
153  : pdu_(apdu->clone()), ts_(tstamp) { }
154 
160  Packet(const PDU& apdu, const Timestamp& tstamp)
161  : pdu_(apdu.clone()), ts_(tstamp) { }
162 
171  Packet(PDU* apdu, const Timestamp& tstamp, own_pdu)
172  : pdu_(apdu), ts_(tstamp) { }
173 
182  Packet(const PDU& rhs)
183  : pdu_(rhs.clone()), ts_(Timestamp::current_time()) { }
184 
191  Packet(const RefPacket& pck)
192  : pdu_(pck.pdu().clone()), ts_(pck.timestamp()) { }
193 
197  Packet(const PtrPacket& pck)
198  : pdu_(pck.pdu()), ts_(pck.timestamp()) { }
199 
205  Packet(const Packet& rhs) : ts_(rhs.timestamp()) {
206  pdu_ = rhs.pdu() ? rhs.pdu()->clone() : 0;
207  }
208 
214  Packet& operator=(const Packet& rhs) {
215  if (this != &rhs) {
216  delete pdu_;
217  ts_ = rhs.timestamp();
218  pdu_ = rhs.pdu() ? rhs.pdu()->clone() : 0;
219  }
220  return* this;
221  }
222 
223  #if TINS_IS_CXX11
224 
227  Packet(Packet &&rhs) TINS_NOEXCEPT : pdu_(rhs.pdu()), ts_(rhs.timestamp()) {
228  rhs.pdu_ = nullptr;
229  }
230 
234  Packet& operator=(Packet &&rhs) TINS_NOEXCEPT {
235  if (this != &rhs) {
236  PDU* tmp = std::move(pdu_);
237  pdu_ = std::move(rhs.pdu_);
238  rhs.pdu_ = std::move(tmp);
239  ts_ = rhs.timestamp();
240  }
241  return* this;
242  }
243  #endif
244 
251  delete pdu_;
252  }
253 
257  const Timestamp& timestamp() const {
258  return ts_;
259  }
260 
266  PDU* pdu() {
267  return pdu_;
268  }
269 
275  const PDU* pdu() const {
276  return pdu_;
277  }
278 
289  PDU* some_pdu = pdu_;
290  pdu_ = 0;
291  return some_pdu;
292  }
293 
299  operator bool() const {
300  return pdu_ ? true : false;
301  }
302 
311  Packet& operator/=(const PDU& rhs) {
312  pdu_ /= rhs;
313  return* this;
314  }
315 private:
316  PDU* pdu_;
317  Timestamp ts_;
318 };
319 }
320 
321 #endif // TINS_PACKET_H
PDU * release_pdu()
Releases ownership of the stored PDU*.
Definition: packet.h:288
~Packet()
Packet destructor.
Definition: packet.h:250
Definition: packet.h:130
Packet & operator=(Packet &&rhs) TINS_NOEXCEPT
Definition: packet.h:234
Packet & operator/=(const PDU &rhs)
Concatenation operator.
Definition: packet.h:311
PDU * pdu()
Returns the stored PDU*.
Definition: packet.h:266
Packet(Packet &&rhs) TINS_NOEXCEPT
Definition: packet.h:227
const PDU * pdu() const
Returns the stored PDU*.
Definition: packet.h:275
Packet & operator=(const Packet &rhs)
Copy assignment operator.
Definition: packet.h:214
const Timestamp & timestamp() const
Returns the packet timestamp.
Definition: packet.h:104
const Timestamp & timestamp() const
Definition: packet.h:257
Packet(PDU *apdu, const Timestamp &tstamp, own_pdu)
Constructs a Packet from a PDU* and a Timestamp.
Definition: packet.h:171
Packet(const PDU &rhs)
Constructs a Packet from a const PDU&.
Definition: packet.h:182
Packet(const RefPacket &pck)
Constructs a Packet from a RefPacket.
Definition: packet.h:191
Packet()
Default constructs a Packet.
Definition: packet.h:144
pdu_type pdu()
Returns the wrapped_type.
Definition: packet.h:87
The Tins namespace.
Definition: address_range.h:38
Packet(const Packet &rhs)
Copy constructor.
Definition: packet.h:205
Represents a packet timestamp.
Definition: timestamp.h:47
PacketWrapper< PDU *, Timestamp > PtrPacket
Thin wrapper over a PDU pointer and a Timestamp.
Definition: packet.h:54
Packet(const PDU *apdu, const Timestamp &tstamp)
Constructs a Packet from a PDU* and a Timestamp.
Definition: packet.h:152
virtual PDU * clone() const =0
Clones this packet.
const pdu_type pdu() const
Returns the PDU const reference.
Definition: packet.h:94
Definition: packet.h:135
Packet(const PDU &apdu, const Timestamp &tstamp)
Constructs a Packet from a PDU& and a Timestamp.
Definition: packet.h:160
Base class for protocol data units.
Definition: pdu.h:107
PacketWrapper< PDU &, const Timestamp & > RefPacket
Thin wrapper over a PDU and Timestamp reference.
Definition: packet.h:43
Packet(const PtrPacket &pck)
Constructs a Packet from a PtrPacket object.
Definition: packet.h:197
Represents a sniffed packet.
Definition: packet.h:43