]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
datatype: Display pre-defined inet_service values in host byte order
authorElise Lennion <elise.lennion@gmail.com>
Sat, 10 Dec 2016 00:35:13 +0000 (22:35 -0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Sat, 10 Dec 2016 13:12:31 +0000 (14:12 +0100)
nft describe displays, to the user, which values are available for a selector,
then the values should be in host byte order.

Variable size was replaced by len to better match the common pattern.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Fixes: ccc5da470e76 ("datatype: Replace getnameinfo() by internal lookup table")
Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/datatype.h
src/datatype.c
src/expression.c

index d4fe817b1e03919468f2d0d86898ebf44017c985..a7db1df4c070f7e97c97b9b1bebc3bef09f13574 100644 (file)
@@ -191,7 +191,8 @@ extern struct error_record *symbolic_constant_parse(const struct expr *sym,
 extern void symbolic_constant_print(const struct symbol_table *tbl,
                                    const struct expr *expr, bool quotes);
 extern void symbol_table_print(const struct symbol_table *tbl,
-                              const struct datatype *dtype);
+                              const struct datatype *dtype,
+                              enum byteorder byteorder);
 
 extern struct symbol_table *rt_symbol_table_init(const char *filename);
 extern void rt_symbol_table_free(struct symbol_table *tbl);
index b5d73bc045e22975ac653b847fa12c57dfe4e37d..5600efcc2cd0085ed8e28669f3b92faa8ec02e67 100644 (file)
@@ -180,15 +180,33 @@ void symbolic_constant_print(const struct symbol_table *tbl,
                printf("%s", s->identifier);
 }
 
+static void switch_byteorder(void *data, unsigned int len)
+{
+       mpz_t op;
+
+       mpz_init(op);
+       mpz_import_data(op, data, BYTEORDER_BIG_ENDIAN, len);
+       mpz_export_data(data, op, BYTEORDER_HOST_ENDIAN, len);
+       mpz_clear(op);
+}
+
 void symbol_table_print(const struct symbol_table *tbl,
-                       const struct datatype *dtype)
+                       const struct datatype *dtype,
+                       enum byteorder byteorder)
 {
        const struct symbolic_constant *s;
-       unsigned int size = 2 * dtype->size / BITS_PER_BYTE;
+       unsigned int len = dtype->size / BITS_PER_BYTE;
+       uint64_t value;
+
+       for (s = tbl->symbols; s->identifier != NULL; s++) {
+               value = s->value;
+
+               if (byteorder == BYTEORDER_BIG_ENDIAN)
+                       switch_byteorder(&value, len);
 
-       for (s = tbl->symbols; s->identifier != NULL; s++)
                printf("\t%-30s\t0x%.*" PRIx64 "\n",
-                      s->identifier, size, s->value);
+                      s->identifier, 2 * len, value);
+       }
 }
 
 static void invalid_type_print(const struct expr *expr)
index a10af5d3a60b5747358febe1b13048e4857f0688..2aada77d6694a8f01f9695765e6299ecceb8879b 100644 (file)
@@ -115,7 +115,8 @@ void expr_describe(const struct expr *expr)
 
        if (expr->dtype->sym_tbl != NULL) {
                printf("\npre-defined symbolic constants:\n");
-               symbol_table_print(expr->dtype->sym_tbl, expr->dtype);
+               symbol_table_print(expr->dtype->sym_tbl, expr->dtype,
+                                  expr->byteorder);
        }
 }