2 * dict.c Routines to read the dictionary file.
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
24 #include <freeradius-devel/libradius.h>
27 #include <freeradius-devel/dhcp.h>
36 #ifdef HAVE_SYS_STAT_H
40 static fr_hash_table_t *vendors_byname = NULL;
41 static fr_hash_table_t *vendors_byvalue = NULL;
43 static fr_hash_table_t *attributes_byname = NULL;
44 static fr_hash_table_t *attributes_byvalue = NULL;
46 static fr_hash_table_t *attributes_combo = NULL;
48 static fr_hash_table_t *values_byvalue = NULL;
49 static fr_hash_table_t *values_byname = NULL;
51 static DICT_ATTR *dict_base_attrs[256];
54 * For faster HUP's, we cache the stat information for
55 * files we've $INCLUDEd
57 typedef struct dict_stat_t {
58 struct dict_stat_t *next;
62 static dict_stat_t *stat_head = NULL;
63 static dict_stat_t *stat_tail = NULL;
65 typedef struct value_fixup_t {
66 char attrstr[DICT_ATTR_MAX_NAME_LEN];
68 struct value_fixup_t *next;
73 * So VALUEs in the dictionary can have forward references.
75 static value_fixup_t *value_fixup = NULL;
77 const FR_NAME_NUMBER dict_attr_types[] = {
78 { "integer", PW_TYPE_INTEGER },
79 { "string", PW_TYPE_STRING },
80 { "ipaddr", PW_TYPE_IPV4_ADDR },
81 { "date", PW_TYPE_DATE },
82 { "abinary", PW_TYPE_ABINARY },
83 { "octets", PW_TYPE_OCTETS },
84 { "ifid", PW_TYPE_IFID },
85 { "ipv6addr", PW_TYPE_IPV6_ADDR },
86 { "ipv6prefix", PW_TYPE_IPV6_PREFIX },
87 { "byte", PW_TYPE_BYTE },
88 { "short", PW_TYPE_SHORT },
89 { "ether", PW_TYPE_ETHERNET },
90 { "combo-ip", PW_TYPE_COMBO_IP_ADDR },
91 { "tlv", PW_TYPE_TLV },
92 { "signed", PW_TYPE_SIGNED },
93 { "extended", PW_TYPE_EXTENDED },
94 { "long-extended", PW_TYPE_LONG_EXTENDED },
95 { "evs", PW_TYPE_EVS },
96 { "uint8", PW_TYPE_BYTE },
97 { "uint16", PW_TYPE_SHORT },
98 { "uint32", PW_TYPE_INTEGER },
99 { "int32", PW_TYPE_SIGNED },
100 { "integer64", PW_TYPE_INTEGER64 },
101 { "uint64", PW_TYPE_INTEGER64 },
102 { "ipv4prefix", PW_TYPE_IPV4_PREFIX },
103 { "cidr", PW_TYPE_IPV4_PREFIX },
104 { "vsa", PW_TYPE_VSA },
109 * Map data types to min / max data sizes.
111 const size_t dict_attr_sizes[PW_TYPE_MAX][2] = {
112 [PW_TYPE_INVALID] = {~0, 0},
113 [PW_TYPE_STRING] = {0, ~0},
114 [PW_TYPE_INTEGER] = {4, 4 },
115 [PW_TYPE_IPV4_ADDR] = {4, 4},
116 [PW_TYPE_DATE] = {4, 4},
117 [PW_TYPE_ABINARY] = {32, ~0},
118 [PW_TYPE_OCTETS] = {0, ~0},
119 [PW_TYPE_IFID] = {8, 8},
120 [PW_TYPE_IPV6_ADDR] = {16, 16},
121 [PW_TYPE_IPV6_PREFIX] = {2, 18},
122 [PW_TYPE_BYTE] = {1, 1},
123 [PW_TYPE_SHORT] = {2, 2},
124 [PW_TYPE_ETHERNET] = {6, 6},
125 [PW_TYPE_SIGNED] = {4, 4},
126 [PW_TYPE_COMBO_IP_ADDR] = {4, 16},
127 [PW_TYPE_TLV] = {2, ~0},
128 [PW_TYPE_EXTENDED] = {2, ~0},
129 [PW_TYPE_LONG_EXTENDED] = {3, ~0},
130 [PW_TYPE_EVS] = {6, ~0},
131 [PW_TYPE_INTEGER64] = {8, 8},
132 [PW_TYPE_IPV4_PREFIX] = {6, 6},
133 [PW_TYPE_VSA] = {4, ~0}
137 * For packing multiple TLV numbers into one 32-bit integer. The
138 * first 3 bytes are just the 8-bit number. The next two are
139 * more limited. We only allow 31 attributes nested 3 layers
140 * deep, and only 7 nested 4 layers deep. This should be
141 * sufficient for most purposes.
143 * For TLVs and extended attributes, we packet the base attribute
144 * number into the upper 8 bits of the "vendor" field.
146 * e.g. OID attribute vendor
147 * 241.1 1 (241 << 24)
148 * 241.26.9.1 1 (241 << 24) | (9)
149 * 241.1.2 1 | (2 << 8) (241 << 24)
151 #define MAX_TLV_NEST (4)
154 * 8 bits of base attribute
155 * 8 bits for nested TLV 1
156 * 8 bits for nested TLV 2
157 * 5 bits for nested TLV 3
158 * 3 bits for nested TLV 4
160 int const fr_attr_max_tlv = MAX_TLV_NEST;
161 int const fr_attr_shift[MAX_TLV_NEST + 1] = { 0, 8, 16, 24, 29 };
163 int const fr_attr_mask[MAX_TLV_NEST + 1] = { 0xff, 0xff, 0xff, 0x1f, 0x07 };
167 * Create the hash of the name.
169 * We copy the hash function here because it's substantially faster.
171 #define FNV_MAGIC_INIT (0x811c9dc5)
172 #define FNV_MAGIC_PRIME (0x01000193)
174 static uint32_t dict_hashname(char const *name)
176 uint32_t hash = FNV_MAGIC_INIT;
179 for (p = name; *p != '\0'; p++) {
180 int c = *(unsigned char const *) p;
181 if (isalpha(c)) c = tolower(c);
183 hash *= FNV_MAGIC_PRIME;
184 hash ^= (uint32_t ) (c & 0xff);
192 * Hash callback functions.
194 static uint32_t dict_attr_name_hash(void const *data)
196 return dict_hashname(((DICT_ATTR const *)data)->name);
199 static int dict_attr_name_cmp(void const *one, void const *two)
201 DICT_ATTR const *a = one;
202 DICT_ATTR const *b = two;
204 return strcasecmp(a->name, b->name);
207 static uint32_t dict_attr_value_hash(void const *data)
210 DICT_ATTR const *attr = data;
212 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
213 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
216 static int dict_attr_value_cmp(void const *one, void const *two)
218 DICT_ATTR const *a = one;
219 DICT_ATTR const *b = two;
221 if (a->vendor < b->vendor) return -1;
222 if (a->vendor > b->vendor) return +1;
224 return a->attr - b->attr;
227 static uint32_t dict_attr_combo_hash(void const *data)
230 DICT_ATTR const *attr = data;
232 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
233 hash = fr_hash_update(&attr->type, sizeof(attr->type), hash);
234 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
237 static int dict_attr_combo_cmp(void const *one, void const *two)
239 DICT_ATTR const *a = one;
240 DICT_ATTR const *b = two;
242 if (a->type < b->type) return -1;
243 if (a->type > b->type) return +1;
245 if (a->vendor < b->vendor) return -1;
246 if (a->vendor > b->vendor) return +1;
248 return a->attr - b->attr;
251 static uint32_t dict_vendor_name_hash(void const *data)
253 return dict_hashname(((DICT_VENDOR const *)data)->name);
256 static int dict_vendor_name_cmp(void const *one, void const *two)
258 DICT_VENDOR const *a = one;
259 DICT_VENDOR const *b = two;
261 return strcasecmp(a->name, b->name);
264 static uint32_t dict_vendor_value_hash(void const *data)
266 return fr_hash(&(((DICT_VENDOR const *)data)->vendorpec),
267 sizeof(((DICT_VENDOR const *)data)->vendorpec));
270 static int dict_vendor_value_cmp(void const *one, void const *two)
272 DICT_VENDOR const *a = one;
273 DICT_VENDOR const *b = two;
275 return a->vendorpec - b->vendorpec;
278 static uint32_t dict_value_name_hash(void const *data)
281 DICT_VALUE const *dval = data;
283 hash = dict_hashname(dval->name);
284 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
285 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
288 static int dict_value_name_cmp(void const *one, void const *two)
291 DICT_VALUE const *a = one;
292 DICT_VALUE const *b = two;
294 rcode = a->attr - b->attr;
295 if (rcode != 0) return rcode;
297 rcode = a->vendor - b->vendor;
298 if (rcode != 0) return rcode;
300 return strcasecmp(a->name, b->name);
303 static uint32_t dict_value_value_hash(void const *data)
306 DICT_VALUE const *dval = data;
308 hash = fr_hash(&dval->attr, sizeof(dval->attr));
309 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
310 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
313 static int dict_value_value_cmp(void const *one, void const *two)
316 DICT_VALUE const *a = one;
317 DICT_VALUE const *b = two;
319 if (a->vendor < b->vendor) return -1;
320 if (a->vendor > b->vendor) return +1;
322 rcode = a->attr - b->attr;
323 if (rcode != 0) return rcode;
325 return a->value - b->value;
330 * Free the list of stat buffers
332 static void dict_stat_free(void)
334 dict_stat_t *this, *next;
341 for (this = stat_head; this != NULL; this = next) {
346 stat_head = stat_tail = NULL;
351 * Add an entry to the list of stat buffers.
353 static void dict_stat_add(struct stat const *stat_buf)
357 this = malloc(sizeof(*this));
359 memset(this, 0, sizeof(*this));
361 memcpy(&(this->stat_buf), stat_buf, sizeof(this->stat_buf));
364 stat_head = stat_tail = this;
366 stat_tail->next = this;
373 * See if any dictionaries have changed. If not, don't
376 static int dict_stat_check(char const *dir, char const *file)
378 struct stat stat_buf;
383 * Nothing cached, all files are new.
385 if (!stat_head) return 0;
390 snprintf(buffer, sizeof(buffer), "%s/%s", dir, file);
391 if (stat(buffer, &stat_buf) < 0) return 0;
394 * Find the cache entry.
395 * FIXME: use a hash table.
396 * FIXME: check dependencies, via children.
397 * if A loads B and B changes, we probably want
398 * to reload B at the minimum.
400 for (this = stat_head; this != NULL; this = this->next) {
401 if (this->stat_buf.st_dev != stat_buf.st_dev) continue;
402 if (this->stat_buf.st_ino != stat_buf.st_ino) continue;
405 * The file has changed. Re-read it.
407 if (this->stat_buf.st_mtime < stat_buf.st_mtime) return 0;
410 * The file is the same. Ignore it.
421 typedef struct fr_pool_t {
424 struct fr_pool_t *page_free;
425 struct fr_pool_t *page_next;
428 #define FR_POOL_SIZE (32768)
429 #define FR_ALLOC_ALIGN (8)
431 static fr_pool_t *dict_pool = NULL;
433 static fr_pool_t *fr_pool_create(void)
435 fr_pool_t *fp = malloc(FR_POOL_SIZE);
437 if (!fp) return NULL;
439 memset(fp, 0, FR_POOL_SIZE);
441 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
442 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
444 fp->page_next = NULL;
448 static void fr_pool_delete(fr_pool_t **pfp)
450 fr_pool_t *fp, *next;
452 if (!pfp || !*pfp) return;
454 for (fp = *pfp; fp != NULL; fp = next) {
455 next = fp->page_next;
456 fp->page_next = NULL;
463 static void *fr_pool_alloc(size_t size)
467 if (size == 0) return NULL;
469 if (size > 256) return NULL; /* shouldn't happen */
472 dict_pool = fr_pool_create();
473 if (!dict_pool) return NULL;
476 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
477 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
480 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
481 dict_pool->page_free->page_next = fr_pool_create();
482 if (!dict_pool->page_free->page_next) return NULL;
483 dict_pool->page_free = dict_pool->page_free->page_next;
486 ptr = dict_pool->page_free->free_ptr;
487 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
493 static void fr_pool_free(UNUSED void *ptr)
496 * Place-holder for later code.
501 * Free the dictionary_attributes and dictionary_values lists.
508 fr_hash_table_free(vendors_byname);
509 fr_hash_table_free(vendors_byvalue);
510 vendors_byname = NULL;
511 vendors_byvalue = NULL;
513 fr_hash_table_free(attributes_byname);
514 fr_hash_table_free(attributes_byvalue);
515 fr_hash_table_free(attributes_combo);
516 attributes_byname = NULL;
517 attributes_byvalue = NULL;
518 attributes_combo = NULL;
520 fr_hash_table_free(values_byname);
521 fr_hash_table_free(values_byvalue);
522 values_byname = NULL;
523 values_byvalue = NULL;
525 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
527 fr_pool_delete(&dict_pool);
533 * Add vendor to the list.
535 int dict_addvendor(char const *name, unsigned int value)
540 if (value >= FR_MAX_VENDOR) {
541 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
545 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
546 fr_strerror_printf("dict_addvendor: vendor name too long");
550 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
551 fr_strerror_printf("dict_addvendor: out of memory");
555 strcpy(dv->name, name);
556 dv->vendorpec = value;
557 dv->type = dv->length = 1; /* defaults */
559 if (!fr_hash_table_insert(vendors_byname, dv)) {
562 old_dv = fr_hash_table_finddata(vendors_byname, dv);
564 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
567 if (old_dv->vendorpec != dv->vendorpec) {
568 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
573 * Already inserted. Discard the duplicate entry.
580 * Insert the SAME pointer (not free'd when this table is
581 * deleted), into another table.
583 * We want this behaviour because we want OLD names for
584 * the attributes to be read from the configuration
585 * files, but when we're printing them, (and looking up
586 * by value) we want to use the NEW name.
588 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
589 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
597 const int dict_attr_allowed_chars[256] = {
598 /* 0x 0 1 2 3 4 5 6 7 8 9 a b c d e f */
599 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
600 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
601 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
602 /* 3 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
603 /* 4 */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
604 /* 5 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
605 /* 6 */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
606 /* 7 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
607 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
608 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
609 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
610 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
611 /* c */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
612 /* d */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
613 /* e */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
614 /* f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
620 int dict_valid_name(char const *name)
624 for (p = (uint8_t const *) name; *p != '\0'; p++) {
625 if (!dict_attr_allowed_chars[*p]) {
628 fr_prints(buff, sizeof(buff), (char const *)p, 1, '\'');
629 fr_strerror_printf("Invalid character '%s' in attribute", buff);
631 return -(p - (uint8_t const *)name);
639 * Add an attribute to the dictionary.
641 int dict_addattr(char const *name, int attr, unsigned int vendor, PW_TYPE type,
645 static int max_attr = 0;
649 namelen = strlen(name);
650 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
651 fr_strerror_printf("dict_addattr: attribute name too long");
655 if (dict_valid_name(name) < 0) return -1;
658 !((type == PW_TYPE_INTEGER) || (type == PW_TYPE_STRING))) {
659 fr_strerror_printf("dict_addattr: Only 'integer' and 'string' attributes can have tags");
665 * If the attr is '-1', that means use a pre-existing
666 * one (if it already exists). If one does NOT already exist,
667 * then create a new attribute, with a non-conflicting value,
671 if (dict_attrbyname(name)) {
672 return 0; /* exists, don't add it again */
677 } else if (vendor == 0) {
681 if (attr > max_attr) {
687 * Additional checks for extended attributes.
689 if (flags.extended || flags.long_extended || flags.evs) {
690 if (vendor && (vendor < FR_MAX_VENDOR)) {
691 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats");
698 || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
699 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set");
705 if (!(flags.extended || flags.long_extended)) {
706 fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
710 /* VSAs cannot be of format EVS */
711 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
712 fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
718 * Allow for generic pointers
727 flags.is_pointer = true;
732 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
736 if (flags.has_tlv && flags.length) {
737 fr_strerror_printf("TLVs cannot have a fixed length");
741 if (vendor && flags.concat) {
742 fr_strerror_printf("VSAs cannot have the \"concat\" flag set");
746 if (flags.concat && (type != PW_TYPE_OCTETS)) {
747 fr_strerror_printf("The \"concat\" flag can only be set for attributes of type \"octets\"");
751 if (flags.concat && (flags.has_tag || flags.array || flags.is_tlv || flags.has_tlv ||
752 flags.length || flags.evs || flags.extended || flags.long_extended ||
753 (flags.encrypt != FLAG_ENCRYPT_NONE))) {
754 fr_strerror_printf("The \"concat\" flag cannot be used with any other flag");
758 if ((vendor & (FR_MAX_VENDOR -1)) != 0) {
760 static DICT_VENDOR *last_vendor = NULL;
762 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
763 fr_strerror_printf("TLV's cannot be encrypted");
767 if (flags.is_tlv && flags.has_tag) {
768 fr_strerror_printf("Sub-TLV's cannot have a tag");
772 if (flags.has_tlv && flags.has_tag) {
773 fr_strerror_printf("TLV's cannot have a tag");
778 * Most ATTRIBUTEs are bunched together by
779 * VENDOR. We can save a lot of lookups on
780 * dictionary initialization by caching the last
784 ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
788 * Ignore the high byte (sigh)
790 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
795 * If the vendor isn't defined, die.
798 fr_strerror_printf("dict_addattr: Unknown vendor %u",
799 vendor & (FR_MAX_VENDOR - 1));
804 * FIXME: Switch over dv->type, and limit things
807 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
808 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255)");
810 } /* else 256..65535 are allowed */
813 * If the attribute is in the standard space, AND
814 * has a sub-type (e.g. 241.1 or 255.3), then its
815 * number is placed into the upper 8 bits of the
818 * This also happens for the new VSAs.
820 * If we find it, then set the various flags
821 * based on what we see.
823 if (vendor >= FR_MAX_VENDOR) {
826 parent = (vendor / FR_MAX_VENDOR) & 0xff;
828 da = dict_attrbyvalue(parent, 0);
830 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown parent attribute %u.", parent);
835 * These flags are inherited from the
838 flags.extended = da->flags.extended;
839 flags.long_extended = da->flags.long_extended;
842 * Non-extended attributes can't have VSAs.
844 if (!flags.extended &&
845 ((vendor & (FR_MAX_VENDOR - 1)) != 0)) {
846 fr_strerror_printf("dict_addattr: ATTRIBUTE cannot be a VSA");
850 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
856 * <sigh> Alvarion, being *again* a horribly
857 * broken vendor, has re-used the WiMAX format in
858 * their proprietary vendor space. This re-use
859 * means that there are *multiple* conflicting
860 * Alvarion dictionaries.
862 flags.wimax = dv->flags;
866 * Create a new attribute for the list
868 if ((n = fr_pool_alloc(sizeof(*n) + namelen)) == NULL) {
870 fr_strerror_printf("dict_addattr: out of memory");
874 memcpy(n->name, name, namelen);
875 n->name[namelen] = '\0';
882 * Insert the attribute, only if it's not a duplicate.
884 if (!fr_hash_table_insert(attributes_byname, n)) {
888 * If the attribute has identical number, then
889 * ignore the duplicate.
891 a = fr_hash_table_finddata(attributes_byname, n);
892 if (a && (strcasecmp(a->name, n->name) == 0)) {
893 if (a->attr != n->attr) {
894 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
900 * Same name, same vendor, same attr,
901 * maybe the flags and/or type is
902 * different. Let the new value
903 * over-ride the old one.
908 fr_hash_table_delete(attributes_byvalue, a);
910 if (!fr_hash_table_replace(attributes_byname, n)) {
911 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
918 * Insert the SAME pointer (not free'd when this entry is
919 * deleted), into another table.
921 * We want this behaviour because we want OLD names for
922 * the attributes to be read from the configuration
923 * files, but when we're printing them, (and looking up
924 * by value) we want to use the NEW name.
926 if (!fr_hash_table_replace(attributes_byvalue, n)) {
927 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
934 if (n->type == PW_TYPE_COMBO_IP_ADDR) {
937 v4 = fr_pool_alloc(sizeof(*v4) + namelen);
940 v6 = fr_pool_alloc(sizeof(*v6) + namelen);
943 memcpy(v4, n, sizeof(*v4) + namelen);
944 v4->type = PW_TYPE_IPV4_ADDR;
946 memcpy(v6, n, sizeof(*v6) + namelen);
947 v6->type = PW_TYPE_IPV6_ADDR;
948 if (!fr_hash_table_replace(attributes_combo, v4)) {
949 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv4", name);
953 if (!fr_hash_table_replace(attributes_combo, v6)) {
954 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv6", name);
959 if (!vendor && (attr > 0) && (attr < 256)) {
960 dict_base_attrs[attr] = n;
968 * Add a value for an attribute to the dictionary.
970 int dict_addvalue(char const *namestr, char const *attrstr, int value)
976 static DICT_ATTR const *last_attr = NULL;
979 fr_strerror_printf("dict_addvalue: empty names are not permitted");
983 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
984 fr_strerror_printf("dict_addvalue: value name too long");
988 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
989 fr_strerror_printf("dict_addvalue: out of memory");
992 memset(dval, 0, sizeof(*dval));
994 strcpy(dval->name, namestr);
998 * Most VALUEs are bunched together by ATTRIBUTE. We can
999 * save a lot of lookups on dictionary initialization by
1000 * caching the last attribute.
1002 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
1005 da = dict_attrbyname(attrstr);
1010 * Remember which attribute is associated with this
1011 * value, if possible.
1014 if (da->flags.has_value_alias) {
1015 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
1019 dval->attr = da->attr;
1020 dval->vendor = da->vendor;
1023 * Enforce valid values
1025 * Don't worry about fixups...
1031 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
1036 if (value > 65535) {
1038 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
1044 * Allow octets for now, because
1045 * of dictionary.cablelabs
1047 case PW_TYPE_OCTETS:
1049 case PW_TYPE_INTEGER:
1052 case PW_TYPE_INTEGER64:
1055 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
1056 fr_int2str(dict_attr_types, da->type, "?Unknown?"));
1060 value_fixup_t *fixup;
1062 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
1065 fr_strerror_printf("dict_addvalue: out of memory");
1068 memset(fixup, 0, sizeof(*fixup));
1070 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
1074 * Insert to the head of the list.
1076 fixup->next = value_fixup;
1077 value_fixup = fixup;
1083 * Add the value into the dictionary.
1087 memcpy(&tmp, &dval, sizeof(tmp));
1089 if (!fr_hash_table_insert(values_byname, tmp)) {
1094 * Suppress duplicates with the same
1095 * name and value. There are lots in
1096 * dictionary.ascend.
1098 old = dict_valbyname(da->attr, da->vendor, namestr);
1099 if (old && (old->value == dval->value)) {
1106 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
1112 * There are multiple VALUE's, keyed by attribute, so we
1113 * take care of that here.
1115 if (!fr_hash_table_replace(values_byvalue, dval)) {
1116 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
1124 static int sscanf_i(char const *str, unsigned int *pvalue)
1128 static char const *tab = "0123456789";
1130 if ((str[0] == '0') &&
1131 ((str[1] == 'x') || (str[1] == 'X'))) {
1132 tab = "0123456789abcdef";
1141 if (*str == '.') break;
1143 c = memchr(tab, tolower((int) *str), base);
1157 * Get the OID based on various pieces of information.
1159 * Remember, the packing format is weird.
1161 * 00VID 000000AA normal VSA for vendor VID
1162 * 00VID AABBCCDD normal VSAs with TLVs
1163 * EE000 000000AA extended attr (241.1)
1164 * EE000 AABBCCDD extended attr with TLVs
1165 * EEVID 000000AA EVS with vendor VID, attr AAA
1166 * EEVID AABBCCDD EVS with TLVs
1168 * <whew>! Are we crazy, or what?
1170 int dict_str2oid(char const *ptr, unsigned int *pvalue, unsigned int *pvendor,
1175 DICT_ATTR const *da = NULL;
1177 if (tlv_depth > fr_attr_max_tlv) {
1178 fr_strerror_printf("Too many sub-attributes");
1183 * If *pvalue is set, check if the attribute exists.
1184 * Otherwise, check that the vendor exists.
1187 da = dict_attrbyvalue(*pvalue, *pvendor);
1189 fr_strerror_printf("Parent attribute is undefined");
1193 if (!da->flags.has_tlv && !da->flags.extended) {
1194 fr_strerror_printf("Parent attribute %s cannot have sub-attributes",
1199 } else if ((*pvendor & (FR_MAX_VENDOR - 1)) != 0) {
1200 if (!dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1))) {
1201 fr_strerror_printf("Unknown vendor %u",
1202 *pvendor & (FR_MAX_VENDOR - 1));
1207 p = strchr(ptr, '.');
1210 * Look for 26.VID.x.y
1212 * If we find it, re-write the parameters, and recurse.
1214 if (!*pvendor && (tlv_depth == 0) && (*pvalue == PW_VENDOR_SPECIFIC)) {
1215 DICT_VENDOR const *dv;
1218 fr_strerror_printf("VSA needs to have sub-attribute");
1222 if (!sscanf_i(ptr, pvendor)) {
1223 fr_strerror_printf("Invalid number in attribute");
1227 if (*pvendor >= FR_MAX_VENDOR) {
1228 fr_strerror_printf("Cannot handle vendor ID larger than 2^24");
1233 dv = dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1));
1235 fr_strerror_printf("Unknown vendor \"%u\" ",
1236 *pvendor & (FR_MAX_VENDOR - 1));
1241 * Start off with (attr=0, vendor=VID), and
1242 * recurse. This causes the various checks above
1246 return dict_str2oid(p + 1, pvalue, pvendor, 0);
1249 if (!sscanf_i(ptr, &value)) {
1250 fr_strerror_printf("Invalid number in attribute");
1254 if (!*pvendor && (tlv_depth == 1) && da &&
1255 (da->flags.has_tlv || da->flags.extended)) {
1258 *pvendor = *pvalue * FR_MAX_VENDOR;
1262 return dict_str2oid(p + 1, pvalue, pvendor, 1);
1266 * And pack the data according to the scheme described in
1267 * the comments at the start of this function.
1270 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
1276 return dict_str2oid(p + 1, pvalue, pvendor, tlv_depth + 1);
1283 * Bamboo skewers under the fingernails in 5, 4, 3, 2, ...
1285 static DICT_ATTR const *dict_parent(unsigned int attr, unsigned int vendor)
1287 if (vendor < FR_MAX_VENDOR) {
1288 return dict_attrbyvalue(attr & 0xff, vendor);
1292 return dict_attrbyvalue((vendor / FR_MAX_VENDOR) & 0xff, 0);
1295 return dict_attrbyvalue(attr & 0xff, vendor);
1300 * Process the ATTRIBUTE command
1302 static int process_attribute(char const* fn, int const line,
1303 unsigned int block_vendor,
1304 DICT_ATTR const *block_tlv, int tlv_depth,
1305 char **argv, int argc)
1308 unsigned int vendor = 0;
1311 unsigned int length = 0;
1315 if ((argc < 3) || (argc > 4)) {
1316 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
1322 * Dictionaries need to have real names, not shitty ones.
1324 if (strncmp(argv[1], "Attr-", 5) == 0) {
1325 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1330 memset(&flags, 0, sizeof(flags));
1333 * Look for OIDs before doing anything else.
1335 p = strchr(argv[1], '.');
1339 * Validate all entries
1341 if (!sscanf_i(argv[1], &value)) {
1342 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1347 DICT_ATTR const *da;
1349 vendor = block_vendor;
1352 * Parse the rest of the OID.
1354 if (dict_str2oid(p + 1, &value, &vendor, tlv_depth + 1) < 0) {
1357 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1359 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1362 block_vendor = vendor;
1365 * Set the flags based on the parents flags.
1367 da = dict_parent(value, vendor);
1369 fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
1373 flags.extended = da->flags.extended;
1374 flags.long_extended = da->flags.long_extended;
1375 flags.evs = da->flags.evs;
1376 if (da->flags.has_tlv) flags.is_tlv = 1;
1379 if (strncmp(argv[2], "octets[", 7) != 0) {
1381 * find the type of the attribute.
1383 type = fr_str2int(dict_attr_types, argv[2], -1);
1385 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1391 type = PW_TYPE_OCTETS;
1393 p = strchr(argv[2] + 7, ']');
1395 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1401 if (!sscanf_i(argv[1], &length)) {
1402 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1406 if ((length == 0) || (length > 253)) {
1407 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1413 * Only look up the vendor if the string
1418 * Force "length" for data types of fixed length;
1430 case PW_TYPE_IPV4_ADDR:
1431 case PW_TYPE_INTEGER:
1432 case PW_TYPE_SIGNED:
1436 case PW_TYPE_INTEGER64:
1440 case PW_TYPE_ETHERNET:
1448 case PW_TYPE_IPV6_ADDR:
1452 case PW_TYPE_EXTENDED:
1453 if ((vendor != 0) || (value < 241)) {
1454 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended\" MUST be RFC attributes with value >= 241.", fn, line);
1460 case PW_TYPE_LONG_EXTENDED:
1461 if ((vendor != 0) || (value < 241)) {
1462 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"long-extended\" MUST be RFC attributes with value >= 241.", fn, line);
1466 flags.long_extended = 1;
1472 if (value != PW_VENDOR_SPECIFIC) {
1473 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1482 flags.length = length;
1484 } else { /* argc == 4: we have options */
1485 char *key, *next, *last;
1490 if (flags.extended) {
1491 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1496 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1502 next = strchr(key, ',');
1503 if (next) *(next++) = '\0';
1505 if (strcmp(key, "has_tag") == 0 ||
1506 strcmp(key, "has_tag=1") == 0) {
1507 /* Boolean flag, means this is a
1511 } else if (strncmp(key, "encrypt=", 8) == 0) {
1512 /* Encryption method, defaults to 0 (none).
1513 Currently valid is just type 2,
1514 Tunnel-Password style, which can only
1515 be applied to strings. */
1516 flags.encrypt = strtol(key + 8, &last, 0);
1518 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1523 if ((flags.encrypt == FLAG_ENCRYPT_ASCEND_SECRET) &&
1524 (type != PW_TYPE_STRING)) {
1525 fr_strerror_printf( "dict_init: %s[%d] Only \"string\" types can have the \"encrypt=3\" flag set.",
1530 } else if (strncmp(key, "array", 6) == 0) {
1534 case PW_TYPE_IPV4_ADDR:
1535 case PW_TYPE_IPV6_ADDR:
1538 case PW_TYPE_INTEGER:
1540 case PW_TYPE_STRING:
1544 fr_strerror_printf( "dict_init: %s[%d] \"%s\" type cannot have the "
1545 "\"array\" flag set",
1547 fr_int2str(dict_attr_types, type, "<UNKNOWN>"));
1551 } else if (strncmp(key, "concat", 7) == 0) {
1554 if (type != PW_TYPE_OCTETS) {
1555 fr_strerror_printf( "dict_init: %s[%d] Only \"octets\" type can have the \"concat\" flag set.",
1560 } else if (strncmp(key, "virtual", 8) == 0) {
1564 fr_strerror_printf( "dict_init: %s[%d] VSAs cannot have the \"virtual\" flag set.",
1570 fr_strerror_printf( "dict_init: %s[%d] Standard attributes cannot have the \"virtual\" flag set.",
1576 * The only thing is the vendor name,
1577 * and it's a known name: allow it.
1579 } else if ((key == argv[3]) && !next) {
1581 fr_strerror_printf( "dict_init: %s[%d] New-style attributes cannot use a vendor flag.",
1587 fr_strerror_printf( "dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" is not allowed.",
1592 vendor = dict_vendorbyname(key);
1593 if (!vendor) goto unknown;
1598 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1604 if (key && !*key) break;
1608 if (block_vendor) vendor = block_vendor;
1611 * Special checks for tags, they make our life much more
1614 if (flags.has_tag) {
1616 * Only string, octets, and integer can be tagged.
1619 case PW_TYPE_STRING:
1620 case PW_TYPE_INTEGER:
1624 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1626 fr_int2str(dict_attr_types, type, "?Unknown?"));
1631 if (type == PW_TYPE_TLV) {
1632 if (vendor && (vendor < FR_MAX_VENDOR)
1634 && (vendor != DHCP_MAGIC_VENDOR)
1639 dv = dict_vendorbyvalue(vendor);
1640 if (!dv || (dv->type != 1) || (dv->length != 1)) {
1641 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1652 * TLV's can be only one octet.
1654 if ((value == 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1655 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1663 value <<= fr_attr_shift[tlv_depth];
1664 value |= block_tlv->attr;
1668 #ifdef WITH_DICTIONARY_WARNINGS
1670 * Hack to help us discover which vendors have illegal
1673 if (!vendor && (value < 256) &&
1674 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1675 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1683 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1686 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1688 fr_strerror_printf("dict_init: %s[%d]: %s",
1698 * Process the VALUE command
1700 static int process_value(char const* fn, int const line, char **argv,
1706 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1711 * For Compatibility, skip "Server-Config"
1713 if (strcasecmp(argv[0], "Server-Config") == 0)
1717 * Validate all entries
1719 if (!sscanf_i(argv[2], &value)) {
1720 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1725 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1728 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1730 fr_strerror_printf("dict_init: %s[%d]: %s",
1740 * Process the VALUE-ALIAS command
1742 * This allows VALUE mappings to be shared among multiple
1745 static int process_value_alias(char const* fn, int const line, char **argv,
1748 DICT_ATTR const *my_da, *da;
1752 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1757 my_da = dict_attrbyname(argv[0]);
1759 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1764 if (my_da->flags.has_value_alias) {
1765 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1770 da = dict_attrbyname(argv[1]);
1772 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1777 if (da->flags.has_value_alias) {
1778 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1783 if (my_da->type != da->type) {
1784 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1789 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1790 fr_strerror_printf("dict_addvalue: out of memory");
1794 dval->name[0] = '\0'; /* empty name */
1795 dval->attr = my_da->attr;
1796 dval->vendor = my_da->vendor;
1797 dval->value = da->attr;
1799 if (!fr_hash_table_insert(values_byname, dval)) {
1800 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1811 * Process the VENDOR command
1813 static int process_vendor(char const* fn, int const line, char **argv,
1817 bool continuation = false;
1818 char const *format = NULL;
1820 if ((argc < 2) || (argc > 3)) {
1821 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1827 * Validate all entries
1829 if (!isdigit((int) argv[1][0])) {
1830 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1834 value = atoi(argv[1]);
1836 /* Create a new VENDOR entry for the list */
1837 if (dict_addvendor(argv[0], value) < 0) {
1840 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1842 fr_strerror_printf("dict_init: %s[%d]: %s",
1848 * Look for a format statement
1853 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1854 format = "format=4,0";
1856 } else if (value == VENDORPEC_LUCENT) {
1857 format = "format=2,1";
1859 } else if (value == VENDORPEC_STARENT) {
1860 format = "format=2,2";
1862 } /* else no fixups to do */
1869 if (strncasecmp(format, "format=", 7) != 0) {
1870 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1876 if ((strlen(p) < 3) ||
1877 !isdigit((int) p[0]) ||
1879 !isdigit((int) p[2]) ||
1880 (p[3] && (p[3] != ','))) {
1881 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1886 type = (int) (p[0] - '0');
1887 length = (int) (p[2] - '0');
1891 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1896 if ((p[4] != 'c') ||
1898 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1902 continuation = true;
1904 if ((value != VENDORPEC_WIMAX) ||
1905 (type != 1) || (length != 1)) {
1906 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1912 dv = dict_vendorbyvalue(value);
1914 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1919 if ((type != 1) && (type != 2) && (type != 4)) {
1920 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1925 if ((length != 0) && (length != 1) && (length != 2)) {
1926 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1932 dv->length = length;
1933 dv->flags = continuation;
1940 * String split routine. Splits an input string IN PLACE
1941 * into pieces, based on spaces.
1943 int str2argv(char *str, char **argv, int max_argc)
1948 if (argc >= max_argc) break;
1951 * Chop out comments early.
1958 while ((*str == ' ') ||
1961 (*str == '\n')) *(str++) = '\0';
1972 (*str != '\n')) str++;
1978 static int my_dict_init(char const *parent, char const *filename,
1979 char const *src_file, int src_line);
1981 int dict_read(char const *dir, char const *filename)
1983 if (!attributes_byname) {
1984 fr_strerror_printf("Must call dict_init() before dict_read()");
1988 return my_dict_init(dir, filename, NULL, 0);
1992 #define MAX_ARGV (16)
1995 * Initialize the dictionary.
1997 static int my_dict_init(char const *parent, char const *filename,
1998 char const *src_file, int src_line)
2001 char dir[256], fn[256];
2005 unsigned int vendor;
2006 unsigned int block_vendor;
2007 struct stat statbuf;
2008 char *argv[MAX_ARGV];
2010 DICT_ATTR const *da, *block_tlv[MAX_TLV_NEST + 1];
2011 int which_block_tlv = 0;
2013 block_tlv[0] = NULL;
2014 block_tlv[1] = NULL;
2015 block_tlv[2] = NULL;
2016 block_tlv[3] = NULL;
2018 if ((strlen(parent) + 3 + strlen(filename)) > sizeof(dir)) {
2019 fr_strerror_printf("dict_init: filename name too long");
2024 * If it's an absolute dir, forget the parent dir,
2025 * and remember the new one.
2027 * If it's a relative dir, tack on the current filename
2028 * to the parent dir. And use that.
2030 if (!FR_DIR_IS_RELATIVE(filename)) {
2031 strlcpy(dir, filename, sizeof(dir));
2032 p = strrchr(dir, FR_DIR_SEP);
2036 strlcat(dir, "/", sizeof(dir));
2039 strlcpy(fn, filename, sizeof(fn));
2041 strlcpy(dir, parent, sizeof(dir));
2042 p = strrchr(dir, FR_DIR_SEP);
2044 if (p[1]) strlcat(dir, "/", sizeof(dir));
2046 strlcat(dir, "/", sizeof(dir));
2048 strlcat(dir, filename, sizeof(dir));
2049 p = strrchr(dir, FR_DIR_SEP);
2053 strlcat(dir, "/", sizeof(dir));
2056 p = strrchr(filename, FR_DIR_SEP);
2058 snprintf(fn, sizeof(fn), "%s%s", dir, p);
2060 snprintf(fn, sizeof(fn), "%s%s", dir, filename);
2066 * Check if we've loaded this file before. If so, ignore it.
2068 p = strrchr(fn, FR_DIR_SEP);
2071 if (dict_stat_check(fn, p + 1)) {
2078 if ((fp = fopen(fn, "r")) == NULL) {
2080 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
2081 fn, fr_syserror(errno));
2083 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
2084 src_file, src_line, fn, fr_syserror(errno));
2089 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
2090 if (!S_ISREG(statbuf.st_mode)) {
2092 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
2098 * Globally writable dictionaries means that users can control
2099 * the server configuration with little difficulty.
2102 if ((statbuf.st_mode & S_IWOTH) != 0) {
2104 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
2110 dict_stat_add(&statbuf);
2113 * Seed the random pool with data.
2115 fr_rand_seed(&statbuf, sizeof(statbuf));
2119 while (fgets(buf, sizeof(buf), fp) != NULL) {
2121 if (buf[0] == '#' || buf[0] == 0 ||
2122 buf[0] == '\n' || buf[0] == '\r')
2126 * Comment characters should NOT be appearing anywhere but
2127 * as start of a comment;
2129 p = strchr(buf, '#');
2132 argc = str2argv(buf, argv, MAX_ARGV);
2133 if (argc == 0) continue;
2136 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
2143 * Process VALUE lines.
2145 if (strcasecmp(argv[0], "VALUE") == 0) {
2146 if (process_value(fn, line,
2147 argv + 1, argc - 1) == -1) {
2155 * Perhaps this is an attribute.
2157 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
2158 if (process_attribute(fn, line, block_vendor,
2159 block_tlv[which_block_tlv],
2161 argv + 1, argc - 1) == -1) {
2169 * See if we need to import another dictionary.
2171 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
2172 if (my_dict_init(dir, argv[1], fn, line) < 0) {
2180 * Optionally include a dictionary
2182 if (strcasecmp(argv[0], "$INCLUDE-") == 0) {
2183 int rcode = my_dict_init(dir, argv[1], fn, line);
2185 if (rcode == -2) continue;
2194 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
2195 if (process_value_alias(fn, line,
2196 argv + 1, argc - 1) == -1) {
2204 * Process VENDOR lines.
2206 if (strcasecmp(argv[0], "VENDOR") == 0) {
2207 if (process_vendor(fn, line,
2208 argv + 1, argc - 1) == -1) {
2215 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
2218 "dict_init: %s[%d] invalid BEGIN-TLV entry",
2224 da = dict_attrbyname(argv[1]);
2227 "dict_init: %s[%d]: unknown attribute %s",
2233 if (da->type != PW_TYPE_TLV) {
2235 "dict_init: %s[%d]: attribute %s is not of type tlv",
2241 if (which_block_tlv >= MAX_TLV_NEST) {
2243 "dict_init: %s[%d]: TLVs are nested too deep",
2250 block_tlv[++which_block_tlv] = da;
2254 if (strcasecmp(argv[0], "END-TLV") == 0) {
2257 "dict_init: %s[%d] invalid END-TLV entry",
2263 da = dict_attrbyname(argv[1]);
2266 "dict_init: %s[%d]: unknown attribute %s",
2272 if (da != block_tlv[which_block_tlv]) {
2274 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
2279 block_tlv[which_block_tlv--] = NULL;
2283 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
2286 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
2292 vendor = dict_vendorbyname(argv[1]);
2295 "dict_init: %s[%d]: unknown vendor %s",
2301 block_vendor = vendor;
2304 * Check for extended attr VSAs
2306 * BEGIN-VENDOR foo format=Foo-Encapsulation-Attr
2309 if (strncmp(argv[2], "format=", 7) != 0) {
2311 "dict_init: %s[%d]: Invalid format %s",
2318 da = dict_attrbyname(p);
2320 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
2326 if (!da->flags.evs) {
2327 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
2334 * Pack the encapsulating
2335 * attribute into the upper 8
2336 * bits of the vendor ID
2338 block_vendor |= (da->attr & fr_attr_mask[0]) * FR_MAX_VENDOR;
2342 } /* BEGIN-VENDOR */
2344 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
2347 "dict_init: %s[%d] invalid END-VENDOR entry",
2353 vendor = dict_vendorbyname(argv[1]);
2356 "dict_init: %s[%d]: unknown vendor %s",
2362 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2364 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2374 * Any other string: We don't recognize it.
2376 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2387 * Empty callback for hash table initialization.
2389 static int null_callback(UNUSED void *ctx, UNUSED void *data)
2396 * Initialize the directory, then fix the attr member of
2399 int dict_init(char const *dir, char const *fn)
2402 * Check if we need to change anything. If not, don't do
2405 if (dict_stat_check(dir, fn)) {
2410 * Free the dictionaries, and the stat cache.
2415 * Create the table of vendor by name. There MAY NOT
2416 * be multiple vendors of the same name.
2418 * Each vendor is malloc'd, so the free function is free.
2420 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2421 dict_vendor_name_cmp,
2423 if (!vendors_byname) {
2428 * Create the table of vendors by value. There MAY
2429 * be vendors of the same value. If there are, we
2430 * pick the latest one.
2432 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2433 dict_vendor_value_cmp,
2435 if (!vendors_byvalue) {
2440 * Create the table of attributes by name. There MAY NOT
2441 * be multiple attributes of the same name.
2443 * Each attribute is malloc'd, so the free function is free.
2445 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2448 if (!attributes_byname) {
2453 * Create the table of attributes by value. There MAY
2454 * be attributes of the same value. If there are, we
2455 * pick the latest one.
2457 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2458 dict_attr_value_cmp,
2460 if (!attributes_byvalue) {
2465 * Horrible hacks for combo-IP.
2467 attributes_combo = fr_hash_table_create(dict_attr_combo_hash,
2468 dict_attr_combo_cmp,
2470 if (!attributes_combo) {
2474 values_byname = fr_hash_table_create(dict_value_name_hash,
2475 dict_value_name_cmp,
2477 if (!values_byname) {
2481 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2482 dict_value_value_cmp,
2484 if (!values_byvalue) {
2488 value_fixup = NULL; /* just to be safe. */
2490 if (my_dict_init(dir, fn, NULL, 0) < 0)
2495 value_fixup_t *this, *next;
2497 for (this = value_fixup; this != NULL; this = next) {
2500 a = dict_attrbyname(this->attrstr);
2503 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2504 this->attrstr, this->dval->name);
2505 return -1; /* leak, but they should die... */
2508 this->dval->attr = a->attr;
2511 * Add the value into the dictionary.
2513 if (!fr_hash_table_replace(values_byname,
2515 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2520 * Allow them to use the old name, but
2521 * prefer the new name when printing
2524 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2525 fr_hash_table_replace(values_byvalue,
2531 * Just so we don't lose track of things.
2538 * Walk over all of the hash tables to ensure they're
2539 * initialized. We do this because the threads may perform
2540 * lookups, and we don't want multi-threaded re-ordering
2541 * of the table entries. That would be bad.
2543 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2544 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2546 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2547 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2549 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2550 fr_hash_table_walk(values_byname, null_callback, NULL);
2555 static size_t print_attr_oid(char *buffer, size_t size, unsigned int attr,
2564 return snprintf(buffer, size, "%u", attr);
2567 return snprintf(buffer, size, "%u", attr & 0xffff);
2571 len = snprintf(buffer, size, "%u", attr & 0xff);
2575 if ((attr >> 8) == 0) return len;
2581 for (nest = 1; nest <= fr_attr_max_tlv; nest++) {
2582 if (((attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]) == 0) break;
2584 len = snprintf(buffer, size, ".%u",
2585 (attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]);
2595 /** Free dynamically allocated (unknown attributes)
2597 * If the da was dynamically allocated it will be freed, else the function
2598 * will return without doing anything.
2600 * @param da to free.
2602 void dict_attr_free(DICT_ATTR const **da)
2606 if (!da || !*da) return;
2608 /* Don't free real DAs */
2609 if (!(*da)->flags.is_unknown) {
2613 memcpy(&tmp, &da, sizeof(*tmp));
2620 /** Initialises a dictionary attr for unknown attributes
2622 * Initialises a dict attr for an unknown attribute/vendor/type without adding
2623 * it to dictionary pools/hashes.
2625 * @param[in,out] da struct to initialise, must be at least DICT_ATTR_SIZE bytes.
2626 * @param[in] attr number.
2627 * @param[in] vendor number.
2628 * @return 0 on success.
2630 int dict_unknown_from_fields(DICT_ATTR *da, unsigned int attr, unsigned int vendor)
2635 size_t bufsize = DICT_ATTR_MAX_NAME_LEN;
2637 memset(da, 0, DICT_ATTR_SIZE);
2640 da->vendor = vendor;
2641 da->type = PW_TYPE_OCTETS;
2642 da->flags.is_unknown = true;
2643 da->flags.is_pointer = true;
2646 * Unknown attributes of the "WiMAX" vendor get marked up
2647 * as being for WiMAX.
2649 if (vendor == VENDORPEC_WIMAX) {
2650 da->flags.wimax = 1;
2655 len = snprintf(p, bufsize, "Attr-");
2659 if (vendor > FR_MAX_VENDOR) {
2660 len = snprintf(p, bufsize, "%u.", vendor / FR_MAX_VENDOR);
2663 vendor &= (FR_MAX_VENDOR) - 1;
2670 * dv_type is the length of the vendor's type field
2671 * RFC 2865 never defined a mandatory length, so
2672 * different vendors have different length type fields.
2674 dv = dict_vendorbyvalue(vendor);
2678 len = snprintf(p, bufsize, "26.%u.", vendor);
2684 print_attr_oid(p, bufsize , attr, dv_type);
2689 /** Allocs a dictionary attr for unknown attributes
2691 * Allocs a dict attr for an unknown attribute/vendor/type without adding
2692 * it to dictionary pools/hashes.
2694 * @param[in] ctx to allocate DA in.
2695 * @param[in] attr number.
2696 * @param[in] vendor number.
2697 * @return 0 on success.
2699 DICT_ATTR const *dict_unknown_afrom_fields(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
2704 p = talloc_zero_array(ctx, uint8_t, DICT_ATTR_SIZE);
2706 fr_strerror_printf("Out of memory");
2709 da = (DICT_ATTR *) p;
2710 talloc_set_type(da, DICT_ATTR);
2712 if (dict_unknown_from_fields(da, attr, vendor) < 0) {
2720 /** Create a DICT_ATTR from an ASCII attribute and value
2722 * Where the attribute name is in the form:
2724 * - Attr-%d.%d.%d...
2725 * - Vendor-%d-Attr-%d
2726 * - VendorName-Attr-%d
2728 * @todo should check attr/vendor against dictionary and return the real da.
2730 * @param[in] da to initialise.
2731 * @param[in] name of attribute.
2732 * @return 0 on success -1 on failure.
2734 int dict_unknown_from_str(DICT_ATTR *da, char const *name)
2736 unsigned int attr, vendor = 0;
2737 unsigned int dv_type = 1; /* The type of vendor field */
2739 char const *p = name;
2743 DICT_ATTR const *found;
2745 if (dict_valid_name(name) < 0) return -1;;
2748 * Pull off vendor prefix first.
2750 if (strncasecmp(p, "Attr-", 5) != 0) {
2751 if (strncasecmp(p, "Vendor-", 7) == 0) {
2752 vendor = (int) strtol(p + 7, &q, 10);
2753 if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2754 fr_strerror_printf("Invalid vendor value in attribute name \"%s\"", name);
2761 /* must be vendor name */
2768 fr_strerror_printf("Invalid vendor name in attribute name \"%s\"", name);
2772 if ((size_t) (q - p) >= sizeof(buffer)) {
2773 fr_strerror_printf("Vendor name too long in attribute name \"%s\"", name);
2778 memcpy(buffer, p, (q - p));
2779 buffer[q - p] = '\0';
2781 vendor = dict_vendorbyname(buffer);
2783 fr_strerror_printf("Unknown name \"%s\"", name);
2792 fr_strerror_printf("Invalid text following vendor definition in attribute name \"%s\"", name);
2802 if (strncasecmp(p, "Attr-", 5) != 0) {
2803 fr_strerror_printf("Unknown attribute \"%s\"", name);
2808 attr = strtol(p + 5, &q, 10);
2814 fr_strerror_printf("Invalid value in attribute name \"%s\"", name);
2823 * VendorName-Attr-%d
2827 * Anything else is invalid.
2829 if (((vendor != 0) && (*p != '\0')) ||
2830 ((vendor == 0) && *p && (*p != '.'))) {
2832 fr_strerror_printf("Invalid OID");
2837 * Look for OIDs. Require the "Attr-26.Vendor-Id.type"
2838 * format, and disallow "Vendor-%d-Attr-%d" and
2839 * "VendorName-Attr-%d"
2841 * This section parses the Vendor-Id portion of
2842 * Attr-%d.%d. where the first number is 26, *or* an
2843 * extended name of the "evs" foundta type.
2846 found = dict_attrbyvalue(attr, 0);
2848 fr_strerror_printf("Cannot parse names without dictionaries");
2853 if ((attr != PW_VENDOR_SPECIFIC) &&
2854 !(found->flags.extended || found->flags.long_extended)) {
2855 fr_strerror_printf("Standard attributes cannot use OIDs");
2860 if ((attr == PW_VENDOR_SPECIFIC) || found->flags.evs) {
2861 vendor = strtol(p + 1, &q, 10);
2862 if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2863 fr_strerror_printf("Invalid vendor");
2868 if (*q != '.') goto invalid;
2872 if (found->flags.evs) vendor |= attr * FR_MAX_VENDOR;
2874 } /* else the second number is a TLV number */
2878 * Get the expected maximum size of the name.
2881 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
2884 if (dv_type > 3) dv_type = 3; /* hack */
2889 * Parse the next number. It could be a Vendor-Type
2890 * of 1..2^24, or it could be a TLV.
2893 attr = strtol(p + 1, &q, 10);
2895 fr_strerror_printf("Invalid name number");
2913 * Enforce a maximum value on the attribute number.
2915 if (attr >= (unsigned) (1 << (dv_type << 3))) goto invalid;
2918 if (dict_str2oid(p + 1, &attr, &vendor, 1) < 0) {
2924 * If the caller doesn't provide a DICT_ATTR
2925 * we can't call dict_unknown_from_fields.
2928 fr_strerror_printf("Unknown attributes disallowed");
2932 return dict_unknown_from_fields(da, attr, vendor);
2935 /** Create a DICT_ATTR from an ASCII attribute and value
2937 * Where the attribute name is in the form:
2939 * - Attr-%d.%d.%d...
2940 * - Vendor-%d-Attr-%d
2941 * - VendorName-Attr-%d
2943 * @todo should check attr/vendor against dictionary and return the real da.
2945 * @param[in] ctx to alloc new attribute in.
2946 * @param[in] name of attribute.
2947 * @return 0 on success -1 on failure.
2949 DICT_ATTR const *dict_unknown_afrom_str(TALLOC_CTX *ctx, char const *name)
2954 p = talloc_zero_array(ctx, uint8_t, DICT_ATTR_SIZE);
2956 fr_strerror_printf("Out of memory");
2959 da = (DICT_ATTR *) p;
2960 talloc_set_type(da, DICT_ATTR);
2962 if (dict_unknown_from_str(da, name) < 0) {
2970 /** Create a dictionary attribute by name embedded in another string
2972 * Find the first invalid attribute name char in the string pointed
2975 * Copy the characters between the start of the name string and the first
2976 * none dict_attr_allowed_char to a buffer and initialise da as an
2977 * unknown attribute.
2979 * @param[out] da to initialise.
2980 * @param[in,out] name string start.
2981 * @return 0 on success or -1 on error;
2983 int dict_unknown_from_substr(DICT_ATTR *da, char const **name)
2987 char buffer[DICT_ATTR_MAX_NAME_LEN + 1];
2989 if (!name || !*name) return -1;
2992 * Advance p until we get something that's not part of
2993 * the dictionary attribute name.
2995 for (p = *name; dict_attr_allowed_chars[(int) *p] || (*p == '.' ) || (*p == '-'); p++);
2998 if (len > DICT_ATTR_MAX_NAME_LEN) {
2999 fr_strerror_printf("Attribute name too long");
3004 fr_strerror_printf("Invalid attribute name");
3007 strlcpy(buffer, *name, len + 1);
3009 if (dict_unknown_from_str(da, buffer) < 0) return -1;
3017 * Get an attribute by its numerical value.
3019 DICT_ATTR const *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
3023 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
3028 return fr_hash_table_finddata(attributes_byvalue, &da);
3032 /** Get an attribute by its numerical value and data type
3034 * Used only for COMBO_IP
3036 * @return The attribute, or NULL if not found
3038 DICT_ATTR const *dict_attrbytype(unsigned int attr, unsigned int vendor,
3047 return fr_hash_table_finddata(attributes_combo, &da);
3050 /** Using a parent and attr/vendor, find a child attr/vendor
3053 int dict_attr_child(DICT_ATTR const *parent,
3054 unsigned int *pattr, unsigned int *pvendor)
3056 unsigned int attr, vendor;
3059 if (!parent || !pattr || !pvendor) return false;
3065 * Only some types can have children
3067 switch (parent->type) {
3068 default: return false;
3073 case PW_TYPE_EXTENDED:
3074 case PW_TYPE_LONG_EXTENDED:
3078 if ((vendor == 0) && (parent->vendor != 0)) return false;
3081 * Bootstrap by starting off with the parents values.
3083 da.attr = parent->attr;
3084 da.vendor = parent->vendor;
3087 * Do various butchery to insert the "attr" value.
3089 * 00VID 000000AA normal VSA for vendor VID
3090 * 00VID DDCCBBAA normal VSAs with TLVs
3091 * EE000 000000AA extended attr (241.1)
3092 * EE000 DDCCBBAA extended attr with TLVs
3093 * EEVID 000000AA EVS with vendor VID, attr AAA
3094 * EEVID DDCCBBAA EVS with TLVs
3097 da.vendor = parent->attr * FR_MAX_VENDOR;
3098 da.vendor |= vendor;
3105 * Trying to nest too deep. It's an error
3107 if (parent->attr & (fr_attr_mask[MAX_TLV_NEST] << fr_attr_shift[MAX_TLV_NEST])) {
3111 for (i = MAX_TLV_NEST - 1; i >= 0; i--) {
3112 if ((parent->attr & (fr_attr_mask[i] << fr_attr_shift[i]))) {
3113 da.attr |= (attr & fr_attr_mask[i + 1]) << fr_attr_shift[i + 1];
3123 fprintf(stderr, "LOOKING FOR %08x %08x + %08x %08x --> %08x %08x\n",
3124 parent->vendor, parent->attr, attr, vendor,
3125 da.vendor, da.attr);
3129 *pvendor = da.vendor;
3134 * Get an attribute by it's numerical value, and the parent
3136 DICT_ATTR const *dict_attrbyparent(DICT_ATTR const *parent, unsigned int attr, unsigned int vendor)
3138 unsigned int my_attr, my_vendor;
3144 if (!dict_attr_child(parent, &my_attr, &my_vendor)) return NULL;
3147 da.vendor = my_vendor;
3149 return fr_hash_table_finddata(attributes_byvalue, &da);
3154 * Get an attribute by its name.
3156 DICT_ATTR const *dict_attrbyname(char const *name)
3159 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
3161 if (!name) return NULL;
3163 da = (DICT_ATTR *) buffer;
3164 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
3166 return fr_hash_table_finddata(attributes_byname, da);
3169 /** Look up a dictionary attribute by name embedded in another string
3171 * Find the first invalid attribute name char in the string pointed
3174 * Copy the characters between the start of the name string and the first
3175 * none dict_attr_allowed_char to a buffer and perform a dictionary lookup
3178 * If the attribute exists, advance the pointer pointed to by name
3179 * to the first none dict_attr_allowed_char char, and return the DA.
3181 * If the attribute does not exist, don't advance the pointer and return
3184 * @param[in,out] name string start.
3185 * @return NULL if no attributes matching the name could be found, else
3187 DICT_ATTR const *dict_attrbyname_substr(char const **name)
3190 DICT_ATTR const *da;
3193 uint32_t buffer[(sizeof(*find) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
3195 if (!name || !*name) return NULL;
3197 find = (DICT_ATTR *) buffer;
3200 * Advance p until we get something that's not part of
3201 * the dictionary attribute name.
3203 for (p = *name; dict_attr_allowed_chars[(int) *p]; p++);
3206 if (len > DICT_ATTR_MAX_NAME_LEN) {
3207 fr_strerror_printf("Attribute name too long");
3211 strlcpy(find->name, *name, len + 1);
3213 da = fr_hash_table_finddata(attributes_byname, find);
3215 fr_strerror_printf("Unknown attribute \"%s\"", find->name);
3224 * Associate a value with an attribute and return it.
3226 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
3228 DICT_VALUE dval, *dv;
3231 * First, look up aliases.
3234 dval.vendor = vendor;
3235 dval.name[0] = '\0';
3238 * Look up the attribute alias target, and use
3239 * the correct attribute number if found.
3241 dv = fr_hash_table_finddata(values_byname, &dval);
3242 if (dv) dval.attr = dv->value;
3246 return fr_hash_table_finddata(values_byvalue, &dval);
3250 * Associate a value with an attribute and return it.
3252 char const *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value)
3256 dv = dict_valbyattr(attr, vendor, value);
3263 * Get a value by its name, keyed off of an attribute.
3265 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, char const *name)
3267 DICT_VALUE *my_dv, *dv;
3268 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
3270 if (!name) return NULL;
3272 my_dv = (DICT_VALUE *) buffer;
3274 my_dv->vendor = vendor;
3275 my_dv->name[0] = '\0';
3278 * Look up the attribute alias target, and use
3279 * the correct attribute number if found.
3281 dv = fr_hash_table_finddata(values_byname, my_dv);
3282 if (dv) my_dv->attr = dv->value;
3284 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
3286 return fr_hash_table_finddata(values_byname, my_dv);
3290 * Get the vendor PEC based on the vendor name
3292 * This is efficient only for small numbers of vendors.
3294 int dict_vendorbyname(char const *name)
3297 size_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + sizeof(size_t) - 1) / sizeof(size_t)];
3299 if (!name) return 0;
3301 dv = (DICT_VENDOR *) buffer;
3302 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
3304 dv = fr_hash_table_finddata(vendors_byname, dv);
3307 return dv->vendorpec;
3311 * Return the vendor struct based on the PEC.
3313 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
3317 dv.vendorpec = vendorpec;
3319 return fr_hash_table_finddata(vendors_byvalue, &dv);
3322 /** Converts an unknown to a known by adding it to the internal dictionaries.
3324 * Does not free old DICT_ATTR, that is left up to the caller.
3326 * @param old unknown attribute to add.
3327 * @return existing DICT_ATTR if old was found in a dictionary, else the new entry in the dictionary
3330 DICT_ATTR const *dict_unknown_add(DICT_ATTR const *old)
3332 DICT_ATTR const *da, *parent;
3335 if (!old) return NULL;
3337 if (!old->flags.is_unknown) return old;
3339 da = dict_attrbyvalue(old->attr, old->vendor);
3342 memcpy(&flags, &old->flags, sizeof(flags));
3343 flags.is_unknown = false;
3345 parent = dict_parent(old->attr, old->vendor);
3347 if (parent->flags.has_tlv) flags.is_tlv = true;
3348 flags.evs = parent->flags.evs;
3349 flags.extended = parent->flags.extended;
3350 flags.long_extended = parent->flags.long_extended;
3353 if (dict_addattr(old->name, old->attr, old->vendor, old->type, flags) < 0) {
3357 da = dict_attrbyvalue(old->attr, old->vendor);