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.
This commit is contained in:
Christopher Faulet 2020-04-01 09:08:32 +02:00
parent 5d503fcf5b
commit 4ccc12fc41
2 changed files with 24 additions and 0 deletions

View 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

View 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 },
}};