PcapPlusPlus  20.08+
Layer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_LAYER
2 #define PACKETPP_LAYER
3 
4 #include <stdint.h>
5 #include <stdio.h>
6 #include "ProtocolType.h"
7 #include <string>
8 
10 
15 namespace pcpp
16 {
17 
24  {
25  public:
31  virtual uint8_t* getDataPtr(size_t offset = 0) const = 0;
32 
33  virtual ~IDataContainer() {}
34  };
35 
36  class Packet;
37 
70  class Layer : public IDataContainer {
71  friend class Packet;
72  public:
76  virtual ~Layer();
77 
81  Layer* getNextLayer() const { return m_NextLayer; }
82 
86  Layer* getPrevLayer() const { return m_PrevLayer; }
87 
91  ProtocolType getProtocol() const { return m_Protocol; }
92 
96  uint8_t* getData() const { return m_Data; }
97 
101  size_t getDataLen() const { return m_DataLen; }
102 
106  uint8_t* getLayerPayload() const { return m_Data + getHeaderLen(); }
107 
111  size_t getLayerPayloadSize() const { return m_DataLen - getHeaderLen(); }
112 
121  bool isAllocatedToPacket() const { return m_Packet != NULL; }
122 
127  void copyData(uint8_t* toArr) const;
128 
129 
130  // implement abstract methods
131 
132  uint8_t* getDataPtr(size_t offset = 0) const { return (uint8_t*)(m_Data + offset); }
133 
134 
135  // abstract methods
136 
140  virtual void parseNextLayer() = 0;
141 
145  virtual size_t getHeaderLen() const = 0;
146 
150  virtual void computeCalculateFields() = 0;
151 
155  virtual std::string toString() const = 0;
156 
160  virtual OsiModelLayer getOsiModelLayer() const = 0;
161 
162  protected:
163  uint8_t* m_Data;
164  size_t m_DataLen;
165  Packet* m_Packet;
166  ProtocolType m_Protocol;
167  Layer* m_NextLayer;
168  Layer* m_PrevLayer;
169  bool m_IsAllocatedInPacket;
170 
171  Layer() : m_Data(NULL), m_DataLen(0), m_Packet(NULL), m_Protocol(UnknownProtocol), m_NextLayer(NULL), m_PrevLayer(NULL), m_IsAllocatedInPacket(false) { }
172 
173  Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) :
174  m_Data(data), m_DataLen(dataLen),
175  m_Packet(packet), m_Protocol(UnknownProtocol),
176  m_NextLayer(NULL), m_PrevLayer(prevLayer), m_IsAllocatedInPacket(false) {}
177 
178  // Copy c'tor
179  Layer(const Layer& other);
180  Layer& operator=(const Layer& other);
181 
182  void setNextLayer(Layer* nextLayer) { m_NextLayer = nextLayer; }
183  void setPrevLayer(Layer* prevLayer) { m_PrevLayer = prevLayer; }
184 
185  virtual bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend);
186  virtual bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten);
187  };
188 
189 } // namespace pcpp
190 
191 #endif /* PACKETPP_LAYER */
size_t getLayerPayloadSize() const
Definition: Layer.h:111
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:238
uint8_t * getLayerPayload() const
Definition: Layer.h:106
uint8_t * getData() const
Definition: Layer.h:96
Definition: Layer.h:70
Layer * getNextLayer() const
Definition: Layer.h:81
Definition: Packet.h:26
size_t getDataLen() const
Definition: Layer.h:101
uint8_t * getDataPtr(size_t offset=0) const
Definition: Layer.h:132
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:23
uint64_t ProtocolType
Definition: ProtocolType.h:18
bool isAllocatedToPacket() const
Definition: Layer.h:121
ProtocolType getProtocol() const
Definition: Layer.h:91
Definition: Layer.h:23
virtual uint8_t * getDataPtr(size_t offset=0) const =0
Layer * getPrevLayer() const
Definition: Layer.h:86