2 * value.c Functions to handle value_data_t
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 2014 The FreeRADIUS server project
25 #include <freeradius-devel/libradius.h>
28 /** Compare two values
30 * @param[in] a_type of data to compare.
31 * @param[in] a_len of data to compare.
32 * @param[in] a Value to compare.
33 * @param[in] b_type of data to compare.
34 * @param[in] b_len of data to compare.
35 * @param[in] b Value to compare.
36 * @return -1 if a is less than b, 0 if both are equal, 1 if a is more than b, < -1 on error.
38 int value_data_cmp(PW_TYPE a_type, value_data_t const *a, size_t a_len,
39 PW_TYPE b_type, value_data_t const *b, size_t b_len)
43 if (a_type != b_type) {
44 fr_strerror_printf("Can't compare values of different types");
49 * After doing the previous check for special comparisons,
50 * do the per-type comparison here.
55 case PW_TYPE_STRING: /* We use memcmp to be \0 safe */
66 compare = memcmp(a->octets, b->octets, length);
67 if (compare != 0) break;
71 * Contents are the same. The return code
72 * is therefore the difference in lengths.
74 * i.e. "0x00" is smaller than "0x0000"
76 compare = a_len - b_len;
81 * Short-hand for simplicity.
83 #define CHECK(_type) if (a->_type < b->_type) { compare = -1; \
84 } else if (a->_type > b->_type) { compare = +1; }
86 case PW_TYPE_BOOLEAN: /* this isn't a RADIUS type, and shouldn't really ever be used */
100 case PW_TYPE_INTEGER:
108 case PW_TYPE_INTEGER64:
112 case PW_TYPE_ETHERNET:
113 compare = memcmp(a->ether, b->ether, sizeof(a->ether));
116 case PW_TYPE_IPV4_ADDR: {
117 uint32_t a_int, b_int;
119 a_int = ntohl(a->ipaddr.s_addr);
120 b_int = ntohl(b->ipaddr.s_addr);
123 } else if (a_int > b_int) {
129 case PW_TYPE_IPV6_ADDR:
130 compare = memcmp(&a->ipv6addr, &b->ipv6addr, sizeof(a->ipv6addr));
133 case PW_TYPE_IPV6_PREFIX:
134 compare = memcmp(a->ipv6prefix, b->ipv6prefix, sizeof(a->ipv6prefix));
137 case PW_TYPE_IPV4_PREFIX:
138 compare = memcmp(a->ipv4prefix, b->ipv4prefix, sizeof(a->ipv4prefix));
142 compare = memcmp(a->ifid, b->ifid, sizeof(a->ifid));
146 * Na of the types below should be in the REQUEST
148 case PW_TYPE_INVALID: /* We should never see these */
149 case PW_TYPE_COMBO_IP_ADDR: /* This should have been converted into IPADDR/IPV6ADDR */
150 case PW_TYPE_COMBO_IP_PREFIX: /* This should have been converted into IPADDR/IPV6ADDR */
152 case PW_TYPE_EXTENDED:
153 case PW_TYPE_LONG_EXTENDED:
156 case PW_TYPE_TIMEVAL:
158 fr_assert(0); /* unknown type */
162 * Do NOT add a default here, as new types are added
163 * static analysis will warn us they're not handled
169 } else if (compare < 0) {
176 * We leverage the fact that IPv4 and IPv6 prefixes both
177 * have the same format:
179 * reserved, prefix-len, data...
181 static int value_data_cidr_cmp_op(FR_TOKEN op, int bytes,
182 uint8_t a_net, uint8_t const *a,
183 uint8_t b_net, uint8_t const *b)
189 * Handle the case of netmasks being identical.
191 if (a_net == b_net) {
194 compare = memcmp(a, b, bytes);
197 * If they're identical return true for
200 if ((compare == 0) &&
201 ((op == T_OP_CMP_EQ) ||
208 * Everything else returns false.
210 * 10/8 == 24/8 --> false
211 * 10/8 <= 24/8 --> false
212 * 10/8 >= 24/8 --> false
218 * Netmasks are different. That limits the
219 * possible results, based on the operator.
229 case T_OP_LT: /* 192/8 < 192.168/16 --> false */
236 case T_OP_GT: /* 192/16 > 192.168/8 --> false */
253 * Do the check byte by byte. If the bytes are
254 * identical, it MAY be a match. If they're different,
260 * All leading bytes are identical.
262 if (common == 0) return true;
265 * Doing bitmasks takes more work.
267 if (common < 8) break;
269 if (a[i] != b[i]) return false;
277 mask <<= (8 - common);
281 if ((a[i] & mask) == ((b[i] & mask))) {
288 /** Compare two attributes using an operator
290 * @param[in] op to use in comparison.
291 * @param[in] a_type of data to compare.
292 * @param[in] a_len of data to compare.
293 * @param[in] a Value to compare.
294 * @param[in] b_type of data to compare.
295 * @param[in] b_len of data to compare.
296 * @param[in] b Value to compare.
297 * @return 1 if true, 0 if false, -1 on error.
299 int value_data_cmp_op(FR_TOKEN op,
300 PW_TYPE a_type, value_data_t const *a, size_t a_len,
301 PW_TYPE b_type, value_data_t const *b, size_t b_len)
305 if (!a || !b) return -1;
308 case PW_TYPE_IPV4_ADDR:
310 case PW_TYPE_IPV4_ADDR: /* IPv4 and IPv4 */
313 case PW_TYPE_IPV4_PREFIX: /* IPv4 and IPv4 Prefix */
314 return value_data_cidr_cmp_op(op, 4, 32, (uint8_t const *) &a->ipaddr,
315 b->ipv4prefix[1], (uint8_t const *) &b->ipv4prefix[2]);
318 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
322 case PW_TYPE_IPV4_PREFIX: /* IPv4 and IPv4 Prefix */
324 case PW_TYPE_IPV4_ADDR:
325 return value_data_cidr_cmp_op(op, 4, a->ipv4prefix[1],
326 (uint8_t const *) &a->ipv4prefix[2],
327 32, (uint8_t const *) &b->ipaddr);
329 case PW_TYPE_IPV4_PREFIX: /* IPv4 Prefix and IPv4 Prefix */
330 return value_data_cidr_cmp_op(op, 4, a->ipv4prefix[1],
331 (uint8_t const *) &a->ipv4prefix[2],
332 b->ipv4prefix[1], (uint8_t const *) &b->ipv4prefix[2]);
335 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
339 case PW_TYPE_IPV6_ADDR:
341 case PW_TYPE_IPV6_ADDR: /* IPv6 and IPv6 */
344 case PW_TYPE_IPV6_PREFIX: /* IPv6 and IPv6 Preifx */
345 return value_data_cidr_cmp_op(op, 16, 128, (uint8_t const *) &a->ipv6addr,
346 b->ipv6prefix[1], (uint8_t const *) &b->ipv6prefix[2]);
349 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
353 case PW_TYPE_IPV6_PREFIX:
355 case PW_TYPE_IPV6_ADDR: /* IPv6 Prefix and IPv6 */
356 return value_data_cidr_cmp_op(op, 16, a->ipv6prefix[1],
357 (uint8_t const *) &a->ipv6prefix[2],
358 128, (uint8_t const *) &b->ipv6addr);
360 case PW_TYPE_IPV6_PREFIX: /* IPv6 Prefix and IPv6 */
361 return value_data_cidr_cmp_op(op, 16, a->ipv6prefix[1],
362 (uint8_t const *) &a->ipv6prefix[2],
363 b->ipv6prefix[1], (uint8_t const *) &b->ipv6prefix[2]);
366 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
372 compare = value_data_cmp(a_type, a, a_len,
374 if (compare < -1) { /* comparison error */
380 * Now do the operator comparison.
384 return (compare == 0);
387 return (compare != 0);
390 return (compare < 0);
393 return (compare > 0);
396 return (compare <= 0);
399 return (compare >= 0);
406 static char const hextab[] = "0123456789abcdef";
408 /** Convert string value to a value_data_t type
410 * @param[in] ctx to alloc strings in.
411 * @param[out] dst where to write parsed value.
412 * @param[in,out] src_type of value data to create/type of value created.
413 * @param[in] src_enumv DICT_ATTR with string aliases for integer values.
414 * @param[in] src String to convert. Binary safe for variable length values if len is provided.
415 * @param[in] src_len may be < 0 in which case strlen(len) is used to determine length, else src_len
416 * should be the length of the string or sub string to parse.
417 * @param[in] quote quotation character used to drive de-escaping
418 * @return length of data written to out or -1 on parse error.
420 ssize_t value_data_from_str(TALLOC_CTX *ctx, value_data_t *dst,
421 PW_TYPE *src_type, DICT_ATTR const *src_enumv,
422 char const *src, ssize_t src_len, char quote)
431 len = (src_len < 0) ? strlen(src) : (size_t)src_len;
434 * Set size for all fixed length attributes.
436 ret = dict_attr_sizes[*src_type][1]; /* Max length */
439 * It's a variable ret src_type so we just alloc a new buffer
440 * of size len and copy.
449 dst->strvalue = p = talloc_array(ctx, char, len + 1);
454 * No de-quoting. Just copy the string.
462 * Do escaping for single quoted strings. Only
463 * single quotes get escaped. Everything else is
469 while (q < (dst->strvalue + len)) {
471 * The quotation character is escaped.
473 if ((q[0] == '\\') &&
481 * Two backslashes get mangled to one.
483 if ((q[0] == '\\') &&
491 * Not escaped, just copy it over.
497 ret = p - dst->strvalue;
498 dst->ptr = talloc_realloc(ctx, dst->ptr, char, ret + 1);
503 * It's "string" or `string`, do all standard
507 while (q < (dst->strvalue + len)) {
510 if ((c == '\\') && (q >= (dst->strvalue + len))) {
511 fr_strerror_printf("Invalid escape at end of string");
516 * Fix up \X -> ... the binary form of it.
542 * \" --> ", but only inside of double quoted strings, etc.
551 * \000 --> binary zero character
559 (sscanf(q, "%3o", &x) == 1)) {
565 * Else It's not a recognised escape sequence DON'T
566 * consume the backslash. This is identical
567 * behaviour to bash and most other things that
568 * use backslash escaping.
577 ret = p - dst->strvalue;
578 dst->ptr = talloc_realloc(ctx, dst->ptr, char, ret + 1);
583 fr_strerror_printf("Must use 'Attr-26 = ...' instead of 'Vendor-Specific = ...'");
586 /* raw octets: 0x01020304... */
592 * No 0x prefix, just copy verbatim.
594 if ((len < 2) || (strncasecmp(src, "0x", 2) != 0)) {
595 dst->octets = talloc_memdup(ctx, (uint8_t const *)src, len);
596 talloc_set_type(dst->octets, uint8_t);
607 if ((len & 0x01) != 0) {
608 fr_strerror_printf("Length of Hex String is not even, got %zu bytes", len);
613 p = talloc_array(ctx, uint8_t, ret);
614 if (fr_hex2bin(p, ret, src + 2, len) != (size_t)ret) {
616 fr_strerror_printf("Invalid hex data");
624 case PW_TYPE_ABINARY:
625 #ifdef WITH_ASCEND_BINARY
626 if ((len > 1) && (strncasecmp(src, "0x", 2) == 0)) goto do_octets;
628 if (ascend_parse_filter(dst, src, len) < 0 ) {
629 /* Allow ascend_parse_filter's strerror to bubble up */
632 ret = sizeof(dst->filter);
636 * If Ascend binary is NOT defined,
637 * then fall through to raw octets, so that
638 * the user can at least make them by hand...
643 /* don't use this! */
645 fr_strerror_printf("Cannot parse TLV");
648 case PW_TYPE_IPV4_ADDR:
652 if (fr_pton4(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
655 * We allow v4 addresses to have a /32 suffix as some databases (PostgreSQL)
656 * print them this way.
658 if (addr.prefix != 32) {
659 fr_strerror_printf("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted "
660 "for non-prefix types", addr.prefix);
664 dst->ipaddr.s_addr = addr.ipaddr.ip4addr.s_addr;
668 case PW_TYPE_IPV4_PREFIX:
672 if (fr_pton4(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
674 dst->ipv4prefix[1] = addr.prefix;
675 memcpy(&dst->ipv4prefix[2], &addr.ipaddr.ip4addr.s_addr, sizeof(dst->ipv4prefix) - 2);
679 case PW_TYPE_IPV6_ADDR:
683 if (fr_pton6(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
686 * We allow v6 addresses to have a /128 suffix as some databases (PostgreSQL)
687 * print them this way.
689 if (addr.prefix != 128) {
690 fr_strerror_printf("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted "
691 "for non-prefix types", addr.prefix);
695 memcpy(&dst->ipv6addr, addr.ipaddr.ip6addr.s6_addr, sizeof(dst->ipv6addr));
699 case PW_TYPE_IPV6_PREFIX:
703 if (fr_pton6(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
705 dst->ipv6prefix[1] = addr.prefix;
706 memcpy(&dst->ipv6prefix[2], addr.ipaddr.ip6addr.s6_addr, sizeof(dst->ipv6prefix) - 2);
715 * It's a fixed size src_type, copy to a temporary buffer and
716 * \0 terminate if insize >= 0.
719 if (len >= sizeof(buffer)) {
720 fr_strerror_printf("Temporary buffer too small");
724 memcpy(buffer, src, src_len);
725 buffer[src_len] = '\0';
736 * Note that ALL integers are unsigned!
738 i = fr_strtoul(src, &p);
741 * Look for the named src for the given
744 if (src_enumv && *p && !is_whitespace(p)) {
745 if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
746 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
747 src, src_enumv->name);
751 dst->byte = dval->value;
754 fr_strerror_printf("Byte value \"%s\" is larger than 255", src);
769 * Note that ALL integers are unsigned!
771 i = fr_strtoul(src, &p);
774 * Look for the named src for the given
777 if (src_enumv && *p && !is_whitespace(p)) {
778 if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
779 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
780 src, src_enumv->name);
784 dst->ushort = dval->value;
787 fr_strerror_printf("Short value \"%s\" is larger than 65535", src);
796 case PW_TYPE_INTEGER:
802 * Note that ALL integers are unsigned!
804 i = fr_strtoul(src, &p);
807 * Look for the named src for the given
810 if (src_enumv && *p && !is_whitespace(p)) {
811 if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
812 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
813 src, src_enumv->name);
817 dst->integer = dval->value;
820 * Value is always within the limits
827 case PW_TYPE_INTEGER64:
832 * Note that ALL integers are unsigned!
834 if (sscanf(src, "%" PRIu64, &i) != 1) {
835 fr_strerror_printf("Failed parsing \"%s\" as unsigned 64bit integer", src);
845 * time_t may be 64 bits, whule vp_date MUST be 32-bits. We need an
846 * intermediary variable to handle the conversions.
850 if (fr_get_time(src, &date) < 0) {
851 fr_strerror_printf("failed to parse time string \"%s\"", src);
861 if (ifid_aton(src, (void *) dst->ifid) == NULL) {
862 fr_strerror_printf("Failed to parse interface-id string \"%s\"", src);
867 case PW_TYPE_ETHERNET:
869 char const *c1, *c2, *cp;
873 * Convert things which are obviously integers to Ethernet addresses
875 * We assume the number is the bigendian representation of the
878 if (is_integer(src)) {
879 uint64_t integer = htonll(atoll(src));
881 memcpy(dst->ether, &integer, sizeof(dst->ether));
889 c2 = memchr(hextab, tolower((int) cp[0]), 16);
891 } else if ((cp[1] != '\0') && ((cp[2] == ':') || (cp[2] == '\0'))) {
892 c1 = memchr(hextab, tolower((int) cp[0]), 16);
893 c2 = memchr(hextab, tolower((int) cp[1]), 16);
895 if (*cp == ':') cp++;
899 if (!c1 || !c2 || (p_len >= sizeof(dst->ether))) {
900 fr_strerror_printf("failed to parse Ethernet address \"%s\"", src);
903 dst->ether[p_len] = ((c1-hextab)<<4) + (c2-hextab);
910 * Crazy polymorphic (IPv4/IPv6) attribute src_type for WiMAX.
912 * We try and make is saner by replacing the original
913 * da, with either an IPv4 or IPv6 da src_type.
915 * These are not dynamic da, and will have the same vendor
916 * and attribute as the original.
918 case PW_TYPE_COMBO_IP_ADDR:
920 if (inet_pton(AF_INET6, src, &dst->ipv6addr) > 0) {
921 *src_type = PW_TYPE_IPV6_ADDR;
922 ret = dict_attr_sizes[PW_TYPE_COMBO_IP_ADDR][1]; /* size of IPv6 address */
926 if (ip_hton(&ipaddr, AF_INET, src, false) < 0) {
927 fr_strerror_printf("Failed to find IPv4 address for %s", src);
931 *src_type = PW_TYPE_IPV4_ADDR;
932 dst->ipaddr.s_addr = ipaddr.ipaddr.ip4addr.s_addr;
933 ret = dict_attr_sizes[PW_TYPE_COMBO_IP_ADDR][0]; /* size of IPv4 address */
939 /* Damned code for 1 WiMAX attribute */
940 dst->sinteger = (int32_t)strtol(src, NULL, 10);
947 fr_strerror_printf("Unknown attribute type %d", *src_type);
955 /** Performs byte order reversal for types that need it
958 static void value_data_hton(value_data_t *dst, PW_TYPE type, void const *src, size_t src_len)
960 /* 8 byte integers */
962 case PW_TYPE_INTEGER64:
963 dst->integer64 = htonll(*(uint64_t const *)src);
966 /* 4 byte integers */
967 case PW_TYPE_INTEGER:
970 dst->integer = htonl(*(uint32_t const *)src);
973 /* 2 byte integers */
975 dst->ushort = htons(*(uint16_t const *)src);
981 return; /* shouldn't happen */
984 memcpy(dst, src, src_len);
988 /** Convert one type of value_data_t to another
990 * @note This should be the canonical function used to convert between data types.
992 * @param ctx to allocate buffers in (usually the same as dst)
993 * @param dst Where to write result of casting.
994 * @param dst_type to cast to.
995 * @param dst_enumv Enumerated values used to converts strings to integers.
996 * @param src_type to cast from.
997 * @param src_enumv Enumerated values used to convert integers to strings.
998 * @param src Input data.
999 * @param src_len Input data len.
1000 * @return the length of data in the dst or -1 on error.
1002 ssize_t value_data_cast(TALLOC_CTX *ctx, value_data_t *dst,
1003 PW_TYPE dst_type, DICT_ATTR const *dst_enumv,
1004 PW_TYPE src_type, DICT_ATTR const *src_enumv,
1005 value_data_t const *src, size_t src_len)
1007 if (!fr_assert(dst_type != src_type)) return -1;
1010 * Deserialise a value_data_t
1012 if (src_type == PW_TYPE_STRING) {
1013 return value_data_from_str(ctx, dst, &dst_type, dst_enumv, src->strvalue, src_len, '\0');
1017 * Converts the src data to octets with no processing.
1019 if (dst_type == PW_TYPE_OCTETS) {
1020 value_data_hton(dst, src_type, src, src_len);
1021 dst->octets = talloc_memdup(ctx, dst, src_len);
1022 talloc_set_type(dst->octets, uint8_t);
1023 return talloc_array_length(dst->strvalue);
1027 * Serialise a value_data_t
1029 if (dst_type == PW_TYPE_STRING) {
1030 dst->strvalue = value_data_aprints(ctx, src_type, src_enumv, src, src_len, '\0');
1031 return talloc_array_length(dst->strvalue) - 1;
1034 if ((src_type == PW_TYPE_IFID) &&
1035 (dst_type == PW_TYPE_INTEGER64)) {
1036 memcpy(&dst->integer64, src->ifid, sizeof(src->ifid));
1037 dst->integer64 = htonll(dst->integer64);
1039 return dict_attr_sizes[dst_type][0];
1042 if ((src_type == PW_TYPE_INTEGER64) &&
1043 (dst_type == PW_TYPE_ETHERNET)) {
1047 i = htonll(src->integer64);
1048 memcpy(array, &i, 8);
1051 * For OUIs in the DB.
1053 if ((array[0] != 0) || (array[1] != 0)) return -1;
1055 memcpy(dst->ether, &array[2], 6);
1060 * For integers, we allow the casting of a SMALL type to
1061 * a larger type, but not vice-versa.
1063 if (dst_type == PW_TYPE_INTEGER64) {
1066 dst->integer64 = src->byte;
1070 dst->integer64 = src->ushort;
1073 case PW_TYPE_INTEGER:
1074 dst->integer64 = src->integer;
1078 dst->integer64 = src->date;
1081 case PW_TYPE_OCTETS:
1086 fr_strerror_printf("Invalid cast from %s to %s",
1087 fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1088 fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1096 * We can cast LONG integers to SHORTER ones, so long
1097 * as the long one is on the LHS.
1099 if (dst_type == PW_TYPE_INTEGER) {
1102 dst->integer = src->byte;
1106 dst->integer = src->ushort;
1109 case PW_TYPE_OCTETS:
1118 if (dst_type == PW_TYPE_SHORT) {
1121 dst->ushort = src->byte;
1124 case PW_TYPE_OCTETS:
1134 * Conversions between IPv4 addresses, IPv6 addresses, IPv4 prefixes and IPv6 prefixes
1136 * For prefix to ipaddress conversions, we assume that the host portion has already
1139 * We allow casts from v6 to v4 if the v6 address has the correct mapping prefix.
1141 * We only allow casts from prefixes to addresses if the prefix is the the length of
1142 * the address, e.g. 32 for ipv4 128 for ipv6.
1146 * 10 bytes of 0x00 2 bytes of 0xff
1148 static uint8_t const v4_v6_map[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1149 0x00, 0x00, 0x00, 0x00, 0xff, 0xff };
1152 case PW_TYPE_IPV4_ADDR:
1154 case PW_TYPE_IPV6_ADDR:
1155 if (memcmp(src->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) {
1157 fr_strerror_printf("Invalid cast from %s to %s. No IPv4-IPv6 mapping prefix",
1158 fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1159 fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1163 memcpy(&dst->ipaddr, &src->ipv6addr.s6_addr[sizeof(v4_v6_map)],
1164 sizeof(dst->ipaddr));
1167 case PW_TYPE_IPV4_PREFIX:
1168 if (src->ipv4prefix[1] != 32) {
1170 fr_strerror_printf("Invalid cast from %s to %s. Only /32 prefixes may be "
1171 "cast to IP address types",
1172 fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1173 fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1177 memcpy(&dst->ipaddr, &src->ipv4prefix[2], sizeof(dst->ipaddr));
1180 case PW_TYPE_IPV6_PREFIX:
1181 if (src->ipv6prefix[1] != 128) {
1183 fr_strerror_printf("Invalid cast from %s to %s. Only /128 prefixes may be "
1184 "cast to IP address types",
1185 fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1186 fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1189 if (memcmp(&src->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map)) != 0) {
1190 goto bad_v6_prefix_map;
1192 memcpy(&dst->ipaddr, &src->ipv6prefix[2 + sizeof(v4_v6_map)],
1193 sizeof(dst->ipaddr));
1201 case PW_TYPE_IPV6_ADDR:
1203 case PW_TYPE_IPV4_ADDR:
1204 /* Add the v4/v6 mapping prefix */
1205 memcpy(dst->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map));
1206 memcpy(&dst->ipv6addr.s6_addr[sizeof(v4_v6_map)], &src->ipaddr,
1207 sizeof(dst->ipv6addr.s6_addr) - sizeof(v4_v6_map));
1211 case PW_TYPE_IPV4_PREFIX:
1212 if (src->ipv4prefix[1] != 32) goto bad_v4_prefix_len;
1214 /* Add the v4/v6 mapping prefix */
1215 memcpy(dst->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map));
1216 memcpy(&dst->ipv6addr.s6_addr[sizeof(v4_v6_map)], &src->ipv4prefix[2],
1217 sizeof(dst->ipv6addr.s6_addr) - sizeof(v4_v6_map));
1220 case PW_TYPE_IPV6_PREFIX:
1221 if (src->ipv4prefix[1] != 128) goto bad_v6_prefix_len;
1223 memcpy(dst->ipv6addr.s6_addr, &src->ipv6prefix[2], sizeof(dst->ipv6addr.s6_addr));
1231 case PW_TYPE_IPV4_PREFIX:
1233 case PW_TYPE_IPV4_ADDR:
1234 memcpy(&dst->ipv4prefix[2], &src->ipaddr, sizeof(dst->ipv4prefix) - 2);
1235 dst->ipv4prefix[0] = 0;
1236 dst->ipv4prefix[1] = 32;
1239 case PW_TYPE_IPV6_ADDR:
1240 if (memcmp(src->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) {
1241 goto bad_v6_prefix_map;
1243 memcpy(&dst->ipv4prefix[2], &src->ipv6addr.s6_addr[sizeof(v4_v6_map)],
1244 sizeof(dst->ipv4prefix) - 2);
1245 dst->ipv4prefix[0] = 0;
1246 dst->ipv4prefix[1] = 32;
1249 case PW_TYPE_IPV6_PREFIX:
1250 if (memcmp(&src->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map)) != 0) {
1251 goto bad_v6_prefix_map;
1255 * Prefix must be >= 96 bits. If it's < 96 bytes and the
1256 * above check passed, the v6 address wasn't masked
1257 * correctly when it was packet into a value_data_t.
1259 if (!fr_assert(src->ipv6prefix[1] >= (sizeof(v4_v6_map) * 8))) return -1;
1261 memcpy(&dst->ipv4prefix[2], &src->ipv6prefix[2 + sizeof(v4_v6_map)],
1262 sizeof(dst->ipv4prefix) - 2);
1263 dst->ipv4prefix[0] = 0;
1264 dst->ipv4prefix[1] = src->ipv6prefix[1] - (sizeof(v4_v6_map) * 8);
1272 case PW_TYPE_IPV6_PREFIX:
1274 case PW_TYPE_IPV4_ADDR:
1275 /* Add the v4/v6 mapping prefix */
1276 memcpy(&dst->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map));
1277 memcpy(&dst->ipv6prefix[2 + sizeof(v4_v6_map)], &src->ipaddr,
1278 (sizeof(dst->ipv6prefix) - 2) - sizeof(v4_v6_map));
1279 dst->ipv6prefix[0] = 0;
1280 dst->ipv6prefix[1] = 128;
1283 case PW_TYPE_IPV4_PREFIX:
1284 /* Add the v4/v6 mapping prefix */
1285 memcpy(&dst->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map));
1286 memcpy(&dst->ipv6prefix[2 + sizeof(v4_v6_map)], &src->ipv4prefix[2],
1287 (sizeof(dst->ipv6prefix) - 2) - sizeof(v4_v6_map));
1288 dst->ipv6prefix[0] = 0;
1289 dst->ipv6prefix[1] = (sizeof(v4_v6_map) * 8) + src->ipv4prefix[1];
1292 case PW_TYPE_IPV6_ADDR:
1293 memcpy(&dst->ipv6prefix[2], &src->ipv6addr, sizeof(dst->ipv6prefix) - 2);
1294 dst->ipv6prefix[0] = 0;
1295 dst->ipv6prefix[1] = 128;
1310 * The attribute we've found has to have a size which is
1311 * compatible with the type of the destination cast.
1313 if ((src_len < dict_attr_sizes[dst_type][0]) ||
1314 (src_len > dict_attr_sizes[dst_type][1])) {
1315 char const *src_type_name;
1317 src_type_name = fr_int2str(dict_attr_types, src_type, "<INVALID>");
1318 fr_strerror_printf("Invalid cast from %s to %s. Length should be between %zu and %zu but is %zu",
1320 fr_int2str(dict_attr_types, dst_type, "<INVALID>"),
1321 dict_attr_sizes[dst_type][0], dict_attr_sizes[dst_type][1],
1326 if (src_type == PW_TYPE_OCTETS) {
1328 value_data_hton(dst, dst_type, src->octets, src_len);
1333 * Convert host order to network byte order.
1335 if ((dst_type == PW_TYPE_IPV4_ADDR) &&
1336 ((src_type == PW_TYPE_INTEGER) ||
1337 (src_type == PW_TYPE_DATE) ||
1338 (src_type == PW_TYPE_SIGNED))) {
1339 dst->ipaddr.s_addr = htonl(src->integer);
1341 } else if ((src_type == PW_TYPE_IPV4_ADDR) &&
1342 ((dst_type == PW_TYPE_INTEGER) ||
1343 (dst_type == PW_TYPE_DATE) ||
1344 (dst_type == PW_TYPE_SIGNED))) {
1345 dst->integer = htonl(src->ipaddr.s_addr);
1347 } else { /* they're of the same byte order */
1348 memcpy(&dst, &src, src_len);
1354 /** Copy value data verbatim duplicating any buffers
1356 * @param ctx To allocate buffers in.
1357 * @param dst Where to copy value_data to.
1358 * @param src_type Type of src.
1359 * @param src Where to copy value_data from.
1360 * @param src_len Where
1362 ssize_t value_data_copy(TALLOC_CTX *ctx, value_data_t *dst, PW_TYPE src_type,
1363 const value_data_t *src, size_t src_len)
1367 memcpy(dst, src, sizeof(*src));
1370 case PW_TYPE_STRING:
1371 dst->strvalue = talloc_bstrndup(ctx, src->strvalue, src_len);
1372 if (!dst->strvalue) return -1;
1375 case PW_TYPE_OCTETS:
1376 dst->octets = talloc_memdup(ctx, src->octets, src_len);
1377 talloc_set_type(dst->strvalue, uint8_t);
1378 if (!dst->octets) return -1;
1387 /** Print one attribute value to a string
1390 char *value_data_aprints(TALLOC_CTX *ctx,
1391 PW_TYPE type, DICT_ATTR const *enumv, value_data_t const *data,
1392 size_t inlen, char quote)
1398 case PW_TYPE_STRING:
1403 p = talloc_bstrndup(ctx, data->strvalue, inlen);
1404 if (!p) return NULL;
1405 talloc_set_type(p, char);
1409 /* Gets us the size of the buffer we need to alloc */
1410 len = fr_prints_len(data->strvalue, inlen, quote);
1411 p = talloc_array(ctx, char, len);
1412 if (!p) return NULL;
1414 ret = fr_prints(p, len, data->strvalue, inlen, quote);
1415 if (!fr_assert(ret == (len - 1))) {
1422 case PW_TYPE_INTEGER:
1435 DICT_VALUE const *dv;
1437 if (enumv && (dv = dict_valbyattr(enumv->attr, enumv->vendor, i))) {
1438 p = talloc_typed_strdup(ctx, dv->name);
1440 p = talloc_typed_asprintf(ctx, "%u", i);
1445 case PW_TYPE_SIGNED:
1446 p = talloc_typed_asprintf(ctx, "%d", data->sinteger);
1449 case PW_TYPE_INTEGER64:
1450 p = talloc_typed_asprintf(ctx, "%" PRIu64 , data->integer64);
1453 case PW_TYPE_ETHERNET:
1454 p = talloc_typed_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1455 data->ether[0], data->ether[1],
1456 data->ether[2], data->ether[3],
1457 data->ether[4], data->ether[5]);
1460 case PW_TYPE_ABINARY:
1461 #ifdef WITH_ASCEND_BINARY
1462 p = talloc_array(ctx, char, 128);
1463 if (!p) return NULL;
1464 print_abinary(p, 128, (uint8_t *) &data->filter, inlen, 0);
1470 case PW_TYPE_OCTETS:
1471 p = talloc_array(ctx, char, 2 + 1 + inlen * 2);
1472 if (!p) return NULL;
1476 fr_bin2hex(p + 2, data->octets, inlen);
1486 p = talloc_array(ctx, char, 64);
1487 strftime(p, 64, "%b %e %Y %H:%M:%S %Z",
1488 localtime_r(&t, &s_tm));
1493 * We need to use the proper inet_ntop functions for IP
1494 * addresses, else the output might not match output of
1495 * other functions, which makes testing difficult.
1497 * An example is tunnelled ipv4 in ipv6 addresses.
1499 case PW_TYPE_IPV4_ADDR:
1500 case PW_TYPE_IPV4_PREFIX:
1502 char buff[INET_ADDRSTRLEN + 4]; // + /prefix
1505 value_data_prints(buff, sizeof(buff), type, enumv, data, inlen, '\0');
1507 p = talloc_typed_strdup(ctx, buff);
1511 case PW_TYPE_IPV6_ADDR:
1512 case PW_TYPE_IPV6_PREFIX:
1514 char buff[INET6_ADDRSTRLEN + 4]; // + /prefix
1517 value_data_prints(buff, sizeof(buff), type, enumv, data, inlen, '\0');
1519 p = talloc_typed_strdup(ctx, buff);
1524 p = talloc_typed_asprintf(ctx, "%x:%x:%x:%x",
1525 (data->ifid[0] << 8) | data->ifid[1],
1526 (data->ifid[2] << 8) | data->ifid[3],
1527 (data->ifid[4] << 8) | data->ifid[5],
1528 (data->ifid[6] << 8) | data->ifid[7]);
1531 case PW_TYPE_BOOLEAN:
1532 p = talloc_typed_strdup(ctx, data->byte ? "yes" : "no");
1536 * Don't add default here
1538 case PW_TYPE_INVALID:
1539 case PW_TYPE_COMBO_IP_ADDR:
1540 case PW_TYPE_COMBO_IP_PREFIX:
1542 case PW_TYPE_EXTENDED:
1543 case PW_TYPE_LONG_EXTENDED:
1546 case PW_TYPE_TIMEVAL:
1556 /** Print the value of an attribute to a string
1558 * @note return value should be checked with is_truncated.
1559 * @note Will always \0 terminate unless outlen == 0.
1561 * @param out Where to write the printed version of the attribute value.
1562 * @param outlen Length of the output buffer.
1563 * @param type of data being printed.
1564 * @param enumv Enumerated string values for integer types.
1565 * @param data to print.
1566 * @param inlen Length of data.
1567 * @param quote char to escape in string output.
1568 * @return the number of bytes written to the out buffer, or a number >= outlen if truncation has occurred.
1570 size_t value_data_prints(char *out, size_t outlen,
1571 PW_TYPE type, DICT_ATTR const *enumv, value_data_t const *data,
1572 ssize_t inlen, char quote)
1575 char buf[1024]; /* Interim buffer to use with poorly behaved printing functions */
1576 char const *a = NULL;
1581 size_t len = 0, freespace = outlen;
1583 if (!data) return 0;
1584 if (outlen == 0) return inlen;
1589 case PW_TYPE_STRING:
1592 * Ensure that WE add the quotation marks around the string.
1595 if (freespace < 3) return inlen + 2;
1600 len = fr_prints(out, freespace, data->strvalue, inlen, quote);
1601 /* always terminate the quoted string with another quote */
1602 if (len >= (freespace - 1)) {
1603 out[outlen - 2] = (char) quote;
1604 out[outlen - 1] = '\0';
1610 *out++ = (char) quote;
1617 return fr_prints(out, outlen, data->strvalue, inlen, quote);
1619 case PW_TYPE_INTEGER:
1631 /* Normal, non-tagged attribute */
1632 if (enumv && (v = dict_valbyattr(enumv->attr, enumv->vendor, i)) != NULL) {
1636 /* should never be truncated */
1637 len = snprintf(buf, sizeof(buf), "%u", i);
1642 case PW_TYPE_INTEGER64:
1643 return snprintf(out, outlen, "%" PRIu64, data->integer64);
1648 len = strftime(buf, sizeof(buf) - 1, "%%%b %e %Y %H:%M:%S %Z%%", localtime_r(&t, &s_tm));
1649 buf[0] = (char) quote;
1650 buf[len - 1] = (char) quote;
1653 len = strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z", localtime_r(&t, &s_tm));
1658 case PW_TYPE_SIGNED: /* Damned code for 1 WiMAX attribute */
1659 len = snprintf(buf, sizeof(buf), "%d", data->sinteger);
1663 case PW_TYPE_IPV4_ADDR:
1664 a = inet_ntop(AF_INET, &(data->ipaddr), buf, sizeof(buf));
1668 case PW_TYPE_ABINARY:
1669 #ifdef WITH_ASCEND_BINARY
1670 print_abinary(buf, sizeof(buf), (uint8_t const *) data->filter, len, quote);
1677 case PW_TYPE_OCTETS:
1682 /* Return the number of bytes we would have written */
1683 len = (inlen * 2) + 2;
1684 if (freespace <= 1) {
1691 if (freespace <= 1) {
1698 if (freespace <= 2) {
1703 /* Get maximum number of bytes we can encode given freespace */
1704 max = ((freespace % 2) ? freespace - 1 : freespace - 2) / 2;
1705 fr_bin2hex(out, data->octets, ((size_t)inlen > max) ? max : (size_t)inlen);
1710 a = ifid_ntoa(buf, sizeof(buf), data->ifid);
1714 case PW_TYPE_IPV6_ADDR:
1715 a = inet_ntop(AF_INET6, &data->ipv6addr, buf, sizeof(buf));
1719 case PW_TYPE_IPV6_PREFIX:
1721 struct in6_addr addr;
1726 memcpy(&addr, &(data->ipv6prefix[2]), sizeof(addr));
1728 a = inet_ntop(AF_INET6, &addr, buf, sizeof(buf));
1734 len += snprintf(p, sizeof(buf) - len, "/%u", (unsigned int) data->ipv6prefix[1]);
1739 case PW_TYPE_IPV4_PREFIX:
1741 struct in_addr addr;
1746 memcpy(&addr, &(data->ipv4prefix[2]), sizeof(addr));
1748 a = inet_ntop(AF_INET, &addr, buf, sizeof(buf));
1754 len += snprintf(p, sizeof(buf) - len, "/%u", (unsigned int) (data->ipv4prefix[1] & 0x3f));
1759 case PW_TYPE_ETHERNET:
1760 return snprintf(out, outlen, "%02x:%02x:%02x:%02x:%02x:%02x",
1761 data->ether[0], data->ether[1],
1762 data->ether[2], data->ether[3],
1763 data->ether[4], data->ether[5]);
1766 * Don't add default here
1768 case PW_TYPE_INVALID:
1769 case PW_TYPE_COMBO_IP_ADDR:
1770 case PW_TYPE_COMBO_IP_PREFIX:
1771 case PW_TYPE_EXTENDED:
1772 case PW_TYPE_LONG_EXTENDED:
1775 case PW_TYPE_TIMEVAL:
1776 case PW_TYPE_BOOLEAN:
1783 if (a) strlcpy(out, a, outlen);
1785 return len; /* Return the number of bytes we would of written (for truncation detection) */