]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: samples: provide a "crc32" converter
authorWilly Tarreau <w@1wt.eu>
Tue, 20 Jan 2015 18:35:24 +0000 (19:35 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 20 Jan 2015 18:48:08 +0000 (19:48 +0100)
This converter hashes a binary input sample into an unsigned 32-bit quantity
using the CRC32 hash function. Optionally, it is possible to apply a full
avalanche hash function to the output if the optional <avalanche> argument
equals 1. This converter uses the same functions as used by the various hash-
based load balancing algorithms, so it will provide exactly the same results.
It is provided for compatibility with other software which want a CRC32 to be
computed on some input keys, so it follows the most common implementation as
found in Ethernet, Gzip, PNG, etc... It is slower than the other algorithms
but may provide a better or at least less predictable distribution.

doc/configuration.txt
src/sample.c

index 48ed813c9ba4fca09760c0b2661918e35f0894d3..13f1f724f90e0594cd5fad3177c07e99bf463212 100644 (file)
@@ -10123,6 +10123,19 @@ bytes(<offset>[,<length>])
   sample starting at an offset (in bytes) of the original sample and
   optionnaly truncated at the given length.
 
+crc32([<avalanche>])
+  Hashes a binary input sample into an unsigned 32-bit quantity using the CRC32
+  hash function. Optionally, it is possible to apply a full avalanche hash
+  function to the output if the optional <avalanche> argument equals 1. This
+  converter uses the same functions as used by the various hash-based load
+  balancing algorithms, so it will provide exactly the same results. It is
+  provided for compatibility with other software which want a CRC32 to be
+  computed on some input keys, so it follows the most common implementation as
+  found in Ethernet, Gzip, PNG, etc... It is slower than the other algorithms
+  but may provide a better or at least less predictable distribution. It must
+  not be used for security purposes as a 32-bit hash is trivial to break. See
+  also "djb2", "sdbm", "wt6" and the "hash-type" directive.
+
 djb2([<avalanche>])
   Hashes a binary input sample into an unsigned 32-bit quantity using the DJB2
   hash function. Optionally, it is possible to apply a full avalanche hash
@@ -10131,8 +10144,8 @@ djb2([<avalanche>])
   balancing algorithms, so it will provide exactly the same results. It is
   mostly intended for debugging, but can be used as a stick-table entry to
   collect rough statistics. It must not be used for security purposes as a
-  32-bit hash is trivial to break. See also "sdbm", "wt6" and the "hash-type"
-  directive.
+  32-bit hash is trivial to break. See also "crc32", "sdbm", "wt6" and the
+  "hash-type" directive.
 
 field(<index>,<delimiters>)
   Extracts the substring at the given index considering given delimiters from
@@ -10317,8 +10330,8 @@ sdbm([<avalanche>])
   balancing algorithms, so it will provide exactly the same results. It is
   mostly intended for debugging, but can be used as a stick-table entry to
   collect rough statistics. It must not be used for security purposes as a
-  32-bit hash is trivial to break. See also "djb2", "wt6" and the "hash-type"
-  directive.
+  32-bit hash is trivial to break. See also "crc32", "djb2", "wt6" and the
+  "hash-type" directive.
 
 table_bytes_in_rate(<table>)
   Uses the string representation of the input sample to perform a look up in
@@ -10490,8 +10503,8 @@ wt6([<avalanche>])
   balancing algorithms, so it will provide exactly the same results. It is
   mostly intended for debugging, but can be used as a stick-table entry to
   collect rough statistics. It must not be used for security purposes as a
-  32-bit hash is trivial to break. See also "djb2", "sdbm", and the "hash-type"
-  directive.
+  32-bit hash is trivial to break. See also "crc32", "djb2", "sdbm", and the
+  "hash-type" directive.
 
 
 7.3.2. Fetching samples from internal states
index 00345a8940ebacd5120a6087b9ed6e26a5de0c28..377c4c8f3f07bacf3b2ef90130845d1d2d9edfad 100644 (file)
@@ -1380,6 +1380,16 @@ static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp)
        return 1;
 }
 
+/* hashes the binary input into a 32-bit unsigned int */
+static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp)
+{
+       smp->data.uint = hash_crc32(smp->data.str.str, smp->data.str.len);
+       if (arg_p && arg_p->data.uint)
+               smp->data.uint = full_hash(smp->data.uint);
+       smp->type = SMP_T_UINT;
+       return 1;
+}
+
 /* This function escape special json characters. The returned string can be
  * safely set between two '"' and used as json string. The json string is
  * defined like this:
@@ -1867,6 +1877,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, {
        { "ipmask", sample_conv_ipmask,    ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
        { "ltime",  sample_conv_ltime,     ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
        { "utime",  sample_conv_utime,     ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
+       { "crc32",  sample_conv_crc32,     ARG1(0,UINT), NULL, SMP_T_BIN,  SMP_T_UINT },
        { "djb2",   sample_conv_djb2,      ARG1(0,UINT), NULL, SMP_T_BIN,  SMP_T_UINT },
        { "sdbm",   sample_conv_sdbm,      ARG1(0,UINT), NULL, SMP_T_BIN,  SMP_T_UINT },
        { "wt6",    sample_conv_wt6,       ARG1(0,UINT), NULL, SMP_T_BIN,  SMP_T_UINT },