PcapPlusPlus
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) = 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  inline Layer* getNextLayer() { return m_NextLayer; }
82 
86  inline Layer* getPrevLayer() { return m_PrevLayer; }
87 
91  inline ProtocolType getProtocol() { return m_Protocol; }
92 
96  inline uint8_t* getData() { return m_Data; }
97 
101  inline size_t getDataLen() { return m_DataLen; }
102 
106  uint8_t* getLayerPayload() { return m_Data + getHeaderLen(); }
107 
111  size_t getLayerPayloadSize() { return m_DataLen - getHeaderLen(); }
112 
121  inline bool isAllocatedToPacket() { return m_Packet != NULL; }
122 
127  void copyData(uint8_t* toArr);
128 
129 
130  // implement abstract methods
131 
132  uint8_t* getDataPtr(size_t offset = 0) { return (uint8_t*)(m_Data + offset); }
133 
134 
135  // abstract methods
136 
140  virtual void parseNextLayer() = 0;
141 
145  virtual size_t getHeaderLen() = 0;
146 
150  virtual void computeCalculateFields() = 0;
151 
155  virtual std::string toString() = 0;
156 
160  virtual OsiModelLayer getOsiModelLayer() = 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  inline void setNextLayer(Layer* nextLayer) { m_NextLayer = nextLayer; }
183  inline 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 */
bool isAllocatedToPacket()
Definition: Layer.h:121
uint8_t * getLayerPayload()
Definition: Layer.h:106
Definition: Packet.h:26
size_t getDataLen()
Definition: Layer.h:101
The main namespace for the PcapPlusPlus lib.
Layer * getNextLayer()
Definition: Layer.h:81
Definition: ProtocolType.h:21
size_t getLayerPayloadSize()
Definition: Layer.h:111
uint8_t * getData()
Definition: Layer.h:96
OsiModelLayer
Definition: ProtocolType.h:208
virtual size_t getHeaderLen()=0
Definition: Layer.h:23
uint8_t * getDataPtr(size_t offset=0)
Definition: Layer.h:132
virtual void computeCalculateFields()=0
ProtocolType getProtocol()
Definition: Layer.h:91
Definition: Layer.h:70
ProtocolType
Definition: ProtocolType.h:16
void copyData(uint8_t *toArr)
virtual uint8_t * getDataPtr(size_t offset=0)=0
Layer * getPrevLayer()
Definition: Layer.h:86
virtual std::string toString()=0
virtual OsiModelLayer getOsiModelLayer()=0
virtual ~Layer()
virtual void parseNextLayer()=0