PcapPlusPlus  20.08
Packet.h
Go to the documentation of this file.
1 #ifndef PACKETPP_PACKET
2 #define PACKETPP_PACKET
3 
4 #include "RawPacket.h"
5 #include "Layer.h"
6 #include <vector>
7 
9 
14 namespace pcpp
15 {
16 
26  class Packet
27  {
28  friend class Layer;
29  private:
30  RawPacket* m_RawPacket;
31  Layer* m_FirstLayer;
32  Layer* m_LastLayer;
33  uint64_t m_ProtocolTypes;
34  size_t m_MaxPacketLen;
35  bool m_FreeRawPacket;
36 
37  public:
38 
44  Packet(size_t maxPacketLen = 1);
45 
60  Packet(RawPacket* rawPacket, bool freeRawPacket = false, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
61 
71  Packet(RawPacket* rawPacket, ProtocolType parseUntil);
72 
82  Packet(RawPacket* rawPacket, OsiModelLayer parseUntilLayer);
83 
89  virtual ~Packet() { destructPacketData(); }
90 
96  Packet(const Packet& other) { copyDataFrom(other); }
97 
105  Packet& operator=(const Packet& other);
106 
111  RawPacket* getRawPacket() const { return m_RawPacket; }
112 
122  void setRawPacket(RawPacket* rawPacket, bool freeRawPacket, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
123 
128  RawPacket* getRawPacketReadOnly() const { return m_RawPacket; }
129 
134  Layer* getFirstLayer() const { return m_FirstLayer; }
135 
140  Layer* getLastLayer() const { return m_LastLayer; }
141 
151  bool addLayer(Layer* newLayer, bool ownInPacket = false) { return insertLayer(m_LastLayer, newLayer, ownInPacket); }
152 
164  bool insertLayer(Layer* prevLayer, Layer* newLayer, bool ownInPacket = false);
165 
166 
181  bool removeLayer(ProtocolType layerType, int index = 0);
182 
189  bool removeFirstLayer();
190 
197  bool removeLastLayer();
198 
208  bool removeAllLayersAfter(Layer* layer);
209 
222  Layer* detachLayer(ProtocolType layerType, int index = 0);
223 
234  bool detachLayer(Layer* layer) { return removeLayer(layer, false); }
235 
244  Layer* getLayerOfType(ProtocolType layerType, int index = 0) const;
245 
251  template<class TLayer>
252  TLayer* getLayerOfType(bool reverseOrder = false) const;
253 
262  template<class TLayer>
263  TLayer* getNextLayerOfType(Layer* startLayer) const;
264 
273  template<class TLayer>
274  TLayer* getPrevLayerOfType(Layer* startLayer) const;
275 
281  bool isPacketOfType(ProtocolType protocolType) const { return m_ProtocolTypes & protocolType; }
282 
287  void computeCalculateFields();
288 
295  std::string toString(bool timeAsLocalTime = true);
296 
302  void toStringList(std::vector<std::string>& result, bool timeAsLocalTime = true) const;
303 
304  private:
305  void copyDataFrom(const Packet& other);
306 
307  void destructPacketData();
308 
309  bool extendLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToExtend);
310  bool shortenLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToShorten);
311 
312  void reallocateRawData(size_t newSize);
313 
314  bool removeLayer(Layer* layer, bool tryToDelete);
315 
316  std::string printPacketInfo(bool timeAsLocalTime) const;
317 
318  Layer* createFirstLayer(LinkLayerType linkType);
319  }; // class Packet
320 
321 
322  // implementation of inline methods
323 
324  template<class TLayer>
325  TLayer* Packet::getLayerOfType(bool reverse) const
326  {
327  if (!reverse)
328  {
329  if (dynamic_cast<TLayer*>(getFirstLayer()) != NULL)
330  return (TLayer*)getFirstLayer();
331 
332  return getNextLayerOfType<TLayer>(getFirstLayer());
333  }
334 
335  // lookup in reverse order
336  if (dynamic_cast<TLayer*>(getLastLayer()) != NULL)
337  return (TLayer*)getLastLayer();
338 
339  return getPrevLayerOfType<TLayer>(getLastLayer());
340  }
341 
342  template<class TLayer>
343  TLayer* Packet::getNextLayerOfType(Layer* curLayer) const
344  {
345  if (curLayer == NULL)
346  return NULL;
347 
348  curLayer = curLayer->getNextLayer();
349  while ((curLayer != NULL) && (dynamic_cast<TLayer*>(curLayer) == NULL))
350  {
351  curLayer = curLayer->getNextLayer();
352  }
353 
354  return (TLayer*)curLayer;
355  }
356 
357  template<class TLayer>
358  TLayer* Packet::getPrevLayerOfType(Layer* curLayer) const
359  {
360  if (curLayer == NULL)
361  return NULL;
362 
363  curLayer = curLayer->getPrevLayer();
364  while (curLayer != NULL && dynamic_cast<TLayer*>(curLayer) == NULL)
365  {
366  curLayer = curLayer->getPrevLayer();
367  }
368 
369  return static_cast<TLayer*>(curLayer);
370  }
371 
372 } // namespace pcpp
373 
374 #endif /* PACKETPP_PACKET */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:233
void toStringList(std::vector< std::string > &result, bool timeAsLocalTime=true) const
TLayer * getNextLayerOfType(Layer *startLayer) const
Definition: Packet.h:343
Definition: Layer.h:70
Layer * getNextLayer() const
Definition: Layer.h:81
Definition: Packet.h:26
Definition: RawPacket.h:252
Packet(const Packet &other)
Definition: Packet.h:96
Layer * getFirstLayer() const
Definition: Packet.h:134
bool insertLayer(Layer *prevLayer, Layer *newLayer, bool ownInPacket=false)
RawPacket * getRawPacket() const
Definition: Packet.h:111
bool removeAllLayersAfter(Layer *layer)
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:23
bool removeLastLayer()
Definition: ProtocolType.h:250
virtual ~Packet()
Definition: Packet.h:89
uint64_t ProtocolType
Definition: ProtocolType.h:18
Layer * getLastLayer() const
Definition: Packet.h:140
bool addLayer(Layer *newLayer, bool ownInPacket=false)
Definition: Packet.h:151
Packet(size_t maxPacketLen=1)
Layer * getLayerOfType(ProtocolType layerType, int index=0) const
void computeCalculateFields()
std::string toString(bool timeAsLocalTime=true)
void setRawPacket(RawPacket *rawPacket, bool freeRawPacket, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
LinkLayerType
Definition: RawPacket.h:25
bool isPacketOfType(ProtocolType protocolType) const
Definition: Packet.h:281
Layer * detachLayer(ProtocolType layerType, int index=0)
RawPacket * getRawPacketReadOnly() const
Definition: Packet.h:128
Packet & operator=(const Packet &other)
bool detachLayer(Layer *layer)
Definition: Packet.h:234
Layer * getPrevLayer() const
Definition: Layer.h:86
TLayer * getPrevLayerOfType(Layer *startLayer) const
Definition: Packet.h:358
bool removeFirstLayer()
bool removeLayer(ProtocolType layerType, int index=0)