in DICT_VENDOR, name is "name", not "vendorname"
authoraland <aland>
Thu, 19 Feb 2004 17:23:17 +0000 (17:23 +0000)
committeraland <aland>
Thu, 19 Feb 2004 17:23:17 +0000 (17:23 +0000)
dict_vendorname is a bad name.  Use dict_vendorbyname for compatibility
with other dictionary functions.

Add dict_vendorbyvalue

When printing names for unknown attributes, use vendor name, if
it exists.  Cisco-Attr-1 is easier to read than Vendor-9-Attr-1

src/include/libradius.h
src/lib/dict.c
src/lib/valuepair.c

index 97d6bc5..7859ddc 100644 (file)
@@ -125,7 +125,7 @@ typedef struct dict_value {
 } DICT_VALUE;
 
 typedef struct dict_vendor {
-       char                    vendorname[40];
+       char                    name[40];
        int                     vendorpec;
        struct dict_vendor      *next;
 } DICT_VENDOR;
@@ -188,7 +188,8 @@ DICT_ATTR   *dict_attrbyvalue(int attr);
 DICT_ATTR      *dict_attrbyname(const char *attr);
 DICT_VALUE     *dict_valbyattr(int attr, int val);
 DICT_VALUE     *dict_valbyname(int attr, const char *val);
-int            dict_vendorname(const char *name);
+int            dict_vendorbyname(const char *name);
+DICT_VENDOR    *dict_vendorbyvalue(int vendor);
 
 /*
  *  Compatibility
index 84f93f8..470fff3 100644 (file)
@@ -35,6 +35,11 @@ static const char rcsid[] = "$Id$";
 #include       "libradius.h"
 #include       "missing.h"
 
+/*
+ *     There are very few vendors, and they're looked up only when we
+ *     read the dictionaries.  So it's OK to have a singly linked
+ *     list here.
+ */
 static DICT_VENDOR     *dictionary_vendors = NULL;
 
 static rbtree_t *attributes_byname = NULL;
@@ -113,7 +118,7 @@ int dict_addvendor(const char *name, int value)
                return -1;
        }
 
-       if (strlen(name) > (sizeof(vval->vendorname) -1)) {
+       if (strlen(name) > (sizeof(vval->name) -1)) {
                librad_log("dict_addvendor: vendor name too long");
                return -1;
        }
@@ -122,7 +127,7 @@ int dict_addvendor(const char *name, int value)
                librad_log("dict_addvendor: out of memory");
                return -1;
        }
-       strcpy(vval->vendorname, name);
+       strcpy(vval->name, name);
        vval->vendorpec  = value;
 
        /* Insert at front. */
@@ -280,7 +285,9 @@ int dict_addvalue(const char *namestr, char *attrstr, int value)
 /*
  *     Process the ATTRIBUTE command
  */
-static int process_attribute(const char* fn, const int line, const int block_vendor, const char* data) {
+static int process_attribute(const char* fn, const int line,
+                            const int block_vendor, const char* data)
+{
        int             vendor;
        char            namestr[256];
        char            valstr[256];
@@ -373,7 +380,7 @@ static int process_attribute(const char* fn, const int line, const int block_ven
                                s += 5;   
                          }
         
-                         vendor = dict_vendorname(s);
+                         vendor = dict_vendorbyname(s);
                          if (!vendor) {
                                librad_log( "dict_init: %s[%d]: unknown vendor %s",
                                           fn, line, optstr);
@@ -404,7 +411,8 @@ static int process_attribute(const char* fn, const int line, const int block_ven
 /*
  *     Process the VALUE command
  */
-static int process_value(const char* fn, const int line, const char* data) {
+static int process_value(const char* fn, const int line, const char* data)
+{
        char    namestr[256];
        char    valstr[256];
        char    attrstr[256];
@@ -447,7 +455,8 @@ static int process_value(const char* fn, const int line, const char* data) {
 /*
  *     Process the VENDOR command
  */
-static int process_vendor(const char* fn, const int line, const char* data) {
+static int process_vendor(const char* fn, const int line, const char* data)
+{
        char    valstr[256];
        char    attrstr[256];
        int     value;
@@ -483,7 +492,8 @@ static int process_vendor(const char* fn, const int line, const char* data) {
 /*
  *     Initialize the dictionary.
  */
-static int my_dict_init(const char *dir, const char *fn, const char *src_file, int src_line)
+static int my_dict_init(const char *dir, const char *fn,
+                       const char *src_file, int src_line)
 {
        FILE    *fp;
        char    dirtmp[256];
@@ -609,7 +619,7 @@ static int my_dict_init(const char *dir, const char *fn, const char *src_file, i
                                return -1;
                        }
 
-                       vendor = dict_vendorname(optstr);
+                       vendor = dict_vendorbyname(optstr);
                        if (!vendor) {
                                librad_log(
                                        "dict_init: %s[%d]: unknown vendor %s",
@@ -631,7 +641,7 @@ static int my_dict_init(const char *dir, const char *fn, const char *src_file, i
                                return -1;
                        }
 
-                       vendor = dict_vendorname(optstr);
+                       vendor = dict_vendorbyname(optstr);
                        if (!vendor) {
                                librad_log(
                                        "dict_init: %s[%d]: unknown vendor %s",
@@ -895,7 +905,7 @@ DICT_VALUE *dict_valbyname(int attr, const char *name)
 /*
  *     Get the vendor PEC based on the vendor name
  */
-int dict_vendorname(const char *name)
+int dict_vendorbyname(const char *name)
 {
        DICT_VENDOR *v;
 
@@ -903,10 +913,30 @@ int dict_vendorname(const char *name)
         *      Find the vendor, if any.
         */
        for (v = dictionary_vendors; v; v = v->next) {
-               if (strcasecmp(name, v->vendorname) == 0) {
+               if (strcasecmp(name, v->name) == 0) {
                        return v->vendorpec;
                }
        }
 
        return 0;
 }
+
+/*
+ *     Return the vendor struct based on the PEC.
+ */
+DICT_VENDOR *dict_vendorbyvalue(int vendor)
+{
+       DICT_VENDOR *v;
+
+       /*
+        *      Find the vendor, if any.
+        */
+       for (v = dictionary_vendors; v; v = v->next) {
+               if (vendor == v->vendorpec) {
+                       return v;
+               }
+       }
+
+       return NULL;
+
+}
index 423994e..32a3b6f 100644 (file)
@@ -75,8 +75,16 @@ VALUE_PAIR *paircreate(int attr, int type)
        } else if (VENDOR(attr) == 0) {
                sprintf(vp->name, "Attr-%u", attr);
        } else {
-               sprintf(vp->name, "Vendor-%u-Attr-%u",
-                       VENDOR(attr), attr & 0xffff);
+               DICT_VENDOR *v;
+
+               v = dict_vendorbyvalue(VENDOR(attr));
+               if (v) {
+                       sprintf(vp->name, "%s-Attr-%u",
+                               v->name, attr & 0xffff);
+               } else {
+                       sprintf(vp->name, "Vendor-%u-Attr-%u",
+                               VENDOR(attr), attr & 0xffff);
+               }
        }
        switch (vp->type) {
                case PW_TYPE_INTEGER:
@@ -855,7 +863,7 @@ static VALUE_PAIR *pairmake_any(const char *attribute, const char *value,
                memcpy(buffer, attribute, p - attribute);
                buffer[p - attribute] = '\0';
 
-               vendor = dict_vendorname(buffer);
+               vendor = dict_vendorbyname(buffer);
                if (vendor == 0) goto error;
 
                p += 6;