introduce libnftables
cover introduce libnftables
Hello,
This patchset adds a basi high level libnftables to nftables code. It is currently supporting running a command from a buffer or from a file as well as batch support allowing to chain commands and commit them at once.
The API is mostly using existing structures such as nft_ctx that are updated to contain enough information. It also adds a structure dedicated to batch.
A simple program running a command is the following:
nft_global_init();
nft = nft_context_new();
nft_context_set_print_func(nft, my_print, buf);
rc = nft_run_command_from_buffer(nft, CMD, sizeof(CMD));
if (rc != NFT_EXIT_SUCCESS) {
nft_get_error(nft, err_buf, sizeof(err_buf));
printf("%s\n", err_buf);
return -1;
}
nft_context_free(nft);
nft_global_deinit(); Transaction support is similar with:
nft = nft_context_new();
batch = nft_batch_start(nft);
if (nft_batch_add(nft, batch, ADD1, strlen(ADD1)) !=0) {
printf("FAIL add 1\n");
goto out;
}
if (nft_batch_add(nft, batch, ADD2, strlen(ADD2)) !=0) {
printf("FAIL add 2\n");
goto out;
}
if (nft_batch_commit(nft, batch) != 0) {
goto out;
}out:
nft_batch_free(batch); nft_context_free(nft); nft_global_deinit();
The library provides a way to get standard output via nft_context_set_print_func and error handling is done via nft_get_error that get error message in a buffer.
This is early stage code as it does not feature things like set handling but IMO it can already be used as a starting point to build more things.
BR,
--
Eric
PATCH 1/16 libnftables: introduce library
Add global init and deinit functions.
--- configure.ac | 4 ++++ include/Makefile.am | 2 +- include/nftables/Makefile.am | 2 ++ include/nftables/nftables.h | 20 +++++++++++++++++ src/Makefile.am | 26 +++++++++++++--------- src/libnftables.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 27 +++------------------- 7 files changed, 99 insertions(+), 35 deletions(-) create mode 100644 include/nftables/Makefile.am create mode 100644 include/nftables/nftables.h create mode 100644 src/libnftables.c
configure.ac +4 -0
@@ -56,6 +56,9 @@ then exit 1 fi +AM_PROG_AR +AM_PROG_LIBTOOL + AC_CHECK_PROG(DOCBOOK2X_MAN, [docbook2x-man], [docbook2x-man], [no]) AC_CHECK_PROG(DOCBOOK2MAN, [docbook2man], [docbook2man], [no]) AC_CHECK_PROG(DB2X_DOCBOOK2MAN, [db2x_docbook2man], [db2x_docbook2man], [no])
@@ -146,6 +149,7 @@ AC_CONFIG_FILES([ \ include/linux/netfilter_bridge/Makefile \ include/linux/netfilter_ipv4/Makefile \ include/linux/netfilter_ipv6/Makefile \ + include/nftables/Makefile \ doc/Makefile \ files/Makefile \ files/nftables/Makefile \
include/Makefile.am +1 -1
@@ -1,4 +1,4 @@ -SUBDIRS = linux +SUBDIRS = linux nftables noinst_HEADERS = cli.h \ datatype.h \
include/nftables/Makefile.am new file +2 -0
@@ -0,0 +1,2 @@ +pkginclude_HEADERS = nftables.h +
include/nftables/nftables.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* Copyright (c) 2017 Eric Leblond <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ + +#ifndef LIB_NFTABLES_H +#define LIB_NFTABLES_H + +#include <stdlib.h> +#include <stdint.h> +#include <stdbool.h> + +void nft_global_init (void ); +void nft_global_deinit (void ); + +#endif
src/Makefile.am +16 -10
@@ -27,8 +27,9 @@ parser_bison.o scanner.o: AM_CFLAGS += -Wno-missing-prototypes -Wno-missing-decl BUILT_SOURCES = parser_bison.h -nft_SOURCES = main.c \ - rule.c \ +lib_LTLIBRARIES = libnftables.la + +libnftables_la_SOURCES = rule.c \ statement.c \ datatype.c \ expression.c \
@@ -54,23 +55,28 @@ nft_SOURCES = main.c \ iface.c \ services.c \ mergesort.c \ + libnftables.c \ scanner.l \ tcpopt.c \ parser_bison.y -if BUILD_CLI -nft_SOURCES += cli.c -endif - if BUILD_MINIGMP mini-gmp.o: AM_CFLAGS += -Wno-sign-compare -nft_SOURCES += mini-gmp.c +libnftables_la_SOURCES += mini-gmp.c endif -nft_LDADD = ${LIBMNL_LIBS} ${LIBNFTNL_LIBS} +nft_SOURCES = main.c + +if BUILD_CLI +nft_SOURCES += cli.c +endif + +libnftables_la_LIBADD = ${LIBMNL_LIBS} ${LIBNFTNL_LIBS} if BUILD_XTABLES -nft_SOURCES += xt.c -nft_LDADD += ${XTABLES_LIBS} +libnftables_la_SOURCES += xt.c +libnftables_la_LIBADD += ${XTABLES_LIBS} endif + +nft_LDADD = libnftables.la
src/libnftables.c new file +53 -0
53 lines not on this page · raw message
src/main.c +3 -24
@@ -18,6 +18,7 @@#include <fcntl.h> #include <sys/types.h> +#include <nftables/nftables.h> #include <nftables.h> #include <utils.h> #include <parser.h>
@@ -272,28 +273,6 @@ err1:return ret; } -void nft_init (void ) -{ -mark_table_init (); -realm_table_rt_init (); -devgroup_table_init (); -realm_table_meta_init (); -ct_label_table_init (); -gmp_init (); -#ifdef HAVE_LIBXTABLES -xt_init (); -#endif -} - -void nft_exit (void ) -{ -ct_label_table_exit (); -realm_table_rt_exit (); -devgroup_table_exit (); -realm_table_meta_exit (); -mark_table_exit (); -} -int main (int argc,char *const *argv) {struct parser_state state;
@@ -309,7 +288,7 @@ int main(int argc, char * const *argv)memset (&cache,0 ,sizeof (cache));init_list_head (&cache.list); -nft_init (); +nft_global_init (); nf_sock =netlink_open_sock ();while (1 ) { val =getopt_long (argc, argv,OPTSTRING , options,NULL );
@@ -440,7 +419,7 @@ out:cache_release (&cache);iface_cache_release ();netlink_close_sock (nf_sock); -nft_exit (); +nft_global_deinit ();return rc; }
Signature · 1 line
2.14.1
Phil Sutter
reviewerRe: [nft PATH 01/16] libnftables: introduce library
On Wed, Aug 16, 2017 at 10:42:55PM +0200, Eric Leblond wrote:
[...]
Quoted 27 lines
src/libnftables.c new file +21
-0@@ -0,0 +1,53 @@ +/* +* Copyright (c) 2017 Eric Leblond <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ + +#include <nftables/nftables.h> +#include <string.h> +#include <errno.h> +#include <nftables.h> +#include <parser.h> +#include <iface.h> +#include <netlink.h> +#include <erec.h> +#include <libmnl/libmnl.h> +#include <mnl.h> + +#include <unistd.h>
src/libnftables.c line 31
Quoted 10 lines
src/libnftables.c new file +10
-0+#include <fcntl.h> + + +unsigned int max_errors =10 ; +unsigned int numeric_output; +unsigned int ip2name_output; +unsigned int handle_output; +#ifdef DEBUG +unsigned int debug_level; +#endif
I guess these global variables have to die. I tried static linking (for quick testing without installing) which didn't work because max_errors is then redefined here. (There is a stale one in main.c which you probably just forgot to remove.)
Maybe max_errors could become part of struct nft_ctx? Alternatively I would make it static and add a getter to be used from parser_bison.y.
The remaining ones apart from debug_level are leftovers, they live in struct output_ctx now.
I am not sure where debug_level really belongs to. One one hand, it has something to do with printing, so struct output_ctx. On the other, nft_ctx would have to be made reachable from all places where debug output happens.
Maybe the whole debugging infrastructure (i.e. debug data dumpers and printers) should be exported to the application?
Quoted 12 lines
src/libnftables.c new file +12
-0+ +void nft_global_init (void ) +{ +mark_table_init (); +realm_table_rt_init (); +devgroup_table_init (); +realm_table_meta_init (); +ct_label_table_init (); +gmp_init (); +#ifdef HAVE_LIBXTABLES +xt_init (); +#endif
src/libnftables.c line 53
Quoted 10 lines
src/libnftables.c new file +10
-0+} + +void nft_global_deinit (void ) +{ +ct_label_table_exit (); +realm_table_rt_exit (); +devgroup_table_exit (); +realm_table_meta_exit (); +mark_table_exit (); +}
How about calling these from nft_context_new() and nft_context_free()?
Cheers, Phil
Re: [nft PATH 01/16] libnftables: introduce library
Hi,
Quoted 49 lines · Phil Sutter
On Wed, Aug 16, 2017 at 10:42:55PM +0200, Eric Leblond wrote:
[...]Quoted 29 lines
src/libnftables.c new file +4
-0@@ -0,0 +1,53 @@ +/* +* Copyright (c) 2017 Eric Leblond <[email protected]> +* +* This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */src/libnftables.c new file +13
-0+ +#include <nftables/nftables.h> +#include <string.h> +#include <errno.h> +#include <nftables.h> +#include <parser.h> +#include <iface.h> +#include <netlink.h> +#include <erec.h> +#include <libmnl/libmnl.h> +#include <mnl.h> + +#include <unistd.h> src/libnftables.c line 31
Quoted 10 lines
src/libnftables.c new file +10
-0+#include <fcntl.h> + + +unsigned int max_errors =10 ; +unsigned int numeric_output; +unsigned int ip2name_output; +unsigned int handle_output; +#ifdef DEBUG +unsigned int debug_level; +#endif I guess these global variables have to die. I tried static linking (for
quick testing without installing) which didn't work becausemax_errors
is then redefined here. (There is a stale one inmain.cwhich you probably just forgot to remove.)
Quoted 2 lines
Maybe
max_errorscould become part of structnft_ctx? Alternatively I would make it static and add a getter to be used from parser_bison.y.
I'm gonna remove the all but max_errors and see how I could addmax_errors somewhere.
Quoted 2 lines
The remaining ones apart from
debug_levelare leftovers, they live in structoutput_ctxnow.
Indeed. Good catch.
Quoted 26 lines
I am not sure where
debug_levelreally belongs to. One one hand, it has
something to do with printing, so structoutput_ctx. On the other,nft_ctxwould have to be made reachable from all places where debug output happens.Maybe the whole debugging infrastructure (i.e. debug data dumpers and printers) should be exported to the application?
Quoted 17 lines
src/libnftables.c new file +17
-0+ +void nft_global_init (void ) +{ +mark_table_init (); +realm_table_rt_init (); +devgroup_table_init (); +realm_table_meta_init (); +ct_label_table_init (); +gmp_init (); +#ifdef HAVE_LIBXTABLES +xt_init (); +#endif +} + +void nft_global_deinit (void ) +{ +ct_label_table_exit ();
Quoted 8 lines
src/libnftables.c line 53
Quoted 5 lines
src/libnftables.c new file +5
-0+realm_table_rt_exit (); +devgroup_table_exit (); +realm_table_meta_exit (); +mark_table_exit (); +}How about calling these from
nft_context_new()andnft_context_free()?
I want to be able to have multiple context for a single process. Hence I defined a global init and deinit. But maybe it does not really make sense and could be attached to each context or init could be done at first usage.
++
Signature · 2 lines
Eric Leblond <[email protected]> Blog: https://home.regit.org/
Phil Sutter
reviewerRe: [nft PATH 01/16] libnftables: introduce library
Hey,
Quoted 2 lines · Eric Leblond
Quoted 1 line · Phil Sutter
On Wed, Aug 16, 2017 at 10:42:55PM +0200, Eric Leblond wrote:
[...]
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
src/libnftables.c new file +16
-0+void nft_global_init (void ) +{ +mark_table_init (); +realm_table_rt_init (); +devgroup_table_init (); +realm_table_meta_init (); +ct_label_table_init (); +gmp_init (); +#ifdef HAVE_LIBXTABLES +xt_init (); +#endif +} + +void nft_global_deinit (void ) +{ +ct_label_table_exit ();Quoted 8 lines
src/libnftables.c line 53
Quoted 5 lines
src/libnftables.c new file +5
-0+realm_table_rt_exit (); +devgroup_table_exit (); +realm_table_meta_exit (); +mark_table_exit (); +}How about calling these from
nft_context_new()andnft_context_free()?
Quoted 4 lines
I want to be able to have multiple context for a single process. Hence I defined a global init and deinit. But maybe it does not really make sense and could be attached to each context or init could be done at first usage.
My idea was to implement simple reference counting to see whether the library was already initialized and whether it is safe to deinit. Of course this needs some serialization for thread-safety.
Or maybe the deinit can be ignored completely and nft_global_init() just has to check whether data is already initialized or not.
Cheers, Phil
Re: [nft PATH 01/16] libnftables: introduce library
Hi,
Quoted 40 lines · Phil Sutter
Hey,
Quoted 2 lines · Eric Leblond
Quoted 1 line · Phil Sutter
On Wed, Aug 16, 2017 at 10:42:55PM +0200, Eric Leblond wrote:
[...]
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
src/libnftables.c new file +16
-0+void nft_global_init (void ) +{ +mark_table_init (); +realm_table_rt_init (); +devgroup_table_init (); +realm_table_meta_init (); +ct_label_table_init (); +gmp_init (); +#ifdef HAVE_LIBXTABLES +xt_init (); +#endif +} + +void nft_global_deinit (void ) +{ +ct_label_table_exit ();Quoted 8 lines
src/libnftables.c line 53
Quoted 5 lines
src/libnftables.c new file +5
-0+realm_table_rt_exit (); +devgroup_table_exit (); +realm_table_meta_exit (); +mark_table_exit (); +}How about calling these from
nft_context_new()andnft_context_free()?Quoted 7 lines
I want to be able to have multiple context for a single process. Hence
I defined a global init and deinit. But maybe it does not really make
sense and could be attached to each context or init could be done at
first usage.
Quoted 7 lines
My idea was to implement simple reference counting to see whether the library was already initialized and whether it is safe to deinit. Of course this needs some serialization for thread-safety.
Or maybe the deinit can be ignored completely and
nft_global_init()just
has to check whether data is already initialized or not.
I have used numerous libraries providing a global init or deinit. For now we could easily skip it with your approach or another but I prefer we have it existing so it is available later in case of needs.
++
Signature · 2 lines
Eric Leblond <[email protected]> Blog: https://home.regit.org/
Re: [nft PATH 01/16] libnftables: introduce library
Quoted 8 lines · Eric Leblond
Quoted 8 lines
Quoted 3 lines
Hence I defined a global init and deinit. But maybe it does not really make sense and could be attached to each context or init could be done at first usage.
My idea was to implement simple reference counting to see whether the library was already initialized and whether it is safe to deinit. Of course this needs some serialization for thread-safety.
Quoted 8 lines
Quoted 3 lines
Or maybe the deinit can be ignored completely and
nft_global_init()just has to check whether data is already initialized or not.I have used numerous libraries providing a global init or deinit. For now we could easily skip it with your approach or another but I prefer we have it existing so it is available later in case of needs.
If a global init - more correctly, a singleton init - is needed, can't it just implicitly be called from make_me_a_new_context()?
Pablo Neira Ayuso
maintainerRe: [nft PATH 01/16] libnftables: introduce library
Quoted 15 lines · Jan Engelhardt
Quoted 12 lines · Eric Leblond
Quoted 11 lines
Quoted 3 lines
Hence I defined a global init and deinit. But maybe it does not really make sense and could be attached to each context or init could be done at first usage.
My idea was to implement simple reference counting to see whether the library was already initialized and whether it is safe to deinit. Of course this needs some serialization for thread-safety.
Or maybe the deinit can be ignored completely and
nft_global_init()just has to check whether data is already initialized or not.
Quoted 7 lines
Quoted 4 lines
I have used numerous libraries providing a global init or deinit. For now we could easily skip it with your approach or another but I prefer we have it existing so it is available later in case of needs.
If a global init - more correctly, a singleton init - is needed, can't it just implicitly be called from
make_me_a_new_context()?
I would advocate for placing the nft_global_init() and deinit() code into the ctx object too.
Those _init() and _deinit() are indeed initializing context information, such as iproute/ct label maps.
So they are context after all.
PATCH 2/16 libnftables: add context new and free
In reply to Eric Leblond--- include/nftables.h | 1 + include/nftables/nftables.h | 3 +++ src/libnftables.c | 20 ++++++++++++++++++++ src/main.c | 27 +++++++++++++-------------- 4 files changed, 37 insertions(+), 14 deletions(-)
include/nftables.h +1 -0
@@ -35,6 +35,7 @@ struct output_ctx {struct nft_ctx {struct output_ctx output;bool check; +struct mnl_socket *nf_sock; };struct nft_cache {
include/nftables/nftables.h +3 -0
@@ -17,4 +17,7 @@void nft_global_init (void );void nft_global_deinit (void ); +struct nft_ctx *nft_context_new (void ); +void nft_context_free (struct nft_ctx *nft); +#endif
src/libnftables.c +20 -0
@@ -51,3 +51,23 @@ void nft_global_deinit(void)realm_table_meta_exit ();mark_table_exit (); } + +struct nft_ctx *nft_context_new (void ) +{ +struct nft_ctx *ctx =NULL ; + ctx =malloc (sizeof (struct nft_ctx )); +if (ctx ==NULL ) +return NULL ; + ctx->nf_sock =netlink_open_sock (); + +return ctx; +} + + +void nft_context_free (struct nft_ctx *nft) +{ +if (nft ==NULL ) +return ; +netlink_close_sock (nft->nf_sock); +xfree (nft); +}
src/main.c +13 -14
@@ -29,7 +29,6 @@#include <iface.h> #include <cli.h> -static struct nft_ctx nft;unsigned int max_errors =10 ;#ifdef DEBUG unsigned int debug_level;
@@ -283,13 +282,13 @@ int main(int argc, char * const *argv)unsigned int len;bool interactive = false;int i, val, rc =NFT_EXIT_SUCCESS ; -struct mnl_socket *nf_sock; +struct nft_ctx *nft;memset (&cache,0 ,sizeof (cache));init_list_head (&cache.list);nft_global_init (); - nf_sock =netlink_open_sock (); + nft =nft_context_new ();while (1 ) { val =getopt_long (argc, argv,OPTSTRING , options,NULL );if (val ==-1 )
@@ -304,7 +303,7 @@ int main(int argc, char * const *argv) PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME);exit (NFT_EXIT_SUCCESS );case OPT_CHECK : - nft.check = true; + nft->check = true;break ;case OPT_FILE : filename = optarg;
@@ -322,7 +321,7 @@ int main(int argc, char * const *argv) include_paths[num_include_paths++] = optarg;break ;case OPT_NUMERIC : -if (++nft.output.numeric >NUMERIC_ALL ) { +if (++nft->output.numeric >NUMERIC_ALL ) {fprintf (stderr,"Too many numeric options " "used, max. %u\n" ,NUMERIC_ALL );
@@ -330,10 +329,10 @@ int main(int argc, char * const *argv) }break ;case OPT_STATELESS : - nft.output.stateless++; + nft->output.stateless++;break ;case OPT_IP2NAME : - nft.output.ip2name++; + nft->output.ip2name++;break ;#ifdef DEBUG case OPT_DEBUG :
@@ -365,7 +364,7 @@ int main(int argc, char * const *argv)break ;#endif case OPT_HANDLE_OUTPUT : - nft.output.handle++; + nft->output.handle++;break ;case OPT_ECHO : nft.output.echo++;
@@ -386,20 +385,20 @@ int main(int argc, char * const *argv)strcat (buf," " ); }strcat (buf,"\n" ); -parser_init (nf_sock, &cache, &state, &msgs); +parser_init (nft->nf_sock, &cache, &state, &msgs); scanner =scanner_init (&state);scanner_push_buffer (scanner, &indesc_cmdline, buf); }else if (filename !=NULL ) { - rc =cache_update (nf_sock, &cache,CMD_INVALID , &msgs); + rc =cache_update (nft->nf_sock, &cache,CMD_INVALID , &msgs);if (rc <0 )return rc; -parser_init (nf_sock, &cache, &state, &msgs); +parser_init (nft->nf_sock, &cache, &state, &msgs); scanner =scanner_init (&state);if (scanner_read_file (scanner, filename, &internal_location) <0 ) goto out; }else if (interactive) { -if (cli_init (&nft, nf_sock, &cache, &state) <0 ) { +if (cli_init (nft, nft->nf_sock, &cache, &state) <0 ) {fprintf (stderr,"%s: interactive CLI not supported in this build\n" , argv[0 ]);exit (NFT_EXIT_FAILURE );
@@ -410,7 +409,7 @@ int main(int argc, char * const *argv)exit (NFT_EXIT_FAILURE ); } -if (nft_run (&nft, nf_sock, &cache, scanner, &state, &msgs) !=0 ) +if (nft_run (nft, nft->nf_sock, &cache, scanner, &state, &msgs) !=0 ) rc =NFT_EXIT_FAILURE ; out:scanner_destroy (scanner);
@@ -418,7 +417,7 @@ out:xfree (buf);cache_release (&cache);iface_cache_release (); -netlink_close_sock (nf_sock); +nft_context_free (nft);nft_global_deinit ();return rc;
Signature · 1 line
2.14.1
Phil Sutter
reviewerRe: [nft PATH 02/16] libnftables: add context new and free
On Wed, Aug 16, 2017 at 10:42:56PM +0200, Eric Leblond wrote:
[...]
src/libnftables.c void nft_global_deinit(void) · line 58
Quoted 13 lines
src/libnftables.c +5
-0@@ -51,3 +51,23 @@ void nft_global_deinit(void)realm_table_meta_exit ();mark_table_exit (); } + +struct nft_ctx *nft_context_new (void ) +{ +struct nft_ctx *ctx =NULL ; + ctx =malloc (sizeof (struct nft_ctx ));
Use calloc() here to avoid having garbage in 'output' and 'check' fields.
src/libnftables.c void nft_global_deinit(void) · line 64
Quoted 6 lines
src/libnftables.c +6
-0+if (ctx ==NULL ) +return NULL ; + ctx->nf_sock =netlink_open_sock (); + +return ctx; +}
Cheers, Phil
PATCH 3/16 libnftables: add nft_run_command_from_buffer
In reply to Eric Leblond--- include/nftables/nftables.h | 3 +++ src/libnftables.c | 26 +++++++++++++++++++++++++- src/main.c | 21 +++++++++------------ 3 files changed, 37 insertions(+), 13 deletions(-)
include/nftables/nftables.h +3 -0
@@ -20,4 +20,7 @@ void nft_global_deinit(void);struct nft_ctx *nft_context_new (void );void nft_context_free (struct nft_ctx *nft); +int nft_run_command_from_buffer (struct nft_ctx *nft,struct nft_cache *cache, +char *buf,size_t buflen); +#endif
src/libnftables.c +25 -1
@@ -7,7 +7,6 @@* */ -#include <nftables/nftables.h> #include <string.h> #include <errno.h> #include <nftables.h>
@@ -18,6 +17,8 @@#include <libmnl/libmnl.h> #include <mnl.h> +#include <nftables/nftables.h> +#include <unistd.h> #include <fcntl.h>
@@ -71,3 +72,26 @@ void nft_context_free(struct nft_ctx *nft)netlink_close_sock (nft->nf_sock);xfree (nft); } + +static const struct input_descriptor indesc_cmdline = { + .type =INDESC_BUFFER , + .name ="<cmdline>" , +}; + +int nft_run_command_from_buffer (struct nft_ctx *nft,struct nft_cache *cache, +char *buf,size_t buflen) +{ +int rc =NFT_EXIT_SUCCESS ; +struct parser_state state; +LIST_HEAD (msgs); +void *scanner; + +parser_init (nft->nf_sock, cache, &state, &msgs); + scanner =scanner_init (&state); +scanner_push_buffer (scanner, &indesc_cmdline, buf); + +if (nft_run (nft, nft->nf_sock, cache, scanner, &state, &msgs) !=0 ) + rc =NFT_EXIT_FAILURE ; + +return rc; +}
src/main.c +9 -12
@@ -18,8 +18,8 @@#include <fcntl.h> #include <sys/types.h> -#include <nftables/nftables.h> #include <nftables.h> +#include <nftables/nftables.h> #include <utils.h> #include <parser.h> #include <rule.h>
@@ -182,11 +182,6 @@ static const struct { };#endif -static const struct input_descriptor indesc_cmdline = { - .type =INDESC_BUFFER , - .name ="<cmdline>" , -}; -static int nft_netlink (struct nft_ctx *nft,struct nft_cache *cache,struct parser_state *state,struct list_head *msgs,struct mnl_socket *nf_sock)
@@ -367,7 +362,7 @@ int main(int argc, char * const *argv) nft->output.handle++;break ;case OPT_ECHO : - nft.output.echo++; + nft->output.echo++;break ;case OPT_INVALID :exit (NFT_EXIT_FAILURE );
@@ -385,9 +380,10 @@ int main(int argc, char * const *argv)strcat (buf," " ); }strcat (buf,"\n" ); -parser_init (nft->nf_sock, &cache, &state, &msgs); - scanner =scanner_init (&state); -scanner_push_buffer (scanner, &indesc_cmdline, buf); + rc =nft_run_command_from_buffer (nft, &cache, buf, len +2 ); +if (rc <0 ) +return rc; + goto libout; }else if (filename !=NULL ) { rc =cache_update (nft->nf_sock, &cache,CMD_INVALID , &msgs);if (rc <0 )
@@ -397,6 +393,8 @@ int main(int argc, char * const *argv) scanner =scanner_init (&state);if (scanner_read_file (scanner, filename, &internal_location) <0 ) goto out; +if (nft_run (nft, nft->nf_sock, &cache, scanner, &state, &msgs) !=0 ) + rc =NFT_EXIT_FAILURE ; }else if (interactive) {if (cli_init (nft, nft->nf_sock, &cache, &state) <0 ) { fprintf(stderr, "%s: interactive CLI not supported in this build\n",
@@ -409,11 +407,10 @@ int main(int argc, char * const *argv)exit (NFT_EXIT_FAILURE ); } -if (nft_run (nft, nft->nf_sock, &cache, scanner, &state, &msgs) !=0 ) - rc =NFT_EXIT_FAILURE ; out:scanner_destroy (scanner);erec_print_list (stderr, &msgs); +libout:xfree (buf);cache_release (&cache);iface_cache_release ();
Signature · 1 line
2.14.1
Phil Sutter
reviewerRe: [nft PATH 03/16] libnftables: add nft_run_command_from_buffer
On Wed, Aug 16, 2017 at 10:42:57PM +0200, Eric Leblond wrote:
[...]
Quoted 13 lines
src/libnftables.c +0
-1@@ -7,7 +7,6 @@* */ -#include <nftables/nftables.h> #include <string.h> #include <errno.h> #include <nftables.h>
src/libnftables.c line 23
Quoted 7 lines
src/libnftables.c +2
-0@@ -18,6 +17,8 @@#include <libmnl/libmnl.h> #include <mnl.h> +#include <nftables/nftables.h> +#include <unistd.h> #include <fcntl.h>
Wat? :)
BTW, someone once told me "headers shouldn't include other headers", though I sometimes doubt that was the whole truth. What do you think, is "oh, you included our lib's header too early" a bug in the library or the application?
[...]
Quoted 2 lines
src/main.c +0
-0
[...]
src/main.c int main(int argc, char * const *argv) · line 365
Quoted 6 lines
src/main.c +1
-1@@ -367,7 +362,7 @@ int main(int argc, char * const *argv) nft->output.handle++;break ;case OPT_ECHO : - nft.output.echo++; + nft->output.echo++;
Rebase scars. :)
Cheers, Phil
PATCH 4/16 libnftables: add nft_run_command_from_filename
In reply to Eric LeblondAdd new function to read nftables command from a file
--- include/nftables/nftables.h | 2 ++ src/libnftables.c | 25 +++++++++++++++++++++++++ src/main.c | 16 +++------------- 3 files changed, 30 insertions(+), 13 deletions(-)
include/nftables/nftables.h +2 -0
@@ -22,5 +22,7 @@ void nft_context_free(struct nft_ctx *nft);int nft_run_command_from_buffer (struct nft_ctx *nft,struct nft_cache *cache,char *buf,size_t buflen); +int nft_run_command_from_filename (struct nft_ctx *nft,struct nft_cache *cache, +const char *filename);#endif
src/libnftables.c +25 -0
@@ -93,5 +93,30 @@ int nft_run_command_from_buffer(struct nft_ctx *nft, struct nft_cache *cache,if (nft_run (nft, nft->nf_sock, cache, scanner, &state, &msgs) !=0 ) rc =NFT_EXIT_FAILURE ; +scanner_destroy (scanner); +erec_print_list (stderr, &msgs); +return rc; +} + +int nft_run_command_from_filename (struct nft_ctx *nft,struct nft_cache *cache, +const char *filename) +{ +int rc =NFT_EXIT_SUCCESS ; +struct parser_state state; +LIST_HEAD (msgs); +void *scanner; + + rc =cache_update (nft->nf_sock, cache,CMD_INVALID , &msgs); +if (rc <0 ) +return rc; +parser_init (nft->nf_sock, cache, &state, &msgs); + scanner =scanner_init (&state); +if (scanner_read_file (scanner, filename, &internal_location) <0 ) +return NFT_EXIT_FAILURE ; +if (nft_run (nft, nft->nf_sock, cache, scanner, &state, &msgs) !=0 ) + rc =NFT_EXIT_FAILURE ; + +scanner_destroy (scanner); +erec_print_list (stderr, &msgs);return rc; }
src/main.c +3 -13
@@ -271,7 +271,6 @@ int main(int argc, char * const *argv) {struct parser_state state;struct nft_cache cache; -void *scanner;LIST_HEAD (msgs);char *buf =NULL , *filename =NULL ;unsigned int len;
@@ -383,18 +382,12 @@ int main(int argc, char * const *argv) rc =nft_run_command_from_buffer (nft, &cache, buf, len +2 );if (rc <0 )return rc; - goto libout; + goto out; }else if (filename !=NULL ) { - rc =cache_update (nft->nf_sock, &cache,CMD_INVALID , &msgs); + rc =nft_run_command_from_filename (nft, &cache, filename);if (rc <0 )return rc; - -parser_init (nft->nf_sock, &cache, &state, &msgs); - scanner =scanner_init (&state); -if (scanner_read_file (scanner, filename, &internal_location) <0 ) - goto out; -if (nft_run (nft, nft->nf_sock, &cache, scanner, &state, &msgs) !=0 ) - rc =NFT_EXIT_FAILURE ; + goto out; }else if (interactive) {if (cli_init (nft, nft->nf_sock, &cache, &state) <0 ) { fprintf(stderr, "%s: interactive CLI not supported in this build\n",
@@ -408,9 +401,6 @@ int main(int argc, char * const *argv) } out: -scanner_destroy (scanner); -erec_print_list (stderr, &msgs); -libout:xfree (buf);cache_release (&cache);iface_cache_release ();
Signature · 1 line
2.14.1
PATCH 5/16 libnftables: put nft_run in library
In reply to Eric Leblond--- include/nftables.h | 4 -- include/nftables_common.h | 18 ++++++++ src/Makefile.am | 1 + src/cli.c | 1 + src/libnftables.c | 2 + src/main.c | 85 ------------------------------------- src/nftables_common.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 126 insertions(+), 89 deletions(-) create mode 100644 include/nftables_common.h create mode 100644 src/nftables_common.c
include/nftables.h +0 -4
@@ -124,10 +124,6 @@ struct input_descriptor {struct parser_state ;struct mnl_socket ; -int nft_run (struct nft_ctx *nft,struct mnl_socket *nf_sock, -struct nft_cache *cache,void *scanner,struct parser_state *state, -struct list_head *msgs); -void ct_label_table_init (void );void mark_table_init (void );void gmp_init (void );
include/nftables_common.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* Copyright (c) 2017 Eric Leblond <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ + + +#ifndef NFTABLES_NFTABLES_COMMON_H +#define NFTABLES_NFTABLES_COMMON_H + +int nft_run (struct nft_ctx *nft,struct mnl_socket *nf_sock, +struct nft_cache *cache,void *scanner,struct parser_state *state, +struct list_head *msgs); + +#endif
src/Makefile.am +1 -0
@@ -58,6 +58,7 @@ libnftables_la_SOURCES = rule.c \ libnftables.c \ scanner.l \ tcpopt.c \ + nftables_common.c \ parser_bison.y if BUILD_MINIGMP
src/cli.c +1 -0
@@ -30,6 +30,7 @@#include <utils.h> #include <iface.h> #include <cli.h> +#include <nftables_common.h> #include <libmnl/libmnl.h>
src/libnftables.c +2 -0
@@ -16,6 +16,8 @@#include <erec.h> #include <libmnl/libmnl.h> #include <mnl.h> +#include <netlink.h> +#include <nftables_common.h> #include <nftables/nftables.h>
src/main.c +0 -85
91 lines not on this page · raw message
src/nftables_common.c new file +104 -0
104 lines not on this page · raw message
Signature · 1 line
2.14.1
PATCH 6/16 libnftables: add missing variable to library
In reply to Eric Leblond--- src/libnftables.c | 2 ++ src/main.c | 1 - 2 files changed, 2 insertions(+), 1 deletion(-)
src/libnftables.c +2 -0
@@ -33,6 +33,8 @@ unsigned int handle_output;unsigned int debug_level;#endif +const char *include_paths[INCLUDE_PATHS_MAX ] = {DEFAULT_INCLUDE_PATH }; +void nft_global_init (void ) {mark_table_init ();
src/main.c +0 -1
@@ -34,7 +34,6 @@ unsigned int max_errors = 10;unsigned int debug_level;#endif -const char *include_paths[INCLUDE_PATHS_MAX ] = {DEFAULT_INCLUDE_PATH };static unsigned int num_include_paths =1 ;enum opt_vals {
Signature · 1 line
2.14.1
Phil Sutter
reviewerRe: [nft PATH 06/16] libnftables: add missing variable to library
Quoted 11 lines · Eric Leblond
Signed-off-by Eric Leblond <[email protected]>--- src/libnftables.c | 2 ++ src/main.c | 1 - 2 files changed, 2 insertions(+), 1 deletion(-)src/libnftables.c +0
-0
src/libnftables.c unsigned int handle_output; · line 37
Quoted 5 lines
src/libnftables.c +2
-0@@ -33,6 +33,8 @@ unsigned int handle_output;unsigned int debug_level;#endif +const char *include_paths[INCLUDE_PATHS_MAX ] = {DEFAULT_INCLUDE_PATH }; +
I guess this belongs into nft_ctx as well.
Cheers, Phil
Re: [nft PATH 06/16] libnftables: add missing variable to library
Hi,
Quoted 7 lines · Phil Sutter
Quoted 6 lines · Eric Leblond
Signed-off-by Eric Leblond <[email protected]>---
src/libnftables.c | 2 ++
src/main.c | 1 -2 files changed, 2 insertions(+), 1 deletion(-)
Quoted 13 lines
Quoted 11 lines
src/libnftables.c +0
-0@@ -33,6 +33,8 @@ unsigned int handle_output; unsigned int debug_level; #endif
+const char *include_paths[INCLUDE_PATHS_MAX] = {DEFAULT_INCLUDE_PATH};
+I guess this belongs into
nft_ctxas well.
Yes, it could be done later.
++
Signature · 2 lines
Eric Leblond <[email protected]> Blog: https://home.regit.org/
PATCH 7/16 libnftables: add NFT_EXIT_* to library
In reply to Eric LeblondSo it can be used by client to check function return.
--- include/nftables.h | 7 ------- include/nftables/nftables.h | 7 +++++++ src/netlink.c | 1 + src/utils.c | 1 + 4 files changed, 9 insertions(+), 7 deletions(-)
include/nftables.h +0 -7
@@ -48,13 +48,6 @@ extern unsigned int max_errors;extern unsigned int debug_level;extern const char *include_paths[INCLUDE_PATHS_MAX ]; -enum nftables_exit_codes { -NFT_EXIT_SUCCESS =0 , -NFT_EXIT_FAILURE =1 , -NFT_EXIT_NOMEM =2 , -NFT_EXIT_NONL =3 , -}; -struct input_descriptor ;struct location {const struct input_descriptor *indesc;
include/nftables/nftables.h +7 -0
@@ -14,6 +14,13 @@#include <stdint.h> #include <stdbool.h> +enum nftables_exit_codes { +NFT_EXIT_SUCCESS =0 , +NFT_EXIT_FAILURE =1 , +NFT_EXIT_NOMEM =2 , +NFT_EXIT_NONL =3 , +}; +void nft_global_init (void );void nft_global_deinit (void );
src/netlink.c +1 -0
@@ -30,6 +30,7 @@#include <linux/netfilter.h> #include <nftables.h> +#include <nftables/nftables.h> #include <netlink.h> #include <mnl.h> #include <expression.h>
src/utils.c +1 -0
@@ -16,6 +16,7 @@#include <string.h> #include <nftables.h> +#include <nftables/nftables.h> #include <utils.h> void __noreturn __memory_allocation_error (const char *filename,uint32_t line)
Signature · 1 line
2.14.1
PATCH 8/16 libnftables: add a nft_cache to nft_ctx
In reply to Eric LeblondHide this structure from the user, this allows simplify the simple functions by just providing easy and meaningfull arguments.
--- include/cli.h | 2 +- include/nftables.h | 13 +++++++------ include/nftables/nftables.h | 5 ++--- src/cli.c | 10 ++++++++-- src/libnftables.c | 19 +++++++++++-------- src/main.c | 11 +++-------- 6 files changed, 32 insertions(+), 28 deletions(-)
include/cli.h +1 -1
@@ -6,7 +6,7 @@struct parser_state ;#ifdef HAVE_LIBREADLINE extern int cli_init (struct nft_ctx *nft,struct mnl_socket *nf_sock, -struct nft_cache *cache,struct parser_state *state); +struct parser_state *state);#else static inline int cli_init (struct nft_ctx *nft,struct mnl_socket *nf_sock,struct nft_cache *cache,struct parser_state *state)
include/nftables.h +7 -6
@@ -32,18 +32,19 @@ struct output_ctx {unsigned int echo; }; -struct nft_ctx { -struct output_ctx output; -bool check; -struct mnl_socket *nf_sock; -}; -struct nft_cache {bool initialized;struct list_head list;uint32_t seqnum; }; +struct nft_ctx { +struct output_ctx output; +bool check; +struct mnl_socket *nf_sock; +struct nft_cache cache; +}; +extern unsigned int max_errors;extern unsigned int debug_level;extern const char *include_paths[INCLUDE_PATHS_MAX ];
include/nftables/nftables.h +2 -3
@@ -27,9 +27,8 @@ void nft_global_deinit(void);struct nft_ctx *nft_context_new (void );void nft_context_free (struct nft_ctx *nft); -int nft_run_command_from_buffer (struct nft_ctx *nft,struct nft_cache *cache, +int nft_run_command_from_buffer (struct nft_ctx *nft,char *buf,size_t buflen); -int nft_run_command_from_filename (struct nft_ctx *nft,struct nft_cache *cache, -const char *filename); +int nft_run_command_from_filename (struct nft_ctx *nft,const char *filename);#endif
src/cli.c +8 -2
@@ -177,13 +177,17 @@ void __fmtstring(1, 0) cli_display(const char *fmt, va_list ap) }int cli_init (struct nft_ctx *nft,struct mnl_socket *nf_sock, -struct nft_cache *cache,struct parser_state *_state) +struct parser_state *_state) {const char *home; +struct nft_cache cache; + +memset (&cache,0 ,sizeof (cache)); +init_list_head (&cache.list); cli_nf_sock = nf_sock; cli_nft = *nft; - cli_cache = cache; + cli_cache = &cache; rl_readline_name ="nft" ; rl_instream = stdin; rl_outstream = stdout;
@@ -204,6 +208,8 @@ int cli_init(struct nft_ctx *nft, struct mnl_socket *nf_sock,while (!eof)rl_callback_read_char (); + +cache_release (&cache);return 0 ; }
src/libnftables.c +11 -8
@@ -63,7 +63,10 @@ struct nft_ctx *nft_context_new(void) ctx =malloc (sizeof (struct nft_ctx ));if (ctx ==NULL )return NULL ; + +memset (ctx,0 ,sizeof (*ctx)); ctx->nf_sock =netlink_open_sock (); +init_list_head (&ctx->cache.list);return ctx; }
@@ -74,6 +77,7 @@ void nft_context_free(struct nft_ctx *nft)if (nft ==NULL )return ;netlink_close_sock (nft->nf_sock); +cache_release (&nft->cache);xfree (nft); }
@@ -82,7 +86,7 @@ static const struct input_descriptor indesc_cmdline = { .name = "<cmdline>", }; -int nft_run_command_from_buffer (struct nft_ctx *nft,struct nft_cache *cache, +int nft_run_command_from_buffer (struct nft_ctx *nft,char *buf,size_t buflen) {int rc =NFT_EXIT_SUCCESS ;
@@ -90,11 +94,11 @@ int nft_run_command_from_buffer(struct nft_ctx *nft, struct nft_cache *cache,LIST_HEAD (msgs);void *scanner; -parser_init (nft->nf_sock, cache, &state, &msgs); +parser_init (nft->nf_sock, &nft->cache, &state, &msgs); scanner =scanner_init (&state);scanner_push_buffer (scanner, &indesc_cmdline, buf); -if (nft_run (nft, nft->nf_sock, cache, scanner, &state, &msgs) !=0 ) +if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, &state, &msgs) !=0 ) rc =NFT_EXIT_FAILURE ;scanner_destroy (scanner);
@@ -102,22 +106,21 @@ int nft_run_command_from_buffer(struct nft_ctx *nft, struct nft_cache *cache,return rc; } -int nft_run_command_from_filename (struct nft_ctx *nft,struct nft_cache *cache, -const char *filename) +int nft_run_command_from_filename (struct nft_ctx *nft,const char *filename) {int rc =NFT_EXIT_SUCCESS ;struct parser_state state;LIST_HEAD (msgs);void *scanner; - rc =cache_update (nft->nf_sock, cache,CMD_INVALID , &msgs); + rc =cache_update (nft->nf_sock, &nft->cache,CMD_INVALID , &msgs);if (rc <0 )return rc; -parser_init (nft->nf_sock, cache, &state, &msgs); +parser_init (nft->nf_sock, &nft->cache, &state, &msgs); scanner =scanner_init (&state);if (scanner_read_file (scanner, filename, &internal_location) <0 )return NFT_EXIT_FAILURE ; -if (nft_run (nft, nft->nf_sock, cache, scanner, &state, &msgs) !=0 ) +if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, &state, &msgs) !=0 ) rc =NFT_EXIT_FAILURE ;scanner_destroy (scanner);
src/main.c +3 -8
@@ -184,7 +184,6 @@ static const struct {int main (int argc,char *const *argv) {struct parser_state state; -struct nft_cache cache;LIST_HEAD (msgs);char *buf =NULL , *filename =NULL ;unsigned int len;
@@ -192,9 +191,6 @@ int main(int argc, char * const *argv)int i, val, rc =NFT_EXIT_SUCCESS ;struct nft_ctx *nft; -memset (&cache,0 ,sizeof (cache)); -init_list_head (&cache.list); -nft_global_init (); nft =nft_context_new ();while (1 ) {
@@ -293,17 +289,17 @@ int main(int argc, char * const *argv)strcat (buf," " ); }strcat (buf,"\n" ); - rc =nft_run_command_from_buffer (nft, &cache, buf, len +2 ); + rc =nft_run_command_from_buffer (nft, buf, len +2 );if (rc <0 )return rc; goto out; }else if (filename !=NULL ) { - rc =nft_run_command_from_filename (nft, &cache, filename); + rc =nft_run_command_from_filename (nft, filename);if (rc <0 )return rc; goto out; }else if (interactive) { -if (cli_init (nft, nft->nf_sock, &cache, &state) <0 ) { +if (cli_init (nft, nft->nf_sock, &state) <0 ) {fprintf (stderr,"%s: interactive CLI not supported in this build\n" , argv[0 ]);exit (NFT_EXIT_FAILURE );
@@ -316,7 +312,6 @@ int main(int argc, char * const *argv) out:xfree (buf); -cache_release (&cache);iface_cache_release ();nft_context_free (nft);nft_global_deinit ();
Signature · 1 line
2.14.1
Phil Sutter
reviewerRe: [nft PATH 08/16] libnftables: add a nft_cache to nft_ctx
On Wed, Aug 16, 2017 at 10:43:02PM +0200, Eric Leblond wrote:
[...]
include/cli.h line 9
Quoted 10 lines
include/cli.h +1
-1@@ -6,7 +6,7 @@struct parser_state ;#ifdef HAVE_LIBREADLINE extern int cli_init (struct nft_ctx *nft,struct mnl_socket *nf_sock, -struct nft_cache *cache,struct parser_state *state); +struct parser_state *state);
One could get rid of nf_sock parameter as well since that resides in nft_ctx, too.
[...]
Quoted 6 lines
src/cli.c +0
-0@@ -177,13 +177,17 @@ void __fmtstring(1, 0) cli_display(const char *fmt, va_list ap) }
src/cli.c void __fmtstring(1, 0) cli_display(const char *fmt, va_list ap) · line 186
Quoted 10 lines
src/cli.c +5
-1int cli_init (struct nft_ctx *nft,struct mnl_socket *nf_sock, -struct nft_cache *cache,struct parser_state *_state) +struct parser_state *_state) {const char *home; +struct nft_cache cache; + +memset (&cache,0 ,sizeof (cache)); +init_list_head (&cache.list);
Is this a leftover, or do you intentionally use a new cache instance instead of nft->cache?
Cheers, Phil
PATCH 9/16 libnftables: move iface_cache_release to deinit
In reply to Eric Leblond--- src/libnftables.c | 1 + src/main.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-)
src/libnftables.c +1 -0
@@ -50,6 +50,7 @@ void nft_global_init(void)void nft_global_deinit (void ) { +iface_cache_release ();ct_label_table_exit ();realm_table_rt_exit ();devgroup_table_exit ();
src/main.c +0 -1
@@ -312,7 +312,6 @@ int main(int argc, char * const *argv) out:xfree (buf); -iface_cache_release ();nft_context_free (nft);nft_global_deinit ();
Signature · 1 line
2.14.1
PATCH 10/16 libnftables: get rid of printf
In reply to Eric LeblondUse a custom print function that user will be able to set instead of using a direct call to printf.
--- include/datatype.h | 5 +- include/expression.h | 2 +- include/nftables.h | 2 + src/ct.c | 20 +++---- src/datatype.c | 61 +++++++++++--------- src/expression.c | 70 +++++++++++------------ src/exthdr.c | 8 +-- src/fib.c | 23 ++++---- src/hash.c | 10 ++-- src/libnftables.c | 14 +++++ src/meta.c | 26 ++++----- src/numgen.c | 4 +- src/payload.c | 4 +- src/rule.c | 159 ++++++++++++++++++++++++++------------------------- src/statement.c | 122 +++++++++++++++++++-------------------- 15 files changed, 279 insertions(+), 251 deletions(-)
include/datatype.h +3 -2
@@ -209,7 +209,8 @@ extern void symbolic_constant_print(const struct symbol_table *tbl, struct output_ctx *octx);extern void symbol_table_print (const struct symbol_table *tbl,const struct datatype *dtype, -enum byteorder byteorder); +enum byteorder byteorder, +struct output_ctx *octx);extern struct symbol_table *rt_symbol_table_init (const char *filename);extern void rt_symbol_table_free (struct symbol_table *tbl);
@@ -261,7 +262,7 @@ extern const struct datatype * set_datatype_alloc(const struct datatype *orig_dtype, unsigned int byteorder);extern void set_datatype_destroy (const struct datatype *dtype); -extern void time_print (uint64_t seconds); +extern void time_print (uint64_t seconds,struct output_ctx *octx);extern struct error_record *time_parse (const struct location *loc,const char *c,uint64_t *res);
include/expression.h +1 -1
@@ -334,7 +334,7 @@ extern struct expr *expr_get(struct expr *expr);extern void expr_free (struct expr *expr);extern void expr_print (const struct expr *expr,struct output_ctx *octx);extern bool expr_cmp (const struct expr *e1,const struct expr *e2); -extern void expr_describe (const struct expr *expr); +extern void expr_describe (const struct expr *expr,struct output_ctx *octx);extern const struct datatype *expr_basetype (const struct expr *expr); extern void expr_set_type(struct expr *expr, const struct datatype *dtype,
include/nftables.h +2 -0
@@ -30,6 +30,8 @@ struct output_ctx {unsigned int ip2name;unsigned int handle;unsigned int echo; +void *ctx; +int (*print)(void *ctx,const char *format, ...); };struct nft_cache {
src/ct.c +10 -10
@@ -141,11 +141,11 @@ static void ct_label_type_print(const struct expr *expr,for (s = ct_label_tbl->symbols; s->identifier !=NULL ; s++) {if (bit != s->value)continue ; -printf ("\"%s\"" , s->identifier); + octx->"\"%s\"" , s->identifier);return ; }/* can happen when connlabel.conf is altered after rules were added */ -printf ("%ld\n" , (long )mpz_scan1 (expr->value,0 )); + octx->"%ld\n" , (long )mpz_scan1 (expr->value,0 )); } static struct error_record *ct_label_type_parse(const struct expr *sym,
@@ -269,27 +269,27 @@ static const struct ct_template ct_templates[] = { BYTEORDER_HOST_ENDIAN, 32), }; -static void ct_print (enum nft_ct_keys key,int8_t dir) +static void ct_print (enum nft_ct_keys key,int8_t dir,struct output_ctx *octx) {const struct symbolic_constant *s; -printf ("ct " ); + octx->"ct " );if (dir <0 ) goto done;for (s = ct_dir_tbl.symbols; s->identifier !=NULL ; s++) {if (dir == (int )s->value) { -printf ("%s " , s->identifier); + octx->"%s " , s->identifier);break ; } } done: -printf ("%s" , ct_templates[key].token); + octx->"%s" , ct_templates[key].token); }static void ct_expr_print (const struct expr *expr,struct output_ctx *octx) { -ct_print (expr->ct.key, expr->ct.direction); +ct_print (expr->ct.key, expr->ct.direction, octx); }static bool ct_expr_cmp (const struct expr *e1,const struct expr *e2)
@@ -445,8 +445,8 @@ void ct_expr_update_type(struct proto_ctx *ctx, struct expr *expr)static void ct_stmt_print (const struct stmt *stmt,struct output_ctx *octx) { -ct_print (stmt->ct.key, stmt->ct.direction); -printf (" set " ); +ct_print (stmt->ct.key, stmt->ct.direction, octx); + octx->" set " );expr_print (stmt->ct.expr, octx); }
@@ -472,7 +472,7 @@ struct stmt *ct_stmt_alloc(const struct location *loc, enum nft_ct_keys key,static void notrack_stmt_print (const struct stmt *stmt,struct output_ctx *octx) { -printf ("notrack" ); + octx->"notrack" ); }static const struct stmt_ops notrack_stmt_ops = {
src/datatype.c +33 -28
176 lines not on this page · raw message
src/expression.c +35 -35
184 lines not on this page · raw message
src/exthdr.c +4 -4
@@ -33,19 +33,19 @@ static void exthdr_expr_print(const struct expr *expr, struct output_ctx *octx)char buf[9 ] = {0 };if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT ) { -printf ("tcp option %s" , expr->exthdr.desc->name); + octx->"tcp option %s" , expr->exthdr.desc->name);return ; }if (offset)snprintf (buf,sizeof buf,"%d" , offset); -printf ("tcp option %s%s %s" , expr->exthdr.desc->name, buf, + octx->"tcp option %s%s %s" , expr->exthdr.desc->name, buf, expr->exthdr.tmpl->token); }else {if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT ) -printf ("exthdr %s" , expr->exthdr.desc->name); + octx->"exthdr %s" , expr->exthdr.desc->name);else { -printf ("%s %s" , expr->exthdr.desc ? expr->exthdr.desc->name :"unknown-exthdr" , + octx->"%s %s" , expr->exthdr.desc ? expr->exthdr.desc->name :"unknown-exthdr" , expr->exthdr.tmpl->token); } }
src/fib.c +12 -11
@@ -60,32 +60,33 @@ static const char *fib_result_str(enum nft_fib_result result)return "unknown" ; } -static void __fib_expr_print_f (unsigned int *flags,unsigned int f,const char *s) +static void __fib_expr_print_f (unsigned int *flags,unsigned int f, +const char *s,struct output_ctx *octx) {if ((*flags & f) ==0 )return ; -printf ("%s" , s); + octx->"%s" , s); *flags &= ~f;if (*flags) -printf (" . " ); + octx->" . " ); }static void fib_expr_print (const struct expr *expr,struct output_ctx *octx) {unsigned int flags = expr->fib.flags & ~NFTA_FIB_F_PRESENT ; -printf ("fib " ); -__fib_expr_print_f (&flags,NFTA_FIB_F_SADDR ,"saddr" ); -__fib_expr_print_f (&flags,NFTA_FIB_F_DADDR ,"daddr" ); -__fib_expr_print_f (&flags,NFTA_FIB_F_MARK ,"mark" ); -__fib_expr_print_f (&flags,NFTA_FIB_F_IIF ,"iif" ); -__fib_expr_print_f (&flags,NFTA_FIB_F_OIF ,"oif" ); + octx->"fib " ); +__fib_expr_print_f (&flags,NFTA_FIB_F_SADDR ,"saddr" , octx); +__fib_expr_print_f (&flags,NFTA_FIB_F_DADDR ,"daddr" , octx); +__fib_expr_print_f (&flags,NFTA_FIB_F_MARK ,"mark" , octx); +__fib_expr_print_f (&flags,NFTA_FIB_F_IIF ,"iif" , octx); +__fib_expr_print_f (&flags,NFTA_FIB_F_OIF ,"oif" , octx);if (flags) -printf ("0x%x" , flags); + octx->"0x%x" , flags); -printf (" %s" ,fib_result_str (expr->fib.result)); + octx->" %s" ,fib_result_str (expr->fib.result)); }static bool fib_expr_cmp (const struct expr *e1,const struct expr *e2)
src/hash.c +5 -5
@@ -19,19 +19,19 @@ static void hash_expr_print(const struct expr *expr, struct output_ctx *octx) {switch (expr->hash.type) {case NFT_HASH_SYM : -printf ("symhash" ); + octx->"symhash" );break ;case NFT_HASH_JENKINS :default : -printf ("jhash " ); + octx->"jhash " );expr_print (expr->hash.expr, octx); } -printf (" mod %u" , expr->hash.mod); + octx->" mod %u" , expr->hash.mod);if (expr->hash.seed_set) -printf (" seed 0x%x" , expr->hash.seed); + octx->" seed 0x%x" , expr->hash.seed);if (expr->hash.offset) -printf (" offset %u" , expr->hash.offset); + octx->" offset %u" , expr->hash.offset); }static bool hash_expr_cmp (const struct expr *e1,const struct expr *e2)
src/libnftables.c +14 -0
@@ -58,6 +58,17 @@ void nft_global_deinit(void)mark_table_exit (); } +__attribute__((format (printf,2 ,0 ))) +static int nft_print (void *ctx,const char *fmt, ...) +{ +va_list arg; +va_start (arg, fmt); +vfprintf (stdout, fmt, arg); +va_end (arg); + +return 0 ; +} +struct nft_ctx *nft_context_new (void ) {struct nft_ctx *ctx =NULL ;
@@ -67,8 +78,11 @@ struct nft_ctx *nft_context_new(void)memset (ctx,0 ,sizeof (*ctx)); ctx->nf_sock =netlink_open_sock (); +init_list_head (&ctx->cache.list); + ctx->output.ctx = ctx; + ctx->output.print = nft_print;return ctx; }
src/meta.c +13 -13
@@ -54,13 +54,13 @@ static void tchandle_type_print(const struct expr *expr,switch (handle) {case TC_H_ROOT : -printf ("root" ); + octx->"root" );break ;case TC_H_UNSPEC : -printf ("none" ); + octx->"none" );break ;default : -printf ("%0x:%0x" ,TC_H_MAJ (handle) >>16 ,TC_H_MIN (handle)); + octx->"%0x:%0x" ,TC_H_MAJ (handle) >>16 ,TC_H_MIN (handle));break ; } }
@@ -134,9 +134,9 @@ static void ifindex_type_print(const struct expr *expr, struct output_ctx *octx) ifindex =mpz_get_uint32 (expr->value);if (nft_if_indextoname (ifindex, name)) -printf ("\"%s\"" , name); + octx->"\"%s\"" , name);else -printf ("%d" , ifindex); + octx->"%d" , ifindex); } static struct error_record *ifindex_type_parse(const struct expr *sym,
@@ -209,9 +209,9 @@ static void uid_type_print(const struct expr *expr, struct output_ctx *octx) pw =getpwuid (uid);if (pw !=NULL ) -printf ("\"%s\"" , pw->pw_name); + octx->"\"%s\"" , pw->pw_name);else -printf ("%d" , uid); + octx->"%d" , uid);return ; }expr_basetype (expr)->
@@ -261,9 +261,9 @@ static void gid_type_print(const struct expr *expr, struct output_ctx *octx) gr =getgrgid (gid);if (gr !=NULL ) -printf ("\"%s\"" , gr->gr_name); + octx->"\"%s\"" , gr->gr_name);else -printf ("%u" , gid); + octx->"%u" , gid);return ; }expr_basetype (expr)->
@@ -446,9 +446,9 @@ static bool meta_key_is_qualified(enum nft_meta_keys key)static void meta_expr_print (const struct expr *expr,struct output_ctx *octx) {if (meta_key_is_qualified (expr->meta.key)) -printf ("meta %s" , meta_templates[expr->meta.key].token); + octx->"meta %s" , meta_templates[expr->meta.key].token);else -printf ("%s" , meta_templates[expr->meta.key].token); + octx->"%s" , meta_templates[expr->meta.key].token); }static bool meta_expr_cmp (const struct expr *e1,const struct expr *e2)
@@ -573,9 +573,9 @@ struct expr *meta_expr_alloc(const struct location *loc, enum nft_meta_keys key)static void meta_stmt_print (const struct stmt *stmt,struct output_ctx *octx) {if (meta_key_is_qualified (stmt->meta.key)) -printf ("meta %s set " , meta_templates[stmt->meta.key].token); + octx->"meta %s set " , meta_templates[stmt->meta.key].token);else -printf ("%s set " , meta_templates[stmt->meta.key].token); + octx->"%s set " , meta_templates[stmt->meta.key].token);expr_print (stmt->meta.expr, octx); }
src/numgen.c +2 -2
@@ -30,10 +30,10 @@ static const char *numgen_type_str(enum nft_ng_types type)static void numgen_expr_print (const struct expr *expr,struct output_ctx *octx) { -printf ("numgen %s mod %u" ,numgen_type_str (expr->numgen.type), + octx->"numgen %s mod %u" ,numgen_type_str (expr->numgen.type), expr->numgen.mod);if (expr->numgen.offset) -printf (" offset %u" , expr->numgen.offset); + octx->" offset %u" , expr->numgen.offset); }static bool numgen_expr_cmp (const struct expr *e1,const struct expr *e2)
src/payload.c +2 -2
@@ -46,9 +46,9 @@ static void payload_expr_print(const struct expr *expr, struct output_ctx *octx) desc = expr->payload.desc; tmpl = expr->payload.tmpl;if (payload_is_known (expr)) -printf ("%s %s" , desc->name, tmpl->token); + octx->"%s %s" , desc->name, tmpl->token);else -printf ("payload @%s,%u,%u" , + octx->"payload @%s,%u,%u" , proto_base_tokens[expr->payload.base], expr->payload.offset, expr->len); }
src/rule.c +82 -77
403 lines not on this page · raw message
src/statement.c +61 -61
335 lines not on this page · raw message
Signature · 1 line
2.14.1
Phil Sutter
reviewerRe: [nft PATH 10/16] libnftables: get rid of printf
On Wed, Aug 16, 2017 at 10:43:04PM +0200, Eric Leblond wrote:
[...]
include/nftables.h struct output_ctx { · line 35
Quoted 11 lines
include/nftables.h +2
-0@@ -30,6 +30,8 @@ struct output_ctx {unsigned int ip2name;unsigned int handle;unsigned int echo; +void *ctx; +int (*print)(void *ctx,const char *format, ...); };
My compiler says:
| libnftables.c: In function 'nft_context_new': | libnftables.c:113:20: warning: assignment left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format] | ctx->output.print = nft_print; | ^
Not really a warning IMO, though.
Abstracting the topic a bit, maybe all these foo_print() callbacks should die eventually and be replaced by formatters for different output types, similar to nftnl_foo_snprintf() functions. Maybe the whole output formatting actually even belongs to the application and libnftables has to provide a way to extract object information. Not sure how much knowlege the application should (need to) have about internal data structures like e.g., struct table or struct expr.
Cheers, Phil
Re: [nft PATH 10/16] libnftables: get rid of printf
Hi,
Quoted 24 lines · Phil Sutter
On Wed, Aug 16, 2017 at 10:43:04PM +0200, Eric Leblond wrote:
[...]include/nftables.h struct output_ctx { · line 35
Quoted 11 lines
include/nftables.h +2
-0@@ -30,6 +30,8 @@ struct output_ctx { unsigned int ip2name; unsigned int handle; unsigned int echo; +void *ctx; +int (*print)(void *ctx,const char *format, ...); };My compiler says:
Quoted 5 lines
libnftables.c: In function 'nft_context_new':
libnftables.c:113:20: warning: assignment left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format]ctx->output.print=nft_print;
^Not really a warning IMO, though.
Quoted 10 lines
Abstracting the topic a bit, maybe all these
foo_print()callbacks should die eventually and be replaced by formatters for different output
types, similar tonftnl_foo_snprintf()functions. Maybe the whole output
formatting actually even belongs to the application and libnftables has
to provide a way to extract object information. Not sure how much knowlege the application should (need to) have about internal data structures like e.g., struct table or struct expr.
Agree, this was a first approach. Not sending structured data is problem in a modern library.
We have already dumping of object in JSON or XML. It maybe could be used as communication method to send structured data to the user applications. Don't know if it is a lazy way but maybe it could be enough.
++
Signature · 2 lines
Eric Leblond <[email protected]> Blog: https://home.regit.org/
PATCH 11/16 libnftables: add nft_context_set_print
In reply to Eric LeblondThis function allows user to set his own printing function. It is still dependant of the format used by nft but at least it can be redirected easily.
--- include/nftables/nftables.h | 3 +++ src/libnftables.c | 9 +++++++++ 2 files changed, 12 insertions(+)
include/nftables/nftables.h +3 -0
@@ -26,6 +26,9 @@ void nft_global_deinit(void);struct nft_ctx *nft_context_new (void );void nft_context_free (struct nft_ctx *nft); +void nft_context_set_print_func (struct nft_ctx *nft, +int (*print)(void *ctx,const char *fmt, ...), +void *ctx);int nft_run_command_from_buffer (struct nft_ctx *nft,char *buf,size_t buflen);
src/libnftables.c +9 -0
@@ -86,6 +86,15 @@ struct nft_ctx *nft_context_new(void)return ctx; } +void nft_context_set_print_func (struct nft_ctx *nft, +int (*print)(void *ctx,const char *fmt, ...), +void *ctx) +{ +if (nft) { + nft->output.print = print; + nft->output.ctx = ctx; + } +}void nft_context_free (struct nft_ctx *nft) {
Signature · 1 line
2.14.1
PATCH 12/16 libnftables: transaction support
In reply to Eric LeblondAdd batch support for libnftables. This patch provides a set of function to handle batch command.
The cinematic is the following:
* nft_batch_start to allocate the batch structure
* multiple call to nft_batch_add
* nft_batch_commit to do the commit to kernel
* nft_batch_free to free the batch
--- include/netlink.h | 5 +++ include/nftables/nftables.h | 7 ++++ src/libnftables.c | 89 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+)
include/netlink.h +5 -0
@@ -54,6 +54,11 @@ struct netlink_ctx {struct nft_cache *cache; }; +struct nft_batch { +struct netlink_ctx nl_ctx; +struct nftnl_batch *batch; +}; +extern struct nftnl_table *alloc_nftnl_table (const struct handle *h);extern struct nftnl_chain *alloc_nftnl_chain (const struct handle *h);extern struct nftnl_rule *alloc_nftnl_rule (const struct handle *h);
include/nftables/nftables.h +7 -0
@@ -34,4 +34,11 @@ int nft_run_command_from_buffer(struct nft_ctx *nft, char *buf, size_t buflen);int nft_run_command_from_filename (struct nft_ctx *nft,const char *filename); + +struct nft_batch *nft_batch_start (struct nft_ctx *ctx); +int nft_batch_add (struct nft_ctx *ctx,struct nft_batch *batch, +const char * buf,size_t buflen); +int nft_batch_commit (struct nft_ctx *ctx,struct nft_batch *batch); +void nft_batch_free (struct nft_batch *batch); +#endif
src/libnftables.c +89 -0
92 lines not on this page · raw message
Signature · 1 line
2.14.1
Phil Sutter
reviewerRe: [nft PATH 12/16] libnftables: transaction support
On Wed, Aug 16, 2017 at 10:43:06PM +0200, Eric Leblond wrote:
[...]
Quoted 10 lines
src/libnftables.c +2
-0@@ -151,3 +151,92 @@ int nft_run_command_from_filename(struct nft_ctx *nft, const char *filename)erec_print_list (stderr, &msgs);return rc; } + +struct nft_batch *nft_batch_start (struct nft_ctx *nft)
src/libnftables.c int nft_run_command_from_filename(struct nft_ctx *nft, const char *filename) · line 165
Quoted 10 lines
src/libnftables.c +10
-0+{ +struct nft_batch *batch =malloc (sizeof (*batch)); +if (batch ==NULL ) +return NULL ; + + batch->batch =mnl_batch_init (); +mnl_batch_begin (batch->batch,mnl_seqnum_alloc (&nft->cache.seqnum)); + +return batch; +}
Don't you need to initialize batch->nl_ctx here as well?
src/libnftables.c int nft_run_command_from_filename(struct nft_ctx *nft, const char *filename) · line 178
Quoted 12 lines
src/libnftables.c +12
-0+int nft_batch_add (struct nft_ctx *nft,struct nft_batch *batch, +const char * buf,size_t buflen) +{ +int rc =NFT_EXIT_SUCCESS ; +int ret =0 ; +struct parser_state state; +LIST_HEAD (msgs); +void *scanner; +struct cmd *cmd, *next; +struct netlink_ctx *ctx = &batch->nl_ctx; +uint32_t seqnum; +bool batch_supported =netlink_batch_supported (nft->nf_sock, &seqnum);
That should be an error condition in nft_batch_start(). If it turns out to be false, later calls to do_command() will execute instead of adding to batch buffer.
Quoted 35 lines
src/libnftables.c +35
-0+parser_init (nft->nf_sock, &nft->cache, &state, &msgs); + scanner =scanner_init (&state); +scanner_push_buffer (scanner, &indesc_cmdline, buf); + + ret =nft_parse (scanner, &state); +if (ret !=0 || state.nerrs >0 ) { + rc =-1 ; + goto err1; + } + +list_for_each_entry (cmd, &state.cmds, list) { +nft_cmd_expand (cmd); +memset (ctx,0 ,sizeof (*ctx)); + ctx->msgs = &msgs; + ctx->seqnum = cmd->seqnum =mnl_seqnum_alloc (&seqnum); + ctx->batch = batch->batch; + ctx->batch_supported = batch_supported; + ctx->octx = &nft->output; + ctx->nf_sock = nft->nf_sock; + ctx->cache = &nft->cache; +init_list_head (&ctx->list); + ret =do_command (ctx, cmd); +if (ret <0 ) +return -1 ; + } + +list_for_each_entry_safe (cmd, next, &state.cmds, list) { +list_del (&cmd->list); +cmd_free (cmd); + } +err1: +scanner_destroy (scanner); +erec_print_list (stderr, &msgs); +return rc; +}
src/libnftables.c int nft_run_command_from_filename(struct nft_ctx *nft, const char *filename) · line 224
Quoted 10 lines
src/libnftables.c +10
-0+ +int nft_batch_commit (struct nft_ctx *nft,struct nft_batch *batch) +{ +int ret =0 ; + +mnl_batch_end (batch->batch,mnl_seqnum_alloc (&nft->cache.seqnum)); +LIST_HEAD (err_list); + +if (!mnl_batch_ready (batch->batch)) + goto out;
That's an error, no? (In the old code it was a regular exit point in case batch support wasn't supported.)
Cheers, Phil
PATCH 13/16 libnftables: set max_errors to 1 in library
In reply to Eric LeblondAs memory handling is defficient if we don't do so, we can't really use a non 1 value for the parameter in the library due to memory leak.
Also this is not a real issue as programmatically a user of the library should only encounter one error at a time.
This patch also introduces a function that can be used to modify the max_errors parameter. It is used in main to keep the existing behavior.
--- include/nftables/nftables.h | 1 + src/libnftables.c | 8 +++++++- src/main.c | 1 + 3 files changed, 9 insertions(+), 1 deletion(-)
include/nftables/nftables.h +1 -0
@@ -22,6 +22,7 @@ enum nftables_exit_codes { };void nft_global_init (void ); +int nft_global_set_max_errors (unsigned int errors);void nft_global_deinit (void );struct nft_ctx *nft_context_new (void );
src/libnftables.c +7 -1
@@ -25,7 +25,7 @@#include <fcntl.h> -unsigned int max_errors =10 ; +unsigned int max_errors =1 ;unsigned int numeric_output;unsigned int ip2name_output;unsigned int handle_output;
@@ -58,6 +58,12 @@ void nft_global_deinit(void)mark_table_exit (); } +int nft_global_set_max_errors (unsigned int errors) +{ + max_errors = errors; +return 0 ; +} + __attribute__((format (printf,2 ,0 )))static int nft_print (void *ctx,const char *fmt, ...) {
src/main.c +1 -0
@@ -192,6 +192,7 @@ int main(int argc, char * const *argv)struct nft_ctx *nft;nft_global_init (); +nft_global_set_max_errors (10 ); nft =nft_context_new ();while (1 ) { val =getopt_long (argc, argv,OPTSTRING , options,NULL );
Signature · 1 line
2.14.1
PATCH 14/16 erec: add function to free list
In reply to Eric Leblond--- include/erec.h | 1 + src/erec.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
include/erec.h +1 -0
@@ -60,6 +60,7 @@ static inline void erec_queue(struct error_record *erec,extern void erec_print (FILE *f,const struct error_record *erec);extern void erec_print_list (FILE *f,struct list_head *list); +void erec_free_list (struct list_head *list);struct eval_ctx ;
src/erec.c +10 -0
@@ -213,6 +213,16 @@ void erec_print_list(FILE *f, struct list_head *list) } } +void erec_free_list (struct list_head *list) +{ +struct error_record *erec, *next; + +list_for_each_entry_safe (erec, next, list, list) { +list_del (&erec->list); +erec_destroy (erec); + } +} + int __fmtstring(4, 5) __stmt_binary_error(struct eval_ctx *ctx, const struct location *l1, const struct location *l2,
Signature · 1 line
2.14.1
PATCH 15/16 libnftables: add error handling
In reply to Eric LeblondIt had a nft_get_error to be able to fetch existing errors and get them into a buffer that can be later used by the application.
--- include/nftables.h | 1 + include/nftables/nftables.h | 1 + src/libnftables.c | 29 ++++++++++++++++++----------- 3 files changed, 20 insertions(+), 11 deletions(-)
include/nftables.h +1 -0
@@ -30,6 +30,7 @@ struct output_ctx {unsigned int ip2name;unsigned int handle;unsigned int echo; +struct list_head msgs;void *ctx;int (*print)(void *ctx,const char *format, ...); };
include/nftables/nftables.h +1 -0
@@ -30,6 +30,7 @@ void nft_context_free(struct nft_ctx *nft);void nft_context_set_print_func (struct nft_ctx *nft,int (*print)(void *ctx,const char *fmt, ...),void *ctx); +int nft_get_error (struct nft_ctx *nft,char *err_buf,size_t err_buf_len);int nft_run_command_from_buffer (struct nft_ctx *nft,char *buf,size_t buflen);
src/libnftables.c +18 -11
@@ -86,6 +86,7 @@ struct nft_ctx *nft_context_new(void) ctx->nf_sock =netlink_open_sock ();init_list_head (&ctx->cache.list); +init_list_head (&ctx->output.msgs); ctx->output.ctx = ctx; ctx->output.print = nft_print;
@@ -108,6 +109,7 @@ void nft_context_free(struct nft_ctx *nft)return ;netlink_close_sock (nft->nf_sock);cache_release (&nft->cache); +erec_free_list (&nft->output.msgs);xfree (nft); }
@@ -116,23 +118,30 @@ static const struct input_descriptor indesc_cmdline = { .name = "<cmdline>", }; +int nft_get_error (struct nft_ctx *nft,char *err_buf,size_t err_buf_len) +{ +FILE *errfile =fmemopen (err_buf, err_buf_len,"w" ); +erec_print_list (errfile, &nft->output.msgs); +fclose (errfile); +return 0 ; +} +int nft_run_command_from_buffer (struct nft_ctx *nft,char *buf,size_t buflen) {int rc =NFT_EXIT_SUCCESS ;struct parser_state state; -LIST_HEAD (msgs);void *scanner; -parser_init (nft->nf_sock, &nft->cache, &state, &msgs); +parser_init (nft->nf_sock, &nft->cache, &state, &nft->output.msgs); scanner =scanner_init (&state);scanner_push_buffer (scanner, &indesc_cmdline, buf); -if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, &state, &msgs) !=0 ) +if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, + &state, &nft->output.msgs) !=0 ) rc =NFT_EXIT_FAILURE ;scanner_destroy (scanner); -erec_print_list (stderr, &msgs);return rc; }
@@ -146,15 +155,15 @@ int nft_run_command_from_filename(struct nft_ctx *nft, const char *filename) rc =cache_update (nft->nf_sock, &nft->cache,CMD_INVALID , &msgs);if (rc <0 )return rc; -parser_init (nft->nf_sock, &nft->cache, &state, &msgs); +parser_init (nft->nf_sock, &nft->cache, &state, &nft->output.msgs); scanner =scanner_init (&state);if (scanner_read_file (scanner, filename, &internal_location) <0 )return NFT_EXIT_FAILURE ; -if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, &state, &msgs) !=0 ) +if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, + &state, &nft->output.msgs) !=0 ) rc =NFT_EXIT_FAILURE ;scanner_destroy (scanner); -erec_print_list (stderr, &msgs);return rc; }
@@ -176,14 +185,13 @@ int nft_batch_add(struct nft_ctx *nft, struct nft_batch *batch,int rc =NFT_EXIT_SUCCESS ;int ret =0 ;struct parser_state state; -LIST_HEAD (msgs);void *scanner;struct cmd *cmd, *next;struct netlink_ctx *ctx = &batch->nl_ctx;uint32_t seqnum;bool batch_supported =netlink_batch_supported (nft->nf_sock, &seqnum); -parser_init (nft->nf_sock, &nft->cache, &state, &msgs); +parser_init (nft->nf_sock, &nft->cache, &state, &nft->output.msgs); scanner =scanner_init (&state);scanner_push_buffer (scanner, &indesc_cmdline, buf);
@@ -196,7 +204,7 @@ int nft_batch_add(struct nft_ctx *nft, struct nft_batch *batch,list_for_each_entry (cmd, &state.cmds, list) {nft_cmd_expand (cmd);memset (ctx,0 ,sizeof (*ctx)); - ctx->msgs = &msgs; + ctx->msgs = &nft->output.msgs; ctx->seqnum = cmd->seqnum =mnl_seqnum_alloc (&seqnum); ctx->batch = batch->batch; ctx->batch_supported = batch_supported;
@@ -215,7 +223,6 @@ int nft_batch_add(struct nft_ctx *nft, struct nft_batch *batch, } err1:scanner_destroy (scanner); -erec_print_list (stderr, &msgs);return rc; }
Signature · 1 line
2.14.1
Phil Sutter
reviewerRe: [nft PATH 15/16] libnftables: add error handling
On Wed, Aug 16, 2017 at 10:43:09PM +0200, Eric Leblond wrote:
[...]
Quoted 32 lines
src/libnftables.c +10
-0@@ -86,6 +86,7 @@ struct nft_ctx *nft_context_new(void) ctx->nf_sock =netlink_open_sock ();init_list_head (&ctx->cache.list); +init_list_head (&ctx->output.msgs); ctx->output.ctx = ctx; ctx->output.print = nft_print;@@ -108,6 +109,7 @@ void nft_context_free(struct nft_ctx *nft)return ;netlink_close_sock (nft->nf_sock);cache_release (&nft->cache); +erec_free_list (&nft->output.msgs);xfree (nft); }@@ -116,23 +118,30 @@ static const struct input_descriptor indesc_cmdline = { .name = "<cmdline>", }; +int nft_get_error (struct nft_ctx *nft,char *err_buf,size_t err_buf_len) +{ +FILE *errfile =fmemopen (err_buf, err_buf_len,"w" ); +erec_print_list (errfile, &nft->output.msgs); +fclose (errfile); +return 0 ; +} +
src/libnftables.c static const struct input_descriptor indesc_cmdline = { · line 136
Quoted 10 lines
src/libnftables.c +1
-2int nft_run_command_from_buffer (struct nft_ctx *nft,char *buf,size_t buflen) {int rc =NFT_EXIT_SUCCESS ;struct parser_state state; -LIST_HEAD (msgs);void *scanner; -parser_init (nft->nf_sock, &nft->cache, &state, &msgs); +parser_init (nft->nf_sock, &nft->cache, &state, &nft->output.msgs);
Change signature to take struct nft_ctx as first parameter and get rid of everything but struct parser_state?
src/libnftables.c static const struct input_descriptor indesc_cmdline = { · line 141
Quoted 6 lines
src/libnftables.c +2
-1scanner =scanner_init (&state);scanner_push_buffer (scanner, &indesc_cmdline, buf); -if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, &state, &msgs) !=0 ) +if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, + &state, &nft->output.msgs) !=0 )
Just use nft->output.msg directly in nft_run() and drop the parameter?
Cheers, Phil
Re: [nft PATH 15/16] libnftables: add error handling
Hi,
Quoted 15 lines · Phil Sutter
On Wed, Aug 16, 2017 at 10:43:09PM +0200, Eric Leblond wrote:
[...]Quoted 13 lines
src/libnftables.c +0
-0@@ -86,6 +86,7 @@ struct nft_ctx *nft_context_new(void) ctx->nf_sock = netlink_open_sock();
init_list_head(&ctx->cache.list);src/libnftables.c +1
-0+init_list_head (&ctx->output.msgs);
ctx->output.ctx= ctx;
ctx->output.print=nft_print;src/libnftables.c +0
-0@@ -108,6 +109,7 @@ void nft_context_free(struct nft_ctx *nft)
..
Quoted 11 lines
Quoted 7 lines
struct
parser_statestate;src/libnftables.c +0
-1-LIST_HEAD (msgs);void *scanner;
src/libnftables.c +1
-1-parser_init (nft->nf_sock, &nft->cache, &state, &msgs); + parser_init(nft->nf_sock, &nft->cache, &state, &nft-Quoted 1 line
output.msgs);
Change signature to take struct
nft_ctxas first parameter and get rid
of everything but structparser_state?
Fully agree with proposal.
Quoted 11 lines
src/libnftables.c
Quoted 8 lines
scanner =
scanner_init(&state);scanner_push_buffer(scanner, &indesc_cmdline, buf);src/libnftables.c +0
-1- if (nft_run(nft, nft->nf_sock, &nft->cache, scanner,&state, &msgs) != 0)
src/libnftables.c +2
-0+if (nft_run (nft, nft->nf_sock, &nft->cache, scanner, + &state, &nft->output.msgs) != 0)Just use
nft->output.msgdirectly innft_run()and drop the parameter?
Yes, I think we can go and simplify a few functions prototype. I did not do it cause of lack of time but also because I wanted to have everybody agree on the data model before doing this changes.
++
Signature · 2 lines
Eric Leblond <[email protected]> Blog: https://home.regit.org/
PATCH 16/16 libnftables: basic doxygen documentation
In reply to Eric Leblond--- src/libnftables.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
src/libnftables.c +51 -0
@@ -35,6 +35,12 @@ unsigned int debug_level;const char *include_paths[INCLUDE_PATHS_MAX ] = {DEFAULT_INCLUDE_PATH }; +/** +* Init cache structure. +* +* This needs to be called once by process to do the initialization +* phase of some structures. +*/ void nft_global_init (void ) {mark_table_init ();
@@ -48,6 +54,11 @@ void nft_global_init(void)#endif } +/** +* Deinit global structures +* +* To be call one before exiting the nftables tasks +*/ void nft_global_deinit (void ) {iface_cache_release ();
@@ -58,6 +69,12 @@ void nft_global_deinit(void)mark_table_exit (); } +/** +* Set number of consecutive errors to handle +* +* This can be useful if you send complex command to nftables +* and want to debug it but it causes memory leak. +*/ int nft_global_set_max_errors (unsigned int errors) { max_errors = errors;
@@ -75,6 +92,10 @@ static int nft_print(void *ctx, const char *fmt, ...)return 0 ; } +/** +* Allocate a nftables context +* +*/ struct nft_ctx *nft_context_new (void ) {struct nft_ctx *ctx =NULL ;
@@ -93,6 +114,12 @@ struct nft_ctx *nft_context_new(void)return ctx; } +/** +* Set print function for your application +* +* Command such as `list ruleset` can trigger an output. This function +* allows you to define which function should be used. +*/ void nft_context_set_print_func (struct nft_ctx *nft,int (*print)(void *ctx,const char *fmt, ...),void *ctx)
@@ -103,6 +130,9 @@ void nft_context_set_print_func(struct nft_ctx *nft, } } +/** +* Free a nftables context +*/ void nft_context_free (struct nft_ctx *nft) {if (nft ==NULL )
@@ -118,6 +148,9 @@ static const struct input_descriptor indesc_cmdline = { .name = "<cmdline>", }; +/** +* Get current errors and write them in provided buffer +*/ int nft_get_error (struct nft_ctx *nft,char *err_buf,size_t err_buf_len) {FILE *errfile =fmemopen (err_buf, err_buf_len,"w" );
@@ -126,6 +159,9 @@ int nft_get_error(struct nft_ctx *nft, char *err_buf, size_t err_buf_len)return 0 ; } +/** +* Run nftables command contained in provided buffer +*/ int nft_run_command_from_buffer (struct nft_ctx *nft,char *buf,size_t buflen) {
@@ -145,6 +181,9 @@ int nft_run_command_from_buffer(struct nft_ctx *nft,return rc; } +/** +* Run all nftables commands contained in a file +*/ int nft_run_command_from_filename (struct nft_ctx *nft,const char *filename) {int rc =NFT_EXIT_SUCCESS ;
@@ -167,6 +206,9 @@ int nft_run_command_from_filename(struct nft_ctx *nft, const char *filename)return rc; } +/** +* Start a batch +*/ struct nft_batch *nft_batch_start (struct nft_ctx *nft) {struct nft_batch *batch =malloc (sizeof (*batch));
@@ -179,6 +221,9 @@ struct nft_batch *nft_batch_start(struct nft_ctx *nft)return batch; } +/** +* Add a command to a already created batch +*/ int nft_batch_add (struct nft_ctx *nft,struct nft_batch *batch,const char * buf,size_t buflen) {
@@ -226,6 +271,9 @@ err1:return rc; } +/** +* Commit a batch to the kernel +*/ int nft_batch_commit (struct nft_ctx *nft,struct nft_batch *batch) {int ret =0 ;
@@ -246,6 +294,9 @@ out: } +/** +* Free ressources allocated to a batch +*/ void nft_batch_free (struct nft_batch *batch) {if (batch ==NULL )
Signature · 1 line
2.14.1
Quoted 12 lines · Eric Leblond
Hello,
This patchset adds a basi high level libnftables to nftables code. It is currently supporting running a command from a buffer or from a file as well as batch support allowing to chain commands and commit them at once.
The API is mostly using existing structures such as
nft_ctxthat are updated to contain enough information. It also adds a structure dedicated to batch.
Great work Eric, thanks!
Some comments below.
Quoted 5 lines
A simple program running a command is the following:
nft_global_init(); nft = nft_context_new(); nft_context_set_print_func(nft, my_print, buf);
^^^
A minor thing: Did you evaluate merging these two? Setting the print function directly when allocating a new context.
Quoted 11 lines
rc = nft_run_command_from_buffer(nft, CMD, sizeof(CMD)); if (rc != NFT_EXIT_SUCCESS) { nft_get_error(nft, err_buf, sizeof(err_buf)); printf("%s\n", err_buf); return -1; } nft_context_free(nft); nft_global_deinit();Transaction support is similar with:
Quoted 11 lines
nft = nft_context_new();
batch = nft_batch_start(nft);
if (nft_batch_add(nft, batch, ADD1, strlen(ADD1)) !=0) {
printf("FAIL add 1\n");
goto out;
}
if (nft_batch_add(nft, batch, ADD2, strlen(ADD2)) !=0) {
printf("FAIL add 2\n");
goto out;
}
if (nft_batch_commit(nft, batch) != 0) {^^^
error handling here is like in the other case? i.e. running nft_get_error() ?
Quoted 8 lines
goto out; }out:
nft_batch_free(batch); nft_context_free(nft); nft_global_deinit();
Quoted 6 lines
The library provides a way to get standard output via
nft_context_set_print_funcand error handling is done vianft_get_errorthat get error message in a buffer.This is early stage code as it does not feature things like set handling but IMO it can already be used as a starting point to build more things.
Any special challenge with sets?
On a side note, I remember in NFWS 2017 we discussed the possibility of libnftables being a separate source project, i.e a standalone repository.
Now that I see your patches, what I see is that libnftables is mostly all the code, while nft itself is very little code.
Still, with my Debian hat, I think that different repositories is good to have.
One more comment: perhaps is good idea to release nftables v0.8 before introducing this code into the repository. We may end not releasing nftables in quite a long time.
Hi,
Quoted 22 lines · Arturo Borrero Gonzalez
Quoted 9 lines · Eric Leblond
Hello,
This patchset adds a basi high level libnftables to nftables code. It is currently supporting running a command from a buffer or from a file as well as batch support allowing to chain commands and commit
them at once.Quoted 5 lines
The API is mostly using existing structures such as
nft_ctxthat are
updated to contain enough information. It also adds a structure dedicated to batch.Great work Eric, thanks!
Some comments below.
Quoted 2 lines
A simple program running a command is the following:
Quoted 7 lines
Quoted 3 lines
nft_global_init();
nft =nft_context_new();nft_context_set_print_func(nft, my_print, buf);^^^
A minor thing: Did you evaluate merging these two? Setting the print function directly when allocating a new context.
Nope but could make sense.
Quoted 25 lines
Quoted 13 lines
rc =
nft_run_command_from_buffer(nft, CMD, sizeof(CMD)); if (rc !=NFT_EXIT_SUCCESS) {nft_get_error(nft, err_buf, sizeof(err_buf));printf("%s\n", err_buf);
return -1;
}
nft_context_free(nft);
nft_global_deinit();Transaction support is similar with:
Quoted 11 lines
nft =
nft_context_new();
batch =nft_batch_start(nft);
if (nft_batch_add(nft, batch, ADD1, strlen(ADD1))!=0) {printf("FAIL add 1\n");
goto out;
}
if (nft_batch_add(nft, batch, ADD2, strlen(ADD2))!=0) {printf("FAIL add 2\n");
goto out;
}
if (nft_batch_commit(nft, batch)!= 0) {
Quoted 3 lines
^^^
error handling here is like in the other case? i.e. runningnft_get_error()?
Yes exactly. It is supposed to do so but it seems I wrote the commit function wrong. I'm gonna send an updated patchset.
Quoted 14 lines
Quoted 14 lines
goto out;
}out:
nft_batch_free(batch);
nft_context_free(nft);
nft_global_deinit();The library provides a way to get standard output via
nft_context_set_print_func
and error handling is done vianft_get_errorthat get error message in a buffer.
Quoted 6 lines
Quoted 4 lines
This is early stage code as it does not feature things like set handling but IMO
it can already be used as a starting point to build more things.Any special challenge with sets?
This is in the list of things that are yet to be developed.
Quoted 7 lines
On a side note, I remember in NFWS 2017 we discussed the possibility of libnftables being a separate source project, i.e a standalone repository.
Now that I see your patches, what I see is that libnftables is mostly all the code, while nft itself is very little code.
Still, with my Debian hat, I think that different repositories is good to have.
I don't like the cascade idea with nftables -> libnftables -> libnftnl -> libmnl that this will induce. Also this means some potential breakage in versionning.
Quoted 4 lines
One more comment: perhaps is good idea to release nftables v0.8 before
introducing this code into the repository. We may end not releasing nftables in quite a long time.
Fully agree on that, this was a point I forgot to mention in my initial mail.
++
Signature · 2 lines
Eric Leblond <[email protected]> Blog: https://home.regit.org/
Florian Westphal
maintainerEric Leblond <[email protected]> wrote:
Thanks a lot for working on this Eric!
Quoted 27 lines · Arturo Borrero Gonzalez
Quoted 26 lines · Eric Leblond
Quoted 9 lines
Hello,
This patchset adds a basi high level libnftables to nftables code. It is currently supporting running a command from a buffer or from a file as well as batch support allowing to chain commands and commit
them at once.Quoted 5 lines
The API is mostly using existing structures such as
nft_ctxthat are
updated to contain enough information. It also adds a structure dedicated to batch.Great work Eric, thanks!
Some comments below.
Quoted 5 lines
A simple program running a command is the following:
nft_global_init();
nft =nft_context_new();nft_context_set_print_func(nft, my_print, buf);
Quoted 5 lines
Quoted 3 lines
^^^
A minor thing: Did you evaluate merging these two? Setting the print function directly when allocating a new context.Nope but could make sense.
I'd recommend to keep it like this, else we can run into problems when we need a new func.
If it stays this way we can simply add nft_context_set_foo_func() instead of breaking nft_context_new() abi or adding nft_context_new2() (ugh...).
Quoted 11 lines
Quoted 7 lines
On a side note, I remember in NFWS 2017 we discussed the possibility of libnftables being a separate source project, i.e a standalone repository.
Now that I see your patches, what I see is that libnftables is mostly all the code, while nft itself is very little code.
Still, with my Debian hat, I think that different repositories is good to have.I don't like the cascade idea with nftables -> libnftables -> libnftnl -> libmnl that this will induce. Also this means some potential breakage in versionning.
I would also like to keep it in same repo, else i fear we will quickly have copy&paste programming...
We can always split later if we think that nft and libnft have matured in a way that they are distinct after all.
Phil Sutter
reviewerQuoted 9 lines · Arturo Borrero Gonzalez
Quoted 8 lines · Eric Leblond
Hello,
This patchset adds a basi high level libnftables to nftables code. It is currently supporting running a command from a buffer or from a file as well as batch support allowing to chain commands and commit them at once.
Quoted 6 lines
Quoted 4 lines
The API is mostly using existing structures such as
nft_ctxthat are updated to contain enough information. It also adds a structure dedicated to batch.Great work Eric, thanks!
+1! Thanks for pushing this forward.
I noticed a few compiler warnings:
| libnftables.c: In function 'nft_batch_commit': | ../include/list.h:28:2: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement] | struct list_head name = LIST_HEAD_INIT(name) | ^ | libnftables.c:282:2: note: in expansion of macro 'LIST_HEAD' | LIST_HEAD(err_list); | ^~~~~~~~~
I don't quite understand why that happens, the code removed from nft_netlink() looks identical. Maybe some compiler flags missing? Or is it just me?
In addition, I see this one:
| parser_bison.y: In function 'nft_parse': | parser_bison.y:122:3: warning: implicit declaration of function 'nft_set_debug' [-Wimplicit-function-declaration] | nft_set_debug(1, scanner); | ^~~~~~~~~~~~~ | parser_bison.c:64:25: warning: implicit declaration of function 'nft_lex' [-Wimplicit-function-declaration] | #define yylex nft_lex | ^ | parser_bison.c:4400:16: note: in expansion of macro 'yylex' | yychar = yylex (&yylval, &yylloc, scanner); | ^~~~~
Not sure why that happens, though.
Cheers, Phil