PcapPlusPlus  Next
Layer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdint.h>
4 #include <stdio.h>
5 #include "ProtocolType.h"
6 #include <string>
7 
9 
14 namespace pcpp
15 {
16 
23  {
24  public:
30  virtual uint8_t* getDataPtr(size_t offset = 0) const = 0;
31 
32  virtual ~IDataContainer() = default;
33  };
34 
35  class Packet;
36 
68  class Layer : public IDataContainer
69  {
70  friend class Packet;
71 
72  public:
77  ~Layer() override;
78 
83  {
84  return m_NextLayer;
85  }
86 
91  {
92  return m_PrevLayer;
93  }
94 
99  {
100  return m_Protocol;
101  }
102 
108  bool isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const;
109 
113  uint8_t* getData() const
114  {
115  return m_Data;
116  }
117 
121  size_t getDataLen() const
122  {
123  return m_DataLen;
124  }
125 
129  uint8_t* getLayerPayload() const
130  {
131  return m_Data + getHeaderLen();
132  }
133 
137  size_t getLayerPayloadSize() const
138  {
139  return m_DataLen - getHeaderLen();
140  }
141 
153  bool isAllocatedToPacket() const
154  {
155  return m_Packet != nullptr;
156  }
157 
162  void copyData(uint8_t* toArr) const;
163 
164  // implement abstract methods
165 
166  uint8_t* getDataPtr(size_t offset = 0) const override
167  {
168  return static_cast<uint8_t*>(m_Data + offset);
169  }
170 
171  // abstract methods
172 
176  virtual void parseNextLayer() = 0;
177 
181  virtual size_t getHeaderLen() const = 0;
182 
186  virtual void computeCalculateFields() = 0;
187 
192  virtual std::string toString() const = 0;
193 
197  virtual OsiModelLayer getOsiModelLayer() const = 0;
198 
199  protected:
200  uint8_t* m_Data;
201  size_t m_DataLen;
202  Packet* m_Packet;
203  ProtocolType m_Protocol;
204  Layer* m_NextLayer;
205  Layer* m_PrevLayer;
206  bool m_IsAllocatedInPacket;
207 
208  Layer()
209  : m_Data(nullptr), m_DataLen(0), m_Packet(nullptr), m_Protocol(UnknownProtocol), m_NextLayer(nullptr),
210  m_PrevLayer(nullptr), m_IsAllocatedInPacket(false)
211  {}
212 
213  Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol = UnknownProtocol)
214  : m_Data(data), m_DataLen(dataLen), m_Packet(packet), m_Protocol(protocol), m_NextLayer(nullptr),
215  m_PrevLayer(prevLayer), m_IsAllocatedInPacket(false)
216  {}
217 
218  // Copy c'tor
219  Layer(const Layer& other);
220  Layer& operator=(const Layer& other);
221 
222  void setNextLayer(Layer* nextLayer)
223  {
224  m_NextLayer = nextLayer;
225  }
226  void setPrevLayer(Layer* prevLayer)
227  {
228  m_PrevLayer = prevLayer;
229  }
230 
231  virtual bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend);
232  virtual bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten);
233  };
234 
235 } // namespace pcpp
236 
237 inline std::ostream& operator<<(std::ostream& os, const pcpp::Layer& layer)
238 {
239  os << layer.toString();
240  return os;
241 }
Definition: Layer.h:23
virtual uint8_t * getDataPtr(size_t offset=0) const =0
Definition: Layer.h:69
void copyData(uint8_t *toArr) const
uint8_t * getLayerPayload() const
Definition: Layer.h:129
virtual std::string toString() const =0
size_t getDataLen() const
Definition: Layer.h:121
uint8_t * getDataPtr(size_t offset=0) const override
Definition: Layer.h:166
uint8_t * getData() const
Definition: Layer.h:113
virtual void parseNextLayer()=0
bool isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const
Layer * getNextLayer() const
Definition: Layer.h:82
bool isAllocatedToPacket() const
Definition: Layer.h:153
Layer * getPrevLayer() const
Definition: Layer.h:90
ProtocolType getProtocol() const
Definition: Layer.h:98
size_t getLayerPayloadSize() const
Definition: Layer.h:137
virtual void computeCalculateFields()=0
virtual size_t getHeaderLen() const =0
~Layer() override
virtual OsiModelLayer getOsiModelLayer() const =0
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:17
OsiModelLayer
Definition: ProtocolType.h:364
uint32_t ProtocolTypeFamily
Definition: ProtocolType.h:23
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:28