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 The FreeRADIUS server project
23 static const char rcsid[] = "$Id$";
35 #ifdef HAVE_SYS_STAT_H
42 #include "libradius.h"
44 #define DICT_VALUE_MAX_NAME_LEN (128)
45 #define DICT_VENDOR_MAX_NAME_LEN (128)
47 static lrad_hash_table_t *vendors_byname = NULL;
48 static lrad_hash_table_t *vendors_byvalue = NULL;
50 static lrad_hash_table_t *attributes_byname = NULL;
51 static lrad_hash_table_t *attributes_byvalue = NULL;
53 static lrad_hash_table_t *values_byvalue = NULL;
54 static lrad_hash_table_t *values_byname = NULL;
57 * For faster HUP's, we cache the stat information for
58 * files we've $INCLUDEd
60 typedef struct dict_stat_t {
61 struct dict_stat_t *next;
66 static char *stat_root_dir = NULL;
67 static char *stat_root_file = NULL;
69 static dict_stat_t *stat_head = NULL;
70 static dict_stat_t *stat_tail = NULL;
72 typedef struct value_fixup_t {
75 struct value_fixup_t *next;
80 * So VALUEs in the dictionary can have forward references.
82 static value_fixup_t *value_fixup = NULL;
84 static const LRAD_NAME_NUMBER type_table[] = {
85 { "string", PW_TYPE_STRING },
86 { "integer", PW_TYPE_INTEGER },
87 { "ipaddr", PW_TYPE_IPADDR },
88 { "date", PW_TYPE_DATE },
89 { "abinary", PW_TYPE_ABINARY },
90 { "octets", PW_TYPE_OCTETS },
91 { "ifid", PW_TYPE_IFID },
92 { "ipv6addr", PW_TYPE_IPV6ADDR },
93 { "ipv6prefix", PW_TYPE_IPV6PREFIX },
99 * Create the hash of the name.
101 * We copy the hash function here because it's substantially faster.
103 #define FNV_MAGIC_INIT (0x811c9dc5)
104 #define FNV_MAGIC_PRIME (0x01000193)
106 static uint32_t dict_hashname(const char *name)
108 uint32_t hash = FNV_MAGIC_INIT;
111 for (p = name; *p != '\0'; p++) {
112 int c = *(const unsigned char *) p;
113 if (isalpha(c)) c = tolower(c);
115 hash *= FNV_MAGIC_PRIME;
116 hash ^= (uint32_t ) (c & 0xff);
124 * Hash callback functions.
126 static uint32_t dict_attr_name_hash(const void *data)
128 return dict_hashname(((const DICT_ATTR *)data)->name);
131 static int dict_attr_name_cmp(const void *one, const void *two)
133 const DICT_ATTR *a = one;
134 const DICT_ATTR *b = two;
136 return strcasecmp(a->name, b->name);
139 static uint32_t dict_attr_value_hash(const void *data)
141 return lrad_hash(&((const DICT_ATTR *)data)->attr,
142 sizeof(((const DICT_ATTR *)data)->attr));
145 static int dict_attr_value_cmp(const void *one, const void *two)
147 const DICT_ATTR *a = one;
148 const DICT_ATTR *b = two;
150 return a->attr - b->attr;
153 static uint32_t dict_vendor_name_hash(const void *data)
155 return dict_hashname(((const DICT_VENDOR *)data)->name);
158 static int dict_vendor_name_cmp(const void *one, const void *two)
160 const DICT_VENDOR *a = one;
161 const DICT_VENDOR *b = two;
163 return strcasecmp(a->name, b->name);
166 static uint32_t dict_vendor_value_hash(const void *data)
168 return lrad_hash(&(((const DICT_VENDOR *)data)->vendorpec),
169 sizeof(((const DICT_VENDOR *)data)->vendorpec));
172 static int dict_vendor_value_cmp(const void *one, const void *two)
174 const DICT_VENDOR *a = one;
175 const DICT_VENDOR *b = two;
177 return a->vendorpec - b->vendorpec;
180 static uint32_t dict_value_name_hash(const void *data)
183 const DICT_VALUE *dval = data;
185 hash = dict_hashname(dval->name);
186 return lrad_hash_update(&dval->attr, sizeof(dval->attr), hash);
189 static int dict_value_name_cmp(const void *one, const void *two)
192 const DICT_VALUE *a = one;
193 const DICT_VALUE *b = two;
195 rcode = a->attr - b->attr;
196 if (rcode != 0) return rcode;
198 return strcasecmp(a->name, b->name);
201 static uint32_t dict_value_value_hash(const void *data)
204 const DICT_VALUE *dval = data;
206 hash = lrad_hash(&dval->attr, sizeof(dval->attr));
207 return lrad_hash_update(&dval->value, sizeof(dval->value), hash);
210 static int dict_value_value_cmp(const void *one, const void *two)
213 const DICT_VALUE *a = one;
214 const DICT_VALUE *b = two;
216 rcode = a->attr - b->attr;
217 if (rcode != 0) return rcode;
219 return a->value - b->value;
224 * Free the list of stat buffers
226 static void dict_stat_free(void)
228 dict_stat_t *this, *next;
231 stat_root_dir = NULL;
232 free(stat_root_file);
233 stat_root_file = NULL;
240 for (this = stat_head; this != NULL; this = next) {
246 stat_head = stat_tail = NULL;
251 * Add an entry to the list of stat buffers.
253 static void dict_stat_add(const char *name, const struct stat *stat_buf)
257 this = malloc(sizeof(*this));
258 memset(this, 0, sizeof(*this));
260 this->name = strdup(name);
261 this->mtime = stat_buf->st_mtime;
264 stat_head = stat_tail = this;
266 stat_tail->next = this;
273 * See if any dictionaries have changed. If not, don't
276 static int dict_stat_check(const char *root_dir, const char *root_file)
281 if (!stat_root_dir) return 0;
282 if (!stat_root_file) return 0;
284 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
285 if (strcmp(root_file, stat_root_file) != 0) return 0;
287 if (!stat_head) return 0; /* changed, reload */
289 for (this = stat_head; this != NULL; this = this->next) {
290 if (stat(this->name, &buf) < 0) return 0;
292 if (buf.st_mtime != this->mtime) return 0;
300 * Free the dictionary_attributes and dictionary_values lists.
307 lrad_hash_table_free(vendors_byname);
308 lrad_hash_table_free(vendors_byvalue);
309 vendors_byname = NULL;
310 vendors_byvalue = NULL;
312 lrad_hash_table_free(attributes_byname);
313 lrad_hash_table_free(attributes_byvalue);
314 attributes_byname = NULL;
315 attributes_byvalue = NULL;
317 lrad_hash_table_free(values_byname);
318 lrad_hash_table_free(values_byvalue);
319 values_byname = NULL;
320 values_byvalue = NULL;
327 * Add vendor to the list.
329 int dict_addvendor(const char *name, int value)
334 if (value >= (1 << 16)) {
335 librad_log("dict_addvendor: Cannot handle vendor ID larger than 65535");
339 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
340 librad_log("dict_addvendor: vendor name too long");
344 if ((dv = malloc(sizeof(*dv) + length)) == NULL) {
345 librad_log("dict_addvendor: out of memory");
349 strcpy(dv->name, name);
350 dv->vendorpec = value;
351 dv->type = dv->length = 1; /* defaults */
353 if (!lrad_hash_table_insert(vendors_byname, dv)) {
356 old_dv = lrad_hash_table_finddata(vendors_byname, dv);
358 librad_log("dict_addvendor: Failed inserting vendor name %s", name);
361 if (old_dv->vendorpec != dv->vendorpec) {
362 librad_log("dict_addvendor: Duplicate vendor name %s", name);
367 * Already inserted. Discard the duplicate entry.
374 * Insert the SAME pointer (not free'd when this table is
375 * deleted), into another table.
377 * We want this behaviour because we want OLD names for
378 * the attributes to be read from the configuration
379 * files, but when we're printing them, (and looking up
380 * by value) we want to use the NEW name.
382 if (!lrad_hash_table_replace(vendors_byvalue, dv)) {
383 librad_log("dict_addvendor: Failed inserting vendor %s",
392 * Add an attribute to the dictionary.
394 int dict_addattr(const char *name, int vendor, int type, int value,
397 static int max_attr = 0;
400 if (strlen(name) > (sizeof(attr->name) -1)) {
401 librad_log("dict_addattr: attribute name too long");
406 * If the value is '-1', that means use a pre-existing
407 * one (if it already exists). If one does NOT already exist,
408 * then create a new attribute, with a non-conflicting value,
412 if (dict_attrbyname(name)) {
413 return 0; /* exists, don't add it again */
418 } else if (vendor == 0) {
422 if (value > max_attr) {
428 librad_log("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
432 if (value >= 65536) {
433 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
438 DICT_VENDOR *dv = dict_vendorbyvalue(vendor);
441 * If the vendor isn't defined, die/
444 librad_log("dict_addattr: Unknown vendor");
449 * With a few exceptions, attributes can only be
450 * 1..255. The check above catches the less than
453 if ((dv->type == 1) && (value >= 256)) {
454 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
456 } /* else 256..65535 are allowed */
460 * Create a new attribute for the list
462 if ((attr = malloc(sizeof(*attr))) == NULL) {
463 librad_log("dict_addattr: out of memory");
467 strcpy(attr->name, name);
469 attr->attr |= (vendor << 16); /* FIXME: hack */
472 attr->vendor = vendor;
475 * Insert the attribute, only if it's not a duplicate.
477 if (!lrad_hash_table_insert(attributes_byname, attr)) {
481 * If the attribute has identical number, then
482 * ignore the duplicate.
484 a = lrad_hash_table_finddata(attributes_byname, attr);
485 if (a && (strcasecmp(a->name, attr->name) == 0)) {
486 if (a->attr != attr->attr) {
487 librad_log("dict_addattr: Duplicate attribute name %s", name);
492 * Same name, same vendor, same attr,
493 * maybe the flags and/or type is
494 * different. Let the new value
495 * over-ride the old one.
501 * Insert the SAME pointer (not free'd when this entry is
502 * deleted), into another table.
504 * We want this behaviour because we want OLD names for
505 * the attributes to be read from the configuration
506 * files, but when we're printing them, (and looking up
507 * by value) we want to use the NEW name.
509 if (!lrad_hash_table_replace(attributes_byvalue, attr)) {
510 librad_log("dict_addattr: Failed inserting attribute name %s", name);
519 * Add a value for an attribute to the dictionary.
521 int dict_addvalue(const char *namestr, const char *attrstr, int value)
527 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
528 librad_log("dict_addvalue: value name too long");
532 if ((dval = malloc(sizeof(*dval) + length)) == NULL) {
533 librad_log("dict_addvalue: out of memory");
536 memset(dval, 0, sizeof(*dval));
538 strcpy(dval->name, namestr);
542 * Remember which attribute is associated with this
543 * value, if possible.
545 dattr = dict_attrbyname(attrstr);
547 dval->attr = dattr->attr;
549 value_fixup_t *fixup;
551 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
553 librad_log("dict_addvalue: out of memory");
556 memset(fixup, 0, sizeof(*fixup));
558 strNcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
562 * Insert to the head of the list.
564 fixup->next = value_fixup;
571 * Add the value into the dictionary.
573 if (!lrad_hash_table_insert(values_byname, dval)) {
578 * Suppress duplicates with the same
579 * name and value. There are lots in
582 old = dict_valbyname(dattr->attr, namestr);
583 if (old && (old->value == dval->value)) {
589 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
594 * There are multiple VALUE's, keyed by attribute, so we
595 * take care of that here.
597 if (!lrad_hash_table_replace(values_byvalue, dval)) {
598 librad_log("dict_addvalue: Failed inserting value %s",
607 * Process the ATTRIBUTE command
609 static int process_attribute(const char* fn, const int line,
610 const int block_vendor, char **argv,
619 if ((argc < 3) || (argc > 4)) {
620 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
626 * Validate all entries
628 if (!isdigit((int) argv[1][0])) {
629 librad_log("dict_init: %s[%d]: invalid value", fn, line);
632 sscanf(argv[1], "%i", &value);
635 * find the type of the attribute.
637 type = lrad_str2int(type_table, argv[2], -1);
639 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
645 * Only look up the vendor if the string
648 memset(&flags, 0, sizeof(flags));
650 s = strtok(argv[3], ",");
652 if (strcmp(s, "has_tag") == 0 ||
653 strcmp(s, "has_tag=1") == 0) {
654 /* Boolean flag, means this is a
658 } else if (strncmp(s, "encrypt=", 8) == 0) {
659 /* Encryption method, defaults to 0 (none).
660 Currently valid is just type 2,
661 Tunnel-Password style, which can only
662 be applied to strings. */
663 flags.encrypt = strtol(s + 8, &c, 0);
665 librad_log( "dict_init: %s[%d] invalid option %s",
670 /* Must be a vendor 'flag'... */
671 if (strncmp(s, "vendor=", 7) == 0) {
676 vendor = dict_vendorbyname(s);
678 librad_log( "dict_init: %s[%d]: unknown vendor %s",
682 if (block_vendor && argv[3][0] &&
683 (block_vendor != vendor)) {
684 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
689 s = strtok(NULL, ",");
693 if (block_vendor) vendor = block_vendor;
696 * Special checks for tags, they make our life much more
701 * Only string, octets, and integer can be tagged.
705 case PW_TYPE_INTEGER:
709 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
711 lrad_int2str(type_table, type, "?Unknown?"));
720 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
721 librad_log("dict_init: %s[%d]: %s",
722 fn, line, librad_errstr);
731 * Process the VALUE command
733 static int process_value(const char* fn, const int line, char **argv,
739 librad_log("dict_init: %s[%d]: invalid VALUE line",
744 * For Compatibility, skip "Server-Config"
746 if (strcasecmp(argv[0], "Server-Config") == 0)
750 * Validate all entries
752 if (!isdigit((int) argv[2][0])) {
753 librad_log("dict_init: %s[%d]: invalid value",
757 sscanf(argv[2], "%i", &value);
759 if (dict_addvalue(argv[1], argv[0], value) < 0) {
760 librad_log("dict_init: %s[%d]: %s",
761 fn, line, librad_errstr);
770 * Process the VENDOR command
772 static int process_vendor(const char* fn, const int line, char **argv,
776 const char *format = NULL;
778 if ((argc < 2) || (argc > 3)) {
779 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
785 * Validate all entries
787 if (!isdigit((int) argv[1][0])) {
788 librad_log("dict_init: %s[%d]: invalid value",
792 value = atoi(argv[1]);
794 /* Create a new VENDOR entry for the list */
795 if (dict_addvendor(argv[0], value) < 0) {
796 librad_log("dict_init: %s[%d]: %s",
797 fn, line, librad_errstr);
802 * Look for a format statement
807 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
808 format = "format=4,0";
810 } else if (value == VENDORPEC_LUCENT) {
811 format = "format=2,1";
813 } else if (value == VENDORPEC_STARENT) {
814 format = "format=2,2";
816 } /* else no fixups to do */
823 if (strncasecmp(format, "format=", 7) != 0) {
824 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
830 if ((strlen(p) != 3) ||
831 !isdigit((int) p[0]) ||
833 !isdigit((int) p[2])) {
834 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
839 type = (int) (p[0] - '0');
840 length = (int) (p[2] - '0');
842 dv = dict_vendorbyvalue(value);
844 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
849 if ((type != 1) && (type != 2) && (type != 4)) {
850 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
855 if ((length != 0) && (length != 1) && (length != 2)) {
856 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
869 * String split routine. Splits an input string IN PLACE
870 * into pieces, based on spaces.
872 static int str2argv(char *str, char **argv, int max_argc)
877 if (argc >= max_argc) return argc;
880 * Chop out comments early.
887 while ((*str == ' ') ||
890 (*str == '\n')) *(str++) = '\0';
892 if (!*str) return argc;
901 (*str != '\n')) str++;
907 #define MAX_ARGV (16)
910 * Initialize the dictionary.
912 static int my_dict_init(const char *dir, const char *fn,
913 const char *src_file, int src_line)
923 char *argv[MAX_ARGV];
926 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
927 strlen(dir) >= sizeof(dirtmp) / 2) {
928 librad_log("dict_init: filename name too long");
933 * First see if fn is relative to dir. If so, create
934 * new filename. If not, remember the absolute dir.
936 if ((p = strrchr(fn, '/')) != NULL) {
940 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
941 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
945 if ((fp = fopen(fn, "r")) == NULL) {
947 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
948 fn, strerror(errno));
950 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
951 src_file, src_line, fn, strerror(errno));
956 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
957 if (!S_ISREG(statbuf.st_mode)) {
959 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
963 dict_stat_add(fn, &statbuf);
966 * Seed the random pool with data.
968 lrad_rand_seed(&statbuf, sizeof(statbuf));
972 while (fgets(buf, sizeof(buf), fp) != NULL) {
974 if (buf[0] == '#' || buf[0] == 0 ||
975 buf[0] == '\n' || buf[0] == '\r')
979 * Comment characters should NOT be appearing anywhere but
980 * as start of a comment;
982 p = strchr(buf, '#');
985 argc = str2argv(buf, argv, MAX_ARGV);
986 if (argc == 0) continue;
989 librad_log( "dict_init: %s[%d] invalid entry",
998 fprintf(stderr, "ARGC = %d\n",argc);
999 for (i = 0; i < argc; i++) {
1000 fprintf(stderr, "\t%s\n", argv[i]);
1005 * See if we need to import another dictionary.
1007 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1008 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1016 * Perhaps this is an attribute.
1018 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1019 if (process_attribute(fn, line, block_vendor,
1020 argv + 1, argc - 1) == -1) {
1028 * Process VALUE lines.
1030 if (strcasecmp(argv[0], "VALUE") == 0) {
1031 if (process_value(fn, line,
1032 argv + 1, argc - 1) == -1) {
1040 * Process VENDOR lines.
1042 if (strcasecmp(argv[0], "VENDOR") == 0) {
1043 if (process_vendor(fn, line,
1044 argv + 1, argc - 1) == -1) {
1051 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1054 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1060 vendor = dict_vendorbyname(argv[1]);
1063 "dict_init: %s[%d]: unknown vendor %s",
1068 block_vendor = vendor;
1070 } /* BEGIN-VENDOR */
1072 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1075 "dict_init: %s[%d] invalid END-VENDOR entry",
1081 vendor = dict_vendorbyname(argv[1]);
1084 "dict_init: %s[%d]: unknown vendor %s",
1090 if (vendor != block_vendor) {
1092 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1102 * Any other string: We don't recognize it.
1105 "dict_init: %s[%d] invalid keyword \"%s\"",
1116 * Empty callback for hash table initialization.
1118 static int null_callback(void *ctx, void *data)
1120 ctx = ctx; /* -Wunused */
1121 data = data; /* -Wunused */
1128 * Initialize the directory, then fix the attr member of
1131 int dict_init(const char *dir, const char *fn)
1134 * Check if we need to change anything. If not, don't do
1137 if (dict_stat_check(dir, fn)) {
1142 * Free the dictionaries, and the stat cache.
1145 stat_root_dir = strdup(dir);
1146 stat_root_file = strdup(fn);
1149 * Create the table of vendor by name. There MAY NOT
1150 * be multiple vendors of the same name.
1152 * Each vendor is malloc'd, so the free function is free.
1154 vendors_byname = lrad_hash_table_create(dict_vendor_name_hash,
1155 dict_vendor_name_cmp,
1157 if (!vendors_byname) {
1162 * Create the table of vendors by value. There MAY
1163 * be vendors of the same value. If there are, we
1164 * pick the latest one.
1166 vendors_byvalue = lrad_hash_table_create(dict_vendor_value_hash,
1167 dict_vendor_value_cmp,
1169 if (!vendors_byvalue) {
1174 * Create the table of attributes by name. There MAY NOT
1175 * be multiple attributes of the same name.
1177 * Each attribute is malloc'd, so the free function is free.
1179 attributes_byname = lrad_hash_table_create(dict_attr_name_hash,
1182 if (!attributes_byname) {
1187 * Create the table of attributes by value. There MAY
1188 * be attributes of the same value. If there are, we
1189 * pick the latest one.
1191 attributes_byvalue = lrad_hash_table_create(dict_attr_value_hash,
1192 dict_attr_value_cmp,
1194 if (!attributes_byvalue) {
1198 values_byname = lrad_hash_table_create(dict_value_name_hash,
1199 dict_value_name_cmp,
1201 if (!values_byname) {
1205 values_byvalue = lrad_hash_table_create(dict_value_value_hash,
1206 dict_value_value_cmp,
1208 if (!values_byvalue) {
1212 value_fixup = NULL; /* just to be safe. */
1214 if (my_dict_init(dir, fn, NULL, 0) < 0)
1219 value_fixup_t *this, *next;
1221 for (this = value_fixup; this != NULL; this = next) {
1224 a = dict_attrbyname(this->attrstr);
1227 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1228 this->attrstr, this->dval->name);
1229 return -1; /* leak, but they should die... */
1232 this->dval->attr = a->attr;
1235 * Add the value into the dictionary.
1237 if (!lrad_hash_table_replace(values_byname,
1239 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1244 * Allow them to use the old name, but
1245 * prefer the new name when printing
1248 if (!lrad_hash_table_finddata(values_byvalue, this->dval)) {
1249 lrad_hash_table_replace(values_byvalue,
1255 * Just so we don't lose track of things.
1262 * Walk over all of the hash tables to ensure they're
1263 * initialized. We do this because the threads may perform
1264 * lookups, and we don't want multi-threaded re-ordering
1265 * of the table entries. That would be bad.
1267 lrad_hash_table_walk(vendors_byname, null_callback, NULL);
1268 lrad_hash_table_walk(vendors_byvalue, null_callback, NULL);
1270 lrad_hash_table_walk(attributes_byname, null_callback, NULL);
1271 lrad_hash_table_walk(attributes_byvalue, null_callback, NULL);
1273 lrad_hash_table_walk(values_byvalue, null_callback, NULL);
1274 lrad_hash_table_walk(values_byname, null_callback, NULL);
1280 * Get an attribute by its numerical value.
1282 DICT_ATTR *dict_attrbyvalue(int attr)
1288 return lrad_hash_table_finddata(attributes_byvalue, &dattr);
1292 * Get an attribute by its name.
1294 DICT_ATTR *dict_attrbyname(const char *name)
1298 if (!name) return NULL;
1300 strNcpy(dattr.name, name, sizeof(dattr.name));
1302 return lrad_hash_table_finddata(attributes_byname, &dattr);
1306 * Associate a value with an attribute and return it.
1308 DICT_VALUE *dict_valbyattr(int attr, int value)
1315 return lrad_hash_table_finddata(values_byvalue, &dval);
1319 * Get a value by its name, keyed off of an attribute.
1321 DICT_VALUE *dict_valbyname(int attr, const char *name)
1324 uint32_t buffer[(sizeof(*dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1326 if (!name) return NULL;
1328 dv = (DICT_VALUE *) buffer;
1330 strNcpy(dv->name, name, DICT_VALUE_MAX_NAME_LEN);
1332 return lrad_hash_table_finddata(values_byname, dv);
1336 * Get the vendor PEC based on the vendor name
1338 * This is efficient only for small numbers of vendors.
1340 int dict_vendorbyname(const char *name)
1343 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1345 if (!name) return 0;
1347 dv = (DICT_VENDOR *) buffer;
1348 strNcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN);
1350 dv = lrad_hash_table_finddata(vendors_byname, dv);
1353 return dv->vendorpec;
1357 * Return the vendor struct based on the PEC.
1359 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1363 dv.vendorpec = vendorpec;
1365 return lrad_hash_table_finddata(vendors_byvalue, &dv);