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 */
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:233
pcpp::Packet
Definition: Packet.h:26
pcpp::Packet::getFirstLayer
Layer * getFirstLayer() const
Definition: Packet.h:134
pcpp::Packet::getLastLayer
Layer * getLastLayer() const
Definition: Packet.h:140
pcpp::Packet::getLayerOfType
Layer * getLayerOfType(ProtocolType layerType, int index=0) const
Layer.h
pcpp::Packet::insertLayer
bool insertLayer(Layer *prevLayer, Layer *newLayer, bool ownInPacket=false)
pcpp::Packet::Packet
Packet(const Packet &other)
Definition: Packet.h:96
pcpp::Packet::toStringList
void toStringList(std::vector< std::string > &result, bool timeAsLocalTime=true) const
pcpp::Packet::getNextLayerOfType
TLayer * getNextLayerOfType(Layer *startLayer) const
Definition: Packet.h:343
pcpp::Packet::removeAllLayersAfter
bool removeAllLayersAfter(Layer *layer)
pcpp::Packet::detachLayer
bool detachLayer(Layer *layer)
Definition: Packet.h:234
pcpp::Packet::~Packet
virtual ~Packet()
Definition: Packet.h:89
pcpp::Packet::getPrevLayerOfType
TLayer * getPrevLayerOfType(Layer *startLayer) const
Definition: Packet.h:358
pcpp::Packet::setRawPacket
void setRawPacket(RawPacket *rawPacket, bool freeRawPacket, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
RawPacket.h
pcpp::Packet::toString
std::string toString(bool timeAsLocalTime=true)
pcpp::ProtocolType
uint64_t ProtocolType
Definition: ProtocolType.h:18
pcpp::OsiModelLayerUnknown
@ OsiModelLayerUnknown
Definition: ProtocolType.h:250
pcpp::UnknownProtocol
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:23
pcpp::Packet::getRawPacketReadOnly
RawPacket * getRawPacketReadOnly() const
Definition: Packet.h:128
pcpp::Packet::removeFirstLayer
bool removeFirstLayer()
pcpp::Packet::Packet
Packet(size_t maxPacketLen=1)
pcpp::Packet::addLayer
bool addLayer(Layer *newLayer, bool ownInPacket=false)
Definition: Packet.h:151
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::Packet::removeLastLayer
bool removeLastLayer()
pcpp::Packet::computeCalculateFields
void computeCalculateFields()
pcpp::Packet::isPacketOfType
bool isPacketOfType(ProtocolType protocolType) const
Definition: Packet.h:281
pcpp::Layer
Definition: Layer.h:70
pcpp::LinkLayerType
LinkLayerType
Definition: RawPacket.h:25
pcpp::Packet::operator=
Packet & operator=(const Packet &other)
pcpp::Packet::getRawPacket
RawPacket * getRawPacket() const
Definition: Packet.h:111
pcpp::Packet::detachLayer
Layer * detachLayer(ProtocolType layerType, int index=0)
pcpp::Packet::removeLayer
bool removeLayer(ProtocolType layerType, int index=0)
pcpp::RawPacket
Definition: RawPacket.h:252
pcpp::Layer::getNextLayer
Layer * getNextLayer() const
Definition: Layer.h:81
pcpp::Layer::getPrevLayer
Layer * getPrevLayer() const
Definition: Layer.h:86