]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Backport hex: expansion
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 12 Oct 2012 11:25:46 +0000 (12:25 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 12 Oct 2012 11:25:46 +0000 (12:25 +0100)
src/main/xlat.c

index f410c3acedb47ec296d99312a4128f34f8c34955..6e9dddb2456b46a5c6d1e00cbf5b1742fa2be390 100644 (file)
@@ -533,6 +533,40 @@ static size_t xlat_string(UNUSED void *instance, REQUEST *request,
        return len;
 }
 
+/**
+ * @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 HAVE_REGEX_H
 /*
  *     Pull %{0} to %{8} out of the packet.
@@ -678,6 +712,11 @@ int xlat_register(const char *module, RAD_XLAT_FUNC func, void *instance)
                c = xlat_find("control");
                rad_assert(c != NULL);
                c->internal = TRUE;
+               
+               xlat_register("hex", xlat_hex, "");
+               c = xlat_find("hex");
+               rad_assert(c != NULL);
+               c->internal = TRUE;
 
                xlat_register("integer", xlat_integer, "");
                c = xlat_find("integer");