PcapPlusPlus
TLVData.h
Go to the documentation of this file.
1 #ifndef PACKETPP_TLV_DATA
2 #define PACKETPP_TLV_DATA
3 
4 #include "Layer.h"
5 #include "IpAddress.h"
6 #include <string.h>
7 
9 
14 namespace pcpp
15 {
23  class TLVRecord
24  {
25  protected:
26 
28  struct TLVRawData
29  {
31  uint8_t recordType;
33  uint8_t recordLen;
35  uint8_t recordValue[];
36  };
37 
38  TLVRawData* m_Data;
39 
40  public:
41 
46  TLVRecord(uint8_t* recordRawData)
47  {
48  if (recordRawData == NULL)
49  m_Data = NULL;
50  else
51  m_Data = (TLVRawData*)recordRawData;
52  }
53 
59  TLVRecord(const TLVRecord& other)
60  {
61  m_Data = other.m_Data;
62  }
63 
67  virtual ~TLVRecord() { }
68 
74  TLVRecord& operator=(const TLVRecord& other)
75  {
76  m_Data = other.m_Data;
77  return *this;
78  }
79 
86  bool operator==(const TLVRecord& rhs)
87  {
88  if (m_Data == rhs.m_Data)
89  return true;
90 
91  if (getTotalSize() != rhs.getTotalSize())
92  return false;
93 
94  if (isNull() || ((TLVRecord&)rhs).isNull())
95  return false;
96 
97  return (memcmp(m_Data, rhs.m_Data, getTotalSize()) == 0);
98  }
99 
100 
104  uint8_t getType() { return m_Data->recordType; }
105 
109  uint8_t* getValue() { return m_Data->recordValue; }
110 
114  bool isNull() { return (m_Data == NULL); }
115 
119  bool isNotNull() { return (m_Data != NULL); }
120 
124  uint8_t* getRecordBasePtr() { return (uint8_t*)m_Data; }
125 
129  void purgeRecordData() { if (!isNull()) delete [] m_Data; }
130 
140  template<typename T>
141  T getValueAs(size_t offset = 0)
142  {
143  if (getDataSize() - offset < sizeof(T))
144  return 0;
145 
146  T result;
147  memcpy(&result, m_Data->recordValue + offset, sizeof(T));
148  return result;
149  }
150 
160  template<typename T>
161  bool setValue(T newValue, int valueOffset = 0)
162  {
163  if (getDataSize() < sizeof(T))
164  return false;
165 
166  memcpy(m_Data->recordValue + valueOffset, &newValue, sizeof(T));
167  return true;
168  }
169 
173  virtual size_t getTotalSize() const = 0;
174 
178  virtual size_t getDataSize() = 0;
179 
180  };
181 
182 
188  template<typename TLVRecordType>
190  {
191  private:
192  size_t m_RecordCount;
193 
194  public:
195 
199  TLVRecordReader() { m_RecordCount = (size_t)-1; }
200 
205  {
206  m_RecordCount = other.m_RecordCount;
207  }
208 
212  virtual ~TLVRecordReader() { }
213 
219  {
220  m_RecordCount = other.m_RecordCount;
221  return *this;
222  }
223 
232  TLVRecordType getFirstTLVRecord(uint8_t* tlvDataBasePtr, size_t tlvDataLen)
233  {
234  // check if there are records at all
235  if (tlvDataLen == 0)
236  return TLVRecordType(NULL);
237 
238  return TLVRecordType(tlvDataBasePtr);
239  }
240 
250  TLVRecordType getNextTLVRecord(TLVRecordType& record, uint8_t* tlvDataBasePtr, size_t tlvDataLen)
251  {
252  if (record.isNull())
253  return TLVRecordType(NULL);
254 
255  // record pointer is out-bounds of the TLV records memory
256  if ((record.getRecordBasePtr() - tlvDataBasePtr) < 0)
257  return TLVRecordType(NULL);
258 
259  // record pointer is out-bounds of the TLV records memory
260  if (record.getRecordBasePtr() - tlvDataBasePtr + (int)record.getTotalSize() >= (int)tlvDataLen)
261  return TLVRecordType(NULL);
262 
263  return TLVRecordType(record.getRecordBasePtr() + record.getTotalSize());
264  }
265 
274  TLVRecordType getTLVRecord(uint8_t recordType, uint8_t* tlvDataBasePtr, size_t tlvDataLen)
275  {
276  TLVRecordType curRec = getFirstTLVRecord(tlvDataBasePtr, tlvDataLen);
277  while (!curRec.isNull())
278  {
279  if (curRec.getType() == recordType)
280  return curRec;
281 
282  curRec = getNextTLVRecord(curRec, tlvDataBasePtr, tlvDataLen);
283  }
284 
285  return TLVRecordType(NULL);
286  }
287 
297  size_t getTLVRecordCount(uint8_t* tlvDataBasePtr, size_t tlvDataLen)
298  {
299  if (m_RecordCount != (size_t)-1)
300  return m_RecordCount;
301 
302  m_RecordCount = 0;
303  TLVRecordType curRec = getFirstTLVRecord(tlvDataBasePtr, tlvDataLen);
304  while (!curRec.isNull())
305  {
306  m_RecordCount++;
307  curRec = getNextTLVRecord(curRec, tlvDataBasePtr, tlvDataLen);
308  }
309 
310  return m_RecordCount;
311  }
312 
320  void changeTLVRecordCount(int changedBy) { if (m_RecordCount != (size_t)-1) m_RecordCount += changedBy; }
321  };
322 
323 
334  {
335  protected:
336 
338 
339  TLVRecordBuilder(uint8_t recType, const uint8_t* recValue, uint8_t recValueLen);
340 
341  TLVRecordBuilder(uint8_t recType, uint8_t recValue);
342 
343  TLVRecordBuilder(uint8_t recType, uint16_t recValue);
344 
345  TLVRecordBuilder(uint8_t recType, uint32_t recValue);
346 
347  TLVRecordBuilder(uint8_t recType, const IPv4Address& recValue);
348 
349  TLVRecordBuilder(uint8_t recType, const std::string& recValue);
350 
351  TLVRecordBuilder(const TLVRecordBuilder& other);
352 
353  virtual ~TLVRecordBuilder();
354 
355  void init(uint8_t recType, const uint8_t* recValue, uint8_t recValueLen);
356 
357  uint8_t* m_RecValue;
358  uint8_t m_RecValueLen;
359  uint8_t m_RecType;
360  };
361 }
362 #endif // PACKETPP_TLV_DATA
uint8_t * getRecordBasePtr()
Definition: TLVData.h:124
uint8_t recordLen
Definition: TLVData.h:33
void changeTLVRecordCount(int changedBy)
Definition: TLVData.h:320
uint8_t recordType
Definition: TLVData.h:31
uint8_t * getValue()
Definition: TLVData.h:109
TLVRecordType getFirstTLVRecord(uint8_t *tlvDataBasePtr, size_t tlvDataLen)
Definition: TLVData.h:232
Definition: TLVData.h:333
Definition: TLVData.h:23
bool isNotNull()
Definition: TLVData.h:119
T getValueAs(size_t offset=0)
Definition: TLVData.h:141
uint8_t recordValue[]
Definition: TLVData.h:35
Definition: IpAddress.h:113
virtual ~TLVRecord()
Definition: TLVData.h:67
TLVRecordType getTLVRecord(uint8_t recordType, uint8_t *tlvDataBasePtr, size_t tlvDataLen)
Definition: TLVData.h:274
TLVRecordType getNextTLVRecord(TLVRecordType &record, uint8_t *tlvDataBasePtr, size_t tlvDataLen)
Definition: TLVData.h:250
void purgeRecordData()
Definition: TLVData.h:129
size_t getTLVRecordCount(uint8_t *tlvDataBasePtr, size_t tlvDataLen)
Definition: TLVData.h:297
bool isNull()
Definition: TLVData.h:114
uint8_t getType()
Definition: TLVData.h:104
Definition: TLVData.h:189
TLVRecordReader(const TLVRecordReader &other)
Definition: TLVData.h:204
Definition: TLVData.h:28
virtual size_t getTotalSize() const =0
TLVRecordReader & operator=(const TLVRecordReader &other)
Definition: TLVData.h:218
bool setValue(T newValue, int valueOffset=0)
Definition: TLVData.h:161
TLVRecordReader()
Definition: TLVData.h:199
virtual ~TLVRecordReader()
Definition: TLVData.h:212
TLVRecord(const TLVRecord &other)
Definition: TLVData.h:59
The main namespace for the PcapPlusPlus lib.
virtual size_t getDataSize()=0
bool operator==(const TLVRecord &rhs)
Definition: TLVData.h:86
TLVRecord & operator=(const TLVRecord &other)
Definition: TLVData.h:74
TLVRecord(uint8_t *recordRawData)
Definition: TLVData.h:46