PcapPlusPlus  19.12
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();
90 
96  Packet(const Packet& 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);
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);
235 
244  Layer* getLayerOfType(ProtocolType layerType, int index = 0) const;
245 
250  template<class TLayer>
251  TLayer* getLayerOfType() const;
252 
261  template<class TLayer>
262  TLayer* getNextLayerOfType(Layer* after) const;
263 
269  bool isPacketOfType(ProtocolType protocolType) const { return m_ProtocolTypes & protocolType; }
270 
275  void computeCalculateFields();
276 
283  std::string toString(bool timeAsLocalTime = true);
284 
290  void toStringList(std::vector<std::string>& result, bool timeAsLocalTime = true) const;
291 
292  private:
293  void copyDataFrom(const Packet& other);
294 
295  void destructPacketData();
296 
297  bool extendLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToExtend);
298  bool shortenLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToShorten);
299 
300  void reallocateRawData(size_t newSize);
301 
302  bool removeLayer(Layer* layer, bool tryToDelete);
303 
304  std::string printPacketInfo(bool timeAsLocalTime) const;
305 
306  Layer* createFirstLayer(LinkLayerType linkType);
307  };
308 
309  template<class TLayer>
310  TLayer* Packet::getLayerOfType() const
311  {
312  if (dynamic_cast<TLayer*>(m_FirstLayer) != NULL)
313  return (TLayer*)m_FirstLayer;
314 
315  return getNextLayerOfType<TLayer>(m_FirstLayer);
316  }
317 
318  template<class TLayer>
319  TLayer* Packet::getNextLayerOfType(Layer* after) const
320  {
321  if (after == NULL)
322  return NULL;
323 
324  Layer* curLayer = after->getNextLayer();
325  while ((curLayer != NULL) && (dynamic_cast<TLayer*>(curLayer) == NULL))
326  {
327  curLayer = curLayer->getNextLayer();
328  }
329 
330  return (TLayer*)curLayer;
331  }
332 
333 } // namespace pcpp
334 
335 #endif /* PACKETPP_PACKET */
pcpp::OsiModelLayer
OsiModelLayer
Definition: ProtocolType.h:224
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
Layer.h
pcpp::Packet::insertLayer
bool insertLayer(Layer *prevLayer, Layer *newLayer, bool ownInPacket=false)
pcpp::Packet::toStringList
void toStringList(std::vector< std::string > &result, bool timeAsLocalTime=true) const
pcpp::Packet::removeAllLayersAfter
bool removeAllLayersAfter(Layer *layer)
pcpp::Packet::~Packet
virtual ~Packet()
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
ProtocolType
Definition: ProtocolType.h:16
pcpp::OsiModelLayerUnknown
Definition: ProtocolType.h:241
pcpp::Packet::getLayerOfType
TLayer * getLayerOfType() const
Definition: Packet.h:310
pcpp::Packet::getNextLayerOfType
TLayer * getNextLayerOfType(Layer *after) const
Definition: Packet.h:319
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)
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:269
pcpp::UnknownProtocol
Definition: ProtocolType.h:21
pcpp::Layer
Definition: Layer.h:70
pcpp::LinkLayerType
LinkLayerType
Definition: RawPacket.h:24
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:219
pcpp::Layer::getNextLayer
Layer * getNextLayer() const
Definition: Layer.h:81