The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
struct.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Functions to encode / decode structures on the wire
18 *
19 * @file src/lib/util/struct.c
20 *
21 * @copyright 2018 The FreeRADIUS server project
22 * @copyright 2018 Alan DeKok (aland@freeradius.org)
23 */
24RCSID("$Id: 9e932265b822887e6b4a6af133a5634fc3bf1cfb $")
25
26#include <freeradius-devel/util/struct.h>
27#include <freeradius-devel/io/pair.h>
28
29/** Convert a STRUCT to one or more VPs
30 *
31 */
33 fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len,
34 void *decode_ctx,
36{
37 unsigned int child_num;
38 uint8_t const *p = data, *end = data + data_len;
39 fr_dict_attr_t const *child, *substruct_da;
40 fr_pair_list_t child_list_head;
41 fr_pair_list_t *child_list;
42 fr_pair_t *vp, *key_vp, *struct_vp = NULL;
43 unsigned int offset = 0;
44 TALLOC_CTX *child_ctx;
45 ssize_t slen;
46 size_t child_length;
47
48 if (data_len == 0) {
49 fr_strerror_const("struct decoder was passed zero bytes of data");
50 return -1; /* at least one byte of data */
51 }
52
53 FR_PROTO_TRACE("Decoding struct %s", parent->name);
54 FR_PROTO_HEX_DUMP(data, data_len, "fr_struct_from_network");
55
56 /*
57 * Start a child list.
58 */
60
61 struct_vp = fr_pair_afrom_da(ctx, parent);
62 if (!struct_vp) {
63 return PAIR_DECODE_OOM;
64 }
65
66 fr_pair_list_init(&child_list_head); /* still used elsewhere */
67 child_list = &struct_vp->vp_group;
68 child_ctx = struct_vp;
69 key_vp = NULL;
70
71 /*
72 * Simplify the code by having a generic decode routine.
73 */
75
76 /*
77 * Decode structs with length prefixes.
78 */
80 size_t claimed_len, field_len, calc_len;
81
82 /*
83 * Set how many bytes there are in the "length" field.
84 */
86 field_len = 1;
87 } else {
89 field_len = 2;
90 }
91
92 if ((size_t) (end - p) < field_len) {
93 FR_PROTO_TRACE("Insufficient room for length field");
94
95 invalid_struct:
96 /*
97 * Some field could not be decoded. Nuke the entire struct, and just make the
98 * whole thing "raw".
99 */
100 TALLOC_FREE(struct_vp);
101
102 slen = fr_pair_raw_from_network(ctx, out, parent, data, data_len);
103 if (slen < 0) return slen;
104 return data_len;
105 }
106
107 claimed_len = p[0];
108 if (field_len > 1) {
109 claimed_len <<= 8;
110 claimed_len |= p[1];
111 }
112 p += field_len;
113
114 if (claimed_len < da_length_offset(parent)) {
115 FR_PROTO_TRACE("Length header (%zu) is smaller than minimum value (%u)",
116 claimed_len, parent->flags.type_size);
117 goto invalid_struct;
118 }
119
120 /*
121 * Get the calculated length of the actual data.
122 */
123 calc_len = claimed_len - da_length_offset(parent);
124
125 if (calc_len > (size_t) (end - p)) {
126 FR_PROTO_TRACE("Length header (%zu) is larger than remaining data (%zu)",
127 claimed_len + field_len, (size_t) (end - p));
128 goto invalid_struct;
129 }
130
131 /*
132 * Limit the size of the decoded structure to the correct length.
133 */
134 data_len = calc_len;
135 end = p + data_len;
136 }
137
138 /*
139 * @todo - If the struct is truncated on a MEMBER boundary, we silently omit
140 * the trailing members. Maybe this should be an error?
141 */
142 for (child_num = 1;
143 (p < end) && (child = fr_dict_attr_child_by_num(parent, child_num)) != NULL;
144 child_num++) {
145 FR_PROTO_TRACE("Decoding struct %s child %s (%u)", parent->name, child->name, child->attr);
146 FR_PROTO_HEX_DUMP(p, (end - p), "fr_struct_from_network - remaining %zu", (size_t) (end - p));
147
148 /*
149 * Check for bit fields.
150 */
151 if (da_is_bit_field(child)) {
152 uint8_t array[8];
153 unsigned int num_bits;
154 uint64_t value;
155
156 num_bits = offset + child->flags.length;
157 if ((size_t)(end - p) < fr_bytes_from_bits(num_bits)) {
158 FR_PROTO_TRACE("not enough data for bit decoder?");
159 goto remainder_raw;
160 }
161
162 memset(array, 0, sizeof(array));
163 memcpy(&array[0], p, fr_bytes_from_bits(num_bits));
164
165 if (offset > 0) array[0] &= (1 << (8 - offset)) - 1; /* mask off bits we don't care about */
166
167 memcpy(&value, &array[0], sizeof(value));
168 value = htonll(value);
169 value >>= (8 - offset); /* move it to the lower bits */
170 value >>= (56 - child->flags.length);
171
172 vp = fr_pair_afrom_da(child_ctx, child);
173 if (!vp) {
174 FR_PROTO_TRACE("fr_struct_from_network - failed allocating child VP");
175 oom:
176 talloc_free(struct_vp);
177 return PAIR_DECODE_OOM;
178 }
179
180 switch (child->type) {
181 case FR_TYPE_BOOL:
182 vp->vp_bool = value;
183 break;
184
185 case FR_TYPE_UINT8:
186 vp->vp_uint8 = value;
187 break;
188
189 case FR_TYPE_UINT16:
190 vp->vp_uint16 = value;
191 break;
192
193 case FR_TYPE_UINT32:
194 vp->vp_uint32 = (uint32_t) value; /* uint32 bit fields are at most 32 bits */
195 break;
196
197 case FR_TYPE_UINT64:
198 vp->vp_uint64 = value;
199 break;
200
201 default:
202 FR_PROTO_TRACE("Can't decode unknown type?");
203 goto remainder_raw;
204 }
205
206 vp->vp_tainted = true;
207 FR_PAIR_APPEND(child_list, vp);
208
209 p += (num_bits >> 3); /* go to the LAST bit, not the byte AFTER the last bit */
210 offset = num_bits & 0x07;
211 continue;
212 }
213
214 fr_assert(offset == 0);
215 offset = 0; /* reset for non-bit-field attributes */
216
217 /*
218 * The child is either unknown width, OR known width with a length that is too large for
219 * the "length" field, OR is known width via some kind of protocol-specific length header.
220 */
221 if (!child->flags.length || child->flags.array) {
222 child_length = end - p;
223
224 } else {
225 child_length = child->flags.length;
226
227 /*
228 * If this field overflows the input, then *all*
229 * of the input is suspect.
230 */
231 if (child_length > (size_t) (end - p)) {
232 child_length = (size_t) (end - p);
233 }
234 }
235
236 /*
237 * We only allow a limited number of data types
238 * inside of a struct.
239 */
240 switch (child->type) {
241 case FR_TYPE_INTERNAL:
242 case FR_TYPE_NULL:
243 FR_PROTO_TRACE("fr_struct_from_network - unknown child type");
244 goto remainder_raw;
245
246 case FR_TYPE_STRUCT:
247 case FR_TYPE_VSA:
248 case FR_TYPE_VENDOR:
249 case FR_TYPE_GROUP:
250 case FR_TYPE_LEAF:
251 break;
252
253 /*
254 * Decode child TLVs, according to the parent attribute.
255 */
256 case FR_TYPE_TLV:
257 fr_assert(!key_vp);
258
259 if (!decode_tlv) {
260 fr_strerror_const("Decoding TLVs requires a decode_tlv() function to be passed");
261 talloc_free(struct_vp);
262 return -(p - data);
263 }
264
265 /*
266 * Decode all of the remaining data as
267 * TLVs. Any malformed TLVs are appended
268 * as raw VP.
269 */
270 while (p < end) {
271 slen = decode_tlv(child_ctx, child_list, child, p, end - p, decode_ctx);
272 if (slen < 0) {
273 FR_PROTO_TRACE("failed decoding TLV?");
274 goto remainder_raw;
275 }
276 p += slen;
277 }
278
279 goto done;
280
281 /*
282 * The child is a union, it MUST be at the end of
283 * the struct, and we must have seen a key before
284 * we reach the union. See dict_tokenize.
285 */
286 case FR_TYPE_UNION:
287 /*
288 * Create the union wrapper, and reset the child_ctx and child_list to it.
289 */
290 vp = fr_pair_afrom_da(child_ctx, child);
291 if (!vp) goto oom;
292
293 FR_PAIR_APPEND(child_list, vp);
294 substruct_da = child;
295 child_ctx = vp;
296 child_list = &vp->vp_group;
297
298 fr_assert(!fr_dict_attr_child_by_num(parent, child_num + 1)); /* has to be the last one */
299 if (!key_vp) {
300 remainder_raw:
301 child_length = (size_t) (end - p);
302 goto raw;
303 }
304
305 goto substruct;
306 }
307
308 /*
309 * Magic values get the callback called.
310 *
311 * @todo - if this is an array of DNS labels, we
312 * need to do decompression checks on the entire
313 * block, and then decode each field
314 * individually.
315 */
316 if (child->flags.array) {
317 slen = fr_pair_array_from_network(child_ctx, child_list, child, p, child_length, decode_ctx, decode_value);
318 } else {
319 slen = decode_value(child_ctx, child_list, child, p, child_length, decode_ctx);
320 }
321 if (slen < 0) {
322 FR_PROTO_TRACE("Failed decoding value");
323
324 raw:
325 slen = fr_pair_raw_from_network(child_ctx, child_list, child, p, child_length);
326 if (slen < 0) {
327 talloc_free(struct_vp);
328 return slen;
329 }
330 }
331
332 p += slen; /* not always the same as child->flags.length */
333
334 if (fr_dict_attr_is_key_field(child)) {
335 fr_assert(!key_vp);
336 key_vp = fr_pair_list_tail(child_list);
337 }
338 }
339
340 /*
341 * Is there a substructure after this one? If so, go
342 * decode it.
343 */
344 if (key_vp) {
345 fr_dict_enum_value_t const *enumv;
346
347 substruct_da = key_vp->da;
348
349 substruct:
350 child = NULL;
351
352 FR_PROTO_TRACE("Key %s", key_vp->da->name);
353 FR_PROTO_HEX_DUMP(p, (end - p), "fr_struct_from_network - child structure");
354
355 /*
356 * Nothing more to decode, don't decode it.
357 */
358 if (p >= end) {
359 FR_PROTO_TRACE("Expected substruct, but there is none. We're done decoding this structure");
360 goto done;
361 }
362
363 enumv = fr_dict_enum_by_value(key_vp->da, &key_vp->data);
364 if (enumv) child = fr_dict_enum_attr_ref(enumv);
365
366 if (!child) {
367 /*
368 * Always encode the unknown child as attribute number 0. Since the unknown
369 * children have no "real" number, and are all unique da's, they are
370 * incomparable. And thus can all be given the same number.
371 */
372 uint64_t attr;
373
374 FR_PROTO_TRACE("No matching child structure found");
375
376 unknown_child:
377 attr = 0;
378
379 /*
380 * But if we have a key field, the unknown attribute number is taken from the
381 * from the key field.
382 */
383 if (fr_type_is_integer(key_vp->vp_type)) {
384 attr = fr_value_box_as_uint64(&key_vp->data);
385 if (attr > UINT32_MAX) attr = 0;
386 }
387
388 child = fr_dict_attr_unknown_raw_afrom_num(child_ctx, substruct_da, (unsigned int) attr);
389 if (!child) {
390 FR_PROTO_TRACE("failed allocating unknown child for key VP %s - %s",
391 key_vp->da->name, fr_strerror());
392 goto oom;
393 }
394
395 slen = fr_pair_raw_from_network(child_ctx, child_list, child, p, end - p);
396 if (slen < 0) {
397 FR_PROTO_TRACE("Failed creating raw VP from malformed or unknown substruct for child %s", child->name);
399 return slen;
400 }
401
402 p = end;
403
404 } else {
405 switch (child->type) {
406 case FR_TYPE_STRUCT:
407 FR_PROTO_TRACE("Decoding child structure %s", child->name);
408 slen = fr_struct_from_network(child_ctx, child_list, child, p, end - p,
409 decode_ctx, decode_value, decode_tlv);
410 break;
411
412 case FR_TYPE_TLV:
413 if (!decode_tlv) {
414 FR_PROTO_TRACE("Failed to pass decode_tlv() for child tlv %s", child->name);
415 goto unknown_child;
416 }
417
418 FR_PROTO_TRACE("Decoding child tlv %s", child->name);
419
420 slen = decode_tlv(child_ctx, child_list, child, p, end - p, decode_ctx);
421 break;
422
423 case FR_TYPE_LEAF:
425
426 FR_PROTO_TRACE("Decoding child %s", child->name);
427
428 /*
429 * @todo - unify this code with the code above, but for now copying is
430 * easier.
431 */
432
433 /*
434 * The child is either unknown width, OR known width with a length that is too large for
435 * the "length" field, OR is known width via some kind of protocol-specific length header.
436 */
437 if (!child->flags.length || child->flags.array) {
438 child_length = end - p;
439
440 } else {
441 child_length = child->flags.length;
442
443 /*
444 * If this field overflows the input, then *all*
445 * of the input is suspect.
446 */
447 if (child_length > (size_t) (end - p)) {
448 FR_PROTO_TRACE("fr_struct_from_network - child length %zu overflows buffer", child_length);
449 goto remainder_raw;
450 }
451 }
452
453 if (child->flags.array) {
454 slen = fr_pair_array_from_network(child_ctx, child_list, child, p, child_length, decode_ctx, decode_value);
455 } else {
456 slen = decode_value(child_ctx, child_list, child, p, child_length, decode_ctx);
457 }
458 break;
459
460 default:
461 FR_PROTO_TRACE("Unknown data type %s in child %s", fr_type_to_str(child->type), child->name);
462 goto unknown_child;
463 }
464
465 if (slen <= 0) {
466 FR_PROTO_TRACE("failed decoding child %s", child->name);
467 goto unknown_child;
468 }
469 p += slen;
470 }
471
473 }
474
475done:
476 fr_assert(struct_vp != NULL);
477 FR_PAIR_APPEND(out, struct_vp);
478
479 FR_PROTO_TRACE("used %zu bytes", data_len);
480 return p - data;
481}
482
483
484/** Put bits into an output dbuff
485 *
486 * @param dbuff where the bytes go
487 * @param p where leftover bits go
488 * @param start_bit start bit in the dbuff where the data goes, 0..7
489 * @param num_bits number of bits to write to the output, 0..55
490 * @param data data to write, all in the lower "num_bits" of the uint64_t variable
491 * @return
492 * >= 0 the next value to pass in for start_bit
493 * < 0 no space or invalid start_bit or num_bits parameter
494 */
495static int put_bits_dbuff(fr_dbuff_t *dbuff, uint8_t *p, int start_bit, uint8_t num_bits, uint64_t data)
496{
497 uint64_t used_bits;
498
499 if (start_bit < 0 || start_bit > 7) return -1;
500 if (num_bits < 1 || num_bits > 56) return -1;
501
502 /* Get bits buffered in *p */
503 used_bits = *p & (-256 >> start_bit);
504
505 /* Mask out all but the least significant num_bits bits of data */
506 data &= (((uint64_t) 1) << num_bits) - 1;
507
508 /* Move it towards the most significant end and put used_bits at the top */
509 data <<= (64 - (start_bit + num_bits));
510 data |= used_bits << 56;
511
512 data = htonll(data);
513
514 start_bit += num_bits;
515 if (start_bit > 7) {
516 ssize_t slen;
517
518 /*
519 * At most 7 bytes are copied, which bounds a
520 * "need more space" return to -7.
521 */
522 slen = fr_dbuff_in_memcpy(dbuff, (uint8_t const *) &data, (size_t)(start_bit / 8));
523 if (slen < 0) return (int)slen;
524 }
525
526 *p = ((uint8_t *) &data)[start_bit / 8];
527 return start_bit % 8;
528}
529
530static fr_cmp_ret_t pair_sort_increasing(void const *a, void const *b)
531{
532 fr_pair_t const *my_a = a;
533 fr_pair_t const *my_b = b;
534 fr_cmp_ret_t ret;
535
536 /*
537 * Deeper attributes come later in the list.
538 */
539 ret = CMP_PREFER_SMALLER(my_a->da->depth, my_b->da->depth);
540 if (ret != 0) return ret;
541
542 return CMP_PREFER_SMALLER(my_a->da->attr, my_b->da->attr);
543}
544
545static void *struct_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
546{
547 fr_pair_t *c = current;
548 fr_dict_attr_t *parent = talloc_get_type_abort(uctx, fr_dict_attr_t);
549
550 while ((c = fr_dcursor_list_next(cursor, c))) {
551 PAIR_VERIFY(c);
552
553 if (c->da->dict != parent->dict || c->da->flags.internal) continue;
554 break;
555 }
556
557 return c;
558}
559
560static ssize_t encode_tlv(fr_dbuff_t *dbuff, fr_dict_attr_t const *tlv,
561 fr_da_stack_t *da_stack, unsigned int depth,
562 fr_dcursor_t *cursor, void *encode_ctx,
564
565{
566 fr_pair_t *vp;
567 fr_dcursor_t child_cursor;
568 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
569
570 if (!encode_pair) {
571 fr_strerror_printf("Asked to encode child attribute %s, but we were not passed an encoding function",
572 tlv->name);
574 }
575
576 vp = fr_dcursor_current(cursor);
577 if (!vp || (vp->da != tlv)) return 0;
578
579 vp = fr_pair_dcursor_init(&child_cursor, &vp->vp_group);
580 if (vp) {
581 ssize_t slen;
582
583 FR_PROTO_TRACE("fr_struct_to_network trailing TLVs of %s", tlv->name);
584 fr_proto_da_stack_build(da_stack, vp->da);
585 FR_PROTO_STACK_PRINT(da_stack, depth);
586
587 slen = fr_pair_cursor_to_network(&work_dbuff, da_stack, depth + 1, &child_cursor, encode_ctx, encode_pair);
588 if (slen < 0) return slen;
589 }
590
591 return fr_dbuff_set(dbuff, &work_dbuff);
592}
593
594static ssize_t encode_union(fr_dbuff_t *dbuff, fr_dict_attr_t const *wrapper,
595 fr_dict_attr_t const *key_da, fr_pair_t const *key_vp, fr_dbuff_marker_t *key_m,
596 fr_da_stack_t *da_stack, unsigned int depth,
597 fr_dcursor_t *cursor, void *encode_ctx,
599
600{
601 ssize_t slen;
602 fr_pair_t *parent, *child, *found = NULL;
603 fr_dict_attr_t const *child_ref;
604 fr_dcursor_t child_cursor;
605 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
606
607 parent = fr_dcursor_current(cursor);
608 if (!parent || (parent->da != wrapper)) return 0;
609
610 fr_assert(key_vp); /* @todo */
611
612 child = fr_pair_dcursor_init(&child_cursor, &parent->vp_group);
613 if (!child) {
614 /*
615 * @todo - do we want to skip encoding the entire parent structure?
616 */
617 FR_PROTO_TRACE("fr_struct_to_network union %s has no children", key_da->name);
618 return 0;
619 }
620
621 /*
622 * There's a key VP, we find the matching child struct, and then set the cursor to encode just
623 * that child.
624 */
625 if (key_vp) {
626 fr_dict_enum_value_t const *enumv;
627
628 enumv = fr_dict_enum_by_value(key_da, &key_vp->data);
629 if (enumv && ((child_ref = fr_dict_enum_attr_ref(enumv)) != NULL)) {
630 found = fr_pair_find_by_da(&parent->vp_group, NULL, child_ref);
631 if (found) {
632 (void) fr_dcursor_set_current(&child_cursor, found);
633 }
634 }
635 }
636
637 /*
638 * @todo - encode the key field based on the attribute number?
639 *
640 * However, we are likely better off just not doing that.
641 * Which allows us to have the key and UNION contents
642 * disagree.
643 */
644 if (!found && child->da->flags.is_unknown) {
645 fr_assert(child->da->type == FR_TYPE_OCTETS);
646
647 goto encode;
648 }
649
650 /*
651 * No child matching the key vp was found. Either there's no key_vp, or the key_vp doesn't match
652 * the chld we have.
653 *
654 * We then update the key field so that it corresponds to the child that we found.
655 */
656 if (!found) {
657 fr_dict_enum_value_t const *enumv;
659 fr_dbuff_t key_dbuff;
660
661 /*
662 * Root through the enum values, looking for a child ref which matches the child we
663 * found.
664 */
665 for (enumv = fr_dict_enum_iter_init(key_da, &iter);
666 enumv != NULL;
667 enumv = fr_dict_enum_iter_next(key_da, &iter)) {
668 child_ref = fr_dict_enum_attr_ref(enumv);
669 if (!child_ref) continue;
670
671 if (child_ref == child->da) break;
672 }
673
674 /*
675 * There's a child, but no matching enum. That's a fatal error of the dictionary
676 * tokenizer.
677 */
678 if (!fr_cond_assert(enumv)) return PAIR_ENCODE_FATAL_ERROR;
679
680 /*
681 * Create a dbuff for the key, and encode the key.
682 *
683 * Note that enumv->value->vb_length is NOT set. That field is really only used for
684 * string / octet data types.
685 */
686 fr_assert(key_da->flags.length >= 1);
687 fr_assert(key_da->flags.length <= 4);
688
689 FR_DBUFF_INIT(&key_dbuff, fr_dbuff_current(key_m), (size_t) key_da->flags.length);
690
691 FR_PROTO_TRACE("fr_struct_to_network union %s encoding key %s for child %s",
692 parent->da->name, key_da->name, child->da->name);
693
694 if (fr_value_box_to_network(&key_dbuff, enumv->value) <= 0) return PAIR_ENCODE_FATAL_ERROR;
695 }
696
697 /*
698 * And finally encode the one child.
699 */
700encode:
701 FR_PROTO_TRACE("fr_struct_to_network union %s encoding child %s", parent->da->name, child->da->name);
702 fr_proto_da_stack_build(da_stack, child->da);
703 FR_PROTO_STACK_PRINT(da_stack, depth);
704
705 switch (child->da->type) {
706 case FR_TYPE_STRUCT:
707 slen = fr_struct_to_network(&work_dbuff, da_stack, depth + 2,
708 &child_cursor, encode_ctx, encode_value, encode_pair);
709 break;
710
711 case FR_TYPE_TLV:
712 slen = encode_tlv(&work_dbuff, child->da, da_stack, depth + 2, &child_cursor, encode_ctx, encode_value, encode_pair);
713 break;
714
715 case FR_TYPE_LEAF:
716 slen = encode_value(&work_dbuff, da_stack, depth + 2, &child_cursor, encode_ctx);
717 break;
718
719 default:
720 slen = 0;
721 break;
722 }
723
724 if (slen < 0) return slen;
725
726 /*
727 * @todo - if there is more than one child of the union, that's an error!
728 */
729
730 return fr_dbuff_set(dbuff, &work_dbuff);
731}
732
734 fr_da_stack_t *da_stack, unsigned int depth,
735 fr_dcursor_t *cursor, void *encode_ctx,
737{
738 FR_PROTO_TRACE("fr_struct_to_network encoding key %s", vp->da->name);
739
740 /*
741 * We usually have a keyed struct for the child.
742 */
743 if (vp->vp_type == FR_TYPE_STRUCT) {
744 fr_proto_da_stack_build(da_stack, vp->da);
745 return fr_struct_to_network(dbuff, da_stack, depth + 2, /* note + 2 !!! */
747 }
748
749 /*
750 * If it's not a real child, then it's a raw something.
751 */
752 fr_assert(vp->vp_type == FR_TYPE_OCTETS);
753 fr_assert(vp->da->flags.is_unknown);
754
755 if (fr_value_box_to_network(dbuff, &vp->data) <= 0) return PAIR_ENCODE_FATAL_ERROR;
756 (void) fr_dcursor_next(cursor);
757 return 0;
758}
759
761 fr_da_stack_t *da_stack, unsigned int depth,
762 fr_dcursor_t *parent_cursor, void *encode_ctx,
764{
765 fr_dbuff_t work_dbuff;
767 int offset = 0;
768 unsigned int child_num;
769 bool do_length = false;
770 uint8_t bit_buffer = 0;
771 fr_pair_t const *vp = fr_dcursor_current(parent_cursor);
772 fr_pair_t const *last = NULL;
773 fr_pair_t const *key_vp = NULL;
774 fr_dict_attr_t const *child, *parent, *key_da = NULL;
775 fr_dcursor_t child_cursor, *cursor;
776 size_t prefix_length = 0;
777 ssize_t slen;
778 fr_dbuff_marker_t key_m;
779
780 if (!vp) {
781 fr_strerror_printf("%s: Can't encode empty struct", __FUNCTION__);
783 }
784
786 parent = da_stack->da[depth];
787
788 if (parent->type != FR_TYPE_STRUCT) {
789 fr_strerror_printf("%s: Expected type \"struct\" got \"%s\"", __FUNCTION__,
790 fr_type_to_str(parent->type));
792 }
793
794 /*
795 * If we get passed a struct VP, sort its children.
796 */
797 if (vp->vp_type == FR_TYPE_STRUCT) {
798 fr_pair_t *sorted = fr_dcursor_current(parent_cursor); /* NOT const */
799
800 fr_pair_list_sort(&sorted->vp_group, pair_sort_increasing);
801 fr_pair_dcursor_iter_init(&child_cursor, &sorted->vp_group, struct_next_encodable, parent);
802
803 /*
804 * Build the da_stack for the new structure.
805 */
806 vp = fr_dcursor_current(&child_cursor);
807 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
808
809 FR_PROTO_TRACE("fr_struct_to_network encoding nested with parent %s", parent->name);
810 cursor = &child_cursor;
811 } else {
812 FR_PROTO_TRACE("fr_struct_to_network encoding flat");
813 cursor = parent_cursor;
814 }
815
816 /*
817 * @todo - if we get a child which *eventually* has the
818 * given parent, then allow encoding of that struct, too.
819 * This allows us to encode structures automatically,
820 * even if key fields are omitted.
821 *
822 * Note that this check catches TLVs which are "flat" and
823 * not nested. We could fix that by adding a special
824 * case, but it's better to just fix everything to handle
825 * nested attributes.
826 */
827 if (vp && (vp->da->parent != parent)) {
828 fr_strerror_printf("%s: Asked to encode %s, but its parent %s is not the expected parent %s",
829 __FUNCTION__, vp->da->name, vp->da->parent->name, parent->name);
831 }
832
833 /*
834 * Some structs are prefixed by a 16-bit length.
835 */
837 work_dbuff = FR_DBUFF(dbuff);
838
839 } else if (da_is_length_field8(parent)) {
840 work_dbuff = FR_DBUFF_MAX(dbuff, UINT8_MAX);
841 fr_dbuff_marker(&hdr, &work_dbuff);
842
843 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
844 prefix_length = 1;
845 do_length = true;
846
847 } else {
849
850 work_dbuff = FR_DBUFF_MAX(dbuff, UINT16_MAX);
851 fr_dbuff_marker(&hdr, &work_dbuff);
852
853 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2);
854 prefix_length = 2;
855 do_length = true;
856 }
857
859
860 /*
861 * Loop over all children.
862 */
863 for (child_num = 1;
864 (child = fr_dict_attr_child_by_num(parent, child_num)) != NULL;
865 child_num++) {
866 /*
867 * The child attributes should be in order. If
868 * they're not, we fill the struct with zeroes.
869 *
870 * The caller will encode TLVs.
871 */
872 FR_PROTO_TRACE("fr_struct_to_network child %s", child->name);
873
874 /*
875 * If the caller specifies a member twice, then we only encode the first member.
876 */
877 while (last && vp && (last->da->parent == vp->da->parent) && (last->da->attr == vp->da->attr)) {
878 fr_assert(last != vp);
879 vp = fr_dcursor_next(cursor);
880 }
881 last = vp;
882
883 /*
884 * The MEMBER may be raw, in which case it is encoded as octets.
885 *
886 * This can happen for the last MEMBER of a struct, such as when the last member is a TLV
887 * or GROUP, and the contents are malformed.
888 *
889 * It can also happen if a middle MEMBER has the right length, but the wrong contents.
890 * e.g. when the contents have to be a well-formed IP prefix, but the prefix values are
891 * out of the permitted range.
892 */
893 if (vp && (vp->da != child) && (vp->da->parent == parent) && (vp->da->attr == child_num)) {
894 fr_assert(vp->vp_raw);
895 fr_assert(vp->vp_type == FR_TYPE_OCTETS);
896 fr_assert(!da_is_bit_field(child));
897
898 goto encode_data; /* we may have a raw entry in an array :( */
899 }
900
901 /*
902 * Remember the key field. Note that we ignore raw key fields.
903 */
904 if (fr_dict_attr_is_key_field(child)) {
905 fr_assert(!key_da);
906
907 key_da = child;
908 key_vp = vp;
909 fr_dbuff_marker(&key_m, &work_dbuff);
910 }
911
912 /*
913 * Skipped a VP, or left one off at the end, fill the struct with zeros.
914 */
915 if (!vp || (vp->da != child)) {
916 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), " no child %s", child->name);
917
918 /*
919 * Zero out the bit field.
920 */
921 if (da_is_bit_field(child)) {
922 offset = put_bits_dbuff(&work_dbuff, &bit_buffer, offset, child->flags.length, 0);
923 if (offset < 0) {
924 fr_strerror_printf("Failed encoding bit field %s", child->name);
925 return offset;
926 }
927 last = NULL;
928 continue;
929 }
930
931 /*
932 * A child TLV is missing, we're done, and we don't encode any data.
933 *
934 * @todo - mark up the TLVs as required?
935 */
936 if (child->type == FR_TYPE_TLV) goto encode_length;
937
938 /*
939 * Zero out the unused field.
940 */
941 FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, child->flags.length);
942
943 /*
944 * We didn't encode the current VP, so it's not the last one.
945 */
946 last = NULL;
947 continue;
948 }
949
950 /*
951 * The 'struct' encoder handles bit fields.
952 * They're just integers, so there's no need to
953 * call the protocol encoder.
954 *
955 * This limitation means that we can't have
956 * encrypted bit fields, but that's fine.
957 */
958 if (da_is_bit_field(child)) {
959 uint64_t value;
960
961 FR_PROTO_TRACE("child %s is a bit field", child->name);
962
963 switch (child->type) {
964 case FR_TYPE_BOOL:
965 value = vp->vp_bool;
966 break;
967
968 case FR_TYPE_UINT8:
969 value = vp->vp_uint8;
970 break;
971
972 case FR_TYPE_UINT16:
973 value = vp->vp_uint16;
974 break;
975
976 case FR_TYPE_UINT32:
977 value = vp->vp_uint32;
978 break;
979
980 case FR_TYPE_UINT64:
981 value = vp->vp_uint64;
982 break;
983
984 default:
985 fr_strerror_const("Invalid bit field");
987 }
988
989 offset = put_bits_dbuff(&work_dbuff, &bit_buffer, offset, child->flags.length, value);
990 if (offset < 0) {
991 fr_strerror_printf("Failed encoding bit field %s", child->name);
992 return offset;
993 }
994
995 /*
996 * We have to go to the next pair manually, as the protocol-specific
997 * encode_value() function will normally go to the next cursor entry.
998 */
999 vp = fr_dcursor_next(cursor);
1000 /* We need to continue, there may be more fields to encode */
1001
1002 goto next;
1003 }
1004
1005 /* Not a bit field; insist that no buffered bits remain. */
1006 if (offset != 0) {
1007 leftover_bits:
1008 fr_strerror_const("leftover bits");
1010 }
1011
1012 /*
1013 * Encode child TLVs at the end of a struct.
1014 *
1015 * In order to encode the child TLVs, we need to
1016 * know the length of "T" and "L", and we don't.
1017 * So just let the caller do the work.
1018 */
1019 if (child->type == FR_TYPE_TLV) {
1020 fr_assert(!key_da);
1021
1022 FR_PROTO_TRACE("child %s is a TLV field", child->name);
1023 slen = encode_tlv(&work_dbuff, child, da_stack, depth, cursor,
1025 if (slen < 0) return slen;
1026 goto encode_length;
1027 }
1028
1029 if (child->type == FR_TYPE_UNION) {
1030 FR_PROTO_TRACE("child %s is a UNION field", child->name);
1031
1032 if (!key_da) {
1033 FR_PROTO_TRACE("structure %s is missing key_da", parent->name);
1034 goto encode_length;
1035 }
1036
1037 slen = encode_union(&work_dbuff, child, key_da, key_vp, &key_m, da_stack, depth, cursor,
1039 if (slen < 0) return slen;
1040 goto encode_length;
1041 }
1042
1043 FR_PROTO_TRACE("child %s encode_value", child->name);
1044
1045 /*
1046 * Call the protocol encoder for non-bit fields.
1047 */
1049 fr_proto_da_stack_build(da_stack, child);
1050
1051 if (child->flags.array) {
1052 slen = fr_pair_array_to_network(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx, encode_value);
1053 } else {
1054 slen = encode_value(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx);
1055 }
1056 if (slen < 0) return slen;
1057 vp = fr_dcursor_current(cursor);
1058
1059 next:
1060 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "fr_struct_to_network after child %s", child->name);
1061 }
1062
1063 /* Check for leftover bits */
1064 if (offset != 0) goto leftover_bits;
1065
1066 /*
1067 * Check for keyed data to encode.
1068 */
1069 if (vp && key_da) {
1070 fr_assert((vp->da->parent->type == FR_TYPE_UNION) || (vp->da->parent == key_da) || vp->da->flags.is_unknown || vp->da->flags.is_raw);
1071
1072 slen = encode_keyed_struct(&work_dbuff, vp, da_stack, depth,
1074 if (slen < 0) return slen;
1075 }
1076
1077encode_length:
1078 if (do_length) {
1079 size_t length = fr_dbuff_used(&work_dbuff);
1080
1081#ifdef __COVERITY__
1082 /*
1083 * Coverity somehow can't infer that length
1084 * is at least as long as the prefix, instead
1085 * thinkings it's zero so that it underflows.
1086 * We therefore add a Coverity-only check to
1087 * reassure it.
1088 */
1089 if (length < prefix_length) return PAIR_ENCODE_FATAL_ERROR;
1090#endif
1092 length -= prefix_length;
1093
1094 length += da_length_offset(parent);
1095
1096 if (length > UINT8_MAX) return PAIR_ENCODE_FATAL_ERROR;
1097
1098 (void) fr_dbuff_in(&hdr, (uint8_t) length);
1099 } else {
1100 length -= prefix_length;
1101
1102 length += da_length_offset(parent);
1103
1104 if (length > UINT16_MAX) return PAIR_ENCODE_FATAL_ERROR;
1105
1106 (void) fr_dbuff_in(&hdr, (uint16_t) length);
1107 }
1108 }
1109
1110 /*
1111 * We've encoded the children, so tell the parent cursor
1112 * that we've encoded the parent.
1113 */
1114 if (cursor != parent_cursor) (void) fr_dcursor_next(parent_cursor);
1115
1116 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done fr_struct_to_network");
1117
1118 return fr_dbuff_set(dbuff, &work_dbuff);
1119}
#define RCSID(id)
Definition build.h:603
#define CMP_PREFER_SMALLER(_a, _b)
Evaluates to +1 for a > b, and -1 for a < b.
Definition build.h:122
#define UNUSED
Definition build.h:415
fr_cmp_ret_t
Result of an ordering comparison.
Definition build.h:113
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:819
#define FR_DBUFF_ADVANCE_RETURN(_dbuff_or_marker, _len)
Advance the 'current' position in dbuff or marker by _len bytes returning if _len is out of range.
Definition dbuff.h:1137
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition dbuff.h:81
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:963
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1056
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:950
#define FR_DBUFF_INIT(_out, _start, _len_or_end)
Initialise a dbuff for encoding, and define the first byte of the output buffer.
Definition dbuff.h:420
#define FR_DBUFF_MEMSET_RETURN(_dbuff_or_marker, _c, _inlen)
Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space.
Definition dbuff.h:1561
static uint8_t * fr_dbuff_marker(fr_dbuff_marker_t *m, fr_dbuff_t *dbuff)
Initialises a new marker pointing to the 'current' position of the dbuff.
Definition dbuff.h:1245
#define fr_dbuff_in_memcpy(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into a dbuff or marker.
Definition dbuff.h:1403
#define fr_dbuff_in(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff or marker.
Definition dbuff.h:1620
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:230
#define FR_DBUFF_MAX(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition dbuff.h:334
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
#define fr_dcursor_list_next(_cursor, _ptr)
Definition dcursor.h:810
static void * fr_dcursor_set_current(fr_dcursor_t *cursor, void *item)
Set the cursor to a specified item.
Definition dcursor.h:353
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:177
ssize_t(* fr_pair_decode_value_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, void *decode_ctx)
Decode a value from the network into an output fr_pair_list_t.
Definition decode.h:45
#define da_is_length_field16(_da)
Definition dict.h:174
#define da_length_offset(_da)
Definition dict.h:175
fr_dict_enum_value_t const * fr_dict_enum_iter_init(fr_dict_attr_t const *da, fr_dict_enum_iter_t *iter)
Iterate over all enumeration values for an attribute.
Definition dict_util.c:3688
#define da_is_length_field8(_da)
Definition dict.h:173
static fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Definition dict.h:635
#define da_is_bit_field(_da)
Definition dict.h:171
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:281
fr_dict_enum_value_t const * fr_dict_enum_iter_next(fr_dict_attr_t const *da, fr_dict_enum_iter_t *iter)
Definition dict_util.c:3709
void fr_dict_attr_unknown_free(fr_dict_attr_t const **da)
Free dynamically allocated (unknown attributes)
fr_dict_enum_value_t const * fr_dict_enum_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the structure representing an enum value in a fr_dict_attr_t.
Definition dict_util.c:3729
#define da_is_length_field(_da)
Definition dict.h:172
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition dict_util.c:3667
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:170
Value of an enumerated attribute.
Definition dict.h:277
static fr_dict_attr_t const * fr_dict_enum_attr_ref(fr_dict_enum_value_t const *enumv)
Return the attribute reference associated with an enum.
Definition dict_ext.h:231
Test enumeration values.
Definition dict_test.h:92
ssize_t(* fr_encode_dbuff_t)(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Typedefs for simplifying the use and declaration of protocol encoders.
Definition encode.h:37
Stores the state of the current iteration operation.
Definition hash.h:41
talloc_free(hp)
#define PAIR_DECODE_OOM
Fatal error - Out of memory.
Definition pair.h:45
#define PAIR_ENCODE_FATAL_ERROR
Fatal encoding error.
Definition pair.h:36
static ssize_t encode_value(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encodes the data portion of an attribute.
Definition encode.c:276
ssize_t fr_pair_decode_value(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, UNUSED void *decode_ctx)
Generic decode value.
Definition decode.c:344
ssize_t fr_pair_raw_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *da, uint8_t const *data, size_t data_len)
Create a "raw" pair from malformed network data.
Definition decode.c:86
ssize_t fr_pair_array_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx, fr_pair_decode_value_t decode_value)
Decode an array of values from the network.
Definition decode.c:41
ssize_t fr_pair_array_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_value)
Encode an array of values from the network.
Definition encode.c:41
ssize_t fr_pair_encode_value(fr_dbuff_t *dbuff, UNUSED fr_da_stack_t *da_stack, UNUSED unsigned int depth, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Generic encode value.
Definition encode.c:164
ssize_t fr_pair_cursor_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_pair)
Definition encode.c:73
unsigned short uint16_t
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
#define UINT8_MAX
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
static int encode(bio_handle_t *h, request_t *request, bio_request_t *u, uint8_t id)
Definition bio.c:1842
static unsigned int fr_bytes_from_bits(unsigned int bits)
Convert bits (as in prefix length) to bytes, rounding up.
Definition nbo.h:243
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:716
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:284
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:47
void fr_proto_da_stack_build(fr_da_stack_t *stack, fr_dict_attr_t const *da)
Build a complete DA stack from the da back to the root.
Definition proto.c:118
static fr_internal_encode_ctx_t encode_ctx
#define decode_value
Definition decode.c:411
#define fr_assert(_expr)
Definition rad_assert.h:37
static bool done
Definition radclient.c:80
fr_pair_t * vp
static fr_cmp_ret_t pair_sort_increasing(void const *a, void const *b)
Definition struct.c:530
static ssize_t encode_keyed_struct(fr_dbuff_t *dbuff, fr_pair_t const *vp, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:733
static ssize_t encode_tlv(fr_dbuff_t *dbuff, fr_dict_attr_t const *tlv, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, UNUSED fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:560
ssize_t fr_struct_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *parent_cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:760
static void * struct_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
Definition struct.c:545
static int put_bits_dbuff(fr_dbuff_t *dbuff, uint8_t *p, int start_bit, uint8_t num_bits, uint64_t data)
Put bits into an output dbuff.
Definition struct.c:495
static ssize_t encode_union(fr_dbuff_t *dbuff, fr_dict_attr_t const *wrapper, fr_dict_attr_t const *key_da, fr_pair_t const *key_vp, fr_dbuff_marker_t *key_m, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:594
ssize_t fr_struct_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx, fr_pair_decode_value_t decode_value, fr_pair_decode_value_t decode_tlv)
Convert a STRUCT to one or more VPs.
Definition struct.c:32
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
static size_t encode_pair(command_result_t *result, command_file_ctx_t *cc, char *data, fr_test_point_pair_encode_t *tp, void *encode_ctx, fr_pair_list_t *head, bool truncate, char const *fuzzer_text)
Encode a list of pairs, writing the result to the data buffer as hex.
static ssize_t encode_data(char *p, uint8_t *output, size_t outlen)
#define fr_pair_dcursor_iter_init(_cursor, _list, _iter, _uctx)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:585
#define PAIR_VERIFY(_x)
Definition pair.h:209
fr_pair_t * fr_pair_list_tail(fr_pair_list_t const *list)
Get the tail of a valuepair list.
Definition pair_inline.c:55
int fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp)
Sort a doubly linked list of fr_pair_ts using merge sort.
#define FR_PAIR_APPEND
Definition pair.h:218
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:605
static fr_slen_t parent
Definition pair.h:864
#define FR_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition proto.h:42
#define FR_PROTO_TRACE(_fmt,...)
Definition proto.h:41
#define FR_PROTO_STACK_PRINT(_stack, _depth)
Definition proto.h:44
fr_dict_attr_t const * da[FR_DICT_MAX_TLV_STACK+1]
The stack.
Definition proto.h:57
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:55
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
@ FR_TYPE_UNION
A union of limited children.
Definition types.h:81
#define FR_TYPE_INTERNAL
Definition types.h:319
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
#define fr_type_is_integer(_x)
Definition types.h:381
#define FR_TYPE_LEAF
Definition types.h:317
uint64_t fr_value_box_as_uint64(fr_value_box_t const *vb)
Return a uint64_t from a fr_value_box_t.
Definition value.c:4376
ssize_t fr_value_box_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
Encode a single value box, serializing its contents in generic network format.
Definition value.c:1534
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char ** out
Definition value.h:1061