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: 4817d13eeba89d2319b1963afc82ab360812e89c $")
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
181
182 return vp;
183}
184
185/** Continue initialising an fr_pair_t assigning a da
186 *
187 * @note Internal use by the pair allocation functions only.
188 */
189static inline CC_HINT(always_inline) void pair_init_from_da(fr_pair_t *vp, fr_dict_attr_t const *da)
190{
191 /*
192 * Use the 'da' to initialize more fields.
193 */
194 vp->da = da;
195
196 if (likely(fr_type_is_leaf(da->type))) {
197 fr_value_box_init(&vp->data, da->type, da, false);
198 } else {
199#ifndef NDEBUG
200 /*
201 * Make it very obvious if we failed
202 * to initialise something.
203 * Given the definition of fr_value_box_t, this entails
204 * writing const-qualified fields. The compiler allows it,
205 * but Coverity points it out as a defect, so it is annotated.
206 */
207 /* coverity[store_writes_const_field] */
208 memset(&vp->data, 0xff, sizeof(vp->data));
209#endif
210
212
213 /*
214 * Make sure that the pad field is initialized.
215 */
216 if (sizeof(vp->pad)) memset(vp->pad, 0, sizeof(vp->pad));
217
218 /*
219 * Hack around const issues...
220 * Here again, the workaround suffices for the compiler but
221 * not for Coverity, so again we annotate.
222 */
223 /* coverity[store_writes_const_field] */
224 memcpy(UNCONST(fr_type_t *, &vp->vp_type), &da->type, sizeof(vp->vp_type));
225 fr_pair_list_init(&vp->vp_group);
226 vp->vp_group.is_child = true;
227 fr_pair_order_list_talloc_init_children(vp, &vp->vp_group.order);
228 }
229}
230
231/** A special allocation function which disables child autofree
232 *
233 * This is intended to allocate root attributes for requests.
234 * These roots are special in that they do not necessarily own
235 * the child attributes and _MUST NOT_ free them when they
236 * themselves are freed. The children are allocated in special
237 * ctxs which may be moved between session state entries and
238 * requests, or may belong to a parent request.
239 *
240 * @param[in] ctx to allocate the pair root in.
241 * @param[in] da The root attribute.
242 * @return
243 * - A new root pair on success.
244 * - NULL on failure.
245 * @hidecallergraph
246 */
248{
249 fr_pair_t *vp;
250
251#ifndef NDEBUG
252 if (da->type != FR_TYPE_GROUP) {
253 fr_strerror_const("Root must be a group type");
254 return NULL;
255 }
256#endif
257
258 vp = talloc_zero(ctx, fr_pair_t);
259 if (unlikely(!vp)) {
260 fr_strerror_const("Out of memory");
261 return NULL;
262 }
263
264 if (unlikely(da->flags.is_unknown)) {
265 fr_strerror_const("Root attribute cannot be unknown");
266 return NULL;
267 }
268
270
271 return vp;
272}
273
274/** Dynamically allocate a new attribute and assign a #fr_dict_attr_t
275 *
276 * @note Will duplicate any unknown attributes passed as the da.
277 *
278 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t
279 * @param[in] da Specifies the dictionary attribute to build the #fr_pair_t from.
280 * If unknown, will be duplicated, with the memory being bound to
281 * the pair.
282 * @return
283 * - A new #fr_pair_t.
284 * - NULL if an error occurred.
285 * @hidecallergraph
286 */
287fr_pair_t *fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
288{
289 fr_pair_t *vp;
290
291 vp = fr_pair_alloc_null(ctx);
292 if (!vp) {
293 fr_strerror_printf("Out of memory");
294 return NULL;
295 }
296
297 /*
298 * If we get passed an unknown da, we need to ensure that
299 * it's parented by "vp".
300 */
301 if (da->flags.is_unknown) {
302 fr_dict_attr_t const *unknown;
303
304 unknown = fr_dict_attr_unknown_copy(vp, da);
305 da = unknown;
306 }
307
309
310 return vp;
311}
312
313/** Re-initialise an attribute with a different da
314 *
315 * If the new da has a different type to the old da, we'll attempt to cast
316 * the current value in place.
317 */
319{
320 fr_dict_attr_t const *to_free;
321
322 /*
323 * vp may be created from fr_pair_alloc_null(), in which case it has no da.
324 */
325 if (vp->da && !vp->da->flags.is_raw) {
326 if (vp->da == da) return 0;
327
328 if (!fr_type_is_leaf(vp->vp_type)) return -1;
329
330 if ((da->type != vp->vp_type) && (fr_value_box_cast_in_place(vp, &vp->data, da->type, da) < 0)) return -1;
331 } else {
332 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)));
333
334 fr_value_box_init(&vp->data, da->type, da, false);
335 }
336
337 to_free = vp->da;
338 vp->da = da;
339
340 /*
341 * Only frees unknown fr_dict_attr_t's
342 */
344
345 /*
346 * Ensure we update the attribute index in the parent.
347 */
348 if (list) {
349 fr_pair_remove(list, vp);
350
351 fr_pair_append(list, vp);
352 }
353
354 return 0;
355}
356
357/** Create a new valuepair
358 *
359 * If attr and vendor match a dictionary entry then a VP with that #fr_dict_attr_t
360 * will be returned.
361 *
362 * If attr or vendor are unknown will call dict_attruknown to create a dynamic
363 * #fr_dict_attr_t of #FR_TYPE_OCTETS.
364 *
365 * Which type of #fr_dict_attr_t the #fr_pair_t was created with can be determined by
366 * checking @verbatim vp->da->flags.is_unknown @endverbatim.
367 *
368 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
369 * @param[in] parent of the attribute being allocated (usually a dictionary or vendor).
370 * @param[in] attr number.
371 * @return
372 * - A new #fr_pair_t.
373 * - NULL on error.
374 */
375fr_pair_t *fr_pair_afrom_child_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
376{
377 fr_dict_attr_t const *da;
378 fr_pair_t *vp;
379
380 vp = fr_pair_alloc_null(ctx);
381 if (unlikely(!vp)) return NULL;
382
384 if (!da) {
385 fr_dict_attr_t *unknown;
386
388 if (!unknown) {
390 return NULL;
391 }
392 da = unknown;
393 }
394
396
397 return vp;
398}
399
400/** Create a pair (and all intermediate parents), and append it to the list
401 *
402 * Unlike fr_pair_afrom_da_nested(), this function starts off at an intermediate ctx and list.
403 *
404 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
405 * @param[out] list where the created pair is supposed to go.
406 * @param[in] da the da for the pair to create
407 * @param[in] start the starting depth. If start != 0, we must have ctx==vp at that depth, and list==&vp->vp_group
408 * @return
409 * - A new #fr_pair_t.
410 * - NULL on error.
411 */
412fr_pair_t *fr_pair_afrom_da_depth_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da, int start)
413{
414 fr_pair_t *vp;
415 unsigned int i;
416 TALLOC_CTX *cur_ctx;
417 fr_dict_attr_t const *find; /* DA currently being looked for */
418 fr_pair_list_t *cur_list; /* Current list being searched */
419 fr_da_stack_t da_stack;
420
421 fr_proto_da_stack_build(&da_stack, da);
422 cur_list = list;
423 cur_ctx = ctx;
424
425 for (i = start; i <= da->depth; i++) {
426 find = da_stack.da[i];
427
428 /*
429 * If we're asked to create a key field, then do it.
430 *
431 * Otherwise if we're creating a child struct (which is magically parented by the key
432 * field), then don't bother creating the key field. It will be automatically filled in
433 * by the encoder.
434 */
435 if ((find != da) && fr_dict_attr_is_key_field(find)) {
436 continue;
437 }
438
439 vp = fr_pair_find_by_da(cur_list, NULL, find);
440 if (!vp || (vp->da == da)) {
441 if (fr_pair_append_by_da(cur_ctx, &vp, cur_list, find) < 0) return NULL;
442 }
443
444 if (find == da) return vp;
445
447
448 cur_ctx = vp;
449 cur_list = &vp->vp_group;
450 }
451
452 fr_assert(0);
453
454 return NULL;
455}
456
457/** Create a pair (and all intermediate parents), and append it to the list
458 *
459 * If the relevant leaf pair already exists, then a new one is created.
460 *
461 * This function is similar to fr_pair_update_by_da_parent(), except that function requires
462 * a parent pair, and this one takes a separate talloc ctx and pair list.
463 *
464 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
465 * @param[out] list where the created pair is supposed to go.
466 * @param[in] da the da for the pair to create
467 * @return
468 * - A new #fr_pair_t.
469 * - NULL on error.
470 */
472{
473 if (da->depth <= 1) {
474 fr_pair_t *vp;
475
476 if (fr_pair_append_by_da(ctx, &vp, list, da) < 0) return NULL;
477 return vp;
478 }
479
480 return fr_pair_afrom_da_depth_nested(ctx, list, da, 0);
481}
482
483/** Copy a single valuepair
484 *
485 * Allocate a new valuepair and copy the da from the old vp.
486 *
487 * @param[in] ctx for talloc
488 * @param[in] vp to copy.
489 * @return
490 * - A copy of the input VP.
491 * - NULL on error.
492 */
493fr_pair_t *fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
494{
495 fr_pair_t *n;
496
498
499 n = fr_pair_afrom_da(ctx, vp->da);
500 if (!n) return NULL;
501
502 n->op = vp->op;
503
504 /*
505 * Groups are special.
506 */
507 if (fr_type_is_structural(n->vp_type)) {
508 if (fr_pair_list_copy(n, &n->vp_group, &vp->vp_group) < 0) {
509 talloc_free(n);
510 return NULL;
511 }
512
513 } else {
514 fr_value_box_copy(n, &n->data, &vp->data);
515 }
516
517 return n;
518}
519
520/** Steal one VP
521 *
522 * @param[in] ctx to move fr_pair_t into
523 * @param[in] vp fr_pair_t to move into the new context.
524 */
525int fr_pair_steal(TALLOC_CTX *ctx, fr_pair_t *vp)
526{
527 fr_pair_t *nvp;
528
529 nvp = talloc_steal(ctx, vp);
530 if (unlikely(!nvp)) {
531 fr_strerror_printf("Failed moving pair %pV to new ctx", vp);
532 return -1;
533 }
534
535 return 0;
536}
537
538#define IN_A_LIST_MSG "Pair %pV is already in a list, and cannot be moved"
539#define NOT_IN_THIS_LIST_MSG "Pair %pV is not in the given list"
540
541/** Change a vp's talloc ctx and insert it into a new list
542 *
543 * @param[in] list_ctx to move vp into.
544 * @param[out] list to add vp to.
545 * @param[in] vp to move.
546 * @return
547 * - 0 on success.
548 * - -1 on failure (already in list).
549 */
550int fr_pair_steal_append(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
551{
552 if (fr_pair_order_list_in_a_list(vp)) {
554 return -1;
555 }
556
557 if (unlikely(fr_pair_steal(list_ctx, vp) < 0)) return -1;
558
559 if (unlikely(fr_pair_append(list, vp) < 0)) return -1;
560
561 return 0;
562}
563
564/** Change a vp's talloc ctx and insert it into a new list
565 *
566 * @param[in] list_ctx to move vp into.
567 * @param[out] list to add vp to.
568 * @param[in] vp to move.
569 * @return
570 * - 0 on success.
571 * - -1 on failure (already in list).
572 */
573int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
574{
575 if (fr_pair_order_list_in_a_list(vp)) {
577 return -1;
578 }
579
580 if (unlikely(fr_pair_steal(list_ctx, vp) < 0)) return -1;
581
582 if (unlikely(fr_pair_prepend(list, vp) < 0)) return -1;
583
584 return 0;
585}
586
587/** Mark malformed attribute as raw
588 *
589 * @param[in] vp to mark as raw.
590 * @param[in] data to parse.
591 * @param[in] data_len of data to parse.
592 *
593 * @return
594 * - 0 on success
595 * - -1 on failure.
596 */
597int fr_pair_raw_afrom_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len)
598{
599 fr_dict_attr_t *unknown;
600
602
603 if (!fr_cond_assert(vp->da->flags.is_unknown == false)) return -1;
604
605 if (!fr_cond_assert(vp->da->parent != NULL)) return -1;
606
608 if (!unknown) return -1;
609
610 vp->da = unknown;
611 fr_assert(vp->da->type == FR_TYPE_OCTETS);
612
613 fr_value_box_init(&vp->data, FR_TYPE_OCTETS, NULL, true);
614
615 fr_pair_value_memdup(vp, data, data_len, true);
616
617 return 0;
618}
619
620/** Iterate over pairs with a specified da
621 *
622 * @param[in] list to iterate over.
623 * @param[in] current The fr_pair_t cursor->current. Will be advanced and checked to
624 * see if it matches the specified fr_dict_attr_t.
625 * @param[in] uctx The fr_dict_attr_t to search for.
626 * @return
627 * - Next matching fr_pair_t.
628 * - NULL if not more matching fr_pair_ts could be found.
629 */
630static void *fr_pair_iter_next_by_da(fr_dlist_head_t *list, void *current, void *uctx)
631{
632 fr_pair_t *c = current;
633 fr_dict_attr_t *da = uctx;
634
635 while ((c = fr_dlist_next(list, c))) {
636 PAIR_VERIFY(c);
637 if (c->da == da) break;
638 }
639
640 return c;
641}
642
643/** Iterate over pairs which are decedents of the specified da
644 *
645 * @param[in] list to iterate over.
646 * @param[in] current The fr_pair_t cursor->current. Will be advanced and checked to
647 * see if it matches the specified fr_dict_attr_t.
648 * @param[in] uctx The fr_dict_attr_t to search for.
649 * @return
650 * - Next matching fr_pair_t.
651 * - NULL if not more matching fr_pair_ts could be found.
652 */
653static void *fr_pair_iter_next_by_ancestor(fr_dlist_head_t *list, void *current, void *uctx)
654{
655 fr_pair_t *c = current;
656 fr_dict_attr_t *da = uctx;
657
658 while ((c = fr_dlist_next(list, c))) {
659 PAIR_VERIFY(c);
660 if (fr_dict_attr_common_parent(da, c->da, true)) break;
661 }
662
663 return c;
664}
665
666/** Return the number of instances of a given da in the specified list
667 *
668 * @param[in] list to search in.
669 * @param[in] da to look for in the list.
670 * @return
671 * - 0 if no instances exist.
672 * - >0 the number of instance of a given attribute.
673 */
674unsigned int fr_pair_count_by_da(fr_pair_list_t const *list, fr_dict_attr_t const *da)
675{
676 fr_pair_t *vp = NULL;
677 unsigned int count = 0;
678
679 if (fr_pair_list_empty(list)) return 0;
680
681 while ((vp = fr_pair_list_next(list, vp))) if (da == vp->da) count++;
682
683 return count;
684}
685
686/** Find the first pair with a matching da
687 *
688 * @param[in] list to search in.
689 * @param[in] prev the previous attribute in the list.
690 * @param[in] da the next da to find.
691 * @return
692 * - first matching fr_pair_t.
693 * - NULL if no fr_pair_ts match.
694 *
695 * @hidecallergraph
696 */
698{
699 fr_pair_t *vp = UNCONST(fr_pair_t *, prev);
700
701 if (fr_pair_list_empty(list)) return NULL;
702
703 PAIR_LIST_VERIFY(list);
704
705 while ((vp = fr_pair_list_next(list, vp))) if (da == vp->da) return vp;
706
707 return NULL;
708}
709
710/** Find the last pair with a matching da
711 *
712 * @param[in] list to search in.
713 * @param[in] prev the previous attribute in the list.
714 * @param[in] da the previous da to find.
715 * @return
716 * - first matching fr_pair_t.
717 * - NULL if no fr_pair_ts match.
718 *
719 * @hidecallergraph
720 */
722{
723 fr_pair_t *vp = UNCONST(fr_pair_t *, prev);
724
725 if (fr_pair_list_empty(list)) return NULL;
726
727 PAIR_LIST_VERIFY(list);
728
729 while ((vp = fr_pair_list_prev(list, vp))) if (da == vp->da) return vp;
730
731 return NULL;
732}
733
734/** Find a pair with a matching da at a given index
735 *
736 * @param[in] list to search in.
737 * @param[in] da to look for in the list.
738 * @param[in] idx Instance of the attribute to return.
739 * @return
740 * - first matching fr_pair_t.
741 * - NULL if no fr_pair_ts match.
742 *
743 * @hidecallergraph
744 */
745fr_pair_t *fr_pair_find_by_da_idx(fr_pair_list_t const *list, fr_dict_attr_t const *da, unsigned int idx)
746{
747 fr_pair_t *vp = NULL;
748
749 if (fr_pair_list_empty(list)) return NULL;
750
751 PAIR_LIST_VERIFY(list);
752
753 while ((vp = fr_pair_list_next(list, vp))) {
754 if (da != vp->da) continue;
755
756 if (idx == 0) return vp;
757
758 idx--;
759 }
760 return NULL;
761}
762
763/** Find a pair with a matching fr_dict_attr_t, by walking the nested fr_dict_attr_t tree
764 *
765 * The list should be the one containing the top level attributes.
766 *
767 * @param[in] list to search in.
768 * @param[in] prev pair to start searching from.
769 * @param[in] da the next da to find.
770 * @return
771 * - first matching fr_pair_t.
772 * - NULL if no fr_pair_ts match.
773 */
775{
776 fr_pair_t *vp;
777 fr_dict_attr_t const **find; /* DA currently being looked for */
778 fr_pair_list_t const *cur_list; /* Current list being searched */
779 fr_da_stack_t da_stack;
780
781 if (fr_pair_list_empty(list)) return NULL;
782
783 /*
784 * In the common case, we're looking for attributes in
785 * the root (at level 1), so we just skip to a special
786 * function for that
787 */
788 if (da->depth <= 1) return fr_pair_find_by_da(list, prev, da);
789
790 fr_proto_da_stack_build(&da_stack, da);
791
792 /*
793 * Find the relevant starting point for `prev`
794 */
795 if (prev) {
796 cur_list = fr_pair_parent_list(prev);
797 find = &da_stack.da[prev->da->depth - 1];
798 vp = UNCONST(fr_pair_t *, prev);
799 } else {
800 cur_list = list;
801 find = &da_stack.da[0];
802 vp = NULL;
803 }
804
805 /*
806 * Loop over the list at each level until we find a matching da.
807 */
808 while (true) {
809 fr_pair_t *next;
810
811 fr_assert((*find)->depth <= da->depth);
812
813 /*
814 * Find a vp which matches a given da. If found,
815 * recurse into the child list to find the child
816 * attribute.
817 *
818 */
819 next = fr_pair_find_by_da(cur_list, vp, *find);
820 if (next) {
821 /*
822 * We've found a match for the requested
823 * da - return it.
824 */
825 if ((*find) == da) return next;
826
827 /*
828 * Prepare to search the next level.
829 */
830 cur_list = &next->vp_group;
831 find++;
832 vp = NULL;
833 continue;
834 }
835
836 /*
837 * We hit the end of the top-level list. Therefore we found nothing.
838 */
839 if (cur_list == list) break;
840
841 /*
842 * We hit the end of *A* list. Go to the parent
843 * VP, and then find its list.
844 *
845 * We still then have to go to the next attribute
846 * in the parent list, as we've checked all of the
847 * children of this VP.
848 */
849 find--;
850 vp = fr_pair_list_parent(cur_list);
851 cur_list = fr_pair_parent_list(vp);
852 }
853
854 /*
855 * Compatiblitity with flat atttrbutes
856 */
857 if (fr_pair_parent_list(prev) != list) prev = NULL;
858 return fr_pair_find_by_da(list, prev, da);
859}
860
861/** Find the pair with the matching child attribute
862 *
863 * @param[in] list in which to search.
864 * @param[in] prev attribute to start search from.
865 * @param[in] parent attribute in which to lookup child.
866 * @param[in] attr id of child.
867 * @return
868 * - first matching value pair.
869 * - NULL if no pair found.
870 */
872 fr_dict_attr_t const *parent, unsigned int attr)
873{
874 fr_dict_attr_t const *da;
875
876 /* List head may be NULL if it contains no VPs */
877 if (fr_pair_list_empty(list)) return NULL;
878
879 PAIR_LIST_VERIFY(list);
880
882 if (!da) return NULL;
883
884 return fr_pair_find_by_da(list, prev, da);
885}
886
887/** Find the pair with the matching child attribute at a given index
888 *
889 * @param[in] list in which to search.
890 * @param[in] parent attribute in which to lookup child.
891 * @param[in] attr id of child.
892 * @param[in] idx Instance of the attribute to return.
893 * @return
894 * - first matching value pair.
895 * - NULL if no pair found.
896 */
898 fr_dict_attr_t const *parent, unsigned int attr, unsigned int idx)
899{
900 fr_dict_attr_t const *da;
901
902 /* List head may be NULL if it contains no VPs */
903 if (fr_pair_list_empty(list)) return NULL;
904
905 PAIR_LIST_VERIFY(list);
906
908 if (!da) return NULL;
909
910 return fr_pair_find_by_da_idx(list, da, idx);
911}
912
913/** Get the child list of a group
914 *
915 * @param[in] vp which MUST be of a type
916 * that can contain children.
917 * @return
918 * - NULL on error
919 * - pointer to head of the child list.
920 */
922{
923 if (!fr_type_is_structural(vp->vp_type)) return NULL;
924
925 return &vp->vp_group;
926}
927
928/** Return a pointer to the parent pair list
929 *
930 */
932{
933 FR_TLIST_HEAD(fr_pair_order_list) *parent;
934
935 if (!vp) return NULL;
936
937 parent = fr_pair_order_list_parent(vp);
938 if (!parent) return NULL;
939
940 return (fr_pair_list_t *) (UNCONST(uint8_t *, parent) - offsetof(fr_pair_list_t, order));
941}
942
943/** Return a pointer to the parent pair.
944 *
945 */
947{
949
950 if (!list) return NULL;
951
952 if (!list->is_child) return NULL;
953
954 return (fr_pair_t *) (UNCONST(uint8_t *, list) - offsetof(fr_pair_t, vp_group));
955}
956
957/** Return a pointer to the parent pair which contains this list.
958 *
959 */
961{
962 if (!list) return NULL;
963
964 if (!list->is_child) return NULL;
965
966 return (fr_pair_t *) (UNCONST(uint8_t *, list) - offsetof(fr_pair_t, vp_group));
967}
968
969/** Keep attr tree and sublists synced on cursor insert
970 *
971 * @param[in] list Underlying order list from the fr_pair_list_t.
972 * @param[in] to_insert fr_pair_t being inserted.
973 * @param[in] uctx fr_pair_list_t containing the order list.
974 * @return
975 * - 0 on success.
976 */
977static int _pair_list_dcursor_insert(fr_dlist_head_t *list, void *to_insert, UNUSED void *uctx)
978{
979 fr_pair_t *vp = to_insert;
980 fr_tlist_head_t *tlist;
981
982 tlist = fr_tlist_head_from_dlist(list);
983
984 /*
985 * Mark the pair as inserted into the list.
986 */
987 fr_pair_order_list_set_head(tlist, vp);
988
990
991 return 0;
992}
993
994/** Keep attr tree and sublists synced on cursor removal
995 *
996 * @param[in] list Underlying order list from the fr_pair_list_t.
997 * @param[in] to_remove fr_pair_t being removed.
998 * @param[in] uctx fr_pair_list_t containing the order list.
999 * @return
1000 * - 0 on success.
1001 */
1002static int _pair_list_dcursor_remove(NDEBUG_UNUSED fr_dlist_head_t *list, void *to_remove, UNUSED void *uctx)
1003{
1004 fr_pair_t *vp = to_remove;
1006
1007#ifndef NDEBUG
1008 fr_tlist_head_t *tlist;
1009
1010 tlist = fr_tlist_head_from_dlist(list);
1011
1012 while (parent && (tlist != vp->order_entry.entry.list_head)) {
1013 tlist = &parent->order.head;
1015 }
1016
1017 fr_assert(vp->order_entry.entry.list_head == tlist);
1019#endif
1020
1021 /*
1022 * Mark the pair as removed from the list.
1023 */
1024 fr_pair_order_list_set_head(NULL, vp);
1025
1026 PAIR_VERIFY(vp);
1027
1028 if (&parent->order.head.dlist_head == list) return 0;
1029
1031 return 1;
1032}
1033
1034/** Iterates over the leaves of a list
1035 *
1036 * @param[in] list to iterate over.
1037 * @param[in] vp the current CVP
1038 * @return
1039 * - NULL when done
1040 * - vp - a leaf pair
1041 */
1043{
1044 fr_pair_t *next, *parent;
1045 fr_pair_list_t *parent_list;
1046
1047 /*
1048 * Start: return the head of the top-level list.
1049 */
1050 if (!vp) {
1051 vp = fr_pair_list_head(list);
1052 if (!vp) goto next_parent_sibling;
1053
1054 next_sibling:
1055 if (fr_type_is_leaf(vp->vp_type)) return vp;
1056
1058
1059 vp = fr_pair_list_iter_leaf(&vp->vp_group, NULL);
1060 if (vp) return vp;
1061
1062 /*
1063 * vp is NULL, so we've processed all of its children.
1064 */
1065 }
1066
1067 /*
1068 * Go to the next sibling in the parent list of vp.
1069 */
1070next_parent_sibling:
1071 parent_list = fr_pair_parent_list(vp);
1072 if (!parent_list) return NULL;
1073
1074 next = fr_pair_list_next(parent_list, vp);
1075 if (!next) {
1076 /*
1077 * We're done the top-level list.
1078 */
1079 if (parent_list == list) return NULL;
1080
1082 fr_assert(&parent->vp_group == parent_list);
1083 vp = parent;
1084 goto next_parent_sibling;
1085 }
1086
1087 /*
1088 * We do have a "next" attribute. Go check if we can return it.
1089 */
1090 vp = next;
1091 goto next_sibling;
1092}
1093
1094/** Initialises a special dcursor with callbacks that will maintain the attr sublists correctly
1095 *
1096 * Filters can be applied later with fr_dcursor_filter_set.
1097 *
1098 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1099 *
1100 * @param[out] cursor to initialise.
1101 * @param[in] list to iterate over.
1102 * @param[in] iter Iterator to use when filtering pairs.
1103 * @param[in] uctx To pass to iterator.
1104 * @param[in] is_const whether the fr_pair_list_t is const.
1105 * @return
1106 * - NULL if src does not point to any items.
1107 * - The first pair in the list.
1108 */
1110 fr_dcursor_iter_t iter, void const *uctx,
1111 bool is_const)
1112{
1113 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1114 iter, NULL, uctx,
1116}
1117
1118/** Initialises a special dcursor with callbacks that will maintain the attr sublists correctly
1119 *
1120 * Filters can be applied later with fr_dcursor_filter_set.
1121 *
1122 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1123 *
1124 * @param[out] cursor to initialise.
1125 * @param[in] list to iterate over.
1126 * @param[in] is_const whether the fr_pair_list_t is const.
1127 * @return
1128 * - NULL if src does not point to any items.
1129 * - The first pair in the list.
1130 */
1132 bool is_const)
1133{
1134 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1135 NULL, NULL, NULL,
1137}
1138
1139/** Initialise a cursor that will return only attributes matching the specified #fr_dict_attr_t
1140 *
1141 * @param[in] cursor to initialise.
1142 * @param[in] list to iterate over.
1143 * @param[in] da to search for.
1144 * @param[in] is_const whether the fr_pair_list_t is const.
1145 * @return
1146 * - The first matching pair.
1147 * - NULL if no pairs match.
1148 */
1150 fr_pair_list_t const *list, fr_dict_attr_t const *da,
1151 bool is_const)
1152{
1153 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1154 fr_pair_iter_next_by_da, NULL, da,
1156}
1157
1158/** Initialise a cursor that will return only attributes descended from the specified #fr_dict_attr_t
1159 *
1160 * @param[in] cursor to initialise.
1161 * @param[in] list to iterate over.
1162 * @param[in] da who's decentness to search for.
1163 * @param[in] is_const whether the fr_pair_list_t is const.
1164 * @return
1165 * - The first matching pair.
1166 * - NULL if no pairs match.
1167 */
1169 fr_pair_list_t const *list, fr_dict_attr_t const *da,
1170 bool is_const)
1171{
1172 fr_pair_t *vp;
1173
1175
1176 /*
1177 * This function is only used by snmp.c and password.c. Once we've fully moved to
1178 * nested attributes, it should be removed.
1179 */
1180 fr_assert(da->parent->flags.is_root);
1181
1182 vp = fr_pair_find_by_da(list, NULL, da);
1183 if (vp) {
1184 list = &vp->vp_group;
1185
1186 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1187 NULL, NULL, NULL,
1189 }
1190
1191 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1194}
1195
1196/** Iterate over pairs
1197 *
1198 * @param[in] list to iterate over.
1199 * @param[in] current The fr_value_box_t cursor->current. Will be advanced and checked to
1200 * see if it matches the specified fr_dict_attr_t.
1201 * @param[in] uctx unused
1202 * @return
1203 * - Next matching fr_pair_t.
1204 * - NULL if not more matching fr_pair_ts could be found.
1205 */
1206static void *_fr_pair_iter_next_value(fr_dlist_head_t *list, void *current, UNUSED void *uctx)
1207{
1208 fr_pair_t *vp;
1209
1210 if (!current) {
1211 vp = NULL;
1212 } else {
1213 vp = (fr_pair_t *) ((uint8_t *) current - offsetof(fr_pair_t, data));
1214 PAIR_VERIFY(vp);
1215 }
1216
1217 while ((vp = fr_dlist_next(list, vp))) {
1218 PAIR_VERIFY(vp);
1219 if (fr_type_is_leaf(vp->vp_type)) return &vp->data;
1220 }
1221
1222 return NULL;
1223}
1224
1225/*
1226 * The value dcursor just runs the iterator, and never uses the dlist. Inserts and deletes are forbidden.
1227 *
1228 * However, the underlying dcursor code needs a dlist, so we create a fake one to pass it. In debug
1229 * builds, the dcursor code will do things like try to check talloc types. So we need to pass it an
1230 * empty dlist with no talloc types.
1231 */
1233 .offset = offsetof(fr_dlist_head_t, entry),
1234 .type = NULL,
1235 .num_elements = 0,
1236 .entry = {
1237 .prev = &value_dlist.entry,
1239 },
1240};
1241
1242/** Initialises a special dcursor over a #fr_pair_list_t, but which returns #fr_value_box_t
1243 *
1244 * Filters can be applied later with fr_dcursor_filter_set.
1245 *
1246 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1247 * @note - the list cannot be modified, and structural attributes are not returned.
1248 *
1249 * @param[out] cursor to initialise.
1250 * @return
1251 * - NULL if src does not point to any items.
1252 * - The first pair in the list.
1253 */
1255{
1256 return _fr_dcursor_init(cursor, &value_dlist,
1257 _fr_pair_iter_next_value, NULL, NULL, NULL, NULL, NULL, true);
1258}
1259
1260/** Iterate over pairs
1261 *
1262 * @param[in] list to iterate over.
1263 * @param[in] current The fr_value_box_t cursor->current. Will be advanced and checked to
1264 * see if it matches the specified fr_dict_attr_t.
1265 * @param[in] uctx The parent dcursor
1266 * @return
1267 * - Next matching fr_pair_t.
1268 * - NULL if not more matching fr_pair_ts could be found.
1269 */
1271{
1272 fr_pair_t *vp;
1273 fr_dcursor_t *parent = uctx;
1274
1275 if (!current) {
1277 if (!vp) return NULL;
1278
1279 PAIR_VERIFY(vp);
1280 return &vp->data;
1281 }
1282
1283 while ((vp = fr_dcursor_next(parent))) {
1284 PAIR_VERIFY(vp);
1285
1286 if (fr_type_is_leaf(vp->vp_type)) return &vp->data;
1287 }
1288
1289 return NULL;
1290}
1291
1292/** Initialises a special dcursor over another cursor which returns #fr_pair_t, but we return #fr_value_box_t
1293 *
1294 * Filters can be applied later with fr_dcursor_filter_set.
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 PAIR_VERIFY(to_add);
1354
1355#ifdef WITH_VERIFY_PTR
1356 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1357 list->verified = false;
1358#endif
1359
1360 if (fr_pair_order_list_in_a_list(to_add)) {
1362 return -1;
1363 }
1364
1365 fr_pair_order_list_insert_tail(&list->order, to_add);
1366
1367 return 0;
1368}
1369
1370/** Add a VP after another VP.
1371 *
1372 * @param[in] list VP in linked list. Will add new VP to this list.
1373 * @param[in] pos to insert pair after.
1374 * @param[in] to_add VP to add to list.
1375 * @return
1376 * - 0 on success.
1377 * - -1 on failure (pair already in list).
1378 */
1380{
1381 PAIR_VERIFY(to_add);
1382
1383#ifdef WITH_VERIFY_PTR
1384 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1385 list->verified = false;
1386#endif
1387
1388 if (fr_pair_order_list_in_a_list(to_add)) {
1390 return -1;
1391 }
1392
1393 if (pos && !fr_pair_order_list_in_list(&list->order, pos)) {
1395 return -1;
1396 }
1397
1398 fr_pair_order_list_insert_after(&list->order, pos, to_add);
1399
1400 return 0;
1401}
1402
1403/** Add a VP before another VP.
1404 *
1405 * @param[in] list VP in linked list. Will add new VP to this list.
1406 * @param[in] pos to insert pair after.
1407 * @param[in] to_add VP to add to list.
1408 * @return
1409 * - 0 on success.
1410 * - -1 on failure (pair already in list).
1411 */
1413{
1414 PAIR_VERIFY(to_add);
1415
1416#ifdef WITH_VERIFY_PTR
1417 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1418 fr_assert(!pos || fr_pair_order_list_in_a_list(pos));
1419 list->verified = false;
1420#endif
1421
1422 if (fr_pair_order_list_in_a_list(to_add)) {
1424 return -1;
1425 }
1426
1427 if (pos && !fr_pair_order_list_in_list(&list->order, pos)) {
1429 return -1;
1430 }
1431
1432 fr_pair_order_list_insert_before(&list->order, pos, to_add);
1433
1434 return 0;
1435}
1436
1437/** Replace a given VP
1438 *
1439 * @note Memory used by the VP being replaced will be freed.
1440 *
1441 * @param[in,out] list pair list
1442 * @param[in] to_replace pair to replace and free, on list
1443 * @param[in] vp New pair to insert.
1444 */
1446{
1447 PAIR_VERIFY_WITH_LIST(list, to_replace);
1448 PAIR_VERIFY(vp);
1449
1450#ifdef WITH_VERIFY_PTR
1451 fr_assert(!fr_pair_order_list_in_a_list(vp));
1452 fr_assert(fr_pair_order_list_in_a_list(to_replace));
1453 list->verified = false;
1454#endif
1455
1456 fr_pair_insert_after(list, to_replace, vp);
1457 fr_pair_remove(list, to_replace);
1458 talloc_free(to_replace);
1459}
1460
1461/** Alloc a new fr_pair_t (and append)
1462 *
1463 * @param[in] ctx to allocate new #fr_pair_t in.
1464 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1465 * care about manipulating the fr_pair_t.
1466 * @param[in,out] list in search and insert into.
1467 * @param[in] da of attribute to update.
1468 * @return
1469 * - 0 on success.
1470 * - -1 on failure.
1471 */
1472int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
1473{
1474 fr_pair_t *vp;
1475
1476 vp = fr_pair_afrom_da(ctx, da);
1477 if (unlikely(!vp)) {
1478 if (out) *out = NULL;
1479 return -1;
1480 }
1481
1482 fr_pair_append(list, vp);
1483 if (out) *out = vp;
1484
1485 return 0;
1486}
1487
1488/** Alloc a new fr_pair_t (and prepend)
1489 *
1490 * @param[in] ctx to allocate new #fr_pair_t in.
1491 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1492 * care about manipulating the fr_pair_t.
1493 * @param[in,out] list in search and insert into.
1494 * @param[in] da of attribute to update.
1495 * @return
1496 * - 0 on success.
1497 * - -1 on failure.
1498 */
1499int fr_pair_prepend_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
1500{
1501 fr_pair_t *vp;
1502
1503 vp = fr_pair_afrom_da(ctx, da);
1504 if (unlikely(!vp)) {
1505 if (out) *out = NULL;
1506 return -1;
1507 }
1508
1509 fr_pair_prepend(list, vp);
1510 if (out) *out = vp;
1511
1512 return 0;
1513}
1514
1515/** Alloc a new fr_pair_t, adding the parent attributes if required
1516 *
1517 * A child pair will be added to the first available matching parent
1518 * found.
1519 *
1520 * @param[in] ctx to allocate new #fr_pair_t in
1521 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1522 * care about manipulating the fr_pair_t.
1523 * @param[in] list in which to insert the pair.
1524 * @param[in] da of the attribute to create.
1525 * @return
1526 * - 0 on success.
1527 * - -1 on failure.
1528 */
1530{
1531 fr_pair_t *vp = NULL;
1532 fr_da_stack_t da_stack;
1533 fr_dict_attr_t const **find;
1534 TALLOC_CTX *pair_ctx = ctx;
1535
1536 /*
1537 * Fast path for non-nested attributes
1538 */
1539 if (da->depth <= 1) return fr_pair_append_by_da(ctx, out, list, da);
1540
1541 fr_proto_da_stack_build(&da_stack, da);
1542 find = &da_stack.da[0];
1543
1544 /*
1545 * Walk down the da stack looking for candidate parent
1546 * attributes and then allocating the leaf.
1547 */
1548 while (true) {
1549 fr_assert((*find)->depth <= da->depth);
1550
1551 /*
1552 * We're not at the leaf, look for a potential parent
1553 */
1554 if ((*find) != da) vp = fr_pair_find_by_da(list, NULL, *find);
1555
1556 /*
1557 * Nothing found, create the pair
1558 */
1559 if (!vp) {
1560 if (fr_pair_append_by_da(pair_ctx, &vp, list, *find) < 0) {
1561 if (out) *out = NULL;
1562 return -1;
1563 }
1564 }
1565
1566 /*
1567 * We're at the leaf, return
1568 */
1569 if ((*find) == da) {
1570 if(out) *out = vp;
1571 return 0;
1572 }
1573
1574 /*
1575 * Prepare for next level
1576 */
1577 list = &vp->vp_group;
1578 pair_ctx = vp;
1579 vp = NULL;
1580 find++;
1581 }
1582}
1583
1584/** Return the first fr_pair_t matching the #fr_dict_attr_t or alloc a new fr_pair_t and its subtree (and append)
1585 *
1586 * @param[in] parent If parent->da is an ancestor of the specified
1587 * da, we continue building out the nested structure
1588 * from the parent.
1589 * If parent is NOT an ancestor, then it must be a group
1590 * attribute, and we will append the shallowest member
1591 * of the struct or TLV as a child, and build out everything
1592 * to the specified da.
1593 * @param[out] out Pair we allocated or found. May be NULL if the caller doesn't
1594 * care about manipulating the fr_pair_t.
1595 * @param[in] da of attribute to locate or alloc.
1596 * @return
1597 * - 1 if attribute already existed.
1598 * - 0 if we allocated a new attribute.
1599 * - -1 on memory allocation failure.
1600 * - -2 if the parent is not a group attribute.
1601 */
1603 fr_dict_attr_t const *da)
1604{
1605 fr_pair_t *vp = NULL;
1606 fr_da_stack_t da_stack;
1607 fr_dict_attr_t const **find; /* ** to allow us to iterate */
1608 TALLOC_CTX *pair_ctx = parent;
1609 fr_pair_list_t *list = &parent->vp_group;
1610
1611 /*
1612 * Fast path for non-nested attributes
1613 */
1614 if (da->depth <= 1) {
1615 vp = fr_pair_find_by_da(list, NULL, da);
1616 if (vp) {
1617 if (out) *out = vp;
1618 return 1;
1619 }
1620
1621 return fr_pair_append_by_da(parent, out, list, da);
1622 }
1623
1624 fr_proto_da_stack_build(&da_stack, da);
1625 /*
1626 * Is parent an ancestor of the attribute we're trying
1627 * to build? If so, we resume from the deepest pairs
1628 * already created.
1629 *
1630 * da stack excludes the root.
1631 */
1632 if ((parent->da->depth < da->depth) && (da_stack.da[parent->da->depth - 1] == parent->da)) {
1633 /*
1634 * Start our search from the parent's children
1635 */
1636 list = &parent->vp_group;
1637 find = &da_stack.da[parent->da->depth]; /* Next deepest attr than parent */
1638 /*
1639 * Disallow building one TLV tree into another
1640 */
1641 } else if (!fr_type_is_group(parent->da->type)) {
1642 fr_strerror_printf("Expected parent \"%s\" to be an ancestor of \"%s\" or a group. "
1643 "But it is not an acestor and is of type %s", parent->da->name, da->name,
1644 fr_type_to_str(parent->da->type));
1645 return -2;
1646 } else {
1647 find = &da_stack.da[0];
1648 }
1649
1650 /*
1651 * Walk down the da stack looking for candidate parent
1652 * attributes and then allocating the leaf, and any
1653 * attributes between the leaf and parent.
1654 */
1655 while (true) {
1656 fr_assert((*find)->depth <= da->depth);
1657
1658 vp = fr_pair_find_by_da(list, NULL, *find);
1659 /*
1660 * Nothing found at this level, create the pair
1661 */
1662 if (!vp) {
1663 if (fr_pair_append_by_da(pair_ctx, &vp, list, *find) < 0) {
1664 if (out) *out = NULL;
1665 return -1;
1666 }
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_array_length(a->vp_strvalue) - 1,
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_array_length(value) - 1, 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 = (a_p->da < b_p->da) - (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 fr_value_box_copy(value, value, &vp->data);
2392 }
2393
2394 if (!first_added) first_added = value;
2395 fr_value_box_list_insert_tail(&dst->vb_group, value);
2396 }
2397
2398 return cnt;
2399}
2400
2401/** Duplicate pairs in a list matching the specified da
2402 *
2403 * Copy all pairs from 'from' matching the specified da.
2404 *
2405 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2406 * @param[in] to where to copy attributes to.
2407 * @param[in] from whence to copy #fr_pair_t (s).
2408 * @param[in] da to match.
2409 * @param[in] count How many instances to copy.
2410 * Use 0 for all attributes.
2411 * @return
2412 * - >0 the number of attributes copied.
2413 * - 0 if no attributes copied.
2414 * - -1 on error.
2415 */
2417 fr_pair_list_t const *from, fr_dict_attr_t const *da, unsigned int count)
2418{
2419 fr_pair_t *vp, *new_vp, *first_added = NULL;
2420 unsigned int cnt = 0;
2421
2422 if (count == 0) count = UINT_MAX;
2423
2424 if (unlikely(!da)) {
2425 fr_strerror_printf("No search attribute provided");
2426 return -1;
2427 }
2428
2429 for (vp = fr_pair_list_head(from);
2430 vp && (cnt < count);
2431 vp = fr_pair_list_next(from, vp)) {
2433
2434 if (vp->da != da) continue;
2435
2436 cnt++;
2437 new_vp = fr_pair_copy(ctx, vp);
2438 if (!new_vp) {
2439 fr_pair_order_list_talloc_free_to_tail(&to->order, first_added);
2440 return -1;
2441 }
2442
2443 if (!first_added) first_added = new_vp;
2444 fr_pair_append(to, new_vp);
2445 }
2446
2447 return cnt;
2448}
2449
2450/** Duplicate pairs in a list where the da is a descendant of parent_da
2451 *
2452 * Copy all pairs from 'from' which are descendants of the specified 'parent_da'.
2453 * This is particularly useful for copying attributes of a particular vendor, where the vendor
2454 * da is passed as parent_da.
2455 *
2456 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2457 * @param[in] to where to copy attributes to.
2458 * @param[in] from whence to copy #fr_pair_t (s).
2459 * @param[in] parent_da to match.
2460 * @return
2461 * - >0 one or more attributes were copied
2462 * - 0 if no attributes copied.
2463 * - -1 on error.
2464 */
2466 fr_pair_list_t const *from, fr_dict_attr_t const *parent_da)
2467{
2468 fr_pair_t *tlv;
2469 bool found = false;
2470
2471 if (!fr_type_is_structural(parent_da->type)) return -1;
2472
2473 /*
2474 * Allow for nested attributes.
2475 */
2476 tlv = fr_pair_find_by_da(from, NULL, parent_da);
2477 if (tlv) {
2478 fr_pair_t *vp;
2479
2480 vp = fr_pair_copy(ctx, tlv);
2481 if (!vp) return -1;
2482
2483 fr_pair_append(to, vp);
2484
2485 return 1;
2486 }
2487
2488 fr_pair_list_foreach(from, vp) {
2489 fr_pair_t *new_vp;
2490
2491 if (!fr_dict_attr_common_parent(parent_da, vp->da, true)) continue;
2492
2493 new_vp = fr_pair_copy(ctx, vp);
2494 if (unlikely(!new_vp)) return -1;
2495
2496 fr_pair_append(to, new_vp);
2497 found = true;
2498 }
2499
2500 return found;
2501}
2502
2503/** Duplicate a list of pairs starting at a particular item
2504 *
2505 * Copy all pairs from 'from' regardless of tag, attribute or vendor, starting at 'item'.
2506 *
2507 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2508 * @param[in] to where to copy attributes to.
2509 * @param[in] from whence to copy #fr_pair_t (s).
2510 * @param[in] start first pair to start copying from.
2511 * @param[in] count How many instances to copy.
2512 * Use 0 for all attributes.
2513 * @return
2514 * - >0 the number of attributes copied.
2515 * - 0 if no attributes copied.
2516 * - -1 on error.
2517 */
2518int fr_pair_sublist_copy(TALLOC_CTX *ctx, fr_pair_list_t *to,
2519 fr_pair_list_t const *from, fr_pair_t const *start, unsigned int count)
2520{
2521 fr_pair_t const *vp;
2522 fr_pair_t *new_vp;
2523 unsigned int cnt = 0;
2524
2525 if (!start) start = fr_pair_list_head(from);
2526
2527 for (vp = start;
2528 vp && ((count == 0) || (cnt < count));
2529 vp = fr_pair_list_next(from, vp), cnt++) {
2531 new_vp = fr_pair_copy(ctx, vp);
2532 if (unlikely(!new_vp)) return -1;
2533 fr_pair_append(to, new_vp);
2534 }
2535
2536 return cnt;
2537}
2538
2539/** Free/zero out value (or children) of a given VP
2540 *
2541 * @param[in] vp to clear value from.
2542 */
2544{
2545 fr_pair_t *child;
2546
2547 switch (vp->vp_type) {
2548 default:
2550 break;
2551
2552 case FR_TYPE_STRUCTURAL:
2553 if (!fr_pair_list_empty(&vp->vp_group)) return;
2554
2555 while ((child = fr_pair_order_list_pop_tail(&vp->vp_group.order))) {
2556 fr_pair_value_clear(child);
2557 talloc_free(child);
2558 }
2559 break;
2560 }
2561}
2562
2563/** Copy the value from one pair to another
2564 *
2565 * @param[out] dst where to copy the value to.
2566 * will clear assigned value.
2567 * @param[in] src where to copy the value from
2568 * Must have an assigned value.
2569 * @return
2570 * - 0 on success.
2571 * - -1 on failure.
2572 */
2574{
2575 if (!fr_cond_assert(src->data.type != FR_TYPE_NULL)) return -1;
2576
2577 if (dst->data.type != FR_TYPE_NULL) fr_value_box_clear_value(&dst->data);
2578 fr_value_box_copy(dst, &dst->data, &src->data);
2579
2580 /*
2581 * If either source or destination is secret, then this value is secret.
2582 */
2583 dst->data.secret |= src->da->flags.secret | dst->da->flags.secret | src->data.secret;
2584 return 0;
2585}
2586
2587/** Convert string value to native attribute value
2588 *
2589 * @param[in] vp to assign value to.
2590 * @param[in] value string to convert. Binary safe for variable
2591 * length values if len is provided.
2592 * @param[in] inlen The length of the input string.
2593 * @param[in] uerules used to perform unescaping.
2594 * @param[in] tainted Whether the value came from a trusted source.
2595 * @return
2596 * - 0 on success.
2597 * - -1 on failure.
2598 */
2600 fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
2601{
2602 /*
2603 * This is not yet supported because the rest of the APIs
2604 * to parse pair names, etc. don't yet enforce "inlen".
2605 * This is likely not a problem in practice, but we
2606 * haven't yet audited the uses of this function for that
2607 * behavior.
2608 */
2609 switch (vp->vp_type) {
2610 case FR_TYPE_STRUCTURAL:
2611 fr_strerror_printf("Attributes of type '%s' are not yet supported",
2612 fr_type_to_str(vp->vp_type));
2613 return -1;
2614
2615 default:
2616 break;
2617 }
2618
2619 /*
2620 * We presume that the input data is from a double quoted
2621 * string, and needs unescaping
2622 */
2623 if (fr_value_box_from_str(vp, &vp->data, vp->vp_type, vp->da,
2624 value, inlen,
2625 uerules) < 0) return -1;
2626
2627 fr_assert(vp->data.safe_for == FR_VALUE_BOX_SAFE_FOR_NONE);
2628
2629 PAIR_VERIFY(vp);
2630
2631 return 0;
2632}
2633
2634/** Copy data into an "string" data type.
2635 *
2636 * @note vp->da must be of type FR_TYPE_STRING.
2637 *
2638 * @param[in,out] vp to update
2639 * @param[in] src data to copy
2640 * @param[in] tainted Whether the value came from a trusted source.
2641 * @return
2642 * - 0 on success.
2643 * - -1 on failure.
2644 */
2645int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
2646{
2647 int ret;
2648
2649 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2650
2651 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2652 ret = fr_value_box_strdup(vp, &vp->data, vp->da, src, tainted);
2653 if (ret == 0) {
2654 PAIR_VERIFY(vp);
2655 }
2656
2657 return ret;
2658}
2659
2660/** Assign a buffer containing a nul terminated string to a vp, but don't copy it
2661 *
2662 * @param[in] vp to assign string to.
2663 * @param[in] src to copy string from.
2664 * @param[in] tainted Whether the value came from a trusted source.
2665 * @return
2666 * - 0 on success.
2667 * - -1 on failure.
2668 */
2669int fr_pair_value_strdup_shallow(fr_pair_t *vp, char const *src, bool tainted)
2670{
2671 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2672
2673 fr_value_box_clear(&vp->data);
2674 fr_value_box_strdup_shallow(&vp->data, vp->da, src, tainted);
2675
2676 PAIR_VERIFY(vp);
2677
2678 return 0;
2679}
2680
2681/** Trim the length of the string buffer to match the length of the C string
2682 *
2683 * @param[in,out] vp to trim.
2684 * @return
2685 * - 0 on success.
2686 * - -1 on failure.
2687 */
2689{
2690 int ret;
2691
2692 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2693
2694 ret = fr_value_box_strtrim(vp, &vp->data);
2695 if (ret == 0) {
2696 PAIR_VERIFY(vp);
2697 }
2698
2699 return ret;
2700}
2701
2702/** Print data into an "string" data type.
2703 *
2704 * @note vp->da must be of type FR_TYPE_STRING.
2705 *
2706 * @param[in,out] vp to update
2707 * @param[in] fmt the format string
2708 */
2709int fr_pair_value_aprintf(fr_pair_t *vp, char const *fmt, ...)
2710{
2711 int ret;
2712 va_list ap;
2713
2714 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2715
2716 fr_value_box_clear(&vp->data);
2717 va_start(ap, fmt);
2718 ret = fr_value_box_vasprintf(vp, &vp->data, vp->da, false, fmt, ap);
2719 va_end(ap);
2720
2721 if (ret == 0) {
2722 PAIR_VERIFY(vp);
2723 }
2724
2725 return ret;
2726}
2727
2728/** Pre-allocate a memory buffer for a "string" type value pair
2729 *
2730 * @note Will clear existing values (including buffers).
2731 *
2732 * @param[in,out] vp to update
2733 * @param[out] out If non-null will be filled with a pointer to the
2734 * new buffer.
2735 * @param[in] size of the data.
2736 * @param[in] tainted Whether the value came from a trusted source.
2737 * @return
2738 * - 0 on success.
2739 * - -1 on failure.
2740 */
2741int fr_pair_value_bstr_alloc(fr_pair_t *vp, char **out, size_t size, bool tainted)
2742{
2743 int ret;
2744
2745 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2746
2747 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2748 ret = fr_value_box_bstr_alloc(vp, out, &vp->data, vp->da, size, tainted);
2749 if (ret == 0) {
2750 PAIR_VERIFY(vp);
2751 }
2752
2753 return ret;
2754}
2755
2756/** Change the length of a buffer for a "string" type value pair
2757 *
2758 * @param[in,out] vp to update
2759 * @param[out] out If non-null will be filled with a pointer to the
2760 * new buffer.
2761 * @param[in] size of the data.
2762 * @return
2763 * - 0 on success.
2764 * - -1 on failure.
2765 */
2766int fr_pair_value_bstr_realloc(fr_pair_t *vp, char **out, size_t size)
2767{
2768 int ret;
2769
2770 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2771
2772 ret = fr_value_box_bstr_realloc(vp, out, &vp->data, size);
2773 if (ret == 0) {
2774 PAIR_VERIFY(vp);
2775 }
2776
2777 return ret;
2778}
2779
2780/** Copy data into a "string" type value pair
2781 *
2782 * @note unlike the original strncpy, this function does not stop
2783 * if it finds \0 bytes embedded in the string.
2784 *
2785 * @note vp->da must be of type FR_TYPE_STRING.
2786 *
2787 * @param[in,out] vp to update.
2788 * @param[in] src data to copy.
2789 * @param[in] len of data to copy.
2790 * @param[in] tainted Whether the value came from a trusted source.
2791 * @return
2792 * - 0 on success.
2793 * - -1 on failure.
2794 */
2795int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
2796{
2797 int ret;
2798
2799 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2800
2801 fr_value_box_clear(&vp->data);
2802 ret = fr_value_box_bstrndup(vp, &vp->data, vp->da, src, len, tainted);
2803 if (ret == 0) {
2804 PAIR_VERIFY(vp);
2805 }
2806
2807 return ret;
2808}
2809
2810/** Copy a nul terminated talloced buffer a "string" type value pair
2811 *
2812 * The buffer must be \0 terminated, or an error will be returned.
2813 *
2814 * @param[in,out] vp to update.
2815 * @param[in] src a talloced nul terminated buffer.
2816 * @param[in] tainted Whether the value came from a trusted source.
2817 * @return
2818 * - 0 on success.
2819 * - -1 on failure.
2820 */
2821int fr_pair_value_bstrdup_buffer(fr_pair_t *vp, char const *src, bool tainted)
2822{
2823 int ret;
2824
2825 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2826
2827 fr_value_box_clear(&vp->data);
2828 ret = fr_value_box_bstrdup_buffer(vp, &vp->data, vp->da, src, tainted);
2829 if (ret == 0) {
2830 PAIR_VERIFY(vp);
2831 }
2832
2833 return ret;
2834}
2835
2836/** Assign a string to a "string" type value pair
2837 *
2838 * @param[in] vp to assign new buffer to.
2839 * @param[in] src a string.
2840 * @param[in] len of src.
2841 * @param[in] tainted Whether the value came from a trusted source.
2842 * @return
2843 * - 0 on success.
2844 * - -1 on failure.
2845 */
2846int fr_pair_value_bstrndup_shallow(fr_pair_t *vp, char const *src, size_t len, bool tainted)
2847{
2848 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2849
2850 fr_value_box_clear(&vp->data);
2851 fr_value_box_bstrndup_shallow(&vp->data, vp->da, src, len, tainted);
2852 PAIR_VERIFY(vp);
2853
2854 return 0;
2855}
2856
2857/** Assign a string to a "string" type value pair
2858 *
2859 * @param[in] vp to assign new buffer to.
2860 * @param[in] src a string.
2861 * @param[in] tainted Whether the value came from a trusted source.
2862 * @return
2863 * - 0 on success.
2864 * - -1 on failure.
2865 */
2866int fr_pair_value_bstrdup_buffer_shallow(fr_pair_t *vp, char const *src, bool tainted)
2867{
2868 int ret;
2869
2870 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2871
2872 fr_value_box_clear(&vp->data);
2873 ret = fr_value_box_bstrdup_buffer_shallow(NULL, &vp->data, vp->da, src, tainted);
2874 if (ret == 0) {
2875 PAIR_VERIFY(vp);
2876 }
2877
2878 return ret;
2879}
2880
2881/** Pre-allocate a memory buffer for a "octets" type value pair
2882 *
2883 * @note Will clear existing values (including buffers).
2884 *
2885 * @param[in,out] vp to update
2886 * @param[out] out If non-null will be filled with a pointer to the
2887 * new buffer.
2888 * @param[in] size of the data.
2889 * @param[in] tainted Whether the value came from a trusted source.
2890 * @return
2891 * - 0 on success.
2892 * - -1 on failure.
2893 */
2894int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
2895{
2896 int ret;
2897
2898 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2899
2900 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2901 ret = fr_value_box_mem_alloc(vp, out, &vp->data, vp->da, size, tainted);
2902 if (ret == 0) {
2903 PAIR_VERIFY(vp);
2904 }
2905
2906 return ret;
2907}
2908
2909/** Change the length of a buffer for a "octets" type value pair
2910 *
2911 * @param[in,out] vp to update
2912 * @param[out] out If non-null will be filled with a pointer to the
2913 * new buffer.
2914 * @param[in] size of the data.
2915 * @return
2916 * - 0 on success.
2917 * - -1 on failure.
2918 */
2920{
2921 int ret;
2922
2923 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2924
2925 ret = fr_value_box_mem_realloc(vp, out, &vp->data, size);
2926 if (ret == 0) {
2927 PAIR_VERIFY(vp);
2928 }
2929
2930 return ret;
2931}
2932
2933/** Copy data into an "octets" data type.
2934 *
2935 * @note Will clear existing values (including buffers).
2936 *
2937 * @param[in,out] vp to update
2938 * @param[in] src data to copy
2939 * @param[in] len of the data.
2940 * @param[in] tainted Whether the value came from a trusted source.
2941 * @return
2942 * - 0 on success.
2943 * - -1 on failure.
2944 */
2945int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
2946{
2947 int ret;
2948
2949 if (unlikely((len > 0) && !src)) {
2950 fr_strerror_printf("Invalid arguments to %s. Len > 0 (%zu) but src was NULL",
2951 __FUNCTION__, len);
2952 return -1;
2953 }
2954
2955 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2956
2957 fr_value_box_clear_value(&vp->data); /* Free any existing buffers */
2958 ret = fr_value_box_memdup(vp, &vp->data, vp->da, src, len, tainted);
2959 if (ret == 0) PAIR_VERIFY(vp);
2960
2961 return ret;
2962}
2963
2964/** Copy data from a talloced buffer into an "octets" data type.
2965 *
2966 * @note Will clear existing values (including buffers).
2967 *
2968 * @param[in,out] vp to update
2969 * @param[in] src data to copy
2970 * @param[in] tainted Whether the value came from a trusted source.
2971 * @return
2972 * - 0 on success.
2973 * - -1 on failure.
2974 */
2975int fr_pair_value_memdup_buffer(fr_pair_t *vp, uint8_t const *src, bool tainted)
2976{
2977 int ret;
2978
2979 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2980
2981 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2982 ret = fr_value_box_memdup_buffer(vp, &vp->data, vp->da, src, tainted);
2983 if (ret == 0) {
2984 PAIR_VERIFY(vp);
2985 }
2986
2987 return ret;
2988}
2989
2990/** Assign a buffer to a "octets" type value pair
2991 *
2992 * @param[in] vp to assign new buffer to.
2993 * @param[in] src data to copy.
2994 * @param[in] len of src.
2995 * @param[in] tainted Whether the value came from a trusted source.
2996 * @return
2997 * - 0 on success.
2998 * - -1 on failure.
2999 */
3000int fr_pair_value_memdup_shallow(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
3001{
3002 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
3003
3004 fr_value_box_clear(&vp->data);
3005 fr_value_box_memdup_shallow(&vp->data, vp->da, src, len, tainted);
3006 PAIR_VERIFY(vp);
3007
3008 return 0;
3009}
3010
3011/** Assign a talloced buffer to a "octets" type value pair
3012 *
3013 * @param[in] vp to assign new buffer to.
3014 * @param[in] src data to copy.
3015 * @param[in] tainted Whether the value came from a trusted source.
3016 * @return
3017 * - 0 on success.
3018 * - -1 on failure.
3019 */
3021{
3022 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
3023
3024 fr_value_box_clear(&vp->data);
3025 fr_value_box_memdup_buffer_shallow(NULL, &vp->data, vp->da, src, tainted);
3026 PAIR_VERIFY(vp);
3027
3028 return 0;
3029}
3030
3031
3032/** Return a const buffer for an enum type attribute
3033 *
3034 * Where the vp type is numeric but does not have any enumv, or its value
3035 * does not map to an enumv, the integer value of the pair will be printed
3036 * to buff, and a pointer to buff will be returned.
3037 *
3038 * @param[in] vp to print.
3039 * @param[in] buff to print integer value to.
3040 * @return a talloced buffer.
3041 */
3042char const *fr_pair_value_enum(fr_pair_t const *vp, char buff[20])
3043{
3044 fr_dict_enum_value_t const *enumv;
3045
3046 if (!fr_box_is_numeric(&vp->data)) {
3047 fr_strerror_printf("Pair %s is not numeric", vp->da->name);
3048 return NULL;
3049 }
3050
3051 if (vp->da->flags.has_value) switch (vp->vp_type) {
3052 case FR_TYPE_BOOL:
3053 return vp->vp_bool ? "yes" : "no";
3054
3055 default:
3056 enumv = fr_dict_enum_by_value(vp->da, &vp->data);
3057 if (enumv) return enumv->name;
3058 break;
3059 }
3060
3062 return buff;
3063}
3064
3065/** Get value box of a VP, optionally prefer enum value.
3066 *
3067 * Get the data value box of the given VP. If 'e' is set to 1 and the VP has an
3068 * enum value, this will be returned instead. Otherwise it will be set to the
3069 * value box of the VP itself.
3070 *
3071 * @param[out] out pointer to a value box.
3072 * @param[in] vp to print.
3073 * @return 1 if the enum value has been used, 0 otherwise, -1 on error.
3074 */
3076{
3077 fr_dict_enum_value_t const *dv;
3078
3079 if (vp->da && vp->da->flags.has_value &&
3080 (dv = fr_dict_enum_by_value(vp->da, &vp->data))) {
3081 *out = dv->value;
3082 return 1;
3083 }
3084
3085 *out = &vp->data;
3086 return 0;
3087}
3088
3089#ifdef WITH_VERIFY_PTR
3090/*
3091 * Verify a fr_pair_t
3092 */
3093void fr_pair_verify(char const *file, int line, fr_pair_list_t const *list, fr_pair_t const *vp)
3094{
3096
3097 if (!vp->da) {
3098 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t da pointer was NULL", file, line);
3099 }
3100
3102 if (list) {
3103 fr_fatal_assert_msg(fr_pair_order_list_parent(vp) == &list->order,
3104 "CONSISTENCY CHECK FAILED %s[%d]: pair does not have the correct parentage "
3105 "at \"%s\"",
3106 file, line, vp->da->name);
3107 }
3108
3109 /*
3110 * This field is only valid for non-structural pairs
3111 */
3112 if (!fr_type_is_structural(vp->vp_type)) {
3114
3115 if (vp->data.enumv) fr_dict_attr_verify(file, line, vp->data.enumv);
3116
3117 if (parent && !fr_dict_attr_can_contain(parent->da, vp->da)) {
3118 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" should be parented by %s, but is parented by %s",
3119 file, line, vp->da->name, vp->da->parent->name, parent->da->name);
3120 }
3121
3122 } else {
3124
3125 if (parent && (parent->vp_type != FR_TYPE_GROUP) && (parent->da == vp->da)) {
3126 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" structural (non-group) type contains itself",
3127 file, line, vp->da->name);
3128 }
3129
3130 fr_pair_list_verify(file, line, vp, &vp->vp_group);
3131 }
3132
3133 switch (vp->vp_type) {
3134 case FR_TYPE_OCTETS:
3135 {
3136 size_t len;
3137 TALLOC_CTX *parent;
3138
3139 if (!vp->vp_octets) break; /* We might be in the middle of initialisation */
3140
3141 if (!talloc_get_type(vp->vp_ptr, uint8_t)) {
3142 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" data buffer type should be "
3143 "uint8_t but is %s", file, line, vp->da->name, talloc_get_name(vp->vp_ptr));
3144 }
3145
3146 len = talloc_array_length(vp->vp_octets);
3147 if (vp->vp_length > len) {
3148 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" length %zu is greater than "
3149 "uint8_t data buffer length %zu", file, line, vp->da->name, vp->vp_length, len);
3150 }
3151
3152 parent = talloc_parent(vp->vp_ptr);
3153 if (parent != vp) {
3154 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" char buffer is not "
3155 "parented by fr_pair_t %p, instead parented by %p (%s)",
3156 file, line, vp->da->name,
3157 vp, parent, parent ? talloc_get_name(parent) : "NULL");
3158 }
3159 }
3160 break;
3161
3162 case FR_TYPE_STRING:
3163 {
3164 size_t len;
3165 TALLOC_CTX *parent;
3166
3167 if (!vp->vp_octets) break; /* We might be in the middle of initialisation */
3168
3169 if (!talloc_get_type(vp->vp_ptr, char)) {
3170 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" data buffer type should be "
3171 "char but is %s", file, line, vp->da->name, talloc_get_name(vp->vp_ptr));
3172 }
3173
3174 len = (talloc_array_length(vp->vp_strvalue) - 1);
3175 if (vp->vp_length > len) {
3176 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" length %zu is greater than "
3177 "char buffer length %zu", file, line, vp->da->name, vp->vp_length, len);
3178 }
3179
3180 if (vp->vp_strvalue[vp->vp_length] != '\0') {
3181 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" char buffer not \\0 "
3182 "terminated", file, line, vp->da->name);
3183 }
3184
3185 parent = talloc_parent(vp->vp_ptr);
3186 if (parent != vp) {
3187 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" char buffer is not "
3188 "parented by fr_pair_t %p, instead parented by %p (%s)",
3189 file, line, vp->da->name,
3190 vp, parent, parent ? talloc_get_name(parent) : "NULL");
3192 }
3193 }
3194 break;
3195
3196 case FR_TYPE_IPV4_ADDR:
3197 if (vp->vp_ip.af != AF_INET) {
3198 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" address family is not "
3199 "set correctly for IPv4 address. Expected %i got %i",
3200 file, line, vp->da->name,
3201 AF_INET, vp->vp_ip.af);
3202 }
3203 if (vp->vp_ip.prefix != 32) {
3204 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" address prefix "
3205 "set correctly for IPv4 address. Expected %i got %i",
3206 file, line, vp->da->name,
3207 32, vp->vp_ip.prefix);
3208 }
3209 break;
3210
3211 case FR_TYPE_IPV6_ADDR:
3212 if (vp->vp_ip.af != AF_INET6) {
3213 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" address family is not "
3214 "set correctly for IPv6 address. Expected %i got %i",
3215 file, line, vp->da->name,
3216 AF_INET6, vp->vp_ip.af);
3217 }
3218 if (vp->vp_ip.prefix != 128) {
3219 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" address prefix "
3220 "set correctly for IPv6 address. Expected %i got %i",
3221 file, line, vp->da->name,
3222 128, vp->vp_ip.prefix);
3223 }
3224 break;
3225
3226 case FR_TYPE_STRUCTURAL:
3227 {
3228 if (vp->vp_group.verified) break;
3229
3230 fr_pair_list_foreach(&vp->vp_group, child) {
3231 TALLOC_CTX *parent = talloc_parent(child);
3232
3234 "CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" should be parented "
3235 "by fr_pair_t \"%s\". Expected talloc parent %p (%s) got %p (%s)",
3236 file, line,
3237 child->da->name, vp->da->name,
3238 vp, talloc_get_name(vp),
3239 parent, talloc_get_name(parent));
3240
3241 /*
3242 * Check if the child can be in the parent.
3243 */
3245 "CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t \"%s\" should be parented "
3246 "by fr_pair_t \"%s\", but it is instead parented by \"%s\"",
3247 file, line,
3248 child->da->name, child->da->parent->name, vp->da->name);
3249
3250 fr_pair_verify(file, line, &vp->vp_group, child);
3251 }
3252
3253 UNCONST(fr_pair_t *, vp)->vp_group.verified = true;
3254 }
3255 break;
3256
3257 default:
3258 break;
3259 }
3260
3261 if (vp->da->flags.is_unknown || vp->vp_raw) {
3263
3264 } else {
3265 fr_dict_attr_t const *da;
3266
3267 da = vp->da;
3268 if (da != vp->da) {
3269 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t "
3270 "dictionary pointer %p \"%s\" (%s) "
3271 "and global dictionary pointer %p \"%s\" (%s) differ",
3272 file, line, vp->da, vp->da->name,
3273 fr_type_to_str(vp->vp_type),
3274 da, da->name,
3275 fr_type_to_str(da->type));
3276 }
3277 }
3278
3279 if (vp->vp_raw || vp->da->flags.is_unknown) {
3280 /*
3281 * Raw or unknown attributes can have specific data types. See DER and CBOR.
3282 */
3283
3284 } else if (fr_type_is_leaf(vp->vp_type) && (vp->vp_type != vp->da->type) &&
3285 !((vp->da->type == FR_TYPE_COMBO_IP_ADDR) && ((vp->vp_type == FR_TYPE_IPV4_ADDR) || (vp->vp_type == FR_TYPE_IPV6_ADDR))) &&
3286 !((vp->da->type == FR_TYPE_COMBO_IP_PREFIX) && ((vp->vp_type == FR_TYPE_IPV4_PREFIX) || (vp->vp_type == FR_TYPE_IPV6_PREFIX)))) {
3287 char data_type_int[10], da_type_int[10];
3288
3289 snprintf(data_type_int, sizeof(data_type_int), "%u", vp->vp_type);
3290 snprintf(da_type_int, sizeof(da_type_int), "%u", vp->vp_type);
3291
3292 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t attribute %p \"%s\" "
3293 "data type (%s) does not match da type (%s)",
3294 file, line, vp->da, vp->da->name,
3295 fr_table_str_by_value(fr_type_table, vp->vp_type, data_type_int),
3296 fr_table_str_by_value(fr_type_table, vp->vp_type, da_type_int));
3297 }
3298}
3299
3300/** Verify a pair list
3301 *
3302 * @param[in] file from which the verification is called
3303 * @param[in] line number in file
3304 * @param[in] expected talloc ctx pairs should have been allocated in
3305 * @param[in] list of fr_pair_ts to verify
3306 */
3307void fr_pair_list_verify(char const *file, int line, TALLOC_CTX const *expected, fr_pair_list_t const *list)
3308{
3309 fr_pair_t *slow, *fast;
3310 TALLOC_CTX *parent;
3311
3312 if (fr_pair_list_empty(list)) return; /* Fast path */
3313
3314 /*
3315 * Only verify the list if it has been modified.
3316 */
3317 if (list->verified) return;
3318
3319 for (slow = fr_pair_list_head(list), fast = fr_pair_list_head(list);
3320 slow && fast;
3321 slow = fr_pair_list_next(list, slow), fast = fr_pair_list_next(list, fast)) {
3322 PAIR_VERIFY_WITH_LIST(list, slow);
3323
3324 /*
3325 * Advances twice as fast as slow...
3326 */
3327 fast = fr_pair_list_next(list, fast);
3328 fr_fatal_assert_msg(fast != slow,
3329 "CONSISTENCY CHECK FAILED %s[%d]: Looping list found. Fast pointer hit "
3330 "slow pointer at \"%s\"",
3331 file, line, slow->da->name);
3332
3333 parent = talloc_parent(slow);
3334 if (expected && (parent != expected)) {
3335 bad_parent:
3336 fr_log_talloc_report(expected);
3338
3339 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: Expected fr_pair_t \"%s\" to be parented "
3340 "by %p (%s), instead parented by %p (%s)\n",
3341 file, line, slow->da->name,
3342 expected, talloc_get_name(expected),
3343 parent, parent ? talloc_get_name(parent) : "NULL");
3344 }
3345 }
3346
3347 /*
3348 * Check the remaining pairs
3349 */
3350 for (; slow; slow = fr_pair_list_next(list, slow)) {
3351 PAIR_VERIFY_WITH_LIST(list, slow);
3352
3353 parent = talloc_parent(slow);
3354 if (expected && (parent != expected)) goto bad_parent;
3355 }
3356
3357 UNCONST(fr_pair_list_t *, list)->verified = true;
3358}
3359#endif
3360
3361/** Mark up a list of VPs as tainted.
3362 *
3363 */
3365{
3366 if (fr_pair_list_empty(list)) return;
3367
3368 fr_pair_list_foreach(list, vp) {
3370
3371 switch (vp->vp_type) {
3372 case FR_TYPE_STRUCTURAL:
3373 fr_pair_list_tainted(&vp->vp_group);
3374 break;
3375
3376 default:
3377 break;
3378 }
3379
3380 vp->vp_tainted = true;
3381 }
3382}
3383
3384/** Evaluation function for matching if vp matches a given da
3385 *
3386 * Can be used as a filter function for fr_dcursor_filter_next()
3387 *
3388 * @param item pointer to a fr_pair_t
3389 * @param uctx da to match
3390 *
3391 * @return true if the pair matches the da
3392 */
3393bool fr_pair_matches_da(void const *item, void const *uctx)
3394{
3395 fr_pair_t const *vp = item;
3396 fr_dict_attr_t const *da = uctx;
3397 return da == vp->da;
3398}
3399
3400/** Find or allocate a parent attribute.
3401 *
3402 * The input da is somewhere down inside of the da hierarchy. We
3403 * need to recursively find or create parent VPs which match the
3404 * given da.
3405 *
3406 * We find (or add) the VP into the "in" list. Any newly created VP
3407 * is inserted before "next". Or if "next==NULL", at the tail of
3408 * "in".
3409 *
3410 * @param[in] in the parent vp to look in
3411 * @param[in] item if we create a new vp, insert it before this item
3412 * @param[in] da look for vps in the parent which match this da
3413 * @return
3414 * - NULL on OOM
3415 * - parent vp we've found or allocated.
3416 */
3418{
3419 fr_pair_t *parent, *vp;
3420
3421 /*
3422 * We should never be called with a leaf da.
3423 *
3424 * If we're asked to create children of a keyed
3425 * structure, just create the children in the parent.
3426 */
3427 if (!fr_type_is_structural(da->type)) {
3429 da = da->parent;
3430 }
3431
3433
3434 /*
3435 * We're looking for a parent in the root of the
3436 * dictionary. Find the relevant VP in the current
3437 * container.
3438 *
3439 * If it's not found, allocate it, and insert it into the
3440 * list. Note that we insert it before the given "item"
3441 * vp so that we don't loop over the newly created pair
3442 * as we're processing the list.
3443 */
3444 if (da->flags.is_root || (da->parent == in->da)) {
3445 return in;
3446 }
3447
3448 /*
3449 * We're not at the root. Go find (or create) the parent
3450 * of this da.
3451 */
3452 parent = pair_alloc_parent(in, item, da->parent);
3453 if (!parent) return NULL;
3454
3455 /*
3456 * We have the parent attribute, maybe it already
3457 * contains the da we're looking for?
3458 */
3459 vp = fr_pair_find_by_da(&parent->vp_group, NULL, da);
3460 if (vp) return vp;
3461
3462 /*
3463 * Now that the entire set of parents has been created,
3464 * create the final VP. Make sure it's in the parent,
3465 * and return it.
3466 */
3467 vp = fr_pair_afrom_da(parent, da);
3468 if (!vp) return NULL;
3469
3470 /*
3471 * If we are at the root, and have been provided with
3472 * an entry to insert before, then do that.
3473 */
3474 if (item && da->parent->flags.is_root) {
3475 fr_pair_insert_before(&parent->vp_group, item, vp);
3476 } else {
3477 fr_pair_append(&parent->vp_group, vp);
3478 }
3479 return vp;
3480}
3481
3482/** Parse a list of VPs from a value box.
3483 *
3484 * @param[in] ctx to allocate new VPs in
3485 * @param[out] out list to add new pairs to
3486 * @param[in] dict to use in parsing
3487 * @param[in] box whose value is to be parsed
3488 */
3489void fr_pair_list_afrom_box(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_t const *dict, fr_value_box_t *box)
3490{
3491 fr_pair_parse_t root, relative;
3492
3493 fr_assert(box->type == FR_TYPE_STRING);
3494
3495 root = (fr_pair_parse_t) {
3496 .ctx = ctx,
3497 .da = fr_dict_root(dict),
3498 .list = out,
3499 .allow_crlf = true,
3500 .tainted = box->tainted,
3501 };
3502 relative = (fr_pair_parse_t) { };
3503
3504 if (fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(box->vb_strvalue, box->vb_length)) < 0) {
3505 return;
3506 }
3507}
3508
3509static const char spaces[] = " ";
3510
3511static void fprintf_pair_list(FILE *fp, fr_pair_list_t const *list, int depth)
3512{
3513 fr_pair_list_foreach(list, vp) {
3514 fprintf(fp, "%.*s", depth, spaces);
3515
3516 if (fr_type_is_leaf(vp->vp_type)) {
3517 fr_fprintf(fp, "%s %s %pV\n", vp->da->name, fr_tokens[vp->op], &vp->data);
3518 continue;
3519 }
3520
3522
3523 fprintf(fp, "%s = {\n", vp->da->name);
3524 fprintf_pair_list(fp, &vp->vp_group, depth + 1);
3525 fprintf(fp, "%.*s}\n", depth, spaces);
3526 }
3527}
3528
3529void fr_fprintf_pair_list(FILE *fp, fr_pair_list_t const *list)
3530{
3531 fprintf_pair_list(fp, list, 0);
3532}
3533
3534/*
3535 * print.c doesn't include pair.h, and doing so causes too many knock-on effects.
3536 */
3537void fr_fprintf_pair(FILE *fp, char const *msg, fr_pair_t const *vp)
3538{
3539 if (msg) fputs(msg, fp);
3540
3541 if (fr_type_is_leaf(vp->vp_type)) {
3542 fr_fprintf(fp, "%s %s %pV\n", vp->da->name, fr_tokens[vp->op], &vp->data);
3543 } else {
3545
3546 fprintf(fp, "%s = {\n", vp->da->name);
3547 fprintf_pair_list(fp, &vp->vp_group, 1);
3548 fprintf(fp, "}\n");
3549 }
3550}
int const char * file
Definition acutest.h:702
va_end(args)
int n
Definition acutest.h:577
log_entry msg
Definition acutest.h:794
static int const char * fmt
Definition acutest.h:573
int const char int line
Definition acutest.h:702
va_start(args, fmt)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:485
#define NDEBUG_UNUSED
Definition build.h:328
#define 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_dlist_head_t *list, 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
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:763
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:1147
#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:191
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
#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:184
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:2040
static fr_dict_attr_t * fr_dict_attr_unknown_copy(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Definition dict.h:559
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:586
void fr_dict_attr_verify(char const *file, int line, fr_dict_attr_t const *da)
Definition dict_util.c:4827
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:4919
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2403
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:236
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:3393
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:3331
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:158
char const * name
Enum name.
Definition dict.h:233
static fr_slen_t in
Definition dict.h:840
Value of an enumerated attribute.
Definition dict.h:232
Test enumeration values.
Definition dict_test.h:92
unsigned int offset
Positive offset from start of structure to fr_dlist_t.
Definition dlist.h:55
fr_dlist_t * next
Definition dlist.h:43
fr_dlist_t entry
Struct holding the head and tail of the list.
Definition dlist.h:52
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:555
Head of a doubly linked list.
Definition dlist.h:51
talloc_free(reap)
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
Definition lst.c:122
fr_type_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_COMBO_IP_PREFIX
IPv4 or IPv6 address prefix depending on length.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
unsigned char uint8_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
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:3393
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:1412
int fr_pair_list_copy_by_da(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_dict_attr_t const *da, unsigned int count)
Duplicate pairs in a list matching the specified da.
Definition pair.c:2416
fr_pair_t * fr_pair_list_parent(fr_pair_list_t const *list)
Return a pointer to the parent pair which contains this list.
Definition pair.c:960
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:189
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:2975
static void * fr_pair_iter_next_by_da(fr_dlist_head_t *list, void *current, void *uctx)
Iterate over pairs with a specified da.
Definition pair.c:630
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:674
void fr_pair_list_tainted(fr_pair_list_t *list)
Mark up a list of VPs as tainted.
Definition pair.c:3364
#define NOT_IN_THIS_LIST_MSG
Definition pair.c:539
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:1472
static void fprintf_pair_list(FILE *fp, fr_pair_list_t const *list, int depth)
Definition pair.c:3511
int fr_pair_value_enum_box(fr_value_box_t const **out, fr_pair_t *vp)
Get value box of a VP, optionally prefer enum value.
Definition pair.c:3075
int fr_pair_value_aprintf(fr_pair_t *vp, char const *fmt,...)
Print data into an "string" data type.
Definition pair.c:2709
int fr_pair_delete_by_da_nested(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list, and prune any empty branches.
Definition pair.c:1719
void fr_fprintf_pair_list(FILE *fp, fr_pair_list_t const *list)
Definition pair.c:3529
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:573
static void * _fr_pair_iter_next_value(fr_dlist_head_t *list, void *current, UNUSED void *uctx)
Iterate over pairs.
Definition pair.c:1206
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:247
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition pair.c:2945
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:774
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition pair.c:2645
static int _pair_list_dcursor_insert(fr_dlist_head_t *list, void *to_insert, UNUSED void *uctx)
Keep attr tree and sublists synced on cursor insert.
Definition pair.c:977
int fr_pair_value_bstrdup_buffer_shallow(fr_pair_t *vp, char const *src, bool tainted)
Assign a string to a "string" type value pair.
Definition pair.c:2866
void fr_pair_init_null(fr_pair_t *vp)
Initialise fields in an fr_pair_t without assigning a da.
Definition pair.c:149
int8_t fr_pair_cmp_by_parent_num(void const *a, void const *b)
Order attributes by their parent(s), attribute number, and tag.
Definition pair.c: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:2599
fr_pair_list_t * fr_pair_parent_list(fr_pair_t const *vp)
Return a pointer to the parent pair list.
Definition pair.c:931
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:697
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:897
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:2846
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c: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:946
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:1168
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:1445
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:745
static void * fr_pair_iter_next_by_ancestor(fr_dlist_head_t *list, void *current, void *uctx)
Iterate over pairs which are decedents of the specified da.
Definition pair.c:653
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:287
int fr_pair_value_bstrdup_buffer(fr_pair_t *vp, char const *src, bool tainted)
Copy a nul terminated talloced buffer a "string" type value pair.
Definition pair.c:2821
int fr_pair_update_by_da_parent(fr_pair_t *parent, fr_pair_t **out, fr_dict_attr_t const *da)
Return the first fr_pair_t matching the fr_dict_attr_t or alloc a new fr_pair_t and its subtree (and ...
Definition pair.c:1602
int fr_pair_list_copy_by_ancestor(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_dict_attr_t const *parent_da)
Duplicate pairs in a list where the da is a descendant of parent_da.
Definition pair.c:2465
static int _fr_pair_free(fr_pair_t *vp)
Free a fr_pair_t.
Definition pair.c:69
static void * _fr_pair_iter_next_dcursor_value(UNUSED fr_dlist_head_t *list, void *current, void *uctx)
Iterate over pairs.
Definition pair.c:1270
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:871
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:550
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:3417
int fr_pair_value_mem_realloc(fr_pair_t *vp, uint8_t **out, size_t size)
Change the length of a buffer for a "octets" type value pair.
Definition pair.c:2919
char const * fr_pair_value_enum(fr_pair_t const *vp, char buff[20])
Return a const buffer for an enum type attribute.
Definition pair.c:3042
int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Copy data into a "string" type value pair.
Definition pair.c:2795
void fr_fprintf_pair(FILE *fp, char const *msg, fr_pair_t const *vp)
Definition pair.c:3537
int fr_pair_value_bstr_alloc(fr_pair_t *vp, char **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "string" type value pair.
Definition pair.c:2741
fr_pair_t * fr_pair_alloc_null(TALLOC_CTX *ctx)
Dynamically allocate a new attribute with no fr_dict_attr_t assigned.
Definition pair.c:169
int fr_pair_value_bstr_realloc(fr_pair_t *vp, char **out, size_t size)
Change the length of a buffer for a "string" type value pair.
Definition pair.c:2766
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:2543
int fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list and free.
Definition pair.c: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:1529
int fr_pair_sublist_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_pair_t const *start, unsigned int count)
Duplicate a list of pairs starting at a particular item.
Definition pair.c:2518
static fr_dlist_head_t value_dlist
Definition pair.c:1232
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:2573
fr_pair_t * _fr_pair_dcursor_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, bool is_const)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.c:1131
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:318
int fr_pair_steal(TALLOC_CTX *ctx, fr_pair_t *vp)
Steal one VP.
Definition pair.c:525
int fr_pair_value_memdup_shallow(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Assign a buffer to a "octets" type value pair.
Definition pair.c:3000
bool fr_pair_validate_relaxed(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check.
Definition pair.c:2211
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:493
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:538
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:1149
int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "octets" type value pair.
Definition pair.c:2894
int fr_pair_value_strtrim(fr_pair_t *vp)
Trim the length of the string buffer to match the length of the C string.
Definition pair.c:2688
int fr_pair_insert_after(fr_pair_list_t *list, fr_pair_t *pos, fr_pair_t *to_add)
Add a VP after another VP.
Definition pair.c:1379
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:471
fr_pair_list_t * fr_pair_children(fr_pair_t *vp)
Get the child list of a group.
Definition pair.c:921
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:1254
static int _pair_list_dcursor_remove(NDEBUG_UNUSED fr_dlist_head_t *list, void *to_remove, UNUSED void *uctx)
Keep attr tree and sublists synced on cursor removal.
Definition pair.c:1002
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:3489
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:1042
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
fr_pair_t * fr_pair_afrom_da_depth_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da, int start)
Create a pair (and all intermediate parents), and append it to the list.
Definition pair.c:412
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:597
int8_t fr_pair_cmp_by_da(void const *a, void const *b)
Order attributes by their da, and tag.
Definition pair.c:1850
static const char spaces[]
Definition pair.c:3509
int fr_pair_value_memdup_buffer_shallow(fr_pair_t *vp, uint8_t const *src, bool tainted)
Assign a talloced buffer to a "octets" type value pair.
Definition pair.c:3020
int fr_pair_value_strdup_shallow(fr_pair_t *vp, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a vp, but don't copy it.
Definition pair.c:2669
int fr_pair_prepend_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and prepend)
Definition pair.c:1499
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:721
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:1109
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:375
fr_slen_t fr_pair_list_afrom_substr(fr_pair_parse_t const *root, fr_pair_parse_t *relative, fr_sbuff_t *in)
Parse a fr_pair_list_t from a substring.
struct fr_pair_parse_s fr_pair_parse_t
TALLOC_CTX * ctx
Definition pair_legacy.h:43
ssize_t fr_fprintf(FILE *fp, char const *fmt,...)
Special version of fprintf which implements custom format specifiers.
Definition print.c:897
void fr_proto_da_stack_build(fr_da_stack_t *stack, fr_dict_attr_t const *da)
Build a complete DA stack from the da back to the root.
Definition proto.c:118
#define fr_assert(_expr)
Definition rad_assert.h:38
static rc_request_t * current
#define FR_SBUFF_IN(_start, _len_or_end)
#define FR_SBUFF_OUT(_start, _len_or_end)
Set of parsing rules for *unescape_until functions.
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
return count
Definition module.c:155
fr_pair_t * vp
bool _CONST is_child
is a child of a VP
Definition pair.h:55
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
#define talloc_get_type_abort_const
Definition talloc.h:287
#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
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:79
@ 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:148
#define fr_pair_cmp_op(_op, _a, _b)
Compare two attributes using and operator.
Definition pair.h:666
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:191
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:137
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:261
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:192
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:53
#define PAIR_LIST_VERIFY(_x)
Definition pair.h:194
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:845
uint8_t depth
Deepest attribute in the stack.
Definition proto.h:55
fr_dict_attr_t const * da[FR_DICT_MAX_TLV_STACK+1]
The stack.
Definition proto.h:56
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:54
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:372
#define fr_type_is_structural(_x)
Definition types.h:388
#define FR_TYPE_STRUCTURAL
Definition types.h:312
#define fr_type_is_leaf(_x)
Definition types.h:389
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:450
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:4718
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:4214
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:4184
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:4523
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:4678
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:4439
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:4556
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:722
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:3962
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:3790
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:4700
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:5459
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:3899
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:4158
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:4267
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:4302
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:3945
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:4335
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:4379
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:4484
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:4463
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:4620
#define fr_box_is_numeric(_x)
Definition value.h:455
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:640
static fr_slen_t data
Definition value.h:1288
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1020
#define FR_VALUE_BOX_SAFE_FOR_NONE
Definition value.h:170
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:606
static size_t char ** out
Definition value.h:1020