Ensure that the directory name always ends with /
[freeradius.git] / src / lib / dict.c
index d5cae2d..a3ea796 100644 (file)
@@ -44,12 +44,16 @@ RCSID("$Id$")
 #define DICT_VENDOR_MAX_NAME_LEN (128)
 #define DICT_ATTR_MAX_NAME_LEN (128)
 
+#define DICT_ATTR_SIZE sizeof(DICT_ATTR) + DICT_ATTR_MAX_NAME_LEN
+
 static fr_hash_table_t *vendors_byname = NULL;
 static fr_hash_table_t *vendors_byvalue = NULL;
 
 static fr_hash_table_t *attributes_byname = NULL;
 static fr_hash_table_t *attributes_byvalue = NULL;
 
+static fr_hash_table_t *attributes_combo = NULL;
+
 static fr_hash_table_t *values_byvalue = NULL;
 static fr_hash_table_t *values_byname = NULL;
 
@@ -108,17 +112,31 @@ const FR_NAME_NUMBER dict_attr_types[] = {
        { "int32",      PW_TYPE_SIGNED },
        { "integer64",  PW_TYPE_INTEGER64 },
        { "uint64",     PW_TYPE_INTEGER64 },
+       { "ipv4prefix", PW_TYPE_IPV4PREFIX },
+       { "vsa",        PW_TYPE_VSA },
        { NULL, 0 }
 };
 
 
 /*
- *     WiMAX craziness.
+ *     For packing multiple TLV numbers into one 32-bit integer.  The
+ *     first 3 bytes are just the 8-bit number.  The next two are
+ *     more limited.  We only allow 31 attributes nested 3 layers
+ *     deep, and only 7 nested 4 layers deep.  This should be
+ *     sufficient for most purposes.
+ *
+ *     For TLVs and extended attributes, we packet the base attribute
+ *     number into the upper 8 bits of the "vendor" field.
+ *
+ *     e.g.    OID             attribute       vendor
+ *             241.1           1               (241 << 8)
+ *             241.26.9.1      1               (241 << 8) | (9)
+ *             241.1.2         1 | (2 << 8)    (241 << 8)
  */
 #define MAX_TLV_NEST (4)
 /*
  *     Bit packing:
- *     8 bits of base VSA
+ *     8 bits of base attribute
  *     8 bits for nested TLV 1
  *     8 bits for nested TLV 2
  *     5 bits for nested TLV 3
@@ -195,6 +213,30 @@ static int dict_attr_value_cmp(const void *one, const void *two)
        return a->attr - b->attr;
 }
 
+static uint32_t dict_attr_combo_hash(const void *data)
+{
+       uint32_t hash;
+       const DICT_ATTR *attr = data;
+
+       hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
+       hash = fr_hash_update(&attr->type, sizeof(attr->type), hash);
+       return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
+}
+
+static int dict_attr_combo_cmp(const void *one, const void *two)
+{
+       const DICT_ATTR *a = one;
+       const DICT_ATTR *b = two;
+
+       if (a->type < b->type) return -1;
+       if (a->type > b->type) return +1;
+
+       if (a->vendor < b->vendor) return -1;
+       if (a->vendor > b->vendor) return +1;
+
+       return a->attr - b->attr;
+}
+
 static uint32_t dict_vendor_name_hash(const void *data)
 {
        return dict_hashname(((const DICT_VENDOR *)data)->name);
@@ -443,8 +485,10 @@ void dict_free(void)
 
        fr_hash_table_free(attributes_byname);
        fr_hash_table_free(attributes_byvalue);
+       fr_hash_table_free(attributes_combo);
        attributes_byname = NULL;
        attributes_byvalue = NULL;
+       attributes_combo = NULL;
 
        fr_hash_table_free(values_byname);
        fr_hash_table_free(values_byvalue);
@@ -458,7 +502,6 @@ void dict_free(void)
        dict_stat_free();
 }
 
-
 /*
  *     Add vendor to the list.
  */
@@ -467,7 +510,7 @@ int dict_addvendor(const char *name, unsigned int value)
        size_t length;
        DICT_VENDOR *dv;
 
-       if (value > FR_MAX_VENDOR) {
+       if (value >= FR_MAX_VENDOR) {
                fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
                return -1;
        }
@@ -533,8 +576,9 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
        size_t namelen;
        static int      max_attr = 0;
        const char      *p;
-       DICT_ATTR       *da;
-
+       const DICT_ATTR *da;
+       DICT_ATTR *n;
+       
        namelen = strlen(name);
        if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
                fr_strerror_printf("dict_addattr: attribute name too long");
@@ -595,8 +639,6 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
                        fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
                        return -1;
                }
-               if (!vendor) vendor = VENDORPEC_EXTENDED;
-
                if (flags.has_tag
 #ifdef WITH_DHCP
                    || flags.array
@@ -613,7 +655,8 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
                        return -1;
                }
 
-               if (vendor <= FR_MAX_VENDOR) {
+               /* VSAs cannot be of format EVS */
+               if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
                        fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
                        return -1;
                }
@@ -629,7 +672,7 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
                return -1;
        }
 
-       if (vendor && (vendor != VENDORPEC_EXTENDED)) {
+       if ((vendor & (FR_MAX_VENDOR -1)) != 0) {
                DICT_VENDOR *dv;
                static DICT_VENDOR *last_vendor = NULL;
 
@@ -670,7 +713,7 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
                 */
                if (!dv) {
                        fr_strerror_printf("dict_addattr: Unknown vendor %u",
-                                          vendor);
+                                          vendor & (FR_MAX_VENDOR - 1));
                        return -1;
                }
 
@@ -684,22 +727,46 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
                } /* else 256..65535 are allowed */
 
                /*
-                *      Set the extended flags as appropriate.
+                *      If the attribute is in the standard space, AND
+                *      has a sub-type (e.g. 241.1 or 255.3), then its
+                *      number is placed into the upper 8 bits of the
+                *      vendor field.
+                *
+                *      This also happens for the new VSAs.
+                *
+                *      If we find it, then set the various flags
+                *      based on what we see.
                 */
