Do not print unset values in xml and json
cover Do not print unset values in xml and json
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(-)
Signature · 1 line
2.0.0
PATCH 1/5 src: table: Free memory in the same function that is reserved
Free memory in the same function that is reserved.
--- src/table.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
src/table.c +9 -8
@@ -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 ;
Signature · 1 line
2.0.0
PATCH 2/5 src: table: Use nft_table_attr_set_* in the xml functions
In reply to Ana ReyCode refactoring to use nft_table_attr_set_* in parse xml functions.
--- src/table.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)
src/table.c +5 -13
@@ -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 ; }
Signature · 1 line
2.0.0
PATCH 3/5 src: table: Add set, unset and parse implementation for use value
In reply to Ana ReyAdd 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
--- src/table.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
src/table.c +12 -6
@@ -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
Signature · 1 line
2.0.0
PATCH 4/5 src: table: Do not print unset values in xml file
In reply to Ana ReyIt changes the parse and snprintf functions to omit unset values.
--- src/table.c | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-)
src/table.c +34 -13
@@ -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)
Signature · 1 line
2.0.0
PATCH 5/5 src: table: Do not print unset values in json file
In reply to Ana ReyIt changes the parse and snprintf functions to omit unset values.
--- src/table.c | 64 ++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 22 deletions(-)
src/table.c +42 -22
@@ -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)
Signature · 1 line
2.0.0
Pablo Neira Ayuso
maintainerQuoted 7 lines · 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
Quoted 8 lines
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.