netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libnftnl PATCH 0/5] Do not print unset values in xml and json
@ 2014-06-26 17:08 Ana Rey
  2014-06-26 17:08 ` [libnftnl PATCH 1/5] src: table: Free memory in the same function that is reserved Ana Rey
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Ana Rey @ 2014-06-26 17:08 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

This patchset does tasks about does not print unset value in xml and json file
and about refactoring code:

* src: table: Free memory in the same function that is reserved
* src: table: Use nft_table_attr_set_* in the xml functions
* src: table: Add set, unset and parse implementation for use value

 Moreover, I add some implementations for use value.

Ana Rey (5):
  src: table: Free memory in the same function that is reserved
  src: table: Use nft_table_attr_set_* in the xml functions
  src: table: Add set, unset and parse implementation for use value
  src: table: Do not print unset values in xml file
  src: table: Do not print unset values in json file

 src/table.c | 144 ++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 92 insertions(+), 52 deletions(-)

-- 
2.0.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 1/5] src: table: Free memory in the same function that is reserved
  2014-06-26 17:08 [libnftnl PATCH 0/5] Do not print unset values in xml and json Ana Rey
@ 2014-06-26 17:08 ` Ana Rey
  2014-06-26 17:08 ` [libnftnl PATCH 2/5] src: table: Use nft_table_attr_set_* in the xml functions Ana Rey
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-06-26 17:08 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

Free memory in the same function that is reserved.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/table.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/table.c b/src/table.c
index b4d1663..ed6e615 100644
--- a/src/table.c
+++ b/src/table.c
@@ -328,25 +328,21 @@ int nft_jansson_parse_table(struct nft_table *t, json_t *tree,
 
 	str = nft_jansson_parse_str(root, "name", err);
 	if (str == NULL)
-		goto err;
+		return -1;
 
 	nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, str);
 
 	if (nft_jansson_parse_family(root, &family, err) != 0)
-		goto err;
+		return -1;
 
 	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
 
 	if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U32, &flags, err) < 0)
-		goto err;
+		return -1;
 
 	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
 
-	nft_jansson_free_root(tree);
 	return 0;
-err:
-	nft_jansson_free_root(tree);
-	return -1;
 }
 #endif
 
@@ -357,12 +353,17 @@ static int nft_table_json_parse(struct nft_table *t, const void *json,
 #ifdef JSON_PARSING
 	json_t *tree;
 	json_error_t error;
+	int ret;
 
 	tree = nft_jansson_create_root(json, &error, err, input);
 	if (tree == NULL)
 		return -1;
 
-	return nft_jansson_parse_table(t, tree, err);
+	ret = nft_jansson_parse_table(t, tree, err);
+
+	nft_jansson_free_root(tree);
+
+	return ret;
 #else
 	errno = EOPNOTSUPP;
 	return -1;
-- 
2.0.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 2/5] src: table: Use nft_table_attr_set_* in the xml functions
  2014-06-26 17:08 [libnftnl PATCH 0/5] Do not print unset values in xml and json Ana Rey
  2014-06-26 17:08 ` [libnftnl PATCH 1/5] src: table: Free memory in the same function that is reserved Ana Rey
@ 2014-06-26 17:08 ` Ana Rey
  2014-06-26 17:08 ` [libnftnl PATCH 3/5] src: table: Add set, unset and parse implementation for use value Ana Rey
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-06-26 17:08 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

Code refactoring to use nft_table_attr_set_* in parse xml functions.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/table.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/src/table.c b/src/table.c
index ed6e615..de4a64d 100644
--- a/src/table.c
+++ b/src/table.c
@@ -263,32 +263,24 @@ int nft_mxml_table_parse(mxml_node_t *tree, struct nft_table *t,
 {
 	const char *name;
 	int family;
+	uint32_t flags;
 
 	name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST,
 				  NFT_XML_MAND, err);
 	if (name == NULL)
 		return -1;
-
-	if (t->name)
-		xfree(t->name);
-
-	t->name = strdup(name);
-	t->flags |= (1 << NFT_TABLE_ATTR_NAME);
+	nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, name);
 
 	family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST,
 				       NFT_XML_MAND, err);
 	if (family < 0)
 		return -1;