-               if (vendor > FR_MAX_VENDOR) {
-                       unsigned int myattr;
+               if (vendor >= FR_MAX_VENDOR) {
+                       unsigned int parent;
 
-                       myattr = (vendor >> 24) & 0xff;
-                       myattr |= PW_VENDOR_SPECIFIC << 8;
+                       parent = (vendor / FR_MAX_VENDOR) & 0xff;
 
-                       da = dict_attrbyvalue(myattr, VENDORPEC_EXTENDED);
+                       da = dict_attrbyvalue(parent, 0);
                        if (!da) {
-                               fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown \"evs\" type.");
+                               fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown parent attribute %u.", parent);
                                return -1;
                        }
+
+                       /*
+                        *      These flags are inhereited inherited
+                        *      from the parent.
+                        */
                        flags.extended = da->flags.extended;
                        flags.long_extended = da->flags.long_extended;
-                       flags.evs = da->flags.evs;
+
+                       /*
+                        *      Non-extended attributes can't have VSAs.
+                        */
+                       if (!flags.extended &&
+                           ((vendor & (FR_MAX_VENDOR - 1)) != 0)) {
+                               fr_strerror_printf("dict_addattr: ATTRIBUTE cannot be a VSA");
+                               return -1;
+                       }
+
+                       if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
+                               flags.evs = 1;
+                       }
                }
 
                /*
@@ -715,33 +782,34 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
        /*
         *      Create a new attribute for the list
         */
-       if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
-               fr_strerror_printf("dict_addattr: out of memory");
+       if ((n = fr_pool_alloc(sizeof(*n) + namelen)) == NULL) {
+       oom:
+               fr_strerror_printf("dict_adnttr: out of memory");
                return -1;
        }
 
-       memcpy(da->name, name, namelen);
-       da->name[namelen] = '\0';
-       da->attr = attr;
-       da->vendor = vendor;
-       da->type = type;
-       da->flags = flags;
+       memcpy(n->name, name, namelen);
+       n->name[namelen] = '\0';
+       n->attr = attr;
+       n->vendor = vendor;
+       n->type = type;
+       n->flags = flags;
 
        /*
         *      Insert the attribute, only if it's not a duplicate.
         */
