]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: add htonl converter
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 1 Apr 2020 07:08:32 +0000 (09:08 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 27 Apr 2020 07:39:37 +0000 (09:39 +0200)
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
src/sample.c

index 663f356b0532af3e75e9e021e858e3196609543c..7c2d838066d18155c2bb5fca7ae62d2f7b4b9a52 100644 (file)
@@ -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([<offset],[<unit>])
   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
index 6cf805aed9adb99bf5e27ee866d1fe2ac870e267..c28b5449e2ffdd25b3dea0d40e95b158f4c06721 100644 (file)
@@ -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 },
 }};