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