The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 771ee0b75dc2e4484a6eed9ea2dbf1beacf933f2 $")
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 (%d)", 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 = value;
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 = 0;
373
374 FR_PROTO_TRACE("No matching child structure found");
375 unknown_child:
376
377 /*
378 * But if we have a key field, the unknown attribute number is taken from the
379 * from the key field.
380 */
381 if (fr_type_is_integer(key_vp->vp_type)) {
382 attr = fr_value_box_as_uint64(&key_vp->data);
383 }
384
385 child = fr_dict_attr_unknown_raw_afrom_num(child_ctx, substruct_da, attr);
386 if (!child) {
387 FR_PROTO_TRACE("failed allocating unknown child for key VP %s - %s",
388 key_vp->da->name, fr_strerror());
389 goto oom;
390 }
391
392 slen = fr_pair_raw_from_network(child_ctx, child_list, child, p, end - p);
393 if (slen < 0) {
394 FR_PROTO_TRACE("Failed creating raw VP from malformed or unknown substruct for child %s", child->name);
396 return slen;
397 }
398
399 p = end;
400
401 } else {
402 switch (child->type) {
403 case FR_TYPE_STRUCT:
404 FR_PROTO_TRACE("Decoding child structure %s", child->name);
405 slen = fr_struct_from_network(child_ctx, child_list, child, p, end - p,
406 decode_ctx, decode_value, decode_tlv);
407 break;
408
409 case FR_TYPE_TLV:
410 if (!decode_tlv) {
411 FR_PROTO_TRACE("Failed to pass decode_tlv() for child tlv %s", child->name);
412 goto unknown_child;
413 }
414
415 FR_PROTO_TRACE("Decoding child tlv %s", child->name);
416
417 slen = decode_tlv(child_ctx, child_list, child, p, end - p, decode_ctx);
418 break;
419
420 case FR_TYPE_LEAF:
422
423 FR_PROTO_TRACE("Decoding child %s", child->name);
424
425 /*
426 * @todo - unify this code with the code above, but for now copying is
427 * easier.
428 */
429
430 /*
431 * The child is either unknown width, OR known width with a length that is too large for
432 * the "length" field, OR is known width via some kind of protocol-specific length header.
433 */
434 if (!child->flags.length || child->flags.array) {
435 child_length = end - p;
436
437 } else {
438 child_length = child->flags.length;
439
440 /*
441 * If this field overflows the input, then *all*
442 * of the input is suspect.
443 */
444 if (child_length > (size_t) (end - p)) {
445 FR_PROTO_TRACE("fr_struct_from_network - child length %zu overflows buffer", child_length);
446 goto remainder_raw;
447 }
448 }
449
450 if (child->flags.array) {
451 slen = fr_pair_array_from_network(child_ctx, child_list, child, p, child_length, decode_ctx, decode_value);
452 } else {
453 slen = decode_value(child_ctx, child_list, child, p, child_length, decode_ctx);
454 }
455 break;
456
457 default:
458 FR_PROTO_TRACE("Unknown data type %s in child %s", fr_type_to_str(child->type), child->name);
459 goto unknown_child;
460 }
461
462 if (slen <= 0) {
463 FR_PROTO_TRACE("failed decoding child %s", child->name);
464 goto unknown_child;
465 }
466 p += slen;
467 }
468
470 }
471
472done:
473 fr_assert(struct_vp != NULL);
474 fr_pair_append(out, struct_vp);
475
476 FR_PROTO_TRACE("used %zu bytes", data_len);
477 return p - data;
478}
479
480
481/** Put bits into an output dbuff
482 *
483 * @param dbuff where the bytes go
484 * @param p where leftover bits go
485 * @param start_bit start bit in the dbuff where the data goes, 0..7
486 * @param num_bits number of bits to write to the output, 0..55
487 * @param data data to write, all in the lower "num_bits" of the uint64_t variable
488 * @return
489 * >= 0 the next value to pass in for start_bit
490 * < 0 no space or invalid start_bit or num_bits parameter
491 */
492static int put_bits_dbuff(fr_dbuff_t *dbuff, uint8_t *p, int start_bit, uint8_t num_bits, uint64_t data)
493{
494 uint64_t used_bits;
495
496 if (start_bit < 0 || start_bit > 7) return -1;
497 if (num_bits < 1 || num_bits > 56) return -1;
498
499 /* Get bits buffered in *p */
500 used_bits = *p & (-256 >> start_bit);
501
502 /* Mask out all but the least significant num_bits bits of data */
503 data &= (((uint64_t) 1) << num_bits) - 1;
504
505 /* Move it towards the most significant end and put used_bits at the top */
506 data <<= (64 - (start_bit + num_bits));
507 data |= used_bits << 56;
508
509 data = htonll(data);
510
511 start_bit += num_bits;
512 if (start_bit > 7) FR_DBUFF_IN_MEMCPY_RETURN(dbuff, (uint8_t const *) &data, (size_t)(start_bit / 8));
513
514 *p = ((uint8_t *) &data)[start_bit / 8];
515 return start_bit % 8;
516}
517
518static int8_t pair_sort_increasing(void const *a, void const *b)
519{
520 fr_pair_t const *my_a = a;
521 fr_pair_t const *my_b = b;
522 int8_t ret;
523
524 /*
525 * Deeper attributes come later in the list.
526 */
527 ret = CMP_PREFER_SMALLER(my_a->da->depth, my_b->da->depth);
528 if (ret != 0) return ret;
529
530 return CMP_PREFER_SMALLER(my_a->da->attr, my_b->da->attr);
531}
532
533static void *struct_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
534{
535 fr_pair_t *c = current;
536 fr_dict_attr_t *parent = talloc_get_type_abort(uctx, fr_dict_attr_t);
537
538 while ((c = fr_dlist_next(cursor->dlist, c))) {
539 PAIR_VERIFY(c);
540
541 if (c->da->dict != parent->dict || c->da->flags.internal) continue;
542 break;
543 }
544
545 return c;
546}
547
548static ssize_t encode_tlv(fr_dbuff_t *dbuff, fr_dict_attr_t const *tlv,
549 fr_da_stack_t *da_stack, unsigned int depth,
550 fr_dcursor_t *cursor, void *encode_ctx,
552
553{
554 fr_pair_t *vp;
555 fr_dcursor_t child_cursor;
556 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
557
558 if (!encode_pair) {
559 fr_strerror_printf("Asked to encode child attribute %s, but we were not passed an encoding function",
560 tlv->name);
562 }
563
564 vp = fr_dcursor_current(cursor);
565 if (!vp || (vp->da != tlv)) return 0;
566
567 vp = fr_pair_dcursor_init(&child_cursor, &vp->vp_group);
568 if (vp) {
569 ssize_t slen;
570
571 FR_PROTO_TRACE("fr_struct_to_network trailing TLVs of %s", tlv->name);
572 fr_proto_da_stack_build(da_stack, vp->da);
573 FR_PROTO_STACK_PRINT(da_stack, depth);
574
575 slen = fr_pair_cursor_to_network(&work_dbuff, da_stack, depth + 1, &child_cursor, encode_ctx, encode_pair);
576 if (slen < 0) return slen;
577 }
578
579 return fr_dbuff_set(dbuff, &work_dbuff);
580}
581
582static ssize_t encode_union(fr_dbuff_t *dbuff, fr_dict_attr_t const *wrapper,
583 fr_dict_attr_t const *key_da, fr_pair_t const *key_vp, fr_dbuff_marker_t *key_m,
584 fr_da_stack_t *da_stack, unsigned int depth,
585 fr_dcursor_t *cursor, void *encode_ctx,
587
588{
589 ssize_t slen;
590 fr_pair_t *parent, *child, *found = NULL;
591 fr_dict_attr_t const *child_ref;
592 fr_dcursor_t child_cursor;
593 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
594
595 parent = fr_dcursor_current(cursor);
596 if (!parent || (parent->da != wrapper)) return 0;
597
598 fr_assert(key_vp); /* @todo */
599
600 child = fr_pair_dcursor_init(&child_cursor, &parent->vp_group);
601 if (!child) {
602 /*
603 * @todo - do we want to skip encoding the entire parent structure?
604 */
605 FR_PROTO_TRACE("fr_struct_to_network union %s has no children", key_da->name);
606 return 0;
607 }
608
609 /*
610 * There's a key VP, we find the matching child struct, and then set the cursor to encode just
611 * that child.
612 */
613 if (key_vp) {
614 fr_dict_enum_value_t const *enumv;
615
616 enumv = fr_dict_enum_by_value(key_da, &key_vp->data);
617 if (enumv && ((child_ref = fr_dict_enum_attr_ref(enumv)) != NULL)) {
618 found = fr_pair_find_by_da(&parent->vp_group, NULL, child_ref);
619 if (found) {
620 (void) fr_dcursor_set_current(&child_cursor, found);
621 }
622 }
623 }
624
625 /*
626 * @todo - encode the key field based on the attribute number?
627 *
628 * However, we are likely better off just not doing that.
629 * Which allows us to have the key and UNION contents
630 * disagree.
631 */
632 if (!found && child->da->flags.is_unknown) {
633 fr_assert(child->da->type == FR_TYPE_OCTETS);
634
635 goto encode;
636 }
637
638 /*
639 * No child matching the key vp was found. Either there's no key_vp, or the key_vp doesn't match
640 * the chld we have.
641 *
642 * We then update the key field so that it corresponds to the child that we found.
643 */
644 if (!found) {
645 fr_dict_enum_value_t const *enumv;
647 fr_dbuff_t key_dbuff;
648
649 /*
650 * Root through the enum values, looking for a child ref which matches the child we
651 * found.
652 */
653 for (enumv = fr_dict_enum_iter_init(key_da, &iter);
654 enumv != NULL;
655 enumv = fr_dict_enum_iter_next(key_da, &iter)) {
656 child_ref = fr_dict_enum_attr_ref(enumv);
657 if (!child_ref) continue;
658
659 if (child_ref == child->da) break;
660 }
661
662 /*
663 * There's a child, but no matching enum. That's a fatal error of the dictionary
664 * tokenizer.
665 */
666 if (!fr_cond_assert(enumv)) return PAIR_ENCODE_FATAL_ERROR;
667
668 /*
669 * Create a dbuff for the key, and encode the key.
670 *
671 * Note that enumv->value->vb_length is NOT set. That field is really only used for
672 * string / octet data types.
673 */
674 fr_assert(key_da->flags.length >= 1);
675 fr_assert(key_da->flags.length <= 4);
676
677 FR_DBUFF_INIT(&key_dbuff, fr_dbuff_current(key_m), (size_t) key_da->flags.length);
678
679 FR_PROTO_TRACE("fr_struct_to_network union %s encoding key %s for child %s",
680 parent->da->name, key_da->name, child->da->name);
681
682 if (fr_value_box_to_network(&key_dbuff, enumv->value) <= 0) return PAIR_ENCODE_FATAL_ERROR;
683 }
684
685 /*
686 * And finally encode the one child.
687 */
688encode:
689 FR_PROTO_TRACE("fr_struct_to_network union %s encoding child %s", parent->da->name, child->da->name);
690 fr_proto_da_stack_build(da_stack, child->da);
691 FR_PROTO_STACK_PRINT(da_stack, depth);
692
693 switch (child->da->type) {
694 case FR_TYPE_STRUCT:
695 slen = fr_struct_to_network(&work_dbuff, da_stack, depth + 2,
696 &child_cursor, encode_ctx, encode_value, encode_pair);
697 break;
698
699 case FR_TYPE_TLV:
700 slen = encode_tlv(&work_dbuff, child->da, da_stack, depth + 2, &child_cursor, encode_ctx, encode_value, encode_pair);
701 break;
702
703 case FR_TYPE_LEAF:
704 slen = encode_value(&work_dbuff, da_stack, depth + 2, &child_cursor, encode_ctx);
705 break;
706
707 default:
708 slen = 0;
709 break;
710 }
711
712 if (slen < 0) return slen;
713
714 /*
715 * @todo - if there is more than one child of the union, that's an error!
716 */
717
718 return fr_dbuff_set(dbuff, &work_dbuff);
719}
720
722 fr_da_stack_t *da_stack, unsigned int depth,
723 fr_dcursor_t *cursor, void *encode_ctx,
725{
726 FR_PROTO_TRACE("fr_struct_to_network encoding key %s", vp->da->name);
727
728 /*
729 * We usually have a keyed struct for the child.
730 */
731 if (vp->vp_type == FR_TYPE_STRUCT) {
732 fr_proto_da_stack_build(da_stack, vp->da);
733 return fr_struct_to_network(dbuff, da_stack, depth + 2, /* note + 2 !!! */
735 }
736
737 /*
738 * If it's not a real child, then it's a raw something.
739 */
740 fr_assert(vp->vp_type == FR_TYPE_OCTETS);
741 fr_assert(vp->da->flags.is_unknown);
742
743 if (fr_value_box_to_network(dbuff, &vp->data) <= 0) return PAIR_ENCODE_FATAL_ERROR;
744 (void) fr_dcursor_next(cursor);
745 return 0;
746}
747
749 fr_da_stack_t *da_stack, unsigned int depth,
750 fr_dcursor_t *parent_cursor, void *encode_ctx,
752{
753 fr_dbuff_t work_dbuff;
755 int offset = 0;
756 unsigned int child_num;
757 bool do_length = false;
758 uint8_t bit_buffer = 0;
759 fr_pair_t const *vp = fr_dcursor_current(parent_cursor);
760 fr_pair_t const *last = NULL;
761 fr_pair_t const *key_vp = NULL;
762 fr_dict_attr_t const *child, *parent, *key_da = NULL;
763 fr_dcursor_t child_cursor, *cursor;
764 size_t prefix_length = 0;
765 ssize_t slen;
766 fr_dbuff_marker_t key_m;
767
768 if (!vp) {
769 fr_strerror_printf("%s: Can't encode empty struct", __FUNCTION__);
771 }
772
774 parent = da_stack->da[depth];
775
776 if (parent->type != FR_TYPE_STRUCT) {
777 fr_strerror_printf("%s: Expected type \"struct\" got \"%s\"", __FUNCTION__,
778 fr_type_to_str(parent->type));
780 }
781
782 /*
783 * If we get passed a struct VP, sort its children.
784 */
785 if (vp->vp_type == FR_TYPE_STRUCT) {
786 fr_pair_t *sorted = fr_dcursor_current(parent_cursor); /* NOT const */
787
788 fr_pair_list_sort(&sorted->vp_group, pair_sort_increasing);
789 fr_pair_dcursor_iter_init(&child_cursor, &sorted->vp_group, struct_next_encodable, parent);
790
791 /*
792 * Build the da_stack for the new structure.
793 */
794 vp = fr_dcursor_current(&child_cursor);
795 fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
796
797 FR_PROTO_TRACE("fr_struct_to_network encoding nested with parent %s", parent->name);
798 cursor = &child_cursor;
799 } else {
800 FR_PROTO_TRACE("fr_struct_to_network encoding flat");
801 cursor = parent_cursor;
802 }
803
804 /*
805 * @todo - if we get a child which *eventually* has the
806 * given parent, then allow encoding of that struct, too.
807 * This allows us to encode structures automatically,
808 * even if key fields are omitted.
809 *
810 * Note that this check catches TLVs which are "flat" and
811 * not nested. We could fix that by adding a special
812 * case, but it's better to just fix everything to handle
813 * nested attributes.
814 */
815 if (vp && (vp->da->parent != parent)) {
816 fr_strerror_printf("%s: Asked to encode %s, but its parent %s is not the expected parent %s",
817 __FUNCTION__, vp->da->name, vp->da->parent->name, parent->name);
819 }
820
821 /*
822 * Some structs are prefixed by a 16-bit length.
823 */
825 work_dbuff = FR_DBUFF(dbuff);
826
827 } else if (da_is_length_field8(parent)) {
828 work_dbuff = FR_DBUFF_MAX(dbuff, UINT8_MAX);
829 fr_dbuff_marker(&hdr, &work_dbuff);
830
831 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
832 prefix_length = 1;
833 do_length = true;
834
835 } else {
837
838 work_dbuff = FR_DBUFF_MAX(dbuff, UINT16_MAX);
839 fr_dbuff_marker(&hdr, &work_dbuff);
840
841 FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2);
842 prefix_length = 2;
843 do_length = true;
844 }
845
847
848 /*
849 * Loop over all children.
850 */
851 for (child_num = 1;
852 (child = fr_dict_attr_child_by_num(parent, child_num)) != NULL;
853 child_num++) {
854 /*
855 * The child attributes should be in order. If
856 * they're not, we fill the struct with zeroes.
857 *
858 * The caller will encode TLVs.
859 */
860 FR_PROTO_TRACE("fr_struct_to_network child %s", child->name);
861
862 /*
863 * If the caller specifies a member twice, then we only encode the first member.
864 */
865 while (last && vp && (last->da->parent == vp->da->parent) && (last->da->attr == vp->da->attr)) {
866 fr_assert(last != vp);
867 vp = fr_dcursor_next(cursor);
868 }
869 last = vp;
870
871 /*
872 * The MEMBER may be raw, in which case it is encoded as octets.
873 *
874 * This can happen for the last MEMBER of a struct, such as when the last member is a TLV
875 * or GROUP, and the contents are malformed.
876 *
877 * It can also happen if a middle MEMBER has the right length, but the wrong contents.
878 * e.g. when the contents have to be a well-formed IP prefix, but the prefix values are
879 * out of the permitted range.
880 */
881 if (vp && (vp->da != child) && (vp->da->parent == parent) && (vp->da->attr == child_num)) {
882 fr_assert(vp->vp_raw);
883 fr_assert(vp->vp_type == FR_TYPE_OCTETS);
884 fr_assert(!da_is_bit_field(child));
885
886 goto encode_data; /* we may have a raw entry in an array :( */
887 }
888
889 /*
890 * Remember the key field. Note that we ignore raw key fields.
891 */
892 if (fr_dict_attr_is_key_field(child)) {
893 fr_assert(!key_da);
894
895 key_da = child;
896 key_vp = vp;
897 fr_dbuff_marker(&key_m, &work_dbuff);
898 }
899
900 /*
901 * Skipped a VP, or left one off at the end, fill the struct with zeros.
902 */
903 if (!vp || (vp->da != child)) {
904 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), " no child %s", child->name);
905
906 /*
907 * Zero out the bit field.
908 */
909 if (da_is_bit_field(child)) {
910 offset = put_bits_dbuff(&work_dbuff, &bit_buffer, offset, child->flags.length, 0);
911 if (offset < 0) {
912 fr_strerror_printf("Failed encoding bit field %s", child->name);
913 return offset;
914 }
915 last = NULL;
916 continue;
917 }
918
919 /*
920 * A child TLV is missing, we're done, and we don't encode any data.
921 *
922 * @todo - mark up the TLVs as required?
923 */
924 if (child->type == FR_TYPE_TLV) goto encode_length;
925
926 /*
927 * Zero out the unused field.
928 */
929 FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, child->flags.length);
930
931 /*
932 * We didn't encode the current VP, so it's not the last one.
933 */
934 last = NULL;
935 continue;
936 }
937
938 /*
939 * The 'struct' encoder handles bit fields.
940 * They're just integers, so there's no need to
941 * call the protocol encoder.
942 *
943 * This limitation means that we can't have
944 * encrypted bit fields, but that's fine.
945 */
946 if (da_is_bit_field(child)) {
947 uint64_t value;
948
949 FR_PROTO_TRACE("child %s is a bit field", child->name);
950
951 switch (child->type) {
952 case FR_TYPE_BOOL:
953 value = vp->vp_bool;
954 break;
955
956 case FR_TYPE_UINT8:
957 value = vp->vp_uint8;
958 break;
959
960 case FR_TYPE_UINT16:
961 value = vp->vp_uint16;
962 break;
963
964 case FR_TYPE_UINT32:
965 value = vp->vp_uint32;
966 break;
967
968 case FR_TYPE_UINT64:
969 value = vp->vp_uint64;
970 break;
971
972 default:
973 fr_strerror_const("Invalid bit field");
975 }
976
977 offset = put_bits_dbuff(&work_dbuff, &bit_buffer, offset, child->flags.length, value);
978 if (offset < 0) {
979 fr_strerror_printf("Failed encoding bit field %s", child->name);
980 return offset;
981 }
982
983 /*
984 * We have to go to the next pair manually, as the protocol-specific
985 * encode_value() function will normally go to the next cursor entry.
986 */
987 vp = fr_dcursor_next(cursor);
988 /* We need to continue, there may be more fields to encode */
989
990 goto next;
991 }
992
993 /* Not a bit field; insist that no buffered bits remain. */
994 if (offset != 0) {
995 leftover_bits:
996 fr_strerror_const("leftover bits");
998 }
999
1000 /*
1001 * Encode child TLVs at the end of a struct.
1002 *
1003 * In order to encode the child TLVs, we need to
1004 * know the length of "T" and "L", and we don't.
1005 * So just let the caller do the work.
1006 */
1007 if (child->type == FR_TYPE_TLV) {
1008 fr_assert(!key_da);
1009
1010 FR_PROTO_TRACE("child %s is a TLV field", child->name);
1011 slen = encode_tlv(&work_dbuff, child, da_stack, depth, cursor,
1013 if (slen < 0) return slen;
1014 goto encode_length;
1015 }
1016
1017 if (child->type == FR_TYPE_UNION) {
1018 FR_PROTO_TRACE("child %s is a UNION field", child->name);
1019
1020 if (!key_da) {
1021 FR_PROTO_TRACE("structure %s is missing key_da", parent->name);
1022 goto encode_length;
1023 }
1024
1025 slen = encode_union(&work_dbuff, child, key_da, key_vp, &key_m, da_stack, depth, cursor,
1027 if (slen < 0) return slen;
1028 goto encode_length;
1029 }
1030
1031 FR_PROTO_TRACE("child %s encode_value", child->name);
1032
1033 /*
1034 * Call the protocol encoder for non-bit fields.
1035 */
1037 fr_proto_da_stack_build(da_stack, child);
1038
1039 if (child->flags.array) {
1040 slen = fr_pair_array_to_network(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx, encode_value);
1041 } else {
1042 slen = encode_value(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx);
1043 }
1044 if (slen < 0) return slen;
1045 vp = fr_dcursor_current(cursor);
1046
1047 next:
1048 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "fr_struct_to_network after child %s", child->name);
1049 }
1050
1051 /* Check for leftover bits */
1052 if (offset != 0) goto leftover_bits;
1053
1054 /*
1055 * Check for keyed data to encode.
1056 */
1057 if (vp && key_da) {
1058 fr_assert((vp->da->parent->type == FR_TYPE_UNION) || (vp->da->parent == key_da) || vp->da->flags.is_unknown || vp->da->flags.is_raw);
1059
1060 slen = encode_keyed_struct(&work_dbuff, vp, da_stack, depth,
1062 if (slen < 0) return slen;
1063 }
1064
1065encode_length:
1066 if (do_length) {
1067 size_t length = fr_dbuff_used(&work_dbuff);
1068
1069#ifdef __COVERITY__
1070 /*
1071 * Coverity somehow can't infer that length
1072 * is at least as long as the prefix, instead
1073 * thinkings it's zero so that it underflows.
1074 * We therefore add a Coverity-only check to
1075 * reassure it.
1076 */
1077 if (length < prefix_length) return PAIR_ENCODE_FATAL_ERROR;
1078#endif
1080 length -= prefix_length;
1081
1082 length += da_length_offset(parent);
1083
1084 if (length > UINT8_MAX) return PAIR_ENCODE_FATAL_ERROR;
1085
1086 (void) fr_dbuff_in(&hdr, (uint8_t) length);
1087 } else {
1088 length -= prefix_length;
1089
1090 length += da_length_offset(parent);
1091
1092 if (length > UINT16_MAX) return PAIR_ENCODE_FATAL_ERROR;
1093
1094 (void) fr_dbuff_in(&hdr, (uint16_t) length);
1095 }
1096 }
1097
1098 /*
1099 * We've encoded the children, so tell the parent cursor
1100 * that we've encoded the parent.
1101 */
1102 if (cursor != parent_cursor) (void) fr_dcursor_next(parent_cursor);
1103
1104 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done fr_struct_to_network");
1105
1106 return fr_dbuff_set(dbuff, &work_dbuff);
1107}
#define RCSID(id)
Definition build.h:485
#define CMP_PREFER_SMALLER(_a, _b)
Evaluates to +1 for a > b, and -1 for a < b.
Definition build.h:104
#define UNUSED
Definition build.h:317
#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:777
#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:1099
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition dbuff.h:83
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:921
#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:1014
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:908
#define FR_DBUFF_INIT(_out, _start, _len_or_end)
Definition dbuff.h:387
#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:1519
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:1203
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1393
#define fr_dbuff_in(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff or marker.
Definition dbuff.h:1578
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:232
#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:311
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:290
static void * fr_dcursor_set_current(fr_dcursor_t *cursor, void *item)
Set the cursor to a specified item.
Definition dcursor.h:355
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:339
fr_dlist_head_t * dlist
Head of the doubly linked list being iterated over.
Definition dcursor.h:94
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
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:3638
#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:616
#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:258
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:3659
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:3679
#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:3617
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:170
Value of an enumerated attribute.
Definition dict.h:254
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:260
Test enumeration values.
Definition dict_test.h:92
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:555
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
#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:277
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:337
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:79
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:42
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:162
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:71
talloc_free(reap)
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.
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:1259
static unsigned int fr_bytes_from_bits(unsigned int bits)
Convert bits (as in prefix length) to bytes, rounding up.
Definition nbo.h:237
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:703
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1348
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:289
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
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
static ssize_t encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Definition base.c:39
#define decode_value
Definition decode.c:410
#define fr_assert(_expr)
Definition rad_assert.h:38
static rc_request_t * current
static bool done
Definition radclient.c:81
fr_pair_t * vp
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:721
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:548
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:748
static void * struct_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
Definition struct.c:533
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, UNUSED fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition struct.c:582
static int8_t pair_sort_increasing(void const *a, void const *b)
Definition struct.c:518
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:492
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 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:582
#define PAIR_VERIFY(_x)
Definition pair.h:202
void 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.
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
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:602
static fr_slen_t parent
Definition pair.h:854
#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:553
#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:82
#define FR_TYPE_INTERNAL
Definition types.h:320
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:455
#define fr_type_is_integer(_x)
Definition types.h:382
#define FR_TYPE_LEAF
Definition types.h:318
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:4028
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:1516
static fr_slen_t data
Definition value.h:1322
static size_t char ** out
Definition value.h:1023