]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: stktable/cli: simplify entry key handling
authorAurelien DARRAGON <adarragon@haproxy.com>
Mon, 6 Nov 2023 16:53:00 +0000 (17:53 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 8 Nov 2023 15:38:06 +0000 (16:38 +0100)
Make use of smp_to_stkey() in table_process_entry_per_key() to simplify
key handling and leverage auto type conversions from sample API.

One noticeable side effect is that integer input checks will be relaxed
given that c_str2int() sample conv is more permissible than the integrated
table_process_entry_per_key() integer parser.

src/stick_table.c

index 905310fc6a1bcba409c927b456b78d5ee774c21e..825c22cee38ce3a0f85b6a4164a4a100644461cb 100644 (file)
@@ -4890,8 +4890,7 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args)
        struct show_table_ctx *ctx = appctx->svcctx;
        struct stktable *t = ctx->target;
        struct stksess *ts;
-       uint32_t uint32_key;
-       unsigned char ip6_key[sizeof(struct in6_addr)];
+       struct sample key;
        long long value;
        int data_type;
        int cur_arg;
@@ -4901,35 +4900,16 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args)
        if (!*args[4])
                return cli_err(appctx, "Key value expected\n");
 
+       memset(&key, 0, sizeof(key));
+       key.data.type = SMP_T_STR;
+       key.data.u.str.area = args[4];
+       key.data.u.str.data = strlen(args[4]);
+
        switch (t->type) {
        case SMP_T_IPV4:
-               if (inet_pton(AF_INET, args[4], &uint32_key) <= 0)
-                       return cli_err(appctx, "Invalid key\n");
-               static_table_key.key = &uint32_key;
-               break;
        case SMP_T_IPV6:
-               if (inet_pton(AF_INET6, args[4], ip6_key) <= 0)
-                       return cli_err(appctx, "Invalid key\n");
-               static_table_key.key = &ip6_key;
-               break;
        case SMP_T_SINT:
-               {
-                       char *endptr;
-                       unsigned long val;
-                       errno = 0;
-                       val = strtoul(args[4], &endptr, 10);
-                       if ((errno == ERANGE && val == ULONG_MAX) ||
-                           (errno != 0 && val == 0) || endptr == args[4] ||
-                           val > 0xffffffff)
-                               return cli_err(appctx, "Invalid key\n");
-                       uint32_key = (uint32_t) val;
-                       static_table_key.key = &uint32_key;
-                       break;
-               }
-               break;
        case SMP_T_STR:
-               static_table_key.key = args[4];
-               static_table_key.key_len = strlen(args[4]);
                break;
        default:
                switch (ctx->action) {
@@ -4944,6 +4924,12 @@ static int table_process_entry_per_key(struct appctx *appctx, char **args)
                }
        }
 
+       /* try to convert key according to table type
+        * (it will fill static_table_key on success)
+        */
+       if (!smp_to_stkey(&key, t))
+               return cli_err(appctx, "Invalid key\n");
+
        /* check permissions */
        if (!cli_has_level(appctx, ACCESS_LVL_OPER))
                return 1;