[{"id":1762338,"web_url":"http://patchwork.ozlabs.org/comment/1762338/","msgid":"<20170903223441.GB12383@salvia>","list_archive_url":null,"date":"2017-09-03T22:34:41","subject":"Re: [nft PATCH 2/2] src: get rid of printf","submitter":{"id":1315,"url":"http://patchwork.ozlabs.org/api/people/1315/","name":"Pablo Neira Ayuso","email":"pablo@netfilter.org"},"content":"On Mon, Sep 04, 2017 at 12:03:56AM +0200, Eric Leblond wrote:\n> This patch introduces the nft_print_to_output_ctx function that has\n> to be used instead of printf to output information that where\n> previously send to stdout. This function accumulate the output in\n> a buffer that can be fetched by the user with the nft_ctx_get_output()\n> function.\n> \n> This modification will allow the libnftables library to provide an\n> easy way to the users to get the output data and display them like\n> they want.\n> \n> Signed-off-by: Eric Leblond <eric@regit.org>\n> ---\n>  include/datatype.h   |   5 +-\n>  include/expression.h |   2 +-\n>  include/nftables.h   |   5 ++\n>  src/cli.c            |   1 +\n>  src/ct.c             |  21 ++---\n>  src/datatype.c       |  66 ++++++++-------\n>  src/expression.c     |  79 ++++++++++--------\n>  src/exthdr.c         |  16 ++--\n>  src/fib.c            |  23 +++---\n>  src/hash.c           |  10 +--\n>  src/main.c           |  45 ++++++++++\n>  src/meta.c           |  32 +++++---\n>  src/numgen.c         |   8 +-\n>  src/payload.c        |   9 +-\n>  src/rule.c           | 228 +++++++++++++++++++++++++++++----------------------\n>  src/statement.c      | 145 +++++++++++++++++---------------\n>  16 files changed, 408 insertions(+), 287 deletions(-)\n> \n> diff --git a/include/datatype.h b/include/datatype.h\n> index 2e34591..e9f6079 100644\n> --- a/include/datatype.h\n> +++ b/include/datatype.h\n> @@ -209,7 +209,8 @@ extern void symbolic_constant_print(const struct symbol_table *tbl,\n>  \t\t\t\t    struct output_ctx *octx);\n>  extern void symbol_table_print(const struct symbol_table *tbl,\n>  \t\t\t       const struct datatype *dtype,\n> -\t\t\t       enum byteorder byteorder);\n> +\t\t\t       enum byteorder byteorder,\n> +\t\t\t       struct output_ctx *octx);\n>  \n>  extern struct symbol_table *rt_symbol_table_init(const char *filename);\n>  extern void rt_symbol_table_free(struct symbol_table *tbl);\n> @@ -261,7 +262,7 @@ extern const struct datatype *\n>  set_datatype_alloc(const struct datatype *orig_dtype, unsigned int byteorder);\n>  extern void set_datatype_destroy(const struct datatype *dtype);\n>  \n> -extern void time_print(uint64_t seconds);\n> +extern void time_print(uint64_t seconds, struct output_ctx *octx);\n>  extern struct error_record *time_parse(const struct location *loc,\n>  \t\t\t\t       const char *c, uint64_t *res);\n>  \n> diff --git a/include/expression.h b/include/expression.h\n> index 32d4423..ce6b702 100644\n> --- a/include/expression.h\n> +++ b/include/expression.h\n> @@ -334,7 +334,7 @@ extern struct expr *expr_get(struct expr *expr);\n>  extern void expr_free(struct expr *expr);\n>  extern void expr_print(const struct expr *expr, struct output_ctx *octx);\n>  extern bool expr_cmp(const struct expr *e1, const struct expr *e2);\n> -extern void expr_describe(const struct expr *expr);\n> +extern void expr_describe(const struct expr *expr, struct output_ctx *octx);\n>  \n>  extern const struct datatype *expr_basetype(const struct expr *expr);\n>  extern void expr_set_type(struct expr *expr, const struct datatype *dtype,\n> diff --git a/include/nftables.h b/include/nftables.h\n> index 7c4e93f..f4d5ce1 100644\n> --- a/include/nftables.h\n> +++ b/include/nftables.h\n> @@ -30,6 +30,8 @@ struct output_ctx {\n>  \tunsigned int ip2name;\n>  \tunsigned int handle;\n>  \tunsigned int echo;\n> +\tchar *output_buf;\n> +\tsize_t output_buf_len;\n>  };\n>  \n>  struct nft_cache {\n> @@ -149,4 +151,7 @@ void realm_table_meta_exit(void);\n>  void devgroup_table_exit(void);\n>  void realm_table_rt_exit(void);\n>  \n> +int nft_print_to_output_ctx(struct output_ctx *octx, const char *fmt, ...);\n> +char *nft_ctx_get_output(struct nft_ctx *ctx);\n> +\n>  #endif /* NFTABLES_NFTABLES_H */\n> diff --git a/src/cli.c b/src/cli.c\n> index d923ff7..ca4418c 100644\n> --- a/src/cli.c\n> +++ b/src/cli.c\n> @@ -138,6 +138,7 @@ static void cli_complete(char *line)\n>  \t\t    cli_nft->debug_mask);\n>  \tscanner_push_buffer(scanner, &indesc_cli, line);\n>  \tnft_run(cli_nft, cli_nf_sock, scanner, state, &msgs);\n> +\tprintf(\"%s\", nft_ctx_get_output(cli_nft));\n>  \terec_print_list(stdout, &msgs, cli_nft->debug_mask);\n>  \txfree(line);\n>  \tcache_release(&cli_nft->cache);\n> diff --git a/src/ct.c b/src/ct.c\n> index d64f467..f19608a 100644\n> --- a/src/ct.c\n> +++ b/src/ct.c\n> @@ -141,11 +141,12 @@ static void ct_label_type_print(const struct expr *expr,\n>  \tfor (s = ct_label_tbl->symbols; s->identifier != NULL; s++) {\n>  \t\tif (bit != s->value)\n>  \t\t\tcontinue;\n> -\t\tprintf(\"\\\"%s\\\"\", s->identifier);\n> +\t\tnft_print_to_output_ctx(octx, \"\\\"%s\\\"\", s->identifier);\n>  \t\treturn;\n>  \t}\n>  \t/* can happen when connlabel.conf is altered after rules were added */\n> -\tprintf(\"%ld\\n\", (long)mpz_scan1(expr->value, 0));\n> +\tnft_print_to_output_ctx(octx, \"%ld\\n\",\n\nLet's call this nft_print(), ok?\n\nI can make the rename here before applying.\n\nI have a bad record of making functions unnecessarily long, so I'm\ntrying to fix it this time. Anyway, this will be internal, so no\nproblem.\n--\nTo unsubscribe from this list: send the line \"unsubscribe netfilter-devel\" in\nthe body of a message to majordomo@vger.kernel.org\nMore majordomo info at  http://vger.kernel.org/majordomo-info.html","headers":{"Return-Path":"<netfilter-devel-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netfilter-devel-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xlnqR4NKwz9s06\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon,  4 Sep 2017 08:35:15 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1753084AbdICWfO (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tSun, 3 Sep 2017 18:35:14 -0400","from ganesha.gnumonks.org ([213.95.27.120]:40750 \"EHLO\n\tganesha.gnumonks.org\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S1752964AbdICWfO (ORCPT\n\t<rfc822;netfilter-devel@vger.kernel.org>);\n\tSun, 3 Sep 2017 18:35:14 -0400","from [31.4.193.113] (helo=gnumonks.org)\n\tby ganesha.gnumonks.org with esmtpsa\n\t(TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2)\n\t(envelope-from <pablo@gnumonks.org>)\n\tid 1dodTa-0002Ck-Lr; Mon, 04 Sep 2017 00:34:52 +0200"],"Date":"Mon, 4 Sep 2017 00:34:41 +0200","From":"Pablo Neira Ayuso <pablo@netfilter.org>","To":"Eric Leblond <eric@regit.org>","Cc":"netfilter-devel@vger.kernel.org","Subject":"Re: [nft PATCH 2/2] src: get rid of printf","Message-ID":"<20170903223441.GB12383@salvia>","References":"<20170903220356.20178-1-eric@regit.org>\n\t<20170903220356.20178-3-eric@regit.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20170903220356.20178-3-eric@regit.org>","User-Agent":"Mutt/1.5.23 (2014-03-12)","X-Spam-Score":"-2.9 (--)","Sender":"netfilter-devel-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netfilter-devel.vger.kernel.org>","X-Mailing-List":"netfilter-devel@vger.kernel.org"}},{"id":1762485,"web_url":"http://patchwork.ozlabs.org/comment/1762485/","msgid":"<1504511125.15611.18.camel@regit.org>","list_archive_url":null,"date":"2017-09-04T07:45:25","subject":"Re: [nft PATCH 2/2] src: get rid of printf","submitter":{"id":8198,"url":"http://patchwork.ozlabs.org/api/people/8198/","name":"Eric Leblond","email":"eric@regit.org"},"content":"Hi,\n\nOn Mon, 2017-09-04 at 00:34 +0200, Pablo Neira Ayuso wrote:\n> On Mon, Sep 04, 2017 at 12:03:56AM +0200, Eric Leblond wrote:\n> > This patch introduces the nft_print_to_output_ctx function that has\n> > to be used instead of printf to output information that where\n> > previously send to stdout. This function accumulate the output in\n> > a buffer that can be fetched by the user with the\n> > nft_ctx_get_output()\n> > function.\n> > \n> > This modification will allow the libnftables library to provide an\n> > easy way to the users to get the output data and display them like\n> > they want.\n> > \n> > Signed-off-by: Eric Leblond <eric@regit.org>\n> > ---\n> >  include/datatype.h   |   5 +-\n> >  include/expression.h |   2 +-\n> >  include/nftables.h   |   5 ++\n> >  src/cli.c            |   1 +\n> >  src/ct.c             |  21 ++---\n> >  src/datatype.c       |  66 ++++++++-------\n> >  src/expression.c     |  79 ++++++++++--------\n> >  src/exthdr.c         |  16 ++--\n> >  src/fib.c            |  23 +++---\n> >  src/hash.c           |  10 +--\n> >  src/main.c           |  45 ++++++++++\n> >  src/meta.c           |  32 +++++---\n> >  src/numgen.c         |   8 +-\n> >  src/payload.c        |   9 +-\n> >  src/rule.c           | 228 +++++++++++++++++++++++++++++--------\n> > --------------\n> >  src/statement.c      | 145 +++++++++++++++++---------------\n> >  16 files changed, 408 insertions(+), 287 deletions(-)\n> > \n> > diff --git a/include/datatype.h b/include/datatype.h\n> > index 2e34591..e9f6079 100644\n> > --- a/include/datatype.h\n> > +++ b/include/datatype.h\n> > @@ -209,7 +209,8 @@ extern void symbolic_constant_print(const\n> > struct symbol_table *tbl,\n> >  \t\t\t\t    struct output_ctx *octx);\n> >  extern void symbol_table_print(const struct symbol_table *tbl,\n> >  \t\t\t       const struct datatype *dtype,\n> > -\t\t\t       enum byteorder byteorder);\n> > +\t\t\t       enum byteorder byteorder,\n> > +\t\t\t       struct output_ctx *octx);\n> >  \n> >  extern struct symbol_table *rt_symbol_table_init(const char\n> > *filename);\n> >  extern void rt_symbol_table_free(struct symbol_table *tbl);\n> > @@ -261,7 +262,7 @@ extern const struct datatype *\n> >  set_datatype_alloc(const struct datatype *orig_dtype, unsigned int\n> > byteorder);\n> >  extern void set_datatype_destroy(const struct datatype *dtype);\n> >  \n> > -extern void time_print(uint64_t seconds);\n> > +extern void time_print(uint64_t seconds, struct output_ctx *octx);\n> >  extern struct error_record *time_parse(const struct location *loc,\n> >  \t\t\t\t       const char *c, uint64_t\n> > *res);\n> >  \n> > diff --git a/include/expression.h b/include/expression.h\n> > index 32d4423..ce6b702 100644\n> > --- a/include/expression.h\n> > +++ b/include/expression.h\n> > @@ -334,7 +334,7 @@ extern struct expr *expr_get(struct expr\n> > *expr);\n> >  extern void expr_free(struct expr *expr);\n> >  extern void expr_print(const struct expr *expr, struct output_ctx\n> > *octx);\n> >  extern bool expr_cmp(const struct expr *e1, const struct expr\n> > *e2);\n> > -extern void expr_describe(const struct expr *expr);\n> > +extern void expr_describe(const struct expr *expr, struct\n> > output_ctx *octx);\n> >  \n> >  extern const struct datatype *expr_basetype(const struct expr\n> > *expr);\n> >  extern void expr_set_type(struct expr *expr, const struct datatype\n> > *dtype,\n> > diff --git a/include/nftables.h b/include/nftables.h\n> > index 7c4e93f..f4d5ce1 100644\n> > --- a/include/nftables.h\n> > +++ b/include/nftables.h\n> > @@ -30,6 +30,8 @@ struct output_ctx {\n> >  \tunsigned int ip2name;\n> >  \tunsigned int handle;\n> >  \tunsigned int echo;\n> > +\tchar *output_buf;\n> > +\tsize_t output_buf_len;\n> >  };\n> >  \n> >  struct nft_cache {\n> > @@ -149,4 +151,7 @@ void realm_table_meta_exit(void);\n> >  void devgroup_table_exit(void);\n> >  void realm_table_rt_exit(void);\n> >  \n> > +int nft_print_to_output_ctx(struct output_ctx *octx, const char\n> > *fmt, ...);\n> > +char *nft_ctx_get_output(struct nft_ctx *ctx);\n> > +\n> >  #endif /* NFTABLES_NFTABLES_H */\n> > diff --git a/src/cli.c b/src/cli.c\n> > index d923ff7..ca4418c 100644\n> > --- a/src/cli.c\n> > +++ b/src/cli.c\n> > @@ -138,6 +138,7 @@ static void cli_complete(char *line)\n> >  \t\t    cli_nft->debug_mask);\n> >  \tscanner_push_buffer(scanner, &indesc_cli, line);\n> >  \tnft_run(cli_nft, cli_nf_sock, scanner, state, &msgs);\n> > +\tprintf(\"%s\", nft_ctx_get_output(cli_nft));\n> >  \terec_print_list(stdout, &msgs, cli_nft->debug_mask);\n> >  \txfree(line);\n> >  \tcache_release(&cli_nft->cache);\n> > diff --git a/src/ct.c b/src/ct.c\n> > index d64f467..f19608a 100644\n> > --- a/src/ct.c\n> > +++ b/src/ct.c\n> > @@ -141,11 +141,12 @@ static void ct_label_type_print(const struct\n> > expr *expr,\n> >  \tfor (s = ct_label_tbl->symbols; s->identifier != NULL;\n> > s++) {\n> >  \t\tif (bit != s->value)\n> >  \t\t\tcontinue;\n> > -\t\tprintf(\"\\\"%s\\\"\", s->identifier);\n> > +\t\tnft_print_to_output_ctx(octx, \"\\\"%s\\\"\", s-\n> > >identifier);\n> >  \t\treturn;\n> >  \t}\n> >  \t/* can happen when connlabel.conf is altered after rules\n> > were added */\n> > -\tprintf(\"%ld\\n\", (long)mpz_scan1(expr->value, 0));\n> > +\tnft_print_to_output_ctx(octx, \"%ld\\n\",\n> \n> Let's call this nft_print(), ok?\n\nYes, that's really better.\n\n> \n> I can make the rename here before applying.\n\nNo need, I'm gonna resubmit the patchset.\n\nBR,","headers":{"Return-Path":"<netfilter-devel-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netfilter-devel-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xm22L1QVnz9s7c\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon,  4 Sep 2017 17:45:30 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1753266AbdIDHp3 (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tMon, 4 Sep 2017 03:45:29 -0400","from home.regit.org ([37.187.126.138]:45276 \"EHLO home.regit.org\"\n\trhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP\n\tid S1753259AbdIDHp2 (ORCPT <rfc822; netfilter-devel@vger.kernel.org>);\n\tMon, 4 Sep 2017 03:45:28 -0400","from [2a01:e35:2fb6:1160:f022:11:465e:d69b] (helo=ice-age2)\n\tby home.regit.org with esmtpsa\n\t(TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89)\n\t(envelope-from <eric@regit.org>)\n\tid 1dom4U-0004bb-2k; Mon, 04 Sep 2017 09:45:26 +0200"],"Message-ID":"<1504511125.15611.18.camel@regit.org>","Subject":"Re: [nft PATCH 2/2] src: get rid of printf","From":"Eric Leblond <eric@regit.org>","To":"Pablo Neira Ayuso <pablo@netfilter.org>","Cc":"netfilter-devel@vger.kernel.org","Date":"Mon, 04 Sep 2017 09:45:25 +0200","In-Reply-To":"<20170903223441.GB12383@salvia>","References":"<20170903220356.20178-1-eric@regit.org>\n\t<20170903220356.20178-3-eric@regit.org>\n\t<20170903223441.GB12383@salvia>","Organization":"INL","Content-Type":"text/plain; charset=\"ISO-8859-15\"","X-Mailer":"Evolution 3.22.6-1 ","Mime-Version":"1.0","Content-Transfer-Encoding":"8bit","X-Spam-Score":"-1.0 (-)","Sender":"netfilter-devel-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netfilter-devel.vger.kernel.org>","X-Mailing-List":"netfilter-devel@vger.kernel.org"}},{"id":1762493,"web_url":"http://patchwork.ozlabs.org/comment/1762493/","msgid":"<20170904075558.10129-1-eric@regit.org>","list_archive_url":null,"date":"2017-09-04T07:55:56","subject":"[nft PATCH v2] libnftables preparation work","submitter":{"id":8198,"url":"http://patchwork.ozlabs.org/api/people/8198/","name":"Eric Leblond","email":"eric@regit.org"},"content":"Hi,\n\nThis patchset update previous one following Pablo's suggestion:\n* remove unused custom netlink flag\n* rename ouput function to nft_print\n* change default output buffer size to 4k instead of 128 that was used\n  for debug (and causing a lot of realloc)\n\nBR,\n--\nEric\n--\nTo unsubscribe from this list: send the line \"unsubscribe netfilter-devel\" in\nthe body of a message to majordomo@vger.kernel.org\nMore majordomo info at  http://vger.kernel.org/majordomo-info.html","headers":{"Return-Path":"<netfilter-devel-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netfilter-devel-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xm2Gk4Xbxz9s06\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon,  4 Sep 2017 17:56:14 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1753292AbdIDH4N (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tMon, 4 Sep 2017 03:56:13 -0400","from home.regit.org ([37.187.126.138]:45412 \"EHLO home.regit.org\"\n\trhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP\n\tid S1751949AbdIDH4N (ORCPT <rfc822; netfilter-devel@vger.kernel.org>);\n\tMon, 4 Sep 2017 03:56:13 -0400","from [2a01:e35:2fb6:1160:f022:11:465e:d69b]\n\t(helo=ice-age2.regit.org) by home.regit.org with esmtpsa\n\t(TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89)\n\t(envelope-from <eric@regit.org>)\n\tid 1domEt-0004oZ-OH; Mon, 04 Sep 2017 09:56:11 +0200"],"From":"Eric Leblond <eric@regit.org>","To":"pablo@netfilter.org","Cc":"netfilter-devel@vger.kernel.org","Subject":"[nft PATCH v2] libnftables preparation work","Date":"Mon,  4 Sep 2017 09:55:56 +0200","Message-Id":"<20170904075558.10129-1-eric@regit.org>","X-Mailer":"git-send-email 2.14.1","In-Reply-To":"<20170903223441.GB12383@salvia>","References":"<20170903223441.GB12383@salvia>","X-Spam-Score":"-1.0 (-)","Sender":"netfilter-devel-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netfilter-devel.vger.kernel.org>","X-Mailing-List":"netfilter-devel@vger.kernel.org"}}]