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
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/libradius.h>
29 #include <freeradius-devel/dhcp.h>
38 #ifdef HAVE_SYS_STAT_H
43 #define DICT_VALUE_MAX_NAME_LEN (128)
44 #define DICT_VENDOR_MAX_NAME_LEN (128)
45 #define DICT_ATTR_MAX_NAME_LEN (128)
47 static fr_hash_table_t *vendors_byname = NULL;
48 static fr_hash_table_t *vendors_byvalue = NULL;
50 static fr_hash_table_t *attributes_byname = NULL;
51 static fr_hash_table_t *attributes_byvalue = NULL;
53 static fr_hash_table_t *values_byvalue = NULL;
54 static fr_hash_table_t *values_byname = NULL;
56 static DICT_ATTR *dict_base_attrs[256];
59 * For faster HUP's, we cache the stat information for
60 * files we've $INCLUDEd
62 typedef struct dict_stat_t {
63 struct dict_stat_t *next;
68 static char *stat_root_dir = NULL;
69 static char *stat_root_file = NULL;
71 static dict_stat_t *stat_head = NULL;
72 static dict_stat_t *stat_tail = NULL;
74 typedef struct value_fixup_t {
75 char attrstr[DICT_ATTR_MAX_NAME_LEN];
77 struct value_fixup_t *next;
82 * So VALUEs in the dictionary can have forward references.
84 static value_fixup_t *value_fixup = NULL;
86 const FR_NAME_NUMBER dict_attr_types[] = {
87 { "integer", PW_TYPE_INTEGER },
88 { "string", PW_TYPE_STRING },
89 { "ipaddr", PW_TYPE_IPADDR },
90 { "date", PW_TYPE_DATE },
91 { "abinary", PW_TYPE_ABINARY },
92 { "octets", PW_TYPE_OCTETS },
93 { "ifid", PW_TYPE_IFID },
94 { "ipv6addr", PW_TYPE_IPV6ADDR },
95 { "ipv6prefix", PW_TYPE_IPV6PREFIX },
96 { "byte", PW_TYPE_BYTE },
97 { "short", PW_TYPE_SHORT },
98 { "ether", PW_TYPE_ETHERNET },
99 { "combo-ip", PW_TYPE_COMBO_IP },
100 { "tlv", PW_TYPE_TLV },
101 { "signed", PW_TYPE_SIGNED },
102 { "extended", PW_TYPE_EXTENDED },
103 { "long-extended", PW_TYPE_LONG_EXTENDED },
104 { "evs", PW_TYPE_EVS },
105 { "uint8", PW_TYPE_BYTE },
106 { "uint16", PW_TYPE_SHORT },
107 { "uint32", PW_TYPE_INTEGER },
108 { "int32", PW_TYPE_SIGNED },
109 { "integer64", PW_TYPE_INTEGER64 },
110 { "uint64", PW_TYPE_INTEGER64 },
111 { "ipv4prefix", PW_TYPE_IPV4PREFIX },
119 #define MAX_TLV_NEST (4)
123 * 8 bits for nested TLV 1
124 * 8 bits for nested TLV 2
125 * 5 bits for nested TLV 3
126 * 3 bits for nested TLV 4
128 const int fr_attr_max_tlv = MAX_TLV_NEST;
129 const int fr_attr_shift[MAX_TLV_NEST + 1] = {
133 const int fr_attr_mask[MAX_TLV_NEST + 1] = {
134 0xff, 0xff, 0xff, 0x1f, 0x07
139 * Create the hash of the name.
141 * We copy the hash function here because it's substantially faster.
143 #define FNV_MAGIC_INIT (0x811c9dc5)
144 #define FNV_MAGIC_PRIME (0x01000193)
146 static uint32_t dict_hashname(const char *name)
148 uint32_t hash = FNV_MAGIC_INIT;
151 for (p = name; *p != '\0'; p++) {
152 int c = *(const unsigned char *) p;
153 if (isalpha(c)) c = tolower(c);
155 hash *= FNV_MAGIC_PRIME;
156 hash ^= (uint32_t ) (c & 0xff);
164 * Hash callback functions.
166 static uint32_t dict_attr_name_hash(const void *data)
168 return dict_hashname(((const DICT_ATTR *)data)->name);
171 static int dict_attr_name_cmp(const void *one, const void *two)
173 const DICT_ATTR *a = one;
174 const DICT_ATTR *b = two;
176 return strcasecmp(a->name, b->name);
179 static uint32_t dict_attr_value_hash(const void *data)
182 const DICT_ATTR *attr = data;
184 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
185 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
188 static int dict_attr_value_cmp(const void *one, const void *two)
190 const DICT_ATTR *a = one;
191 const DICT_ATTR *b = two;
193 if (a->vendor < b->vendor) return -1;
194 if (a->vendor > b->vendor) return +1;
196 return a->attr - b->attr;
199 static uint32_t dict_vendor_name_hash(const void *data)
201 return dict_hashname(((const DICT_VENDOR *)data)->name);
204 static int dict_vendor_name_cmp(const void *one, const void *two)
206 const DICT_VENDOR *a = one;
207 const DICT_VENDOR *b = two;
209 return strcasecmp(a->name, b->name);
212 static uint32_t dict_vendor_value_hash(const void *data)
214 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
215 sizeof(((const DICT_VENDOR *)data)->vendorpec));
218 static int dict_vendor_value_cmp(const void *one, const void *two)
220 const DICT_VENDOR *a = one;
221 const DICT_VENDOR *b = two;
223 return a->vendorpec - b->vendorpec;
226 static uint32_t dict_value_name_hash(const void *data)
229 const DICT_VALUE *dval = data;
231 hash = dict_hashname(dval->name);
232 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
233 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
236 static int dict_value_name_cmp(const void *one, const void *two)
239 const DICT_VALUE *a = one;
240 const DICT_VALUE *b = two;
242 rcode = a->attr - b->attr;
243 if (rcode != 0) return rcode;
245 rcode = a->vendor - b->vendor;
246 if (rcode != 0) return rcode;
248 return strcasecmp(a->name, b->name);
251 static uint32_t dict_value_value_hash(const void *data)
254 const DICT_VALUE *dval = data;
256 hash = fr_hash(&dval->attr, sizeof(dval->attr));
257 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
258 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
261 static int dict_value_value_cmp(const void *one, const void *two)
264 const DICT_VALUE *a = one;
265 const DICT_VALUE *b = two;
267 if (a->vendor < b->vendor) return -1;
268 if (a->vendor > b->vendor) return +1;
270 rcode = a->attr - b->attr;
271 if (rcode != 0) return rcode;
273 return a->value - b->value;
278 * Free the list of stat buffers
280 static void dict_stat_free(void)
282 dict_stat_t *this, *next;
285 stat_root_dir = NULL;
286 free(stat_root_file);
287 stat_root_file = NULL;
294 for (this = stat_head; this != NULL; this = next) {
300 stat_head = stat_tail = NULL;
305 * Add an entry to the list of stat buffers.
307 static void dict_stat_add(const char *name, const struct stat *stat_buf)
311 this = malloc(sizeof(*this));
313 memset(this, 0, sizeof(*this));
315 this->name = strdup(name);
316 this->mtime = stat_buf->st_mtime;
319 stat_head = stat_tail = this;
321 stat_tail->next = this;
328 * See if any dictionaries have changed. If not, don't
331 static int dict_stat_check(const char *root_dir, const char *root_file)
336 if (!stat_root_dir) return 0;
337 if (!stat_root_file) return 0;
339 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
340 if (strcmp(root_file, stat_root_file) != 0) return 0;
342 if (!stat_head) return 0; /* changed, reload */
344 for (this = stat_head; this != NULL; this = this->next) {
345 if (stat(this->name, &buf) < 0) return 0;
347 if (buf.st_mtime != this->mtime) return 0;
353 typedef struct fr_pool_t {
356 struct fr_pool_t *page_free;
357 struct fr_pool_t *page_next;
360 #define FR_POOL_SIZE (32768)
361 #define FR_ALLOC_ALIGN (8)
363 static fr_pool_t *dict_pool = NULL;
365 static fr_pool_t *fr_pool_create(void)
367 fr_pool_t *fp = malloc(FR_POOL_SIZE);
369 if (!fp) return NULL;
371 memset(fp, 0, FR_POOL_SIZE);
373 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
374 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
376 fp->page_next = NULL;
380 static void fr_pool_delete(fr_pool_t **pfp)
382 fr_pool_t *fp, *next;
384 if (!pfp || !*pfp) return;
386 for (fp = *pfp; fp != NULL; fp = next) {
387 next = fp->page_next;
388 fp->page_next = NULL;
395 static void *fr_pool_alloc(size_t size)
399 if (size == 0) return NULL;
401 if (size > 256) return NULL; /* shouldn't happen */
404 dict_pool = fr_pool_create();
405 if (!dict_pool) return NULL;
408 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
409 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
412 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
413 dict_pool->page_free->page_next = fr_pool_create();
414 if (!dict_pool->page_free->page_next) return NULL;
415 dict_pool->page_free = dict_pool->page_free->page_next;
418 ptr = dict_pool->page_free->free_ptr;
419 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
425 static void fr_pool_free(UNUSED void *ptr)
428 * Place-holder for later code.
433 * Free the dictionary_attributes and dictionary_values lists.
440 fr_hash_table_free(vendors_byname);
441 fr_hash_table_free(vendors_byvalue);
442 vendors_byname = NULL;
443 vendors_byvalue = NULL;
445 fr_hash_table_free(attributes_byname);
446 fr_hash_table_free(attributes_byvalue);
447 attributes_byname = NULL;
448 attributes_byvalue = NULL;
450 fr_hash_table_free(values_byname);
451 fr_hash_table_free(values_byvalue);
452 values_byname = NULL;
453 values_byvalue = NULL;
455 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
457 fr_pool_delete(&dict_pool);
464 * Add vendor to the list.
466 int dict_addvendor(const char *name, unsigned int value)
471 if (value > FR_MAX_VENDOR) {
472 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
476 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
477 fr_strerror_printf("dict_addvendor: vendor name too long");
481 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
482 fr_strerror_printf("dict_addvendor: out of memory");
486 strcpy(dv->name, name);
487 dv->vendorpec = value;
488 dv->type = dv->length = 1; /* defaults */
490 if (!fr_hash_table_insert(vendors_byname, dv)) {
493 old_dv = fr_hash_table_finddata(vendors_byname, dv);
495 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
498 if (old_dv->vendorpec != dv->vendorpec) {
499 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
504 * Already inserted. Discard the duplicate entry.
511 * Insert the SAME pointer (not free'd when this table is
512 * deleted), into another table.
514 * We want this behaviour because we want OLD names for
515 * the attributes to be read from the configuration
516 * files, but when we're printing them, (and looking up
517 * by value) we want to use the NEW name.
519 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
520 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
529 * Add an attribute to the dictionary.
531 int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
535 static int max_attr = 0;
539 namelen = strlen(name);
540 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
541 fr_strerror_printf("dict_addattr: attribute name too long");
545 for (p = name; *p != '\0'; p++) {
547 fr_strerror_printf("dict_addattr: attribute name cannot contain control characters");
551 if ((*p == '"') || (*p == '\\')) {
552 fr_strerror_printf("dict_addattr: attribute name cannot contain quotation or backslash");
556 if ((*p == '<') || (*p == '>') || (*p == '&')) {
557 fr_strerror_printf("dict_addattr: attribute name cannot contain XML control characters");
563 !((type == PW_TYPE_INTEGER) || (type == PW_TYPE_STRING))) {
564 fr_strerror_printf("dict_addattr: Only 'integer' and 'string' attributes can have tags");
570 * If the attr is '-1', that means use a pre-existing
571 * one (if it already exists). If one does NOT already exist,
572 * then create a new attribute, with a non-conflicting value,
576 if (dict_attrbyname(name)) {
577 return 0; /* exists, don't add it again */
582 } else if (vendor == 0) {
586 if (attr > max_attr) {
592 * Additional checks for extended attributes.
594 if (flags.extended || flags.long_extended || flags.evs) {
595 if (vendor && (vendor < FR_MAX_VENDOR)) {
596 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
599 if (!vendor) vendor = VENDORPEC_EXTENDED;
605 || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
606 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
612 if (!(flags.extended || flags.long_extended)) {
613 fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
617 if (vendor <= FR_MAX_VENDOR) {
618 fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
624 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
628 if (flags.has_tlv && flags.length) {
629 fr_strerror_printf("TLVs cannot have a fixed length");
633 if (vendor && (vendor != VENDORPEC_EXTENDED)) {
635 static DICT_VENDOR *last_vendor = NULL;
637 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
638 fr_strerror_printf("TLV's cannot be encrypted");
642 if (flags.is_tlv && flags.has_tag) {
643 fr_strerror_printf("Sub-TLV's cannot have a tag");
647 if (flags.has_tlv && flags.has_tag) {
648 fr_strerror_printf("TLV's cannot have a tag");
653 * Most ATTRIBUTEs are bunched together by
654 * VENDOR. We can save a lot of lookups on
655 * dictionary initialization by caching the last
659 ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
663 * Ignore the high byte (sigh)
665 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
670 * If the vendor isn't defined, die.
673 fr_strerror_printf("dict_addattr: Unknown vendor %u",
679 * FIXME: Switch over dv->type, and limit things
682 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
683 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
685 } /* else 256..65535 are allowed */
688 * Set the extended flags as appropriate.
690 if (vendor > FR_MAX_VENDOR) {
693 myattr = (vendor >> 24) & 0xff;
694 myattr |= PW_VENDOR_SPECIFIC << 8;
696 da = dict_attrbyvalue(myattr, VENDORPEC_EXTENDED);
698 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown \"evs\" type.");
701 flags.extended = da->flags.extended;
702 flags.long_extended = da->flags.long_extended;
703 flags.evs = da->flags.evs;
707 * <sigh> Alvarion, being *again* a horribly
708 * broken vendor, has re-used the WiMAX format in
709 * their proprietary vendor space. This re-use
710 * means that there are *multiple* conflicting
711 * Alvarion dictionaries.
713 flags.wimax = dv->flags;
717 * Create a new attribute for the list
719 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
720 fr_strerror_printf("dict_addattr: out of memory");
724 memcpy(da->name, name, namelen);
725 da->name[namelen] = '\0';
732 * Insert the attribute, only if it's not a duplicate.
734 if (!fr_hash_table_insert(attributes_byname, da)) {
738 * If the attribute has identical number, then
739 * ignore the duplicate.
741 a = fr_hash_table_finddata(attributes_byname, da);
742 if (a && (strcasecmp(a->name, da->name) == 0)) {
743 if (a->attr != da->attr) {
744 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
750 * Same name, same vendor, same attr,
751 * maybe the flags and/or type is
752 * different. Let the new value
753 * over-ride the old one.
758 fr_hash_table_delete(attributes_byvalue, a);
760 if (!fr_hash_table_replace(attributes_byname, da)) {
761 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
768 * Insert the SAME pointer (not free'd when this entry is
769 * deleted), into another table.
771 * We want this behaviour because we want OLD names for
772 * the attributes to be read from the configuration
773 * files, but when we're printing them, (and looking up
774 * by value) we want to use the NEW name.
776 if (!fr_hash_table_replace(attributes_byvalue, da)) {
777 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
781 if (!vendor && (attr > 0) && (attr < 256)) {
782 dict_base_attrs[attr] = da;
790 * Add a value for an attribute to the dictionary.
792 int dict_addvalue(const char *namestr, const char *attrstr, int value)
798 static DICT_ATTR *last_attr = NULL;
801 fr_strerror_printf("dict_addvalue: empty names are not permitted");
805 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
806 fr_strerror_printf("dict_addvalue: value name too long");
810 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
811 fr_strerror_printf("dict_addvalue: out of memory");
814 memset(dval, 0, sizeof(*dval));
816 strcpy(dval->name, namestr);
820 * Most VALUEs are bunched together by ATTRIBUTE. We can
821 * save a lot of lookups on dictionary initialization by
822 * caching the last attribute.
824 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
827 dattr = dict_attrbyname(attrstr);
832 * Remember which attribute is associated with this
833 * value, if possible.
836 if (dattr->flags.has_value_alias) {
837 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
841 dval->attr = dattr->attr;
842 dval->vendor = dattr->vendor;
845 * Enforce valid values
847 * Don't worry about fixups...
849 switch (dattr->type) {
853 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
860 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
866 * Allow octets for now, because
867 * of dictionary.cablelabs
871 case PW_TYPE_INTEGER:
874 case PW_TYPE_INTEGER64:
877 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
878 fr_int2str(dict_attr_types, dattr->type, "?Unknown?"));
882 dattr->flags.has_value = 1;
884 value_fixup_t *fixup;
886 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
889 fr_strerror_printf("dict_addvalue: out of memory");
892 memset(fixup, 0, sizeof(*fixup));
894 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
898 * Insert to the head of the list.
900 fixup->next = value_fixup;
907 * Add the value into the dictionary.
909 if (!fr_hash_table_insert(values_byname, dval)) {
914 * Suppress duplicates with the same
915 * name and value. There are lots in
918 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
919 if (old && (old->value == dval->value)) {
926 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
931 * There are multiple VALUE's, keyed by attribute, so we
932 * take care of that here.
934 if (!fr_hash_table_replace(values_byvalue, dval)) {
935 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
943 static int sscanf_i(const char *str, unsigned int *pvalue)
947 static const char *tab = "0123456789";
949 if ((str[0] == '0') &&
950 ((str[1] == 'x') || (str[1] == 'X'))) {
951 tab = "0123456789abcdef";
960 if (*str == '.') break;
962 c = memchr(tab, tolower((int) *str), base);
975 int dict_str2oid(const char *ptr, unsigned int *pvalue, int vendor, int tlv_depth)
981 if (tlv_depth > fr_attr_max_tlv) {
982 fr_strerror_printf("Attribute has too long OID");
988 da = dict_attrbyvalue(*pvalue, vendor);
990 da = dict_attrbyvalue(*pvalue, VENDORPEC_EXTENDED);
993 fr_strerror_printf("Parent attribute is undefined.");
997 if (!(da->flags.has_tlv || da->flags.extended || da->flags.long_extended)) {
998 fr_strerror_printf("Parent attribute %s cannot have sub-tlvs",
1004 p = strchr(ptr, '.');
1006 if (!sscanf_i(ptr, &value)) {
1007 fr_strerror_printf("Failed parsing attribute identifier %s",
1013 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
1019 return dict_str2oid(p + 1, pvalue, vendor, tlv_depth + 1);
1027 * Process the ATTRIBUTE command
1029 static int process_attribute(const char* fn, const int line,
1030 const unsigned int block_vendor, DICT_ATTR *block_tlv,
1031 int tlv_depth, char **argv, int argc)
1034 unsigned int vendor = 0;
1037 unsigned int length = 0;
1041 if ((argc < 3) || (argc > 4)) {
1042 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
1047 if (strncmp(argv[1], "Attr-", 5) == 0) {
1048 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1053 memset(&flags, 0, sizeof(flags));
1056 * Look for extended attributes before doing anything else.
1058 p = strchr(argv[1], '.');
1065 * Validate all entries
1067 if (!sscanf_i(argv[1], &value)) {
1068 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1073 if (value > (1 << 24)) {
1074 fr_strerror_printf("dict_init: %s[%d]: Attribute number is too large", fn, line);
1080 * Parse NUM.NUM.NUM.NUM
1086 *p = '.'; /* reset for later printing */
1089 da = dict_attrbyvalue(value, block_vendor);
1090 } else if (value == PW_VENDOR_SPECIFIC) {
1092 const DICT_VENDOR *dv;
1098 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier", fn, line);
1103 if (!sscanf_i(p, &vendor)) {
1109 dv = dict_vendorbyvalue(vendor);
1111 fr_strerror_printf("dict_init: %s[%d]: Unknown vendor \"%u\" in attribute identifier", fn, line, vendor);
1116 return process_attribute(fn, line, vendor, NULL, 0,
1119 da = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
1122 fr_strerror_printf("dict_init: %s[%d]: Entry refers to unknown attribute %d", fn, line, value);
1127 * 241.1 means 241 is of type "extended".
1130 if (!(da->flags.has_tlv || da->flags.extended || da->flags.long_extended)) {
1131 fr_strerror_printf("dict_init: %s[%d]: Parent attribute %s cannot contain sub-attributes", fn, line, da->name);
1135 my_depth = dict_str2oid(p + 1, &value, block_vendor, tlv_depth + 1);
1139 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1141 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1146 * Look up the REAL parent TLV.
1149 unsigned int parent = value;
1151 parent &= ~(fr_attr_mask[my_depth] << fr_attr_shift[my_depth]);
1153 da = dict_attrbyvalue(parent, da->vendor);
1155 fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
1161 * Set which type of attribute this is.
1163 flags.extended = da->flags.extended;
1164 flags.long_extended = da->flags.long_extended;
1165 flags.evs = da->flags.evs;
1166 if (da->flags.has_tlv) flags.is_tlv = 1;
1169 if (strncmp(argv[2], "octets[", 7) != 0) {
1171 * find the type of the attribute.
1173 type = fr_str2int(dict_attr_types, argv[2], -1);
1175 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1181 type = PW_TYPE_OCTETS;
1183 p = strchr(argv[2] + 7, ']');
1185 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1191 if (!sscanf_i(argv[1], &length)) {
1192 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1196 if ((length == 0) || (length > 253)) {
1197 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1203 * Only look up the vendor if the string
1208 * Force "length" for data types of fixed length;
1220 case PW_TYPE_IPADDR:
1221 case PW_TYPE_INTEGER:
1222 case PW_TYPE_SIGNED:
1226 case PW_TYPE_INTEGER64:
1230 case PW_TYPE_ETHERNET:
1238 case PW_TYPE_IPV6ADDR:
1242 case PW_TYPE_EXTENDED:
1243 if ((vendor != 0) || (value < 241)) {
1244 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended\" MUST be RFC attributes with value >= 241.", fn, line);
1247 type = PW_TYPE_OCTETS;
1251 case PW_TYPE_LONG_EXTENDED:
1252 if ((vendor != 0) || (value < 241)) {
1253 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"long-extended\" MUST be RFC attributes with value >= 241.", fn, line);
1256 type = PW_TYPE_OCTETS;
1257 flags.long_extended = 1;
1261 type = PW_TYPE_OCTETS;
1263 if (((value >> fr_attr_shift[1]) & fr_attr_mask[1]) != PW_VENDOR_SPECIFIC) {
1264 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1273 flags.length = length;
1275 } else { /* argc == 4: we have options */
1276 char *key, *next, *last;
1281 if (flags.extended || flags.long_extended || flags.evs) {
1282 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1287 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1293 next = strchr(key, ',');
1294 if (next) *(next++) = '\0';
1296 if (strcmp(key, "has_tag") == 0 ||
1297 strcmp(key, "has_tag=1") == 0) {
1298 /* Boolean flag, means this is a
1302 } else if (strncmp(key, "encrypt=", 8) == 0) {
1303 /* Encryption method, defaults to 0 (none).
1304 Currently valid is just type 2,
1305 Tunnel-Password style, which can only
1306 be applied to strings. */
1307 flags.encrypt = strtol(key + 8, &last, 0);
1309 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1314 if ((flags.encrypt == FLAG_ENCRYPT_ASCEND_SECRET) &&
1315 (type != PW_TYPE_STRING)) {
1316 fr_strerror_printf( "dict_init: %s[%d] Only \"string\" types can have the \"encrypt=3\" flag set.",
1321 } else if (strncmp(key, "array", 6) == 0) {
1325 case PW_TYPE_IPADDR:
1328 case PW_TYPE_INTEGER:
1333 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1339 * The only thing is the vendor name,
1340 * and it's a known name: allow it.
1342 } else if ((key == argv[3]) && !next) {
1344 fr_strerror_printf( "dict_init: %s[%d] New-style attributes cannot use a vendor flag.",
1350 fr_strerror_printf( "dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" is not allowed.",
1355 vendor = dict_vendorbyname(key);
1356 if (!vendor) goto unknown;
1361 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1367 if (key && !*key) break;
1371 if (block_vendor) vendor = block_vendor;
1374 * Special checks for tags, they make our life much more
1377 if (flags.has_tag) {
1379 * Only string, octets, and integer can be tagged.
1382 case PW_TYPE_STRING:
1383 case PW_TYPE_INTEGER:
1387 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1389 fr_int2str(dict_attr_types, type, "?Unknown?"));
1394 if (type == PW_TYPE_TLV) {
1395 if (vendor && (vendor < FR_MAX_VENDOR)
1397 && (vendor != DHCP_MAGIC_VENDOR)
1402 dv = dict_vendorbyvalue(vendor);
1403 if (!dv || (dv->type != 1) || (dv->length != 1)) {
1404 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1415 * TLV's can be only one octet.
1417 if ((value == 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1418 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1426 value <<= fr_attr_shift[tlv_depth];
1427 value |= block_tlv->attr;
1431 #ifdef WITH_DICTIONARY_WARNINGS
1433 * Hack to help us discover which vendors have illegal
1436 if (!vendor && (value < 256) &&
1437 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1438 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1446 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1449 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1451 fr_strerror_printf("dict_init: %s[%d]: %s",
1461 * Process the VALUE command
1463 static int process_value(const char* fn, const int line, char **argv,
1469 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1474 * For Compatibility, skip "Server-Config"
1476 if (strcasecmp(argv[0], "Server-Config") == 0)
1480 * Validate all entries
1482 if (!sscanf_i(argv[2], &value)) {
1483 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1488 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1491 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1493 fr_strerror_printf("dict_init: %s[%d]: %s",
1503 * Process the VALUE-ALIAS command
1505 * This allows VALUE mappings to be shared among multiple
1508 static int process_value_alias(const char* fn, const int line, char **argv,
1511 DICT_ATTR *my_da, *da;
1515 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1520 my_da = dict_attrbyname(argv[0]);
1522 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1527 if (my_da->flags.has_value) {
1528 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1533 if (my_da->flags.has_value_alias) {
1534 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1539 da = dict_attrbyname(argv[1]);
1541 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1546 if (!da->flags.has_value) {
1547 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1552 if (da->flags.has_value_alias) {
1553 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1558 if (my_da->type != da->type) {
1559 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1564 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1565 fr_strerror_printf("dict_addvalue: out of memory");
1569 dval->name[0] = '\0'; /* empty name */
1570 dval->attr = my_da->attr;
1571 dval->vendor = my_da->vendor;
1572 dval->value = da->attr;
1574 if (!fr_hash_table_insert(values_byname, dval)) {
1575 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1586 * Process the VENDOR command
1588 static int process_vendor(const char* fn, const int line, char **argv,
1592 int continuation = 0;
1593 const char *format = NULL;
1595 if ((argc < 2) || (argc > 3)) {
1596 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1602 * Validate all entries
1604 if (!isdigit((int) argv[1][0])) {
1605 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1609 value = atoi(argv[1]);
1611 /* Create a new VENDOR entry for the list */
1612 if (dict_addvendor(argv[0], value) < 0) {
1615 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1617 fr_strerror_printf("dict_init: %s[%d]: %s",
1623 * Look for a format statement
1628 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1629 format = "format=4,0";
1631 } else if (value == VENDORPEC_LUCENT) {
1632 format = "format=2,1";
1634 } else if (value == VENDORPEC_STARENT) {
1635 format = "format=2,2";
1637 } /* else no fixups to do */
1644 if (strncasecmp(format, "format=", 7) != 0) {
1645 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1651 if ((strlen(p) < 3) ||
1652 !isdigit((int) p[0]) ||
1654 !isdigit((int) p[2]) ||
1655 (p[3] && (p[3] != ','))) {
1656 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1661 type = (int) (p[0] - '0');
1662 length = (int) (p[2] - '0');
1666 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1671 if ((p[4] != 'c') ||
1673 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1679 if ((value != VENDORPEC_WIMAX) ||
1680 (type != 1) || (length != 1)) {
1681 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1687 dv = dict_vendorbyvalue(value);
1689 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1694 if ((type != 1) && (type != 2) && (type != 4)) {
1695 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1700 if ((length != 0) && (length != 1) && (length != 2)) {
1701 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1707 dv->length = length;
1708 dv->flags = continuation;
1715 * String split routine. Splits an input string IN PLACE
1716 * into pieces, based on spaces.
1718 int str2argv(char *str, char **argv, int max_argc)
1723 if (argc >= max_argc) break;
1726 * Chop out comments early.
1733 while ((*str == ' ') ||
1736 (*str == '\n')) *(str++) = '\0';
1747 (*str != '\n')) str++;
1753 #define MAX_ARGV (16)
1756 * Initialize the dictionary.
1758 static int my_dict_init(const char *dir, const char *fn,
1759 const char *src_file, int src_line)
1766 unsigned int vendor;
1767 unsigned int block_vendor;
1768 struct stat statbuf;
1769 char *argv[MAX_ARGV];
1771 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1772 int which_block_tlv = 0;
1774 block_tlv[0] = NULL;
1775 block_tlv[1] = NULL;
1776 block_tlv[2] = NULL;
1777 block_tlv[3] = NULL;
1779 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1780 strlen(dir) >= sizeof(dirtmp) / 2) {
1781 fr_strerror_printf("dict_init: filename name too long");
1786 * First see if fn is relative to dir. If so, create
1787 * new filename. If not, remember the absolute dir.
1789 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1793 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1794 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1798 if ((fp = fopen(fn, "r")) == NULL) {
1800 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1801 fn, strerror(errno));
1803 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1804 src_file, src_line, fn, strerror(errno));
1809 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1810 if (!S_ISREG(statbuf.st_mode)) {
1812 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1818 * Globally writable dictionaries means that users can control
1819 * the server configuration with little difficulty.
1822 if ((statbuf.st_mode & S_IWOTH) != 0) {
1824 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1830 dict_stat_add(fn, &statbuf);
1833 * Seed the random pool with data.
1835 fr_rand_seed(&statbuf, sizeof(statbuf));
1839 while (fgets(buf, sizeof(buf), fp) != NULL) {
1841 if (buf[0] == '#' || buf[0] == 0 ||
1842 buf[0] == '\n' || buf[0] == '\r')
1846 * Comment characters should NOT be appearing anywhere but
1847 * as start of a comment;
1849 p = strchr(buf, '#');
1852 argc = str2argv(buf, argv, MAX_ARGV);
1853 if (argc == 0) continue;
1856 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1863 * Process VALUE lines.
1865 if (strcasecmp(argv[0], "VALUE") == 0) {
1866 if (process_value(fn, line,
1867 argv + 1, argc - 1) == -1) {
1875 * Perhaps this is an attribute.
1877 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1878 if (process_attribute(fn, line, block_vendor,
1879 block_tlv[which_block_tlv],
1881 argv + 1, argc - 1) == -1) {
1889 * See if we need to import another dictionary.
1891 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1892 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1900 * Optionally include a dictionary
1902 if (strcasecmp(argv[0], "$INCLUDE-") == 0) {
1903 int rcode = my_dict_init(dir, argv[1], fn, line);
1905 if (rcode == -2) continue;
1914 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1915 if (process_value_alias(fn, line,
1916 argv + 1, argc - 1) == -1) {
1924 * Process VENDOR lines.
1926 if (strcasecmp(argv[0], "VENDOR") == 0) {
1927 if (process_vendor(fn, line,
1928 argv + 1, argc - 1) == -1) {
1935 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1938 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1944 da = dict_attrbyname(argv[1]);
1947 "dict_init: %s[%d]: unknown attribute %s",
1953 if (da->type != PW_TYPE_TLV) {
1955 "dict_init: %s[%d]: attribute %s is not of type tlv",
1961 if (which_block_tlv >= MAX_TLV_NEST) {
1963 "dict_init: %s[%d]: TLVs are nested too deep",
1970 block_tlv[++which_block_tlv] = da;
1974 if (strcasecmp(argv[0], "END-TLV") == 0) {
1977 "dict_init: %s[%d] invalid END-TLV entry",
1983 da = dict_attrbyname(argv[1]);
1986 "dict_init: %s[%d]: unknown attribute %s",
1992 if (da != block_tlv[which_block_tlv]) {
1994 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1999 block_tlv[which_block_tlv--] = NULL;
2003 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
2006 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
2012 vendor = dict_vendorbyname(argv[1]);
2015 "dict_init: %s[%d]: unknown vendor %s",
2021 block_vendor = vendor;
2024 * Check for extended attr VSAs
2027 if (strncmp(argv[2], "format=", 7) != 0) {
2029 "dict_init: %s[%d]: Invalid format %s",
2036 da = dict_attrbyname(p);
2038 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
2044 if (!da->flags.evs) {
2045 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
2052 * Pack the encapsulating attribute
2053 * into the vendor Id. This attribute
2056 block_vendor |= (da->attr & fr_attr_mask[1]) * FR_MAX_VENDOR;
2060 } /* BEGIN-VENDOR */
2062 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
2065 "dict_init: %s[%d] invalid END-VENDOR entry",
2071 vendor = dict_vendorbyname(argv[1]);
2074 "dict_init: %s[%d]: unknown vendor %s",
2080 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2082 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2092 * Any other string: We don't recognize it.
2094 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2105 * Empty callback for hash table initialization.
2107 static int null_callback(void *ctx, void *data)
2109 ctx = ctx; /* -Wunused */
2110 data = data; /* -Wunused */
2117 * Initialize the directory, then fix the attr member of
2120 int dict_init(const char *dir, const char *fn)
2123 * Check if we need to change anything. If not, don't do
2126 if (dict_stat_check(dir, fn)) {
2131 * Free the dictionaries, and the stat cache.
2134 stat_root_dir = strdup(dir);
2135 stat_root_file = strdup(fn);
2138 * Create the table of vendor by name. There MAY NOT
2139 * be multiple vendors of the same name.
2141 * Each vendor is malloc'd, so the free function is free.
2143 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2144 dict_vendor_name_cmp,
2146 if (!vendors_byname) {
2151 * Create the table of vendors by value. There MAY
2152 * be vendors of the same value. If there are, we
2153 * pick the latest one.
2155 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2156 dict_vendor_value_cmp,
2158 if (!vendors_byvalue) {
2163 * Create the table of attributes by name. There MAY NOT
2164 * be multiple attributes of the same name.
2166 * Each attribute is malloc'd, so the free function is free.
2168 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2171 if (!attributes_byname) {
2176 * Create the table of attributes by value. There MAY
2177 * be attributes of the same value. If there are, we
2178 * pick the latest one.
2180 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2181 dict_attr_value_cmp,
2183 if (!attributes_byvalue) {
2187 values_byname = fr_hash_table_create(dict_value_name_hash,
2188 dict_value_name_cmp,
2190 if (!values_byname) {
2194 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2195 dict_value_value_cmp,
2197 if (!values_byvalue) {
2201 value_fixup = NULL; /* just to be safe. */
2203 if (my_dict_init(dir, fn, NULL, 0) < 0)
2208 value_fixup_t *this, *next;
2210 for (this = value_fixup; this != NULL; this = next) {
2213 a = dict_attrbyname(this->attrstr);
2216 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2217 this->attrstr, this->dval->name);
2218 return -1; /* leak, but they should die... */
2221 this->dval->attr = a->attr;
2224 * Add the value into the dictionary.
2226 if (!fr_hash_table_replace(values_byname,
2228 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2233 * Allow them to use the old name, but
2234 * prefer the new name when printing
2237 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2238 fr_hash_table_replace(values_byvalue,
2244 * Just so we don't lose track of things.
2251 * Walk over all of the hash tables to ensure they're
2252 * initialized. We do this because the threads may perform
2253 * lookups, and we don't want multi-threaded re-ordering
2254 * of the table entries. That would be bad.
2256 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2257 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2259 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2260 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2262 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2263 fr_hash_table_walk(values_byname, null_callback, NULL);
2269 * Get an attribute by its numerical value.
2271 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2275 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2278 dattr.vendor = vendor;
2280 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2284 * Get an attribute by its name.
2286 DICT_ATTR *dict_attrbyname(const char *name)
2289 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2291 if (!name) return NULL;
2293 da = (DICT_ATTR *) buffer;
2294 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2296 return fr_hash_table_finddata(attributes_byname, da);
2300 * Associate a value with an attribute and return it.
2302 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2304 DICT_VALUE dval, *dv;
2307 * First, look up aliases.
2310 dval.vendor = vendor;
2311 dval.name[0] = '\0';
2314 * Look up the attribute alias target, and use
2315 * the correct attribute number if found.
2317 dv = fr_hash_table_finddata(values_byname, &dval);
2318 if (dv) dval.attr = dv->value;
2322 return fr_hash_table_finddata(values_byvalue, &dval);
2326 * Associate a value with an attribute and return it.
2328 const char *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value)
2332 dv = dict_valbyattr(attr, vendor, value);
2339 * Get a value by its name, keyed off of an attribute.
2341 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2343 DICT_VALUE *my_dv, *dv;
2344 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2346 if (!name) return NULL;
2348 my_dv = (DICT_VALUE *) buffer;
2350 my_dv->vendor = vendor;
2351 my_dv->name[0] = '\0';
2354 * Look up the attribute alias target, and use
2355 * the correct attribute number if found.
2357 dv = fr_hash_table_finddata(values_byname, my_dv);
2358 if (dv) my_dv->attr = dv->value;
2360 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2362 return fr_hash_table_finddata(values_byname, my_dv);
2366 * Get the vendor PEC based on the vendor name
2368 * This is efficient only for small numbers of vendors.
2370 int dict_vendorbyname(const char *name)
2373 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
2375 if (!name) return 0;
2377 dv = (DICT_VENDOR *) buffer;
2378 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
2380 dv = fr_hash_table_finddata(vendors_byname, dv);
2383 return dv->vendorpec;
2387 * Return the vendor struct based on the PEC.
2389 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
2393 dv.vendorpec = vendorpec;
2395 return fr_hash_table_finddata(vendors_byvalue, &dv);