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 
18  class Packet;
19 
52  class Layer {
53  friend class Packet;
54  public:
58  virtual ~Layer();
59 
63  inline Layer* getNextLayer() { return m_NextLayer; }
64 
68  inline Layer* getPrevLayer() { return m_PrevLayer; }
69 
73  inline ProtocolType getProtocol() { return m_Protocol; }
74 
78  inline uint8_t* getData() { return m_Data; }
79 
83  inline size_t getDataLen() { return m_DataLen; }
84 
88  uint8_t* getLayerPayload() { return m_Data + getHeaderLen(); }
89 
93  size_t getLayerPayloadSize() { return m_DataLen - getHeaderLen(); }
94 
103  inline bool isAllocatedToPacket() { return m_Packet != NULL; }
104 
109  void copyData(uint8_t* toArr);
110 
111 
112  // abstract methods
113 
117  virtual void parseNextLayer() = 0;
118 
122  virtual size_t getHeaderLen() = 0;
123 
127  virtual void computeCalculateFields() = 0;
128 
132  virtual std::string toString() = 0;
133 
137  virtual OsiModelLayer getOsiModelLayer() = 0;
138 
139  protected:
140  uint8_t* m_Data;
141  size_t m_DataLen;
142  Packet* m_Packet;
143  ProtocolType m_Protocol;
144  Layer* m_NextLayer;
145  Layer* m_PrevLayer;
146 
147  Layer() : m_Data(NULL), m_DataLen(0), m_Packet(NULL), m_Protocol(UnknownProtocol), m_NextLayer(NULL), m_PrevLayer(NULL) { }
148 
149  Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) :
150  m_Data(data), m_DataLen(dataLen),
151  m_Packet(packet), m_Protocol(UnknownProtocol),
152  m_NextLayer(NULL), m_PrevLayer(prevLayer) {}
153 
154  // Copy c'tor
155  Layer(const Layer& other);
156  Layer& operator=(const Layer& other);
157 
158  inline void setNextLayer(Layer* nextLayer) { m_NextLayer = nextLayer; }
159  inline void setPrevLayer(Layer* prevLayer) { m_PrevLayer = prevLayer; }
160 
161  virtual bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend);
162  virtual bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten);
163  };
164 
165 } // namespace pcpp
166 
167 #endif /* PACKETPP_LAYER */
Layer * getPrevLayer()
Definition: Layer.h:68
virtual std::string toString()=0
uint8_t * getData()
Definition: Layer.h:78
virtual OsiModelLayer getOsiModelLayer()=0
virtual void parseNextLayer()=0
Layer * getNextLayer()
Definition: Layer.h:63
virtual void computeCalculateFields()=0
OsiModelLayer
Definition: ProtocolType.h:203
Definition: Packet.h:26
ProtocolType
Definition: ProtocolType.h:16
size_t getLayerPayloadSize()
Definition: Layer.h:93
size_t getDataLen()
Definition: Layer.h:83
Definition: Layer.h:52
virtual ~Layer()
Definition: ProtocolType.h:21
uint8_t * getLayerPayload()
Definition: Layer.h:88
ProtocolType getProtocol()
Definition: Layer.h:73
virtual size_t getHeaderLen()=0
The main namespace for the PcapPlusPlus lib.
void copyData(uint8_t *toArr)
bool isAllocatedToPacket()
Definition: Layer.h:103