PcapPlusPlus  21.11
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  {
72  friend class Packet;
73  public:
77  virtual ~Layer();
78 
82  Layer* getNextLayer() const { return m_NextLayer; }
83 
87  Layer* getPrevLayer() const { return m_PrevLayer; }
88 
92  ProtocolType getProtocol() const { return m_Protocol; }
93 
97  uint8_t* getData() const { return m_Data; }
98 
102  size_t getDataLen() const { return m_DataLen; }
103 
107  uint8_t* getLayerPayload() const { return m_Data + getHeaderLen(); }
108 
112  size_t getLayerPayloadSize() const { return m_DataLen - getHeaderLen(); }
113 
122  bool isAllocatedToPacket() const { return m_Packet != NULL; }
123 
128  void copyData(uint8_t* toArr) const;
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 inline std::ostream& operator<<(std::ostream& os, const pcpp::Layer &layer)
192 {
193  os << layer.toString();
194  return os;
195 }
196 
197 #endif /* PACKETPP_LAYER */
size_t getLayerPayloadSize() const
Definition: Layer.h:112
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:258
uint8_t * getLayerPayload() const
Definition: Layer.h:107
uint8_t * getData() const
Definition: Layer.h:97
Definition: Layer.h:70
Layer * getNextLayer() const
Definition: Layer.h:82
Definition: Packet.h:26
size_t getDataLen() const
Definition: Layer.h:102
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:122
ProtocolType getProtocol() const
Definition: Layer.h:92
Definition: Layer.h:23
virtual std::string toString() const =0
virtual uint8_t * getDataPtr(size_t offset=0) const =0
Layer * getPrevLayer() const
Definition: Layer.h:87