2 * valuepair.c Functions to handle VALUE_PAIRs
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
25 #include <freeradius-devel/libradius.h>
29 #ifdef HAVE_PCREPOSIX_H
31 # include <pcreposix.h>
32 #elif defined(HAVE_REGEX_H)
37 * For POSIX Regular expressions.
38 * (0) Means no extended regular expressions.
39 * REG_EXTENDED means use extended regular expressions.
42 # define REG_EXTENDED (0)
46 # define REG_NOSUB (0)
50 #define attribute_eq(_x, _y) ((_x && _y) && (_x->da == _y->da) && (_x->tag == _y->tag))
54 * @note Do not call directly, use talloc_free instead.
59 static int _pairfree(VALUE_PAIR *vp) {
61 * The lack of DA means something has gone wrong
64 fr_strerror_printf("VALUE_PAIR has NULL DICT_ATTR pointer (probably already freed)");
66 * Only free the DICT_ATTR if it was dynamically allocated
67 * and was marked for free when the VALUE_PAIR is freed.
69 * @fixme This is an awful hack and needs to be removed once DICT_ATTRs are allocated by talloc.
71 } else if (vp->da->flags.vp_free) {
72 dict_attr_free(&(vp->da));
76 vp->vp_integer = FREE_MAGIC;
80 talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL);
85 /** Dynamically allocate a new attribute
87 * Allocates a new attribute and a new dictionary attr if no DA is provided.
89 * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
90 * @param[in] da Specifies the dictionary attribute to build the VP from.
91 * @return a new value pair or NULL if an error occurred.
93 VALUE_PAIR *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da)
98 * Caller must specify a da else we don't know what the attribute type is.
101 fr_strerror_printf("Invalid arguments");
105 vp = talloc_zero(ctx, VALUE_PAIR);
107 fr_strerror_printf("Out of memory");
115 vp->length = da->flags.length;
117 talloc_set_destructor(vp, _pairfree);
122 /** Create a new valuepair
124 * If attr and vendor match a dictionary entry then a VP with that DICT_ATTR
127 * If attr or vendor are uknown will call dict_attruknown to create a dynamic
128 * DICT_ATTR of PW_TYPE_OCTETS.
130 * Which type of DICT_ATTR the VALUE_PAIR was created with can be determined by
131 * checking @verbatim vp->da->flags.is_unknown @endverbatim.
133 * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
134 * @param[in] attr number.
135 * @param[in] vendor number.
136 * @return the new valuepair or NULL on error.
138 VALUE_PAIR *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
142 da = dict_attrbyvalue(attr, vendor);
144 da = dict_attrunknown(attr, vendor, true);
150 return pairalloc(ctx, da);
153 /** Free memory used by a valuepair list.
155 * @todo TLV: needs to free all dependents of each VP freed.
157 void pairfree(VALUE_PAIR **vps)
166 for (vp = fr_cursor_init(&cursor, vps);
168 vp = fr_cursor_next(&cursor)) {
176 /** Mark malformed or unrecognised attributed as unknown
178 * @param vp to change DICT_ATTR of.
179 * @return 0 on success (or if already unknown) else -1 on error.
181 int pair2unknown(VALUE_PAIR *vp)
186 if (vp->da->flags.is_unknown) {
190 da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
200 /** Find the pair with the matching attribute
202 * @todo should take DAs and do a pointer comparison.
204 VALUE_PAIR *pairfind(VALUE_PAIR *vp, unsigned int attr, unsigned int vendor,
210 for (i = fr_cursor_init(&cursor, &vp);
212 i = fr_cursor_next(&cursor)) {
214 if ((i->da->attr == attr) && (i->da->vendor == vendor)
215 && ((tag == TAG_ANY) || (i->da->flags.has_tag &&
224 /** Find the pair with the matching DAs
227 VALUE_PAIR *pairfind_da(VALUE_PAIR *vp, DICT_ATTR const *da, int8_t tag)
236 for (i = fr_cursor_init(&cursor, &vp);
238 i = fr_cursor_next(&cursor)) {
240 if ((i->da == da) && (!i->da->flags.has_tag || (tag == TAG_ANY) || (i->tag == tag))) {
248 /** Delete matching pairs
250 * Delete matching pairs from the attribute list.
252 * @param[in,out] first VP in list.
253 * @param[in] attr to match.
254 * @param[in] vendor to match.
255 * @param[in] tag to match. TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
257 * @todo should take DAs and do a point comparison.
259 void pairdelete(VALUE_PAIR **first, unsigned int attr, unsigned int vendor,
262 VALUE_PAIR *i, *next;
263 VALUE_PAIR **last = first;
265 for(i = *first; i; i = next) {
268 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
270 (i->da->flags.has_tag && (i->tag == tag)))) {
279 /** Add a VP to the end of the list.
281 * Locates the end of 'first', and links an additional VP 'add' at the end.
283 * @param[in] first VP in linked list. Will add new VP to the end of this list.
284 * @param[in] add VP to add to list.
286 void pairadd(VALUE_PAIR **first, VALUE_PAIR *add)
294 if (*first == NULL) {
298 for(i = *first; i->next; i = i->next)
303 /** Replace all matching VPs
305 * Walks over 'first', and replaces the first VP that matches 'replace'.
307 * @note Memory used by the VP being replaced will be freed.
308 * @note Will not work with unknown attributes.
310 * @param[in,out] first VP in linked list. Will search and replace in this list.
311 * @param[in] replace VP to replace.
313 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *replace)
315 VALUE_PAIR *i, *next;
316 VALUE_PAIR **prev = first;
320 if (*first == NULL) {
326 * Not an empty list, so find item if it is there, and
327 * replace it. Note, we always replace the first one, and
328 * we ignore any others that might exist.
330 for(i = *first; i; i = next) {
335 * Found the first attribute, replace it,
338 if ((i->da == replace->da) &&
339 (!i->da->flags.has_tag || (i->tag == replace->tag))
344 * Should really assert that replace->next == NULL
346 replace->next = next;
352 * Point to where the attribute should go.
358 * If we got here, we didn't find anything to replace, so
359 * stopped at the last item, which we just append to.
364 static void pairsort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
370 * Stopping condition - no more elements left to split
372 if (!source || !source->next) {
380 * Fast advances twice as fast as slow, so when it gets to the end,
381 * slow will point to the middle of the linked list.
399 static VALUE_PAIR *pairsort_merge(VALUE_PAIR *a, VALUE_PAIR *b, bool with_tag)
401 VALUE_PAIR *result = NULL;
407 * Compare the DICT_ATTRs and tags
409 if ((with_tag && (a->tag < b->tag)) || (a->da <= b->da)) {
411 result->next = pairsort_merge(a->next, b, with_tag);
414 result->next = pairsort_merge(a, b->next, with_tag);
420 /** Sort a linked list of VALUE_PAIRs using merge sort
422 * @param[in,out] vps List of VALUE_PAIRs to sort.
423 * @param[in] with_tag sort by tag then by DICT_ATTR
425 void pairsort(VALUE_PAIR **vps, bool with_tag)
427 VALUE_PAIR *head = *vps;
432 * If there's 0-1 elements it must already be sorted.
434 if (!head || !head->next) {
438 pairsort_split(head, &a, &b); /* Split into sublists */
439 pairsort(&a, with_tag); /* Traverse left */
440 pairsort(&b, with_tag); /* Traverse right */
443 * merge the two sorted lists together
445 *vps = pairsort_merge(a, b, with_tag);
448 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
450 * @param filter attributes to check list against.
451 * @param list attributes, probably a request or reply
453 bool pairvalidate(VALUE_PAIR *filter, VALUE_PAIR *list)
455 vp_cursor_t filter_cursor;
456 vp_cursor_t list_cursor;
458 VALUE_PAIR *check, *match;
460 if (!filter && !list) {
463 if (!filter || !list) {
468 * This allows us to verify the sets of validate and reply are equal
469 * i.e. we have a validate rule which matches every reply attribute.
471 * @todo this should be removed one we have sets and lists
473 pairsort(&filter, true);
474 pairsort(&list, true);
476 match = fr_cursor_init(&list_cursor, &list);
477 check = fr_cursor_init(&filter_cursor, &filter);
481 * The lists are sorted, so if the first
482 * attributes aren't of the same type, then we're
485 if (!attribute_eq(check, match)) {
490 * They're of the same type, but don't have the
491 * same values. This is a problem.
493 * Note that the RFCs say that for attributes of
494 * the same type, order is important.
496 if (!paircmp(check, match)) {
500 match = fr_cursor_next(&list_cursor);
501 check = fr_cursor_next(&filter_cursor);
503 if (!match && !check) break;
506 * One list ended earlier than the others, they
509 if (!match || !check) {
517 /** Copy a single valuepair
519 * Allocate a new valuepair and copy the da from the old vp.
521 * @param[in] ctx for talloc
522 * @param[in] vp to copy.
523 * @return a copy of the input VP or NULL on error.
525 VALUE_PAIR *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
529 if (!vp) return NULL;
533 n = pairalloc(ctx, vp->da);
536 memcpy(n, vp, sizeof(*n));
541 if (vp->type == VT_XLAT) {
542 n->value.xlat = talloc_strdup(n, n->value.xlat);
545 n->da = dict_attr_copy(vp->da, true);
553 if ((n->da->type == PW_TYPE_TLV) ||
554 (n->da->type == PW_TYPE_OCTETS)) {
555 if (n->vp_octets != NULL) {
556 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
559 } else if (n->da->type == PW_TYPE_STRING) {
560 if (n->vp_strvalue != NULL) {
562 * Equivalent to, and faster than strdup.
564 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1);
571 /** Copy data from one VP to another
573 * Allocate a new pair using da, and copy over the value from the specified
576 * @todo Should be able to do type conversions.
578 * @param[in] ctx for talloc
579 * @param[in] da of new attribute to alloc.
580 * @param[in] vp to copy data from.
581 * @return the new valuepair.
583 VALUE_PAIR *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp)
587 if (!vp) return NULL;
592 * The types have to be identical, OR the "from" VP has
595 if (da->type != vp->da->type) {
598 VALUE_PAIR const **pvp;
600 if (vp->da->type == PW_TYPE_OCTETS) {
602 * Decode the data. It may be wrong!
604 if (rad_data2vp(da->attr, da->vendor, vp->vp_octets, vp->length, &n) < 0) {
613 * Else the destination type is octets
615 switch (vp->da->type) {
617 return NULL; /* can't do it */
619 case PW_TYPE_INTEGER:
623 case PW_TYPE_IPV6ADDR:
624 case PW_TYPE_IPV6PREFIX:
627 case PW_TYPE_ETHERNET:
629 case PW_TYPE_INTEGER64:
630 case PW_TYPE_IPV4PREFIX:
634 n = pairalloc(ctx, da);
637 p = talloc_array(n, uint8_t, dict_attr_sizes[vp->da->type][1] + 2);
640 length = rad_vp2attr(NULL, NULL, NULL, pvp, p, dict_attr_sizes[vp->da->type][1]);
646 pairmemcpy(n, p + 2, length - 2);
651 n = pairalloc(ctx, da);
654 memcpy(n, vp, sizeof(*n));
657 if (n->type == VT_XLAT) {
658 n->value.xlat = talloc_strdup(n, n->value.xlat);
661 switch (n->da->type) {
664 if (n->vp_octets != NULL) {
665 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
670 if (n->vp_strvalue != NULL) {
671 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1); /* NULL byte */
684 /** Copy matching pairs
686 * Copy pairs of a matching attribute number, vendor number and tag from the
687 * the input list to a new list, and returns the head of this list.
689 * @param[in] ctx for talloc
690 * @param[in] from whence to copy VALUE_PAIRs.
691 * @param[in] attr to match, if 0 input list will not be filtered by attr.
692 * @param[in] vendor to match.
693 * @param[in] tag to match, TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
694 * @return the head of the new VALUE_PAIR list or NULL on error.
696 VALUE_PAIR *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from,
697 unsigned int attr, unsigned int vendor, int8_t tag)
699 vp_cursor_t src, dst;
701 VALUE_PAIR *out = NULL, *vp;
703 fr_cursor_init(&dst, &out);
704 for (vp = fr_cursor_init(&src, &from);
706 vp = fr_cursor_next(&src)) {
709 if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
713 if ((tag != TAG_ANY) && vp->da->flags.has_tag && (vp->tag != tag)) {
717 vp = paircopyvp(ctx, vp);
722 fr_cursor_insert(&dst, vp);
731 * Copy all pairs from 'from' regardless of tag, attribute or vendor.
733 * @param[in] ctx for new VALUE_PAIRs to be allocated in.
734 * @param[in] from whence to copy VALUE_PAIRs.
735 * @return the head of the new VALUE_PAIR list or NULL on error.
737 VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from)
739 vp_cursor_t src, dst;
741 VALUE_PAIR *out = NULL, *vp;
743 fr_cursor_init(&dst, &out);
744 for (vp = fr_cursor_init(&src, &from);
746 vp = fr_cursor_next(&src)) {
748 vp = paircopyvp(ctx, vp);
753 fr_cursor_insert(&dst, vp); /* paircopy sets next pointer to NULL */
759 /** Move pairs from source list to destination list respecting operator
761 * @note This function does some additional magic that's probably not needed
762 * in most places. Consider using radius_pairmove in server code.
764 * @note pairfree should be called on the head of the source list to free
765 * unmoved attributes (if they're no longer needed).
767 * @note Does not respect tags when matching.
769 * @param[in] ctx for talloc
770 * @param[in,out] to destination list.
771 * @param[in,out] from source list.
773 * @see radius_pairmove
775 void pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from)
777 VALUE_PAIR *i, *found;
778 VALUE_PAIR *head_new, **tail_new;
779 VALUE_PAIR **tail_from;
781 if (!to || !from || !*from) return;
784 * We're editing the "to" list while we're adding new
785 * attributes to it. We don't want the new attributes to
786 * be edited, so we create an intermediate list to hold
787 * them during the editing process.
790 tail_new = &head_new;
793 * We're looping over the "from" list, moving some
794 * attributes out, but leaving others in place.
797 while ((i = *tail_from) != NULL) {
801 * We never move Fall-Through.
803 if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
804 tail_from = &(i->next);
809 * Unlike previous versions, we treat all other
810 * attributes as normal. i.e. there's no special
811 * treatment for passwords or Hint.
816 * Anything else are operators which
817 * shouldn't occur. We ignore them, and
818 * leave them in place.
821 tail_from = &(i->next);
825 * Add it to the "to" list, but only if
826 * it doesn't already exist.
829 found = pairfind(*to, i->da->attr, i->da->vendor,
831 if (!found) goto do_add;
833 tail_from = &(i->next);
837 * Add it to the "to" list, and delete any attribute
838 * of the same vendor/attr which already exists.
841 found = pairfind(*to, i->da->attr, i->da->vendor,
843 if (!found) goto do_add;
846 * Do NOT call pairdelete() here,
847 * due to issues with re-writing
848 * "request->username".
850 * Everybody calls pairmove, and
851 * expects it to work. We can't
852 * update request->username here,
853 * so instead we over-write the
854 * vp that it's pointing to.
856 switch (found->da->type) {
861 memcpy(found, i, sizeof(*found));
866 pairmemsteal(found, i->vp_tlv);
871 pairmemsteal(found, i->vp_octets);
876 pairstrsteal(found, i->vp_strvalue);
877 i->vp_strvalue = NULL;
883 * Delete *all* of the attributes
884 * of the same number.
886 pairdelete(&found->next,
888 found->da->vendor, TAG_ANY);
891 * Remove this attribute from the
894 *tail_from = i->next;
900 * Move it from the old list and add it
905 *tail_from = i->next;
907 *tail_new = talloc_steal(ctx, i);
908 tail_new = &(i->next);
911 } /* loop over the "from" list. */
914 * Take the "new" list, and append it to the "to" list.
916 pairadd(to, head_new);
919 /** Move matching pairs between VALUE_PAIR lists
921 * Move pairs of a matching attribute number, vendor number and tag from the
922 * the input list to the output list.
924 * @note pairfree should be called on the head of the old list to free unmoved
925 attributes (if they're no longer needed).
927 * @param[in] ctx for talloc
928 * @param[in,out] to destination list.
929 * @param[in,out] from source list.
930 * @param[in] attr to match, if PW_VENDOR_SPECIFIC and vendor 0, only VSAs will
931 * be copied. If 0 and 0, all attributes will match
932 * @param[in] vendor to match.
933 * @param[in] tag to match, TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
935 void pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from, unsigned int attr, unsigned int vendor, int8_t tag)
937 VALUE_PAIR *to_tail, *i, *next;
938 VALUE_PAIR *iprev = NULL;
941 * Find the last pair in the "to" list and put it in "to_tail".
943 * @todo: replace the "if" with "VALUE_PAIR **tail"
947 for(i = *to; i; i = i->next) {
955 * Attr/vendor of 0 means "move them all".
956 * It's better than "pairadd(foo,bar);bar=NULL"
958 if ((vendor == 0) && (attr == 0)) {
960 to_tail->next = *from;
965 for (i = *from; i; i = i->next) {
966 (void) talloc_steal(ctx, i);
973 for(i = *from; i; i = next) {
977 if ((tag != TAG_ANY) && i->da->flags.has_tag &&
983 * vendor=0, attr = PW_VENDOR_SPECIFIC means
984 * "match any vendor attribute".
986 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
988 * It's a VSA: move it over.
990 if (i->da->vendor != 0) goto move;
993 * It's Vendor-Specific: move it over.
995 if (i->da->attr == attr) goto move;
998 * It's not a VSA: ignore it.
1005 * If it isn't an exact match, ignore it.
1007 if (!((i->da->vendor == vendor) && (i->da->attr == attr))) {
1014 * Remove the attribute from the "from" list.
1022 * Add the attribute to the "to" list.
1030 (void) talloc_steal(ctx, i);
1034 static char const *hextab = "0123456789abcdef";
1036 bool pairparsevalue(VALUE_PAIR *vp, char const *value)
1039 char const *cp, *cs;
1045 if (!value) return false;
1049 * It's a comparison, not a real VALUE_PAIR, copy the string over verbatim
1051 if ((vp->op == T_OP_REG_EQ) || (vp->op == T_OP_REG_NE)) {
1052 pairstrcpy(vp, value); /* Icky hacky ewww */
1056 switch(vp->da->type) {
1057 case PW_TYPE_STRING:
1061 p = talloc_strdup(vp, value);
1062 vp->vp_strvalue = p;
1100 c = '\\'; /* no cp++ */
1103 if ((cp[0] >= '0') &&
1109 (sscanf(cp, "%3o", &x) == 1)) {
1112 } /* else just do '\\' */
1119 vp->length = length;
1122 case PW_TYPE_IPADDR:
1124 * FIXME: complain if hostname
1125 * cannot be resolved, or resolve later!
1133 * Convert things which are obviously integers to IP addresses
1135 * We assume the number is the bigendian representation of the
1138 if (fr_integer_check(value)) {
1139 vp->vp_ipaddr = htonl(atol(value));
1144 * Certain applications/databases print IPv4 addresses with a
1145 * /32 suffix. Strip it off if the mask is 32, else error out.
1147 p = strchr(value, '/');
1149 if ((p[1] != '3') || (p[2] != '2') || (p[3] != '\0')) {
1150 fr_strerror_printf("Invalid IP address suffix \"%s\". Only '/32' permitted "
1151 "for non-prefix types", p);
1155 strlcpy(ipv4, value, sizeof(ipv4));
1156 ipv4[p - value] = '\0';
1162 if (ip_hton(cs, AF_INET, &ipaddr) < 0) {
1163 fr_strerror_printf("Failed to find IP address for %s", cs);
1167 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1176 * Note that ALL integers are unsigned!
1178 vp->vp_integer = fr_strtoul(value, &p);
1180 if (vp->vp_integer > 255) {
1181 fr_strerror_printf("Byte value \"%s\" is larger than 255", value);
1186 if (fr_whitespace_check(p)) break;
1187 goto check_for_value;
1191 * Note that ALL integers are unsigned!
1193 vp->vp_integer = fr_strtoul(value, &p);
1196 if (vp->vp_integer > 65535) {
1197 fr_strerror_printf("Byte value \"%s\" is larger than 65535", value);
1202 if (fr_whitespace_check(p)) break;
1203 goto check_for_value;
1205 case PW_TYPE_INTEGER:
1207 * Note that ALL integers are unsigned!
1209 vp->vp_integer = fr_strtoul(value, &p);
1212 if (fr_whitespace_check(p)) break;
1216 * Look for the named value for the given
1219 if ((dval = dict_valbyname(vp->da->attr, vp->da->vendor, value)) == NULL) {
1220 fr_strerror_printf("Unknown value '%s' for attribute '%s'", value, vp->da->name);
1223 vp->vp_integer = dval->value;
1226 case PW_TYPE_INTEGER64:
1228 * Note that ALL integers are unsigned!
1230 if (sscanf(value, "%" PRIu64, &y) != 1) {
1231 fr_strerror_printf("Invalid value '%s' for attribute '%s'",
1232 value, vp->da->name);
1235 vp->vp_integer64 = y;
1237 length = strspn(value, "0123456789");
1238 if (fr_whitespace_check(value + length)) break;
1244 * time_t may be 64 bits, whule vp_date
1245 * MUST be 32-bits. We need an
1246 * intermediary variable to handle
1251 if (fr_get_time(value, &date) < 0) {
1252 fr_strerror_printf("failed to parse time string "
1262 case PW_TYPE_ABINARY:
1263 #ifdef WITH_ASCEND_BINARY
1264 if (strncasecmp(value, "0x", 2) == 0) {
1268 if (ascend_parse_filter(vp, value) < 0 ) {
1269 /* Allow ascend_parse_filter's strerror to bubble up */
1275 * If Ascend binary is NOT defined,
1276 * then fall through to raw octets, so that
1277 * the user can at least make them by hand...
1280 /* raw octets: 0x01020304... */
1282 if (strcmp(value, "ANY") == 0) {
1285 } /* else it's hex */
1287 case PW_TYPE_OCTETS:
1288 if (strncasecmp(value, "0x", 2) == 0) {
1292 #ifdef WITH_ASCEND_BINARY
1297 vp->length = size >> 1;
1298 us = talloc_array(vp, uint8_t, vp->length);
1303 if ((size & 0x01) != 0) {
1304 fr_strerror_printf("Hex string is not an even length string");
1308 if (fr_hex2bin(us, cp, vp->length) != vp->length) {
1309 fr_strerror_printf("Invalid hex data");
1314 pairstrcpy(vp, value);
1319 if (ifid_aton(value, (void *) &vp->vp_ifid) == NULL) {
1320 fr_strerror_printf("Failed to parse interface-id string \"%s\"", value);
1326 case PW_TYPE_IPV6ADDR:
1330 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
1333 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1335 fr_strerror_printf("failed to parse IPv6 address "
1336 "string \"%s\": %s", value, buffer);
1339 vp->vp_ipv6addr = ipaddr.ipaddr.ip6addr;
1340 vp->length = 16; /* length of IPv6 address */
1344 case PW_TYPE_IPV6PREFIX:
1345 p = strchr(value, '/');
1346 if (!p || ((p - value) >= 256)) {
1347 fr_strerror_printf("invalid IPv6 prefix string \"%s\"", value);
1350 unsigned int prefix;
1351 char buffer[256], *eptr;
1353 memcpy(buffer, value, p - value);
1354 buffer[p - value] = '\0';
1356 if (inet_pton(AF_INET6, buffer, vp->vp_ipv6prefix + 2) <= 0) {
1357 fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1361 prefix = strtoul(p + 1, &eptr, 10);
1362 if ((prefix > 128) || *eptr) {
1363 fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1366 vp->vp_ipv6prefix[1] = prefix;
1369 struct in6_addr addr;
1371 addr = fr_ipaddr_mask6((struct in6_addr *)(&vp->vp_ipv6prefix[2]), prefix);
1372 memcpy(vp->vp_ipv6prefix + 2, &addr, sizeof(addr));
1375 vp->length = 16 + 2;
1378 case PW_TYPE_IPV4PREFIX:
1379 p = strchr(value, '/');
1382 * 192.0.2.2 is parsed as if it was /32
1385 vp->vp_ipv4prefix[1] = 32;
1387 if (inet_pton(AF_INET, value, vp->vp_ipv4prefix + 2) <= 0) {
1388 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1391 vp->length = sizeof(vp->vp_ipv4prefix);
1396 * Otherwise parse the prefix
1398 if ((p - value) >= 256) {
1399 fr_strerror_printf("invalid IPv4 prefix string \"%s\"", value);
1402 unsigned int prefix;
1403 char buffer[256], *eptr;
1405 memcpy(buffer, value, p - value);
1406 buffer[p - value] = '\0';
1408 if (inet_pton(AF_INET, buffer, vp->vp_ipv4prefix + 2) <= 0) {
1409 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1413 prefix = strtoul(p + 1, &eptr, 10);
1414 if ((prefix > 32) || *eptr) {
1415 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1418 vp->vp_ipv4prefix[1] = prefix;
1421 struct in_addr addr;
1423 addr = fr_ipaddr_mask((struct in_addr *)(&vp->vp_ipv4prefix[2]), prefix);
1424 memcpy(vp->vp_ipv4prefix + 2, &addr, sizeof(addr));
1427 vp->length = sizeof(vp->vp_ipv4prefix);
1430 case PW_TYPE_ETHERNET:
1432 char const *c1, *c2;
1435 * Convert things which are obviously integers to Ethernet addresses
1437 * We assume the number is the bigendian representation of the
1440 if (fr_integer_check(value)) {
1441 uint64_t integer = htonll(atoll(value));
1443 memcpy(&vp->vp_ether, &integer, sizeof(vp->vp_ether));
1452 c2 = memchr(hextab, tolower((int) cp[0]), 16);
1454 } else if ((cp[1] != '\0') &&
1457 c1 = memchr(hextab, tolower((int) cp[0]), 16);
1458 c2 = memchr(hextab, tolower((int) cp[1]), 16);
1460 if (*cp == ':') cp++;
1464 if (!c1 || !c2 || (length >= sizeof(vp->vp_ether))) {
1465 fr_strerror_printf("failed to parse Ethernet address \"%s\"", value);
1468 vp->vp_ether[length] = ((c1-hextab)<<4) + (c2-hextab);
1476 * Crazy polymorphic (IPv4/IPv6) attribute type for WiMAX.
1478 * We try and make is saner by replacing the original
1479 * da, with either an IPv4 or IPv6 da type.
1481 * These are not dynamic da, and will have the same vendor
1482 * and attribute as the original.
1484 case PW_TYPE_COMBO_IP:
1486 DICT_ATTR const *da;
1488 if (inet_pton(AF_INET6, value, &vp->vp_ipv6addr) > 0) {
1489 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1492 fr_strerror_printf("Cannot find ipv6addr for %s", vp->da->name);
1496 vp->length = 16; /* length of IPv6 address */
1500 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1503 fr_strerror_printf("Cannot find ipaddr for %s", vp->da->name);
1507 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
1508 fr_strerror_printf("Failed to find IPv4 address for %s", value);
1512 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1520 case PW_TYPE_SIGNED: /* Damned code for 1 WiMAX attribute */
1521 vp->vp_signed = (int32_t) strtol(value, &p, 10);
1525 case PW_TYPE_TLV: /* don't use this! */
1526 if (strncasecmp(value, "0x", 2) != 0) {
1527 fr_strerror_printf("Invalid TLV specification");
1530 length = strlen(value + 2) / 2;
1531 if (vp->length < length) {
1532 TALLOC_FREE(vp->vp_tlv);
1534 vp->vp_tlv = talloc_array(vp, uint8_t, length);
1536 fr_strerror_printf("No memory");
1539 if (fr_hex2bin(vp->vp_tlv, value + 2, length) != length) {
1540 fr_strerror_printf("Invalid hex data in TLV");
1543 vp->length = length;
1550 fr_strerror_printf("unknown attribute type %d", vp->da->type);
1559 /** Use simple heuristics to create an VALUE_PAIR from an unknown address string
1561 * If a DICT_ATTR is not provided for the address type, parsing will fail with
1564 * @param ctx to allocate VP in.
1565 * @param value IPv4/IPv6 address/prefix string.
1566 * @param ipv4 dictionary attribute to use for an IPv4 address.
1567 * @param ipv6 dictionary attribute to use for an IPv6 address.
1568 * @param ipv4_prefix dictionary attribute to use for an IPv4 prefix.
1569 * @param ipv6_prefix dictionary attribute to use for an IPv6 prefix.
1570 * @return NULL on error, or new VALUE_PAIR.
1572 VALUE_PAIR *pairmake_ip(TALLOC_CTX *ctx, char const *value, DICT_ATTR *ipv4, DICT_ATTR *ipv6,
1573 DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix)
1578 if (!fr_assert(ipv4 || ipv6 || ipv4_prefix || ipv6_prefix)) {
1582 /* No point in repeating the work of pairparsevalue */
1583 if (strchr(value, ':')) {
1584 if (strchr(value, '/')) {
1593 if (strchr(value, '/')) {
1600 fr_strerror_printf("Invalid IP value specified, allowed types are %s%s%s%s",
1601 ipv4 ? "ipaddr " : "", ipv6 ? "ipv6addr " : "",
1602 ipv4_prefix ? "ipv4prefix " : "", ipv6_prefix ? "ipv6prefix" : "");
1605 vp = pairalloc(ctx, da);
1606 if (!vp) return NULL;
1607 if (!pairparsevalue(vp, value)) {
1615 /** Create a valuepair from an ASCII attribute and value
1617 * Where the attribute name is in the form:
1619 * - Attr-%d.%d.%d...
1620 * - Vendor-%d-Attr-%d
1621 * - VendorName-Attr-%d
1623 * @param ctx for talloc
1624 * @param attribute name to parse.
1625 * @param value to parse (must be a hex string).
1626 * @param op to assign to new valuepair.
1627 * @return new valuepair or NULL on error.
1629 static VALUE_PAIR *pairmake_any(TALLOC_CTX *ctx,
1630 char const *attribute, char const *value,
1634 DICT_ATTR const *da;
1639 da = dict_attrunknownbyname(attribute, true);
1640 if (!da) return NULL;
1643 * Unknown attributes MUST be of type 'octets'
1645 if (value && (strncasecmp(value, "0x", 2) != 0)) {
1646 fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
1647 "string, not \"%s\"", attribute, value);
1649 dict_attr_free(&da);
1654 * We've now parsed the attribute properly, Let's create
1655 * it. This next stop also looks the attribute up in the
1656 * dictionary, and creates the appropriate type for it.
1658 vp = pairalloc(ctx, da);
1660 dict_attr_free(&da);
1664 vp->op = (op == 0) ? T_OP_EQ : op;
1666 if (!value) return vp;
1668 size = strlen(value + 2);
1669 vp->length = size >> 1;
1670 data = talloc_array(vp, uint8_t, vp->length);
1672 if (fr_hex2bin(data, value + 2, size) != vp->length) {
1673 fr_strerror_printf("Invalid hex string");
1678 vp->vp_octets = data;
1684 /** Create a VALUE_PAIR from ASCII strings
1686 * Converts an attribute string identifier (with an optional tag qualifier)
1687 * and value string into a VALUE_PAIR.
1689 * The string value is parsed according to the type of VALUE_PAIR being created.
1691 * @param[in] ctx for talloc
1692 * @param[in] vps list where the attribute will be added (optional)
1693 * @param[in] attribute name.
1694 * @param[in] value attribute value (may be NULL if value will be set later).
1695 * @param[in] op to assign to new VALUE_PAIR.
1696 * @return a new VALUE_PAIR.
1698 VALUE_PAIR *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps,
1699 char const *attribute, char const *value, FR_TOKEN op)
1701 DICT_ATTR const *da;
1707 char const *attrname = attribute;
1710 * Check for tags in 'Attribute:Tag' format.
1715 ts = strrchr(attribute, ':');
1717 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1722 strlcpy(buffer, attribute, sizeof(buffer));
1724 ts = strrchr(attrname, ':');
1725 if (!ts) return NULL;
1727 /* Colon found with something behind it */
1728 if (ts[1] == '*' && ts[2] == 0) {
1729 /* Wildcard tag for check items */
1732 } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1733 /* It's not a wild card tag */
1734 tag = strtol(ts + 1, &tc, 0);
1735 if (tc && !*tc && TAG_VALID_ZERO(tag))
1739 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1746 * It's not found in the dictionary, so we use
1747 * another method to create the attribute.
1749 da = dict_attrbyname(attrname);
1751 vp = pairmake_any(ctx, attrname, value, op);
1752 if (vp && vps) pairadd(vps, vp);
1756 /* Check for a tag in the 'Merit' format of:
1757 * :Tag:Value. Print an error if we already found
1758 * a tag in the Attribute.
1761 if (value && (*value == ':' && da->flags.has_tag)) {
1762 /* If we already found a tag, this is invalid */
1764 fr_strerror_printf("Duplicate tag %s for attribute %s",
1766 DEBUG("Duplicate tag %s for attribute %s\n",
1770 /* Colon found and attribute allows a tag */
1771 if (value[1] == '*' && value[2] == ':') {
1772 /* Wildcard tag for check items */
1777 tag = strtol(value + 1, &tc, 0);
1778 if (tc && *tc==':' && TAG_VALID_ZERO(tag))
1784 vp = pairalloc(ctx, da);
1785 if (!vp) return NULL;
1786 vp->op = (op == 0) ? T_OP_EQ : op;
1794 case T_OP_CMP_FALSE:
1795 vp->vp_strvalue = NULL;
1797 value = NULL; /* ignore it! */
1801 * Regular expression comparison of integer attributes
1802 * does a STRING comparison of the names of their
1803 * integer attributes.
1805 case T_OP_REG_EQ: /* =~ */
1806 case T_OP_REG_NE: /* !~ */
1808 fr_strerror_printf("Regular expressions are not supported");
1814 * Someone else will fill in the value.
1824 compare = regcomp(®, value, REG_EXTENDED);
1826 regerror(compare, ®, buffer, sizeof(buffer));
1827 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
1833 vp = pairmake(ctx, NULL, attribute, NULL, op);
1834 if (!vp) return NULL;
1836 if (pairmark_xlat(vp, value) < 0) {
1841 value = NULL; /* ignore it */
1847 * FIXME: if (strcasecmp(attribute, vp->da->name) != 0)
1848 * then the user MAY have typed in the attribute name
1849 * as Vendor-%d-Attr-%d, and the value MAY be octets.
1851 * We probably want to fix pairparsevalue to accept
1852 * octets as values for any attribute.
1854 if (value && !pairparsevalue(vp, value)) {
1859 if (vps) pairadd(vps, vp);
1863 /** Mark a valuepair for xlat expansion
1865 * Copies xlat source (unprocessed) string to valuepair value,
1866 * and sets value type.
1868 * @param vp to mark for expansion.
1869 * @param value to expand.
1870 * @return 0 if marking succeeded or -1 if vp already had a value, or OOM.
1872 int pairmark_xlat(VALUE_PAIR *vp, char const *value)
1877 * valuepair should not already have a value.
1879 if (vp->type != VT_NONE) {
1883 raw = talloc_strdup(vp, value);
1889 vp->value.xlat = raw;
1895 /** Read a single valuepair from a buffer, and advance the pointer
1897 * Sets *eol to T_EOL if end of line was encountered.
1899 * @param[in,out] ptr to read from and update.
1900 * @param[out] raw The struct to write the raw VALUE_PAIR to.
1901 * @return the last token read.
1903 FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw)
1907 FR_TOKEN ret = T_OP_INVALID, next, quote;
1910 if (!ptr || !*ptr || !raw) {
1911 fr_strerror_printf("Invalid arguments");
1912 return T_OP_INVALID;
1916 * Skip leading spaces
1919 while ((*p == ' ') || (*p == '\t')) p++;
1922 fr_strerror_printf("No token read where we expected "
1923 "an attribute name");
1924 return T_OP_INVALID;
1928 fr_strerror_printf("Read a comment instead of a token");
1934 * Try to get the attribute name.
1939 uint8_t const *t = (uint8_t const *) p;
1941 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1943 fr_strerror_printf("Attribute name too long");
1944 return T_OP_INVALID;
1948 * Only ASCII is allowed, and only a subset of that.
1950 if ((*t < 32) || (*t >= 128)) {
1952 fr_strerror_printf("Invalid attribute name");
1953 return T_OP_INVALID;
1957 * This is arguably easier than trying to figure
1958 * out which operators come after the attribute
1959 * name. Yes, our "lexer" is bad.
1961 if (!dict_attr_allowed_chars[(int) *t]) {
1969 * ASCII, but not a valid attribute name.
1971 if (!*raw->l_opand) goto invalid;
1974 * Look for tag (:#). This is different from :=, which
1977 if ((*p == ':') && (isdigit((int) p[1]))) {
1978 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1983 while (isdigit((int) *p)) {
1984 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1994 /* Now we should have an operator here. */
1995 raw->op = gettoken(ptr, buf, sizeof(buf));
1996 if (raw->op < T_EQSTART || raw->op > T_EQEND) {
1997 fr_strerror_printf("Expecting operator");
1999 return T_OP_INVALID;
2003 * Read value. Note that empty string values are allowed
2005 quote = gettoken(ptr, raw->r_opand, sizeof(raw->r_opand));
2006 if (quote == T_EOL) {
2007 fr_strerror_printf("Failed to get value");
2009 return T_OP_INVALID;
2013 * Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
2017 next = gettoken(&p, buf, sizeof(buf));
2028 fr_strerror_printf("Expected end of line or comma");
2029 return T_OP_INVALID;
2037 case T_DOUBLE_QUOTED_STRING:
2039 * Only report as double quoted if it contained valid
2040 * a valid xlat expansion.
2042 p = strchr(raw->r_opand, '%');
2043 if (p && (p[1] == '{')) {
2046 raw->quote = T_SINGLE_QUOTED_STRING;
2059 /** Read one line of attribute/value pairs into a list.
2061 * The line may specify multiple attributes separated by commas.
2063 * @note If the function returns T_OP_INVALID, an error has occurred and
2064 * @note the valuepair list should probably be freed.
2066 * @param ctx for talloc
2067 * @param buffer to read valuepairs from.
2068 * @param list where the parsed VALUE_PAIRs will be appended.
2069 * @return the last token parsed, or T_OP_INVALID
2071 FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **list)
2073 VALUE_PAIR *vp, *head, **tail;
2075 FR_TOKEN last_token = T_OP_INVALID;
2076 FR_TOKEN previous_token;
2080 * We allow an empty line.
2082 if (buffer[0] == 0) {
2091 raw.l_opand[0] = '\0';
2092 raw.r_opand[0] = '\0';
2094 previous_token = last_token;
2096 last_token = pairread(&p, &raw);
2097 if (last_token == T_OP_INVALID) break;
2099 if (raw.quote == T_DOUBLE_QUOTED_STRING) {
2100 vp = pairmake(ctx, NULL, raw.l_opand, NULL, raw.op);
2102 last_token = T_OP_INVALID;
2105 if (pairmark_xlat(vp, raw.r_opand) < 0) {
2107 last_token = T_OP_INVALID;
2111 vp = pairmake(ctx, NULL, raw.l_opand, raw.r_opand, raw.op);
2113 last_token = T_OP_INVALID;
2119 tail = &((*tail)->next);
2120 } while (*p && (last_token == T_COMMA));
2123 * Don't tell the caller that there was a comment.
2125 if (last_token == T_HASH) {
2126 last_token = previous_token;
2129 if (last_token == T_OP_INVALID) {
2132 pairadd(list, head);
2136 * And return the last token which we read.
2142 * Read valuepairs from the fp up to End-Of-File.
2144 * Hmm... this function is only used by radclient..
2146 VALUE_PAIR *readvp2(TALLOC_CTX *ctx, FILE *fp, bool *pfiledone, char const *errprefix)
2149 FR_TOKEN last_token = T_EOL;
2156 while (!error && fgets(buf, sizeof(buf), fp) != NULL) {
2158 * If we get a '\n' by itself, we assume that's
2159 * the end of that VP
2161 if ((buf[0] == '\n') && (list)) {
2164 if ((buf[0] == '\n') && (!list)) {
2169 * Comments get ignored
2171 if (buf[0] == '#') continue;
2174 * Read all of the attributes on the current line.
2177 last_token = userparse(ctx, buf, &vp);
2179 if (last_token != T_EOL) {
2180 fr_perror("%s", errprefix);
2191 if (error) pairfree(&list);
2199 * We leverage the fact that IPv4 and IPv6 prefixes both
2200 * have the same format:
2202 * reserved, prefix-len, data...
2204 static int paircmp_cidr(FR_TOKEN op, int bytes, uint8_t const *one, uint8_t const *two)
2210 * Handle the case of netmasks being identical.
2212 if (one[1] == two[1]) {
2215 compare = memcmp(one + 2, two + 2, bytes);
2218 * If they're identical return true for
2221 if ((compare == 0) &&
2222 ((op == T_OP_CMP_EQ) ||
2229 * Everything else returns false.
2231 * 10/8 == 24/8 --> false
2232 * 10/8 <= 24/8 --> false
2233 * 10/8 >= 24/8 --> false
2239 * Netmasks are different. That limits the
2240 * possible results, based on the operator.
2250 case T_OP_LT: /* 192/8 < 192.168/16 --> false */
2251 if (one[1] < two[1]) {
2257 case T_OP_GT: /* 192/16 > 192.168/8 --> false */
2258 if (one[1] > two[1]) {
2267 if (one[1] < two[1]) {
2274 * Do the check byte by byte. If the bytes are
2275 * identical, it MAY be a match. If they're different,
2276 * it is NOT a match.
2279 while (i < (2 + bytes)) {
2281 * All leading bytes are identical.
2283 if (common == 0) return true;
2286 * Doing bitmasks takes more work.
2288 if (common < 8) break;
2290 if (one[i] != two[i]) return false;
2298 mask <<= (8 - common);
2302 if ((one[i] & mask) == ((two[i] & mask))) {
2310 * Compare two pairs, using the operator from "one".
2312 * i.e. given two attributes, it does:
2314 * (two->data) (one->operator) (one->data)
2316 * e.g. "foo" != "bar"
2318 * Returns true (comparison is true), or false (comparison is not true);
2320 int paircmp(VALUE_PAIR *one, VALUE_PAIR *two)
2329 return (two != NULL);
2331 case T_OP_CMP_FALSE:
2332 return (two == NULL);
2335 * One is a regex, compile it, print two to a string,
2336 * and then do string comparisons.
2345 char buffer[MAX_STRING_LEN * 4 + 1];
2347 compare = regcomp(®, one->vp_strvalue, REG_EXTENDED);
2349 regerror(compare, ®, buffer, sizeof(buffer));
2350 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
2351 one->da->name, buffer);
2355 vp_prints_value(buffer, sizeof(buffer), two, 0);
2358 * Don't care about substring matches,
2361 compare = regexec(®, buffer, 0, NULL, 0);
2364 if (one->op == T_OP_REG_EQ) return (compare == 0);
2365 return (compare != 0);
2369 default: /* we're OK */
2373 return paircmp_op(two, one->op, one);
2376 /* Compare two attributes
2378 * @param[in] one the first attribute
2379 * @param[in] op the operator for comparison
2380 * @param[in] two the second attribute
2381 * @return true (comparison is true), or false (comparison is not true);
2383 int paircmp_op(VALUE_PAIR const *one, FR_TOKEN op, VALUE_PAIR const *two)
2391 * Can't compare two attributes of differing types
2393 * FIXME: maybe do checks for IP OP IP/mask ??
2395 if (one->da->type != two->da->type) {
2396 return one->da->type - two->da->type;
2400 * After doing the previous check for special comparisons,
2401 * do the per-type comparison here.
2403 switch (one->da->type) {
2404 case PW_TYPE_ABINARY:
2405 case PW_TYPE_OCTETS:
2409 if (one->length > two->length) {
2410 length = one->length;
2412 length = two->length;
2416 compare = memcmp(one->vp_octets, two->vp_octets,
2418 if (compare != 0) break;
2422 * Contents are the same. The return code
2423 * is therefore the difference in lengths.
2425 * i.e. "0x00" is smaller than "0x0000"
2427 compare = one->length - two->length;
2431 case PW_TYPE_STRING:
2432 compare = strcmp(one->vp_strvalue, two->vp_strvalue);
2437 case PW_TYPE_INTEGER:
2439 if (one->vp_integer < two->vp_integer) {
2441 } else if (one->vp_integer == two->vp_integer) {
2448 case PW_TYPE_SIGNED:
2449 if (one->vp_signed < two->vp_signed) {
2451 } else if (one->vp_signed == two->vp_signed) {
2458 case PW_TYPE_INTEGER64:
2460 * Don't want integer overflow!
2462 if (one->vp_integer64 < two->vp_integer64) {
2464 } else if (one->vp_integer64 > two->vp_integer64) {
2471 case PW_TYPE_ETHERNET:
2472 compare = memcmp(&one->vp_ether, &two->vp_ether,
2473 sizeof(one->vp_ether));
2476 case PW_TYPE_IPADDR:
2477 if (ntohl(one->vp_ipaddr) < ntohl(two->vp_ipaddr)) {
2479 } else if (one->vp_ipaddr == two->vp_ipaddr) {
2486 case PW_TYPE_IPV6ADDR:
2487 compare = memcmp(&one->vp_ipv6addr, &two->vp_ipv6addr,
2488 sizeof(one->vp_ipv6addr));
2491 case PW_TYPE_IPV6PREFIX:
2492 return paircmp_cidr(op, 16,
2493 (uint8_t const *) &one->vp_ipv6prefix,
2494 (uint8_t const *) &two->vp_ipv6prefix);
2496 case PW_TYPE_IPV4PREFIX:
2497 return paircmp_cidr(op, 4,
2498 (uint8_t const *) &one->vp_ipv4prefix,
2499 (uint8_t const *) &two->vp_ipv4prefix);
2502 compare = memcmp(&one->vp_ifid, &two->vp_ifid, sizeof(one->vp_ifid));
2506 * None of the types below should be in the REQUEST
2508 case PW_TYPE_COMBO_IP: /* This should of been converted into IPADDR/IPV6ADDR */
2510 case PW_TYPE_EXTENDED:
2511 case PW_TYPE_LONG_EXTENDED:
2514 case PW_TYPE_INVALID: /* We should never see these */
2516 fr_assert(0); /* unknown type */
2520 * Do NOT add a default here, as new types are added
2521 * static analysis will warn us they're not handled
2526 * Now do the operator comparison.
2530 return (compare == 0);
2533 return (compare != 0);
2536 return (compare < 0);
2539 return (compare > 0);
2542 return (compare <= 0);
2545 return (compare >= 0);
2552 /** Copy data into an "octets" data type.
2554 * @param[in,out] vp to update
2555 * @param[in] src data to copy
2556 * @param[in] size of the data
2558 void pairmemcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
2564 p = talloc_memdup(vp, src, size);
2567 memcpy(&q, &vp->vp_octets, sizeof(q));
2574 /** Reparent an allocated octet buffer to a VALUE_PAIR
2576 * @param[in,out] vp to update
2577 * @param[in] src buffer to steal.
2579 void pairmemsteal(VALUE_PAIR *vp, uint8_t const *src)
2584 memcpy(&q, &vp->vp_octets, sizeof(q));
2587 vp->vp_octets = talloc_steal(vp, src);
2589 vp->length = talloc_array_length(vp->vp_octets);
2592 /** Reparent an allocated char buffer to a VALUE_PAIR
2594 * @param[in,out] vp to update
2595 * @param[in] src buffer to steal.
2597 void pairstrsteal(VALUE_PAIR *vp, char const *src)
2602 memcpy(&q, &vp->vp_octets, sizeof(q));
2605 vp->vp_strvalue = talloc_steal(vp, src);
2607 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2610 /** Copy data into an "string" data type.
2612 * @param[in,out] vp to update
2613 * @param[in] src data to copy
2615 void pairstrcpy(VALUE_PAIR *vp, char const *src)
2621 p = talloc_strdup(vp, src);
2624 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2627 vp->vp_strvalue = p;
2629 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2633 /** Print data into an "string" data type.
2635 * @param[in,out] vp to update
2636 * @param[in] fmt the format string
2638 void pairsprintf(VALUE_PAIR *vp, char const *fmt, ...)
2646 p = talloc_vasprintf(vp, fmt, ap);
2651 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2654 vp->vp_strvalue = p;
2657 vp->length = talloc_array_length(vp->vp_strvalue) - 1;