[PATCH 3/3,libnftnl] examples: Modify the example 'nft-set-add' to

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



It lets you to add an string:
	> nft add table mytable
	> exmample/nft-set-add ip mytable myset "hello world!"
	> example/nft-set-get ip json
	{"set":{"name":"myset","table":"mytable","flags":2,"family":"ip","key_ty
	pe":0,"key_len":2,"userdata_len":12,"userdata":"hello world!"}}

Signed-off-by: Carlos Falgueras García <carlosfg@xxxxxxxxxx>
---
 examples/nft-set-add.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/examples/nft-set-add.c b/examples/nft-set-add.c
index e040aca..42d99ce 100644
--- a/examples/nft-set-add.c
+++ b/examples/nft-set-add.c
@@ -29,7 +29,8 @@
 #include <libnftnl/set.h>
 
 static struct nftnl_set *setup_set(uint8_t family, const char *table,
-				 const char *name)
+				 const char *name, const char *udata,
+				 unsigned int udlen)
 {
 	struct nftnl_set *s = NULL;
 
@@ -44,6 +45,8 @@ static struct nftnl_set *setup_set(uint8_t family, const char *table,
 	nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
 	nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, 2);
 	nftnl_set_set_u32(s, NFTNL_SET_ID, 1);
+	if (udata != NULL)
+		nftnl_set_set_data(s, NFTNL_SET_USERDATA, udata, udlen);
 	nftnl_set_set_u32(s, NFTNL_SET_FLAGS, NFT_SET_CONSTANT);
 
 	return s;
@@ -72,12 +75,15 @@ int main(int argc, char *argv[])
 	struct nlmsghdr *nlh;
 	struct mnl_nlmsg_batch *batch;
 	uint8_t family;
-	char buf[MNL_SOCKET_BUFFER_SIZE];
+	char buf[2*MNL_SOCKET_BUFFER_SIZE];
 	uint32_t seq = time(NULL);
 	int ret;
 
-	if (argc != 4) {
-		fprintf(stderr, "Usage: %s <family> <table> <setname>\n", argv[0]);
+	if (argc < 4 || argc > 5) {
+		fprintf(stderr,
+			"Usage: %s <family> <table> <setname> [<user_data>]\n",
+			argv[0]
+		);
 		exit(EXIT_FAILURE);
 	}
 
@@ -94,7 +100,10 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	s = setup_set(family, argv[2], argv[3]);
+	if (argc == 5)
+		s = setup_set(family, argv[2], argv[3], argv[4], strlen(argv[4]));
+	else
+		s = setup_set(family, argv[2], argv[3], NULL, 0);
 
 	nl = mnl_socket_open(NETLINK_NETFILTER);
 	if (nl == NULL) {
@@ -107,7 +116,7 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
+	batch = mnl_nlmsg_batch_start(buf, sizeof(buf)/2);
 
 	nftnl_mnl_batch_put(mnl_nlmsg_batch_current(batch),
 			  NFNL_MSG_BATCH_BEGIN, seq++);
-- 
2.6.4

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux