Skip to contentSkip to conversation

datatype: add xinteger_type alias to print in hexadecimal

PATCH Superseded Pablo Neira Ayuso 1 participant 1 message Nov 2, 2021 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 datatype: add xinteger_type alias to print in hexadecimal

Add an alias of the integer type to print raw payload expressions in hexadecimal.

---
 include/datatype.h |  1 +
 src/datatype.c     | 25 +++++++++++++++++++++++++
 src/payload.c      |  2 +-
 3 files changed, 27 insertions(+), 1 deletion(-)
include/datatype.h +1 -0
index 7ddd3566d459..f5bb9dc4d937 100644
@@ -254,6 +254,7 @@ extern const struct datatype verdict_type;
 extern const struct datatype nfproto_type;
 extern const struct datatype bitmask_type;
 extern const struct datatype integer_type;
+extern const struct datatype xinteger_type;
 extern const struct datatype string_type;
 extern const struct datatype lladdr_type;
 extern const struct datatype ipaddr_type;
src/datatype.c +25 -0
index b849f70833c7..728c28b38075 100644
@@ -410,6 +410,31 @@ const struct datatype integer_type = {
 	.parse		= integer_type_parse,
 };
 
+static void xinteger_type_print(const struct expr *expr, struct output_ctx *octx)
+{
+	const struct datatype *dtype = expr->dtype;
+	const char *fmt = "0x%Zx";
+
+	do {
+		if (dtype->basefmt != NULL) {
+			fmt = dtype->basefmt;
+			break;
+		}
+	} while ((dtype = dtype->basetype));
+
+	nft_gmp_print(octx, fmt, expr->value);
+}
+
+/* Alias of integer_type to print raw payload expressions in hexadecimal. */
+const struct datatype xinteger_type = {
+	.type		= TYPE_INTEGER,
+	.name		= "integer",
+	.desc		= "integer",
+	.print		= xinteger_type_print,
+	.json		= integer_type_json,
+	.parse		= integer_type_parse,
+};
+
 static void string_type_print(const struct expr *expr, struct output_ctx *octx)
 {
 	unsigned int len = div_round_up(expr->len, BITS_PER_BYTE);
src/payload.c +1 -1
index c662900bdaac..d9e0d4254f19 100644
@@ -269,7 +269,7 @@ void payload_init_raw(struct expr *expr, enum proto_bases base,
 	expr->payload.base	= base;
 	expr->payload.offset	= offset;
 	expr->len		= len;
-	expr->dtype		= &integer_type;
+	expr->dtype		= &xinteger_type;
 
 	if (base != PROTO_BASE_TRANSPORT_HDR)
 		return;
Signature · 1 line
2.30.2