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 
12 namespace pcpp
13 {
14 
19  {
20  public:
24  virtual uint8_t* getDataPtr(size_t offset = 0) const = 0;
25 
26  virtual ~IDataContainer() = default;
27  };
28 
29  class Packet;
30 
59  class Layer : public IDataContainer
60  {
61  friend class Packet;
62 
63  public:
66  ~Layer() override;
67 
70  {
71  return m_NextLayer;
72  }
73 
76  {
77  return m_PrevLayer;
78  }
79 
82  {
83  return m_Protocol;
84  }
85 
89  bool isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const;
90 
92  uint8_t* getData() const
93  {
94  return m_Data;
95  }
96 
98  size_t getDataLen() const
99  {
100  return m_DataLen;
101  }
102 
104  uint8_t* getLayerPayload() const
105  {
106  return m_Data + getHeaderLen();
107  }
108 
110  size_t getLayerPayloadSize() const
111  {
112  return m_DataLen - getHeaderLen();
113  }
114 
124  bool isAllocatedToPacket() const
125  {
126  return m_Packet != nullptr;
127  }
128 
131  void copyData(uint8_t* toArr) const;
132 
133  // implement abstract methods
134 
135  uint8_t* getDataPtr(size_t offset = 0) const override
136  {
137  return static_cast<uint8_t*>(m_Data + offset);
138  }
139 
140  // abstract methods
141 
143  virtual void parseNextLayer() = 0;
144 
146  virtual size_t getHeaderLen() const = 0;
147 
149  virtual void computeCalculateFields() = 0;
150 
153  virtual std::string toString() const = 0;
154 
156  virtual OsiModelLayer getOsiModelLayer() const = 0;
157 
158  protected:
159  uint8_t* m_Data;
160  size_t m_DataLen;
161  Packet* m_Packet;
162  ProtocolType m_Protocol;
163  Layer* m_NextLayer;
164  Layer* m_PrevLayer;
165  bool m_IsAllocatedInPacket;
166 
167  Layer()
168  : m_Data(nullptr), m_DataLen(0), m_Packet(nullptr), m_Protocol(UnknownProtocol), m_NextLayer(nullptr),
169  m_PrevLayer(nullptr), m_IsAllocatedInPacket(false)
170  {}
171 
172  Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol = UnknownProtocol)
173  : m_Data(data), m_DataLen(dataLen), m_Packet(packet), m_Protocol(protocol), m_NextLayer(nullptr),
174  m_PrevLayer(prevLayer), m_IsAllocatedInPacket(false)
175  {}
176 
177  // Copy c'tor
178  Layer(const Layer& other);
179  Layer& operator=(const Layer& other);
180 
181  void setNextLayer(Layer* nextLayer)
182  {
183  m_NextLayer = nextLayer;
184  }
185  void setPrevLayer(Layer* prevLayer)
186  {
187  m_PrevLayer = prevLayer;
188  }
189 
190  virtual bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend);
191  virtual bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten);
192  };
193 
194  inline std::ostream& operator<<(std::ostream& os, const pcpp::Layer& layer)
195  {
196  os << layer.toString();
197  return os;
198  }
199 } // namespace pcpp
Definition: Layer.h:19
virtual uint8_t * getDataPtr(size_t offset=0) const =0
Definition: Layer.h:60
void copyData(uint8_t *toArr) const
uint8_t * getLayerPayload() const
Definition: Layer.h:104
virtual std::string toString() const =0
size_t getDataLen() const
Definition: Layer.h:98
uint8_t * getDataPtr(size_t offset=0) const override
Definition: Layer.h:135
uint8_t * getData() const
Definition: Layer.h:92
virtual void parseNextLayer()=0
Each layer is responsible for parsing the next layer.
bool isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const
Layer * getNextLayer() const
Definition: Layer.h:69
bool isAllocatedToPacket() const
Definition: Layer.h:124
Layer * getPrevLayer() const
Definition: Layer.h:75
ProtocolType getProtocol() const
Definition: Layer.h:81
size_t getLayerPayloadSize() const
Definition: Layer.h:110
virtual void computeCalculateFields()=0
Each layer can compute field values automatically using this method. This is an abstract method.
virtual size_t getHeaderLen() const =0
~Layer() override
virtual OsiModelLayer getOsiModelLayer() const =0
Definition: Packet.h:22
The main namespace for the PcapPlusPlus lib.
uint8_t ProtocolType
Definition: ProtocolType.h:13
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
uint32_t ProtocolTypeFamily
Definition: ProtocolType.h:17
const ProtocolType UnknownProtocol
Unknown protocol (or unsupported by PcapPlusPlus)
Definition: ProtocolType.h:20