Series Add Json Support
cover Series Add Json Support
I have done the function for exporting set to JSON Format and I have changed nft-table-get for exporting tables in other families.
---
Álvaro Neira Ayuso (2):
set: add Json Export Support
examples: nft-table-get different families options
examples/nft-set-elem-get.c | 15 ++++++++---- examples/nft-set-get.c | 15 ++++++++---- examples/nft-table-get.c | 26 ++++++++++++++++---- include/libnftables/set.h | 5 ++++ src/set.c | 56 +++++++++++++++++++++++++++++++++++++++++-- src/set_elem.c | 49 ++++++++++++++++++++++++++++++++++++-- 6 files changed, 147 insertions(+), 19 deletions(-)
Signature · 5 lines
Álvaro Neira Ayuso -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 1/2 set: add Json Export Support
From: Álvaro Neira Ayuso <[email protected]>
--- examples/nft-set-elem-get.c | 15 ++++++++---- examples/nft-set-get.c | 15 ++++++++---- include/libnftables/set.h | 5 ++++ src/set.c | 56 +++++++++++++++++++++++++++++++++++++++++-- src/set_elem.c | 49 ++++++++++++++++++++++++++++++++++++-- 5 files changed, 126 insertions(+), 14 deletions(-)
examples/nft-set-elem-get.c +10 -5
@@ -23,6 +23,7 @@ static int set_cb(const struct nlmsghdr *nlh, void *data) {struct nft_set *t;char buf[4096 ]; +uint32_t *type = data; t =nft_set_alloc ();if (t ==NULL ) {
@@ -35,7 +36,7 @@ static int set_cb(const struct nlmsghdr *nlh, void *data) goto err_free; } -nft_set_snprintf (buf,sizeof (buf), t,0 ,0 ); +nft_set_snprintf (buf,sizeof (buf), t, *type,0 );printf ("%s\n" , buf); err_free:
@@ -50,11 +51,12 @@ int main(int argc, char *argv[])char buf[MNL_SOCKET_BUFFER_SIZE ];struct nlmsghdr *nlh;uint32_t portid, seq, family; +uint32_t type =NFT_SET_O_DEFAULT ;struct nft_set *t =NULL ;int ret; -if (argc !=4 ) { -fprintf (stderr,"%s <family> <table> <set>\n" , argv[0 ]); +if (argc <4 || argc >5 ) { +fprintf (stderr,"%s <family> <table> <set> [default|json]\n" , argv[0 ]);return EXIT_FAILURE ; } t =nft_set_alloc ();
@@ -67,13 +69,16 @@ int main(int argc, char *argv[]) family =AF_INET ; else if (strcmp(argv[1], "ip6") == 0) family =AF_INET6 ; - else if (strcmp(argv[2], "bridge") == 0) + else if (strcmp(argv[1], "bridge") == 0) family =AF_BRIDGE ; else {fprintf (stderr,"Unknown family: ip, ip6, bridge\n" );exit (EXIT_FAILURE ); } +if (argc ==5 &&strcmp (argv[4 ],"json" ) ==0 ) + type =NFT_SET_O_JSON ; + nlh =nft_set_nlmsg_build_hdr (buf,NFT_MSG_GETSETELEM , family,NLM_F_DUMP |NLM_F_ACK , seq);nft_set_attr_set (t,NFT_SET_ATTR_NAME , argv[3 ]);
@@ -100,7 +105,7 @@ int main(int argc, char *argv[]) ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));while (ret >0 ) { - ret =mnl_cb_run (buf, ret, seq, portid, set_cb,NULL ); + ret =mnl_cb_run (buf, ret, seq, portid, set_cb, &type);if (ret <=0 )break ; ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));
examples/nft-set-get.c +10 -5
@@ -23,6 +23,7 @@ static int set_cb(const struct nlmsghdr *nlh, void *data) {struct nft_set *t;char buf[4096 ]; +uint32_t *type = data; t =nft_set_alloc ();if (t ==NULL ) {
@@ -35,7 +36,7 @@ static int set_cb(const struct nlmsghdr *nlh, void *data) goto err_free; } -nft_set_snprintf (buf,sizeof (buf), t,0 ,0 ); +nft_set_snprintf (buf,sizeof (buf), t, *type,0 );printf ("%s\n" , buf); err_free:
@@ -50,11 +51,12 @@ int main(int argc, char *argv[])char buf[MNL_SOCKET_BUFFER_SIZE ];struct nlmsghdr *nlh;uint32_t portid, seq, family; +uint32_t type =NFT_SET_O_DEFAULT ;struct nft_set *t =NULL ;int ret; -if (argc !=3 ) { -fprintf (stderr,"%s <family> <table>\n" , argv[0 ]); +if (argc <3 || argc >4 ) { +fprintf (stderr,"%s <family> <table> [default|json]\n" , argv[0 ]);return EXIT_FAILURE ; } t =nft_set_alloc ();
@@ -67,13 +69,16 @@ int main(int argc, char *argv[]) family =AF_INET ; else if (strcmp(argv[1], "ip6") == 0) family =AF_INET6 ; - else if (strcmp(argv[2], "bridge") == 0) + else if (strcmp(argv[1], "bridge") == 0) family =AF_BRIDGE ; else {fprintf (stderr,"Unknown family: ip, ip6, bridge\n" );exit (EXIT_FAILURE ); } +if (argc ==4 &&strcmp (argv[3 ],"json" ) ==0 ) + type =NFT_SET_O_JSON ; + nlh =nft_set_nlmsg_build_hdr (buf,NFT_MSG_GETSET , family,NLM_F_DUMP |NLM_F_ACK , seq);nft_set_attr_set (t,NFT_SET_ATTR_TABLE , argv[2 ]);
@@ -99,7 +104,7 @@ int main(int argc, char *argv[]) ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));while (ret >0 ) { - ret =mnl_cb_run (buf, ret, seq, portid, set_cb,NULL ); + ret =mnl_cb_run (buf, ret, seq, portid, set_cb, &type);if (ret <=0 )break ; ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));
include/libnftables/set.h +5 -0
@@ -62,6 +62,11 @@ enum { NFT_SET_ELEM_ATTR_DATA, }; +enum { +NFT_SET_O_DEFAULT =0 , +NFT_SET_O_JSON , +}; +struct nft_set_elem ;struct nft_set_elem *nft_set_elem_alloc (void );
src/set.c +54 -2
68 lines not on this page · raw message
src/set_elem.c +47 -2
61 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerRe: [libnftables PATCH 1/2] set: add Json Export Support
Quoted 10 lines · Alvaro Neira
From: Álvaro Neira Ayuso <[email protected]>
Signed-off-by Alvaro Neira Ayuso <[email protected]>--- examples/nft-set-elem-get.c | 15 ++++++++---- examples/nft-set-get.c | 15 ++++++++---- include/libnftables/set.h | 5 ++++ src/set.c | 56 +++++++++++++++++++++++++++++++++++++++++-- src/set_elem.c | 49 ++++++++++++++++++++++++++++++++++++-- 5 files changed, 126 insertions(+), 14 deletions(-)
Applied, thanks Alvaro.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 2/2 examples: nft-table-get different families options
In reply to Alvaro NeiraFrom: Álvaro Neira Ayuso <[email protected]>
--- examples/nft-table-get.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
examples/nft-table-get.c +21 -5
@@ -50,11 +50,27 @@ int main(int argc, char *argv[])struct mnl_socket *nl;char buf[MNL_SOCKET_BUFFER_SIZE ];struct nlmsghdr *nlh; -uint32_t portid, seq; +uint32_t portid, seq, family;struct nft_table *t =NULL ;int ret;uint32_t type =NFT_TABLE_O_DEFAULT ; +if (argc <2 || argc >4 ) { +fprintf (stderr,"%s <family> [<table>] [default|xml|json]\n" , argv[0 ]); +return EXIT_FAILURE ; + } + +if (strcmp (argv[1 ],"ip" ) ==0 ) + family =AF_INET ; +else if (strcmp (argv[1 ],"ip6" ) ==0 ) + family =AF_INET6 ; +else if (strcmp (argv[1 ],"bridge" ) ==0 ) + family =AF_BRIDGE ; +else { +fprintf (stderr,"Unknown family: ip, ip6, bridge\n" ); +exit (EXIT_FAILURE ); + } +if (strcmp (argv[argc-1 ],"xml" ) ==0 ) { type =NFT_TABLE_O_XML ; argv[argc-1 ] =NULL ;
@@ -67,7 +83,7 @@ int main(int argc, char *argv[]) argc--; } -if (argc ==2 ) { +if (argc ==3 ) { t =nft_table_alloc ();if (t ==NULL ) {perror ("OOM" );
@@ -77,12 +93,12 @@ int main(int argc, char *argv[]) seq =time (NULL );if (t ==NULL ) { - nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_GETTABLE ,AF_INET , + nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_GETTABLE , family,NLM_F_DUMP , seq); }else { - nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_GETTABLE ,AF_INET , + nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_GETTABLE , family,NLM_F_ACK , seq); -nft_table_attr_set (t,NFT_TABLE_ATTR_NAME , argv[1 ]); +nft_table_attr_set (t,NFT_TABLE_ATTR_NAME , argv[2 ]);nft_table_nlmsg_build_payload (nlh, t);nft_table_free (t); }
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerRe: [libnftables PATCH 2/2] examples: nft-table-get different families options
Quoted 6 lines · Alvaro Neira
From: Álvaro Neira Ayuso <[email protected]>
Signed-off-by Alvaro Neira Ayuso <[email protected]>--- examples/nft-table-get.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
Also applied, thanks.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html