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