forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlvstream.c
344 lines (295 loc) · 8.12 KB
/
tlvstream.c
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
#include "config.h"
#include <ccan/asort/asort.h>
#include <common/bigsize.h>
#include <wire/tlvstream.h>
#include <wire/wire.h>
#ifndef SUPERVERBOSE
#define SUPERVERBOSE(...)
#endif
/* This simply needs to be an address which is neither NULL nor a
* tal_arr return */
static const u64 dummy;
const u64 *FROMWIRE_TLV_ANY_TYPE = &dummy;
static int tlv_field_cmp(const struct tlv_field *a, const struct tlv_field *b,
void *x)
{
return a->numtype > b->numtype ? 1 : -1;
}
void towire_tlvstream_raw(u8 **pptr, struct tlv_field *fields)
{
if (!fields)
return;
asort(fields, tal_count(fields), tlv_field_cmp, NULL);
for (size_t i = 0; i < tal_count(fields); i++) {
const struct tlv_field *field = &fields[i];
/* BOLT #1:
*
* The sending node:
...
* - MUST minimally encode `type` and `length`.
*/
towire_bigsize(pptr, field->numtype);
towire_bigsize(pptr, field->length);
towire(pptr, field->value, field->length);
}
}
static struct tlv_field *tlvstream_get_raw(struct tlv_field *stream, u64 type)
{
for (size_t i=0; i<tal_count(stream); i++)
if (stream[i].numtype == type)
return &stream[i];
return NULL;
}
void tlvstream_set_raw(struct tlv_field **stream, u64 type, const void *value TAKES, size_t valuelen)
{
struct tlv_field f, *e = tlvstream_get_raw(*stream, type);
if (e != NULL) {
tal_free(e->value);
e->length = valuelen;
e->value = tal_dup_arr(*stream, u8, value, e->length, 0);
} else {
/* If we haven't found it insert it insead. */
f.length = valuelen;
f.numtype = type;
f.value = tal_dup_arr(*stream, u8, value, f.length, 0);
tal_arr_expand(stream, f);
}
}
void tlvstream_set_short_channel_id(struct tlv_field **stream, u64 type,
struct short_channel_id *value)
{
u8 *ser = tal_arr(NULL, u8, 0);
towire_short_channel_id(&ser, value);
tlvstream_set_raw(stream, type, take(ser), tal_bytelen(ser));
}
void tlvstream_set_tu64(struct tlv_field **stream, u64 type, u64 value)
{
u8 *ser = tal_arr(NULL, u8, 0);
towire_tu64(&ser, value);
tlvstream_set_raw(stream, type, take(ser), tal_bytelen(ser));
}
void tlvstream_set_tu32(struct tlv_field **stream, u64 type, u32 value)
{
u8 *ser = tal_arr(NULL, u8, 0);
towire_tu64(&ser, value);
tlvstream_set_raw(stream, type, take(ser), tal_bytelen(ser));
}
/* Get the offset of this field: returns size of msg if not found (or
* tlv malformed) */
size_t tlv_field_offset(const u8 *tlvstream, size_t tlvlen, u64 fieldtype)
{
size_t max = tlvlen;
while (max > 0) {
u64 type, length;
size_t field_off = tlvlen - max;
type = fromwire_bigsize(&tlvstream, &max);
length = fromwire_bigsize(&tlvstream, &max);
if (!tlvstream)
break;
/* Found it! */
if (type == fieldtype)
return field_off;
if (length > max)
break;
max -= length;
tlvstream += length;
}
return tlvlen;
}
static bool tlv_type_is_allowed(const struct tlv_field *f,
const u64 *extra_types)
{
/* Simple case: it's an odd field. */
if (f->numtype % 2 != 0)
return true;
if (extra_types == FROMWIRE_TLV_ANY_TYPE)
return true;
/* Now iterate through the extras and see if we should make an
* exception. */
for (size_t i = 0; i < tal_count(extra_types); i++)
if (extra_types[i] == f->numtype)
return true;
return false;
}
/* Update err_off to point to current offset. */
static void update_err_off(size_t *err_off, size_t initial_len, size_t max)
{
if (err_off)
*err_off = initial_len - max;
}
bool fromwire_tlv(const u8 **cursor, size_t *max,
const struct tlv_record_type *types, size_t num_types,
void *record, struct tlv_field **fields,
const u64 *extra_types,
size_t *err_off, u64 *err_type)
{
bool first = true;
u64 prev_type = 0;
size_t initial_len = *max;
update_err_off(err_off, initial_len, *max);
while (*max > 0) {
struct tlv_field field;
/* BOLT #1:
*
* The `type` is encoded using the BigSize format.
*/
field.numtype = fromwire_bigsize(cursor, max);
/* BOLT #1:
* - if a `type` or `length` is not minimally encoded:
* - MUST fail to parse the `tlv_stream`.
*/
if (!*cursor) {
SUPERVERBOSE("type");
if (err_type)
*err_type = 0;
goto fail;
}
/* BOLT #1:
* - if decoded `type`s are not strictly-increasing
* (including situations when two or more occurrences
* of the same `type` are met):
* - MUST fail to parse the `tlv_stream`.
*/
if (!first && field.numtype <= prev_type) {
if (field.numtype == prev_type)
SUPERVERBOSE("duplicate tlv type");
else
SUPERVERBOSE("invalid ordering");
if (err_type)
*err_type = field.numtype;
goto fail;
}
first = false;
prev_type = field.numtype;
/* BOLT #1:
* - if `type` is known:
* - MUST decode the next `length` bytes using the known
* encoding for `type`.
*/
field.meta = NULL;
for (size_t i = 0; i < num_types; i++) {
if (types[i].type == field.numtype) {
field.meta = &types[i];
break;
}
}
if (!field.meta && !tlv_type_is_allowed(&field, extra_types)) {
SUPERVERBOSE("unknown even");
if (err_type != NULL)
*err_type = field.numtype;
goto fail;
}
/* We're happy with type field. Move on. */
update_err_off(err_off, initial_len, *max);
field.length = fromwire_bigsize(cursor, max);
/* BOLT #1:
* - if a `type` or `length` is not minimally encoded:
* - MUST fail to parse the `tlv_stream`.
*/
if (!*cursor) {
SUPERVERBOSE("length");
if (err_type)
*err_type = field.numtype;
goto fail;
}
/* BOLT #1:
* - if `length` exceeds the number of bytes remaining in the
* message:
* - MUST fail to parse the `tlv_stream`.
*/
if (field.length > *max) {
SUPERVERBOSE("value");
if (err_type)
*err_type = field.numtype;
goto fail;
}
/* We're happy with length field. Move on. */
update_err_off(err_off, initial_len, *max);
field.value = tal_dup_arr(record, u8, *cursor, field.length, 0);
if (field.meta) {
/* Length of message can't exceed 16 bits anyway. */
size_t tlvlen = field.length;
/* We're happy with type field. Move on. */
update_err_off(err_off, initial_len, *max);
/* FIXME: We could add an err_off in here for more accuracy. */
field.meta->fromwire(cursor, &tlvlen, record);
if (!*cursor) {
if (err_type != NULL)
*err_type = field.numtype;
goto fail;
}
/* BOLT #1:
* - if `length` is not exactly equal to that required
* for the known encoding for `type`:
* - MUST fail to parse the `tlv_stream`.
*/
if (tlvlen != 0) {
if (err_type != NULL)
*err_type = field.numtype;
SUPERVERBOSE("greater than encoding length");
goto fail;
}
} else {
/* We're happy with type field. Move on. */
update_err_off(err_off, initial_len, *max);
/* We didn't read from *cursor through a fromwire, so
* update manually. */
*cursor += field.length;
}
/* We've read bytes in ->fromwire, so update max */
*max -= field.length;
/* We're happy with contents. Move on. */
update_err_off(err_off, initial_len, *max);
tal_arr_expand(fields, field);
}
return true;
fail:
fromwire_fail(cursor, max);
return false;
}
void towire_tlv(u8 **pptr,
const struct tlv_record_type *types, size_t num_types,
const void *record)
{
if (!record)
return;
for (size_t i = 0; i < num_types; i++) {
u8 *val;
if (i != 0)
assert(types[i].type > types[i-1].type);
val = types[i].towire(NULL, record);
if (!val)
continue;
/* BOLT #1:
*
* The sending node:
...
* - MUST minimally encode `type` and `length`.
*/
towire_bigsize(pptr, types[i].type);
towire_bigsize(pptr, tal_bytelen(val));
towire(pptr, val, tal_bytelen(val));
tal_free(val);
}
}
struct tlv_field *tlv_make_fields_(const struct tlv_record_type *types,
size_t num_types,
const void *record)
{
struct tlv_field *fields = tal_arr(record, struct tlv_field, 0);
for (size_t i = 0; i < num_types; i++) {
struct tlv_field f;
u8 *val;
if (i != 0)
assert(types[i].type > types[i-1].type);
val = types[i].towire(NULL, record);
if (!val)
continue;
f.meta = &types[i];
f.numtype = types[i].type;
f.length = tal_bytelen(val);
f.value = tal_steal(fields, val);
tal_arr_expand(&fields, f);
}
return fields;
}