From 612698d4cedb3fbc2a02480c05b9a9d8cb13d3a8 Mon Sep 17 00:00:00 2001 From: Arturo Borrero Date: Tue, 15 Apr 2014 20:12:58 +0200 Subject: src: add flag to add event wrapping in output functions This patch uses the flag option of each output function to print an event wrapper string in each object. In order to use this functionality, the caller must pass the corresponding flags: NFT_OF_EVENT_NEW / NFT_OF_EVENT_DEL. (I have slightly refactorized the original code to add the xml/json header and footer --pablo). Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- src/common.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'src/common.c') diff --git a/src/common.c b/src/common.c index 336d2b4..929f25d 100644 --- a/src/common.c +++ b/src/common.c @@ -66,3 +66,80 @@ int nft_parse_perror(const char *str, struct nft_parse_err *err) } } EXPORT_SYMBOL(nft_parse_perror); + +int nft_event_header_snprintf(char *buf, size_t size, uint32_t type, + uint32_t flags) +{ + int ret = 0; + + switch (type) { + case NFT_OUTPUT_XML: + if (flags & NFT_OF_EVENT_NEW) { + ret = snprintf(buf, size, "new"); + } else if (flags & NFT_OF_EVENT_DEL) { + ret = snprintf(buf, size, + "delete"); + } else { + ret = snprintf(buf, size, + "unknown"); + } + break; + case NFT_OUTPUT_JSON: + if (flags & NFT_OF_EVENT_NEW) { + ret = snprintf(buf, size, "{event:{type:\"new\",{\""); + } else if (flags & NFT_OF_EVENT_DEL) { + ret = snprintf(buf, size, + "{event:{type:\"delete\",{\""); + } else { + ret = snprintf(buf, size, + "{event:{type:\"unknown\",{\""); + } + break; + default: + if (flags & NFT_OF_EVENT_NEW) { + ret = snprintf(buf, size, "%9s", "[NEW] "); + } else if (flags & NFT_OF_EVENT_DEL) { + ret = snprintf(buf, size, "%9s", "[DELETE] "); + } else { + ret = snprintf(buf, size, "%9s", "[unknown] "); + } + break; + } + return ret; +} + +int nft_event_header_fprintf(FILE *fp, uint32_t type, uint32_t flags) +{ + char buf[64]; /* enough for the maximum string length above */ + + nft_event_header_snprintf(buf, sizeof(buf), type, flags); + buf[sizeof(buf) - 1] = '\0'; + + return fprintf(fp, "%s", buf); +} + +int nft_event_footer_snprintf(char *buf, size_t size, uint32_t type, + uint32_t flags) +{ + if (!(flags & NFT_OF_EVENT_ANY)) + return 0; + + switch (type) { + case NFT_OUTPUT_XML: + return snprintf(buf, size, ""); + case NFT_OUTPUT_JSON: + return snprintf(buf, size, "}}}"); + default: + return 0; + } +} + +int nft_event_footer_fprintf(FILE *fp, uint32_t type, uint32_t flags) +{ + char buf[32]; /* enough for the maximum string length above */ + + nft_event_footer_snprintf(buf, sizeof(buf), type, flags); + buf[sizeof(buf) - 1] = '\0'; + + return fprintf(fp, "%s", buf); +} -- cgit v1.2.3