-
-	t->family = family;
-	t->flags |= (1 << NFT_TABLE_ATTR_FAMILY);
+	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
 
 	if (nft_mxml_num_parse(tree, "flags", MXML_DESCEND, BASE_DEC,
-			       &t->table_flags, NFT_TYPE_U32,
-			       NFT_XML_MAND, err) != 0)
+			       &flags, NFT_TYPE_U32, NFT_XML_MAND, err) != 0)
 		return -1;
-
-	t->flags |= (1 << NFT_TABLE_ATTR_FLAGS);
+	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
 
 	return 0;
 }
-- 
2.0.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 3/5] src: table: Add set, unset and parse implementation for use value
  2014-06-26 17:08 [libnftnl PATCH 0/5] Do not print unset values in xml and json Ana Rey
  2014-06-26 17:08 ` [libnftnl PATCH 1/5] src: table: Free memory in the same function that is reserved Ana Rey
  2014-06-26 17:08 ` [libnftnl PATCH 2/5] src: table: Use nft_table_attr_set_* in the xml functions Ana Rey
@ 2014-06-26 17:08 ` Ana Rey
  2014-06-26 17:08 ` [libnftnl PATCH 4/5] src: table: Do not print unset values in xml file Ana Rey
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-06-26 17:08 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

Add some parts of the implemention of 'use' vualue in table that miss it.

These changes are neeeded for a correct import/export of xml/json file

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/table.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/table.c b/src/table.c
index de4a64d..5a0135f 100644
--- a/src/table.c
+++ b/src/table.c
@@ -72,8 +72,7 @@ void nft_table_attr_unset(struct nft_table *t, uint16_t attr)
 	case NFT_TABLE_ATTR_FAMILY:
 		break;
 	case NFT_TABLE_ATTR_USE:
-		/* Cannot be unset, ignoring it */
-		return;
+		break;
 	}
 	t->flags &= ~(1 << attr);
 }
@@ -106,8 +105,8 @@ void nft_table_attr_set_data(struct nft_table *t, uint16_t attr,
 		t->family = *((uint32_t *)data);
 		break;
 	case NFT_TABLE_ATTR_USE:
-		/* Cannot be set, ignoring it */
-		return;
+		t->use = *((uint32_t *)data);
+		break;
 	}
 	t->flags |= (1 << attr);
 }
