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 
149  bool addLayer(Layer* newLayer);
150 
161  bool insertLayer(Layer* prevLayer, Layer* newLayer);
162 
169  bool removeLayer(Layer* layer);
170 
175  template<class TLayer>
176  TLayer* getLayerOfType();
177 
186  template<class TLayer>
187  TLayer* getNextLayerOfType(Layer* after);
188 
194  inline bool isPacketOfType(ProtocolType protocolType) { return m_ProtocolTypes & protocolType; }
195 
200  void computeCalculateFields();
201 
208  std::string toString(bool timeAsLocalTime = true);
209 
215  void toStringList(std::vector<std::string>& result, bool timeAsLocalTime = true);
216 
217  private:
218  void copyDataFrom(const Packet& other);
219 
220  void destructPacketData();
221 
222  bool extendLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToExtend);
223  bool shortenLayer(Layer* layer, int offsetInLayer, size_t numOfBytesToShorten);
224 
225  void reallocateRawData(size_t newSize);
226 
227  std::string printPacketInfo(bool timeAsLocalTime);
228  };
229 
230  template<class TLayer>
232  {
233  if (dynamic_cast<TLayer*>(m_FirstLayer) != NULL)
234  return (TLayer*)m_FirstLayer;
235 
236  return getNextLayerOfType<TLayer>(m_FirstLayer);
237  }
238 
239  template<class TLayer>
241  {
242  if (after == NULL)
243  return NULL;
244 
245  Layer* curLayer = after->getNextLayer();
246  while ((curLayer != NULL) && (dynamic_cast<TLayer*>(curLayer) == NULL))
247  {
248  curLayer = curLayer->getNextLayer();
249  }
250 
251  return (TLayer*)curLayer;
252  }
253 
254 } // namespace pcpp
255 
256 #endif /* PACKETPP_PACKET */
bool removeLayer(Layer *layer)
bool isPacketOfType(ProtocolType protocolType)
Definition: Packet.h:194
void setRawPacket(RawPacket *rawPacket, bool freeRawPacket, ProtocolType parseUntil=UnknownProtocol, OsiModelLayer parseUntilLayer=OsiModelLayerUnknown)
TLayer * getNextLayerOfType(Layer *after)
Definition: Packet.h:240
TLayer * getLayerOfType()
Definition: Packet.h:231
Definition: RawPacket.h:220
Definition: Packet.h:26
Layer * getLastLayer()
Definition: Packet.h:139
The main namespace for the PcapPlusPlus lib.
Layer * getNextLayer()
Definition: Layer.h:81
Definition: ProtocolType.h:21
OsiModelLayer
Definition: ProtocolType.h:208
virtual ~Packet()
bool insertLayer(Layer *prevLayer, Layer *newLayer)
std::string toString(bool timeAsLocalTime=true)
bool addLayer(Layer *newLayer)
Packet(size_t maxPacketLen=1)
Definition: Layer.h:70
ProtocolType
Definition: ProtocolType.h:16
Layer * getFirstLayer()
Definition: Packet.h:133
RawPacket * getRawPacket()
Definition: Packet.h:110
Definition: ProtocolType.h:225
RawPacket * getRawPacketReadOnly() const
Definition: Packet.h:127
void toStringList(std::vector< std::string > &result, bool timeAsLocalTime=true)
Packet & operator=(const Packet &other)
void computeCalculateFields()