From 059b9bf6fb31b971f79f83a01d9794288ab88a6e Mon Sep 17 00:00:00 2001 From: Elise Lennion Date: Thu, 2 Mar 2017 15:34:13 -0300 Subject: src: Use nftnl_buf to export XML/JSON rules This completes the use of nftnl_buf and its auxiliary functions to export XML/JSON rules. Highly based on work from Shivani Bhardwaj . Signed-off-by: Elise Lennion Signed-off-by: Pablo Neira Ayuso --- src/buffer.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/buffer.c') diff --git a/src/buffer.c b/src/buffer.c index d97d517..f9d5a83 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -171,3 +171,49 @@ int nftnl_buf_reg(struct nftnl_buf *b, int type, union nftnl_data_reg *reg, } return 0; } + +int nftnl_buf_expr_open(struct nftnl_buf *b, int type) +{ + switch (type) { + case NFTNL_OUTPUT_XML: + return 0; + case NFTNL_OUTPUT_JSON: + return nftnl_buf_put(b, "\"expr\":["); + } + return 0; +} + +int nftnl_buf_expr_close(struct nftnl_buf *b, int type) +{ + switch (type) { + case NFTNL_OUTPUT_XML: + return 0; + case NFTNL_OUTPUT_JSON: + nftnl_buf_done(b); + return nftnl_buf_put(b, "]"); + } + return 0; +} + +int nftnl_buf_expr(struct nftnl_buf *b, int type, uint32_t flags, + struct nftnl_expr *expr) +{ + int ret; + + switch (type) { + case NFTNL_OUTPUT_XML: + return 0; + case NFTNL_OUTPUT_JSON: + nftnl_buf_put(b, "{"); + nftnl_buf_str(b, type, expr->ops->name, TYPE); + ret = expr->ops->snprintf(b->buf + b->off, b->len, type, flags, + expr); + if (ret > 0) + nftnl_buf_update(b, ret); + else + nftnl_buf_done(b); + + return nftnl_buf_put(b, "},"); + } + return 0; +} -- cgit v1.2.3