]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
set_elem: Use nftnl_data_reg_snprintf()
authorPhil Sutter <phil@nwl.cc>
Mon, 14 Dec 2020 16:53:47 +0000 (17:53 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 15 Dec 2020 10:14:15 +0000 (11:14 +0100)
Introduce a flag to allow toggling the '0x' prefix when printing data
values, then use the existing routines to print data registers from
set_elem code.

Signed-off-by: Phil Sutter <phil@nwl.cc>
include/data_reg.h
src/expr/data_reg.c
src/set_elem.c

index d9578aa47990143063bcc824522339c8f1340b13..0d6b77829cf89b668c6062d5646b69faa9f0d00e 100644 (file)
@@ -13,6 +13,10 @@ enum {
        DATA_CHAIN,
 };
 
+enum {
+       DATA_F_NOPFX = 1 << 0,
+};
+
 union nftnl_data_reg {
        struct {
                uint32_t        val[NFT_DATA_VALUE_MAXLEN / sizeof(uint32_t)];
index 4e35a799e9591e536b9bf6e3ca4f82ee3e9e97ef..d3ccc612ce8124adcbc176b48c492f677afafa00 100644 (file)
@@ -29,10 +29,14 @@ nftnl_data_reg_value_snprintf_default(char *buf, size_t size,
                                      const union nftnl_data_reg *reg,
                                      uint32_t flags)
 {
+       const char *pfx = flags & DATA_F_NOPFX ? "" : "0x";
        int remain = size, offset = 0, ret, i;
 
+
+
        for (i = 0; i < div_round_up(reg->len, sizeof(uint32_t)); i++) {
-               ret = snprintf(buf + offset, remain, "0x%.8x ", reg->val[i]);
+               ret = snprintf(buf + offset, remain,
+                              "%s%.8x ", pfx, reg->val[i]);
                SNPRINTF_BUFFER_SIZE(ret, remain, offset);
        }
 
index e82684bc1c53e3d31623d2b2be176c74fcd7d308..51bf2c7b853bb46bd5bcead90d6ce069354b75b3 100644 (file)
@@ -629,18 +629,18 @@ static int nftnl_set_elem_snprintf_default(char *buf, size_t size,
        ret = snprintf(buf, remain, "element ");
        SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
-       for (i = 0; i < div_round_up(e->key.len, sizeof(uint32_t)); i++) {
-               ret = snprintf(buf + offset, remain, "%.8x ", e->key.val[i]);
-               SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-       }
+       ret = nftnl_data_reg_snprintf(buf + offset, remain, &e->key,
+                                     NFTNL_OUTPUT_DEFAULT,
+                                     DATA_F_NOPFX, DATA_VALUE);
+       SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
        ret = snprintf(buf + offset, remain, " : ");
        SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
-       for (i = 0; i < div_round_up(e->data.len, sizeof(uint32_t)); i++) {
-               ret = snprintf(buf + offset, remain, "%.8x ", e->data.val[i]);
-               SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-       }
+       ret = nftnl_data_reg_snprintf(buf + offset, remain, &e->data,
+                                     NFTNL_OUTPUT_DEFAULT,
+                                     DATA_F_NOPFX, DATA_VALUE);
+       SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
        ret = snprintf(buf + offset, remain, "%u [end]", e->set_elem_flags);
        SNPRINTF_BUFFER_SIZE(ret, remain, offset);