]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
table: rework flags printing
authorPablo Neira Ayuso <pablo@netfilter.org>
Mon, 22 Feb 2021 14:44:35 +0000 (15:44 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 2 Mar 2021 10:08:49 +0000 (11:08 +0100)
Simplify routine to print the table flags. Add table_flag_name() and use
it from json too.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/rule.h
src/json.c
src/rule.c

index 330a09aa77fa39aaad50e685b26e9f8b6fe95072..87b6828edca45c19e80d0305876b6ed3de8373f2 100644 (file)
@@ -134,7 +134,7 @@ enum table_flags {
 };
 #define TABLE_FLAGS_MAX 1
 
-extern const char *table_flags_name[TABLE_FLAGS_MAX];
+const char *table_flag_name(uint32_t flag);
 
 /**
  * struct table - nftables table
index 0ccbbe8a75d2dff164272530cfbe2a0feb79997b..defbc8fb44df0983c409d9115120ee4f32e4fcff 100644 (file)
@@ -444,7 +444,7 @@ static json_t *table_flags_json(const struct table *table)
 
        while (flags) {
                if (flags & 0x1) {
-                       tmp = json_string(table_flags_name[i]);
+                       tmp = json_string(table_flag_name(i));
                        json_array_append_new(root, tmp);
                }
                flags >>= 1;
index e4bb6bae276a066daef8af67da92b623604fa17b..d22ab50097908c84d5dd52642d74e4941a1893f7 100644 (file)
@@ -1405,29 +1405,40 @@ struct table *table_lookup_fuzzy(const struct handle *h,
        return st.obj;
 }
 
-const char *table_flags_name[TABLE_FLAGS_MAX] = {
+static const char *table_flags_name[TABLE_FLAGS_MAX] = {
        "dormant",
 };
 
-static void table_print_options(const struct table *table, const char **delim,
-                               struct output_ctx *octx)
+const char *table_flag_name(uint32_t flag)
+{
+       if (flag >= TABLE_FLAGS_MAX)
+               return "unknown";
+
+       return table_flags_name[flag];
+}
+
+static void table_print_flags(const struct table *table, const char **delim,
+                             struct output_ctx *octx)
 {
        uint32_t flags = table->flags;
+       bool comma = false;
        int i;
 
-       if (flags) {
-               nft_print(octx, "\tflags ");
+       if (!table->flags)
+               return;
 
-               for (i = 0; i < TABLE_FLAGS_MAX; i++) {
-                       if (flags & 0x1)
-                               nft_print(octx, "%s", table_flags_name[i]);
-                       flags >>= 1;
-                       if (flags)
+       nft_print(octx, "\tflags ");
+       for (i = 0; i < TABLE_FLAGS_MAX; i++) {
+               if (flags & (1 << i)) {
+                       if (comma)
                                nft_print(octx, ",");
+
+                       nft_print(octx, "%s", table_flag_name(i));
+                       comma = true;
                }
-               nft_print(octx, "\n");
-               *delim = "\n";
        }
+       nft_print(octx, "\n");
+       *delim = "\n";
 }
 
 static void table_print(const struct table *table, struct output_ctx *octx)
@@ -1443,7 +1454,7 @@ static void table_print(const struct table *table, struct output_ctx *octx)
        if (nft_output_handle(octx))
                nft_print(octx, " # handle %" PRIu64, table->handle.handle.id);
        nft_print(octx, "\n");
-       table_print_options(table, &delim, octx);
+       table_print_flags(table, &delim, octx);
 
        if (table->comment)
                nft_print(octx, "\tcomment \"%s\"\n", table->comment);