Whitespace changes in license headers.
[radsecproxy.git] / lib / avp.c
index 4489f66..11c56db 100644 (file)
--- a/lib/avp.c
+++ b/lib/avp.c
@@ -1,5 +1,5 @@
-/* Copyright 2011 PADL Software Pty Ltd. All rights reserved.
-   See the file COPYING for licensing information.  */
+/* Copyright 2011 JANET(UK). All rights reserved.
+   See LICENSE for licensing information. */
 
 #if defined HAVE_CONFIG_H
 #include <config.h>
@@ -9,7 +9,6 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
-#include <libgen.h>
 #include <assert.h>
 
 #include <radsec/radsec.h>
@@ -26,14 +25,19 @@ rs_avp_free (rs_avp **vps)
 size_t
 rs_avp_length (rs_const_avp *vp)
 {
-  assert (vp != NULL);
+  if (vp == NULL)
+    return 0;
+
   return vp->length;
 }
 
 rs_attr_type_t
 rs_avp_typeof (rs_const_avp *vp)
 {
-  return vp ? vp->da->type : RS_TYPE_INVALID;
+  if (vp == NULL)
+    return RS_TYPE_INVALID;
+
+  return vp->da->type;
 }
 
 void
@@ -41,6 +45,8 @@ rs_avp_attrid (rs_const_avp *vp,
               unsigned int *attr,
               unsigned int *vendor)
 {
+  assert (vp != NULL);
+
   *attr = vp->da->attr;
   *vendor = vp->da->vendor;
 }
@@ -48,7 +54,7 @@ rs_avp_attrid (rs_const_avp *vp,
 const char *
 rs_avp_name (rs_const_avp *vp)
 {
-  return vp ? vp->da->name : NULL;
+  return (vp != NULL) ? vp->da->name : NULL;
 }
 
 void
@@ -100,7 +106,10 @@ rs_avp_dup (rs_const_avp *vp)
 {
   rs_avp *vp2;
 
-  vp2 = nr_vp_alloc (vp->da);
+  if (vp->da->flags.unknown)
+    vp2 = nr_vp_alloc_raw (vp->da->attr, vp->da->vendor);
+  else
+    vp2 = nr_vp_alloc (vp->da);
   if (vp2 == NULL)
     return NULL;
 
@@ -128,15 +137,15 @@ rs_avp_dup (rs_const_avp *vp)
 }
 
 rs_avp *
-rs_avp_next (rs_avp *avp)
+rs_avp_next (rs_avp *vp)
 {
-  return avp ? avp->next : NULL;
+  return (vp != NULL) ? vp->next : NULL;
 }
 
 rs_const_avp *
-rs_avp_next_const (rs_const_avp *avp)
+rs_avp_next_const (rs_const_avp *vp)
 {
-  return avp ? avp->next : NULL;
+  return (vp != NULL) ? vp->next : NULL;
 }
 
 int
@@ -448,6 +457,79 @@ rs_attr_find (const char *name,
   return RSE_OK;
 }
 
+int
+rs_attr_display_name (unsigned int attr,
+                      unsigned int vendor,
+                      char *buffer,
+                      size_t bufsize,
+                      int canonical)
+{
+  const DICT_ATTR *da = NULL;
+  DICT_ATTR da2;
+  int err;
+
+  if (!canonical) {
+    da = nr_dict_attr_byvalue (attr, vendor);
+  }
+  if (da == NULL) {
+    err = nr_dict_attr_2struct(&da2, attr, vendor,
+                               buffer, bufsize);
+    if (err < 0)
+      return -err;
+  } else {
+    snprintf(buffer, bufsize, "%s", da->name);
+  }
+
+  return RSE_OK;
+}
+
+int
+rs_attr_parse_name (const char *name,
+                   unsigned int *attr,
+                   unsigned int *vendor)
+{
+  const DICT_ATTR *da;
+
+  if (strncmp(name, "Attr-", 5) == 0) {
+    char *s = (char *)&name[5];
+    unsigned int tmp;
+
+    tmp = strtoul(s, &s, 10);
+    if (*s == '.') {
+      s++;
+
+      switch (tmp) {
+      case PW_VENDOR_SPECIFIC:
+       *vendor = strtoul(s, &s, 10);
+       if (*s != '.')
+         return RSE_ATTR_BAD_NAME;
+
+       s++;
+
+       *attr = strtoul(s, &s, 10);
+       if (*s != '\0')
+         return RSE_ATTR_BAD_NAME;
+
+       break;
+      default:
+       return RSE_ATTR_BAD_NAME;
+      }
+    } else {
+      *attr = tmp;
+      *vendor = 0;
+    }
+  } else {
+    da = nr_dict_attr_byname (name);
+    if (da == NULL)
+      return RSE_ATTR_UNKNOWN;
+
+    *attr = da->attr;
+    *vendor = da->vendor;
+  }
+
+  return RSE_OK;
+}
+
 size_t
 rs_avp_display_value (rs_const_avp *vp,
                       char *buffer,
@@ -455,3 +537,4 @@ rs_avp_display_value (rs_const_avp *vp,
 {
   return nr_vp_snprintf_value (buffer, buflen, vp);
 }
+