1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#ifndef PACKETPP_TLV_DATA
#define PACKETPP_TLV_DATA
#include "Layer.h"
#include "IpAddress.h"
#include <string.h>
/// @file
/**
* \namespace pcpp
* \brief The main namespace for the PcapPlusPlus lib
*/
namespace pcpp
{
/**
* @class TLVRecord
* A wrapper class for a Type-Length-Value (TLV) record. This class does not create or modify TLV records, but rather
* serves as a wrapper and provides useful methods for retrieving data from them. This class has several abstract methods
* that should be implemented in derived classes. These methods are for record length value calculation (the 'L' in TLV)
* which is implemented differently in different protocols
*/
template<typename TRecType, typename TRecLen>
class TLVRecord
{
protected:
/** A struct representing the TLV construct */
struct TLVRawData
{
/** Record type */
TRecType recordType;
/** Record length in bytes */
TRecLen recordLen;
/** Record value (variable size) */
uint8_t recordValue[];
};
TLVRawData* m_Data;
public:
/**
* A c'tor for this class that gets a pointer to the TLV record raw data (byte array)
* @param[in] recordRawData A pointer to the TLV record raw data
*/
TLVRecord(uint8_t* recordRawData)
{
assign(recordRawData);
}
/**
* A copy c'tor for this class. This copy c'tor doesn't copy the TLV data, but only the pointer to it,
* which means that after calling it both the old and the new instance will point to the same TLV raw data
* @param[in] other The TLVRecord instance to copy from
*/
TLVRecord(const TLVRecord& other)
{
m_Data = other.m_Data;
}
/**
* A d'tor for this class, currently does nothing
*/
virtual ~TLVRecord() { }
/**
* Assign a pointer to the TLV record raw data (byte array)
* @param[in] recordRawData A pointer to the TLV record raw data
*/
void assign(uint8_t* recordRawData)
{
if(recordRawData == NULL)
m_Data = NULL;
else
m_Data = (TLVRawData*)recordRawData;
}
/**
* Overload of the assignment operator. This operator doesn't copy the TLV data, but rather copies the pointer to it,
* which means that after calling it both the old and the new instance will point to the same TLV raw data
* @param[in] other The TLVRecord instance to assign
*/
TLVRecord& operator=(const TLVRecord& other)
{
m_Data = other.m_Data;
return *this;
}
/**
* Overload of the equality operator. Two record are equal if both of them point to the same data, or if they point
* to different data but their total size is equal and the raw data they both contain is similar.
* @param[in] rhs The object to compare to
* @return True if both objects are equal, false otherwise
*/
bool operator==(const TLVRecord& rhs) const
{
if (m_Data == rhs.m_Data)
return true;
if (getTotalSize() != rhs.getTotalSize())
return false;
if (isNull() || ((TLVRecord&)rhs).isNull())
return false;
return (memcmp(m_Data, rhs.m_Data, getTotalSize()) == 0);
}
/**
* Overload of the not equal operator.
* @param[in] rhs The object to compare to
* @return True if objects are not equal, false otherwise
*/
bool operator!=(const TLVRecord& rhs) const
{
return !operator==(rhs);
}
/**
* @return The type field of the record (the 'T' in __Type__-Length-Value)
*/
TRecType getType() const {
if (m_Data == nullptr)
return 0;
return m_Data->recordType;
}
/**
* @return A pointer to the value of the record as byte array (the 'V' in Type-Length- __Value__)
*/
uint8_t* getValue() const {
if (m_Data == nullptr)
return nullptr;
return m_Data->recordValue;
}
/**
* @return True if the TLV record raw data is NULL, false otherwise
*/
bool isNull() const { return (m_Data == nullptr); }
/**
* @return True if the TLV record raw data is not NULL, false otherwise
*/
bool isNotNull() const { return (m_Data != nullptr); }
/**
* @return A pointer to the TLV record raw data byte stream
*/
uint8_t* getRecordBasePtr() const { return (uint8_t*)m_Data; }
/**
* Free the memory of the TLV record raw data
*/
void purgeRecordData() { if (!isNull()) delete [] m_Data; }
/**
* A templated method to retrieve the record data as a certain type T. For example, if record data is 4B long
* (integer) then this method should be used as getValueAs<int>() and it will return the record data as an integer.<BR>
* Notice this return value is a copy of the data, not a pointer to the actual data
* @param[in] offset The offset in the record data to start reading the value from. Useful for cases when you want
* to read some of the data that doesn't start at offset 0. This is an optional parameter and the default value
* is 0, meaning start reading the value at the beginning of the record data
* @return The record data as type T
*/
template<typename T>
T getValueAs(size_t offset = 0) const
{
if (getDataSize() - offset < sizeof(T))
return 0;
T result;
memcpy(&result, m_Data->recordValue + offset, sizeof(T));
return result;
}
/**
* A templated method to copy data of type T into the TLV record data. For example: if record data is 4[Bytes] long use
* this method with \<int\> to set an integer value into the record data: setValue<int>(num)
* @param[in] newValue The value of type T to copy to the record data
* @param[in] valueOffset An optional parameter that specifies where to start setting the record data (default set to 0). For example:
* if record data is 20 bytes long and you only need to set the 4 last bytes as integer then use this method like this:
* setValue<int>(num, 16)
* @return True if value was set successfully or false if the size of T is larger than the record data size
*/
template<typename T>
bool setValue(T newValue, int valueOffset = 0)
{
if (getDataSize() < sizeof(T))
return false;
memcpy(m_Data->recordValue + valueOffset, &newValue, sizeof(T));
return true;
}
/**
* @return The total size of the TLV record (in bytes)
*/
virtual size_t getTotalSize() const = 0;
/**
* @return The size of the record value (meaning the size of the 'V' part in TLV)
*/
virtual size_t getDataSize() const = 0;
};
/**
* @class TLVRecordReader
* A class for reading TLV records data out of a byte stream. This class contains helper methods for retrieving and
* counting TLV records. This is a template class that expects template argument class derived from TLVRecord.
*/
template<typename TLVRecordType>
class TLVRecordReader
{
private:
mutable size_t m_RecordCount;
public:
/**
* A default c'tor for this class
*/
TLVRecordReader() { m_RecordCount = (size_t)-1; }
/**
* A default copy c'tor for this class
*/
TLVRecordReader(const TLVRecordReader& other)
{
m_RecordCount = other.m_RecordCount;
}
/**
* A d'tor for this class which currently does nothing
*/
virtual ~TLVRecordReader() { }
/**
* Overload of the assignment operator for this class
* @param[in] other The TLVRecordReader instance to assign
*/
TLVRecordReader& operator=(const TLVRecordReader& other)
{
m_RecordCount = other.m_RecordCount;
return *this;
}
/**
* Get the first TLV record out of a byte stream
* @param[in] tlvDataBasePtr A pointer to the TLV data byte stream
* @param[in] tlvDataLen The TLV data byte stream length
* @return An instance of type TLVRecordType that contains the first TLV record. If tlvDataBasePtr is NULL or
* tlvDataLen is zero the returned TLVRecordType instance will be logically NULL, meaning TLVRecordType.isNull() will
* return true
*/
TLVRecordType getFirstTLVRecord(uint8_t* tlvDataBasePtr, size_t tlvDataLen) const
{
TLVRecordType resRec(tlvDataBasePtr); // for NRVO optimization
// resRec pointer is out-bounds of the TLV records memory
if (resRec.getRecordBasePtr() + resRec.getTotalSize() > tlvDataBasePtr + tlvDataLen)
resRec.assign(NULL);
// check if there are records at all and the total size is not zero
if (!resRec.isNull() && (tlvDataLen == 0 || resRec.getTotalSize() == 0))
resRec.assign(NULL);
return resRec;
}
/**
* Get a TLV record that follows a given TLV record in a byte stream
* @param[in] record A given TLV record
* @param[in] tlvDataBasePtr A pointer to the TLV data byte stream
* @param[in] tlvDataLen The TLV data byte stream length
* @return An instance of type TLVRecordType that wraps the record following the record given as input. If the
* input record.isNull() is true or if the next record is out of bounds of the byte stream, a logical NULL instance
* of TLVRecordType will be returned, meaning TLVRecordType.isNull() will return true
*/
TLVRecordType getNextTLVRecord(TLVRecordType& record, const uint8_t* tlvDataBasePtr, size_t tlvDataLen) const
{
TLVRecordType resRec(NULL); // for NRVO optimization
if (record.isNull())
return resRec;
resRec.assign(record.getRecordBasePtr() + record.getTotalSize());
if (resRec.getTotalSize() == 0)
resRec.assign(NULL);
// resRec pointer is out-bounds of the TLV records memory
if ((resRec.getRecordBasePtr() - tlvDataBasePtr) < 0)
resRec.assign(NULL);
// resRec pointer is out-bounds of the TLV records memory
if (!resRec.isNull() && resRec.getRecordBasePtr() + resRec.getTotalSize() > tlvDataBasePtr + tlvDataLen)
resRec.assign(NULL);
return resRec;
}
/**
* Search for the first TLV record that corresponds to a given record type (the 'T' in __Type__-Length-Value)
* @param[in] recordType The record type to search for
* @param[in] tlvDataBasePtr A pointer to the TLV data byte stream
* @param[in] tlvDataLen The TLV data byte stream length
* @return An instance of type TLVRecordType that contains the result record. If record was not found a logical
* NULL instance of TLVRecordType will be returned, meaning TLVRecordType.isNull() will return true
*/
TLVRecordType getTLVRecord(uint32_t recordType, uint8_t* tlvDataBasePtr, size_t tlvDataLen) const
{
TLVRecordType curRec = getFirstTLVRecord(tlvDataBasePtr, tlvDataLen);
while (!curRec.isNull())
{
if (curRec.getType() == recordType)
{
return curRec;
}
curRec = getNextTLVRecord(curRec, tlvDataBasePtr, tlvDataLen);
}
curRec.assign(NULL);
return curRec; // for NRVO optimization
}
/**
* Get the TLV record count in a given TLV data byte stream. For efficiency purposes the count is being cached
* so only the first call to this method will go over all the TLV records, while all consequent calls will return
* the cached number. This implies that if there is a change in the number of records, it's the user's responsibility
* to call changeTLVRecordCount() with the record count change
* @param[in] tlvDataBasePtr A pointer to the TLV data byte stream
* @param[in] tlvDataLen The TLV data byte stream length
* @return The TLV record count
*/
size_t getTLVRecordCount(uint8_t* tlvDataBasePtr, size_t tlvDataLen) const
{
if (m_RecordCount != (size_t)-1)
return m_RecordCount;
m_RecordCount = 0;
TLVRecordType curRec = getFirstTLVRecord(tlvDataBasePtr, tlvDataLen);
while (!curRec.isNull())
{
m_RecordCount++;
curRec = getNextTLVRecord(curRec, tlvDataBasePtr, tlvDataLen);
}
return m_RecordCount;
}
/**
* As described in getTLVRecordCount(), the TLV record count is being cached for efficiency purposes. So if the
* number of TLV records change, it's the user's responsibility to call this method with the number of TLV records
* being added or removed. If records were added the change should be a positive number, or a negative number
* if records were removed
* @param[in] changedBy Number of records that were added or removed
*/
void changeTLVRecordCount(int changedBy) { if (m_RecordCount != (size_t)-1) m_RecordCount += changedBy; }
};
/**
* @class TLVRecordBuilder
* A base class for building Type-Length-Value (TLV) records. This builder receives the record parameters in its c'tor,
* builds the record raw buffer and provides a method to build a TLVRecord object out of it. Please notice this is
* a base class that lacks the capability of actually building TLVRecord objects and also cannot be instantiated. The
* reason for that is that different protocols build TLV records in different ways, so these missing capabilities will
* be implemented by the derived classes which are specific to each protocol. This class only provides the common
* infrastructure that will be used by them
*/
class TLVRecordBuilder
{
protected:
TLVRecordBuilder();
TLVRecordBuilder(uint32_t recType, const uint8_t* recValue, uint8_t recValueLen);
TLVRecordBuilder(uint32_t recType, uint8_t recValue);
TLVRecordBuilder(uint32_t recType, uint16_t recValue);
TLVRecordBuilder(uint32_t recType, uint32_t recValue);
TLVRecordBuilder(uint32_t recType, const IPv4Address& recValue);
TLVRecordBuilder(uint32_t recType, const std::string& recValue, bool valueIsHexString = false);
TLVRecordBuilder(const TLVRecordBuilder& other);
TLVRecordBuilder& operator=(const TLVRecordBuilder& other);
virtual ~TLVRecordBuilder();
void init(uint32_t recType, const uint8_t* recValue, size_t recValueLen);
uint8_t* m_RecValue;
size_t m_RecValueLen;
uint32_t m_RecType;
private:
void copyData(const TLVRecordBuilder& other);
};
}
#endif // PACKETPP_TLV_DATA