From: Christopher Faulet Date: Wed, 1 Apr 2020 07:08:32 +0000 (+0200) Subject: MINOR: sample: add htonl converter X-Git-Tag: v2.2-dev7~143 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4ccc12fc410f103552e403e35a73f3611a5073a6;p=thirdparty%2Fhaproxy.git MINOR: sample: add htonl converter This converter tranform a integer to its binary representation in the network byte order. Integer are already automatically converted to binary during sample expression evaluation. But because samples own 8-bytes integers, the conversion produces 8 bytes. the htonl converter do the same but for 4-bytes integer. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 663f356b05..7c2d838066 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -14119,6 +14119,12 @@ hex2i Converts a hex string containing two hex digits per input byte to an integer. If the input value cannot be converted, then zero is returned. +htonl + Converts the input integer value to its 32-bit binary representation in the + network byte order. Because sample fetches own signed 64-bit integer, when + this converter is used, the input integer value is first casted to an + unsigned 32-bit integer. + http_date([]) Converts an integer supposed to contain a date since epoch to a string representing this date in a format suitable for use in HTTP header fields. If diff --git a/src/sample.c b/src/sample.c index 6cf805aed9..c28b5449e2 100644 --- a/src/sample.c +++ b/src/sample.c @@ -2962,6 +2962,23 @@ static int smp_check_strcmp(struct arg *args, struct sample_conv *conv, return 0; } +/**/ +static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private) +{ + struct buffer *tmp; + uint32_t n; + + n = htonl((uint32_t)smp->data.u.sint); + tmp = get_trash_chunk(); + + memcpy(b_head(tmp), &n, 4); + b_add(tmp, 4); + + smp->data.u.str = *tmp; + smp->data.type = SMP_T_BIN; + return 1; +} + /************************************************************************/ /* All supported sample fetch functions must be declared here */ /************************************************************************/ @@ -3430,6 +3447,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, { { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT }, + { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN }, { NULL, NULL, 0, 0, 0 }, }};