libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
packet.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_PACKET_H
31 #define TINS_PACKET_H
32 
33 #include <algorithm>
34 #include "cxxstd.h"
35 #include "pdu.h"
36 #include "timestamp.h"
37 
41 namespace Tins {
42 
43 template<typename WrappedType, typename TimestampType>
45 
46 
51 
56 
68 template<typename PDUType, typename TimestampType>
69 class PacketWrapper {
70 public:
71  typedef PDUType pdu_type;
72  typedef TimestampType timestamp_type;
73 
81  operator pdu_type() {
82  return pdu_;
83  }
84 
88  pdu_type pdu() {
89  return pdu_;
90  }
91 
95  const pdu_type pdu() const {
96  return pdu_;
97  }
98 
105  const Timestamp& timestamp() const {
106  return ts_;
107  }
108 private:
109  friend class BaseSniffer;
110  friend class SnifferIterator;
111 
112  PacketWrapper(pdu_type pdu, const Timestamp& ts)
113  : pdu_(pdu), ts_(ts) {}
114 
115  PacketWrapper(const PacketWrapper&);
116  PacketWrapper& operator=(const PacketWrapper&);
117  void* operator new (size_t size);
118  void operator delete (void* p);
119 
120  pdu_type pdu_;
121  timestamp_type ts_;
122 };
123 
131 class Packet {
132 public:
136  struct own_pdu {
137 
138  };
139 
146  : pdu_(0) { }
147 
153  Packet(const PDU* apdu, const Timestamp& tstamp)
154  : pdu_(apdu->clone()), ts_(tstamp) { }
155 
161  Packet(const PDU& apdu, const Timestamp& tstamp)
162  : pdu_(apdu.clone()), ts_(tstamp) { }
163 
172  Packet(PDU* apdu, const Timestamp& tstamp, own_pdu)
173  : pdu_(apdu), ts_(tstamp) { }
174 
183  Packet(const PDU& rhs)
184  : pdu_(rhs.clone()), ts_(Timestamp::current_time()) { }
185 
192  Packet(const RefPacket& pck)
193  : pdu_(pck.pdu().clone()), ts_(pck.timestamp()) { }
194 
198  Packet(const PtrPacket& pck)
199  : pdu_(pck.pdu()), ts_(pck.timestamp()) { }
200 
206  Packet(const Packet& rhs) : ts_(rhs.timestamp()) {
207  pdu_ = rhs.pdu() ? rhs.pdu()->clone() : 0;
208  }
209 
215  Packet& operator=(const Packet& rhs) {
216  if (this != &rhs) {
217  delete pdu_;
218  ts_ = rhs.timestamp();
219  pdu_ = rhs.pdu() ? rhs.pdu()->clone() : 0;
220  }
221  return* this;
222  }
223 
224  #if TINS_IS_CXX11
225 
228  Packet(Packet &&rhs) TINS_NOEXCEPT : pdu_(rhs.pdu()), ts_(rhs.timestamp()) {
229  rhs.pdu_ = nullptr;
230  }
231 
235  Packet& operator=(Packet &&rhs) TINS_NOEXCEPT {
236  if (this != &rhs) {
237  std::swap(pdu_, rhs.pdu_);
238  ts_ = rhs.timestamp();
239  }
240  return* this;
241  }
242  #endif
243 
250  delete pdu_;
251  }
252 
256  const Timestamp& timestamp() const {
257  return ts_;
258  }
259 
265  PDU* pdu() {
266  return pdu_;
267  }
268 
274  const PDU* pdu() const {
275  return pdu_;
276  }
277 
288  PDU* some_pdu = pdu_;
289  pdu_ = 0;
290  return some_pdu;
291  }
292 
298  operator bool() const {
299  return pdu_ ? true : false;
300  }
301 
310  Packet& operator/=(const PDU& rhs) {
311  pdu_ /= rhs;
312  return* this;
313  }
314 private:
315  PDU* pdu_;
316  Timestamp ts_;
317 };
318 }
319 
320 #endif // TINS_PACKET_H
PDU * release_pdu()
Releases ownership of the stored PDU*.
Definition: packet.h:287
~Packet()
Packet destructor.
Definition: packet.h:249
const Timestamp & timestamp() const
Returns the packet timestamp.
Definition: packet.h:105
Definition: packet.h:131
Packet & operator=(Packet &&rhs) TINS_NOEXCEPT
Definition: packet.h:235
Packet & operator/=(const PDU &rhs)
Concatenation operator.
Definition: packet.h:310
PDU * pdu()
Returns the stored PDU*.
Definition: packet.h:265
Packet(Packet &&rhs) TINS_NOEXCEPT
Definition: packet.h:228
Base class for sniffers.
Definition: sniffer.h:61
const PDU * pdu() const
Returns the stored PDU*.
Definition: packet.h:274
Iterates over packets sniffed by a BaseSniffer.
Definition: sniffer.h:415
Packet & operator=(const Packet &rhs)
Copy assignment operator.
Definition: packet.h:215
const Timestamp & timestamp() const
Definition: packet.h:256
Packet(PDU *apdu, const Timestamp &tstamp, own_pdu)
Constructs a Packet from a PDU* and a Timestamp.
Definition: packet.h:172
const pdu_type pdu() const
Returns the PDU const reference.
Definition: packet.h:95
pdu_type pdu()
Returns the wrapped_type.
Definition: packet.h:88
Packet(const PDU &rhs)
Constructs a Packet from a const PDU&.
Definition: packet.h:183
Packet(const RefPacket &pck)
Constructs a Packet from a RefPacket.
Definition: packet.h:192
Packet()
Default constructs a Packet.
Definition: packet.h:145
Packet(const Packet &rhs)
Copy constructor.
Definition: packet.h:206
Represents a packet timestamp.
Definition: timestamp.h:47
PacketWrapper< PDU *, Timestamp > PtrPacket
Thin wrapper over a PDU pointer and a Timestamp.
Definition: packet.h:55
Packet(const PDU *apdu, const Timestamp &tstamp)
Constructs a Packet from a PDU* and a Timestamp.
Definition: packet.h:153
virtual PDU * clone() const =0
Clones this packet.
Definition: packet.h:136
Packet(const PDU &apdu, const Timestamp &tstamp)
Constructs a Packet from a PDU& and a Timestamp.
Definition: packet.h:161
Base class for protocol data units.
Definition: pdu.h:108
PacketWrapper< PDU &, const Timestamp & > RefPacket
Thin wrapper over a PDU and Timestamp reference.
Definition: packet.h:44
Packet(const PtrPacket &pck)
Constructs a Packet from a PtrPacket object.
Definition: packet.h:198
Represents a sniffed packet.
Definition: packet.h:44