-       if (!fr_hash_table_insert(attributes_byname, da)) {
+       if (!fr_hash_table_insert(attributes_byname, n)) {
                DICT_ATTR       *a;
 
                /*
                 *      If the attribute has identical number, then
                 *      ignore the duplicate.
                 */
-               a = fr_hash_table_finddata(attributes_byname, da);
-               if (a && (strcasecmp(a->name, da->name) == 0)) {
-                       if (a->attr != da->attr) {
-                               fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
-                               fr_pool_free(da);
+               a = fr_hash_table_finddata(attributes_byname, n);
+               if (a && (strcasecmp(a->name, n->name) == 0)) {
+                       if (a->attr != n->attr) {
+                               fr_strerror_printf("dict_adnttr: Duplicate attribute name %s", name);
+                               fr_pool_free(n);
                                return -1;
                        }
 
@@ -756,9 +824,9 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
 
                fr_hash_table_delete(attributes_byvalue, a);
 
-               if (!fr_hash_table_replace(attributes_byname, da)) {
-                       fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
-                       fr_pool_free(da);
+               if (!fr_hash_table_replace(attributes_byname, n)) {
+                       fr_strerror_printf("dict_adnttr: Internal error storing attribute %s", name);
+                       fr_pool_free(n);
                        return -1;
                }
        }
@@ -772,13 +840,48 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
         *      files, but when we're printing them, (and looking up
         *      by value) we want to use the NEW name.
         */
-       if (!fr_hash_table_replace(attributes_byvalue, da)) {
-               fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
+       if (!fr_hash_table_replace(attributes_byvalue, n)) {
+               fr_strerror_printf("dict_adnttr: Failed inserting attribute name %s", name);
                return -1;
        }
 
+       /*
+        *      Hacks for combo-IP
+        */
+       if (n->type == PW_TYPE_COMBO_IP) {
+               DICT_ATTR *v4, *v6;
+
+               v4 = fr_pool_alloc(sizeof(*v4));
+               if (!v4) goto oom;
+
+               v6 = fr_pool_alloc(sizeof(*v6));
+               if (!v6) {
+                       free(v4);
+                       goto oom;
+               }
+
+               memcpy(v4, n, sizeof(*v4));
+               v4->type = PW_TYPE_IPADDR;
+
+               memcpy(v6, n, sizeof(*v6));
+               v6->type = PW_TYPE_IPV6ADDR;
+
+               if (!fr_hash_table_insert(attributes_combo, v4)) {
+                       fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv4", name);
+                       free(v4);
+                       free(v6);
+                       return -1;
+               }
+
+               if (!fr_hash_table_insert(attributes_combo, v6)) {
+                       fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv6", name);
+                       free(v6);
+                       return -1;
+               }
+       }
+
        if (!vendor && (attr > 0) && (attr < 256)) {
-                dict_base_attrs[attr] = da;
+                dict_base_attrs[attr] = n;
        }
 
        return 0;
@@ -791,10 +894,10 @@ int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
 int dict_addvalue(const char *namestr, const char *attrstr, int value)
 {
        size_t          length;
-       DICT_ATTR       *dattr;
+       const DICT_ATTR *dattr;
        DICT_VALUE      *dval;
 
-       static DICT_ATTR *last_attr = NULL;
+       static const DICT_ATTR *last_attr = NULL;
 
        if (!*namestr) {
                fr_strerror_printf("dict_addvalue: empty names are not permitted");
@@ -877,8 +980,6 @@ int dict_addvalue(const char *namestr, const char *attrstr, int value)
                                           fr_int2str(dict_attr_types, dattr->type, "?Unknown?"));
                                return -1;
                }
-
-               dattr->flags.has_value = 1;
        } else {
                value_fixup_t *fixup;
 
@@ -905,25 +1006,30 @@ int dict_addvalue(const char *namestr, const char *attrstr, int value)
        /*
         *      Add the value into the dictionary.
         */
-       if (!fr_hash_table_insert(values_byname, dval)) {
-               if (dattr) {
-                       DICT_VALUE *old;
+       {
+               DICT_ATTR *tmp;
+               memcpy(&tmp, &dval, sizeof(tmp));
+               
+               if (!fr_hash_table_insert(values_byname, tmp)) {
+                       if (dattr) {
+                               DICT_VALUE *old;
 
-                       /*
-                        *      Suppress duplicates with the same
-                        *      name and value.  There are lots in
-                        *      dictionary.ascend.
-                        */
-                       old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
-                       if (old && (old->value == dval->value)) {
-                               fr_pool_free(dval);
-                               return 0;
+                               /*
+                                *      Suppress duplicates with the same
+                                *      name and value.  There are lots in
+                                *      dictionary.ascend.
+                                */
+                               old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
+                               if (old && (old->value == dval->value)) {
+                                       fr_pool_free(dval);
+                                       return 0;
+                               }
                        }
-               }
 
-               fr_pool_free(dval);
-               fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
-               return -1;
+                       fr_pool_free(dval);
+                       fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
+                       return -1;
+               }
        }
 
        /*
@@ -971,43 +1077,119 @@ static int sscanf_i(const char *str, unsigned int *pvalue)
 }
 
 
-int dict_str2oid(const char *ptr, unsigned int *pvalue, int vendor, int tlv_depth)
+/*
+ *     Get the OID based on various pieces of information.
+ *
+ *     Remember, the packing format is weird.
+ *
+ *     00VID   000000AA        normal VSA for vendor VID
+ *     00VID   AABBCCDD        normal VSAs with TLVs
+ *     EE000   000000AA        extended attr (241.1)
+ *     EE000   AABBCCDD        extended attr with TLVs
+ *     EEVID   000000AA        EVS with vendor VID, attr AAA
+ *     EEVID   AABBCCDD        EVS with TLVs
+ *
+ *     <whew>!  Are we crazy, or what?
+ */
+int dict_str2oid(const char *ptr, unsigned int *pvalue, unsigned int *pvendor,
+                int tlv_depth)
 {
        const char *p;
        unsigned int value;
-       DICT_ATTR *da;
+       const DICT_ATTR *da = NULL;
 
        if (tlv_depth > fr_attr_max_tlv) {
-               fr_strerror_printf("Attribute has too long OID");
-               return 0;
+               fr_strerror_printf("Too many sub-attributes");
+               return -1;
        }
 
+       /*
+        *      If *pvalue is set, check if the attribute exists.
+        *      Otherwise, check that the vendor exists.
+        */
        if (*pvalue) {
-               if (vendor) {
-                       da = dict_attrbyvalue(*pvalue, vendor);
-               } else {
-                       da = dict_attrbyvalue(*pvalue, VENDORPEC_EXTENDED);
-               }
+               da = dict_attrbyvalue(*pvalue, *pvendor);
                if (!da) {
                        fr_strerror_printf("Parent attribute is undefined.");
-                       return 0;
+                       return -1;
                }
-       
-               if (!(da->flags.has_tlv || da->flags.extended || da->flags.long_extended)) {
-                       fr_strerror_printf("Parent attribute %s cannot have sub-tlvs",
+               
+               if (!da->flags.has_tlv && !da->flags.extended) {
+                       fr_strerror_printf("Parent attribute %s cannot have sub-attributes",
                                           da->name);
-                       return 0;
+                       return -1;
+               }
+
+       } else if ((*pvendor & (FR_MAX_VENDOR - 1)) != 0) {
+               if (!dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1))) {
+                       fr_strerror_printf("Unknown vendor %u",
+                                          *pvendor & (FR_MAX_VENDOR - 1));
+                       return -1;
                }
        }
 
        p = strchr(ptr, '.');
 
+       /*
+        *      Look for 26.VID.x.y
+        *
+        *      If we find it, re-write the parameters, and recurse.
+        */
+       if (!*pvendor && (tlv_depth == 0) && (*pvalue == PW_VENDOR_SPECIFIC)) {
+               const DICT_VENDOR *dv;
+
+               if (!p) {
+                       fr_strerror_printf("VSA needs to have sub-attribute");
+                       return -1;
+               }
+
+               if (!sscanf_i(ptr, pvendor)) {
+                       fr_strerror_printf("Invalid number in attribute");
+                       return -1;
+               }
+
+               if (*pvendor >= FR_MAX_VENDOR) {
+                       fr_strerror_printf("Cannot handle vendor ID larger than 2^24");
+                       
+                       return -1;
+               }
+
+               dv = dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1));
+               if (!dv) {
+                       fr_strerror_printf("Unknown vendor \"%u\" ",
+                                          *pvendor  & (FR_MAX_VENDOR - 1));
+                       return -1;
+               }
+
+               /*
+                *      Start off with (attr=0, vendor=VID), and
+                *      recurse.  This causes the various checks above
+                *      to be done.
+                */
+               *pvalue = 0;
+               return dict_str2oid(p + 1, pvalue, pvendor, 0);
+       }
+
        if (!sscanf_i(ptr, &value)) {
-               fr_strerror_printf("Failed parsing attribute identifier %s",
-                                  ptr);
-               return 0;
+               fr_strerror_printf("Invalid number in attribute");
+               return -1;
        }
 
+       if (!*pvendor && (tlv_depth == 1) && da &&
+           (da->flags.has_tlv || da->flags.extended)) {
+
+
+               *pvendor = *pvalue * FR_MAX_VENDOR;
+               *pvalue = value;
+
+               if (!p) return 0;
+               return dict_str2oid(p + 1, pvalue, pvendor, 1);
+       }
+
+       /*
+        *      And pack the data according to the scheme described in
+        *      the comments at the start of this function.
+        */
        if (*pvalue) {
                *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
        } else {
@@ -1015,19 +1197,36 @@ int dict_str2oid(const char *ptr, unsigned int *pvalue, int vendor, int tlv_dept
        }
 
        if (p) {
-               return dict_str2oid(p + 1, pvalue, vendor, tlv_depth + 1);
+               return dict_str2oid(p + 1, pvalue, pvendor, tlv_depth + 1);
        }
 
        return tlv_depth;
 }
 
+/*
+ *     Bamboo skewers under the fingernails in 5, 4, 3, 2, ...
+ */
+static const DICT_ATTR *dict_parent(unsigned int attr, unsigned int vendor)
+{
+       if (vendor < FR_MAX_VENDOR) {
+               return dict_attrbyvalue(attr & 0xff, vendor);
+       }
+
+       if (attr < 256) {
+               return dict_attrbyvalue((vendor / FR_MAX_VENDOR) & 0xff, 0);
+       }
+
+       return dict_attrbyvalue(attr & 0xff, vendor);
+}
+
 
 /*
  *     Process the ATTRIBUTE command
  */
 static int process_attribute(const char* fn, const int line,
-                            const unsigned int block_vendor, DICT_ATTR *block_tlv,
-                            int tlv_depth, char **argv, int argc)
+                            unsigned int block_vendor,
+                            const DICT_ATTR *block_tlv, int tlv_depth,
+                            char **argv, int argc)
 {
        int             oid = 0;
        unsigned int    vendor = 0;
@@ -1043,6 +1242,9 @@ static int process_attribute(const char* fn, const int line,
                return -1;
        }
 
+       /*
+        *      Dictionaries need to have real names, not shitty ones.
+        */
        if (strncmp(argv[1], "Attr-", 5) == 0) {
                fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
                                   fn, line);
@@ -1052,13 +1254,10 @@ static int process_attribute(const char* fn, const int line,
        memset(&flags, 0, sizeof(flags));
 
        /*
-        *      Look for extended attributes before doing anything else.
+        *      Look for OIDs before doing anything else.
         */
        p = strchr(argv[1], '.');
-       if (p) {
-               *p = '\0';
-               oid = 1;
-       }
+       if (p) oid = 1;
 
        /*
         *      Validate all entries
@@ -1068,71 +1267,15 @@ static int process_attribute(const char* fn, const int line,
                return -1;
        }
 
-       if (!p) {
-               if (value > (1 << 24)) {
-                       fr_strerror_printf("dict_init: %s[%d]: Attribute number is too large", fn, line);
-                       return -1;
-               }
-
-               
-               /*
-                *      Parse NUM.NUM.NUM.NUM
-                */
-       } else {
-               int my_depth;
-               DICT_ATTR *da;
-
-               *p = '.';       /* reset for later printing */
-
-               if (block_vendor) {
-                       da = dict_attrbyvalue(value, block_vendor);
-               } else if (value == PW_VENDOR_SPECIFIC) {
-                       char *q;
-                       const DICT_VENDOR *dv;
-
-                       p++;
-                       q = strchr(p, '.');
-                       if (!q) {
-                       invalid:
-                               fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier", fn, line);
-                               return -1;
-                       }
-                       *q = '\0';
+       if (oid) {
+               const DICT_ATTR *da;
 
-                       if (!sscanf_i(p, &vendor)) {
-                               *q = '.';
-                               goto invalid;
-                       }
-                       *(q++) = '.';
-                       p = q;
-                       dv = dict_vendorbyvalue(vendor);
-                       if (!dv) {
-                               fr_strerror_printf("dict_init: %s[%d]: Unknown vendor \"%u\" in attribute identifier", fn, line, vendor);
-                               return -1;
-                       }
-
-                       argv[1] = p;
-                       return process_attribute(fn, line, vendor, NULL, 0,
-                                                argv, argc);
-               } else {
-                       da = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
-               }
-               if (!da) {
-                       fr_strerror_printf("dict_init: %s[%d]: Entry refers to unknown attribute %d", fn, line, value);
-                       return -1;
-               }
+               vendor = block_vendor;
 
                /*
-                *      241.1 means 241 is of type "extended".
-                *      Otherwise, die.
+                *      Parse the rest of the OID.
                 */
-               if (!(da->flags.has_tlv || da->flags.extended || da->flags.long_extended)) {
-                       fr_strerror_printf("dict_init: %s[%d]: Parent attribute %s cannot contain sub-attributes", fn, line, da->name);
-                       return -1;
-               }
-
-               my_depth = dict_str2oid(p + 1, &value, block_vendor, tlv_depth + 1);
-               if (!my_depth) {
+               if (dict_str2oid(p + 1, &value, &vendor, tlv_depth + 1) < 0) {
                        char buffer[256];
 
                        strlcpy(buffer, fr_strerror(), sizeof(buffer));
@@ -1140,25 +1283,17 @@ static int process_attribute(const char* fn, const int line,
                        fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
                        return -1;
                }
+               block_vendor = vendor;
 
                /*
-                *      Look up the REAL parent TLV.
+                *      Set the flags based on the parents flags.
                 */
-               if (my_depth > 1) {
-                       unsigned int parent = value;
-
-                       parent &= ~(fr_attr_mask[my_depth] << fr_attr_shift[my_depth]);
-
-                       da = dict_attrbyvalue(parent, da->vendor);
-                       if (!da) {
-                               fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
-                               return -1;
-                       }
+               da = dict_parent(value, vendor);
+               if (!da) {
+                       fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
+                       return -1;
                }
-               
-               /*
-                *      Set which type of attribute this is.
-                */
+
                flags.extended = da->flags.extended;
                flags.long_extended = da->flags.long_extended;
                flags.evs = da->flags.evs;
@@ -1243,7 +1378,6 @@ static int process_attribute(const char* fn, const int line,
                                fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended\" MUST be RFC attributes with value >= 241.", fn, line);
                                return -1;
                        }
-                       type = PW_TYPE_OCTETS;
                        flags.extended = 1;
                        break;
 
@@ -1252,14 +1386,14 @@ static int process_attribute(const char* fn, const int line,
                                fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"long-extended\" MUST be RFC attributes with value >= 241.", fn, line);
                                return -1;
                        }
-                       type = PW_TYPE_OCTETS;
+                       flags.extended = 1;
                        flags.long_extended = 1;
                        break;
 
                case PW_TYPE_EVS:
-                       type = PW_TYPE_OCTETS;
+                       flags.extended = 1;
                        flags.evs = 1;
-                       if (((value >> fr_attr_shift[1]) & fr_attr_mask[1]) != PW_VENDOR_SPECIFIC) {
+                       if (value != PW_VENDOR_SPECIFIC) {
                                fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
                                return -1;
                        }
@@ -1277,7 +1411,7 @@ static int process_attribute(const char* fn, const int line,
                /*
                 *      Keep it real.
                 */
-               if (flags.extended || flags.long_extended || flags.evs) {
+               if (flags.extended) {
                        fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
                        return -1;
                }
@@ -1507,7 +1641,7 @@ static int process_value(const char* fn, const int line, char **argv,
 static int process_value_alias(const char* fn, const int line, char **argv,
                               int argc)
 {
-       DICT_ATTR *my_da, *da;
+       const DICT_ATTR *my_da, *da;
        DICT_VALUE *dval;
 
        if (argc != 2) {
@@ -1523,12 +1657,6 @@ static int process_value_alias(const char* fn, const int line, char **argv,
                return -1;
        }
 
-       if (my_da->flags.has_value) {
-               fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
-                          fn, line, argv[0]);
-               return -1;
-       }
-
        if (my_da->flags.has_value_alias) {
                fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
                           fn, line, argv[0]);
@@ -1542,12 +1670,6 @@ static int process_value_alias(const char* fn, const int line, char **argv,
                return -1;
        }
 
-       if (!da->flags.has_value) {
-               fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
-                          fn, line, argv[1]);
-               return -1;
-       }
-
        if (da->flags.has_value_alias) {
                fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
                           fn, line, argv[1]);
@@ -1661,6 +1783,12 @@ static int process_vendor(const char* fn, const int line, char **argv,
                length = (int) (p[2] - '0');
 
                if (p[3] == ',') {
+                       if (!p[4]) {
+                               fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
+                                  fn, line, p);
+                               return -1;
+                       }
+
                        if ((p[4] != 'c') ||
                            (p[5] != '\0')) {
                                fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
@@ -1748,11 +1876,11 @@ int str2argv(char *str, char **argv, int max_argc)
 /*
  *     Initialize the dictionary.
  */
-static int my_dict_init(const char *dir, const char *fn,
+static int my_dict_init(const char *parent, const char *filename,
                        const char *src_file, int src_line)
 {
        FILE    *fp;
-       char    dirtmp[256];
+       char    dir[256], fn[256];
        char    buf[256];
        char    *p;
        int     line = 0;
@@ -1761,7 +1889,7 @@ static int my_dict_init(const char *dir, const char *fn,
        struct stat statbuf;
        char    *argv[MAX_ARGV];
        int     argc;
-       DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
+       const DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
        int     which_block_tlv = 0;
 
        block_tlv[0] = NULL;
@@ -1769,23 +1897,51 @@ static int my_dict_init(const char *dir, const char *fn,
        block_tlv[2] = NULL;
        block_tlv[3] = NULL;
 
-       if (strlen(fn) >= sizeof(dirtmp) / 2 ||
-           strlen(dir) >= sizeof(dirtmp) / 2) {
+       if ((strlen(parent) + 3 + strlen(filename)) > sizeof(dir)) {
                fr_strerror_printf("dict_init: filename name too long");
                return -1;
        }
 
        /*
-        *      First see if fn is relative to dir. If so, create
-        *      new filename. If not, remember the absolute dir.
+        *      If it's an absolute dir, forget the parent dir,
+        *      and remember the new one.
+        *
+        *      If it's a relative dir, tack on the current filename
+        *      to the parent dir.  And use that.
         */
-       if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
-               strcpy(dirtmp, fn);
-               dirtmp[p - fn] = 0;
-               dir = dirtmp;
-       } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
-               snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
-               fn = dirtmp;
+       if (!FR_DIR_IS_RELATIVE(filename)) {
+               strlcpy(dir, filename, sizeof(dir));
+               p = strrchr(dir, FR_DIR_SEP);
+               if (p) {
+                       p[1] = '\0';
+               } else {
+                       strlcat(dir, "/", sizeof(dir));
+               }
+
+               strlcpy(fn, filename, sizeof(fn));
+       } else {
+               strlcpy(dir, parent, sizeof(dir));
+               p = strrchr(dir, FR_DIR_SEP);
+               if (p) {
+                       if (p[1]) strlcat(dir, "/", sizeof(dir));
+               } else {
+                       strlcat(dir, "/", sizeof(dir));
+               }
+               strlcat(dir, filename, sizeof(dir));
+               p = strrchr(dir, FR_DIR_SEP);
+               if (p) {
+                       p[1] = '\0';
+               } else {
+                       strlcat(dir, "/", sizeof(dir));
+               }
+
+               p = strrchr(filename, FR_DIR_SEP);
+               if (p) {
+                       snprintf(fn, sizeof(fn), "%s%s", dir, p);
+               } else {
+                       snprintf(fn, sizeof(fn), "%s%s", dir, filename);
+               }
+
        }
 
        if ((fp = fopen(fn, "r")) == NULL) {
@@ -2015,6 +2171,8 @@ static int my_dict_init(const char *dir, const char *fn,
 
                        /*
                         *      Check for extended attr VSAs
+                        *
+                        *      BEGIN-VENDOR foo format=Foo-Encapsulation-Attr
                         */
                        if (argc > 2) {
                                if (strncmp(argv[2], "format=", 7) != 0) {
@@ -2042,11 +2200,11 @@ static int my_dict_init(const char *dir, const char *fn,
                                }
                                
                                /*
-                                *      Pack the encapsulating attribute
-                                *      into the vendor Id.  This attribute
-                                *      MUST be >= 241.
+                                *      Pack the encapsulating
+                                *      attribute into the upper 8
+                                *      bits of the vendor ID
                                 */
-                               block_vendor |= (da->attr & fr_attr_mask[1]) * FR_MAX_VENDOR;
+                               block_vendor |= (da->attr & fr_attr_mask[0]) * FR_MAX_VENDOR;
                        }
 
                        continue;
@@ -2177,6 +2335,16 @@ int dict_init(const char *dir, const char *fn)
                return -1;
        }
 
+       /*
+        *      Horrible hacks for combo-IP.
+        */
+       attributes_combo = fr_hash_table_create(dict_attr_combo_hash,
+                                               dict_attr_combo_cmp,
+                                               fr_pool_free);
+       if (!attributes_combo) {
+               return -1;
+       }
+
        values_byname = fr_hash_table_create(dict_value_name_hash,
                                               dict_value_name_cmp,
                                               fr_pool_free);
@@ -2197,7 +2365,7 @@ int dict_init(const char *dir, const char *fn)
                return -1;
 
        if (value_fixup) {
-               DICT_ATTR *a;
+               const DICT_ATTR *a;
                value_fixup_t *this, *next;
 
                for (this = value_fixup; this != NULL; this = next) {
@@ -2258,10 +2426,390 @@ int dict_init(const char *dir, const char *fn)
        return 0;
 }
 
+static size_t print_attr_oid(char *buffer, size_t size, unsigned int attr,
+                            int dv_type)
+{
+       int nest;
+       size_t outlen;
+       size_t len;
+
+       switch (dv_type) {
+       case 4:
+               return snprintf(buffer, size, "%u", attr);
+
+       case 2:
+               return snprintf(buffer, size, "%u", attr & 0xffff);
+
+       default:
+       case 1:
+               len = snprintf(buffer, size, "%u", attr & 0xff);
+               break;
+       }
+
+       if ((attr >> 8) == 0) return len;
+
+       outlen = len;
+       buffer += len;
+       size -= len;
+
+       for (nest = 1; nest <= fr_attr_max_tlv; nest++) {
+               if (((attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]) == 0) break;
+
+               len = snprintf(buffer, size, ".%u",
+                              (attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]);
+
+               outlen = len;
+               buffer += len;
+               size -= len;
+       }
+
+       return outlen;
+}
+
+/** Free dynamically allocated (unknown attributes)
+ * 
+ * If the da was dynamically allocated it will be freed, else the function
+ * will return without doing anything.
+ *
+ * @param da to free.
+ */
+void dict_attr_free(DICT_ATTR const **da)
+{
+       DICT_ATTR **tmp;
+       
+       if (!da || !*da) return;
+       
+       /* Don't free real DAs */
+       if (!(*da)->flags.is_unknown) {
+               return;
+       }
+       
+       memcpy(&tmp, &da, sizeof(*tmp));
+       free(*tmp);
+       
+       *tmp = NULL;    
+}
+
+/** Copies a dictionary attr
+ *
+ * If the attr is dynamically allocated (unknown attribute), then it will be
+ * copied to a new attr.
+ *
+ * If the attr is known, a pointer to the da will be returned.
+ *
+ * @param da to copy.
+ * @param vp_free if TRUE, da will be freed at the same time as the
+ *     VALUE_PAIR which contains it.
+ * @return return a copy of the da.
+ */
+const DICT_ATTR *dict_attr_copy(const DICT_ATTR *da, int vp_free)
+{
+       DICT_ATTR *copy;
+       
+       if (!da) return NULL;
+       
+       if (!da->flags.is_unknown) {
+               return da;
+       }
+       
+       copy = malloc(DICT_ATTR_SIZE);
+       if (!copy) {
+               fr_strerror_printf("Out of memory");
+               return NULL;
+       }
+       
+       memcpy(copy, da, DICT_ATTR_SIZE);
+       copy->flags.vp_free = (vp_free != 0);
+       
+       return copy;
+}
+
+
+/** Allocs an dictionary attr for unknown attributes
+ *
+ * Allocates a dict attr for an unknown attribute/vendor/type
+ * without adding it to dictionary pools/hashes.
+ *
+ * @note Must be freed with dict_attr_free if not used as part of a valuepair.
+ *
+ * @param[in] attr number.
+ * @param[in] vendor number.
+ * @param[in] vp_free if > 0 DICT_ATTR will be freed on VALUE_PAIR free.
+ * @return new dictionary attribute.
+ */
+const DICT_ATTR *dict_attrunknown(unsigned int attr, unsigned int vendor,
+                                 int vp_free)
+{
+       DICT_ATTR *da;
+       char *p;
+       int dv_type = 1;
+       size_t len = 0;
+       size_t bufsize = DICT_ATTR_MAX_NAME_LEN;
+       
+       da = malloc(DICT_ATTR_SIZE);
+       if (!da) {
+               fr_strerror_printf("Out of memory");
+               return NULL;
+       }
+       memset(da, 0, DICT_ATTR_SIZE);
+       
+       da->attr = attr;
+       da->vendor = vendor;
+       da->type = PW_TYPE_OCTETS;
+       da->flags.is_unknown = TRUE;
+       da->flags.vp_free = (vp_free != 0);
+       
+       p = da->name;
+       
+       len = snprintf(p, bufsize, "Attr-");
+       p += len;
+       bufsize -= len;
+
+       if (vendor > FR_MAX_VENDOR) {
+               len = snprintf(p, bufsize, "%u.", vendor / FR_MAX_VENDOR);
+               p += len;
+               bufsize -= len;
+               vendor &= (FR_MAX_VENDOR) - 1;
+       }
+
+       if (vendor) {
+               DICT_VENDOR *dv;
+
+               /*
+                *      dv_type is the length of the vendor's type field
+                *      RFC 2865 never defined a mandatory length, so
+                *      different vendors have different length type fields.
+                */
+               dv = dict_vendorbyvalue(vendor);
+               if (dv) {
+                       dv_type = dv->type;
+               }
+               len = snprintf(p, bufsize, "26.%u.", vendor);
+               
+               p += len;
+               bufsize -= len;
+       }
+
+       p += print_attr_oid(p, bufsize , attr, dv_type);
+       
+       return da;
+}
+
+/** Create a DICT_ATTR from an ASCII attribute and value
+ *
+ * Where the attribute name is in the form:
+ *  - Attr-%d
+ *  - Attr-%d.%d.%d...
+ *  - Vendor-%d-Attr-%d
+ *  - VendorName-Attr-%d
+ *
+ * @todo should check attr/vendor against dictionary and return the real da.
+ *
+ * @param[in] attribute name.
+ * @param[in] vp_free if > 0 DICT_ATTR will be freed on VALUE_PAIR free.
+ * @return new da or NULL on error.
+ */
+const DICT_ATTR *dict_attrunknownbyname(const char *attribute, int vp_free)
+{
+       unsigned int    attr, vendor = 0;
+       unsigned int    dv_type = 1;    /* The type of vendor field */
+
+       const char      *p = attribute;
+       char            *q;
+       
+       DICT_VENDOR     *dv;
+       const DICT_ATTR *da;
+
+       /*
+        *      Pull off vendor prefix first.
+        */
+       if (strncasecmp(p, "Attr-", 5) != 0) {
+               if (strncasecmp(p, "Vendor-", 7) == 0) {
+                       vendor = (int) strtol(p + 7, &q, 10);
+                       if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
+                               fr_strerror_printf("Invalid vendor value in "
+                                                  "attribute name \"%s\"", 
+                                                  attribute);
+                               return NULL;
+                       }
+
+                       p = q;
+
+               /* must be vendor name */
+               } else {
+                       char buffer[256];
+
+                       q = strchr(p, '-');
+
+                       if (!q) {
+                               fr_strerror_printf("Invalid vendor name in "
+                                                  "attribute name \"%s\"",
+                                                  attribute);
+                               return NULL;
+                       }
+
+                       if ((size_t) (q - p) >= sizeof(buffer)) {
+                               fr_strerror_printf("Vendor name too long "
+                                                  "in attribute name \"%s\"",
+                                                  attribute);
+                               return NULL;
+                       }
+
+                       memcpy(buffer, p, (q - p));
+                       buffer[q - p] = '\0';
+
+                       vendor = dict_vendorbyname(buffer);
+                       if (!vendor) {
+                               fr_strerror_printf("Unknown vendor name in "
+                                                  "attribute name \"%s\"",
+                                                  attribute);
+                               return NULL;
+                       }
+
+                       p = q;
+               }
+
+               if (*p != '-') {
+                       fr_strerror_printf("Invalid text following vendor "
+                                          "definition in attribute name "
+                                          "\"%s\"", attribute);
+                       return NULL;
+               }
+               p++;
+       }
+       
+       /*
+        *      Attr-%d
+        */
+       if (strncasecmp(p, "Attr-", 5) != 0) {
+               fr_strerror_printf("Invalid format in attribute name \"%s\"",
+                                  attribute);
+               return NULL;
+       }
+
+       attr = strtol(p + 5, &q, 10);
+
+       /*
+        *      Invalid attribute.
+        */
+       if (attr == 0) {
+               fr_strerror_printf("Invalid value in attribute name \"%s\"",
+                                  attribute);
+               return NULL;
+       }
+
+       p = q;
+       
+       /*
+        *      Vendor-%d-Attr-%d
+        *      VendorName-Attr-%d
+        *      Attr-%d
+        *      Attr-%d.
+        *
+        *      Anything else is invalid.
+        */
+       if (((vendor != 0) && (*p != '\0')) ||
+           ((vendor == 0) && *p && (*p != '.'))) {
+       invalid:
+               fr_strerror_printf("Invalid OID");
+               return NULL;
+       }
+       
+       /*
+        *      Look for OIDs.  Require the "Attr-26.Vendor-Id.type"
+        *      format, and disallow "Vendor-%d-Attr-%d" and
+        *      "VendorName-Attr-%d"
+        *
+        *      This section parses the Vendor-Id portion of
+        *      Attr-%d.%d.  where the first number is 26, *or* an
+        *      extended attribute of the "evs" data type.
+        */
+       if (*p == '.') {
+               da = dict_attrbyvalue(attr, 0);
+               if (!da) {
+                       fr_strerror_printf("Cannot parse attributes without "
+                                          "dictionaries");
+                       return NULL;
+               }               
+               
+               if ((attr != PW_VENDOR_SPECIFIC) &&
+                   !(da->flags.extended || da->flags.long_extended)) {
+                       fr_strerror_printf("Standard attributes cannot use "
+                                          "OIDs");
+                       return NULL;
+               }
+
+               if ((attr == PW_VENDOR_SPECIFIC) || da->flags.evs) {
+                       vendor = strtol(p + 1, &q, 10);
+                       if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
+                               fr_strerror_printf("Invalid vendor");
+                               return NULL;
+                       }
+
+                       if (*q != '.') goto invalid;
+
+                       p = q;
+
+                       if (da->flags.evs) {
+                               vendor |= attr * FR_MAX_VENDOR;
+                       }
+                       attr = 0;
+               } /* else the second number is a TLV number */
+       }
+
+       /*
+        *      Get the expected maximum size of the attribute.
+        */
+       if (vendor) {
+               dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
+               if (dv) {
+                       dv_type = dv->type;
+                       if (dv_type > 3) dv_type = 3; /* hack */
+               }
+       }
+       
+       /*
+        *      Parse the next number.  It could be a Vendor-Type
+        *      of 1..2^24, or it could be a TLV.
+        */
+       if (*p == '.') {
+               attr = strtol(p + 1, &q, 10);
+               if (attr == 0) {
+                       fr_strerror_printf("Invalid attribute number");
+                       return NULL;
+               }
+
+               if (*q) {
+                       if (*q != '.') {
+                               goto invalid;
+                       }
+                       
+                       if (dv_type != 1) {
+                               goto invalid;
+                       }
+               }
+
+               p = q;
+       }
+
+       /*
+        *      Enforce a maximum value on the attribute number.
+        */
+       if (attr >= (unsigned) (1 << (dv_type << 3))) goto invalid;
+
+       if (*p == '.') {
+               if (dict_str2oid(p + 1, &attr, &vendor, 1) < 0) {
+                       return NULL;
+               }
+       }
+
+       return dict_attrunknown(attr, vendor, vp_free);
+}
+
 /*
  *     Get an attribute by its numerical value.
  */
-DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
+const DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
 {
        DICT_ATTR dattr;
 
@@ -2273,10 +2821,134 @@ DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
        return fr_hash_table_finddata(attributes_byvalue, &dattr);
 }
 
+
+/**
+ * @brief Get an attribute by its numerical value. and data type
+ *
+ *     Used only for COMBO_IP
+ *
+ * @return The attribute, or NULL if not found
+ */
+const DICT_ATTR *dict_attrbytype(unsigned int attr, unsigned int vendor,
+                                PW_TYPE type)
+{
+       DICT_ATTR dattr;
+
+       dattr.attr = attr;
+       dattr.vendor = vendor;
+       dattr.type = type;
+
+       return fr_hash_table_finddata(attributes_combo, &dattr);
+}
+
+/**
+ * @brief Using a parent and attr/vendor, find a child attr/vendor
+ */
+int dict_attr_child(const DICT_ATTR *parent,
+                   unsigned int *pattr, unsigned int *pvendor)
+{
+       unsigned int attr, vendor;
+       DICT_ATTR dattr;
+
+       if (!parent || !pattr || !pvendor) return FALSE;
+
+       attr = *pattr;
+       vendor = *pvendor;
+
+       /*
+        *      Only some types can have children
+        */
+       switch (parent->type) {
+       default: return FALSE;
+
+       case PW_TYPE_VSA:
+       case PW_TYPE_TLV:
+       case PW_TYPE_EVS:
+       case PW_TYPE_EXTENDED:
+       case PW_TYPE_LONG_EXTENDED:
+         break;
+       }
+
+       if ((vendor == 0) && (parent->vendor != 0)) return FALSE;
+
+       /*
+        *      Bootstrap by starting off with the parents values.
+        */
+       dattr.attr = parent->attr;
+       dattr.vendor = parent->vendor;
+
+       /*
+        *      Do various butchery to insert the "attr" value.
+        *
+        *      00VID   000000AA        normal VSA for vendor VID
+        *      00VID   DDCCBBAA        normal VSAs with TLVs
+        *      EE000   000000AA        extended attr (241.1)
+        *      EE000   DDCCBBAA        extended attr with TLVs
+        *      EEVID   000000AA        EVS with vendor VID, attr AAA
+        *      EEVID   DDCCBBAA        EVS with TLVs
+        */
+       if (!dattr.vendor) {
+               dattr.vendor = parent->attr * FR_MAX_VENDOR;
+               dattr.vendor |= vendor;
+               dattr.attr = attr;
+
+       } else {
+               int i;
+
+               /*
+                *      Trying to nest too deep.  It's an error
+                */
+               if (parent->attr & (fr_attr_mask[MAX_TLV_NEST] << fr_attr_shift[MAX_TLV_NEST])) {
+                       return FALSE;
+               }
+
+               for (i = MAX_TLV_NEST - 1; i >= 0; i--) {
+                       if ((parent->attr & (fr_attr_mask[i] << fr_attr_shift[i]))) {
+                               dattr.attr |= (attr & fr_attr_mask[i + 1]) << fr_attr_shift[i + 1];
+                               goto find;
+                       }
+               }
+
+               return FALSE;
+       }
+
+find:
+#if 0
+       fprintf(stderr, "LOOKING FOR %08x %08x + %08x %08x --> %08x %08x\n",
+               parent->vendor, parent->attr, attr, vendor,
+               dattr.vendor, dattr.attr);
+#endif
+
+       *pattr = dattr.attr;
+       *pvendor = dattr.vendor;
+       return TRUE;
+}
+
+/*
+ *     Get an attribute by it's numerical value, and the parent
+ */
+const DICT_ATTR *dict_attrbyparent(const DICT_ATTR *parent,
+                            unsigned int attr, unsigned int vendor)
+{
+       unsigned int my_attr, my_vendor;
+       DICT_ATTR dattr;
+
+       my_attr = attr;
+       my_vendor = vendor;
+
+       if (!dict_attr_child(parent, &my_attr, &my_vendor)) return NULL;
+
+       dattr.attr = my_attr;
+       dattr.vendor = my_vendor;
+
+       return fr_hash_table_finddata(attributes_byvalue, &dattr);
+}
+
+
 /*
  *     Get an attribute by its name.
  */
-DICT_ATTR *dict_attrbyname(const char *name)
+const DICT_ATTR *dict_attrbyname(const char *name)
 {
        DICT_ATTR *da;
        uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];