Patchwork [libnftables] rule: complete print/parse position attr

login
register
mail settings
Submitter Arturo Borrero
Date Aug. 23, 2013, 11:35 a.m.
Message ID <20130823113535.2701.175.stgit@nfdev.cica.es>
Download mbox | patch
Permalink /patch/269427/
State Accepted
Headers show

Comments

Arturo Borrero - Aug. 23, 2013, 11:35 a.m.
Do printf/parse of position attr in both XML and JSON.

Note that the position attr is optional as stated in net/netfilter/nf_tables_api.c

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 0 files changed


--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/src/rule.c b/src/rule.c
index 98c2022..aca88d0 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -538,6 +538,14 @@  static int nft_rule_json_parse(struct nft_rule *r, const char *json)
 		nft_rule_attr_set_u32(r, NFT_RULE_ATTR_COMPAT_FLAGS, uval32);
 	}
 
+	if (nft_jansson_node_exist(root, "position")) {
+		if (nft_jansson_value_parse_val(root, "position",
+						NFT_TYPE_U64, &uval64) == -1)
+			goto err;
+
+		nft_rule_attr_set_u64(r, NFT_RULE_ATTR_POSITION, uval64);
+	}
+
 	array = json_object_get(root, "expr");
 	if (array == NULL)
 		goto err;
@@ -636,6 +644,16 @@  static int nft_rule_xml_parse(struct nft_rule *r, const char *xml)
 		r->flags |= (1 << NFT_RULE_ATTR_COMPAT_FLAGS);
 	}
 
+	node = mxmlFindElement(tree, tree, "position", NULL, NULL,
+			       MXML_DESCEND_FIRST);
+	if (node != NULL && node->child != NULL) {
+		if (nft_strtoi(node->child->value.opaque, BASE_DEC,
+			       &r->position, NFT_TYPE_U64) != 0)
+			goto err;
+
+		r->flags |= (1 << NFT_RULE_ATTR_POSITION);
+	}
+
 	/* Iterating over <expr> */
 	for (node = mxmlFindElement(tree, tree, "expr", "type",
 				    NULL, MXML_DESCEND);
@@ -714,6 +732,13 @@  static int nft_rule_snprintf_json(char *buf, size_t size, struct nft_rule *r,
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}
 
+	if (r->flags & (1 << NFT_RULE_ATTR_POSITION)) {
+		ret = snprintf(buf+offset, len,
+			       "\"position\" : %"PRIu64", ",
+			       r->position);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
 	ret = snprintf(buf+offset, len, "\"expr\" : [");
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
@@ -756,6 +781,13 @@  static int nft_rule_snprintf_xml(char *buf, size_t size, struct nft_rule *r,
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}
 
+	if (r->flags & (1 << NFT_RULE_ATTR_POSITION)) {
+		ret = snprintf(buf+offset, len,
+			       "<position>%"PRIu64"</position>",
+			       r->position);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
 	list_for_each_entry(expr, &r->expr_list, head) {
 		ret = snprintf(buf+offset, len,
 				"<expr type=\"%s\">", expr->ops->name);