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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file
* This file includes definitions for generating and processing MLE TLVs.
*/
#ifndef TLVS_HPP_
#define TLVS_HPP_
#include "openthread-core-config.h"
#include <openthread/error.h>
#include <openthread/thread.h>
#include <openthread/platform/toolchain.h>
#include "common/encoding.hpp"
namespace ot {
using ot::Encoding::BigEndian::HostSwap16;
class Message;
/**
* This class implements TLV generation and parsing.
*
*/
OT_TOOL_PACKED_BEGIN
class Tlv
{
public:
/**
* Length values.
*
*/
enum
{
kBaseTlvMaxLength = OT_NETWORK_BASE_TLV_MAX_LENGTH, ///< The maximum length of the Base TLV format.
};
/**
* This method returns the Type value.
*
* @returns The Type value.
*
*/
uint8_t GetType(void) const { return mType; }
/**
* This method sets the Type value.
*
* @param[in] aType The Type value.
*
*/
void SetType(uint8_t aType) { mType = aType; }
/**
* This method indicates whether the TLV is an Extended TLV.
*
* @retval TRUE If the TLV is an Extended TLV.
* @retval FALSE If the TLV is not an Extended TLV.
*
*/
bool IsExtended(void) const { return (mLength == kExtendedLength); }
/**
* This method returns the Length value.
*
* @note This method should be used when TLV is not an Extended TLV, otherwise the returned length from this method
* would not be correct. When TLV is an Extended TLV, the TLV should be down-casted to the `ExtendedTlv` type and
* the `ExtendedTlv::GetLength()` should be used instead.
*
* @returns The Length value.
*
*/
uint8_t GetLength(void) const { return mLength; }
/**
* This method sets the Length value.
*
* @param[in] aLength The Length value.
*
*/
void SetLength(uint8_t aLength) { mLength = aLength; }
/**
* This method returns the TLV's total size (number of bytes) including Type, Length, and Value fields.
*
* This method correctly returns the TLV size independent of whether the TLV is an Extended TLV or not.
*
* @returns The total size include Type, Length, and Value fields.
*
*/
uint32_t GetSize(void) const;
/**
* This method returns a pointer to the Value.
*
* This method can be used independent of whether the TLV is an Extended TLV or not.
*
* @returns A pointer to the value.
*
*/
uint8_t *GetValue(void);
/**
* This method returns a pointer to the Value.
*
* This method can be used independent of whether the TLV is an Extended TLV or not.
*
* @returns A pointer to the value.
*
*/
const uint8_t *GetValue(void) const;
/**
* This method returns a pointer to the next TLV.
*
* This method correctly returns the next TLV independent of whether the current TLV is an Extended TLV or not.
*
* @returns A pointer to the next TLV.
*
*/
Tlv *GetNext(void) { return reinterpret_cast<Tlv *>(reinterpret_cast<uint8_t *>(this) + GetSize()); }
/**
* This method returns a pointer to the next TLV.
*
* This method correctly returns the next TLV independent of whether the current TLV is an Extended TLV or not.
*
* @returns A pointer to the next TLV.
*
*/
const Tlv *GetNext(void) const
{
return reinterpret_cast<const Tlv *>(reinterpret_cast<const uint8_t *>(this) + GetSize());
}
/**
* This method appends a TLV to the end of the message.
*
* On success, this method grows the message by the size of the TLV.
*
* @param[in] aMessage A reference to the message to append to.
*
* @retval OT_ERROR_NONE Successfully appended the TLV to the message.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message.
*
*/
otError AppendTo(Message &aMessage) const;
/**
* This static method reads a TLV from a message at a given offset with TLV's value as an `uint8_t`.
*
* @param[in] aMessage The message to read from.
* @param[in] aOffset The offset into the message pointing to the start of the TLV.
* @param[out] aValue A reference to a `uint8_t` to output the TLV's value.
*
* @retval OT_ERROR_NONE Successfully read the TLV and updated @p aValue.
* @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed.
*
*/
static otError ReadUint8Tlv(const Message &aMessage, uint16_t aOffset, uint8_t &aValue);
/**
* This static method reads a TLV from a message at a given offset with TLV's value as an `uint16_t`.
*
* @param[in] aMessage The message to read from.
* @param[in] aOffset The offset into the message pointing to the start of the TLV.
* @param[out] aValue A reference to a `uint16_t` to output the TLV's value.
*
* @retval OT_ERROR_NONE Successfully read the TLV and updated @p aValue.
* @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed.
*
*/
static otError ReadUint16Tlv(const Message &aMessage, uint16_t aOffset, uint16_t &aValue);
/**
* This static method reads a TLV from a message at a given offset with TLV's value as an `uint32_t`.
*
* @param[in] aMessage The message to read from.
* @param[in] aOffset The offset into the message pointing to the start of the TLV.
* @param[out] aValue A reference to a `uint32_t` to output the TLV's value.
*
* @retval OT_ERROR_NONE Successfully read the TLV and updated @p aValue.
* @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed.
*
*/
static otError ReadUint32Tlv(const Message &aMessage, uint16_t aOffset, uint32_t &aValue);
/**
* This static method reads a TLV in a message at a given offset expecting a minimum length for the value.
*
* @param[in] aMessage The message to read from.
* @param[in] aOffset The offset into the message pointing to the start of the TLV.
* @param[out] aValue A buffer to output the TLV's value, must contain (at least) @p aMinLength bytes.
* @param[in] aMinLength The minimum expected length of TLV and number of bytes to copy into @p aValue buffer.
*
* @retval OT_ERROR_NONE Successfully read the TLV and copied @p aMinLength into @p aValue.
* @retval OT_ERROR_PARSE The TLV was not well-formed and could not be parsed.
*
*/
static otError ReadTlv(const Message &aMessage, uint16_t aOffset, void *aValue, uint8_t aMinLength);
/**
* This static method reads the requested TLV out of @p aMessage.
*
* This method can be used independent of whether the read TLV (from message) is an Extended TLV or not.
*
* @param[in] aMessage A reference to the message.
* @param[in] aType The Type value to search for.
* @param[in] aMaxSize Maximum number of bytes to read.
* @param[out] aTlv A reference to the TLV that will be copied to.
*
* @retval OT_ERROR_NONE Successfully copied the TLV.
* @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType.
*
*/
static otError FindTlv(const Message &aMessage, uint8_t aType, uint16_t aMaxSize, Tlv &aTlv);
/**
* This static method obtains the offset of a TLV within @p aMessage.
*
* This method can be used independent of whether the read TLV (from message) is an Extended TLV or not.
*
* @param[in] aMessage A reference to the message.
* @param[in] aType The Type value to search for.
* @param[out] aOffset A reference to the offset of the TLV.
*
* @retval OT_ERROR_NONE Successfully copied the TLV.
* @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType.
*
*/
static otError FindTlvOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset);
/**
* This static method finds the offset and length of a given TLV type.
*
* This method can be used independent of whether the read TLV (from message) is an Extended TLV or not.
*
* @param[in] aMessage A reference to the message.
* @param[in] aType The Type value to search for.
* @param[out] aOffset The offset where the value starts.
* @param[out] aLength The length of the value.
*
* @retval OT_ERROR_NONE Successfully found the TLV.
* @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType.
*
*/
static otError FindTlvValueOffset(const Message &aMessage, uint8_t aType, uint16_t &aOffset, uint16_t &aLength);
/**
* This static method searches for a TLV with a given type in a message and reads its value as an `uint8_t`.
*
* @param[in] aMessage A reference to the message.
* @param[in] aType The TLV type to search for.
* @param[out] aValue A reference to a `uint8_t` to output the TLV's value.
*
* @retval OT_ERROR_NONE Successfully found the TLV and updated @p aValue.
* @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType.
* @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed.
*
*/
static otError FindUint8Tlv(const Message &aMessage, uint8_t aType, uint8_t &aValue);
/**
* This static method searches for a TLV with a given type in a message and reads its value as an `uint16_t`.
*
* @param[in] aMessage A reference to the message.
* @param[in] aType The TLV type to search for.
* @param[out] aValue A reference to a `uint16_t` to output the TLV's value.
*
* @retval OT_ERROR_NONE Successfully found the TLV and updated @p aValue.
* @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType.
* @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed.
*
*/
static otError FindUint16Tlv(const Message &aMessage, uint8_t aType, uint16_t &aValue);
/**
* This static method searches for a TLV with a given type in a message and reads its value as an `uint32_t`.
*
* @param[in] aMessage A reference to the message.
* @param[in] aType The TLV type to search for.
* @param[out] aValue A reference to a `uint32_t` to output the TLV's value.
*
* @retval OT_ERROR_NONE Successfully found the TLV and updated @p aValue.
* @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType.
* @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed.
*
*/
static otError FindUint32Tlv(const Message &aMessage, uint8_t aType, uint32_t &aValue);
/**
* This static method searches for a TLV with a given type in a message, ensures its length is same or larger than
* an expected minimum value, and then reads its value into a given buffer.
*
* If the TLV length is smaller than the minimum length @p aLength, the TLV is considered invalid. In this case,
* this method returns `OT_ERROR_PARSE` and the @p aValue buffer is not updated.
*
* If the TLV length is larger than @p aLength, the TLV is considered valid, but only the first @p aLength bytes
* of the value are read and copied into the @p aValue buffer.
*
* @param[in] aMessage A reference to the message.
* @param[in] aType The TLV type to search for.
* @param[out] aValue A buffer to output the value (must contain at least @p aLength bytes).
* @param[in] aLength The expected (minimum) length of the TLV value.
*
* @retval OT_ERROR_NONE The TLV was found and read successfully. @p aValue is updated.
* @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType.
* @retval OT_ERROR_PARSE TLV was found but it was not well-formed and could not be parsed.
*
*/
static otError FindTlv(const Message &aMessage, uint8_t aType, void *aValue, uint8_t aLength);
/**
* This static method appends a simple TLV with a given type and an `uint8_t` value to a message.
*
* On success this method grows the message by the size of the TLV.
*
* @param[in] aMessage A reference to the message to append to.
* @param[in] aType The TLV type.
* @param[in] aValue The TLV value (`uint8_t`).
*
* @retval OT_ERROR_NONE Successfully appended the TLV to the message.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message.
*
*/
static otError AppendUint8Tlv(Message &aMessage, uint8_t aType, uint8_t aValue);
/**
* This static method appends a simple TLV with a given type and an `uint16_t` value to a message.
*
* On success this method grows the message by the size of the TLV.
*
* @param[in] aMessage A reference to the message to append to.
* @param[in] aType The TLV type.
* @param[in] aValue The TLV value (`uint16_t`).
*
* @retval OT_ERROR_NONE Successfully appended the TLV to the message.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message.
*
*/
static otError AppendUint16Tlv(Message &aMessage, uint8_t aType, uint16_t aValue);
/**
* This static method appends a (simple) TLV with a given type and an `uint32_t` value to a message.
*
* On success this method grows the message by the size of the TLV.
*
* @param[in] aMessage A reference to the message to append to.
* @param[in] aType The TLV type.
* @param[in] aValue The TLV value (`uint32_t`).
*
* @retval OT_ERROR_NONE Successfully appended the TLV to the message.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message.
*
*/
static otError AppendUint32Tlv(Message &aMessage, uint8_t aType, uint32_t aValue);
/**
* This static method appends a TLV with a given type and value to a message.
*
* On success this method grows the message by the size of the TLV.
*
* @param[in] aMessage A reference to the message to append to.
* @param[in] aType The TLV type.
* @param[in] aValue A buffer containing the TLV value.
* @param[in] aLength The value length (in bytes).
*
* @retval OT_ERROR_NONE Successfully appended the TLV to the message.
* @retval OT_ERROR_NO_BUFS Insufficient available buffers to grow the message.
*
*/
static otError AppendTlv(Message &aMessage, uint8_t aType, const void *aValue, uint8_t aLength);
protected:
enum
{
kExtendedLength = 255, ///< Extended Length value
};
private:
/**
* This private static method searches within a given message for TLV type and outputs the TLV offset, size and
* whether it is an Extended TLV.
*
* A NULL pointer can be used for output parameters @p aOffset, @p aSize, or @p aIsExtendedTlv if the parameter
* is not required.
*
* @param[in] aMessage A reference to the message to search within.
* @param[in] aType The TLV type to search for.
* @param[out] aOffset A pointer to a variable to output the offset to the start of the TLV.
* @param[out] aSize A pointer to a variable to output the size (total number of bytes) of the TLV.
* @param[out] aIsExtendedTlv A pointer to a boolean variable to output whether the found TLV is extended or not.
*
* @retval OT_ERROR_NONE Successfully found the TLV.
* @retval OT_ERROR_NOT_FOUND Could not find the TLV with Type @p aType.
*
*/
static otError Find(const Message &aMessage,
uint8_t aType,
uint16_t * aOffset,
uint16_t * aSize,
bool * aIsExtendedTlv);
uint8_t mType;
uint8_t mLength;
} OT_TOOL_PACKED_END;
OT_TOOL_PACKED_BEGIN
class ExtendedTlv : public Tlv
{
public:
/**
* This method returns the Length value.
*
*/
uint16_t GetLength(void) const { return HostSwap16(mLength); }
/**
* This method sets the Length value.
*
* @param[in] aLength The Length value.
*
*/
void SetLength(uint16_t aLength)
{
Tlv::SetLength(kExtendedLength);
mLength = HostSwap16(aLength);
}
private:
uint16_t mLength;
} OT_TOOL_PACKED_END;
} // namespace ot
#endif // TLVS_HPP_