PcapPlusPlus  23.09
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  bool m_CanReallocateData;
37 
38  public:
39 
45  explicit Packet(size_t maxPacketLen = 1);
46 
56  Packet(uint8_t* buffer, size_t bufferSize);
57 
72  explicit Packet(RawPacket* rawPacket, bool freeRawPacket = false, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
73 
83  Packet(RawPacket* rawPacket, ProtocolType parseUntil);
84 
94  Packet(RawPacket* rawPacket, OsiModelLayer parseUntilLayer);
95 
101  virtual ~Packet() { destructPacketData(); }
102 
108  Packet(const Packet& other) { copyDataFrom(other); }
109 
117  Packet& operator=(const Packet& other);
118 
123  RawPacket* getRawPacket() const { return m_RawPacket; }
124 
134  void setRawPacket(RawPacket* rawPacket, bool freeRawPacket, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
135 
140  RawPacket* getRawPacketReadOnly() const { return m_RawPacket; }
141 
146  Layer* getFirstLayer() const { return m_FirstLayer; }
147 
152  Layer* getLastLayer() const { return m_LastLayer; }
153 
163  bool addLayer(Layer* newLayer, bool ownInPacket = false) { return insertLayer(m_LastLayer, newLayer, ownInPacket); }
164 
176  bool insertLayer(Layer* prevLayer, Layer* newLayer, bool ownInPacket = false);
177 
178 
193  bool removeLayer(ProtocolType layerType, int index = 0);
194 
201  bool removeFirstLayer();
202 
209  bool removeLastLayer();
210 
220  bool removeAllLayersAfter(Layer* layer);
221 
234  Layer* detachLayer(ProtocolType layerType, int index = 0);
235 
246  bool detachLayer(Layer* layer) { return removeLayer(layer, false); }
247 
256  Layer* getLayerOfType(ProtocolType layerType, int index = 0) const;
257 
263  template<class TLayer>
264  TLayer* getLayerOfType(bool reverseOrder = false) const;
265 
274  template<class TLayer>
275  TLayer* getNextLayerOfType(Layer* startLayer) const;
276 
285  template<class TLayer>
286  TLayer* getPrevLayerOfType(Layer* startLayer) const;
287 
293  bool isPacketOfType(ProtocolType protocolType) const { return m_ProtocolTypes & protocolType; }
294 
299  void computeCalculateFields();
300 
307  std::string toString(bool timeAsLocalTime = true) const;
308 
314  void toStringList(std::vector<std::string>& result, bool timeAsLocalTime = true) const;
315 
316  private:
317  void copyDataFrom(const Packet& other);
318 
319  void destructPacketData();
320 
321  bool extendLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToExtend);
322  bool shortenLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToShorten);
323 
324  void reallocateRawData(size_t newSize);
325 
326  bool removeLayer(Layer* layer, bool tryToDelete);
327 
328  std::string printPacketInfo(bool timeAsLocalTime) const;
329 
330  Layer* createFirstLayer(LinkLayerType linkType);
331  }; // class Packet
332 
333 
334  // implementation of inline methods
335 
336  template<class TLayer>
337  TLayer* Packet::getLayerOfType(bool reverse) const
338  {
339  if (!reverse)
340  {
341  if (dynamic_cast<TLayer*>(getFirstLayer()) != NULL)
342  return dynamic_cast<TLayer*>(getFirstLayer());
343 
344  return getNextLayerOfType<TLayer>(getFirstLayer());
345  }
346 
347  // lookup in reverse order
348  if (dynamic_cast<TLayer*>(getLastLayer()) != NULL)
349  return dynamic_cast<TLayer*>(getLastLayer());
350 
351  return getPrevLayerOfType<TLayer>(getLastLayer());
352  }
353 
354  template<class TLayer>
355  TLayer* Packet::getNextLayerOfType(Layer* curLayer) const
356  {
357  if (curLayer == NULL)
358  return NULL;
359 
360  curLayer = curLayer->getNextLayer();
361  while ((curLayer != NULL) && (dynamic_cast<TLayer*>(curLayer) == NULL))
362  {
363  curLayer = curLayer->getNextLayer();
364  }
365 
366  return dynamic_cast<TLayer*>(curLayer);
367  }
368 
369  template<class TLayer>
370  TLayer* Packet::getPrevLayerOfType(Layer* curLayer) const
371  {
372  if (curLayer == NULL)
373  return NULL;
374 
375  curLayer = curLayer->getPrevLayer();
376  while (curLayer != NULL && dynamic_cast<TLayer*>(curLayer) == NULL)
377  {
378  curLayer = curLayer->getPrevLayer();
379  }
380 
381  return dynamic_cast<TLayer*>(curLayer);
382  }
383 
384 } // namespace pcpp
385 
386 inline std::ostream& operator<<(std::ostream& os, const pcpp::Packet& packet)
387 {
388  os << packet.toString();
389  return os;
390 }
391 
392 #endif /* PACKETPP_PACKET */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
void toStringList(std::vector< std::string > &result, bool timeAsLocalTime=true) const
TLayer * getNextLayerOfType(Layer *startLayer) const
Definition: Packet.h:355
Definition: Layer.h:70
Layer * getNextLayer() const
Definition: Layer.h:82
Definition: Packet.h:26
Definition: RawPacket.h:254
Packet(const Packet &other)
Definition: Packet.h:108
Layer * getFirstLayer() const
Definition: Packet.h:146
std::string toString(bool timeAsLocalTime=true) const
bool insertLayer(Layer *prevLayer, Layer *newLayer, bool ownInPacket=false)
RawPacket * getRawPacket() const
Definition: Packet.h:123
bool removeAllLayersAfter(Layer *layer)
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:23
bool removeLastLayer()
Definition: ProtocolType.h:350
virtual ~Packet()
Definition: Packet.h:101
uint64_t ProtocolType
Definition: ProtocolType.h:18
Layer * getLastLayer() const
Definition: Packet.h:152
bool addLayer(Layer *newLayer, bool ownInPacket=false)
Definition: Packet.h:163
Packet(size_t maxPacketLen=1)
Layer * getLayerOfType(ProtocolType layerType, int index=0) const
void computeCalculateFields()
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:293
Layer * detachLayer(ProtocolType layerType, int index=0)
RawPacket * getRawPacketReadOnly() const
Definition: Packet.h:140
Packet & operator=(const Packet &other)
bool detachLayer(Layer *layer)
Definition: Packet.h:246
Layer * getPrevLayer() const
Definition: Layer.h:87
TLayer * getPrevLayerOfType(Layer *startLayer) const
Definition: Packet.h:370
bool removeFirstLayer()
bool removeLayer(ProtocolType layerType, int index=0)