PcapPlusPlus  24.09
NflogLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "TLVData.h"
5 #include "GeneralUtils.h"
6 
8 
13 namespace pcpp
14 {
19 #pragma pack(push, 1)
20  struct nflog_header
21  {
23  uint8_t addressFamily;
25  uint8_t version;
27  uint16_t resourceId;
28  };
29 #pragma pack(pop)
30 
35  enum class NflogTlvType
36  {
38  NFULA_PACKET_HDR = 1,
40  NFULA_MARK = 2,
42  NFULA_TIMESTAMP = 3,
52  NFULA_HWADDR = 8,
54  NFULA_PAYLOAD = 9,
56  NFULA_PREFIX = 10,
58  NFULA_UID = 11,
60  NFULA_SEQ = 12,
62  NFULA_SEQ_GLOBAL = 13,
64  NFULA_GID = 14,
66  NFULA_HWTYPE = 15,
68  NFULA_HWHEADER = 16,
70  NFULA_HWLEN = 17,
71  };
72 
78  class NflogTlv
79  {
80  private:
81  struct NflogTLVRawData
82  {
84  uint16_t recordLen;
86  uint16_t recordType;
88  uint8_t recordValue[];
89  };
90  NflogTLVRawData* m_Data;
91 
92  public:
97  explicit NflogTlv(uint8_t* recordRawData)
98  {
99  assign(recordRawData);
100  }
101 
105  size_t getTotalSize() const
106  {
107  // as in
108  // https://github.com/the-tcpdump-group/libpcap/blob/766b607d60d8038087b49fc4cf433dac3dcdb49c/pcap-util.c#L371-L374
109  return align<4>(m_Data->recordLen);
110  }
111 
116  void assign(uint8_t* recordRawData)
117  {
118  m_Data = (NflogTLVRawData*)recordRawData;
119  }
120 
127  static bool canAssign(const uint8_t* recordRawData, size_t tlvDataLen)
128  {
129  return recordRawData != nullptr && tlvDataLen >= sizeof(NflogTLVRawData::recordLen);
130  }
131 
135  bool isNull() const
136  {
137  return (m_Data == nullptr);
138  }
139 
143  uint16_t getType() const
144  {
145  return m_Data->recordType;
146  }
147 
151  uint8_t* getRecordBasePtr() const
152  {
153  return (uint8_t*)m_Data;
154  }
155 
159  uint8_t* getValue() const
160  {
161  return m_Data->recordValue;
162  }
163  };
164 
169  class NflogLayer : public Layer
170  {
171  public:
178  NflogLayer(uint8_t* data, size_t dataLen, Packet* packet) : Layer(data, dataLen, nullptr, packet, NFLOG)
179  {}
180 
181  ~NflogLayer()
182  {}
183 
189  {
190  return (nflog_header*)m_Data;
191  }
192 
197  uint8_t getFamily();
198 
204  uint8_t getVersion();
205 
211  uint16_t getResourceId();
212 
220 
221  // implement abstract methods
222 
228 
232  size_t getHeaderLen() const;
233 
238 
239  std::string toString() const;
240 
242  {
243  return OsiModelDataLinkLayer;
244  }
245 
252  static bool isDataValid(const uint8_t* data, size_t dataLen);
253 
254  private:
255  uint8_t* getTlvsBasePtr() const
256  {
257  return m_Data + sizeof(nflog_header);
258  }
259 
260  TLVRecordReader<NflogTlv> m_TlvReader;
261  };
262 
263 } // namespace pcpp
Definition: Layer.h:70
Definition: NflogLayer.h:170
NflogLayer(uint8_t *data, size_t dataLen, Packet *packet)
Definition: NflogLayer.h:178
OsiModelLayer getOsiModelLayer() const
Definition: NflogLayer.h:241
nflog_header * getNflogHeader() const
Definition: NflogLayer.h:188
std::string toString() const
uint16_t getResourceId()
size_t getHeaderLen() const
static bool isDataValid(const uint8_t *data, size_t dataLen)
NflogTlv getTlvByType(NflogTlvType type) const
uint8_t getFamily()
uint8_t getVersion()
void computeCalculateFields()
Definition: NflogLayer.h:237
Definition: NflogLayer.h:79
uint16_t getType() const
Definition: NflogLayer.h:143
bool isNull() const
Definition: NflogLayer.h:135
uint8_t * getRecordBasePtr() const
Definition: NflogLayer.h:151
size_t getTotalSize() const
Definition: NflogLayer.h:105
NflogTlv(uint8_t *recordRawData)
Definition: NflogLayer.h:97
static bool canAssign(const uint8_t *recordRawData, size_t tlvDataLen)
Definition: NflogLayer.h:127
void assign(uint8_t *recordRawData)
Definition: NflogLayer.h:116
uint8_t * getValue() const
Definition: NflogLayer.h:159
Definition: Packet.h:27
Definition: TLVData.h:239
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelDataLinkLayer
Definition: ProtocolType.h:358
const ProtocolType NFLOG
Definition: ProtocolType.h:303
NflogTlvType
Definition: NflogLayer.h:36
Definition: NflogLayer.h:21
uint16_t resourceId
Definition: NflogLayer.h:27
uint8_t version
Definition: NflogLayer.h:25
uint8_t addressFamily
Definition: NflogLayer.h:23