@@ -263,7 +262,7 @@ int nft_mxml_table_parse(mxml_node_t *tree, struct nft_table *t,
 {
 	const char *name;
 	int family;
-	uint32_t flags;
+	uint32_t flags, use;
 
 	name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST,
 				  NFT_XML_MAND, err);
@@ -282,6 +281,10 @@ int nft_mxml_table_parse(mxml_node_t *tree, struct nft_table *t,
 		return -1;
 	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
 
+	if (nft_mxml_num_parse(tree, "use", MXML_DESCEND, BASE_DEC,
+			       &use, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
+		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_USE, use);
+
 	return 0;
 }
 #endif
@@ -310,7 +313,7 @@ int nft_jansson_parse_table(struct nft_table *t, json_t *tree,
 			    struct nft_parse_err *err)
 {
 	json_t *root;
-	uint32_t flags;
+	uint32_t flags, use;
 	const char *str;
 	int family;
 
@@ -334,6 +337,9 @@ int nft_jansson_parse_table(struct nft_table *t, json_t *tree,
 
 	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
 
+	if (nft_jansson_parse_val(root, "use", NFT_TYPE_U32, &use, err) == 0)
+		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_USE, use);
+
 	return 0;
 }
 #endif
-- 
2.0.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 4/5] src: table: Do not print unset values in xml file
  2014-06-26 17:08 [libnftnl PATCH 0/5] Do not print unset values in xml and json Ana Rey
                   ` (2 preceding siblings ...)
  2014-06-26 17:08 ` [libnftnl PATCH 3/5] src: table: Add set, unset and parse implementation for use value Ana Rey
@ 2014-06-26 17:08 ` Ana Rey
  2014-06-26 17:08 ` [libnftnl PATCH 5/5] src: table: Do not print unset values in json file Ana Rey
  2014-06-30 10:49 ` [libnftnl PATCH 0/5] Do not print unset values in xml and json Pablo Neira Ayuso
  5 siblings, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-06-26 17:08 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

It changes the parse and snprintf functions to omit unset values.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/table.c | 47 ++++++++++++++++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 13 deletions(-)

diff --git a/src/table.c b/src/table.c
index 5a0135f..7874245 100644
--- a/src/table.c
+++ b/src/table.c
@@ -266,20 +266,17 @@ int nft_mxml_table_parse(mxml_node_t *tree, struct nft_table *t,
 
 	name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST,
 				  NFT_XML_MAND, err);
-	if (name == NULL)
-		return -1;
-	nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, name);
+	if (name != NULL)
+		nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, name);
 
 	family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST,
 				       NFT_XML_MAND, err);
-	if (family < 0)
-		return -1;
-	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
+	if (family >= 0)
+		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
 
 	if (nft_mxml_num_parse(tree, "flags", MXML_DESCEND, BASE_DEC,
-			       &flags, NFT_TYPE_U32, NFT_XML_MAND, err) != 0)
-		return -1;
-	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
+			       &flags, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
+		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
 
 	if (nft_mxml_num_parse(tree, "use", MXML_DESCEND, BASE_DEC,
 			       &use, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
@@ -424,10 +421,34 @@ static int nft_table_snprintf_json(char *buf, size_t size, struct nft_table *t)
 
 static int nft_table_snprintf_xml(char *buf, size_t size, struct nft_table *t)
 {
-	return snprintf(buf, size, "<table><name>%s</name><family>%s</family>"
-			"<flags>%d</flags><use>%d</use></table>",
-			t->name, nft_family2str(t->family),
-			t->table_flags, t->use);
+	int ret, len = size, offset = 0;
+
+	ret = snprintf(buf, size, "<table>");
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	if (t->flags & (1 << NFT_TABLE_ATTR_NAME)) {
+		ret = snprintf(buf + offset, size, "<name>%s</name>", t->name);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (t->flags & (1 << NFT_TABLE_ATTR_FAMILY)) {
+		ret = snprintf(buf + offset, size, "<family>%s</family>",
+			       nft_family2str(t->family));
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (t->flags & (1 << NFT_TABLE_ATTR_FLAGS)) {
+		ret = snprintf(buf + offset, size, "<flags>%u</flags>",
+			       t->table_flags);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (t->flags & (1 << NFT_TABLE_ATTR_USE)) {
+		ret = snprintf(buf + offset, size, "<use>%u</use>", t->use);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	ret = snprintf(buf + offset, size, "</table>");
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	return offset;
 }
 
 static int nft_table_snprintf_default(char *buf, size_t size, struct nft_table *t)
-- 
2.0.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 5/5] src: table: Do not print unset values in json file
  2014-06-26 17:08 [libnftnl PATCH 0/5] Do not print unset values in xml and json Ana Rey
                   ` (3 preceding siblings ...)
  2014-06-26 17:08 ` [libnftnl PATCH 4/5] src: table: Do not print unset values in xml file Ana Rey
@ 2014-06-26 17:08 ` Ana Rey
  2014-06-30 10:49 ` [libnftnl PATCH 0/5] Do not print unset values in xml and json Pablo Neira Ayuso
  5 siblings, 0 replies; 7+ messages in thread
From: Ana Rey @ 2014-06-26 17:08 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Ana Rey

It changes the parse and snprintf functions to omit unset values.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/table.c | 64 ++++++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 42 insertions(+), 22 deletions(-)

diff --git a/src/table.c b/src/table.c
index 7874245..abbaa52 100644
--- a/src/table.c
+++ b/src/table.c
@@ -319,20 +319,15 @@ int nft_jansson_parse_table(struct nft_table *t, json_t *tree,
 		return -1;
 
 	str = nft_jansson_parse_str(root, "name", err);
-	if (str == NULL)
-		return -1;
-
-	nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, str);
-
-	if (nft_jansson_parse_family(root, &family, err) != 0)
-		return -1;
-
-	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
+	if (str != NULL)
+		nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, str);
 
-	if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U32, &flags, err) < 0)
-		return -1;
+	if (nft_jansson_parse_family(root, &family, err) == 0)
+		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
 
-	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
+	if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U32, &flags,
+				  err) == 0)
+		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags);
 
 	if (nft_jansson_parse_val(root, "use", NFT_TYPE_U32, &use, err) == 0)
 		nft_table_attr_set_u32(t, NFT_TABLE_ATTR_USE, use);
@@ -407,16 +402,41 @@ EXPORT_SYMBOL(nft_table_parse_file);
 
 static int nft_table_snprintf_json(char *buf, size_t size, struct nft_table *t)
 {
-	return snprintf(buf, size,
-			"{\"table\":{"
-			"\"name\":\"%s\","
-			"\"family\":\"%s\","
-			"\"flags\":%d,"
-			"\"use\":%d"
-			"}"
-			"}" ,
-			t->name, nft_family2str(t->family),
-			t->table_flags, t->use);
+	int ret, len = size, offset = 0;
+
+	ret = snprintf(buf, size, "{\"table\":{");
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	ret = 0;
+
+	if (t->flags & (1 << NFT_TABLE_ATTR_NAME)) {
+		ret = snprintf(buf + offset, size, "\"name\":\"%s\",", t->name);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (t->flags & (1 << NFT_TABLE_ATTR_FAMILY)) {
+		ret = snprintf(buf + offset, size, "\"family\":\"%s\",",
+			       nft_family2str(t->family));
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (t->flags & (1 << NFT_TABLE_ATTR_FLAGS)) {
+		ret = snprintf(buf + offset, size, "\"flags\":%u,",
+			       t->table_flags);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (t->flags & (1 << NFT_TABLE_ATTR_USE)) {
+		ret = snprintf(buf + offset, size, "\"use\":%u,", t->use);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	/* If  some values is set, ret is not 0. So, It's needed to remove the
+	 * last comma characther
+	 */
+	if (ret > 0)
+		offset--;
+
+	ret = snprintf(buf + offset, size, "}}");
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	return offset;
 }
 
 static int nft_table_snprintf_xml(char *buf, size_t size, struct nft_table *t)
-- 
2.0.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [libnftnl PATCH 0/5] Do not print unset values in xml and json
  2014-06-26 17:08 [libnftnl PATCH 0/5] Do not print unset values in xml and json Ana Rey
                   ` (4 preceding siblings ...)
  2014-06-26 17:08 ` [libnftnl PATCH 5/5] src: table: Do not print unset values in json file Ana Rey
@ 2014-06-30 10:49 ` Pablo Neira Ayuso
  5 siblings, 0 replies; 7+ messages in thread
From: Pablo Neira Ayuso @ 2014-06-30 10:49 UTC (permalink / raw)
  To: Ana Rey; +Cc: netfilter-devel

On Thu, Jun 26, 2014 at 07:08:36PM +0200, Ana Rey wrote:
> This patchset does tasks about does not print unset value in xml and json file
> and about refactoring code:
> 
> * src: table: Free memory in the same function that is reserved
> * src: table: Use nft_table_attr_set_* in the xml functions
> * src: table: Add set, unset and parse implementation for use value
> 
>  Moreover, I add some implementations for use value.
> 
> Ana Rey (5):
>   src: table: Free memory in the same function that is reserved
>   src: table: Use nft_table_attr_set_* in the xml functions
>   src: table: Add set, unset and parse implementation for use value
>   src: table: Do not print unset values in xml file
>   src: table: Do not print unset values in json file

Series applied, thanks Ana.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-06-30 10:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-26 17:08 [libnftnl PATCH 0/5] Do not print unset values in xml and json Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 1/5] src: table: Free memory in the same function that is reserved Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 2/5] src: table: Use nft_table_attr_set_* in the xml functions Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 3/5] src: table: Add set, unset and parse implementation for use value Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 4/5] src: table: Do not print unset values in xml file Ana Rey
2014-06-26 17:08 ` [libnftnl PATCH 5/5] src: table: Do not print unset values in json file Ana Rey
2014-06-30 10:49 ` [libnftnl PATCH 0/5] Do not print unset values in xml and json Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).