The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
pair.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/** AVP manipulation and search API
18 *
19 * @file src/lib/util/pair.c
20 *
21 * @copyright 2021 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22 * @copyright 2000,2006,2015,2020 The FreeRADIUS server project
23 */
24RCSID("$Id: e30052c600c7e1f06b904e6523cdf2ffb3c0daf5 $")
25
26#define _PAIR_PRIVATE 1
27#define _PAIR_INLINE 1
28
29#include <freeradius-devel/util/debug.h>
30#include <freeradius-devel/util/misc.h>
31#include <freeradius-devel/util/pair.h>
32#include <freeradius-devel/util/pair_legacy.h>
33#include <freeradius-devel/util/proto.h>
34#include <freeradius-devel/util/regex.h>
35
36FR_TLIST_FUNCS(fr_pair_order_list, fr_pair_t, order_entry)
37
38#include <freeradius-devel/util/pair_inline.c>
39
40/** Initialise a pair list header
41 *
42 * @param[in,out] list to initialise
43 *
44 * @hidecallergraph
45 */
47{
48 /*
49 * Initialises the order list. This
50 * maintains the overall order of attributes
51 * in the list and allows us to iterate over
52 * all of them.
53 */
54 fr_pair_order_list_talloc_init(&list->order);
55
56#ifdef WITH_VERIFY_PTR
57 list->verified = true;
58#endif
59 list->is_child = false;
60}
61
62/** Free a fr_pair_t
63 *
64 * @note Do not call directly, use talloc_free instead.
65 *
66 * @param vp to free.
67 * @return 0
68 */
70{
71#ifdef TALLOC_DEBUG
72 talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL);
73#endif
74
75#if 0
76 /*
77 * We would like to enforce that a VP must be removed from a list before it's freed. However, we
78 * free pair_lists via talloc_free(). And the talloc code just frees things in (essentially) a
79 * random order. So this guarantee can't be enforced.
80 */
81 fr_assert(fr_pair_order_list_parent(vp) == NULL);
82#endif
83
84 /*
85 * Pairs with children have the children
86 * freed explicitly.
87 */
88 if (likely(vp->da != NULL)) switch (vp->vp_type) {
90 fr_pair_list_free(&vp->vp_group);
91 break;
92
93 case FR_TYPE_STRING:
94 case FR_TYPE_OCTETS:
95 fr_assert(!vp->vp_edit);
96 if (vp->data.secret) memset_explicit(vp->vp_ptr, 0, vp->vp_length);
97 break;
98
99 default:
100 fr_assert(!vp->vp_edit);
101 if (vp->data.secret) memset_explicit(&vp->data, 0, sizeof(vp->data));
102 break;
103 }
104
105#ifndef NDEBUG
106 memset(vp, 0, sizeof(*vp));
107#endif
108
109 return 0;
110}
111
112/** Allocate a new pair list on the heap
113 *
114 * @param[in] ctx to allocate the pair list in.
115 * @return
116 * - A new #fr_pair_list_t.
117 * - NULL if an error occurred.
118 */
120{
121 fr_pair_list_t *pl;
122
123 pl = talloc(ctx, fr_pair_list_t);
124 if (unlikely(!pl)) return NULL;
125
127
128 return pl;
129}
130
131/** Initialise fields in an fr_pair_t without assigning a da
132 *
133 * @note Internal use by the allocation functions only.
134 */
135static inline CC_HINT(always_inline) void pair_init_null(fr_pair_t *vp)
136{
137 fr_pair_order_list_entry_init(vp);
138
139 /*
140 * Legacy cruft
141 */
142 vp->op = T_OP_EQ;
143}
144
145/** Initialise fields in an fr_pair_t without assigning a da
146 *
147 * Used only for temporary value-pairs which are not placed in any list.
148 */
150{
151 memset(vp, 0, sizeof(*vp));
152
154}
155
156/** Dynamically allocate a new attribute with no #fr_dict_attr_t assigned
157 *
158 * This is not the function you're looking for (unless you're binding
159 * unknown attributes to pairs, and need to pre-allocate the memory).
160 * You probably want #fr_pair_afrom_da instead.
161 *
162 * @note You must assign a #fr_dict_attr_t before freeing this #fr_pair_t.
163 *
164 * @param[in] ctx to allocate the pair list in.
165 * @return
166 * - A new #fr_pair_t.
167 * - NULL if an error occurred.
168 */
170{
171 fr_pair_t *vp;
172
173 vp = talloc_zero(ctx, fr_pair_t);
174 if (!vp) {
175 fr_strerror_printf("Out of memory");
176 return NULL;
177 }
178 talloc_set_destructor(vp, _fr_pair_free);
179
182
183 return vp;
184}
185
186/** Continue initialising an fr_pair_t assigning a da
187 *
188 * @note Internal use by the pair allocation functions only.
189 */
190static inline CC_HINT(always_inline) void pair_init_from_da(fr_pair_t *vp, fr_dict_attr_t const *da)
191{
192 /*
193 * Use the 'da' to initialize more fields.
194 */
195 vp->da = da;
196
197 if (likely(fr_type_is_leaf(da->type))) {
198 fr_value_box_init(&vp->data, da->type, da, false);
199 } else {
200#ifndef NDEBUG
201 /*
202 * Make it very obvious if we failed
203 * to initialise something.
204 * Given the definition of fr_value_box_t, this entails
205 * writing const-qualified fields. The compiler allows it,
206 * but Coverity points it out as a defect, so it is annotated.
207 */
208 /* coverity[store_writes_const_field] */
209 memset(&vp->data, 0xff, sizeof(vp->data));
210#endif
211
213
214 /*
215 * Make sure that the pad field is initialized.
216 */
217 if (sizeof(vp->pad)) memset(vp->pad, 0, sizeof(vp->pad));
218
219 /*
220 * Hack around const issues...
221 * Here again, the workaround suffices for the compiler but
222 * not for Coverity, so again we annotate.
223 */
224 /* coverity[store_writes_const_field] */
225 memcpy(UNCONST(fr_type_t *, &vp->vp_type), &da->type, sizeof(vp->vp_type));
226 fr_pair_list_init(&vp->vp_group);
227 vp->vp_group.is_child = true;
228 fr_pair_order_list_talloc_init_children(vp, &vp->vp_group.order);
229 }
230}
231
232/** A special allocation function which disables child autofree
233 *
234 * This is intended to allocate root attributes for requests.
235 * These roots are special in that they do not necessarily own
236 * the child attributes and _MUST NOT_ free them when they
237 * themselves are freed. The children are allocated in special
238 * ctxs which may be moved between session state entries and
239 * requests, or may belong to a parent request.
240 *
241 * @param[in] ctx to allocate the pair root in.
242 * @param[in] da The root attribute.
243 * @return
244 * - A new root pair on success.
245 * - NULL on failure.
246 * @hidecallergraph
247 */
249{
250 fr_pair_t *vp;
251
252#ifndef NDEBUG
253 if (da->type != FR_TYPE_GROUP) {
254 fr_strerror_const("Root must be a group type");
255 return NULL;
256 }
257#endif
258
259 vp = talloc_zero(ctx, fr_pair_t);
260 if (unlikely(!vp)) {
261 fr_strerror_const("Out of memory");
262 return NULL;
263 }
264
265 if (unlikely(da->flags.is_unknown)) {
266 fr_strerror_const("Root attribute cannot be unknown");
268 return NULL;
269 }
270
273
274 return vp;
275}
276
277/** Dynamically allocate a new attribute and assign a #fr_dict_attr_t
278 *
279 * @note Will duplicate any unknown attributes passed as the da.
280 *
281 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t
282 * @param[in] da Specifies the dictionary attribute to build the #fr_pair_t from.
283 * If unknown, will be duplicated, with the memory being bound to
284 * the pair.
285 * @return
286 * - A new #fr_pair_t.
287 * - NULL if an error occurred.
288 * @hidecallergraph
289 */
290fr_pair_t *fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
291{
292 fr_pair_t *vp;
293
294 vp = fr_pair_alloc_null(ctx);
295 if (!vp) {
296 oom:
297 fr_strerror_const("Out of memory");
298 return NULL;
299 }
300
301 /*
302 * If we get passed an unknown da, we need to ensure that
303 * it's parented by "vp".
304 */
305 if (da->flags.is_unknown) {
306 fr_dict_attr_t const *unknown;
307
308 unknown = fr_dict_attr_unknown_copy(vp, da);
309 if (!unknown) {
311 goto oom;
312 }
313 da = unknown;
314 }
315
318
319 return vp;
320}
321
322/** Re-initialise an attribute with a different da
323 *
324 * If the new da has a different type to the old da, we'll attempt to cast
325 * the current value in place.
326 */
328{
329 fr_dict_attr_t const *to_free;
330
331 /*
332 * vp may be created from fr_pair_alloc_null(), in which case it has no da.
333 */
334 if (vp->da && !vp->da->flags.is_raw) {
335 if (vp->da == da) return 0;
336
337 if (!fr_type_is_leaf(vp->vp_type)) return -1;
338
339 if ((da->type != vp->vp_type) && (fr_value_box_cast_in_place(vp, &vp->data, da->type, da) < 0)) return -1;
340 } else {
341 fr_assert(fr_type_is_leaf(vp->vp_type) || (fr_type_is_structural(vp->vp_type) && (fr_pair_list_num_elements(&vp->vp_group) == 0)));
342
343 fr_value_box_init(&vp->data, da->type, da, false);
344 }
345
346 to_free = vp->da;
347 vp->da = da;
348
349 /*
350 * Only frees unknown fr_dict_attr_t's
351 */
353
354 /*
355 * Ensure we update the attribute index in the parent.
356 */
357 if (list) {
358 fr_pair_remove(list, vp);
359
360 fr_pair_append(list, vp);
361 }
362
363 return 0;
364}
365
366/** Create a new valuepair
367 *
368 * If attr and vendor match a dictionary entry then a VP with that #fr_dict_attr_t
369 * will be returned.
370 *
371 * If attr or vendor are unknown will call dict_attruknown to create a dynamic
372 * #fr_dict_attr_t of #FR_TYPE_OCTETS.
373 *
374 * Which type of #fr_dict_attr_t the #fr_pair_t was created with can be determined by
375 * checking @verbatim vp->da->flags.is_unknown @endverbatim.
376 *
377 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
378 * @param[in] parent of the attribute being allocated (usually a dictionary or vendor).
379 * @param[in] attr number.
380 * @return
381 * - A new #fr_pair_t.
382 * - NULL on error.
383 */
384fr_pair_t *fr_pair_afrom_child_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
385{
386 fr_dict_attr_t const *da;
387 fr_pair_t *vp;
388
389 vp = fr_pair_alloc_null(ctx);
390 if (unlikely(!vp)) return NULL;
391
393 if (!da) {
394 fr_dict_attr_t *unknown;
395
397 if (!unknown) {
399 return NULL;
400 }
401 da = unknown;
402 }
403
406
407 return vp;
408}
409
410/** Create a pair (and all intermediate parents), and append it to the list
411 *
412 * Unlike fr_pair_afrom_da_nested(), this function starts off at an intermediate ctx and list.
413 *
414 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
415 * @param[out] list where the created pair is supposed to go.
416 * @param[in] da the da for the pair to create
417 * @param[in] start the starting depth. If start != 0, we must have ctx==vp at that depth, and list==&vp->vp_group
418 * @return
419 * - A new #fr_pair_t.
420 * - NULL on error.
421 */
422fr_pair_t *fr_pair_afrom_da_depth_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da, unsigned int start)
423{
424 fr_pair_t *vp;
425 unsigned int i;
426 TALLOC_CTX *cur_ctx;
427 fr_dict_attr_t const *find; /* DA currently being looked for */
428 fr_pair_list_t *cur_list; /* Current list being searched */
429 fr_da_stack_t da_stack;
430
431 /*
432 * Short-circuit the common case.
433 */
434 if (da->depth == (start + 1)) {
435 if (fr_pair_append_by_da(ctx, &vp, list, da) < 0) return NULL;
437 return vp;
438 }
439
440 fr_proto_da_stack_build(&da_stack, da);
441 cur_list = list;
442 cur_ctx = ctx;
443
444 for (i = start; i <= da->depth; i++) {
445 find = da_stack.da[i];
446
447 vp = fr_pair_find_by_da(cur_list, NULL, find);
448 if (!vp || (vp->da == da)) {
449 if (fr_pair_append_by_da(cur_ctx, &vp, cur_list, find) < 0) return NULL;
451 }
452
453 if (find == da) return vp;
454
456
457 cur_ctx = vp;
458 cur_list = &vp->vp_group;
459 }
460
461 fr_assert(0);
462
463 return NULL;
464}
465
466/** Create a pair (and all intermediate parents), and append it to the list
467 *
468 * If the relevant leaf pair already exists, then a new one is created.
469 *
470 * This function is similar to fr_pair_update_by_da_parent(), except that function requires
471 * a parent pair, and this one takes a separate talloc ctx and pair list.
472 *
473 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
474 * @param[out] list where the created pair is supposed to go.
475 * @param[in] da the da for the pair to create
476 * @return
477 * - A new #fr_pair_t.
478 * - NULL on error.
479 */
481{
482 if (da->depth <= 1) {
483 fr_pair_t *vp;
484
485 if (fr_pair_append_by_da(ctx, &vp, list, da) < 0) return NULL;
487 return vp;
488 }
489
490 return fr_pair_afrom_da_depth_nested(ctx, list, da, 0);
491}
492
493/** Copy a single valuepair
494 *
495 * Allocate a new valuepair and copy the da from the old vp.
496 *
497 * @param[in] ctx for talloc
498 * @param[in] vp to copy.
499 * @return
500 * - A copy of the input VP.
501 * - NULL on error.
502 */
503fr_pair_t *fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
504{
505 fr_pair_t *n;
506
508
509 n = fr_pair_afrom_da(ctx, vp->da);
510 if (!n) return NULL;
511
512 n->op = vp->op;
514
515 /*
516 * Groups are special.
517 */
518 if (fr_type_is_structural(n->vp_type)) {
519 if (fr_pair_list_copy(n, &n->vp_group, &vp->vp_group) < 0) {
520 error:
521 talloc_free(n);
522 return NULL;
523 }
524
525 } else {
526 if (unlikely(fr_value_box_copy(n, &n->data, &vp->data) < 0)) goto error;
527 }
528
529 return n;
530}
531
532/** Steal one VP
533 *
534 * @param[in] ctx to move fr_pair_t into
535 * @param[in] vp fr_pair_t to move into the new context.
536 */
537int fr_pair_steal(TALLOC_CTX *ctx, fr_pair_t *vp)
538{
539 fr_pair_t *nvp;
540
541 nvp = talloc_steal(ctx, vp);
542 if (unlikely(!nvp)) {
543 fr_strerror_printf("Failed moving pair %pV to new ctx", vp);
544 return -1;
545 }
546
547 return 0;
548}
549
550#define IN_A_LIST_MSG "Pair %pV is already in a list, and cannot be moved"
551#define NOT_IN_THIS_LIST_MSG "Pair %pV is not in the given list"
552
553/** Change a vp's talloc ctx and insert it into a new list
554 *
555 * @param[in] list_ctx to move vp into.
556 * @param[out] list to add vp to.
557 * @param[in] vp to move.
558 * @return
559 * - 0 on success.
560 * - -1 on failure (already in list).
561 */
562int fr_pair_steal_append(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
563{
564 if (fr_pair_order_list_in_a_list(vp)) {
566 return -1;
567 }
568
569 if (unlikely(fr_pair_steal(list_ctx, vp) < 0)) return -1;
570
571 if (unlikely(fr_pair_append(list, vp) < 0)) return -1;
572
573 return 0;
574}
575
576/** Change a vp's talloc ctx and insert it into a new list
577 *
578 * @param[in] list_ctx to move vp into.
579 * @param[out] list to add vp to.
580 * @param[in] vp to move.
581 * @return
582 * - 0 on success.
583 * - -1 on failure (already in list).
584 */
585int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
586{
587 if (fr_pair_order_list_in_a_list(vp)) {
589 return -1;
590 }
591
592 if (unlikely(fr_pair_steal(list_ctx, vp) < 0)) return -1;
593
594 if (unlikely(fr_pair_prepend(list, vp) < 0)) return -1;
595
596 return 0;
597}
598
599/** Mark malformed attribute as raw
600 *
601 * @param[in] vp to mark as raw.
602 * @param[in] data to parse.
603 * @param[in] data_len of data to parse.
604 *
605 * @return
606 * - 0 on success
607 * - -1 on failure.
608 */
609int fr_pair_raw_afrom_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len)
610{
611 fr_dict_attr_t *unknown;
612
614
615 if (!fr_cond_assert(vp->da->flags.is_unknown == false)) return -1;
616
617 if (!fr_cond_assert(vp->da->parent != NULL)) return -1;
618
620 if (!unknown) return -1;
621
622 fr_value_box_clear(&vp->data);
623
624 vp->da = unknown;
625 fr_assert(vp->da->type == FR_TYPE_OCTETS);
626
627 return fr_value_box_memdup(vp, &vp->data, NULL, data, data_len, true);
628}
629
630/** Iterate over pairs with a specified da
631 *
632 * @param[in] cursor to iterate over
633 * @param[in] current The fr_pair_t cursor->current. Will be advanced and checked to
634 * see if it matches the specified fr_dict_attr_t.
635 * @param[in] uctx The fr_dict_attr_t to search for.
636 * @return
637 * - Next matching fr_pair_t.
638 * - NULL if not more matching fr_pair_ts could be found.
639 */
640static void *fr_pair_iter_next_by_da(fr_dcursor_t *cursor, void *current, void *uctx)
641{
642 fr_pair_t *c = current;
643 fr_dict_attr_t *da = uctx;
644
645 while ((c = fr_dlist_next(cursor->dlist, c))) {
646 PAIR_VERIFY(c);
647 if (c->da == da) break;
648 }
649
650 return c;
651}
652
653/** Iterate over pairs which are decedents of the specified da
654 *
655 * @param[in] cursor to iterate over.
656 * @param[in] current The fr_pair_t cursor->current. Will be advanced and checked to
657 * see if it matches the specified fr_dict_attr_t.
658 * @param[in] uctx The fr_dict_attr_t to search for.
659 * @return
660 * - Next matching fr_pair_t.
661 * - NULL if not more matching fr_pair_ts could be found.
662 */
663static void *fr_pair_iter_next_by_ancestor(fr_dcursor_t *cursor, void *current, void *uctx)
664{
665 fr_pair_t *c = current;
666 fr_dict_attr_t *da = uctx;
667
668 while ((c = fr_dlist_next(cursor->dlist, c))) {
669 PAIR_VERIFY(c);
670 if (fr_dict_attr_common_parent(da, c->da, true)) break;
671 }
672
673 return c;
674}
675
676/** Return the number of instances of a given da in the specified list
677 *
678 * @param[in] list to search in.
679 * @param[in] da to look for in the list.
680 * @return
681 * - 0 if no instances exist.
682 * - >0 the number of instance of a given attribute.
683 */
684unsigned int fr_pair_count_by_da(fr_pair_list_t const *list, fr_dict_attr_t const *da)
685{
686 fr_pair_t *vp = NULL;
687 unsigned int count = 0;
688
689 if (fr_pair_list_empty(list)) return 0;
690
691 while ((vp = fr_pair_list_next(list, vp))) if (da == vp->da) count++;
692
693 return count;
694}
695
696/** Find the first pair with a matching da
697 *
698 * @param[in] list to search in.
699 * @param[in] prev the previous attribute in the list.
700 * @param[in] da the next da to find.
701 * @return
702 * - first matching fr_pair_t.
703 * - NULL if no fr_pair_ts match.
704 *
705 * @hidecallergraph
706 */
708{
709 fr_pair_t *vp = UNCONST(fr_pair_t *, prev);
710
711 if (fr_pair_list_empty(list)) return NULL;
712
713 PAIR_LIST_VERIFY(list);
714
715 while ((vp = fr_pair_list_next(list, vp))) if (da == vp->da) return vp;
716
717 return NULL;
718}
719
720/** Find the last pair with a matching da
721 *
722 * @param[in] list to search in.
723 * @param[in] prev the previous attribute in the list.
724 * @param[in] da the previous da to find.
725 * @return
726 * - first matching fr_pair_t.
727 * - NULL if no fr_pair_ts match.
728 *
729 * @hidecallergraph
730 */
732{
733 fr_pair_t *vp = UNCONST(fr_pair_t *, prev);
734
735 if (fr_pair_list_empty(list)) return NULL;
736
737 PAIR_LIST_VERIFY(list);
738
739 while ((vp = fr_pair_list_prev(list, vp))) if (da == vp->da) return vp;
740
741 return NULL;
742}
743
744/** Find a pair with a matching da at a given index
745 *
746 * @param[in] list to search in.
747 * @param[in] da to look for in the list.
748 * @param[in] idx Instance of the attribute to return.
749 * @return
750 * - first matching fr_pair_t.
751 * - NULL if no fr_pair_ts match.
752 *
753 * @hidecallergraph
754 */
755fr_pair_t *fr_pair_find_by_da_idx(fr_pair_list_t const *list, fr_dict_attr_t const *da, unsigned int idx)
756{
757 fr_pair_t *vp = NULL;
758
759 if (fr_pair_list_empty(list)) return NULL;
760
761 PAIR_LIST_VERIFY(list);
762
763 while ((vp = fr_pair_list_next(list, vp))) {
764 if (da != vp->da) continue;
765
766 if (idx == 0) return vp;
767
768 idx--;
769 }
770 return NULL;
771}
772
773/** Find a pair with a matching fr_dict_attr_t, by walking the nested fr_dict_attr_t tree
774 *
775 * The list should be the one containing the top level attributes.
776 *
777 * @param[in] list to search in.
778 * @param[in] prev pair to start searching from.
779 * @param[in] da the next da to find.
780 * @return
781 * - first matching fr_pair_t.
782 * - NULL if no fr_pair_ts match.
783 */
785{
786 fr_pair_t *vp;
787 fr_dict_attr_t const **find; /* DA currently being looked for */
788 fr_pair_list_t const *cur_list; /* Current list being searched */
789 fr_da_stack_t da_stack;
790
791 if (fr_pair_list_empty(list)) return NULL;
792
793 /*
794 * In the common case, we're looking for attributes in
795 * the root (at level 1), so we just skip to a special
796 * function for that
797 */
798 if (da->depth <= 1) return fr_pair_find_by_da(list, prev, da);
799
800 fr_proto_da_stack_build(&da_stack, da);
801
802 /*
803 * Find the relevant starting point for `prev`
804 */
805 if (prev) {
806 cur_list = fr_pair_parent_list(prev);
807 find = &da_stack.da[prev->da->depth - 1];
808 vp = UNCONST(fr_pair_t *, prev);
809 } else {
810 cur_list = list;
811 find = &da_stack.da[0];
812 vp = NULL;
813 }
814
815 /*
816 * Loop over the list at each level until we find a matching da.
817 */
818 while (true) {
819 fr_pair_t *next;
820
821 fr_assert((*find)->depth <= da->depth);
822
823 /*
824 * Find a vp which matches a given da. If found,
825 * recurse into the child list to find the child
826 * attribute.
827 *
828 */
829 next = fr_pair_find_by_da(cur_list, vp, *find);
830 if (next) {
831 /*
832 * We've found a match for the requested
833 * da - return it.
834 */
835 if ((*find) == da) return next;
836
837 /*
838 * Prepare to search the next level.
839 */
840 cur_list = &next->vp_group;
841 find++;
842 vp = NULL;
843 continue;
844 }
845
846 /*
847 * We hit the end of the top-level list. Therefore we found nothing.
848 */
849 if (cur_list == list) break;
850
851 /*
852 * We hit the end of *A* list. Go to the parent
853 * VP, and then find its list.
854 *
855 * We still then have to go to the next attribute
856 * in the parent list, as we've checked all of the
857 * children of this VP.
858 */
859 find--;
860 vp = fr_pair_list_parent(cur_list);
861 cur_list = fr_pair_parent_list(vp);
862 }
863
864 /*
865 * Compatibility with flat attributes
866 */
867 if (fr_pair_parent_list(prev) != list) prev = NULL;
868 return fr_pair_find_by_da(list, prev, da);
869}
870
871/** Find the pair with the matching child attribute
872 *
873 * @param[in] list in which to search.
874 * @param[in] prev attribute to start search from.
875 * @param[in] parent attribute in which to lookup child.
876 * @param[in] attr id of child.
877 * @return
878 * - first matching value pair.
879 * - NULL if no pair found.
880 */
882 fr_dict_attr_t const *parent, unsigned int attr)
883{
884 fr_dict_attr_t const *da;
885
886 /* List head may be NULL if it contains no VPs */
887 if (fr_pair_list_empty(list)) return NULL;
888
889 PAIR_LIST_VERIFY(list);
890
892 if (!da) return NULL;
893
894 return fr_pair_find_by_da(list, prev, da);
895}
896
897/** Find the pair with the matching child attribute at a given index
898 *
899 * @param[in] list in which to search.
900 * @param[in] parent attribute in which to lookup child.
901 * @param[in] attr id of child.
902 * @param[in] idx Instance of the attribute to return.
903 * @return
904 * - first matching value pair.
905 * - NULL if no pair found.
906 */
908 fr_dict_attr_t const *parent, unsigned int attr, unsigned int idx)
909{
910 fr_dict_attr_t const *da;
911
912 /* List head may be NULL if it contains no VPs */
913 if (fr_pair_list_empty(list)) return NULL;
914
915 PAIR_LIST_VERIFY(list);
916
918 if (!da) return NULL;
919
920 return fr_pair_find_by_da_idx(list, da, idx);
921}
922
923/** Get the child list of a group
924 *
925 * @param[in] vp which MUST be of a type
926 * that can contain children.
927 * @return
928 * - NULL on error
929 * - pointer to head of the child list.
930 */
932{
933 if (!fr_type_is_structural(vp->vp_type)) return NULL;
934
935 return &vp->vp_group;
936}
937
938/** Return a pointer to the parent pair list
939 *
940 */
942{
943 FR_TLIST_HEAD(fr_pair_order_list) *parent;
944
945 if (!vp) return NULL;
946
947 parent = fr_pair_order_list_parent(vp);
948 if (!parent) return NULL;
949
950 return (fr_pair_list_t *) (UNCONST(uint8_t *, parent) - offsetof(fr_pair_list_t, order));
951}
952
953/** Return a pointer to the parent pair.
954 *
955 */
957{
959
960 if (!list) return NULL;
961
962 if (!list->is_child) return NULL;
963
964 return (fr_pair_t *) (UNCONST(uint8_t *, list) - offsetof(fr_pair_t, vp_group));
965}
966
967/** Return a pointer to the parent pair which contains this list.
968 *
969 */
971{
972 if (!list) return NULL;
973
974 if (!list->is_child) return NULL;
975
976 return (fr_pair_t *) (UNCONST(uint8_t *, list) - offsetof(fr_pair_t, vp_group));
977}
978
979/** Keep attr tree and sublists synced on cursor insert
980 *
981 * @param[in] cursor the cursor being modified
982 * @param[in] to_insert fr_pair_t being inserted.
983 * @param[in] uctx fr_pair_list_t containing the order list.
984 * @return
985 * - 0 on success.
986 */
987static int _pair_list_dcursor_insert(fr_dcursor_t *cursor, void *to_insert, UNUSED void *uctx)
988{
989 fr_pair_t *vp = to_insert;
990 fr_tlist_head_t *tlist;
991
992 tlist = fr_tlist_head_from_dlist(cursor->dlist);
993
994 /*
995 * Mark the pair as inserted into the list.
996 */
997 fr_pair_order_list_set_head(tlist, vp);
998
1000
1001 return 0;
1002}
1003
1004/** Keep attr tree and sublists synced on cursor removal
1005 *
1006 * @param[in] cursor the cursor being modified
1007 * @param[in] to_remove fr_pair_t being removed.
1008 * @param[in] uctx fr_pair_list_t containing the order list.
1009 * @return
1010 * - 0 on success.
1011 */
1012static int _pair_list_dcursor_remove(NDEBUG_UNUSED fr_dcursor_t *cursor, void *to_remove, UNUSED void *uctx)
1013{
1014 fr_pair_t *vp = to_remove;
1016
1017#ifndef NDEBUG
1018 fr_tlist_head_t *tlist;
1019
1020 tlist = fr_tlist_head_from_dlist(cursor->dlist);
1021
1022 while (parent && (tlist != vp->order_entry.entry.list_head)) {
1023 tlist = &parent->order.head;
1025 }
1026
1027 fr_assert(vp->order_entry.entry.list_head == tlist);
1029#endif
1030
1031 /*
1032 * Mark the pair as removed from the list.
1033 */
1034 fr_pair_order_list_set_head(NULL, vp);
1035
1036 PAIR_VERIFY(vp);
1037
1038 if (&parent->order.head.dlist_head == cursor->dlist) return 0;
1039
1041 return 1;
1042}
1043
1044/** Iterates over the leaves of a list
1045 *
1046 * @param[in] list to iterate over.
1047 * @param[in] vp the current CVP
1048 * @return
1049 * - NULL when done
1050 * - vp - a leaf pair
1051 */
1053{
1054 fr_pair_t *next, *parent;
1055 fr_pair_list_t *parent_list;
1056
1057 /*
1058 * Start: return the head of the top-level list.
1059 */
1060 if (!vp) {
1061 vp = fr_pair_list_head(list);
1062 if (!vp) goto next_parent_sibling;
1063
1064 next_sibling:
1065 if (fr_type_is_leaf(vp->vp_type)) return vp;
1066
1068
1069 vp = fr_pair_list_iter_leaf(&vp->vp_group, NULL);
1070 if (vp) return vp;
1071
1072 /*
1073 * vp is NULL, so we've processed all of its children.
1074 */
1075 }
1076
1077 /*
1078 * Go to the next sibling in the parent list of vp.
1079 */
1080next_parent_sibling:
1081 parent_list = fr_pair_parent_list(vp);
1082 if (!parent_list) return NULL;
1083
1084 next = fr_pair_list_next(parent_list, vp);
1085 if (!next) {
1086 /*
1087 * We're done the top-level list.
1088 */
1089 if (parent_list == list) return NULL;
1090
1092 fr_assert(&parent->vp_group == parent_list);
1093 vp = parent;
1094 goto next_parent_sibling;
1095 }
1096
1097 /*
1098 * We do have a "next" attribute. Go check if we can return it.
1099 */
1100 vp = next;
1101 goto next_sibling;
1102}
1103
1104/** Initialises a special dcursor with callbacks that will maintain the attr sublists correctly
1105 *
1106 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1107 *
1108 * @param[out] cursor to initialise.
1109 * @param[in] list to iterate over.
1110 * @param[in] iter Iterator to use when filtering pairs.
1111 * @param[in] uctx To pass to iterator.
1112 * @param[in] is_const whether the fr_pair_list_t is const.
1113 * @return
1114 * - NULL if src does not point to any items.
1115 * - The first pair in the list.
1116 */
1118 fr_dcursor_iter_t iter, void const *uctx,
1119 bool is_const)
1120{
1121 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1122 iter, NULL, uctx,
1124}
1125
1126/** Initialises a special dcursor with callbacks that will maintain the attr sublists correctly
1127 *
1128 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1129 *
1130 * @param[out] cursor to initialise.
1131 * @param[in] list to iterate over.
1132 * @param[in] is_const whether the fr_pair_list_t is const.
1133 * @return
1134 * - NULL if src does not point to any items.
1135 * - The first pair in the list.
1136 */
1138 bool is_const)
1139{
1140 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1141 NULL, NULL, NULL,
1143}
1144
1145/** Initialise a cursor that will return only attributes matching the specified #fr_dict_attr_t
1146 *
1147 * @param[in] cursor to initialise.
1148 * @param[in] list to iterate over.
1149 * @param[in] da to search for.
1150 * @param[in] is_const whether the fr_pair_list_t is const.
1151 * @return
1152 * - The first matching pair.
1153 * - NULL if no pairs match.
1154 */
1156 fr_pair_list_t const *list, fr_dict_attr_t const *da,
1157 bool is_const)
1158{
1159 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1160 fr_pair_iter_next_by_da, NULL, da,
1162}
1163
1164/** Initialise a cursor that will return only attributes descended from the specified #fr_dict_attr_t
1165 *
1166 * @param[in] cursor to initialise.
1167 * @param[in] list to iterate over.
1168 * @param[in] da who's descendants to search for.
1169 * @param[in] is_const whether the fr_pair_list_t is const.
1170 * @return
1171 * - The first matching pair.
1172 * - NULL if no pairs match.
1173 */
1175 fr_pair_list_t const *list, fr_dict_attr_t const *da,
1176 bool is_const)
1177{
1178 fr_pair_t *vp;
1179
1181
1182 /*
1183 * This function is only used by snmp.c and password.c. Once we've fully moved to
1184 * nested attributes, it should be removed.
1185 */
1186 fr_assert(da->parent->flags.is_root);
1187
1188 vp = fr_pair_find_by_da(list, NULL, da);
1189 if (vp) {
1190 list = &vp->vp_group;
1191
1192 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1193 NULL, NULL, NULL,
1195 }
1196
1197 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1200}
1201
1202/** Iterate over pairs
1203 *
1204 * @param[in] cursor to iterate over.
1205 * @param[in] current The fr_value_box_t cursor->current. Will be advanced and checked to
1206 * see if it matches the specified fr_dict_attr_t.
1207 * @param[in] uctx unused
1208 * @return
1209 * - Next matching fr_pair_t.
1210 * - NULL if not more matching fr_pair_ts could be found.
1211 */
1212static void *_fr_pair_iter_next_value(fr_dcursor_t *cursor, void *current, UNUSED void *uctx)
1213{
1214 fr_pair_t *vp;
1215
1216 if (!current) {
1217 vp = NULL;
1218 } else {
1219 vp = (fr_pair_t *) ((uint8_t *) current - offsetof(fr_pair_t, data));
1220 PAIR_VERIFY(vp);
1221 }
1222
1223 while ((vp = fr_dlist_next(cursor->dlist, vp))) {
1224 PAIR_VERIFY(vp);
1225 if (fr_type_is_leaf(vp->vp_type)) return &vp->data;
1226 }
1227
1228 return NULL;
1229}
1230
1231/*
1232 * The value dcursor just runs the iterator, and never uses the dlist. Inserts and deletes are forbidden.
1233 *
1234 * However, the underlying dcursor code needs a dlist, so we create a fake one to pass it. In debug
1235 * builds, the dcursor code will do things like try to check talloc types. So we need to pass it an
1236 * empty dlist with no talloc types.
1237 */
1239 .offset = offsetof(fr_dlist_head_t, entry),
1240 .type = NULL,
1241 .num_elements = 0,
1242 .entry = {
1243 .prev = &value_dlist.entry,
1245 },
1246};
1247
1248/** Initialises a special dcursor over a #fr_pair_list_t, but which returns #fr_value_box_t
1249 *
1250 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1251 * @note - the list cannot be modified, and structural attributes are not returned.
1252 *
1253 * @param[out] cursor to initialise.
1254 * @return
1255 * - NULL if src does not point to any items.
1256 * - The first pair in the list.
1257 */
1259{
1260 return _fr_dcursor_init(cursor, &value_dlist,
1261 _fr_pair_iter_next_value, NULL, NULL, NULL, NULL, NULL, true);
1262}
1263
1264/** Iterate over pairs
1265 *
1266 * @param[in] cursor to iterate over.
1267 * @param[in] current The fr_value_box_t cursor->current. Will be advanced and checked to
1268 * see if it matches the specified fr_dict_attr_t.
1269 * @param[in] uctx The parent dcursor
1270 * @return
1271 * - Next matching fr_pair_t.
1272 * - NULL if not more matching fr_pair_ts could be found.
1273 */
1274static void *_fr_pair_iter_next_dcursor_value(UNUSED fr_dcursor_t *cursor, void *current, void *uctx)
1275{
1276 fr_pair_t *vp;
1277 fr_dcursor_t *parent = uctx;
1278
1279 if (!current) {
1281 if (!vp) return NULL;
1282 goto check;
1283 }
1284
1285 while ((vp = fr_dcursor_next(parent))) {
1286 check:
1287 PAIR_VERIFY(vp);
1288
1289 if (fr_type_is_leaf(vp->vp_type)) return &vp->data;
1290 }
1291
1292 return NULL;
1293}
1294
1295/** Initialises a special dcursor over another cursor which returns #fr_pair_t, but we return #fr_value_box_t
1296 *
1297 * @note - the list cannot be modified, and structural attributes are not returned.
1298 *
1299 * @param[out] cursor to initialise.
1300 * @param[in] parent to iterate over
1301 * @return
1302 * - NULL if src does not point to any items.
1303 * - The first pair in the list.
1304 */
1310
1311/** Add a VP to the start of the list.
1312 *
1313 * Links an additional VP 'add' at the beginning a list.
1314 *
1315 * @param[in] list VP in linked list. Will add new VP to this list.
1316 * @param[in] to_add VP to add to list.
1317 * @return
1318 * - 0 on success.
1319 * - -1 on failure (pair already in list).
1320 */
1322{
1323 PAIR_VERIFY(to_add);
1324
1325#ifdef WITH_VERIFY_PTR
1326 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1327 list->verified = false;
1328#endif
1329
1330 if (fr_pair_order_list_in_a_list(to_add)) {
1332 return -1;
1333 }
1334
1335 fr_pair_order_list_insert_head(&list->order, to_add);
1336
1337 return 0;
1338}
1339
1340/** Add a VP to the end of the list.
1341 *
1342 * Links an additional VP 'to_add' at the end of a list.
1343 *
1344 * @param[in] list VP in linked list. Will add new VP to this list.
1345 * @param[in] to_add VP to add to list.
1346 * @return
1347 * - 0 on success.
1348 * - -1 on failure (pair already in list).
1349 *
1350 * @hidecallergraph
1351 */
1353{
1354#ifdef WITH_VERIFY_PTR
1355 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1356 list->verified = false;
1357#endif
1358
1359 if (fr_pair_order_list_in_a_list(to_add)) {
1361 return -1;
1362 }
1363
1364 fr_pair_order_list_insert_tail(&list->order, to_add);
1365
1366 return 0;
1367}
1368
1369/** Add a VP after another VP.
1370 *
1371 * @param[in] list VP in linked list. Will add new VP to this list.
1372 * @param[in] pos to insert pair after.
1373 * @param[in] to_add VP to add to list.
1374 * @return
1375 * - 0 on success.
1376 * - -1 on failure (pair already in list).
1377 */
1379{
1380 PAIR_VERIFY(to_add);
1381
1382#ifdef WITH_VERIFY_PTR
1383 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1384 list->verified = false;
1385#endif
1386
1387 if (fr_pair_order_list_in_a_list(to_add)) {
1389 return -1;
1390 }
1391
1392 if (pos && !fr_pair_order_list_in_list(&list->order, pos)) {
1394 return -1;
1395 }
1396
1397 fr_pair_order_list_insert_after(&list->order, pos, to_add);
1398
1399 return 0;
1400}
1401
1402/** Add a VP before another VP.
1403 *
1404 * @param[in] list VP in linked list. Will add new VP to this list.
1405 * @param[in] pos to insert pair after.
1406 * @param[in] to_add VP to add to list.
1407 * @return
1408 * - 0 on success.
1409 * - -1 on failure (pair already in list).
1410 */
1412{
1413 PAIR_VERIFY(to_add);
1414
1415#ifdef WITH_VERIFY_PTR
1416 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1417 fr_assert(!pos || fr_pair_order_list_in_a_list(pos));
1418 list->verified = false;
1419#endif
1420
1421 if (fr_pair_order_list_in_a_list(to_add)) {
1423 return -1;
1424 }
1425
1426 if (pos && !fr_pair_order_list_in_list(&list->order, pos)) {
1428 return -1;
1429 }
1430
1431 fr_pair_order_list_insert_before(&list->order, pos, to_add);
1432
1433 return 0;
1434}
1435
1436/** Replace a given VP
1437 *
1438 * @note Memory used by the VP being replaced will be freed.
1439 *
1440 * @param[in,out] list pair list
1441 * @param[in] to_replace pair to replace and free, on list
1442 * @param[in] vp New pair to insert.
1443 */
1445{
1446 PAIR_VERIFY_WITH_LIST(list, to_replace);
1447 PAIR_VERIFY(vp);
1448
1449#ifdef WITH_VERIFY_PTR
1450 fr_assert(!fr_pair_order_list_in_a_list(vp));
1451 fr_assert(fr_pair_order_list_in_a_list(to_replace));
1452 list->verified = false;
1453#endif
1454
1455 fr_pair_insert_after(list, to_replace, vp);
1456 fr_pair_remove(list, to_replace);
1457 talloc_free(to_replace);
1458}
1459
1460/** Alloc a new fr_pair_t (and append)
1461 *
1462 * @param[in] ctx to allocate new #fr_pair_t in.
1463 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1464 * care about manipulating the fr_pair_t.
1465 * @param[in,out] list in which to append the pair.
1466 * @param[in] da of attribute to create.
1467 * @return
1468 * - 0 on success.
1469 * - -1 on failure.
1470 */
1471int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
1472{
1473 fr_pair_t *vp;
1474
1475 vp = fr_pair_afrom_da(ctx, da);
1476 if (unlikely(!vp)) {
1477 if (out) *out = NULL;
1478 return -1;
1479 }
1480
1481 fr_pair_append(list, vp);
1482 if (out) *out = vp;
1483
1484 return 0;
1485}
1486
1487/** Alloc a new fr_pair_t (and prepend)
1488 *
1489 * @param[in] ctx to allocate new #fr_pair_t in.
1490 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1491 * care about manipulating the fr_pair_t.
1492 * @param[in,out] list in which to prepend the pair.
1493 * @param[in] da of attribute to create.
1494 * @return
1495 * - 0 on success.
1496 * - -1 on failure.
1497 */
1498int fr_pair_prepend_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
1499{
1500 fr_pair_t *vp;
1501
1502 vp = fr_pair_afrom_da(ctx, da);
1503 if (unlikely(!vp)) {
1504 if (out) *out = NULL;
1505 return -1;
1506 }
1507
1508 fr_pair_prepend(list, vp);
1509 if (out) *out = vp;
1510
1511 return 0;
1512}
1513
1514/** Alloc a new fr_pair_t, adding the parent attributes if required
1515 *
1516 * A child pair will be added to the first available matching parent
1517 * found.
1518 *
1519 * @param[in] ctx to allocate new #fr_pair_t in
1520 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1521 * care about manipulating the fr_pair_t.
1522 * @param[in] list in which to insert the pair.
1523 * @param[in] da of the attribute to create.
1524 * @return
1525 * - 0 on success.
1526 * - -1 on failure.
1527 */
1529{
1530 fr_pair_t *vp = NULL;
1531 fr_da_stack_t da_stack;
1532 fr_dict_attr_t const **find;
1533 TALLOC_CTX *pair_ctx = ctx;
1534
1535 /*
1536 * Fast path for non-nested attributes
1537 */
1538 if (da->depth <= 1) return fr_pair_append_by_da(ctx, out, list, da);
1539
1540 fr_proto_da_stack_build(&da_stack, da);
1541 find = &da_stack.da[0];
1542
1543 /*
1544 * Walk down the da stack looking for candidate parent
1545 * attributes and then allocating the leaf.
1546 */
1547 while (true) {
1548 fr_assert((*find)->depth <= da->depth);
1549
1550 /*
1551 * We're not at the leaf, look for a potential parent
1552 */
1553 if ((*find) != da) vp = fr_pair_find_by_da(list, NULL, *find);
1554
1555 /*
1556 * Nothing found, create the pair
1557 */
1558 if (!vp) {
1559 if (fr_pair_append_by_da(pair_ctx, &vp, list, *find) < 0) {
1560 if (out) *out = NULL;
1561 return -1;
1562 }
1564 }
1565
1566 /*
1567 * We're at the leaf, return
1568 */
1569 if ((*find) == da) {
1570 if(out) *out = vp;
1571 return 0;
1572 }
1573
1574 /*
1575 * Prepare for next level
1576 */
1577 list = &vp->vp_group;
1578 pair_ctx = vp;
1579 vp = NULL;
1580 find++;
1581 }
1582}
1583
1584/** Return the first fr_pair_t matching the #fr_dict_attr_t or alloc a new fr_pair_t and its subtree (and append)
1585 *
1586 * @param[in] parent If parent->da is an ancestor of the specified
1587 * da, we continue building out the nested structure
1588 * from the parent.
1589 * If parent is NOT an ancestor, then it must be a group
1590 * attribute, and we will append the shallowest member
1591 * of the struct or TLV as a child, and build out everything
1592 * to the specified da.
1593 * @param[out] out Pair we allocated or found. May be NULL if the caller doesn't
1594 * care about manipulating the fr_pair_t.
1595 * @param[in] da of attribute to locate or alloc.
1596 * @return
1597 * - 1 if attribute already existed.
1598 * - 0 if we allocated a new attribute.
1599 * - -1 on memory allocation failure.
1600 * - -2 if the parent is not a group attribute.
1601 */
1603 fr_dict_attr_t const *da)
1604{
1605 fr_pair_t *vp = NULL;
1606 fr_da_stack_t da_stack;
1607 fr_dict_attr_t const **find; /* ** to allow us to iterate */
1608 TALLOC_CTX *pair_ctx = parent;
1609 fr_pair_list_t *list = &parent->vp_group;
1610
1611 /*
1612 * Fast path for non-nested attributes
1613 */
1614 if (da->depth <= 1) {
1615 vp = fr_pair_find_by_da(list, NULL, da);
1616 if (vp) {
1617 if (out) *out = vp;
1618 return 1;
1619 }
1620
1621 return fr_pair_append_by_da(parent, out, list, da);
1622 }
1623
1624 fr_proto_da_stack_build(&da_stack, da);
1625 /*
1626 * Is parent an ancestor of the attribute we're trying
1627 * to build? If so, we resume from the deepest pairs
1628 * already created.
1629 *
1630 * da stack excludes the root.
1631 */
1632 if ((parent->da->depth < da->depth) && (da_stack.da[parent->da->depth - 1] == parent->da)) {
1633 /*
1634 * Start our search from the parent's children
1635 */
1636 list = &parent->vp_group;
1637 find = &da_stack.da[parent->da->depth]; /* Next deepest attr than parent */
1638 /*
1639 * Disallow building one TLV tree into another
1640 */
1641 } else if (!fr_type_is_group(parent->da->type)) {
1642 fr_strerror_printf("Expected parent \"%s\" to be an ancestor of \"%s\" or a group. "
1643 "But it is not an ancestor and is of type %s", parent->da->name, da->name,
1644 fr_type_to_str(parent->da->type));
1645 return -2;
1646 } else {
1647 find = &da_stack.da[0];
1648 }
1649
1650 /*
1651 * Walk down the da stack looking for candidate parent
1652 * attributes and then allocating the leaf, and any
1653 * attributes between the leaf and parent.
1654 */
1655 while (true) {
1656 fr_assert((*find)->depth <= da->depth);
1657
1658 vp = fr_pair_find_by_da(list, NULL, *find);
1659 /*
1660 * Nothing found at this level, create the pair
1661 */
1662 if (!vp) {
1663 if (fr_pair_append_by_da(pair_ctx, &vp, list, *find) < 0) {
1664 if (out) *out = NULL;
1665 return -1;
1666 }
1668 }
1669
1670 /*
1671 * We're at the leaf, return
1672 */
1673 if ((*find) == da) {
1674 if (out) *out = vp;
1675 return 0;
1676 }
1677
1678 /*
1679 * Prepare for next level
1680 */
1681 list = &vp->vp_group;
1682 pair_ctx = vp;
1683 vp = NULL;
1684 find++;
1685 }
1686}
1687
1688/** Delete matching pairs from the specified list
1689 *
1690 * @param[in,out] list to search for attributes in or delete attributes from.
1691 * @param[in] da to match.
1692 * @return
1693 * - >0 the number of pairs deleted.
1694 * - 0 if no pairs were deleted.
1695 */
1697{
1698 int cnt = 0;
1699
1700 fr_pair_list_foreach(list, vp) {
1701 if (da == vp->da) {
1702 if (fr_pair_immutable(vp)) continue;
1703
1704 cnt++;
1705 fr_pair_delete(list, vp);
1706 }
1707 }
1708
1709 return cnt;
1710}
1711
1712/** Delete matching pairs from the specified list, and prune any empty branches
1713 *
1714 * @param[in,out] list to search for attributes in or delete attributes from.
1715 * @param[in] da to match.
1716 * @return
1717 * - >0 the number of pairs deleted.
1718 * - 0 if no pairs were deleted.
1719 */
1721{
1722 int cnt = 0;
1723 fr_pair_t *vp;
1724 fr_dict_attr_t const **find; /* DA currently being looked for */
1725 fr_pair_list_t *cur_list; /* Current list being searched */
1726 fr_da_stack_t da_stack;
1727
1728 /*
1729 * Fast path for non-nested attributes
1730 */
1731 if (da->depth <= 1) return fr_pair_delete_by_da(list, da);
1732
1733 /*
1734 * No pairs, fast path!
1735 */
1736 if (fr_pair_list_empty(list)) return 0;
1737
1738 /*
1739 * Similar to fr_pair_find_by_da_nested()
1740 */
1741 fr_proto_da_stack_build(&da_stack, da);
1742 cur_list = list;
1743 find = &da_stack.da[0];
1744 vp = NULL;
1745
1746 /*
1747 * Loop over the list at each level until we find a matching da.
1748 */
1749 while (true) {
1750 fr_pair_t *next;
1751
1752 fr_assert((*find)->depth <= da->depth);
1753
1754 /*
1755 * Find a vp which matches a given da. If found,
1756 * recurse into the child list to find the child
1757 * attribute.
1758 *
1759 */
1760 next = fr_pair_find_by_da(cur_list, vp, *find);
1761 if (next) {
1762 /*
1763 * We've found a match for the requested
1764 * da - delete it
1765 */
1766 if ((*find) == da) {
1767 do {
1768 fr_pair_delete(cur_list, next);
1769 cnt++;
1770 } while ((next = fr_pair_find_by_da(cur_list, vp, *find)) != NULL);
1771
1772 return cnt;
1773 }
1774
1775 /*
1776 * Prepare to search the next level.
1777 */
1778 cur_list = &next->vp_group;
1779 find++;
1780 vp = NULL;
1781 continue;
1782 }
1783
1784 /*
1785 * We hit the end of the top-level list. Therefore we found nothing.
1786 */
1787 if (cur_list == list) break;
1788
1789 /*
1790 * We hit the end of *A* list. Go to the parent
1791 * VP, and then find its list.
1792 *
1793 * We still then have to go to the next attribute
1794 * in the parent list, as we've checked all of the
1795 * children of this VP.
1796 */
1797 find--;
1798 vp = fr_pair_list_parent(cur_list);
1799 cur_list = fr_pair_parent_list(vp);
1800 }
1801
1802 return fr_pair_delete_by_da(list, da);
1803}
1804
1805/** Delete matching pairs from the specified list
1806 *
1807 * @param[in] list to delete attributes from.
1808 * @param[in] parent to match.
1809 * @param[in] attr to match.
1810 * @return
1811 * - >0 the number of pairs deleted.
1812 * - 0 if no pairs were delete.
1813 * - -1 if we couldn't resolve the attribute number.
1814 */
1816{
1817 fr_dict_attr_t const *da;
1818
1820 if (!da) return -1;
1821
1822 return fr_pair_delete_by_da(list, da);
1823}
1824
1825/** Remove fr_pair_t from a list and free
1826 *
1827 * @param[in] list of value pairs to remove VP from.
1828 * @param[in] vp to remove
1829 * @return
1830 * - <0 on error: pair wasn't deleted
1831 * - 0 on success
1832 */
1834{
1835 fr_pair_remove(list, vp);
1836 return talloc_free(vp);
1837}
1838
1839/** Order attributes by their da, and tag
1840 *
1841 * Useful where attributes need to be aggregated, but not necessarily
1842 * ordered by attribute number.
1843 *
1844 * @param[in] a first dict_attr_t.
1845 * @param[in] b second dict_attr_t.
1846 * @return
1847 * - +1 if a > b
1848 * - 0 if a == b
1849 * - -1 if a < b
1850 */
1851int8_t fr_pair_cmp_by_da(void const *a, void const *b)
1852{
1853 fr_pair_t const *my_a = a;
1854 fr_pair_t const *my_b = b;
1855
1856 PAIR_VERIFY(my_a);
1857 PAIR_VERIFY(my_b);
1858
1859 return CMP(my_a->da, my_b->da);
1860}
1861
1862/** Order attributes by their attribute number, and tag
1863 *
1864 * @param[in] a first dict_attr_t.
1865 * @param[in] b second dict_attr_t.
1866 * @return
1867 * - +1 if a > b
1868 * - 0 if a == b
1869 * - -1 if a < b
1870 */
1871static inline int8_t pair_cmp_by_num(void const *a, void const *b)
1872{
1873 int8_t ret;
1874 unsigned int i, min;
1875 fr_pair_t const *my_a = a;
1876 fr_pair_t const *my_b = b;
1877 fr_da_stack_t da_stack_a, da_stack_b;
1878
1879 PAIR_VERIFY(my_a);
1880 PAIR_VERIFY(my_b);
1881
1882 fr_proto_da_stack_build(&da_stack_a, my_a->da);
1883 fr_proto_da_stack_build(&da_stack_b, my_b->da);
1884
1885 if (da_stack_a.depth <= da_stack_b.depth) {
1886 min = da_stack_a.depth;
1887 } else {
1888 min = da_stack_b.depth;
1889 }
1890
1891 for (i = 0; i < min; i++) {
1892 ret = CMP(da_stack_a.da[i]->attr, da_stack_b.da[i]->attr);
1893 if (ret != 0) return ret;
1894 }
1895
1896 /*
1897 * Sort attributes of similar depth together.
1898 *
1899 * What we really want to do is to sort by entire parent da_stack.
1900 */
1901 ret = CMP(my_a->da->depth, my_b->da->depth);
1902 if (ret != 0) return ret;
1903
1904 /*
1905 * Attributes of the same depth get sorted by their parents.
1906 */
1907 ret = CMP(my_a->da->parent->attr, my_b->da->parent->attr);
1908 if (ret != 0) return ret;
1909
1910 /*
1911 * If the attributes have the same parent, they get sorted by number.
1912 */
1913 return CMP(my_a->da->attr, my_b->da->attr);
1914}
1915
1916/** Order attributes by their parent(s), attribute number, and tag
1917 *
1918 * Useful for some protocols where attributes of the same number should by aggregated
1919 * within a packet or container TLV.
1920 *
1921 * @param[in] a first dict_attr_t.
1922 * @param[in] b second dict_attr_t.
1923 * @return
1924 * - +1 if a > b
1925 * - 0 if a == b
1926 * - -1 if a < b
1927 */
1928int8_t fr_pair_cmp_by_parent_num(void const *a, void const *b)
1929{
1930 fr_pair_t const *vp_a = a;
1931 fr_pair_t const *vp_b = b;
1932 fr_dict_attr_t const *da_a = vp_a->da;
1933 fr_dict_attr_t const *da_b = vp_b->da;
1934 fr_da_stack_t da_stack_a;
1935 fr_da_stack_t da_stack_b;
1936 int8_t cmp;
1937 int i;
1938
1939 /*
1940 * Fast path (assuming attributes
1941 * are in the same dictionary).
1942 */
1943 if ((da_a->parent->flags.is_root) && (da_b->parent->flags.is_root)) return pair_cmp_by_num(vp_a, vp_b);
1944
1945 fr_proto_da_stack_build(&da_stack_a, da_a);
1946 fr_proto_da_stack_build(&da_stack_b, da_b);
1947
1948 for (i = 0; (da_a = da_stack_a.da[i]) && (da_b = da_stack_b.da[i]); i++) {
1949 cmp = CMP(da_a->attr, da_b->attr);
1950 if (cmp != 0) return cmp;
1951 }
1952
1953 /*
1954 * If a has a shallower attribute
1955 * hierarchy than b, it should come
1956 * before b.
1957 */
1958 return (da_a && !da_b) - (!da_a && da_b);
1959}
1960
1961/** Compare two pairs, using the operator from "a"
1962 *
1963 * i.e. given two attributes, it does:
1964 *
1965 * (b->data) (a->operator) (a->data)
1966 *
1967 * e.g. "foo" != "bar"
1968 *
1969 * @param[in] a the head attribute
1970 * @param[in] b the second attribute
1971 * @return
1972 * - 1 if true.
1973 * - 0 if false.
1974 * - -1 on failure.
1975 */
1976int fr_pair_cmp(fr_pair_t const *a, fr_pair_t const *b)
1977{
1978 if (!a) return -1;
1979
1980 PAIR_VERIFY(a);
1981 if (b) PAIR_VERIFY(b);
1982
1983 switch (a->op) {
1984 case T_OP_CMP_TRUE:
1985 return (b != NULL);
1986
1987 case T_OP_CMP_FALSE:
1988 return (b == NULL);
1989
1990 /*
1991 * a is a regex, compile it, print b to a string,
1992 * and then do string comparisons.
1993 */
1994 case T_OP_REG_EQ:
1995 case T_OP_REG_NE:
1996#ifndef HAVE_REGEX
1997 return -1;
1998#else
1999 if (!b) return false;
2000
2001 {
2002 ssize_t slen;
2003 regex_t *preg;
2004 char *value;
2005
2006 if (!fr_cond_assert(a->vp_type == FR_TYPE_STRING)) return -1;
2007
2008 slen = regex_compile(NULL, &preg, a->vp_strvalue, talloc_strlen(a->vp_strvalue),
2009 NULL, false, true);
2010 if (slen <= 0) {
2011 fr_strerror_printf_push("Error at offset %zd compiling regex for %s", -slen,
2012 a->da->name);
2013 return -1;
2014 }
2015 fr_pair_aprint(NULL, &value, NULL, b);
2016 if (!value) {
2017 talloc_free(preg);
2018 return -1;
2019 }
2020
2021 /*
2022 * Don't care about substring matches, oh well...
2023 */
2024 slen = regex_exec(preg, value, talloc_strlen(value), NULL);
2025 talloc_free(preg);
2027
2028 if (slen < 0) return -1;
2029 if (a->op == T_OP_REG_EQ) return (int)slen;
2030 return !slen;
2031 }
2032#endif
2033
2034 default: /* we're OK */
2035 if (!b) return false;
2036 break;
2037 }
2038
2039 return fr_pair_cmp_op(a->op, b, a);
2040}
2041
2042/** Determine equality of two lists
2043 *
2044 * This is useful for comparing lists of attributes inserted into a binary tree.
2045 *
2046 * @param a head list of #fr_pair_t.
2047 * @param b second list of #fr_pair_t.
2048 * @return
2049 * - -1 if a < b.
2050 * - 0 if the two lists are equal.
2051 * - 1 if a > b.
2052 * - -2 on error.
2053 */
2055{
2056 fr_pair_t *a_p, *b_p;
2057
2058 for (a_p = fr_pair_list_head(a), b_p = fr_pair_list_head(b);
2059 a_p && b_p;
2060 a_p = fr_pair_list_next(a, a_p), b_p = fr_pair_list_next(b, b_p)) {
2061 int ret;
2062
2063 /* Same VP, no point doing expensive checks */
2064 if (a_p == b_p) continue;
2065
2066 ret = CMP(a_p->da, b_p->da);
2067 if (ret != 0) return ret;
2068
2069 switch (a_p->vp_type) {
2070 case FR_TYPE_STRUCTURAL:
2071 ret = fr_pair_list_cmp(&a_p->vp_group, &b_p->vp_group);
2072 if (ret != 0) return ret;
2073 break;
2074
2075 default:
2076 ret = fr_value_box_cmp(&a_p->data, &b_p->data);
2077 if (ret != 0) {
2078 (void)fr_cond_assert(ret >= -1); /* Comparison error */
2079 return ret;
2080 }
2081 }
2082
2083 }
2084
2085 if (!a_p && !b_p) return 0;
2086 if (!a_p) return -1;
2087
2088 /* if(!b_p) */
2089 return 1;
2090}
2091
2092/** Write an error to the library errorbuff detailing the mismatch
2093 *
2094 * Retrieve output with fr_strerror();
2095 *
2096 * @todo add thread specific talloc contexts.
2097 *
2098 * @param failed pair of attributes which didn't match.
2099 */
2100void fr_pair_validate_debug(fr_pair_t const *failed[2])
2101{
2102 fr_pair_t const *filter = failed[0];
2103 fr_pair_t const *list = failed[1];
2104
2105 fr_strerror_clear(); /* Clear any existing messages */
2106
2107 if (!list) {
2108 if (!filter) {
2109 (void) fr_cond_assert(filter != NULL);
2110 return;
2111 }
2112 fr_strerror_printf("Attribute \"%s\" not found in list", filter->da->name);
2113 return;
2114 }
2115
2116 if (!filter || (filter->da != list->da)) {
2117 fr_strerror_printf("Attribute \"%s\" not found in filter", list->da->name);
2118 return;
2119 }
2120
2121 fr_strerror_printf("Attribute value: %pP didn't match filter: %pP", list, filter);
2122
2123 return;
2124}
2125
2126/** Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check
2127 *
2128 * @note will sort both filter and list in place.
2129 *
2130 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
2131 * May be NULL.
2132 * @param filter attributes to check list against.
2133 * @param list attributes, probably a request or reply
2134 */
2135bool fr_pair_validate(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
2136{
2137 fr_pair_t *check, *match;
2138
2139 if (fr_pair_list_empty(filter) && fr_pair_list_empty(list)) return true;
2140
2141 /*
2142 * This allows us to verify the sets of validate and reply are equal
2143 * i.e. we have a validate rule which matches every reply attribute.
2144 *
2145 * @todo this should be removed one we have sets and lists
2146 */
2149
2150 check = fr_pair_list_head(filter);
2151 match = fr_pair_list_head(list);
2152 while (match || check) {
2153 /*
2154 * Lists are of different lengths
2155 */
2156 if (!match || !check) goto mismatch;
2157
2158 /*
2159 * The lists are sorted, so if the head
2160 * attributes aren't of the same type, then we're
2161 * done.
2162 */
2163 if (!ATTRIBUTE_EQ(check, match)) goto mismatch;
2164
2165 /*
2166 * They're of the same type, but don't have the
2167 * same values. This is a problem.
2168 *
2169 * Note that the RFCs say that for attributes of
2170 * the same type, order is important.
2171 */
2172 switch (check->vp_type) {
2173 case FR_TYPE_STRUCTURAL:
2174 /*
2175 * Return from here on failure, so that the nested mismatch
2176 * information is preserved.
2177 */
2178 if (!fr_pair_validate(failed, &check->vp_group, &match->vp_group)) return false;
2179 break;
2180
2181 default:
2182 /*
2183 * This attribute passed the filter
2184 */
2185 if (!fr_pair_cmp(check, match)) goto mismatch;
2186 break;
2187 }
2188
2189 check = fr_pair_list_next(filter, check);
2190 match = fr_pair_list_next(list, match);
2191 }
2192
2193 return true;
2194
2195mismatch:
2196 if (failed) {
2197 failed[0] = check;
2198 failed[1] = match;
2199 }
2200 return false;
2201}
2202
2203/** Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check
2204 *
2205 * @note will sort both filter and list in place.
2206 *
2207 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
2208 * May be NULL.
2209 * @param filter attributes to check list against.
2210 * @param list attributes, probably a request or reply
2211 */
2213{
2214 fr_pair_t *last_check = NULL, *match = NULL;
2215
2216 if (fr_pair_list_empty(filter) && fr_pair_list_empty(list)) return true;
2217
2218 /*
2219 * This allows us to verify the sets of validate and reply are equal
2220 * i.e. we have a validate rule which matches every reply attribute.
2221 *
2222 * @todo this should be removed one we have sets and lists
2223 */
2226
2227 fr_pair_list_foreach(filter, check) {
2228 /*
2229 * Were processing check attributes of a new type.
2230 */
2231 if (!ATTRIBUTE_EQ(last_check, check)) {
2232 /*
2233 * Record the start of the matching attributes in the pair list
2234 * For every other operator we require the match to be present
2235 */
2236 while ((match = fr_pair_list_next(list, match))) {
2237 if (match->da == check->da) break;
2238 }
2239 if (!match) {
2240 if (check->op == T_OP_CMP_FALSE) continue;
2241 goto mismatch;
2242 }
2243
2244 last_check = check;
2245 } else {
2246 match = fr_pair_list_head(list);
2247 }
2248
2249 /*
2250 * Now iterate over all attributes of the same type.
2251 */
2252 for (;
2253 ATTRIBUTE_EQ(match, check);
2254 match = fr_pair_list_next(list, match)) {
2255 switch (check->vp_type) {
2256 case FR_TYPE_STRUCTURAL:
2257 if (!fr_pair_validate_relaxed(failed, &check->vp_group, &match->vp_group)) goto mismatch;
2258 break;
2259
2260 default:
2261 /*
2262 * This attribute passed the filter
2263 */
2264 if (!fr_pair_cmp(check, match)) {
2265 mismatch:
2266 if (failed) {
2267 failed[0] = check;
2268 failed[1] = match;
2269 }
2270 return false;
2271 }
2272 break;
2273 }
2274 }
2275 }
2276
2277 return true;
2278}
2279
2280/**
2281 *
2282 * @param[in] vp the pair to check
2283 * @return
2284 * - true the pair is immutable, or has an immutable child
2285 * - false the pair is not immutable, or has no immutable children.
2286 */
2288{
2289 if (fr_type_is_leaf(vp->vp_type)) return vp->vp_immutable;
2290
2292
2293 fr_pair_list_foreach(&vp->vp_group, child) {
2294 if (fr_type_is_leaf(child->vp_type)) {
2295 if (child->vp_immutable) return true;
2296
2297 continue;
2298 }
2299
2300 fr_assert(fr_type_is_structural(child->vp_type));
2301
2302 if (fr_pair_immutable(child)) return true;
2303 }
2304
2305 return false;
2306}
2307
2308/** Steal a list of pairs to a new context
2309 *
2310 */
2311void fr_pair_list_steal(TALLOC_CTX *ctx, fr_pair_list_t *list)
2312{
2313 fr_pair_list_foreach(list, vp) {
2314 (void) fr_pair_steal(ctx, vp);
2315 }
2316}
2317
2318/** Duplicate a list of pairs
2319 *
2320 * Copy all pairs from 'from' regardless of tag, attribute or vendor.
2321 *
2322 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2323 * @param[in] to where to copy attributes to.
2324 * @param[in] from whence to copy #fr_pair_t (s).
2325 * @return
2326 * - >0 the number of attributes copied.
2327 * - 0 if no attributes copied.
2328 * - -1 on error.
2329 */
2330int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
2331{
2332 fr_pair_t *new_vp, *first_added = NULL;
2333 int cnt = 0;
2334
2335 fr_pair_list_foreach(from, vp) {
2336 cnt++;
2338
2339 new_vp = fr_pair_copy(ctx, vp);
2340 if (!new_vp) {
2341 fr_pair_order_list_talloc_free_to_tail(&to->order, first_added);
2342 return -1;
2343 }
2344
2345 if (!first_added) first_added = new_vp;
2346 fr_pair_append(to, new_vp);
2347 }
2348
2349 return cnt;
2350}
2351
2352
2353/** Copy the contents of a pair list to a set of value-boxes
2354 *
2355 * This function should be removed when the xlats use dcursors
2356 * of copying all of the boxes.
2357 *
2358 * @param[in] dst where boxes will be created
2359 * @param[in] from whence to copy #fr_pair_t (s).
2360 * @return
2361 * - >0 the number of boxes copied.
2362 * - 0 if no boxes copied.
2363 * - -1 on error.
2364 */
2366{
2367 int cnt = 0;
2368 fr_value_box_t *value, *first_added = NULL;
2369
2370 fr_assert(dst->type == FR_TYPE_GROUP);
2371
2372 fr_pair_list_foreach(from, vp) {
2373 cnt++;
2375
2376 if (fr_type_is_structural(vp->vp_type)) {
2378 if (!value) goto fail;
2379
2380 if (fr_pair_list_copy_to_box(value, &vp->vp_group) < 0) {
2382 goto fail;
2383 }
2384
2385 } else {
2386 value = fr_value_box_alloc(dst, vp->vp_type, vp->da);
2387 if (!value) {
2388 fail:
2389 fr_value_box_list_talloc_free_to_tail(&dst->vb_group, first_added);
2390 return -1;
2391 }
2392 if (unlikely(fr_value_box_copy(value, value, &vp->data) < 0)) {
2394 goto fail;
2395 }
2396 }
2397
2398 if (!first_added) first_added = value;
2399 fr_value_box_list_insert_tail(&dst->vb_group, value);
2400 }
2401
2402 return cnt;
2403}
2404
2405/** Duplicate pairs in a list matching the specified da
2406 *
2407 * Copy all pairs from 'from' matching the specified da.
2408 *
2409 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2410 * @param[in] to where to copy attributes to.
2411 * @param[in] from whence to copy #fr_pair_t (s).
2412 * @param[in] da to match.
2413 * @param[in] count How many instances to copy.
2414 * Use 0 for all attributes.
2415 * @return
2416 * - >0 the number of attributes copied.
2417 * - 0 if no attributes copied.
2418 * - -1 on error.
2419 */
2421 fr_pair_list_t const *from, fr_dict_attr_t const *da, unsigned int count)
2422{
2423 fr_pair_t *vp, *new_vp, *first_added = NULL;
2424 unsigned int cnt = 0;
2425
2426 if (count == 0) count = UINT_MAX;
2427
2428 if (unlikely(!da)) {
2429 fr_strerror_printf("No search attribute provided");
2430 return -1;
2431 }
2432
2433 for (vp = fr_pair_list_head(from);
2434 vp && (cnt < count);
2435 vp = fr_pair_list_next(from, vp)) {
2437
2438 if (vp->da != da) continue;
2439
2440 cnt++;
2441 new_vp = fr_pair_copy(ctx, vp);
2442 if (!new_vp) {
2443 fr_pair_order_list_talloc_free_to_tail(&to->order, first_added);
2444 return -1;
2445 }
2446
2447 if (!first_added) first_added = new_vp;
2448 fr_pair_append(to, new_vp);
2449 }
2450
2451 return cnt;
2452}
2453
2454/** Duplicate pairs in a list where the da is a descendant of parent_da
2455 *
2456 * Copy all pairs from 'from' which are descendants of the specified 'parent_da'.
2457 * This is particularly useful for copying attributes of a particular vendor, where the vendor
2458 * da is passed as parent_da.
2459 *
2460 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2461 * @param[in] to where to copy attributes to.
2462 * @param[in] from whence to copy #fr_pair_t (s).
2463 * @param[in] parent_da to match.
2464 * @return
2465 * - >0 one or more attributes were copied
2466 * - 0 if no attributes copied.
2467 * - -1 on error.
2468 */
2470 fr_pair_list_t const *from, fr_dict_attr_t const *parent_da)
2471{
2472 fr_pair_t *tlv;
2473 bool found = false;
2474 fr_pair_list_t list;
2475
2476 if (!fr_type_is_structural(parent_da->type)) return -1;
2477
2478 /*
2479 * Allow for nested attributes.
2480 */
2481 tlv = fr_pair_find_by_da(from, NULL, parent_da);
2482 if (tlv) {
2483 fr_pair_t *vp;
2484
2485 vp = fr_pair_copy(ctx, tlv);
2486 if (!vp) return -1;
2487
2488 fr_pair_append(to, vp);
2489
2490 return 1;
2491 }
2492
2493 fr_pair_list_init(&list);
2494
2495 fr_pair_list_foreach(from, vp) {
2496 fr_pair_t *new_vp;
2497
2498 if (!fr_dict_attr_common_parent(parent_da, vp->da, true)) continue;
2499
2500 new_vp = fr_pair_copy(ctx, vp);
2501 if (unlikely(!new_vp)) {
2502 fr_pair_list_free(&list);
2503 return -1;
2504 }
2505
2506 fr_pair_append(&list, new_vp);
2507 found = true;
2508 }
2509
2510 fr_pair_list_append(to, &list);
2511
2512 return found;
2513}
2514
2515/** Duplicate a list of pairs starting at a particular item
2516 *
2517 * Copy all pairs from 'from' regardless of tag, attribute or vendor, starting at 'item'.
2518 *
2519 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2520 * @param[in] to where to copy attributes to.
2521 * @param[in] from whence to copy #fr_pair_t (s).
2522 * @param[in] start first pair to start copying from.
2523 * @param[in] count How many instances to copy.
2524 * Use 0 for all attributes.
2525 * @return
2526 * - >0 the number of attributes copied.
2527 * - 0 if no attributes copied.
2528 * - -1 on error.
2529 */
2530int fr_pair_sublist_copy(TALLOC_CTX *ctx, fr_pair_list_t *to,
2531 fr_pair_list_t const *from, fr_pair_t const *start, unsigned int count)
2532{
2533 fr_pair_t const *vp;
2534 fr_pair_t *new_vp;
2535 unsigned int cnt = 0;
2536 fr_pair_list_t list;
2537
2538 fr_pair_list_init(&list);
2539
2540 if (!start) start = fr_pair_list_head(from);
2541
2542 for (vp = start;
2543 vp && ((count == 0) || (cnt < count));
2544 vp = fr_pair_list_next(from, vp), cnt++) {
2546
2547 new_vp = fr_pair_copy(ctx, vp);
2548 if (unlikely(!new_vp)) {
2549 fr_pair_list_free(&list);
2550 return -1;
2551 }
2552
2553 fr_pair_append(&list, new_vp);
2554 }
2555
2556 fr_pair_list_append(to, &list);
2557
2558 return cnt;
2559}
2560
2561/** Free/zero out value (or children) of a given VP
2562 *
2563 * @param[in] vp to clear value from.
2564 */
2566{
2567 fr_pair_t *child;
2568
2569 switch (vp->vp_type) {
2570 default:
2572 break;
2573
2574 case FR_TYPE_STRUCTURAL:
2575 if (fr_pair_list_empty(&vp->vp_group)) return;
2576
2577 while ((child = fr_pair_order_list_pop_tail(&vp->vp_group.order))) {
2578 fr_pair_value_clear(child);
2579 talloc_free(child);
2580 }
2581 break;
2582 }
2583}
2584
2585/** Copy the value from one pair to another
2586 *
2587 * @param[out] dst where to copy the value to.
2588 * will clear assigned value.
2589 * @param[in] src where to copy the value from
2590 * Must have an assigned value.
2591 * @return
2592 * - 0 on success.
2593 * - -1 on failure.
2594 */
2596{
2597 if (!fr_cond_assert(src->data.type != FR_TYPE_NULL)) return -1;
2598
2599 fr_value_box_clear_value(&dst->data);
2600 if (unlikely(fr_value_box_copy(dst, &dst->data, &src->data) < 0)) return -1;
2601
2602 /*
2603 * If either source or destination is secret, then this value is secret.
2604 */
2605 dst->data.secret |= src->da->flags.secret | dst->da->flags.secret | src->data.secret;
2606 return 0;
2607}
2608
2609/** Convert string value to native attribute value
2610 *
2611 * @param[in] vp to assign value to.
2612 * @param[in] value string to convert. Binary safe for variable
2613 * length values if len is provided.
2614 * @param[in] inlen The length of the input string.
2615 * @param[in] uerules used to perform unescaping.
2616 * @param[in] tainted Whether the value came from a trusted source.
2617 * @return
2618 * - 0 on success.
2619 * - -1 on failure.
2620 */
2622 fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
2623{
2624 /*
2625 * This is not yet supported because the rest of the APIs
2626 * to parse pair names, etc. don't yet enforce "inlen".
2627 * This is likely not a problem in practice, but we
2628 * haven't yet audited the uses of this function for that
2629 * behavior.
2630 */
2631 switch (vp->vp_type) {
2632 case FR_TYPE_STRUCTURAL:
2633 fr_strerror_printf("Attributes of type '%s' are not yet supported",
2634 fr_type_to_str(vp->vp_type));
2635 return -1;
2636
2637 default:
2638 break;
2639 }
2640
2641 /*
2642 * We presume that the input data is from a double quoted
2643 * string, and needs unescaping
2644 */
2645 if (fr_value_box_from_str(vp, &vp->data, vp->vp_type, vp->da,
2646 value, inlen,
2647 uerules) < 0) return -1;
2648
2649 fr_assert(vp->data.safe_for == FR_VALUE_BOX_SAFE_FOR_NONE);
2650
2651 PAIR_VERIFY(vp);
2652
2653 return 0;
2654}
2655
2656/** Copy data into an "string" data type.
2657 *
2658 * @note vp->da must be of type FR_TYPE_STRING.
2659 *
2660 * @param[in,out] vp to update
2661 * @param[in] src data to copy
2662 * @param[in] tainted Whether the value came from a trusted source.
2663 * @return
2664 * - 0 on success.
2665 * - -1 on failure.
2666 */
2667int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
2668{
2669 int ret;
2670
2671 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2672
2673 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2674 ret = fr_value_box_strdup(vp, &vp->data, vp->da, src, tainted);
2675 if (ret == 0) {
2676 PAIR_VERIFY(vp);
2677 }
2678
2679 return ret;
2680}
2681
2682/** Assign a buffer containing a nul terminated string to a vp, but don't copy it
2683 *
2684 * @param[in] vp to assign string to.
2685 * @param[in] src to copy string from.
2686 * @param[in] tainted Whether the value came from a trusted source.
2687 * @return
2688 * - 0 on success.
2689 * - -1 on failure.
2690 */
2691int fr_pair_value_strdup_shallow(fr_pair_t *vp, char const *src, bool tainted)
2692{
2693 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2694
2695 fr_value_box_clear(&vp->data);
2696 fr_value_box_strdup_shallow(&vp->data, vp->da, src, tainted);
2697
2698 PAIR_VERIFY(vp);
2699
2700 return 0;
2701}
2702
2703/** Trim the length of the string buffer to match the length of the C string
2704 *
2705 * @param[in,out] vp to trim.
2706 * @return
2707 * - 0 on success.
2708 * - -1 on failure.
2709 */
2711{
2712 int ret;
2713
2714 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2715
2716 ret = fr_value_box_strtrim(vp, &vp->data);
2717 if (ret == 0) {
2718 PAIR_VERIFY(vp);
2719 }
2720
2721 return ret;
2722}
2723
2724/** Print data into an "string" data type.
2725 *
2726 * @note vp->da must be of type FR_TYPE_STRING.
2727 *
2728 * @param[in,out] vp to update
2729 * @param[in] fmt the format string
2730 */
2731int fr_pair_value_aprintf(fr_pair_t *vp, char const *fmt, ...)
2732{
2733 int ret;
2734 va_list ap;
2735
2736 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2737
2738 fr_value_box_clear(&vp->data);
2739 va_start(ap, fmt);
2740 ret = fr_value_box_vasprintf(vp, &vp->data, vp->da, false, fmt, ap);
2741 va_end(ap);
2742
2743 if (ret == 0) {
2744 PAIR_VERIFY(vp);
2745 }
2746
2747 return ret;
2748}
2749
2750/** Pre-allocate a memory buffer for a "string" type value pair
2751 *
2752 * @note Will clear existing values (including buffers).
2753 *
2754 * @param[in,out] vp to update
2755 * @param[out] out If non-null will be filled with a pointer to the
2756 * new buffer.
2757 * @param[in] size of the data.
2758 * @param[in] tainted Whether the value came from a trusted source.
2759 * @return
2760 * - 0 on success.
2761 * - -1 on failure.
2762 */
2763int fr_pair_value_bstr_alloc(fr_pair_t *vp, char **out, size_t size, bool tainted)
2764{
2765 int ret;
2766
2767 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2768
2769 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2770 ret = fr_value_box_bstr_alloc(vp, out, &vp->data, vp->da, size, tainted);
2771 if (ret == 0) {
2772 PAIR_VERIFY(vp);
2773 }
2774
2775 return ret;
2776}
2777
2778/** Change the length of a buffer for a "string" type value pair
2779 *
2780 * @param[in,out] vp to update
2781 * @param[out] out If non-null will be filled with a pointer to the
2782 * new buffer.
2783 * @param[in] size of the data.
2784 * @return
2785 * - 0 on success.
2786 * - -1 on failure.
2787 */
2788int fr_pair_value_bstr_realloc(fr_pair_t *vp, char **out, size_t size)
2789{
2790 int ret;
2791
2792 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2793
2794 ret = fr_value_box_bstr_realloc(vp, out, &vp->data, size);
2795 if (ret == 0) {
2796 PAIR_VERIFY(vp);
2797 }
2798
2799 return ret;
2800}
2801
2802/** Copy data into a "string" type value pair
2803 *
2804 * @note This API will copy binary data, including embedded '\0'
2805 *
2806 * @note vp->da must be of type FR_TYPE_STRING.
2807 *
2808 * @param[in,out] vp to update.
2809 * @param[in] src data to copy.
2810 * @param[in] len of data to copy.
2811 * @param[in] tainted Whether the value came from a trusted source.
2812 * @return
2813 * - 0 on success.
2814 * - -1 on failure.
2815 */
2816int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
2817{
2818 int ret;
2819
2820 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2821
2822 fr_value_box_clear(&vp->data);
2823 ret = fr_value_box_bstrndup(vp, &vp->data, vp->da, src, len, tainted);
2824 if (ret == 0) {
2825 PAIR_VERIFY(vp);
2826 }
2827
2828 return ret;
2829}
2830
2831/** Copy a nul terminated talloced buffer a "string" type value pair
2832 *
2833 * The buffer must be \0 terminated, or an error will be returned.
2834 *
2835 * @param[in,out] vp to update.
2836 * @param[in] src a talloced nul terminated buffer.
2837 * @param[in] tainted Whether the value came from a trusted source.
2838 * @return
2839 * - 0 on success.
2840 * - -1 on failure.
2841 */
2842int fr_pair_value_bstrdup_buffer(fr_pair_t *vp, char const *src, bool tainted)
2843{
2844 int ret;
2845
2846 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2847
2848 fr_value_box_clear(&vp->data);
2849 ret = fr_value_box_bstrdup_buffer(vp, &vp->data, vp->da, src, tainted);
2850 if (ret == 0) {
2851 PAIR_VERIFY(vp);
2852 }
2853
2854 return ret;
2855}
2856
2857/** Assign a string to a "string" type value pair
2858 *
2859 * @param[in] vp to assign new buffer to.
2860 * @param[in] src a string.
2861 * @param[in] len of src.
2862 * @param[in] tainted Whether the value came from a trusted source.
2863 * @return
2864 * - 0 on success.
2865 * - -1 on failure.
2866 */
2867int fr_pair_value_bstrndup_shallow(fr_pair_t *vp, char const *src, size_t len, bool tainted)
2868{
2869 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2870
2871 fr_value_box_clear(&vp->data);
2872 fr_value_box_bstrndup_shallow(&vp->data, vp->da, src, len, tainted);
2873 PAIR_VERIFY(vp);
2874
2875 return 0;
2876}
2877
2878/** Assign a string to a "string" type value pair
2879 *
2880 * @param[in] vp to assign new buffer to.
2881 * @param[in] src a string.
2882 * @param[in] tainted Whether the value came from a trusted source.
2883 * @return
2884 * - 0 on success.
2885 * - -1 on failure.
2886 */
2887int fr_pair_value_bstrdup_buffer_shallow(fr_pair_t *vp, char const *src, bool tainted)
2888{
2889 int ret;
2890
2891 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2892
2893 fr_value_box_clear(&vp->data);
2894 ret = fr_value_box_bstrdup_buffer_shallow(NULL, &vp->data, vp->da, src, tainted);
2895 if (ret == 0) {
2896 PAIR_VERIFY(vp);
2897 }
2898
2899 return ret;
2900}
2901
2902/** Pre-allocate a memory buffer for a "octets" type value pair
2903 *
2904 * @note Will clear existing values (including buffers).
2905 *
2906 * @param[in,out] vp to update
2907 * @param[out] out If non-null will be filled with a pointer to the
2908 * new buffer.
2909 * @param[in] size of the data.
2910 * @param[in] tainted Whether the value came from a trusted source.
2911 * @return
2912 * - 0 on success.
2913 * - -1 on failure.
2914 */
2915int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
2916{
2917 int ret;
2918
2919 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2920
2921 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2922 ret = fr_value_box_mem_alloc(vp, out, &vp->data, vp->da, size, tainted);
2923 if (ret == 0) {
2924 PAIR_VERIFY(vp);
2925 }
2926
2927 return ret;
2928}
2929
2930/** Change the length of a buffer for a "octets" type value pair
2931 *
2932 * @param[in,out] vp to update
2933 * @param[out] out If non-null will be filled with a pointer to the
2934 * new buffer.
2935 * @param[in] size of the data.
2936 * @return
2937 * - 0 on success.
2938 * - -1 on failure.
2939 */
2941{
2942 int ret;
2943
2944 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2945
2946 ret = fr_value_box_mem_realloc(vp, out, &vp->data, size);
2947 if (ret == 0) {
2948 PAIR_VERIFY(vp);
2949 }
2950
2951 return ret;
2952}
2953
2954/** Copy data into an "octets" data type.
2955 *
2956 * @note Will clear existing values (including buffers).
2957 *
2958 * @param[in,out] vp to update
2959 * @param[in] src data to copy
2960 * @param[in] len of the data.
2961 * @param[in] tainted Whether the value came from a trusted source.
2962 * @return
2963 * - 0 on success.
2964 * - -1 on failure.
2965 */
2966int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
2967{
2968 int ret;
2969
2970 if (unlikely((len > 0) && !src)) {
2971 fr_strerror_printf("Invalid arguments to %s. Len > 0 (%zu) but src was NULL",
2972 __FUNCTION__, len);
2973 return -1;
2974 }
2975
2976 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2977
2978 fr_value_box_clear_value(&vp->data); /* Free any existing buffers */
2979 ret = fr_value_box_memdup(vp, &vp->data, vp->da, src, len, tainted);
2980 if (ret == 0) PAIR_VERIFY(vp);
2981
2982 return ret;
2983}
2984
2985/** Copy data from a talloced buffer into an "octets" data type.
2986 *
2987 * @note Will clear existing values (including buffers).
2988 *
2989 * @param[in,out] vp to update
2990 * @param[in] src data to copy
2991 * @param[in] tainted Whether the value came from a trusted source.
2992 * @return
2993 * - 0 on success.
2994 * - -1 on failure.
2995 */
2996int fr_pair_value_memdup_buffer(fr_pair_t *vp, uint8_t const *src, bool tainted)
2997{
2998 int ret;
2999
3000 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
3001
3002 fr_value_box_clear(&vp->data); /* Free any existing buffers */
3003 ret = fr_value_box_memdup_buffer(vp, &vp->data, vp->da, src, tainted);
3004 if (ret == 0) {
3005 PAIR_VERIFY(vp);
3006 }
3007
3008 return ret;
3009}
3010
3011/** Assign a buffer to a "octets" type value pair
3012 *
3013 * @param[in] vp to assign new buffer to.
3014 * @param[in] src data to copy.
3015 * @param[in] len of src.
3016 * @param[in] tainted Whether the value came from a trusted source.
3017 * @return
3018 * - 0 on success.
3019 * - -1 on failure.
3020 */
3021int fr_pair_value_memdup_shallow(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
3022{
3023 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
3024
3025 fr_value_box_clear(&vp->data);
3026 fr_value_box_memdup_shallow(&vp->data, vp->da, src, len, tainted);
3027 PAIR_VERIFY(vp);
3028
3029 return 0;
3030}
3031
3032/** Assign a talloced buffer to a "octets" type value pair
3033 *
3034 * @param[in] vp to assign new buffer to.
3035 * @param[in] src data to copy.
3036 * @param[in] tainted Whether the value came from a trusted source.
3037 * @return
3038 * - 0 on success.
3039 * - -1 on failure.
3040 */
3042{
3043 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
3044
3045 fr_value_box_clear(&vp->data);
3046 fr_value_box_memdup_buffer_shallow(NULL, &vp->data, vp->da, src, tainted);
3047 PAIR_VERIFY(vp);
3048
3049 return 0;
3050}
3051
3052
3053/** Return a const buffer for an enum type attribute
3054 *
3055 * Where the vp type is numeric but does not have any enumv, or its value
3056 * does not map to an enumv, the integer value of the pair will be printed
3057 * to buff, and a pointer to buff will be returned.
3058 *
3059 * @param[in] vp to print.
3060 * @param[in] buff to print integer value to.
3061 * @return a talloced buffer.
3062 */
3063char const *fr_pair_value_enum(fr_pair_t const *vp, char buff[20])
3064{
3065 fr_dict_enum_value_t const *enumv;
3066
3067 if (!fr_box_is_numeric(&vp->data)) {
3068 fr_strerror_printf("Pair %s is not numeric", vp->da->name);
3069 return NULL;
3070 }
3071
3072 if (vp->da->flags.has_value) switch (vp->vp_type) {
3073 case FR_TYPE_BOOL:
3074 return vp->vp_bool ? "yes" : "no";
3075
3076 default:
3077 enumv = fr_dict_enum_by_value(vp->da, &vp->data);
3078 if (enumv) return enumv->name;
3079 break;
3080 }
3081
3083 return buff;
3084}
3085
3086/** Get value box of a VP, optionally prefer enum value.
3087 *
3088 * Get the data value box of the given VP. If 'e' is set to 1 and the VP has an
3089 * enum value, this will be returned instead. Otherwise it will be set to the
3090 * value box of the VP itself.
3091 *
3092 * @param[out] out pointer to a value box.
3093 * @param[in] vp to print.
3094 * @return 1 if the enum value has been used, 0 otherwise, -1 on error.
3095 */
3097{
3098 fr_dict_enum_value_t const *dv;
3099
3100 if (vp->da && vp->da->flags.has_value &&
3101 (dv = fr_dict_enum_by_value(vp->da, &vp->data))) {
3102 *out = dv->value;
3103 return 1;
3104 }
3105
3106 *out = &vp->data;
3107 return 0;
3108}
3109
3110#ifdef WITH_VERIFY_PTR
3111/*
3112 * Verify a fr_pair_t
3113 */
3114void fr_pair_verify(char const *file, int line, fr_dict_attr_t const *parent_da,
3115 fr_pair_list_t const *list, fr_pair_t const *vp, bool verify_values)
3116{
3118
3119 if (!vp->da) {
3120 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t da pointer was NULL", file, line);
3121 }
3122
3124
3125
3126 /*
3127 * Enforce correct parentage. If the parent exists, AND it's not a group (because groups break
3128 * the strict hierarchy), then check parentage.
3129 *
3130 * We also ignore parentage if either the expected parent or the vp is raw / unknown. We may
3131 * want to tighten that a little bit, as there are cases where we create raw / unknown
3132 * attributes, and the parent is also raw / unknown. In which case the parent_da _should_ be the
3133 * same as vp->da->parent.
3134 */
3135 if (parent_da && (parent_da->type != FR_TYPE_GROUP) &&
3136 !parent_da->flags.is_raw && !parent_da->flags.is_unknown &&
3137 !vp->da->flags.is_raw && !vp->da->flags.is_unknown) {
3138 fr_fatal_assert_msg(vp->da->parent == parent_da,
3139 "CONSISTENCY CHECK FAILED %s[%d]: pair %s does not have the correct parentage - "
3140 "expected parent %s, found different parent %s",
3141 file, line, vp->da->name, parent_da->name, vp->da->parent->name);
3142 }
3143
3144 if (list) {
3145 fr_fatal_assert_msg(fr_pair_order_list_parent(vp) == &list->order,
3146 "CONSISTENCY CHECK FAILED %s[%d]: pair does not have the correct parentage "
3147 "at \"%s\"",
3148 file, line, vp->da->name);
3149 }
3150
3151 /*
3152 * This field is only valid for non-structural pairs
3153 */
3154 if (!fr_type_is_structural(vp->vp_type)) {
3156
3157 if (vp->data.enumv) fr_dict_attr_verify(file, line, vp->data.enumv);
3158
3159 if (parent && !fr_dict_attr_can_contain(parent->da, vp->da)) {
3160 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" should be parented by %s, but is parented by %s",
3161 file, line, vp->da->name, vp->da->parent->name, parent->da->name);
3162 }
3163
3164 /*
3165 * The data types have to agree, except for comb-ip and combo-ipaddr.
3166 */
3167 if (vp->vp_type != vp->da->type) switch (vp->da->type) {
3169 if ((vp->vp_type == FR_TYPE_IPV4_ADDR) ||
3170 (vp->vp_type == FR_TYPE_IPV6_ADDR)) {
3171 break;
3172 }
3173 goto failed_type;
3174
3176 if ((vp->vp_type == FR_TYPE_IPV4_PREFIX) ||
3177 (vp->vp_type == FR_TYPE_IPV6_PREFIX)) {
3178 break;
3179 }
3181
3182 default:
3183 failed_type:
3184 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" has value of data type '%s', which disagrees with the dictionary data type '%s'",
3185 file, line, vp->da->name, fr_type_to_str(vp->vp_type), fr_type_to_str(vp->da->type));
3186 }
3187
3188 /*
3189 * We would like to enable this, but there's a
3190 * lot of code like fr_pair_append_by_da() which
3191 * creates the #fr_pair_t with no value.
3192 */
3193 if (verify_values) fr_value_box_verify(file, line, &vp->data);
3194
3195 } else {
3197
3198 if (parent && (parent->vp_type != FR_TYPE_GROUP) && (parent->da == vp->da)) {
3199 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" structural (non-group) type contains itself",
3200 file, line, vp->da->name);
3201 }
3202
3203 fr_pair_list_verify(file, line, vp, &vp->vp_group, verify_values);
3204 }
3205
3206 switch (vp->vp_type) {
3207 case FR_TYPE_OCTETS:
3208 {
3209 size_t len;
3210 TALLOC_CTX *parent;
3211
3212 if (!vp->vp_octets) break; /* We might be in the middle of initialisation */
3213
3214 if (!talloc_get_type(vp->vp_ptr, uint8_t)) {
3215 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" data buffer type should be "
3216 "uint8_t but is %s", file, line, vp->da->name, talloc_get_name(vp->vp_ptr));
3217 }
3218
3219 len = talloc_array_length(vp->vp_octets);
3220 if (vp->vp_length > len) {
3221 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" length %zu is greater than "
3222 "uint8_t data buffer length %zu", file, line, vp->da->name, vp->vp_length, len);
3223 }
3224
3225 parent = talloc_parent(vp->vp_ptr);
3226 if (parent != vp) {
3227 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" char buffer is not "
3228 "parented by fr_pair_t %p, instead parented by %p (%s)",
3229 file, line, vp->da->name,
3230 vp, parent, parent ? talloc_get_name(parent) : "NULL");
3231 }
3232 }
3233 break;
3234
3235 case FR_TYPE_STRING:
3236 {
3237 size_t len;
3238 TALLOC_CTX *parent;
3239
3240 if (!vp->vp_octets) break; /* We might be in the middle of initialisation */
3241
3242 if (!talloc_get_type(vp->vp_ptr, char)) {
3243 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" data buffer type should be "
3244 "char but is %s", file, line, vp->da->name, talloc_get_name(vp->vp_ptr));
3245 }
3246
3247 len = (talloc_strlen(vp->vp_strvalue));
3248 if (vp->vp_length > len) {
3249 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" length %zu is greater than "
3250 "char buffer length %zu", file, line, vp->da->name, vp->vp_length, len);
3251 }
3252
3253 if (vp->vp_strvalue[vp->vp_length] != '\0') {
3254 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" char buffer not \\0 "
3255 "terminated", file, line, vp->da->name);
3256 }
3257
3258 parent = talloc_parent(vp->vp_ptr);
3259 if (parent != vp) {
3260 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" char buffer is not "
3261 "parented by fr_pair_t %p, instead parented by %p (%s)",
3262 file, line, vp->da->name,
3263 vp, parent, parent ? talloc_get_name(parent) : "NULL");
3264 }
3265 }
3266 break;
3267
3268 case FR_TYPE_IPV4_ADDR:
3269 if (vp->vp_ip.af != AF_INET) {
3270 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" address family is not "
3271 "set correctly for IPv4 address. Expected %i got %i",
3272 file, line, vp->da->name,
3273 AF_INET, vp->vp_ip.af);
3274 }
3275 if (vp->vp_ip.prefix != 32) {
3276 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" address prefix "
3277 "not set correctly for IPv4 address. Expected %i got %i",
3278 file, line, vp->da->name,
3279 32, vp->vp_ip.prefix);
3280 }
3281 break;
3282
3283 case FR_TYPE_IPV6_ADDR:
3284 if (vp->vp_ip.af != AF_INET6) {
3285 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" address family is not "
3286 "set correctly for IPv6 address. Expected %i got %i",
3287 file, line, vp->da->name,
3288 AF_INET6, vp->vp_ip.af);
3289 }
3290 if (vp->vp_ip.prefix != 128) {
3291 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" address prefix "
3292 "set correctly for IPv6 address. Expected %i got %i",
3293 file, line, vp->da->name,
3294 128, vp->vp_ip.prefix);
3295 }
3296 break;
3297
3298 case FR_TYPE_ATTR:
3299 if (!vp->vp_attr) {
3300 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" attribute pointer is NULL",
3301 file, line, vp->da->name);
3302 }
3303 break;
3304
3305 case FR_TYPE_STRUCTURAL:
3306 {
3307 if (vp->vp_group.verified) break;
3308
3309 fr_pair_list_foreach(&vp->vp_group, child) {
3310 TALLOC_CTX *parent = talloc_parent(child);
3311
3313 "CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" should be parented "
3314 "by fr_pair_t \"%s\". Expected talloc parent %p (%s) got %p (%s)",
3315 file, line,
3316 child->da->name, vp->da->name,
3317 vp, talloc_get_name(vp),
3318 parent, talloc_get_name(parent));
3319
3320 /*
3321 * Check if the child can be in the parent.
3322 */
3324 "CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" should be parented "
3325 "by fr_pair_t \"%s\", but it is instead parented by \"%s\"",
3326 file, line,
3327 child->da->name, child->da->parent->name, vp->da->name);
3328
3329 fr_pair_verify(file, line, vp->da, &vp->vp_group, child, verify_values);
3330 }
3331
3332 UNCONST(fr_pair_t *, vp)->vp_group.verified = true;
3333 }
3334 break;
3335
3336 default:
3337 break;
3338 }
3339
3340 if (vp->da->flags.is_unknown || vp->vp_raw) {
3342
3343 /*
3344 * Raw or unknown attributes can have specific data types. See DER and CBOR.
3345 */
3346
3347 } else if (fr_type_is_leaf(vp->vp_type) && (vp->vp_type != vp->da->type) &&
3348 !((vp->da->type == FR_TYPE_COMBO_IP_ADDR) && ((vp->vp_type == FR_TYPE_IPV4_ADDR) || (vp->vp_type == FR_TYPE_IPV6_ADDR))) &&
3349 !((vp->da->type == FR_TYPE_COMBO_IP_PREFIX) && ((vp->vp_type == FR_TYPE_IPV4_PREFIX) || (vp->vp_type == FR_TYPE_IPV6_PREFIX)))) {
3350 char data_type_int[10], da_type_int[10];
3351
3352 snprintf(data_type_int, sizeof(data_type_int), "%u", vp->vp_type);
3353 snprintf(da_type_int, sizeof(da_type_int), "%u", vp->da->type);
3354
3355 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t attribute %p \"%s\" "
3356 "data type (%s) does not match da type (%s)",
3357 file, line, vp->da, vp->da->name,
3358 fr_table_str_by_value(fr_type_table, vp->vp_type, data_type_int),
3359 fr_table_str_by_value(fr_type_table, vp->da->type, da_type_int));
3360 }
3361}
3362
3363/** Verify a pair list
3364 *
3365 * @param[in] file from which the verification is called
3366 * @param[in] line number in file
3367 * @param[in] expected talloc ctx pairs should have been allocated in
3368 * @param[in] list of fr_pair_ts to verify
3369 * @param[in] verify_values whether we verify the values, too.
3370 */
3371void fr_pair_list_verify(char const *file, int line, TALLOC_CTX const *expected, fr_pair_list_t const *list, bool verify_values)
3372{
3373 fr_pair_t *slow, *fast;
3374 TALLOC_CTX *parent;
3375
3376 if (fr_pair_list_empty(list)) return; /* Fast path */
3377
3378 /*
3379 * Only verify the list if it has been modified.
3380 */
3381 if (list->verified) return;
3382
3383 for (slow = fr_pair_list_head(list), fast = fr_pair_list_head(list);
3384 slow && fast;
3385 slow = fr_pair_list_next(list, slow), fast = fr_pair_list_next(list, fast)) {
3386 fr_pair_verify(__FILE__, __LINE__, NULL, list, slow, verify_values);
3387
3388 /*
3389 * Advances twice as fast as slow...
3390 */
3391 fast = fr_pair_list_next(list, fast);
3392 fr_fatal_assert_msg(fast != slow,
3393 "CONSISTENCY CHECK FAILED %s[%d]: Looping list found. Fast pointer hit "
3394 "slow pointer at \"%s\"",
3395 file, line, slow->da->name);
3396
3397 parent = talloc_parent(slow);
3398 if (expected && (parent != expected)) {
3399 bad_parent:
3400 fr_log_talloc_report(expected);
3402
3403 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: Expected fr_pair_t \"%s\" to be parented "
3404 "by %p (%s), instead parented by %p (%s)\n",
3405 file, line, slow->da->name,
3406 expected, talloc_get_name(expected),
3407 parent, parent ? talloc_get_name(parent) : "NULL");
3408 }
3409 }
3410
3411 /*
3412 * Check the remaining pairs
3413 */
3414 for (; slow; slow = fr_pair_list_next(list, slow)) {
3415 fr_pair_verify(__FILE__, __LINE__, NULL, list, slow, verify_values);
3416
3417 parent = talloc_parent(slow);
3418 if (expected && (parent != expected)) goto bad_parent;
3419 }
3420
3421 UNCONST(fr_pair_list_t *, list)->verified = true;
3422}
3423#endif
3424
3425/** Mark up a list of VPs as tainted.
3426 *
3427 */
3429{
3430 if (fr_pair_list_empty(list)) return;
3431
3432 fr_pair_list_foreach(list, vp) {
3434
3435 switch (vp->vp_type) {
3436 case FR_TYPE_STRUCTURAL:
3437 fr_pair_list_tainted(&vp->vp_group);
3438 break;
3439
3440 default:
3441 break;
3442 }
3443
3444 vp->vp_tainted = true;
3445 }
3446}
3447
3448/** Evaluation function for matching if vp matches a given da
3449 *
3450 * @param item pointer to a fr_pair_t
3451 * @param uctx da to match
3452 *
3453 * @return true if the pair matches the da
3454 */
3455bool fr_pair_matches_da(void const *item, void const *uctx)
3456{
3457 fr_pair_t const *vp = item;
3458 fr_dict_attr_t const *da = uctx;
3459 return da == vp->da;
3460}
3461
3462/** Find or allocate a parent attribute.
3463 *
3464 * The input da is somewhere down inside of the da hierarchy. We
3465 * need to recursively find or create parent VPs which match the
3466 * given da.
3467 *
3468 * We find (or add) the VP into the "in" list. Any newly created VP
3469 * is inserted before "next". Or if "next==NULL", at the tail of
3470 * "in".
3471 *
3472 * @param[in] in the parent vp to look in
3473 * @param[in] item if we create a new vp, insert it before this item
3474 * @param[in] da look for vps in the parent which match this da
3475 * @return
3476 * - NULL on OOM
3477 * - parent vp we've found or allocated.
3478 */
3480{
3481 fr_pair_t *parent, *vp;
3482
3484
3485 /*
3486 * We're looking for a parent in the root of the
3487 * dictionary. Find the relevant VP in the current
3488 * container.
3489 *
3490 * If it's not found, allocate it, and insert it into the
3491 * list. Note that we insert it before the given "item"
3492 * vp so that we don't loop over the newly created pair
3493 * as we're processing the list.
3494 */
3495 if (da->flags.is_root || (da->parent == in->da)) {
3496 return in;
3497 }
3498
3499 /*
3500 * We're not at the root. Go find (or create) the parent
3501 * of this da.
3502 */
3503 parent = pair_alloc_parent(in, item, da->parent);
3504 if (!parent) return NULL;
3505
3506 /*
3507 * We have the parent attribute, maybe it already
3508 * contains the da we're looking for?
3509 */
3510 vp = fr_pair_find_by_da(&parent->vp_group, NULL, da);
3511 if (vp) return vp;
3512
3513 /*
3514 * Now that the entire set of parents has been created,
3515 * create the final VP. Make sure it's in the parent,
3516 * and return it.
3517 */
3518 vp = fr_pair_afrom_da(parent, da);
3519 if (!vp) return NULL;
3520
3521 /*
3522 * If we are at the root, and have been provided with
3523 * an entry to insert before, then do that.
3524 */
3525 if (item && da->parent->flags.is_root) {
3526 fr_pair_insert_before(&parent->vp_group, item, vp);
3527 } else {
3528 fr_pair_append(&parent->vp_group, vp);
3529 }
3530 return vp;
3531}
3532
3533/** Parse a list of VPs from a value box.
3534 *
3535 * @param[in] ctx to allocate new VPs in
3536 * @param[out] out list to add new pairs to
3537 * @param[in] dict to use in parsing
3538 * @param[in] box whose value is to be parsed
3539 */
3540void fr_pair_list_afrom_box(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_t const *dict, fr_value_box_t *box)
3541{
3542 fr_pair_parse_t root, relative;
3543
3544 fr_assert(box->type == FR_TYPE_STRING);
3545
3546 root = (fr_pair_parse_t) {
3547 .ctx = ctx,
3548 .da = fr_dict_root(dict),
3549 .list = out,
3550 .dict = dict,
3551 .internal = fr_dict_internal(),
3552 .allow_crlf = true,
3553 .tainted = box->tainted,
3554 };
3555 relative = (fr_pair_parse_t) { };
3556
3557 if (fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(box->vb_strvalue, box->vb_length)) < 0) {
3558 return;
3559 }
3560}
int const char * file
Definition acutest.h:704
va_end(args)
int n
Definition acutest.h:579
static int const char * fmt
Definition acutest.h:575
int const char int line
Definition acutest.h:704
va_start(args, fmt)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:487
#define NDEBUG_UNUSED
Definition build.h:328
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:112
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
static size_t min(size_t x, size_t y)
Definition dbuff.c:66
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:290
void *(* fr_dcursor_iter_t)(fr_dcursor_t *cursor, void *to_eval, void *uctx)
Callback for implementing custom iterators.
Definition dcursor.h:51
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
static void * _fr_dcursor_init(fr_dcursor_t *cursor, fr_dlist_head_t const *head, fr_dcursor_iter_t iter, fr_dcursor_iter_t peek, void const *iter_uctx, fr_dcursor_insert_t insert, fr_dcursor_remove_t remove, void const *mod_uctx, bool is_const)
Setup a cursor to iterate over attribute items in dlists.
Definition dcursor.h:737
int fr_log_talloc_report(TALLOC_CTX const *ctx)
Generate a talloc memory report for a context and print to stderr/stdout.
Definition debug.c:962
#define fr_fatal_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:183
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
#define fr_fatal_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:176
fr_dict_attr_t const * fr_dict_attr_common_parent(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition dict_util.c:2314
static fr_dict_attr_t * fr_dict_attr_unknown_copy(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Definition dict.h:586
fr_dict_attr_t * fr_dict_attr_unknown_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da))
Copy a known or unknown attribute to produce an unknown attribute with the specified name.
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:613
void fr_dict_attr_verify(char const *file, int line, fr_dict_attr_t const *da)
Definition dict_util.c:5100
bool fr_dict_attr_can_contain(fr_dict_attr_t const *parent, fr_dict_attr_t const *child)
See if a structural da is allowed to contain another da.
Definition dict_util.c:5190
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2666
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:259
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:3656
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4929
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:3594
char const * name
Enum name.
Definition dict.h:256
static fr_slen_t in
Definition dict.h:884
Value of an enumerated attribute.
Definition dict.h:255
Test enumeration values.
Definition dict_test.h:92
unsigned int offset
Positive offset from start of structure to fr_dlist_t.
Definition dlist.h:55
fr_dlist_t * next
Definition dlist.h:43
fr_dlist_t entry
Struct holding the head and tail of the list.
Definition dlist.h:52
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:537
Head of a doubly linked list.
Definition dlist.h:51
talloc_free(hp)
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
Definition lst.c:122
fr_type_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_COMBO_IP_PREFIX
IPv4 or IPv6 address prefix depending on length.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
unsigned char uint8_t
void * memset_explicit(void *ptr, int ch, size_t len)
Definition missing.c:625
bool fr_pair_matches_da(void const *item, void const *uctx)
Evaluation function for matching if vp matches a given da.
Definition pair.c:3455
int fr_pair_insert_before(fr_pair_list_t *list, fr_pair_t *pos, fr_pair_t *to_add)
Add a VP before another VP.
Definition pair.c:1411
int fr_pair_list_copy_by_da(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_dict_attr_t const *da, unsigned int count)
Duplicate pairs in a list matching the specified da.
Definition pair.c:2420
fr_pair_t * fr_pair_list_parent(fr_pair_list_t const *list)
Return a pointer to the parent pair which contains this list.
Definition pair.c:970
static int _pair_list_dcursor_remove(NDEBUG_UNUSED fr_dcursor_t *cursor, void *to_remove, UNUSED void *uctx)
Keep attr tree and sublists synced on cursor removal.
Definition pair.c:1012
static void pair_init_from_da(fr_pair_t *vp, fr_dict_attr_t const *da)
Continue initialising an fr_pair_t assigning a da.
Definition pair.c:190
int fr_pair_list_cmp(fr_pair_list_t const *a, fr_pair_list_t const *b)
Determine equality of two lists.
Definition pair.c:2054
int fr_pair_value_memdup_buffer(fr_pair_t *vp, uint8_t const *src, bool tainted)
Copy data from a talloced buffer into an "octets" data type.
Definition pair.c:2996
fr_pair_t * fr_pair_afrom_da_depth_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da, unsigned int start)
Create a pair (and all intermediate parents), and append it to the list.
Definition pair.c:422
unsigned int fr_pair_count_by_da(fr_pair_list_t const *list, fr_dict_attr_t const *da)
Return the number of instances of a given da in the specified list.
Definition pair.c:684
void fr_pair_list_tainted(fr_pair_list_t *list)
Mark up a list of VPs as tainted.
Definition pair.c:3428
#define NOT_IN_THIS_LIST_MSG
Definition pair.c:551
int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and append)
Definition pair.c:1471
int fr_pair_value_enum_box(fr_value_box_t const **out, fr_pair_t *vp)
Get value box of a VP, optionally prefer enum value.
Definition pair.c:3096
int fr_pair_value_aprintf(fr_pair_t *vp, char const *fmt,...)
Print data into an "string" data type.
Definition pair.c:2731
int fr_pair_delete_by_da_nested(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list, and prune any empty branches.
Definition pair.c:1720
static int8_t pair_cmp_by_num(void const *a, void const *b)
Order attributes by their attribute number, and tag.
Definition pair.c:1871
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
Definition pair.c:2330
int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
Change a vp's talloc ctx and insert it into a new list.
Definition pair.c:585
fr_pair_t * fr_pair_root_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
A special allocation function which disables child autofree.
Definition pair.c:248
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition pair.c:2966
static void * _fr_pair_iter_next_value(fr_dcursor_t *cursor, void *current, UNUSED void *uctx)
Iterate over pairs.
Definition pair.c:1212
static void * fr_pair_iter_next_by_da(fr_dcursor_t *cursor, void *current, void *uctx)
Iterate over pairs with a specified da.
Definition pair.c:640
void fr_pair_validate_debug(fr_pair_t const *failed[2])
Write an error to the library errorbuff detailing the mismatch.
Definition pair.c:2100
fr_pair_t * fr_pair_find_by_da_nested(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find a pair with a matching fr_dict_attr_t, by walking the nested fr_dict_attr_t tree.
Definition pair.c:784
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition pair.c:2667
int fr_pair_value_bstrdup_buffer_shallow(fr_pair_t *vp, char const *src, bool tainted)
Assign a string to a "string" type value pair.
Definition pair.c:2887
void fr_pair_init_null(fr_pair_t *vp)
Initialise fields in an fr_pair_t without assigning a da.
Definition pair.c:149
int8_t fr_pair_cmp_by_parent_num(void const *a, void const *b)
Order attributes by their parent(s), attribute number, and tag.
Definition pair.c:1928
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
Convert string value to native attribute value.
Definition pair.c:2621
fr_pair_list_t * fr_pair_parent_list(fr_pair_t const *vp)
Return a pointer to the parent pair list.
Definition pair.c:941
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:707
fr_pair_t * fr_pair_find_by_child_num_idx(fr_pair_list_t const *list, fr_dict_attr_t const *parent, unsigned int attr, unsigned int idx)
Find the pair with the matching child attribute at a given index.
Definition pair.c:907
int fr_pair_cmp(fr_pair_t const *a, fr_pair_t const *b)
Compare two pairs, using the operator from "a".
Definition pair.c:1976
fr_pair_list_t * fr_pair_list_alloc(TALLOC_CTX *ctx)
Allocate a new pair list on the heap.
Definition pair.c:119
int fr_pair_value_bstrndup_shallow(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Assign a string to a "string" type value pair.
Definition pair.c:2867
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:1352
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
Definition pair.c:1696
fr_pair_t * fr_pair_parent(fr_pair_t const *vp)
Return a pointer to the parent pair.
Definition pair.c:956
fr_pair_t * _fr_pair_dcursor_by_ancestor_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dict_attr_t const *da, bool is_const)
Initialise a cursor that will return only attributes descended from the specified fr_dict_attr_t.
Definition pair.c:1174
static void pair_init_null(fr_pair_t *vp)
Initialise fields in an fr_pair_t without assigning a da.
Definition pair.c:135
void fr_pair_replace(fr_pair_list_t *list, fr_pair_t *to_replace, fr_pair_t *vp)
Replace a given VP.
Definition pair.c:1444
fr_pair_t * fr_pair_find_by_da_idx(fr_pair_list_t const *list, fr_dict_attr_t const *da, unsigned int idx)
Find a pair with a matching da at a given index.
Definition pair.c:755
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:290
static int _pair_list_dcursor_insert(fr_dcursor_t *cursor, void *to_insert, UNUSED void *uctx)
Keep attr tree and sublists synced on cursor insert.
Definition pair.c:987
int fr_pair_value_bstrdup_buffer(fr_pair_t *vp, char const *src, bool tainted)
Copy a nul terminated talloced buffer a "string" type value pair.
Definition pair.c:2842
int fr_pair_update_by_da_parent(fr_pair_t *parent, fr_pair_t **out, fr_dict_attr_t const *da)
Return the first fr_pair_t matching the fr_dict_attr_t or alloc a new fr_pair_t and its subtree (and ...
Definition pair.c:1602
int fr_pair_list_copy_by_ancestor(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_dict_attr_t const *parent_da)
Duplicate pairs in a list where the da is a descendant of parent_da.
Definition pair.c:2469
static int _fr_pair_free(fr_pair_t *vp)
Free a fr_pair_t.
Definition pair.c:69
bool fr_pair_validate(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check.
Definition pair.c:2135
fr_pair_t * fr_pair_find_by_child_num(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *parent, unsigned int attr)
Find the pair with the matching child attribute.
Definition pair.c:881
int fr_pair_steal_append(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
Change a vp's talloc ctx and insert it into a new list.
Definition pair.c:562
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
static fr_pair_t * pair_alloc_parent(fr_pair_t *in, fr_pair_t *item, fr_dict_attr_t const *da)
Find or allocate a parent attribute.
Definition pair.c:3479
int fr_pair_value_mem_realloc(fr_pair_t *vp, uint8_t **out, size_t size)
Change the length of a buffer for a "octets" type value pair.
Definition pair.c:2940
char const * fr_pair_value_enum(fr_pair_t const *vp, char buff[20])
Return a const buffer for an enum type attribute.
Definition pair.c:3063
int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Copy data into a "string" type value pair.
Definition pair.c:2816
int fr_pair_value_bstr_alloc(fr_pair_t *vp, char **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "string" type value pair.
Definition pair.c:2763
fr_pair_t * fr_pair_alloc_null(TALLOC_CTX *ctx)
Dynamically allocate a new attribute with no fr_dict_attr_t assigned.
Definition pair.c:169
int fr_pair_value_bstr_realloc(fr_pair_t *vp, char **out, size_t size)
Change the length of a buffer for a "string" type value pair.
Definition pair.c:2788
static void * fr_pair_iter_next_by_ancestor(fr_dcursor_t *cursor, void *current, void *uctx)
Iterate over pairs which are decedents of the specified da.
Definition pair.c:663
bool fr_pair_immutable(fr_pair_t const *vp)
Definition pair.c:2287
void fr_pair_value_clear(fr_pair_t *vp)
Free/zero out value (or children) of a given VP.
Definition pair.c:2565
int fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list and free.
Definition pair.c:1833
int fr_pair_append_by_da_parent(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t, adding the parent attributes if required.
Definition pair.c:1528
int fr_pair_sublist_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_pair_t const *start, unsigned int count)
Duplicate a list of pairs starting at a particular item.
Definition pair.c:2530
static fr_dlist_head_t value_dlist
Definition pair.c:1238
int fr_pair_delete_by_child_num(fr_pair_list_t *list, fr_dict_attr_t const *parent, unsigned int attr)
Delete matching pairs from the specified list.
Definition pair.c:1815
int fr_pair_value_copy(fr_pair_t *dst, fr_pair_t *src)
Copy the value from one pair to another.
Definition pair.c:2595
fr_pair_t * _fr_pair_dcursor_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, bool is_const)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.c:1137
int fr_pair_reinit_from_da(fr_pair_list_t *list, fr_pair_t *vp, fr_dict_attr_t const *da)
Re-initialise an attribute with a different da.
Definition pair.c:327
int fr_pair_steal(TALLOC_CTX *ctx, fr_pair_t *vp)
Steal one VP.
Definition pair.c:537
int fr_pair_value_memdup_shallow(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Assign a buffer to a "octets" type value pair.
Definition pair.c:3021
bool fr_pair_validate_relaxed(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check.
Definition pair.c:2212
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:503
fr_value_box_t * fr_pair_dcursor_nested_init(fr_dcursor_t *cursor, fr_dcursor_t *parent)
Initialises a special dcursor over another cursor which returns fr_pair_t, but we return fr_value_box...
Definition pair.c:1305
#define IN_A_LIST_MSG
Definition pair.c:550
fr_pair_t * _fr_pair_dcursor_by_da_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dict_attr_t const *da, bool is_const)
Initialise a cursor that will return only attributes matching the specified fr_dict_attr_t.
Definition pair.c:1155
static void * _fr_pair_iter_next_dcursor_value(UNUSED fr_dcursor_t *cursor, void *current, void *uctx)
Iterate over pairs.
Definition pair.c:1274
int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "octets" type value pair.
Definition pair.c:2915
int fr_pair_value_strtrim(fr_pair_t *vp)
Trim the length of the string buffer to match the length of the C string.
Definition pair.c:2710
int fr_pair_insert_after(fr_pair_list_t *list, fr_pair_t *pos, fr_pair_t *to_add)
Add a VP after another VP.
Definition pair.c:1378
fr_pair_t * fr_pair_afrom_da_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da)
Create a pair (and all intermediate parents), and append it to the list.
Definition pair.c:480
fr_pair_list_t * fr_pair_children(fr_pair_t *vp)
Get the child list of a group.
Definition pair.c:931
int fr_pair_prepend(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the start of the list.
Definition pair.c:1321
fr_value_box_t * fr_pair_dcursor_value_init(fr_dcursor_t *cursor)
Initialises a special dcursor over a fr_pair_list_t, but which returns fr_value_box_t.
Definition pair.c:1258
void fr_pair_list_afrom_box(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_t const *dict, fr_value_box_t *box)
Parse a list of VPs from a value box.
Definition pair.c:3540
fr_pair_t * fr_pair_list_iter_leaf(fr_pair_list_t *list, fr_pair_t *vp)
Iterates over the leaves of a list.
Definition pair.c:1052
void fr_pair_list_steal(TALLOC_CTX *ctx, fr_pair_list_t *list)
Steal a list of pairs to a new context.
Definition pair.c:2311
int fr_pair_raw_afrom_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len)
Mark malformed attribute as raw.
Definition pair.c:609
int8_t fr_pair_cmp_by_da(void const *a, void const *b)
Order attributes by their da, and tag.
Definition pair.c:1851
int fr_pair_value_memdup_buffer_shallow(fr_pair_t *vp, uint8_t const *src, bool tainted)
Assign a talloced buffer to a "octets" type value pair.
Definition pair.c:3041
int fr_pair_value_strdup_shallow(fr_pair_t *vp, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a vp, but don't copy it.
Definition pair.c:2691
int fr_pair_prepend_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and prepend)
Definition pair.c:1498
fr_pair_t * fr_pair_find_last_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the last pair with a matching da.
Definition pair.c:731
fr_pair_t * _fr_pair_dcursor_iter_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dcursor_iter_t iter, void const *uctx, bool is_const)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.c:1117
int fr_pair_list_copy_to_box(fr_value_box_t *dst, fr_pair_list_t *from)
Copy the contents of a pair list to a set of value-boxes.
Definition pair.c:2365
fr_pair_t * fr_pair_afrom_child_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Create a new valuepair.
Definition pair.c:384
fr_slen_t fr_pair_list_afrom_substr(fr_pair_parse_t const *root, fr_pair_parse_t *relative, fr_sbuff_t *in)
Parse a fr_pair_list_t from a substring.
TALLOC_CTX * ctx
Definition pair_legacy.h:43
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
#define fr_assert(_expr)
Definition rad_assert.h:38
#define FR_SBUFF_IN(_start, _len_or_end)
#define FR_SBUFF_OUT(_start, _len_or_end)
Set of parsing rules for *unescape_until functions.
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
return count
Definition module.c:155
fr_pair_t * vp
bool _CONST is_child
is a child of a VP
Definition pair.h:55
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
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
#define talloc_get_type_abort_const
Definition talloc.h:113
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:139
#define FR_TLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for tlists.
Definition tlist.h:790
#define FR_TLIST_HEAD(_name)
Expands to the type name used for the head wrapper structure.
Definition tlist.h:769
static fr_tlist_head_t * fr_tlist_head_from_dlist(fr_dlist_head_t *dlist_head)
Get a fr_tlist_head_t from a fr_dlist_head_t.
Definition tlist.h:69
@ T_OP_CMP_TRUE
Definition token.h:104
@ T_BARE_WORD
Definition token.h:120
@ T_OP_EQ
Definition token.h:83
@ T_OP_CMP_FALSE
Definition token.h:105
@ T_OP_REG_EQ
Definition token.h:102
@ T_OP_REG_NE
Definition token.h:103
#define ATTRIBUTE_EQ(_x, _y)
Definition pair.h:155
#define fr_pair_cmp_op(_op, _a, _b)
Compare two attributes using and operator.
Definition pair.h:677
#define PAIR_ALLOCED(_x)
Definition pair.h:212
static fr_slen_t fr_pair_aprint(TALLOC_CTX *ctx, char **out, fr_dict_attr_t const *parent, fr_pair_t const *vp) 1(fr_pair_print
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define PAIR_VERIFY(_x)
Definition pair.h:204
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_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition pair_inline.c:69
#define vp_group
Definition pair.h:144
fr_pair_t * fr_pair_remove(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list without freeing.
Definition pair_inline.c:93
#define fr_pair_list_foreach(_list_head, _iter)
Iterate over the contents of a fr_pair_list_t.
Definition pair.h:279
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src)
Appends a list of fr_pair_t from a temporary list to a destination list.
#define PAIR_VERIFY_WITH_LIST(_l, _x)
Definition pair.h:205
ssize_t fr_pair_print_value_quoted(fr_sbuff_t *out, fr_pair_t const *vp, fr_token_t quote)
Print the value of an attribute to a string.
Definition pair_print.c:59
#define PAIR_LIST_VERIFY(_x)
Definition pair.h:207
fr_pair_t * fr_pair_list_prev(fr_pair_list_t const *list, fr_pair_t const *item))
Get the previous item in a valuepair list before a specific entry.
Definition pair_inline.c:82
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:42
size_t fr_pair_list_num_elements(fr_pair_list_t const *list)
Get the length of a list of fr_pair_t.
static fr_slen_t parent
Definition pair.h:858
uint8_t depth
Deepest attribute in the stack.
Definition proto.h:56
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
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84
#define fr_strerror_const(_msg)
Definition strerror.h:223
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
#define fr_type_is_group(_x)
Definition types.h:377
#define fr_type_is_structural(_x)
Definition types.h:393
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:84
#define FR_TYPE_STRUCTURAL
Definition types.h:317
#define fr_type_is_leaf(_x)
Definition types.h:394
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:455
void fr_value_box_memdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, bool tainted)
Assign a talloced buffer to a box, but don't copy it.
Definition value.c:5196
int fr_value_box_vasprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted, char const *fmt, va_list ap)
Print a formatted string using our internal printf wrapper and assign it to a value box.
Definition value.c:4691
int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb)
Trim the length of the string buffer to match the length of the C string.
Definition value.c:4660
int fr_value_box_mem_alloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, size_t len, bool tainted)
Pre-allocate an octets buffer for filling by the caller.
Definition value.c:5000
int fr_value_box_memdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, bool tainted)
Copy a talloced buffer to a fr_value_box_t.
Definition value.c:5156
int fr_value_box_bstrdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated talloced buffer to a fr_value_box_t.
Definition value.c:4916
int fr_value_box_mem_realloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:5033
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:749
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:4409
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:4211
void fr_value_box_memdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Assign a buffer to a box, but don't copy it.
Definition value.c:5178
ssize_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:6079
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:4346
void fr_value_box_verify(char const *file, int line, fr_value_box_t const *vb)
Validation function to check that a fr_value_box_t is correctly initialised.
Definition value.c:7208
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4634
void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4744
int fr_value_box_bstr_alloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, size_t len, bool tainted)
Alloc and assign an empty \0 terminated string to a fr_value_box_t.
Definition value.c:4779
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:4392
int fr_value_box_bstr_realloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:4812
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4853
int fr_value_box_bstrdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a talloced buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4961
void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Assign a string to to a fr_value_box_t.
Definition value.c:4940
int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Copy a buffer to a fr_value_box_t.
Definition value.c:5094
#define fr_box_is_numeric(_x)
Definition value.h:459
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:644
static fr_slen_t data
Definition value.h:1340
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1030
#define FR_VALUE_BOX_SAFE_FOR_NONE
Definition value.h:172
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:610
static size_t char ** out
Definition value.h:1030