From: Arran Cudbard-Bell Date: Fri, 12 Oct 2012 11:25:46 +0000 (+0100) Subject: Backport hex: expansion X-Git-Tag: release_2_2_1~257 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf52cedfd003cf8fdcff9de0b48f67b8055276a4;p=thirdparty%2Ffreeradius-server.git Backport hex: expansion --- diff --git a/src/main/xlat.c b/src/main/xlat.c index f410c3acedb..6e9dddb2456 100644 --- a/src/main/xlat.c +++ b/src/main/xlat.c @@ -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");