Skip to contentSkip to conversation

common: add wrapper to represent events

PATCH In review Arturo Borrero Gonzalez 2 participants 3 messages Apr 15, 2014 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 common: add wrapper to represent events

This patch adds a simple string wrapper to represent nf_tables events.

---
 include/libnftnl/common.h |    6 +++++
 src/common.c              |   59 +++++++++++++++++++++++++++++++++++++++++++++
 src/libnftnl.map          |    3 ++
 3 files changed, 68 insertions(+)
include/libnftnl/common.h +6 -0
index f0c20f0..96f8155 100644
@@ -2,6 +2,7 @@
 #define _LIBNFTNL_COMMON_H_
 
 #include <stdint.h>
+#include <stdio.h>
 
 enum {
 	NFT_PARSE_EBADINPUT	= 0,
@@ -30,4 +31,9 @@ struct nlmsghdr *nft_nlmsg_build_hdr(char *buf, uint16_t cmd, uint16_t family,
 struct nft_parse_err *nft_parse_err_alloc(void);
 void nft_parse_err_free(struct nft_parse_err *);
 int nft_parse_perror(const char *str, struct nft_parse_err *err);
+int nft_event_snprintf(char *buf, size_t bufsiz, const char *content,
+		       uint32_t format, uint32_t type);
+int nft_event_fprintf(FILE *fp, const char *content,
+		      uint32_t format, uint32_t type);
+
 #endif
src/common.c +59 -0

71 lines not on this page · raw message

src/libnftnl.map +3 -0
index b11db67..18e58fa 100644
@@ -205,4 +205,7 @@ LIBNFTNL_1.1 {
   nft_rule_attr_get_data;
   nft_set_attr_set_data;
   nft_set_attr_get_data;
+
+  nft_event_snprintf;
+  nft_event_fprintf;
 } LIBNFTNL_1.0;
#1 lore raw

PATCH 2/2 examples: nft-events: use new events wrappers

Let's use the new event wrappers in the events example.

---
 examples/nft-events.c |   16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
examples/nft-events.c +11 -5
index 989f4bd..9d33295 100644
@@ -22,6 +22,7 @@
 #include <libnftnl/chain.h>
 #include <libnftnl/rule.h>
 #include <libnftnl/set.h>
+#include <libnftnl/common.h>
 
 static int table_cb(const struct nlmsghdr *nlh, int type)
 {
@@ -40,7 +41,8 @@ static int table_cb(const struct nlmsghdr *nlh, int type)
 	}
 
 	nft_table_snprintf(buf, sizeof(buf), t, NFT_OUTPUT_DEFAULT, 0);
-	printf("[%s]\t%s\n", type == NFT_MSG_NEWTABLE ? "NEW" : "DEL", buf);
+	nft_event_fprintf(stdout, buf, NFT_OUTPUT_DEFAULT, type);
+	fprintf(stdout, "\n");
 
 err_free:
 	nft_table_free(t);
@@ -65,7 +67,8 @@ static int rule_cb(const struct nlmsghdr *nlh, int type)
 	}
 
 	nft_rule_snprintf(buf, sizeof(buf), t, NFT_OUTPUT_DEFAULT, 0);
-	printf("[%s]\t%s\n", type == NFT_MSG_NEWRULE ? "NEW" : "DEL", buf);
+	nft_event_fprintf(stdout, buf, NFT_OUTPUT_DEFAULT, type);
+	fprintf(stdout, "\n");
 
 err_free:
 	nft_rule_free(t);
@@ -90,7 +93,8 @@ static int chain_cb(const struct nlmsghdr *nlh, int type)
 	}
 
 	nft_chain_snprintf(buf, sizeof(buf), t, NFT_OUTPUT_DEFAULT, 0);
-	printf("[%s]\t%s\n", type == NFT_MSG_NEWCHAIN ? "NEW" : "DEL", buf);
+	nft_event_fprintf(stdout, buf, NFT_OUTPUT_DEFAULT, type);
+	fprintf(stdout, "\n");
 
 err_free:
 	nft_chain_free(t);
@@ -115,7 +119,8 @@ static int set_cb(const struct nlmsghdr *nlh, int type)
 	}
 
 	nft_set_snprintf(buf, sizeof(buf), t, NFT_OUTPUT_DEFAULT, 0);
-	printf("[%s]\t%s\n", type == NFT_MSG_NEWSET ? "NEW" : "DEL", buf);
+	nft_event_fprintf(stdout, buf, NFT_OUTPUT_DEFAULT, type);
+	fprintf(stdout, "\n");
 
 err_free:
 	nft_set_free(t);
@@ -141,7 +146,8 @@ static int setelem_cb(const struct nlmsghdr *nlh, int type)
 	}
 
 	nft_set_snprintf(buf, sizeof(buf), s, NFT_OUTPUT_DEFAULT, 0);
-	printf("[%s]\t%s\n", type == NFT_MSG_NEWSETELEM ? "NEW" : "DEL", buf);
+	nft_event_fprintf(stdout, buf, NFT_OUTPUT_DEFAULT, type);
+	fprintf(stdout, "\n");
 
 err_free:
 	nft_set_free(s);
#2 lore raw
In reply to Arturo Borrero Gonzalez
Quoted 22 lines · Arturo Borrero Gonzalez

This patch adds a simple string wrapper to represent nf_tables events.

---
 include/libnftnl/common.h |    6 +++++
 src/common.c              |   59 +++++++++++++++++++++++++++++++++++++++++++++
 src/libnftnl.map          |    3 ++
 3 files changed, 68 insertions(+)
include/libnftnl/common.h +1 -0
index f0c20f0..96f8155 100644
@@ -2,6 +2,7 @@
 #define _LIBNFTNL_COMMON_H_
 
 #include <stdint.h>
+#include <stdio.h>
 
 enum {
 	NFT_PARSE_EBADINPUT	= 0,

include/libnftnl/common.h struct nlmsghdr *nft_nlmsg_build_hdr(char *buf, uint16_t cmd, uint16_t family, · line 37

Quoted 7 lines
include/libnftnl/common.h +4 -0
@@ -30,4 +31,9 @@ struct nlmsghdr *nft_nlmsg_build_hdr(char *buf, uint16_t cmd, uint16_t family,
 struct nft_parse_err *nft_parse_err_alloc(void);
 void nft_parse_err_free(struct nft_parse_err *);
 int nft_parse_perror(const char *str, struct nft_parse_err *err);
+int nft_event_snprintf(char *buf, size_t bufsiz, const char *content,
+		       uint32_t format, uint32_t type);
+int nft_event_fprintf(FILE *fp, const char *content,
+		      uint32_t format, uint32_t type);

We have flags in the existing output functions:

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);

I think you can add a new flag that tells that you have to add the extra event handling that you need, thus, we don't need a new interface.

You can most likely reuse most of the code in this patch.