src: add fprintf API functions
PATCH v2 1/1 src: add fprintf API functions
Now it's possible to print directly from libnftables to a file or other stream. In order to don't force any format, the caller must print any trailing '\n'.
The error reporting of fprintf (< 0) is respected.
Signed-off-by Arturo Borrero Gonzalez <[email protected]>
---
v2: include <stdio.h> in header files.
include/libnftables/chain.h | 2 include/libnftables/rule.h | 2 include/libnftables/ruleset.h | 3 + include/libnftables/set.h | 3 + include/libnftables/table.h | 2 src/chain.c | 13 +++ src/internal.h | 4 + src/libnftables.map | 6 + src/rule.c | 13 +++ src/ruleset.c | 203 +++++++++++++++++++++++++++++++++++++++++ src/set.c | 13 +++ src/set_elem.c | 13 +++ src/table.c | 13 +++ src/utils.c | 27 +++++ 14 files changed, 317 insertions(+)
include/libnftables/chain.h +2 -0
@@ -1,6 +1,7 @@#ifndef _CHAIN_H_#define _CHAIN_H_ +#include <stdio.h> #include <stdint.h> #include <stdbool.h>
@@ -60,6 +61,7 @@ enum nft_chain_parse_type {int nft_chain_parse (struct nft_chain *c,enum nft_chain_parse_type type,const char *data);int nft_chain_snprintf (char *buf,size_t size,struct nft_chain *t,uint32_t type,uint32_t flags); +int nft_chain_fprintf (FILE *fp,struct nft_chain *c,uint32_t type,uint32_t flags);struct nlmsghdr *nft_chain_nlmsg_build_hdr (char *buf,uint16_t cmd,uint16_t family,uint16_t type,uint32_t seq);int nft_chain_nlmsg_parse (const struct nlmsghdr *nlh,struct nft_chain *t);
include/libnftables/rule.h +2 -0
@@ -1,6 +1,7 @@#ifndef _RULE_H_#define _RULE_H_ +#include <stdio.h> #include <stdint.h> #include <stdbool.h>
@@ -58,6 +59,7 @@ enum nft_rule_parse_type {int nft_rule_parse (struct nft_rule *r,enum nft_rule_parse_type type,const char *data);int nft_rule_snprintf (char *buf,size_t size,struct nft_rule *t,uint32_t type,uint32_t flags); +int nft_rule_fprintf (FILE *fp,struct nft_rule *r,uint32_t type,uint32_t flags);struct nlmsghdr *nft_rule_nlmsg_build_hdr (char *buf,uint16_t cmd,uint16_t family,uint16_t type,uint32_t seq);int nft_rule_nlmsg_parse (const struct nlmsghdr *nlh,struct nft_rule *t);
include/libnftables/ruleset.h +3 -0
@@ -1,6 +1,8 @@ #ifndef _RULESET_H_ #define _RULESET_H_ +#include <stdio.h> + #ifdef __cplusplus extern "C" { #endif
@@ -37,6 +39,7 @@ enum nft_ruleset_parse_type {int nft_ruleset_parse (struct nft_ruleset *rs,enum nft_ruleset_parse_type type,const char *data);int nft_ruleset_snprintf (char *buf,size_t size,const struct nft_ruleset *rs,uint32_t type,uint32_t flags); +int nft_ruleset_fprintf (FILE *fp,const struct nft_ruleset *rs,uint32_t type,uint32_t flags); #ifdef __cplusplus }/* extern "C" */
include/libnftables/set.h +3 -0
@@ -1,6 +1,7 @@#ifndef _NFT_SET_H_#define _NFT_SET_H_ +#include <stdio.h> #include <stdint.h> #include <stdbool.h>
@@ -38,6 +39,7 @@ int nft_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_set *s);int nft_set_elems_nlmsg_parse (const struct nlmsghdr *nlh,struct nft_set *s);int nft_set_snprintf (char *buf,size_t size,struct nft_set *s,uint32_t type,uint32_t flags); +int nft_set_fprintf (FILE *fp,struct nft_set *s,uint32_t type,uint32_t flags);struct nft_set_list ;
@@ -108,6 +110,7 @@ int nft_set_elem_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_set_elem *s)int nft_set_elem_parse (struct nft_set_elem *e,enum nft_set_parse_type type,const char *data);int nft_set_elem_snprintf (char *buf,size_t size,struct nft_set_elem *s,uint32_t type,uint32_t flags); +int nft_set_elem_fprintf (FILE *fp,struct nft_set_elem *se,uint32_t type,uint32_t flags);int nft_set_elem_foreach (struct nft_set *s,int (*cb)(struct nft_set_elem *e,void *data),void *data);
include/libnftables/table.h +2 -0
@@ -1,6 +1,7 @@#ifndef _TABLE_H_#define _TABLE_H_ +#include <stdio.h> #include <stdint.h> #include <stdbool.h>
@@ -48,6 +49,7 @@ enum nft_table_parse_type {int nft_table_parse (struct nft_table *t,enum nft_table_parse_type type,const char *data);int nft_table_snprintf (char *buf,size_t size,struct nft_table *t,uint32_t type,uint32_t flags); +int nft_table_fprintf (FILE *fp,struct nft_table *t,uint32_t type,uint32_t flags);struct nlmsghdr *nft_table_nlmsg_build_hdr (char *buf,uint16_t cmd,uint16_t family,uint16_t type,uint32_t seq);int nft_table_nlmsg_parse (const struct nlmsghdr *nlh,struct nft_table *t);
src/chain.c +13 -0
@@ -851,6 +851,19 @@ int nft_chain_snprintf(char *buf, size_t size, struct nft_chain *c, }EXPORT_SYMBOL (nft_chain_snprintf); +static inline int nft_chain_do_snprintf (char *buf,size_t size,void *c, +uint32_t type,uint32_t flags) +{ +return nft_chain_snprintf (buf, size, c, type, flags); +} + +int nft_chain_fprintf (FILE *fp,struct nft_chain *c,uint32_t type, +uint32_t flags) +{ +return nft_fprintf (fp, c, type, flags, nft_chain_do_snprintf); +} +EXPORT_SYMBOL (nft_chain_fprintf); +struct nft_chain_list {struct list_head list; };
src/internal.h +4 -0
@@ -17,6 +17,8 @@#define BASE_DEC 10#define BASE_HEX 16 +#define NFT_SNPRINTF_BUFSIZ 4096 +enum nft_type {NFT_TYPE_U8 ,NFT_TYPE_U16 ,
@@ -88,6 +90,8 @@ const char *nft_verdict2str(uint32_t verdict);int nft_str2verdict (const char *verdict);int nft_get_value (enum nft_type type,void *val,void *out); +int nft_fprintf (FILE *fp,void *obj,uint32_t type,uint32_t flags,int (*snprintf_cb)(char *buf,size_t bufsiz,void *obj,uint32_t type,uint32_t flags)); +void xfree (const void *ptr);struct expr_ops ;
src/libnftables.map +6 -0
@@ -12,6 +12,7 @@ global: nft_table_attr_get_str; nft_table_parse; nft_table_snprintf; + nft_table_fprintf; nft_table_nlmsg_build_hdr; nft_table_nlmsg_build_payload; nft_table_nlmsg_parse;
@@ -42,6 +43,7 @@ global: nft_chain_attr_get_str; nft_chain_parse; nft_chain_snprintf; + nft_chain_fprintf; nft_chain_nlmsg_build_hdr; nft_chain_nlmsg_build_payload; nft_chain_nlmsg_parse;
@@ -71,6 +73,7 @@ global: nft_rule_attr_get_str; nft_rule_parse; nft_rule_snprintf; + nft_rule_fprintf; nft_rule_nlmsg_build_hdr; nft_rule_nlmsg_build_payload; nft_rule_nlmsg_parse;
@@ -126,6 +129,7 @@ global: nft_set_nlmsg_parse; nft_set_parse; nft_set_snprintf; + nft_set_fprintf; nft_set_list_alloc; nft_set_list_free;
@@ -157,6 +161,7 @@ global: nft_set_elem_nlmsg_parse; nft_set_elem_parse; nft_set_elem_snprintf; + nft_set_elem_fprinf; nft_set_elems_nlmsg_build_payload; nft_set_elems_nlmsg_parse;
@@ -176,6 +181,7 @@ global: nft_ruleset_attr_get; nft_ruleset_parse; nft_ruleset_snprintf; + nft_ruleset_fprintf; local: *; };
src/rule.c +13 -0
@@ -819,6 +819,19 @@ int nft_rule_snprintf(char *buf, size_t size, struct nft_rule *r, }EXPORT_SYMBOL (nft_rule_snprintf); +static inline int nft_rule_do_snprintf (char *buf,size_t size,void *r, +uint32_t type,uint32_t flags) +{ +return nft_rule_snprintf (buf, size, r, type, flags); +} + +int nft_rule_fprintf (FILE *fp,struct nft_rule *r,uint32_t type, +uint32_t flags) +{ +return nft_fprintf (fp, r, type, flags, nft_rule_do_snprintf); +} +EXPORT_SYMBOL (nft_rule_fprintf); +int nft_rule_expr_foreach (struct nft_rule *r,int (*cb)(struct nft_rule_expr *e,void *data),void *data)
src/ruleset.c +203 -0
206 lines not on this page · raw message
src/set.c +13 -0
@@ -668,6 +668,19 @@ int nft_set_snprintf(char *buf, size_t size, struct nft_set *s, }EXPORT_SYMBOL (nft_set_snprintf); +static inline int nft_set_do_snprintf (char *buf,size_t size,void *s, +uint32_t type,uint32_t flags) +{ +return nft_set_snprintf (buf, size, s, type, flags); +} + +int nft_set_fprintf (FILE *fp,struct nft_set *s,uint32_t type, +uint32_t flags) +{ +return nft_fprintf (fp, s, type, flags, nft_set_do_snprintf); +} +EXPORT_SYMBOL (nft_set_fprintf); +void nft_set_elem_add (struct nft_set *s,struct nft_set_elem *elem) {list_add_tail (&elem->head, &s->element_list);
src/set_elem.c +13 -0
@@ -577,6 +577,19 @@ int nft_set_elem_snprintf(char *buf, size_t size, struct nft_set_elem *e, }EXPORT_SYMBOL (nft_set_elem_snprintf); +static inline int nft_set_elem_do_snprintf (char *buf,size_t size,void *e, +uint32_t type,uint32_t flags) +{ +return nft_set_elem_snprintf (buf, size, e, type, flags); +} + +int nft_set_elem_fprintf (FILE *fp,struct nft_set_elem *se,uint32_t type, +uint32_t flags) +{ +return nft_fprintf (fp, se, type, flags, nft_set_elem_do_snprintf); +} +EXPORT_SYMBOL (nft_set_elem_fprintf); +int nft_set_elem_foreach (struct nft_set *s,int (*cb)(struct nft_set_elem *e,void *data),void *data)
src/table.c +13 -0
@@ -388,6 +388,19 @@ int nft_table_snprintf(char *buf, size_t size, struct nft_table *t, }EXPORT_SYMBOL (nft_table_snprintf); +static inline int nft_table_do_snprintf (char *buf,size_t size,void *t, +uint32_t type,uint32_t flags) +{ +return nft_table_snprintf (buf, size, t, type, flags); +} + +int nft_table_fprintf (FILE *fp,struct nft_table *t,uint32_t type, +uint32_t flags) +{ +return nft_fprintf (fp, t, type, flags, nft_table_do_snprintf); +} +EXPORT_SYMBOL (nft_table_fprintf); +struct nft_table_list {struct list_head list; };
src/utils.c +27 -0
@@ -174,3 +174,30 @@ void xfree(const void *ptr) {free ((void *)ptr); } + +int nft_fprintf (FILE *fp,void *obj,uint32_t type,uint32_t flags, +int (*snprintf_cb)(char *buf,size_t bufsiz,void *obj, +uint32_t type,uint32_t flags)) +{ +char _buf[NFT_SNPRINTF_BUFSIZ ]; +char *buf = _buf; +size_t bufsiz =sizeof (_buf); +int ret; + + ret =snprintf_cb (buf, bufsiz, obj, type, flags); +if (ret >NFT_SNPRINTF_BUFSIZ ) { + buf =calloc (1 , ret); +if (buf ==NULL ) +return -1 ; + + bufsiz = ret; + ret =snprintf_cb (buf, bufsiz, obj, type, flags); + } + + ret =fprintf (fp,"%s" , buf); + +if (buf != _buf) +xfree (buf); + +return ret; +}
Quoted 4 lines · Arturo Borrero Gonzalez
Now it's possible to print directly from libnftables to a file or other stream. In order to don't force any format, the caller must print any trailing '\n'.
The error reporting of fprintf (< 0) is respected.
Applied, thanks.