PcapPlusPlus  Next
Packet.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RawPacket.h"
4 #include "Layer.h"
5 #include <vector>
6 
8 
13 namespace pcpp
14 {
15 
26  class Packet
27  {
28  friend class Layer;
29 
30  private:
31  RawPacket* m_RawPacket;
32  Layer* m_FirstLayer;
33  Layer* m_LastLayer;
34  size_t m_MaxPacketLen;
35  bool m_FreeRawPacket;
36  bool m_CanReallocateData;
37 
38  public:
45  explicit Packet(size_t maxPacketLen = 1);
46 
57  Packet(uint8_t* buffer, size_t bufferSize);
58 
77  explicit Packet(RawPacket* rawPacket, bool freeRawPacket = false, ProtocolType parseUntil = UnknownProtocol,
78  OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
79 
91  explicit Packet(RawPacket* rawPacket, ProtocolType parseUntil);
92 
104  explicit Packet(RawPacket* rawPacket, ProtocolTypeFamily parseUntilFamily);
105 
118  explicit Packet(RawPacket* rawPacket, OsiModelLayer parseUntilLayer);
119 
126  virtual ~Packet()
127  {
128  destructPacketData();
129  }
130 
136  Packet(const Packet& other)
137  {
138  copyDataFrom(other);
139  }
140 
149  Packet& operator=(const Packet& other);
150 
156  {
157  return m_RawPacket;
158  }
159 
174  void setRawPacket(RawPacket* rawPacket, bool freeRawPacket, ProtocolTypeFamily parseUntil = UnknownProtocol,
175  OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
176 
182  {
183  return m_RawPacket;
184  }
185 
191  {
192  return m_FirstLayer;
193  }
194 
200  {
201  return m_LastLayer;
202  }
203 
214  bool addLayer(Layer* newLayer, bool ownInPacket = false)
215  {
216  return insertLayer(m_LastLayer, newLayer, ownInPacket);
217  }
218 
231  bool insertLayer(Layer* prevLayer, Layer* newLayer, bool ownInPacket = false);
232 
247  bool removeLayer(ProtocolType layerType, int index = 0);
248 
256 
264 
276 
289  Layer* detachLayer(ProtocolType layerType, int index = 0);
290 
301  bool detachLayer(Layer* layer)
302  {
303  return removeLayer(layer, false);
304  }
305 
314  Layer* getLayerOfType(ProtocolType layerType, int index = 0) const;
315 
323  template <class TLayer> TLayer* getLayerOfType(bool reverseOrder = false) const;
324 
333  template <class TLayer> TLayer* getNextLayerOfType(Layer* startLayer) const;
334 
343  template <class TLayer> TLayer* getPrevLayerOfType(Layer* startLayer) const;
344 
350  bool isPacketOfType(ProtocolType protocolType) const;
351 
357  bool isPacketOfType(ProtocolTypeFamily protocolTypeFamily) const;
358 
364 
374  std::string toString(bool timeAsLocalTime = true) const;
375 
382  void toStringList(std::vector<std::string>& result, bool timeAsLocalTime = true) const;
383 
384  private:
385  void copyDataFrom(const Packet& other);
386 
387  void destructPacketData();
388 
389  bool extendLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToExtend);
390  bool shortenLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToShorten);
391 
392  void reallocateRawData(size_t newSize);
393 
394  bool removeLayer(Layer* layer, bool tryToDelete);
395 
396  std::string printPacketInfo(bool timeAsLocalTime) const;
397 
398  Layer* createFirstLayer(LinkLayerType linkType);
399  }; // class Packet
400 
401  // implementation of inline methods
402 
403  template <class TLayer> TLayer* Packet::getLayerOfType(bool reverse) const
404  {
405  if (!reverse)
406  {
407  if (dynamic_cast<TLayer*>(getFirstLayer()) != nullptr)
408  return dynamic_cast<TLayer*>(getFirstLayer());
409 
410  return getNextLayerOfType<TLayer>(getFirstLayer());
411  }
412 
413  // lookup in reverse order
414  if (dynamic_cast<TLayer*>(getLastLayer()) != nullptr)
415  return dynamic_cast<TLayer*>(getLastLayer());
416 
417  return getPrevLayerOfType<TLayer>(getLastLayer());
418  }
419 
420  template <class TLayer> TLayer* Packet::getNextLayerOfType(Layer* curLayer) const
421  {
422  if (curLayer == nullptr)
423  return nullptr;
424 
425  curLayer = curLayer->getNextLayer();
426  while ((curLayer != nullptr) && (dynamic_cast<TLayer*>(curLayer) == nullptr))
427  {
428  curLayer = curLayer->getNextLayer();
429  }
430 
431  return dynamic_cast<TLayer*>(curLayer);
432  }
433 
434  template <class TLayer> TLayer* Packet::getPrevLayerOfType(Layer* curLayer) const
435  {
436  if (curLayer == nullptr)
437  return nullptr;
438 
439  curLayer = curLayer->getPrevLayer();
440  while (curLayer != nullptr && dynamic_cast<TLayer*>(curLayer) == nullptr)
441  {
442  curLayer = curLayer->getPrevLayer();
443  }
444 
445  return dynamic_cast<TLayer*>(curLayer);
446  }
447 
448 } // namespace pcpp
449 
450 inline std::ostream& operator<<(std::ostream& os, const pcpp::Packet& packet)
451 {
452  os << packet.toString();
453  return os;
454 }
Definition: Layer.h:69
Layer * getNextLayer() const
Definition: Layer.h:82
Layer * getPrevLayer() const
Definition: Layer.h:90
Definition: Packet.h:27
bool isPacketOfType(ProtocolType protocolType) const
bool insertLayer(Layer *prevLayer, Layer *newLayer, bool ownInPacket=false)
Packet(const Packet &other)
Definition: Packet.h:136
Packet(size_t maxPacketLen=1)
const RawPacket * getRawPacketReadOnly() const
Definition: Packet.h:181
Packet & operator=(const Packet &other)
bool removeLayer(ProtocolType layerType, int index=0)
bool removeFirstLayer()
virtual ~Packet()
Definition: Packet.h:126
Layer * detachLayer(ProtocolType layerType, int index=0)
Packet(uint8_t *buffer, size_t bufferSize)
RawPacket * getRawPacket() const
Definition: Packet.h:155
bool isPacketOfType(ProtocolTypeFamily protocolTypeFamily) const
bool removeAllLayersAfter(Layer *layer)
void setRawPacket(RawPacket *rawPacket, bool freeRawPacket, ProtocolTypeFamily parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
Layer * getLayerOfType(ProtocolType layerType, int index=0) const
TLayer * getPrevLayerOfType(Layer *startLayer) const
Definition: Packet.h:434
Packet(RawPacket *rawPacket, OsiModelLayer parseUntilLayer)
Layer * getLastLayer() const
Definition: Packet.h:199
std::string toString(bool timeAsLocalTime=true) const
TLayer * getNextLayerOfType(Layer *startLayer) const
Definition: Packet.h:420
Packet(RawPacket *rawPacket, ProtocolType parseUntil)
Packet(RawPacket *rawPacket, bool freeRawPacket=false, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
void computeCalculateFields()
void toStringList(std::vector< std::string > &result, bool timeAsLocalTime=true) const
bool removeLastLayer()
bool addLayer(Layer *newLayer, bool ownInPacket=false)
Definition: Packet.h:214
bool detachLayer(Layer *layer)
Definition: Packet.h:301
Packet(RawPacket *rawPacket, ProtocolTypeFamily parseUntilFamily)
Layer * getFirstLayer() const
Definition: Packet.h:190
Definition: RawPacket.h:269
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:17
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelLayerUnknown
Definition: ProtocolType.h:380
uint32_t ProtocolTypeFamily
Definition: ProtocolType.h:23
LinkLayerType
Definition: RawPacket.h:25
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:28