From df9c2ef2c33d0fc732ed6a1adbc8cd51b792ac74 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Mon, 30 Dec 2024 19:26:44 +0100 Subject: [PATCH] MINOR: stktable: add stkey_to_smp() helper reverse operation for smp_to_stkey(): fills input from a stktable_key struct. Returns 1 on success and 0 on failure. --- src/stick_table.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/stick_table.c b/src/stick_table.c index 5fbe564c1..31eeff528 100644 --- a/src/stick_table.c +++ b/src/stick_table.c @@ -1520,6 +1520,49 @@ struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t) return &static_table_key; } +/* reverse operation for smp_to_stkey(): fills input with content of + * + * smp_make_safe() may be applied to smp before returning to ensure it can be + * used even if the key is no longer available upon return. + * Returns 1 on success and 0 on failure + */ +int stkey_to_smp(struct sample *smp, struct stktable_key *key, int key_type) +{ + smp->data.type = key_type; + smp->flags = 0; + + switch (key_type) { + + case SMP_T_IPV4: + memcpy(&smp->data.u.ipv4, static_table_key.key, sizeof(struct in_addr)); + break; + case SMP_T_IPV6: + memcpy(&smp->data.u.ipv6, static_table_key.key, sizeof(struct in6_addr)); + break; + + case SMP_T_SINT: + /* The stick table require a 32bit unsigned int, "sint" is a + * signed 64 it, so we can convert it inplace. + */ + smp->data.u.sint = *(unsigned int *)static_table_key.key; + break; + + case SMP_T_STR: + case SMP_T_BIN: + smp->data.u.str.area = static_table_key.key; + smp->data.u.str.data = static_table_key.key_len; + smp->flags = SMP_F_CONST; + if (!smp_make_safe(smp)) + return 0; + break; + + default: /* impossible case. */ + return 0; + } + + return 1; +} + /* * Process a fetch + format conversion as defined by the sample expression * on request or response considering the parameter. Returns either NULL if