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  std::vector<Layer*> m_LayersAllocatedInPacket;
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  inline RawPacket* getRawPacket() { return m_RawPacket; }
112 
122  void setRawPacket(RawPacket* rawPacket, bool freeRawPacket, ProtocolType parseUntil = UnknownProtocol, OsiModelLayer parseUntilLayer = OsiModelLayerUnknown);
123 
128  inline RawPacket* getRawPacketReadOnly() const { return m_RawPacket; }
129 
134  inline Layer* getFirstLayer() { return m_FirstLayer; }
135 
140  inline Layer* getLastLayer() { return m_LastLayer; }
141 
150  bool addLayer(Layer* newLayer);
151 
162  bool insertLayer(Layer* prevLayer, Layer* newLayer);
163 
170  bool removeLayer(Layer* layer);
171 
176  template<class TLayer>
177  TLayer* getLayerOfType();
178 
187  template<class TLayer>
188  TLayer* getNextLayerOfType(Layer* after);
189 
195  inline bool isPacketOfType(ProtocolType protocolType) { return m_ProtocolTypes & protocolType; }
196 
201  void computeCalculateFields();
202 
209  std::string printToString(bool timeAsLocalTime = true);
210 
216  void printToStringList(std::vector<std::string>& result, bool timeAsLocalTime = true);
217 
218  private:
219  void copyDataFrom(const Packet& other);
220 
221  void destructPacketData();
222 
223  bool extendLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToExtend);
224  bool shortenLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToShorten);
225 
226  void reallocateRawData(size_t newSize);
227 
228  std::string printPacketInfo(bool timeAsLocalTime);
229  };
230 
231  template<class TLayer>
233  {
234  if (dynamic_cast<TLayer*>(m_FirstLayer) != NULL)
235  return (TLayer*)m_FirstLayer;
236 
237  return getNextLayerOfType<TLayer>(m_FirstLayer);
238  }
239 
240  template<class TLayer>
242  {
243  if (after == NULL)
244  return NULL;
245 
246  Layer* curLayer = after->getNextLayer();
247  while ((curLayer != NULL) && (dynamic_cast<TLayer*>(curLayer) == NULL))
248  {
249  curLayer = curLayer->getNextLayer();
250  }
251 
252  return (TLayer*)curLayer;
253  }
254 
255 } // namespace pcpp
256 
257 #endif /* PACKETPP_PACKET */
RawPacket * getRawPacketReadOnly() const
Definition: Packet.h:128
bool insertLayer(Layer *prevLayer, Layer *newLayer)
TLayer * getLayerOfType()
Definition: Packet.h:232
Definition: RawPacket.h:220
virtual ~Packet()
bool addLayer(Layer *newLayer)
Layer * getNextLayer()
Definition: Layer.h:63
TLayer * getNextLayerOfType(Layer *after)
Definition: Packet.h:241
void computeCalculateFields()
RawPacket * getRawPacket()
Definition: Packet.h:111
OsiModelLayer
Definition: ProtocolType.h:203
Layer * getFirstLayer()
Definition: Packet.h:134
Definition: Packet.h:26
ProtocolType
Definition: ProtocolType.h:16
Layer * getLastLayer()
Definition: Packet.h:140
Definition: Layer.h:52
std::string printToString(bool timeAsLocalTime=true)
bool removeLayer(Layer *layer)
Definition: ProtocolType.h:21
Packet(size_t maxPacketLen=1)
Packet & operator=(const Packet &other)
The main namespace for the PcapPlusPlus lib.
void setRawPacket(RawPacket *rawPacket, bool freeRawPacket, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
void printToStringList(std::vector< std::string > &result, bool timeAsLocalTime=true)
Definition: ProtocolType.h:220
bool isPacketOfType(ProtocolType protocolType)
Definition: Packet.h:195