]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: allow integers to cast to binary
authorWilly Tarreau <w@1wt.eu>
Tue, 15 Jul 2014 19:19:08 +0000 (21:19 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 15 Jul 2014 19:36:15 +0000 (21:36 +0200)
Doing so finally allows to apply the hex converter to integers as well.
Note that all integers are represented in 32-bit, big endian so that their
conversion remains human readable and portable. A later improvement to the
hex converter could be to make it trim leading zeroes, and/or to only report
a number of least significant bytes.

src/sample.c

index 6128649c2dfe60be3df9fe1088c75fb53d920645..99acb5adc154f6e31cd6aa6d15c20a3788d9261e 100644 (file)
@@ -675,6 +675,18 @@ static int c_addr2bin(struct sample *smp)
        return 1;
 }
 
+static int c_int2bin(struct sample *smp)
+{
+       struct chunk *chk = get_trash_chunk();
+
+       *(unsigned int *)chk->str = htonl(smp->data.uint);
+       chk->len = 4;
+
+       smp->data.str = *chk;
+       smp->type = SMP_T_BIN;
+       return 1;
+}
+
 
 /*****************************************************************/
 /*      Sample casts matrix:                                     */
@@ -685,8 +697,8 @@ static int c_addr2bin(struct sample *smp)
 sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
 /*            to:  BOOL       UINT       SINT       ADDR        IPV4      IPV6        STR         BIN         METH */
 /* from: BOOL */ { c_none,    c_none,    c_none,    NULL,       NULL,     NULL,       c_int2str,  NULL,       NULL,       },
-/*       UINT */ { c_none,    c_none,    c_none,    c_int2ip,   c_int2ip, NULL,       c_int2str,  NULL,       NULL,       },
-/*       SINT */ { c_none,    c_none,    c_none,    c_int2ip,   c_int2ip, NULL,       c_int2str,  NULL,       NULL,       },
+/*       UINT */ { c_none,    c_none,    c_none,    c_int2ip,   c_int2ip, NULL,       c_int2str,  c_int2bin,  NULL,       },
+/*       SINT */ { c_none,    c_none,    c_none,    c_int2ip,   c_int2ip, NULL,       c_int2str,  c_int2bin,  NULL,       },
 /*       ADDR */ { NULL,      NULL,      NULL,      NULL,       NULL,     NULL,       NULL,       NULL,       NULL,       },
 /*       IPV4 */ { NULL,      c_ip2int,  c_ip2int,  c_none,     c_none,   c_ip2ipv6,  c_ip2str,   c_addr2bin, NULL,       },
 /*       IPV6 */ { NULL,      NULL,      NULL,      c_none,     NULL,     c_none,     c_ipv62str, c_addr2bin, NULL,       },