summaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
authorArturo Borrero <arturo.borrero.glez@gmail.com>2014-04-15 20:12:58 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2014-04-26 14:06:49 +0200
commit612698d4cedb3fbc2a02480c05b9a9d8cb13d3a8 (patch)
treeec29b7eaf18bacfee5a0cd850d15a115b9730d7a /src/common.c
parent4e92a484c00a8c2e18d4e2b8a1912ac1b86b4089 (diff)
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 <arturo.borrero.glez@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c77
1 files changed, 77 insertions, 0 deletions
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, "<event><type>new</type>");
+ } else if (flags & NFT_OF_EVENT_DEL) {
+ ret = snprintf(buf, size,
+ "<event><type>delete</type>");
+ } else {
+ ret = snprintf(buf, size,
+ "<event><type>unknown</type>");
+ }
+ 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, "</event>");
+ 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);
+}