summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2020-12-14 17:53:47 +0100
committerPhil Sutter <phil@nwl.cc>2020-12-15 11:14:15 +0100
commite53b6fdacea2c4b68c0932804546cf10babfb43e (patch)
treee8c791b6a38e92ac976e31b3076f6f00db9efd28
parent8fbc98c6b97679171a5af782df86035615c5b8f0 (diff)
set_elem: Use nftnl_data_reg_snprintf()
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>
-rw-r--r--include/data_reg.h4
-rw-r--r--src/expr/data_reg.c6
-rw-r--r--src/set_elem.c16
3 files changed, 17 insertions, 9 deletions
diff --git a/include/data_reg.h b/include/data_reg.h
index d9578aa..0d6b778 100644
--- a/include/data_reg.h
+++ b/include/data_reg.h
@@ -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)];
diff --git a/src/expr/data_reg.c b/src/expr/data_reg.c
index 4e35a79..d3ccc61 100644
--- a/src/expr/data_reg.c
+++ b/src/expr/data_reg.c
@@ -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);
}
diff --git a/src/set_elem.c b/src/set_elem.c
index e82684b..51bf2c7 100644
--- a/src/set_elem.c
+++ b/src/set_elem.c
@@ -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);