]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Added %{hex:...}
authorAlan T. DeKok <aland@freeradius.org>
Fri, 4 May 2012 15:14:08 +0000 (17:14 +0200)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 4 May 2012 15:33:22 +0000 (17:33 +0200)
Will print the hex version of the contents of the attribute.

Mainly useful for integers, IPaddresses, etc.

src/main/xlat.c

index 522bcf27ae1c60e7697d33d3b9304433e0de55ef..7f4932e90ef33f956456214760304a01db42e39f 100644 (file)
@@ -524,6 +524,40 @@ static size_t xlat_integer(UNUSED void *instance, REQUEST *request,
        return snprintf(out, outlen, "%u", vp->vp_integer);
 }
 
+/**
+ * @brief Print data as hex, not as VALUE.
+ */
+static size_t xlat_hex(UNUSED void *instance, REQUEST *request,
+                          char *fmt, char *out, size_t outlen,
+                          UNUSED RADIUS_ESCAPE_STRING func)
+{
+       size_t i;
+       uint8_t *p;
+       VALUE_PAIR *vp;
+
+       while (isspace((int) *fmt)) fmt++;
+
+       if (!radius_get_vp(request, fmt, &vp) || !vp) {
+               *out = '\0';
+               return 0;
+       }
+
+       /*
+        *      Don't truncate the data.
+        */
+       if (outlen < (vp->length * 2)) {
+               *out = 0;
+               return 0;
+       }
+
+       p = &vp->vp_octets[0];
+       for (i = 0; i < vp->length; i++) {
+               snprintf(out + 2*i, 3, "%02x", p[i]);
+       }
+
+       return vp->length * 2;
+}
+
 
 #ifdef WITH_UNLANG
 /**
@@ -842,6 +876,11 @@ int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
                rad_assert(c != NULL);
                c->internal = TRUE;
 
+               xlat_register("hex", xlat_hex, NULL);
+               c = xlat_find("hex");
+               rad_assert(c != NULL);
+               c->internal = TRUE;
+
                xlat_register("string", xlat_string, NULL);
                c = xlat_find("string");
                rad_assert(c != NULL);