From 7e5286666f88b381498334a6cf129735abfdfa2d Mon Sep 17 00:00:00 2001
From: lucaschimweg <l.schimweg@gmail.com>
Date: Tue, 10 Sep 2019 15:42:52 +0200
Subject: [PATCH] MINOR: sample: Add UUID-fetch

---
 doc/configuration.txt |  5 +++++
 src/sample.c          | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/doc/configuration.txt b/doc/configuration.txt
index ba5a8b36..ee9712f9 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -14316,6 +14316,11 @@ rand([<range>]) : integer
   needed to take some routing decisions for example, or just for debugging
   purposes. This random must not be used for security purposes.
 
+uuid([<version>]) : string
+  Returns a UUID following the RFC4122 standard. If the version is not
+  specified, a UUID version 4 (fully random) is returned.
+  Currently, only version 4 is supported.
+
 srv_conn([<backend>/]<server>) : integer
   Returns an integer value corresponding to the number of currently established
   connections on the designated server, possibly including the connection being
diff --git a/src/sample.c b/src/sample.c
index 6327b6b0..b6c27787 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -3196,6 +3196,47 @@ static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, cons
 	return 1;
 }
 
+// Generate a RFC4122 UUID (default is v4 = fully random)
+static int
+smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
+{
+    if (!args[0].data.sint || args[0].data.sint == 4) {
+        uint32_t rnd[4] = { 0, 0, 0, 0 };
+        uint64_t last = 0;
+        int byte = 0;
+        uint8_t bits = 0;
+        unsigned int rand_max_bits = my_flsl(RAND_MAX);
+
+        while (byte < 4) {
+            while (bits < 32) {
+                last |= (uint64_t)random() << bits;
+                bits += rand_max_bits;
+            }
+            rnd[byte++] = last;
+            last >>= 32u;
+            bits  -= 32;
+        }
+
+        chunk_printf(&trash, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
+                     rnd[0], 
+                     rnd[1] & 0xFFFF, 
+                     ((rnd[1] >> 16u) % 0xFFF) | 0x4000,  // highest 4 bits indicate the uuid version
+                     (rnd[2] & 0x3FFF) | 0x8000,  // the highest 2 bits indicate the UUID variant (10),
+					 ((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFu
+        );
+
+        smp->data.type = SMP_T_STR;
+        smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
+        smp->data.u.str = trash;
+
+        return 1;
+    }
+
+    // more implementations of other uuid formats possible here
+    return 0;
+
+}
+
 /* Note: must not be declared <const> as its list will be overwritten.
  * Note: fetches that may return multiple types must be declared as the lowest
  * common denominator, the type that can be casted into all other ones. For
@@ -3214,6 +3255,7 @@ static struct sample_fetch_kw_list smp_kws = {ILH, {
 	{ "rand",         smp_fetch_rand,  ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
 	{ "stopping",     smp_fetch_stopping, 0,         NULL, SMP_T_BOOL, SMP_USE_INTRN },
 	{ "stopping",     smp_fetch_stopping, 0,         NULL, SMP_T_BOOL, SMP_USE_INTRN },
+	{ "uuid",         smp_fetch_uuid,  ARG1(0, SINT),      NULL, SMP_T_STR, SMP_USE_INTRN },
 
 	{ "cpu_calls",    smp_fetch_cpu_calls,  0,       NULL, SMP_T_SINT, SMP_USE_INTRN },
 	{ "cpu_ns_avg",   smp_fetch_cpu_ns_avg, 0,       NULL, SMP_T_SINT, SMP_USE_INTRN },
-- 
2.20.1 (Apple Git-117)

