PcapPlusPlus
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  friend class Layer;
28  private:
29  RawPacket* m_RawPacket;
30  Layer* m_FirstLayer;
31  Layer* m_LastLayer;
32  uint64_t m_ProtocolTypes;
33  size_t m_MaxPacketLen;
34  bool m_FreeRawPacket;
35 
36  public:
37 
43  Packet(size_t maxPacketLen = 1);
44 
59  Packet(RawPacket* rawPacket, bool freeRawPacket = false, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
60 
70  Packet(RawPacket* rawPacket, ProtocolType parseUntil);
71 
81  Packet(RawPacket* rawPacket, OsiModelLayer parseUntilLayer);
82 
88  virtual ~Packet();
89 
95  Packet(const Packet& other);
96 
104  Packet& operator=(const Packet& other);
105 
110  inline RawPacket* getRawPacket() { return m_RawPacket; }
111 
121  void setRawPacket(RawPacket* rawPacket, bool freeRawPacket, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
122 
127  inline RawPacket* getRawPacketReadOnly() const { return m_RawPacket; }
128 
133  inline Layer* getFirstLayer() { return m_FirstLayer; }
134 
139  inline Layer* getLastLayer() { return m_LastLayer; }
140 
150  bool addLayer(Layer* newLayer, bool ownInPacket = false);
151 
163  bool insertLayer(Layer* prevLayer, Layer* newLayer, bool ownInPacket = false);
164 
165 
180  bool removeLayer(ProtocolType layerType, int index = 0);
181 
188  bool removeFirstLayer();
189 
196  bool removeLastLayer();
197 
207  bool removeAllLayersAfter(Layer* layer);
208 
221  Layer* detachLayer(ProtocolType layerType, int index = 0);
222 
233  bool detachLayer(Layer* layer);
234 
243  Layer* getLayerOfType(ProtocolType layerType, int index = 0);
244 
249  template<class TLayer>
250  TLayer* getLayerOfType();
251 
260  template<class TLayer>
261  TLayer* getNextLayerOfType(Layer* after);
262 
268  inline bool isPacketOfType(ProtocolType protocolType) { return m_ProtocolTypes & protocolType; }
269 
274  void computeCalculateFields();
275 
282  std::string toString(bool timeAsLocalTime = true);
283 
289  void toStringList(std::vector<std::string>& result, bool timeAsLocalTime = true);
290 
291  private:
292  void copyDataFrom(const Packet& other);
293 
294  void destructPacketData();
295 
296  bool extendLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToExtend);
297  bool shortenLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToShorten);
298 
299  void reallocateRawData(size_t newSize);
300 
301  bool removeLayer(Layer* layer, bool tryToDelete);
302 
303  std::string printPacketInfo(bool timeAsLocalTime);
304 
305  Layer* createFirstLayer(LinkLayerType linkType);
306  };
307 
308  template<class TLayer>
310  {
311  if (dynamic_cast<TLayer*>(m_FirstLayer) != NULL)
312  return (TLayer*)m_FirstLayer;
313 
314  return getNextLayerOfType<TLayer>(m_FirstLayer);
315  }
316 
317  template<class TLayer>
319  {
320  if (after == NULL)
321  return NULL;
322 
323  Layer* curLayer = after->getNextLayer();
324  while ((curLayer != NULL) && (dynamic_cast<TLayer*>(curLayer) == NULL))
325  {
326  curLayer = curLayer->getNextLayer();
327  }
328 
329  return (TLayer*)curLayer;
330  }
331 
332 } // namespace pcpp
333 
334 #endif /* PACKETPP_PACKET */
std::string toString(bool timeAsLocalTime=true)
Definition: ProtocolType.h:21
bool insertLayer(Layer *prevLayer, Layer *newLayer, bool ownInPacket=false)
Layer * getLastLayer()
Definition: Packet.h:139
void setRawPacket(RawPacket *rawPacket, bool freeRawPacket, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
bool addLayer(Layer *newLayer, bool ownInPacket=false)
Definition: Layer.h:70
TLayer * getNextLayerOfType(Layer *after)
Definition: Packet.h:318
bool removeAllLayersAfter(Layer *layer)
Layer * detachLayer(ProtocolType layerType, int index=0)
Definition: RawPacket.h:219
Definition: Packet.h:26
Packet & operator=(const Packet &other)
RawPacket * getRawPacketReadOnly() const
Definition: Packet.h:127
Layer * getFirstLayer()
Definition: Packet.h:133
virtual ~Packet()
OsiModelLayer
Definition: ProtocolType.h:213
Layer * getNextLayer()
Definition: Layer.h:81
Definition: ProtocolType.h:230
RawPacket * getRawPacket()
Definition: Packet.h:110
void computeCalculateFields()
ProtocolType
Definition: ProtocolType.h:16
bool removeFirstLayer()
bool isPacketOfType(ProtocolType protocolType)
Definition: Packet.h:268
Packet(size_t maxPacketLen=1)
bool removeLastLayer()
void toStringList(std::vector< std::string > &result, bool timeAsLocalTime=true)
The main namespace for the PcapPlusPlus lib.
LinkLayerType
Definition: RawPacket.h:24
bool removeLayer(ProtocolType layerType, int index=0)
TLayer * getLayerOfType()
Definition: Packet.h:309