Get output under application control
Phil Sutter
reviewerv2 cover Get output under application control
The following series introduces struct output_ctx field 'output_fp' and adjusts the whole code base to use that for regular (e.g. 'list' command) and debug output.
Patches 1-4 contain preparation changes which I extracted for easier review. Patch 5 is a modified version of what Eric Leblond sent earlier (hence why I tagged this whole series as v2). I sent him parts of it earlier, but didn't get a reply. So I'll just hijack his work and make it a joint one between him and me.
Patches 1-4 are new ones not present in Eric's v1. Patch 5 contains a changelog.
Note that with this series applied, the testsuite (including monitor tests) passes (apart from the unrelated ICMPv6 asymmetry).
Phil Sutter (5):
rule: Use C99-style initializer in cache_init()
exthdr: Simplify tcp option printing a bit
erec_print: Pass output FILE pointer to netlink_dump_expr()
rule: Refactor chain_print_declaration()
src: get rid of printf
include/datatype.h | 5 +- include/expression.h | 2 +- include/netlink.h | 10 +- include/nftables.h | 6 + include/parser.h | 2 +- include/rule.h | 7 +- src/cli.c | 2 +- src/ct.c | 20 ++-- src/datatype.c | 60 +++++----- src/erec.c | 2 +- src/evaluate.c | 22 ++-- src/expression.c | 74 ++++++------ src/exthdr.c | 21 ++-- src/fib.c | 23 ++-- src/hash.c | 10 +- src/main.c | 51 ++++++++- src/meta.c | 32 +++--- src/netlink.c | 131 ++++++++++----------- src/netlink_linearize.c | 2 +- src/numgen.c | 7 +- src/parser_bison.y | 3 +- src/payload.c | 10 +- src/rt.c | 2 +- src/rule.c | 297 +++++++++++++++++++++++++----------------------- src/statement.c | 138 +++++++++++----------- 25 files changed, 510 insertions(+), 429 deletions(-)
Signature · 1 line
2.13.1
Phil Sutter
reviewerPATCH v2 1/5 rule: Use C99-style initializer in cache_init()
--- src/rule.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
src/rule.c +8 -9
@@ -128,17 +128,16 @@ static int cache_init(struct mnl_socket *nf_sock, struct nft_cache *cache,struct handle handle = { .family =NFPROTO_UNSPEC , }; -struct netlink_ctx ctx; +struct netlink_ctx ctx = { + .list =LIST_HEAD_INIT (ctx.list), + .nf_sock = nf_sock, + .cache = cache, + .msgs = msgs, + .seqnum = cache->seqnum++, + .debug_mask = debug_mask, + };int ret; -memset (&ctx,0 ,sizeof (ctx)); -init_list_head (&ctx.list); - ctx.nf_sock = nf_sock; - ctx.cache = cache; - ctx.msgs = msgs; - ctx.seqnum = cache->seqnum++; - ctx.debug_mask = debug_mask; - ret =cache_init_tables (&ctx, &handle, cache);if (ret <0 )return ret;
Signature · 1 line
2.13.1
Re: [nft PATCH v2 1/5] rule: Use C99-style initializer in cache_init()
Applied, thanks Phil.
Phil Sutter
reviewerPATCH v2 2/5 exthdr: Simplify tcp option printing a bit
In reply to Phil SutterThis eliminates the need for that temporary buffer.
--- src/exthdr.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
src/exthdr.c +4 -8
@@ -31,17 +31,13 @@ static void exthdr_expr_print(const struct expr *expr, struct output_ctx *octx)* multiplicator */ unsigned int offset = expr->exthdr.offset /64 ; -char buf[9 ] = {0 }; -if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT ) { -printf ("tcp option %s" , expr->exthdr.desc->name); +printf ("tcp option %s" , expr->exthdr.desc->name); +if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT )return ; - } -if (offset) -snprintf (buf,sizeof buf,"%d" , offset); -printf ("tcp option %s%s %s" , expr->exthdr.desc->name, buf, - expr->exthdr.tmpl->token); +printf ("%d" , offset); +printf (" %s" , expr->exthdr.tmpl->token); }else {if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT )printf ("exthdr %s" , expr->exthdr.desc->name);
Signature · 1 line
2.13.1
Pablo Neira Ayuso
maintainerRe: [nft PATCH v2 2/5] exthdr: Simplify tcp option printing a bit
Quoted 1 line · Phil Sutter
This eliminates the need for that temporary buffer.
Also applied, thanks.
Phil Sutter
reviewerPATCH v2 3/5 erec_print: Pass output FILE pointer to netlink_dump_expr()
In reply to Phil SutterIt was a bit odd that erec_print() outputs to a given FILE pointer but then calls netlink_dump_expr() which just prints to stdout. Fix this by passing the given FILE pointer along so output is guaranteed to go to the same destination.
--- include/netlink.h | 2 +- src/erec.c | 2 +- src/netlink.c | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-)
include/netlink.h +1 -1
@@ -184,7 +184,7 @@ extern void netlink_dump_chain(const struct nftnl_chain *nlc,extern void netlink_dump_rule (const struct nftnl_rule *nlr,unsigned int debug_mask);extern void netlink_dump_expr (const struct nftnl_expr *nle, -unsigned int debug_mask); +FILE *fp,unsigned int debug_mask);extern void netlink_dump_set (const struct nftnl_set *nls,unsigned int debug_mask);extern void netlink_dump_obj (struct nftnl_obj *nlo,unsigned int debug_mask);
src/erec.c +1 -1
@@ -153,7 +153,7 @@ void erec_print(FILE *f, const struct error_record *erec,fprintf (f,"%s\n" , erec->msg);for (l =0 ; l < (int )erec->num_locations; l++) { loc = &erec->locations[l]; -netlink_dump_expr (loc->nle, debug_mask); +netlink_dump_expr (loc->nle, f, debug_mask); }fprintf (f,"\n" ); }else {
src/netlink.c +3 -2
@@ -515,7 +515,8 @@ void netlink_dump_rule(const struct nftnl_rule *nlr, unsigned int debug_mask)fprintf (stdout,"%s\n" , buf); } -void netlink_dump_expr (const struct nftnl_expr *nle,unsigned int debug_mask) +void netlink_dump_expr (const struct nftnl_expr *nle, +FILE *fp,unsigned int debug_mask) {char buf[4096 ];
@@ -523,7 +524,7 @@ void netlink_dump_expr(const struct nftnl_expr *nle, unsigned int debug_mask)return ;nftnl_expr_snprintf (buf,sizeof (buf), nle,0 ,0 ); -fprintf (stdout,"%s\n" , buf); +fprintf (fp,"%s\n" , buf); }static int list_rule_cb (struct nftnl_rule *nlr,void *arg)
Signature · 1 line
2.13.1
Re: [nft PATCH v2 3/5] erec_print: Pass output FILE pointer to netlink_dump_expr()
Quoted 4 lines · Phil Sutter
It was a bit odd that
erec_print()outputs to a given FILE pointer but then callsnetlink_dump_expr()which just prints to stdout. Fix this by passing the given FILE pointer along so output is guaranteed to go to the same destination.
Applied, thanks Phil.
Phil Sutter
reviewerPATCH v2 4/5 rule: Refactor chain_print_declaration()
In reply to Phil SutterInstead of having two nearly identical printf() calls for netdev and other chains, print the common parts separately and include the device bit only for netdev chains.
--- src/rule.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
src/rule.c +6 -12
@@ -666,18 +666,12 @@ static void chain_print_declaration(const struct chain *chain) {printf ("\tchain %s {\n" , chain->handle.chain);if (chain->flags &CHAIN_F_BASECHAIN ) { -if (chain->dev !=NULL ) { -printf ("\t\ttype %s hook %s device %s priority %d; policy %s;\n" , - chain->type, -hooknum2str (chain->handle.family, chain->hooknum), - chain->dev, chain->priority, -chain_policy2str (chain->policy)); - }else { -printf ("\t\ttype %s hook %s priority %d; policy %s;\n" , - chain->type, -hooknum2str (chain->handle.family, chain->hooknum), - chain->priority,chain_policy2str (chain->policy)); - } +printf ("\t\ttype %s hook %s" , chain->type, +hooknum2str (chain->handle.family, chain->hooknum)); +if (chain->dev !=NULL ) +printf (" device %s" , chain->dev); +printf (" priority %d; policy %s;\n" , + chain->priority,chain_policy2str (chain->policy)); } }
Signature · 1 line
2.13.1
Re: [nft PATCH v2 4/5] rule: Refactor chain_print_declaration()
Quoted 3 lines · Phil Sutter
Instead of having two nearly identical
printf()calls for netdev and other chains, print the common parts separately and include the device bit only for netdev chains.
Applied, thanks.
Phil Sutter
reviewerPATCH v2 5/5 src: get rid of printf
In reply to Phil SutterThis patch introduces nft_print()/nft_gmp_print() functions which have to be used instead of printf to output information that were previously send to stdout. These functions print to a FILE pointer defined in struct output_ctx. It is set by calling:
| old_fp = nft_ctx_set_output(ctx, new_fp);
Having an application-defined FILE pointer is actually quite flexible:
Using fmemopen() or even fopencookie(), an application gains full control over what is printed and where it should go to.
---
Changes since v1:
- Print into a FILE pointer instead of a buffer since that allows to include monitor code into the same solution.
- Change monitor code to use nft_print() as well.
- Introduce nft_gmp_print() to replace former calls to gmp_printf().
- Add struct output_ctx pointer to struct eval_ctx, this is required by cache_update().
- Make all netlink_dump_*() functions print to output_fp by making use of nftnl_*_fprintf() functions.
--- include/datatype.h | 5 +- include/expression.h | 2 +- include/netlink.h | 8 +- include/nftables.h | 6 ++ include/parser.h | 2 +- include/rule.h | 7 +- src/cli.c | 2 +- src/ct.c | 20 ++-- src/datatype.c | 60 +++++------ src/evaluate.c | 22 ++-- src/expression.c | 74 ++++++------- src/exthdr.c | 15 +-- src/fib.c | 23 ++-- src/hash.c | 10 +- src/main.c | 51 ++++++++- src/meta.c | 32 +++--- src/netlink.c | 126 +++++++++++----------- src/netlink_linearize.c | 2 +- src/numgen.c | 7 +- src/parser_bison.y | 3 +- src/payload.c | 10 +- src/rt.c | 2 +- src/rule.c | 272 +++++++++++++++++++++++++----------------------- src/statement.c | 138 ++++++++++++------------ 24 files changed, 495 insertions(+), 404 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/netlink.h +4 -4
@@ -180,14 +180,14 @@ extern int netlink_delete_obj(struct netlink_ctx *ctx, const struct handle *h, struct location *loc, uint32_t type);extern void netlink_dump_chain (const struct nftnl_chain *nlc, -unsigned int debug_mask); +struct netlink_ctx *ctx);extern void netlink_dump_rule (const struct nftnl_rule *nlr, -unsigned int debug_mask); +struct netlink_ctx *ctx);extern void netlink_dump_expr (const struct nftnl_expr *nle,FILE *fp,unsigned int debug_mask);extern void netlink_dump_set (const struct nftnl_set *nls, -unsigned int debug_mask); -extern void netlink_dump_obj (struct nftnl_obj *nlo,unsigned int debug_mask); +struct netlink_ctx *ctx); +extern void netlink_dump_obj (struct nftnl_obj *nlo,struct netlink_ctx *ctx);extern int netlink_batch_send (struct netlink_ctx *ctx,struct list_head *err_list);
include/nftables.h +6 -0
@@ -30,6 +30,7 @@ struct output_ctx {unsigned int ip2name;unsigned int handle;unsigned int echo; +FILE *output_fp; };struct nft_cache {
@@ -148,4 +149,9 @@ void realm_table_meta_exit(void);void devgroup_table_exit (void );void realm_table_rt_exit (void ); +int nft_print (struct output_ctx *octx,const char *fmt, ...) + __attribute__((format (printf,2 ,3 ))); +int nft_gmp_print (struct output_ctx *octx,const char *fmt, ...) + __attribute__((format (printf,2 ,0 ))); +#endif /* NFTABLES_NFTABLES_H */
include/parser.h +1 -1
@@ -33,7 +33,7 @@ struct mnl_socket;extern void parser_init (struct mnl_socket *nf_sock,struct nft_cache *cache,struct parser_state *state,struct list_head *msgs, -unsigned int debug_level); +unsigned int debug_level,struct output_ctx *octx);extern int nft_parse (struct nft_ctx *ctx,void *,struct parser_state *state);extern void *scanner_init (struct parser_state *state);
include/rule.h +5 -2
@@ -172,7 +172,8 @@ extern struct chain *chain_lookup(const struct table *table,extern const char *family2str (unsigned int family);extern const char *hooknum2str (unsigned int family,unsigned int hooknum); -extern void chain_print_plain (const struct chain *chain); +extern void chain_print_plain (const struct chain *chain, +struct output_ctx *octx);/** * struct rule - nftables rule
@@ -493,6 +494,7 @@ struct eval_ctx {struct set *set;struct stmt *stmt;struct nft_cache *cache; +struct output_ctx *octx;unsigned int debug_mask;struct expr_ctx ectx;struct proto_ctx pctx;
@@ -506,7 +508,8 @@ struct netlink_ctx;extern int do_command (struct netlink_ctx *ctx,struct cmd *cmd);extern int cache_update (struct mnl_socket *nf_sock,struct nft_cache *cache, -enum cmd_ops cmd,struct list_head *msgs,bool debug); +enum cmd_ops cmd,struct list_head *msgs,bool debug, +struct output_ctx *octx);extern void cache_flush (struct list_head *table_list);extern void cache_release (struct nft_cache *cache);
src/cli.c +1 -1
@@ -135,7 +135,7 @@ static void cli_complete(char *line) line = s;parser_init (cli_nf_sock, &cli_nft->cache, state, &msgs, - cli_nft->debug_mask); + cli_nft->debug_mask, &cli_nft->output);scanner_push_buffer (scanner, &indesc_cli, line);nft_run (cli_nft, cli_nf_sock, scanner, state, &msgs);erec_print_list (stdout, &msgs, cli_nft->debug_mask);
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); +nft_print (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 )); +nft_print (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 " ); +nft_print (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); +nft_print (octx,"%s " , s->identifier);break ; } } done: -printf ("%s" , ct_templates[key].token); +nft_print (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); +nft_print (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" ); +nft_print (octx,"notrack" ); }static const struct stmt_ops notrack_stmt_ops = {
src/datatype.c +31 -29
168 lines not on this page · raw message
src/evaluate.c +11 -11
@@ -193,7 +193,7 @@ static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr)break ;case SYMBOL_SET : ret =cache_update (ctx->nf_sock, ctx->cache, ctx->cmd->op, - ctx->msgs, ctx->debug_mask &DEBUG_NETLINK ); + ctx->msgs, ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -2986,14 +2986,14 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)switch (cmd->obj) {case CMD_OBJ_SETELEM : ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, - ctx->msgs, ctx->debug_mask &DEBUG_NETLINK ); + ctx->msgs, ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;return setelem_evaluate (ctx, &cmd->expr);case CMD_OBJ_SET : ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, - ctx->msgs, ctx->debug_mask &DEBUG_NETLINK ); + ctx->msgs, ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -3004,7 +3004,7 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)return rule_evaluate (ctx, cmd->rule);case CMD_OBJ_CHAIN : ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, - ctx->msgs, ctx->debug_mask &DEBUG_NETLINK ); + ctx->msgs, ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -3028,7 +3028,7 @@ static int cmd_evaluate_delete(struct eval_ctx *ctx, struct cmd *cmd)switch (cmd->obj) {case CMD_OBJ_SETELEM : ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, - ctx->msgs, ctx->debug_mask &DEBUG_NETLINK ); + ctx->msgs, ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -3072,7 +3072,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)int ret; ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs, - ctx->debug_mask &DEBUG_NETLINK ); + ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -3159,7 +3159,7 @@ static int cmd_evaluate_reset(struct eval_ctx *ctx, struct cmd *cmd)int ret; ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs, - ctx->debug_mask &DEBUG_NETLINK ); + ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -3186,7 +3186,7 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)int ret; ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs, - ctx->debug_mask &DEBUG_NETLINK ); + ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -3245,7 +3245,7 @@ static int cmd_evaluate_rename(struct eval_ctx *ctx, struct cmd *cmd)switch (cmd->obj) {case CMD_OBJ_CHAIN : ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, - ctx->msgs, ctx->debug_mask &DEBUG_NETLINK ); + ctx->msgs, ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -3343,7 +3343,7 @@ static int cmd_evaluate_monitor(struct eval_ctx *ctx, struct cmd *cmd)int ret; ret =cache_update (ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs, - ctx->debug_mask &DEBUG_NETLINK ); + ctx->debug_mask &DEBUG_NETLINK , ctx->octx);if (ret <0 )return ret;
@@ -3365,7 +3365,7 @@ static int cmd_evaluate_monitor(struct eval_ctx *ctx, struct cmd *cmd)static int cmd_evaluate_export (struct eval_ctx *ctx,struct cmd *cmd) {return cache_update (ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs, - ctx->debug_mask &DEBUG_NETLINK ); + ctx->debug_mask &DEBUG_NETLINK , ctx->octx); }static const char *cmd_op_name[] = {
src/expression.c +38 -36
187 lines not on this page · raw message
src/exthdr.c +8 -7
@@ -32,18 +32,19 @@ static void exthdr_expr_print(const struct expr *expr, struct output_ctx *octx)*/ unsigned int offset = expr->exthdr.offset /64 ; -printf ("tcp option %s" , expr->exthdr.desc->name); +nft_print (octx,"tcp option %s" , expr->exthdr.desc->name);if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT )return ;if (offset) -printf ("%d" , offset); -printf (" %s" , expr->exthdr.tmpl->token); +nft_print (octx,"%d" , offset); +nft_print (octx," %s" , expr->exthdr.tmpl->token); }else {if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT ) -printf ("exthdr %s" , expr->exthdr.desc->name); +nft_print (octx,"exthdr %s" , expr->exthdr.desc->name);else { -printf ("%s %s" , expr->exthdr.desc ? expr->exthdr.desc->name :"unknown-exthdr" , - expr->exthdr.tmpl->token); +nft_print (octx,"%s %s" , + expr->exthdr.desc ? expr->exthdr.desc->name :"unknown-exthdr" , + expr->exthdr.tmpl->token); } } }
@@ -98,7 +99,7 @@ struct expr *exthdr_expr_alloc(const struct location *loc,static void exthdr_stmt_print (const struct stmt *stmt,struct output_ctx *octx) {expr_print (stmt->exthdr.expr, octx); -printf (" set " ); +nft_print (octx," set " );expr_print (stmt->exthdr.val, octx); }
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); +nft_print (octx,"%s" , s); *flags &= ~f;if (*flags) -printf (" . " ); +nft_print (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" ); +nft_print (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); +nft_print (octx,"0x%x" , flags); -printf (" %s" ,fib_result_str (expr->fib.result)); +nft_print (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" ); +nft_print (octx,"symhash" );break ;case NFT_HASH_JENKINS :default : -printf ("jhash " ); +nft_print (octx,"jhash " );expr_print (expr->hash.expr, octx); } -printf (" mod %u" , expr->hash.mod); +nft_print (octx," mod %u" , expr->hash.mod);if (expr->hash.seed_set) -printf (" seed 0x%x" , expr->hash.seed); +nft_print (octx," seed 0x%x" , expr->hash.seed);if (expr->hash.offset) -printf (" offset %u" , expr->hash.offset); +nft_print (octx," offset %u" , expr->hash.offset); }static bool hash_expr_cmp (const struct expr *e1,const struct expr *e2)
src/main.c +48 -3
86 lines not on this page · raw message
src/meta.c +19 -13
@@ -54,13 +54,15 @@ static void tchandle_type_print(const struct expr *expr,switch (handle) {case TC_H_ROOT : -printf ("root" ); +nft_print (octx,"root" );break ;case TC_H_UNSPEC : -printf ("none" ); +nft_print (octx,"none" );break ;default : -printf ("%0x:%0x" ,TC_H_MAJ (handle) >>16 ,TC_H_MIN (handle)); +nft_print (octx,"%0x:%0x" , +TC_H_MAJ (handle) >>16 , +TC_H_MIN (handle));break ; } }
@@ -134,9 +136,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); +nft_print (octx,"\"%s\"" , name);else -printf ("%d" , ifindex); +nft_print (octx,"%d" , ifindex); } static struct error_record *ifindex_type_parse(const struct expr *sym,
@@ -209,9 +211,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); +nft_print (octx,"\"%s\"" , pw->pw_name);else -printf ("%d" , uid); +nft_print (octx,"%d" , uid);return ; }expr_basetype (expr)->
@@ -261,9 +263,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); +nft_print (octx,"\"%s\"" , gr->gr_name);else -printf ("%u" , gid); +nft_print (octx,"%u" , gid);return ; }expr_basetype (expr)->
@@ -446,9 +448,11 @@ 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); +nft_print (octx,"meta %s" , + meta_templates[expr->meta.key].token);else -printf ("%s" , meta_templates[expr->meta.key].token); +nft_print (octx,"%s" , + meta_templates[expr->meta.key].token); }static bool meta_expr_cmp (const struct expr *e1,const struct expr *e2)
@@ -573,9 +577,11 @@ 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); +nft_print (octx,"meta %s set " , + meta_templates[stmt->meta.key].token);else -printf ("%s set " , meta_templates[stmt->meta.key].token); +nft_print (octx,"%s set " , + meta_templates[stmt->meta.key].token);expr_print (stmt->meta.expr, octx); }
src/netlink.c +64 -62
363 lines not on this page · raw message
src/netlink_linearize.c +1 -1
@@ -1337,5 +1337,5 @@ void netlink_linearize_rule(struct netlink_ctx *ctx, struct nftnl_rule *nlr,nftnl_udata_buf_free (udata); } -netlink_dump_rule (nlr, ctx->debug_mask); +netlink_dump_rule (nlr, ctx); }
src/numgen.c +4 -3
@@ -30,10 +30,11 @@ 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), - expr->numgen.mod); +nft_print (octx,"numgen %s mod %u" , +numgen_type_str (expr->numgen.type), + expr->numgen.mod);if (expr->numgen.offset) -printf (" offset %u" , expr->numgen.offset); +nft_print (octx," offset %u" , expr->numgen.offset); }static bool numgen_expr_cmp (const struct expr *e1,const struct expr *e2)
src/parser_bison.y +2 -1
@@ -37,7 +37,7 @@ void parser_init(struct mnl_socket *nf_sock, struct nft_cache *cache, struct parser_state *state, struct list_head *msgs, - unsigned int debug_mask) + unsigned int debug_mask, struct output_ctx *octx) { memset(state, 0, sizeof(*state)); init_list_head(&state->cmds);
@@ -48,6 +48,7 @@ void parser_init(struct mnl_socket *nf_sock, struct nft_cache *cache, state->ectx.msgs = msgs; state->ectx.nf_sock = nf_sock; state->ectx.debug_mask = debug_mask; + state->ectx.octx = octx; } static void yyerror(struct location *loc, struct nft_ctx *nft, void *scanner,
src/payload.c +5 -5
@@ -46,11 +46,11 @@ 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); +nft_print (octx,"%s %s" , desc->name, tmpl->token);else -printf ("payload @%s,%u,%u" , - proto_base_tokens[expr->payload.base], - expr->payload.offset, expr->len); +nft_print (octx,"payload @%s,%u,%u" , + proto_base_tokens[expr->payload.base], + expr->payload.offset, expr->len); }static bool payload_expr_cmp (const struct expr *e1,const struct expr *e2)
@@ -187,7 +187,7 @@ unsigned int payload_hdr_field(const struct expr *expr)static void payload_stmt_print (const struct stmt *stmt,struct output_ctx *octx) {expr_print (stmt->payload.expr, octx); -printf (" set " ); +nft_print (octx," set " );expr_print (stmt->payload.val, octx); }
src/rt.c +1 -1
@@ -82,7 +82,7 @@ static const struct rt_template rt_templates[] = {static void rt_expr_print (const struct expr *expr,struct output_ctx *octx) { -printf ("rt %s" , rt_templates[expr->rt.key].token); +nft_print (octx,"rt %s" , rt_templates[expr->rt.key].token); }static bool rt_expr_cmp (const struct expr *e1,const struct expr *e2)
src/rule.c +144 -128
590 lines not on this page · raw message
src/statement.c +71 -67
346 lines not on this page · raw message
Signature · 1 line
2.13.1
Re: [nft PATCH v2 5/5] src: get rid of printf
Quoted 10 lines · Phil Sutter
This patch introduces nft_print()/nft_gmp_print() functions which have to be used instead of printf to output information that were previously send to stdout. These functions print to a FILE pointer defined in struct
output_ctx. It is set by calling:|
old_fp=nft_ctx_set_output(ctx, new_fp);Having an application-defined FILE pointer is actually quite flexible:
Usingfmemopen()or evenfopencookie(), an application gains full control over what is printed and where it should go to.
Applied, thanks a lot for taking over this work.
Phil Sutter
reviewerRe: [nft PATCH v2 5/5] src: get rid of printf
Hi Pablo,
Quoted 6 lines · Pablo Neira Ayuso
Quoted 5 lines · Phil Sutter
This patch introduces nft_print()/nft_gmp_print() functions which have to be used instead of printf to output information that were previously send to stdout. These functions print to a FILE pointer defined in struct
output_ctx. It is set by calling:
Quoted 7 lines
Quoted 5 lines
|
old_fp=nft_ctx_set_output(ctx, new_fp);Having an application-defined FILE pointer is actually quite flexible:
Usingfmemopen()or evenfopencookie(), an application gains full control over what is printed and where it should go to.Applied, thanks a lot for taking over this work.
Cool, thanks!
Note that I missed two spots when reviewing the patches:
* nft_gmp_print() lacks a call to fflush(). This is not a problem in practice, but inconsistent with regards to nft_print(). I'll send a follow-up fixing it.
* Did you notice how my patch changes main() to move stdout to another file descriptor? I used that to make sure I didn't miss a spot when converting the code. In theory, we could just drop it again since everything's working. What do you suggest?
Cheers, Phil
Pablo Neira Ayuso
maintainerRe: [nft PATCH v2 5/5] src: get rid of printf
Quoted 17 lines · Phil Sutter
Hi Pablo,
Quoted 6 lines · Pablo Neira Ayuso
Quoted 5 lines · Phil Sutter
This patch introduces nft_print()/nft_gmp_print() functions which have to be used instead of printf to output information that were previously send to stdout. These functions print to a FILE pointer defined in struct
output_ctx. It is set by calling:Quoted 7 lines
Quoted 5 lines
|
old_fp=nft_ctx_set_output(ctx, new_fp);Having an application-defined FILE pointer is actually quite flexible:
Usingfmemopen()or evenfopencookie(), an application gains full control over what is printed and where it should go to.Applied, thanks a lot for taking over this work.
Quoted 7 lines
Cool, thanks!
Note that I missed two spots when reviewing the patches:
*
nft_gmp_print()lacks a call tofflush(). This is not a problem in practice, but inconsistent with regards tonft_print(). I'll send a follow-up fixing it.
OK.
Quoted 4 lines
* Did you notice how my patch changes
main()to move stdout to another file descriptor? I used that to make sure I didn't miss a spot when converting the code. In theory, we could just drop it again since everything's working. What do you suggest?
Please, drop it. Thanks.