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 { "extended-flags", PW_TYPE_EXTENDED_FLAGS },
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 },
118 #define MAX_TLV_NEST (4)
122 * 8 bits for nested TLV 1
123 * 8 bits for nested TLV 2
124 * 5 bits for nested TLV 3
125 * 3 bits for nested TLV 4
127 const int fr_attr_max_tlv = MAX_TLV_NEST;
128 const int fr_attr_shift[MAX_TLV_NEST + 1] = {
132 const int fr_attr_mask[MAX_TLV_NEST + 1] = {
133 0xff, 0xff, 0xff, 0x1f, 0x07
138 * Create the hash of the name.
140 * We copy the hash function here because it's substantially faster.
142 #define FNV_MAGIC_INIT (0x811c9dc5)
143 #define FNV_MAGIC_PRIME (0x01000193)
145 static uint32_t dict_hashname(const char *name)
147 uint32_t hash = FNV_MAGIC_INIT;
150 for (p = name; *p != '\0'; p++) {
151 int c = *(const unsigned char *) p;
152 if (isalpha(c)) c = tolower(c);
154 hash *= FNV_MAGIC_PRIME;
155 hash ^= (uint32_t ) (c & 0xff);
163 * Hash callback functions.
165 static uint32_t dict_attr_name_hash(const void *data)
167 return dict_hashname(((const DICT_ATTR *)data)->name);
170 static int dict_attr_name_cmp(const void *one, const void *two)
172 const DICT_ATTR *a = one;
173 const DICT_ATTR *b = two;
175 return strcasecmp(a->name, b->name);
178 static uint32_t dict_attr_value_hash(const void *data)
181 const DICT_ATTR *attr = data;
183 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
184 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
187 static int dict_attr_value_cmp(const void *one, const void *two)
189 const DICT_ATTR *a = one;
190 const DICT_ATTR *b = two;
192 if (a->vendor < b->vendor) return -1;
193 if (a->vendor > b->vendor) return +1;
195 return a->attr - b->attr;
198 static uint32_t dict_vendor_name_hash(const void *data)
200 return dict_hashname(((const DICT_VENDOR *)data)->name);
203 static int dict_vendor_name_cmp(const void *one, const void *two)
205 const DICT_VENDOR *a = one;
206 const DICT_VENDOR *b = two;
208 return strcasecmp(a->name, b->name);
211 static uint32_t dict_vendor_value_hash(const void *data)
213 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
214 sizeof(((const DICT_VENDOR *)data)->vendorpec));
217 static int dict_vendor_value_cmp(const void *one, const void *two)
219 const DICT_VENDOR *a = one;
220 const DICT_VENDOR *b = two;
222 return a->vendorpec - b->vendorpec;
225 static uint32_t dict_value_name_hash(const void *data)
228 const DICT_VALUE *dval = data;
230 hash = dict_hashname(dval->name);
231 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
232 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
235 static int dict_value_name_cmp(const void *one, const void *two)
238 const DICT_VALUE *a = one;
239 const DICT_VALUE *b = two;
241 rcode = a->attr - b->attr;
242 if (rcode != 0) return rcode;
244 rcode = a->vendor - b->vendor;
245 if (rcode != 0) return rcode;
247 return strcasecmp(a->name, b->name);
250 static uint32_t dict_value_value_hash(const void *data)
253 const DICT_VALUE *dval = data;
255 hash = fr_hash(&dval->attr, sizeof(dval->attr));
256 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
257 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
260 static int dict_value_value_cmp(const void *one, const void *two)
263 const DICT_VALUE *a = one;
264 const DICT_VALUE *b = two;
266 if (a->vendor < b->vendor) return -1;
267 if (a->vendor > b->vendor) return +1;
269 rcode = a->attr - b->attr;
270 if (rcode != 0) return rcode;
272 return a->value - b->value;
277 * Free the list of stat buffers
279 static void dict_stat_free(void)
281 dict_stat_t *this, *next;
284 stat_root_dir = NULL;
285 free(stat_root_file);
286 stat_root_file = NULL;
293 for (this = stat_head; this != NULL; this = next) {
299 stat_head = stat_tail = NULL;
304 * Add an entry to the list of stat buffers.
306 static void dict_stat_add(const char *name, const struct stat *stat_buf)
310 this = malloc(sizeof(*this));
312 memset(this, 0, sizeof(*this));
314 this->name = strdup(name);
315 this->mtime = stat_buf->st_mtime;
318 stat_head = stat_tail = this;
320 stat_tail->next = this;
327 * See if any dictionaries have changed. If not, don't
330 static int dict_stat_check(const char *root_dir, const char *root_file)
335 if (!stat_root_dir) return 0;
336 if (!stat_root_file) return 0;
338 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
339 if (strcmp(root_file, stat_root_file) != 0) return 0;
341 if (!stat_head) return 0; /* changed, reload */
343 for (this = stat_head; this != NULL; this = this->next) {
344 if (stat(this->name, &buf) < 0) return 0;
346 if (buf.st_mtime != this->mtime) return 0;
352 typedef struct fr_pool_t {
355 struct fr_pool_t *page_free;
356 struct fr_pool_t *page_next;
359 #define FR_POOL_SIZE (32768)
360 #define FR_ALLOC_ALIGN (8)
362 static fr_pool_t *dict_pool = NULL;
364 static fr_pool_t *fr_pool_create(void)
366 fr_pool_t *fp = malloc(FR_POOL_SIZE);
368 if (!fp) return NULL;
370 memset(fp, 0, FR_POOL_SIZE);
372 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
373 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
375 fp->page_next = NULL;
379 static void fr_pool_delete(fr_pool_t **pfp)
381 fr_pool_t *fp, *next;
383 if (!pfp || !*pfp) return;
385 for (fp = *pfp; fp != NULL; fp = next) {
386 next = fp->page_next;
387 fp->page_next = NULL;
394 static void *fr_pool_alloc(size_t size)
398 if (size == 0) return NULL;
400 if (size > 256) return NULL; /* shouldn't happen */
403 dict_pool = fr_pool_create();
404 if (!dict_pool) return NULL;
407 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
408 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
411 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
412 dict_pool->page_free->page_next = fr_pool_create();
413 if (!dict_pool->page_free->page_next) return NULL;
414 dict_pool->page_free = dict_pool->page_free->page_next;
417 ptr = dict_pool->page_free->free_ptr;
418 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
424 static void fr_pool_free(UNUSED void *ptr)
427 * Place-holder for later code.
432 * Free the dictionary_attributes and dictionary_values lists.
439 fr_hash_table_free(vendors_byname);
440 fr_hash_table_free(vendors_byvalue);
441 vendors_byname = NULL;
442 vendors_byvalue = NULL;
444 fr_hash_table_free(attributes_byname);
445 fr_hash_table_free(attributes_byvalue);
446 attributes_byname = NULL;
447 attributes_byvalue = NULL;
449 fr_hash_table_free(values_byname);
450 fr_hash_table_free(values_byvalue);
451 values_byname = NULL;
452 values_byvalue = NULL;
454 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
456 fr_pool_delete(&dict_pool);
463 * Add vendor to the list.
465 int dict_addvendor(const char *name, unsigned int value)
470 if (value > FR_MAX_VENDOR) {
471 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
475 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
476 fr_strerror_printf("dict_addvendor: vendor name too long");
480 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
481 fr_strerror_printf("dict_addvendor: out of memory");
485 strcpy(dv->name, name);
486 dv->vendorpec = value;
487 dv->type = dv->length = 1; /* defaults */
489 if (!fr_hash_table_insert(vendors_byname, dv)) {
492 old_dv = fr_hash_table_finddata(vendors_byname, dv);
494 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
497 if (old_dv->vendorpec != dv->vendorpec) {
498 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
503 * Already inserted. Discard the duplicate entry.
510 * Insert the SAME pointer (not free'd when this table is
511 * deleted), into another table.
513 * We want this behaviour because we want OLD names for
514 * the attributes to be read from the configuration
515 * files, but when we're printing them, (and looking up
516 * by value) we want to use the NEW name.
518 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
519 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
528 * Add an attribute to the dictionary.
530 int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
534 static int max_attr = 0;
538 namelen = strlen(name);
539 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
540 fr_strerror_printf("dict_addattr: attribute name too long");
544 for (p = name; *p != '\0'; p++) {
546 fr_strerror_printf("dict_addattr: attribute name cannot contain control characters");
550 if ((*p == '"') || (*p == '\\')) {
551 fr_strerror_printf("dict_addattr: attribute name cannot contain quotation or backslash");
555 if ((*p == '<') || (*p == '>') || (*p == '&')) {
556 fr_strerror_printf("dict_addattr: attribute name cannot contain XML control characters");
562 !((type == PW_TYPE_INTEGER) || (type == PW_TYPE_STRING))) {
563 fr_strerror_printf("dict_addattr: Only 'integer' and 'string' attributes can have tags");
569 * If the attr is '-1', that means use a pre-existing
570 * one (if it already exists). If one does NOT already exist,
571 * then create a new attribute, with a non-conflicting value,
575 if (dict_attrbyname(name)) {
576 return 0; /* exists, don't add it again */
581 } else if (vendor == 0) {
585 if (attr > max_attr) {
591 * Additional checks for extended attributes.
593 if (flags.extended || flags.extended_flags || flags.evs) {
594 if (vendor && (vendor < FR_MAX_VENDOR)) {
595 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
598 if (!vendor) vendor = VENDORPEC_EXTENDED;
604 || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
605 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
611 if (!(flags.extended || flags.extended_flags)) {
612 fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
616 if (vendor <= FR_MAX_VENDOR) {
617 fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
623 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
627 if (flags.has_tlv && flags.length) {
628 fr_strerror_printf("TLVs cannot have a fixed length");
632 if (vendor && (vendor != VENDORPEC_EXTENDED)) {
634 static DICT_VENDOR *last_vendor = NULL;
636 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
637 fr_strerror_printf("TLV's cannot be encrypted");
641 if (flags.is_tlv && flags.has_tag) {
642 fr_strerror_printf("Sub-TLV's cannot have a tag");
646 if (flags.has_tlv && flags.has_tag) {
647 fr_strerror_printf("TLV's cannot have a tag");
652 * Most ATTRIBUTEs are bunched together by
653 * VENDOR. We can save a lot of lookups on
654 * dictionary initialization by caching the last
658 ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
662 * Ignore the high byte (sigh)
664 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
669 * If the vendor isn't defined, die.
672 fr_strerror_printf("dict_addattr: Unknown vendor %u",
678 * FIXME: Switch over dv->type, and limit things
681 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
682 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
684 } /* else 256..65535 are allowed */
687 * Set the extended flags as appropriate.
689 if (vendor > FR_MAX_VENDOR) {
692 myattr = (vendor >> 24) & 0xff;
693 myattr |= PW_VENDOR_SPECIFIC << 8;
695 da = dict_attrbyvalue(myattr, VENDORPEC_EXTENDED);
697 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown \"evs\" type.");
700 flags.extended = da->flags.extended;
701 flags.extended_flags = da->flags.extended_flags;
702 flags.evs = da->flags.evs;
706 * <sigh> Alvarion, being *again* a horribly
707 * broken vendor, has re-used the WiMAX format in
708 * their proprietary vendor space. This re-use
709 * means that there are *multiple* conflicting
710 * Alvarion dictionaries.
712 flags.wimax = dv->flags;
716 * Create a new attribute for the list
718 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
719 fr_strerror_printf("dict_addattr: out of memory");
723 memcpy(da->name, name, namelen);
724 da->name[namelen] = '\0';
731 * Insert the attribute, only if it's not a duplicate.
733 if (!fr_hash_table_insert(attributes_byname, da)) {
737 * If the attribute has identical number, then
738 * ignore the duplicate.
740 a = fr_hash_table_finddata(attributes_byname, da);
741 if (a && (strcasecmp(a->name, da->name) == 0)) {
742 if (a->attr != da->attr) {
743 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
749 * Same name, same vendor, same attr,
750 * maybe the flags and/or type is
751 * different. Let the new value
752 * over-ride the old one.
757 fr_hash_table_delete(attributes_byvalue, a);
759 if (!fr_hash_table_replace(attributes_byname, da)) {
760 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
767 * Insert the SAME pointer (not free'd when this entry is
768 * deleted), into another table.
770 * We want this behaviour because we want OLD names for
771 * the attributes to be read from the configuration
772 * files, but when we're printing them, (and looking up
773 * by value) we want to use the NEW name.
775 if (!fr_hash_table_replace(attributes_byvalue, da)) {
776 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
780 if (!vendor && (attr > 0) && (attr < 256)) {
781 dict_base_attrs[attr] = da;
789 * Add a value for an attribute to the dictionary.
791 int dict_addvalue(const char *namestr, const char *attrstr, int value)
797 static DICT_ATTR *last_attr = NULL;
800 fr_strerror_printf("dict_addvalue: empty names are not permitted");
804 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
805 fr_strerror_printf("dict_addvalue: value name too long");
809 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
810 fr_strerror_printf("dict_addvalue: out of memory");
813 memset(dval, 0, sizeof(*dval));
815 strcpy(dval->name, namestr);
819 * Most VALUEs are bunched together by ATTRIBUTE. We can
820 * save a lot of lookups on dictionary initialization by
821 * caching the last attribute.
823 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
826 dattr = dict_attrbyname(attrstr);
831 * Remember which attribute is associated with this
832 * value, if possible.
835 if (dattr->flags.has_value_alias) {
836 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
840 dval->attr = dattr->attr;
841 dval->vendor = dattr->vendor;
844 * Enforce valid values
846 * Don't worry about fixups...
848 switch (dattr->type) {
852 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
859 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
865 * Allow octets for now, because
866 * of dictionary.cablelabs
870 case PW_TYPE_INTEGER:
873 case PW_TYPE_INTEGER64:
876 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
877 fr_int2str(dict_attr_types, dattr->type, "?Unknown?"));
881 dattr->flags.has_value = 1;
883 value_fixup_t *fixup;
885 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
888 fr_strerror_printf("dict_addvalue: out of memory");
891 memset(fixup, 0, sizeof(*fixup));
893 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
897 * Insert to the head of the list.
899 fixup->next = value_fixup;
906 * Add the value into the dictionary.
908 if (!fr_hash_table_insert(values_byname, dval)) {
913 * Suppress duplicates with the same
914 * name and value. There are lots in
917 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
918 if (old && (old->value == dval->value)) {
925 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
930 * There are multiple VALUE's, keyed by attribute, so we
931 * take care of that here.
933 if (!fr_hash_table_replace(values_byvalue, dval)) {
934 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
942 static int sscanf_i(const char *str, unsigned int *pvalue)
946 static const char *tab = "0123456789";
948 if ((str[0] == '0') &&
949 ((str[1] == 'x') || (str[1] == 'X'))) {
950 tab = "0123456789abcdef";
959 if (*str == '.') break;
961 c = memchr(tab, tolower((int) *str), base);
974 int dict_str2oid(const char *ptr, unsigned int *pvalue, int vendor, int tlv_depth)
980 if (tlv_depth > fr_attr_max_tlv) {
981 fr_strerror_printf("Attribute has too long OID");
987 da = dict_attrbyvalue(*pvalue, vendor);
989 da = dict_attrbyvalue(*pvalue, VENDORPEC_EXTENDED);
992 fr_strerror_printf("Parent attribute is undefined.");
996 if (!(da->flags.has_tlv || da->flags.extended || da->flags.extended_flags)) {
997 fr_strerror_printf("Parent attribute %s cannot have sub-tlvs",
1003 p = strchr(ptr, '.');
1005 if (!sscanf_i(ptr, &value)) {
1006 fr_strerror_printf("Failed parsing attribute identifier %s",
1012 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
1018 return dict_str2oid(p + 1, pvalue, vendor, tlv_depth + 1);
1026 * Process the ATTRIBUTE command
1028 static int process_attribute(const char* fn, const int line,
1029 const unsigned int block_vendor, DICT_ATTR *block_tlv,
1030 int tlv_depth, char **argv, int argc)
1033 unsigned int vendor = 0;
1036 unsigned int length = 0;
1040 if ((argc < 3) || (argc > 4)) {
1041 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
1046 if (strncmp(argv[1], "Attr-", 5) == 0) {
1047 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1052 memset(&flags, 0, sizeof(flags));
1055 * Look for extended attributes before doing anything else.
1057 p = strchr(argv[1], '.');
1064 * Validate all entries
1066 if (!sscanf_i(argv[1], &value)) {
1067 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1072 if (value > (1 << 24)) {
1073 fr_strerror_printf("dict_init: %s[%d]: Attribute number is too large", fn, line);
1079 * Parse NUM.NUM.NUM.NUM
1085 *p = '.'; /* reset for later printing */
1088 da = dict_attrbyvalue(value, block_vendor);
1089 } else if (value == PW_VENDOR_SPECIFIC) {
1091 const DICT_VENDOR *dv;
1097 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier", fn, line);
1102 if (!sscanf_i(p, &vendor)) {
1108 dv = dict_vendorbyvalue(vendor);
1110 fr_strerror_printf("dict_init: %s[%d]: Unknown vendor \"%u\" in attribute identifier", fn, line, vendor);
1115 return process_attribute(fn, line, vendor, NULL, 0,
1118 da = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
1121 fr_strerror_printf("dict_init: %s[%d]: Entry refers to unknown attribute %d", fn, line, value);
1126 * 241.1 means 241 is of type "extended".
1129 if (!(da->flags.has_tlv || da->flags.extended || da->flags.extended_flags)) {
1130 fr_strerror_printf("dict_init: %s[%d]: Parent attribute %s cannot contain sub-attributes", fn, line, da->name);
1134 my_depth = dict_str2oid(p + 1, &value, block_vendor, tlv_depth + 1);
1138 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1140 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1145 * Look up the REAL parent TLV.
1148 unsigned int parent = value;
1150 parent &= ~(fr_attr_mask[my_depth] << fr_attr_shift[my_depth]);
1152 da = dict_attrbyvalue(parent, da->vendor);
1154 fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
1160 * Set which type of attribute this is.
1162 flags.extended = da->flags.extended;
1163 flags.extended_flags = da->flags.extended_flags;
1164 flags.evs = da->flags.evs;
1165 if (da->flags.has_tlv) flags.is_tlv = 1;
1168 if (strncmp(argv[2], "octets[", 7) != 0) {
1170 * find the type of the attribute.
1172 type = fr_str2int(dict_attr_types, argv[2], -1);
1174 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1180 type = PW_TYPE_OCTETS;
1182 p = strchr(argv[2] + 7, ']');
1184 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1190 if (!sscanf_i(argv[1], &length)) {
1191 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1195 if ((length == 0) || (length > 253)) {
1196 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1202 * Only look up the vendor if the string
1207 * Force "length" for data types of fixed length;
1219 case PW_TYPE_IPADDR:
1220 case PW_TYPE_INTEGER:
1221 case PW_TYPE_SIGNED:
1225 case PW_TYPE_INTEGER64:
1229 case PW_TYPE_ETHERNET:
1237 case PW_TYPE_IPV6ADDR:
1241 case PW_TYPE_EXTENDED:
1242 if ((vendor != 0) || (value < 241)) {
1243 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended\" MUST be RFC attributes with value >= 241.", fn, line);
1246 type = PW_TYPE_OCTETS;
1250 case PW_TYPE_EXTENDED_FLAGS:
1251 if ((vendor != 0) || (value < 241)) {
1252 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended-flags\" MUST be RFC attributes with value >= 241.", fn, line);
1255 type = PW_TYPE_OCTETS;
1256 flags.extended_flags = 1;
1260 type = PW_TYPE_OCTETS;
1262 if (((value >> fr_attr_shift[1]) & fr_attr_mask[1]) != PW_VENDOR_SPECIFIC) {
1263 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1272 flags.length = length;
1274 } else { /* argc == 4: we have options */
1275 char *key, *next, *last;
1280 if (flags.extended || flags.extended_flags || flags.evs) {
1281 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1286 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1292 next = strchr(key, ',');
1293 if (next) *(next++) = '\0';
1295 if (strcmp(key, "has_tag") == 0 ||
1296 strcmp(key, "has_tag=1") == 0) {
1297 /* Boolean flag, means this is a
1301 } else if (strncmp(key, "encrypt=", 8) == 0) {
1302 /* Encryption method, defaults to 0 (none).
1303 Currently valid is just type 2,
1304 Tunnel-Password style, which can only
1305 be applied to strings. */
1306 flags.encrypt = strtol(key + 8, &last, 0);
1308 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1313 if ((flags.encrypt == FLAG_ENCRYPT_ASCEND_SECRET) &&
1314 (type != PW_TYPE_STRING)) {
1315 fr_strerror_printf( "dict_init: %s[%d] Only \"string\" types can have the \"encrypt=3\" flag set.",
1320 } else if (strncmp(key, "array", 6) == 0) {
1324 case PW_TYPE_IPADDR:
1327 case PW_TYPE_INTEGER:
1332 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1338 * The only thing is the vendor name,
1339 * and it's a known name: allow it.
1341 } else if ((key == argv[3]) && !next) {
1343 fr_strerror_printf( "dict_init: %s[%d] New-style attributes cannot use a vendor flag.",
1349 fr_strerror_printf( "dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" is not allowed.",
1354 vendor = dict_vendorbyname(key);
1355 if (!vendor) goto unknown;
1360 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1366 if (key && !*key) break;
1370 if (block_vendor) vendor = block_vendor;
1373 * Special checks for tags, they make our life much more
1376 if (flags.has_tag) {
1378 * Only string, octets, and integer can be tagged.
1381 case PW_TYPE_STRING:
1382 case PW_TYPE_INTEGER:
1386 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1388 fr_int2str(dict_attr_types, type, "?Unknown?"));
1393 if (type == PW_TYPE_TLV) {
1394 if (vendor && (vendor < FR_MAX_VENDOR)
1396 && (vendor != DHCP_MAGIC_VENDOR)
1401 dv = dict_vendorbyvalue(vendor);
1402 if (!dv || (dv->type != 1) || (dv->length != 1)) {
1403 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1414 * TLV's can be only one octet.
1416 if ((value <= 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1417 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1425 value <<= fr_attr_shift[tlv_depth];
1426 value |= block_tlv->attr;
1430 #ifdef WITH_DICTIONARY_WARNINGS
1432 * Hack to help us discover which vendors have illegal
1435 if (!vendor && (value < 256) &&
1436 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1437 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1445 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1448 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1450 fr_strerror_printf("dict_init: %s[%d]: %s",
1460 * Process the VALUE command
1462 static int process_value(const char* fn, const int line, char **argv,
1468 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1473 * For Compatibility, skip "Server-Config"
1475 if (strcasecmp(argv[0], "Server-Config") == 0)
1479 * Validate all entries
1481 if (!sscanf_i(argv[2], &value)) {
1482 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1487 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1490 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1492 fr_strerror_printf("dict_init: %s[%d]: %s",
1502 * Process the VALUE-ALIAS command
1504 * This allows VALUE mappings to be shared among multiple
1507 static int process_value_alias(const char* fn, const int line, char **argv,
1510 DICT_ATTR *my_da, *da;
1514 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1519 my_da = dict_attrbyname(argv[0]);
1521 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1526 if (my_da->flags.has_value) {
1527 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1532 if (my_da->flags.has_value_alias) {
1533 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1538 da = dict_attrbyname(argv[1]);
1540 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1545 if (!da->flags.has_value) {
1546 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1551 if (da->flags.has_value_alias) {
1552 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1557 if (my_da->type != da->type) {
1558 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1563 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1564 fr_strerror_printf("dict_addvalue: out of memory");
1568 dval->name[0] = '\0'; /* empty name */
1569 dval->attr = my_da->attr;
1570 dval->vendor = my_da->vendor;
1571 dval->value = da->attr;
1573 if (!fr_hash_table_insert(values_byname, dval)) {
1574 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1585 * Process the VENDOR command
1587 static int process_vendor(const char* fn, const int line, char **argv,
1591 int continuation = 0;
1592 const char *format = NULL;
1594 if ((argc < 2) || (argc > 3)) {
1595 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1601 * Validate all entries
1603 if (!isdigit((int) argv[1][0])) {
1604 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1608 value = atoi(argv[1]);
1610 /* Create a new VENDOR entry for the list */
1611 if (dict_addvendor(argv[0], value) < 0) {
1614 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1616 fr_strerror_printf("dict_init: %s[%d]: %s",
1622 * Look for a format statement
1627 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1628 format = "format=4,0";
1630 } else if (value == VENDORPEC_LUCENT) {
1631 format = "format=2,1";
1633 } else if (value == VENDORPEC_STARENT) {
1634 format = "format=2,2";
1636 } /* else no fixups to do */
1643 if (strncasecmp(format, "format=", 7) != 0) {
1644 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1650 if ((strlen(p) < 3) ||
1651 !isdigit((int) p[0]) ||
1653 !isdigit((int) p[2]) ||
1654 (p[3] && (p[3] != ','))) {
1655 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1660 type = (int) (p[0] - '0');
1661 length = (int) (p[2] - '0');
1664 if ((p[4] != 'c') ||
1666 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1672 if ((value != VENDORPEC_WIMAX) ||
1673 (type != 1) || (length != 1)) {
1674 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1680 dv = dict_vendorbyvalue(value);
1682 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1687 if ((type != 1) && (type != 2) && (type != 4)) {
1688 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1693 if ((length != 0) && (length != 1) && (length != 2)) {
1694 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1700 dv->length = length;
1701 dv->flags = continuation;
1708 * String split routine. Splits an input string IN PLACE
1709 * into pieces, based on spaces.
1711 static int str2argv(char *str, char **argv, int max_argc)
1716 if (argc >= max_argc) break;
1719 * Chop out comments early.
1726 while ((*str == ' ') ||
1729 (*str == '\n')) *(str++) = '\0';
1740 (*str != '\n')) str++;
1746 #define MAX_ARGV (16)
1749 * Initialize the dictionary.
1751 static int my_dict_init(const char *dir, const char *fn,
1752 const char *src_file, int src_line)
1759 unsigned int vendor;
1760 unsigned int block_vendor;
1761 struct stat statbuf;
1762 char *argv[MAX_ARGV];
1764 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1765 int which_block_tlv = 0;
1767 block_tlv[0] = NULL;
1768 block_tlv[1] = NULL;
1769 block_tlv[2] = NULL;
1770 block_tlv[3] = NULL;
1772 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1773 strlen(dir) >= sizeof(dirtmp) / 2) {
1774 fr_strerror_printf("dict_init: filename name too long");
1779 * First see if fn is relative to dir. If so, create
1780 * new filename. If not, remember the absolute dir.
1782 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1786 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1787 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1791 if ((fp = fopen(fn, "r")) == NULL) {
1793 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1794 fn, strerror(errno));
1796 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1797 src_file, src_line, fn, strerror(errno));
1802 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1803 if (!S_ISREG(statbuf.st_mode)) {
1805 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1811 * Globally writable dictionaries means that users can control
1812 * the server configuration with little difficulty.
1815 if ((statbuf.st_mode & S_IWOTH) != 0) {
1817 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1823 dict_stat_add(fn, &statbuf);
1826 * Seed the random pool with data.
1828 fr_rand_seed(&statbuf, sizeof(statbuf));
1832 while (fgets(buf, sizeof(buf), fp) != NULL) {
1834 if (buf[0] == '#' || buf[0] == 0 ||
1835 buf[0] == '\n' || buf[0] == '\r')
1839 * Comment characters should NOT be appearing anywhere but
1840 * as start of a comment;
1842 p = strchr(buf, '#');
1845 argc = str2argv(buf, argv, MAX_ARGV);
1846 if (argc == 0) continue;
1849 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1856 * Process VALUE lines.
1858 if (strcasecmp(argv[0], "VALUE") == 0) {
1859 if (process_value(fn, line,
1860 argv + 1, argc - 1) == -1) {
1868 * Perhaps this is an attribute.
1870 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1871 if (process_attribute(fn, line, block_vendor,
1872 block_tlv[which_block_tlv],
1874 argv + 1, argc - 1) == -1) {
1882 * See if we need to import another dictionary.
1884 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1885 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1892 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1893 if (process_value_alias(fn, line,
1894 argv + 1, argc - 1) == -1) {
1902 * Process VENDOR lines.
1904 if (strcasecmp(argv[0], "VENDOR") == 0) {
1905 if (process_vendor(fn, line,
1906 argv + 1, argc - 1) == -1) {
1913 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1916 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1922 da = dict_attrbyname(argv[1]);
1925 "dict_init: %s[%d]: unknown attribute %s",
1931 if (da->type != PW_TYPE_TLV) {
1933 "dict_init: %s[%d]: attribute %s is not of type tlv",
1939 if (which_block_tlv >= MAX_TLV_NEST) {
1941 "dict_init: %s[%d]: TLVs are nested too deep",
1948 block_tlv[++which_block_tlv] = da;
1952 if (strcasecmp(argv[0], "END-TLV") == 0) {
1955 "dict_init: %s[%d] invalid END-TLV entry",
1961 da = dict_attrbyname(argv[1]);
1964 "dict_init: %s[%d]: unknown attribute %s",
1970 if (da != block_tlv[which_block_tlv]) {
1972 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1977 block_tlv[which_block_tlv--] = NULL;
1981 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1984 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1990 vendor = dict_vendorbyname(argv[1]);
1993 "dict_init: %s[%d]: unknown vendor %s",
1999 block_vendor = vendor;
2002 * Check for extended attr VSAs
2005 if (strncmp(argv[2], "format=", 7) != 0) {
2007 "dict_init: %s[%d]: Invalid format %s",
2014 da = dict_attrbyname(p);
2016 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
2022 if (!da->flags.evs) {
2023 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
2030 * Pack the encapsulating attribute
2031 * into the vendor Id. This attribute
2034 block_vendor |= (da->attr & fr_attr_mask[1]) * FR_MAX_VENDOR;
2038 } /* BEGIN-VENDOR */
2040 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
2043 "dict_init: %s[%d] invalid END-VENDOR entry",
2049 vendor = dict_vendorbyname(argv[1]);
2052 "dict_init: %s[%d]: unknown vendor %s",
2058 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2060 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2070 * Any other string: We don't recognize it.
2072 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2083 * Empty callback for hash table initialization.
2085 static int null_callback(void *ctx, void *data)
2087 ctx = ctx; /* -Wunused */
2088 data = data; /* -Wunused */
2095 * Initialize the directory, then fix the attr member of
2098 int dict_init(const char *dir, const char *fn)
2101 * Check if we need to change anything. If not, don't do
2104 if (dict_stat_check(dir, fn)) {
2109 * Free the dictionaries, and the stat cache.
2112 stat_root_dir = strdup(dir);
2113 stat_root_file = strdup(fn);
2116 * Create the table of vendor by name. There MAY NOT
2117 * be multiple vendors of the same name.
2119 * Each vendor is malloc'd, so the free function is free.
2121 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2122 dict_vendor_name_cmp,
2124 if (!vendors_byname) {
2129 * Create the table of vendors by value. There MAY
2130 * be vendors of the same value. If there are, we
2131 * pick the latest one.
2133 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2134 dict_vendor_value_cmp,
2136 if (!vendors_byvalue) {
2141 * Create the table of attributes by name. There MAY NOT
2142 * be multiple attributes of the same name.
2144 * Each attribute is malloc'd, so the free function is free.
2146 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2149 if (!attributes_byname) {
2154 * Create the table of attributes by value. There MAY
2155 * be attributes of the same value. If there are, we
2156 * pick the latest one.
2158 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2159 dict_attr_value_cmp,
2161 if (!attributes_byvalue) {
2165 values_byname = fr_hash_table_create(dict_value_name_hash,
2166 dict_value_name_cmp,
2168 if (!values_byname) {
2172 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2173 dict_value_value_cmp,
2175 if (!values_byvalue) {
2179 value_fixup = NULL; /* just to be safe. */
2181 if (my_dict_init(dir, fn, NULL, 0) < 0)
2186 value_fixup_t *this, *next;
2188 for (this = value_fixup; this != NULL; this = next) {
2191 a = dict_attrbyname(this->attrstr);
2194 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2195 this->attrstr, this->dval->name);
2196 return -1; /* leak, but they should die... */
2199 this->dval->attr = a->attr;
2202 * Add the value into the dictionary.
2204 if (!fr_hash_table_replace(values_byname,
2206 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2211 * Allow them to use the old name, but
2212 * prefer the new name when printing
2215 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2216 fr_hash_table_replace(values_byvalue,
2222 * Just so we don't lose track of things.
2229 * Walk over all of the hash tables to ensure they're
2230 * initialized. We do this because the threads may perform
2231 * lookups, and we don't want multi-threaded re-ordering
2232 * of the table entries. That would be bad.
2234 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2235 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2237 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2238 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2240 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2241 fr_hash_table_walk(values_byname, null_callback, NULL);
2247 * Get an attribute by its numerical value.
2249 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2253 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2256 dattr.vendor = vendor;
2258 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2262 * Get an attribute by its name.
2264 DICT_ATTR *dict_attrbyname(const char *name)
2267 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2269 if (!name) return NULL;
2271 da = (DICT_ATTR *) buffer;
2272 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2274 return fr_hash_table_finddata(attributes_byname, da);
2278 * Associate a value with an attribute and return it.
2280 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2282 DICT_VALUE dval, *dv;
2285 * First, look up aliases.
2288 dval.vendor = vendor;
2289 dval.name[0] = '\0';
2292 * Look up the attribute alias target, and use
2293 * the correct attribute number if found.
2295 dv = fr_hash_table_finddata(values_byname, &dval);
2296 if (dv) dval.attr = dv->value;
2300 return fr_hash_table_finddata(values_byvalue, &dval);
2304 * Associate a value with an attribute and return it.
2306 const char *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value)
2310 dv = dict_valbyattr(attr, vendor, value);
2317 * Get a value by its name, keyed off of an attribute.
2319 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2321 DICT_VALUE *my_dv, *dv;
2322 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2324 if (!name) return NULL;
2326 my_dv = (DICT_VALUE *) buffer;
2328 my_dv->vendor = vendor;
2329 my_dv->name[0] = '\0';
2332 * Look up the attribute alias target, and use
2333 * the correct attribute number if found.
2335 dv = fr_hash_table_finddata(values_byname, my_dv);
2336 if (dv) my_dv->attr = dv->value;
2338 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2340 return fr_hash_table_finddata(values_byname, my_dv);
2344 * Get the vendor PEC based on the vendor name
2346 * This is efficient only for small numbers of vendors.
2348 int dict_vendorbyname(const char *name)
2351 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
2353 if (!name) return 0;
2355 dv = (DICT_VENDOR *) buffer;
2356 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
2358 dv = fr_hash_table_finddata(vendors_byname, dv);
2361 return dv->vendorpec;
2365 * Return the vendor struct based on the PEC.
2367 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
2371 dv.vendorpec = vendorpec;
2373 return fr_hash_table_finddata(vendors_byvalue, &dv);