PcapPlusPlus  24.09
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()
33  {}
34  };
35 
36  class Packet;
37 
69  class Layer : public IDataContainer
70  {
71  friend class Packet;
72 
73  public:
78  virtual ~Layer();
79 
84  {
85  return m_NextLayer;
86  }
87 
92  {
93  return m_PrevLayer;
94  }
95 
100  {
101  return m_Protocol;
102  }
103 
109  bool isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const;
110 
114  uint8_t* getData() const
115  {
116  return m_Data;
117  }
118 
122  size_t getDataLen() const
123  {
124  return m_DataLen;
125  }
126 
130  uint8_t* getLayerPayload() const
131  {
132  return m_Data + getHeaderLen();
133  }
134 
138  size_t getLayerPayloadSize() const
139  {
140  return m_DataLen - getHeaderLen();
141  }
142 
154  bool isAllocatedToPacket() const
155  {
156  return m_Packet != nullptr;
157  }
158 
163  void copyData(uint8_t* toArr) const;
164 
165  // implement abstract methods
166 
167  uint8_t* getDataPtr(size_t offset = 0) const
168  {
169  return (uint8_t*)(m_Data + offset);
170  }
171 
172  // abstract methods
173 
177  virtual void parseNextLayer() = 0;
178 
182  virtual size_t getHeaderLen() const = 0;
183 
187  virtual void computeCalculateFields() = 0;
188 
193  virtual std::string toString() const = 0;
194 
198  virtual OsiModelLayer getOsiModelLayer() const = 0;
199 
200  protected:
201  uint8_t* m_Data;
202  size_t m_DataLen;
203  Packet* m_Packet;
204  ProtocolType m_Protocol;
205  Layer* m_NextLayer;
206  Layer* m_PrevLayer;
207  bool m_IsAllocatedInPacket;
208 
209  Layer()
210  : m_Data(nullptr), m_DataLen(0), m_Packet(nullptr), m_Protocol(UnknownProtocol), m_NextLayer(nullptr),
211  m_PrevLayer(nullptr), m_IsAllocatedInPacket(false)
212  {}
213 
214  Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol = UnknownProtocol)
215  : m_Data(data), m_DataLen(dataLen), m_Packet(packet), m_Protocol(protocol), m_NextLayer(nullptr),
216  m_PrevLayer(prevLayer), m_IsAllocatedInPacket(false)
217  {}
218 
219  // Copy c'tor
220  Layer(const Layer& other);
221  Layer& operator=(const Layer& other);
222 
223  void setNextLayer(Layer* nextLayer)
224  {
225  m_NextLayer = nextLayer;
226  }
227  void setPrevLayer(Layer* prevLayer)
228  {
229  m_PrevLayer = prevLayer;
230  }
231 
232  virtual bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend);
233  virtual bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten);
234  };
235 
236 } // namespace pcpp
237 
238 inline std::ostream& operator<<(std::ostream& os, const pcpp::Layer& layer)
239 {
240  os << layer.toString();
241  return os;
242 }
Definition: Layer.h:23
virtual uint8_t * getDataPtr(size_t offset=0) const =0
Definition: Layer.h:70
void copyData(uint8_t *toArr) const
uint8_t * getLayerPayload() const
Definition: Layer.h:130
uint8_t * getDataPtr(size_t offset=0) const
Definition: Layer.h:167
virtual std::string toString() const =0
size_t getDataLen() const
Definition: Layer.h:122
uint8_t * getData() const
Definition: Layer.h:114
virtual void parseNextLayer()=0
bool isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const
Layer * getNextLayer() const
Definition: Layer.h:83
bool isAllocatedToPacket() const
Definition: Layer.h:154
virtual ~Layer()
Layer * getPrevLayer() const
Definition: Layer.h:91
ProtocolType getProtocol() const
Definition: Layer.h:99
size_t getLayerPayloadSize() const
Definition: Layer.h:138
virtual void computeCalculateFields()=0
virtual size_t getHeaderLen() const =0
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:354
uint32_t ProtocolTypeFamily
Definition: ProtocolType.h:23
const ProtocolType UnknownProtocol
Definition: ProtocolType.h:28