From 4ccc12fc410f103552e403e35a73f3611a5073a6 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Wed, 1 Apr 2020 09:08:32 +0200 Subject: [PATCH] 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. --- doc/configuration.txt | 6 ++++++ src/sample.c | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/doc/configuration.txt b/doc/configuration.txt index 663f356b0..7c2d83806 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 6cf805aed..c28b5449e 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 }, }};