[libnftnl PATCH 3/6] examples: nft-table-add: add table_add_parse()

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

 



This fucntion parses the command line options and creates the nft_table object.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@xxxxxxxxx>
---
 examples/nft-table-add.c |   55 +++++++++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/examples/nft-table-add.c b/examples/nft-table-add.c
index 3b7572f..9c3ae6f 100644
--- a/examples/nft-table-add.c
+++ b/examples/nft-table-add.c
@@ -20,27 +20,11 @@
 #include <libmnl/libmnl.h>
 #include <libnftnl/table.h>
 
-int main(int argc, char *argv[])
+static struct nft_table *table_add_parse(int argc, char *argv[])
 {
-	struct mnl_socket *nl;
-	char buf[MNL_SOCKET_BUFFER_SIZE];
-	struct nlmsghdr *nlh;
-	uint32_t portid, seq, family;
-	struct nft_table *t = NULL;
-	int ret;
-
-	if (argc != 3) {
-		fprintf(stderr, "%s <family> <name>\n", argv[0]);
-		exit(EXIT_FAILURE);
-	}
+	struct nft_table *t;
+	uint16_t family;
 
-	t = nft_table_alloc();
-	if (t == NULL) {
-		perror("OOM");
-		exit(EXIT_FAILURE);
-	}
-
-	seq = time(NULL);
 	if (strcmp(argv[1], "ip") == 0)
 		family = NFPROTO_IPV4;
 	else if (strcmp(argv[1], "ip6") == 0)
@@ -51,11 +35,42 @@ int main(int argc, char *argv[])
 		family = NFPROTO_ARP;
 	else {
 		fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
+		return NULL;
+	}
+
+	t = nft_table_alloc();
+	if (t == NULL) {
+		perror("OOM");
+		return NULL;
+	}
+
+	nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, family);
+	nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, argv[2]);
+
+	return t;
+}
+
+int main(int argc, char *argv[])
+{
+	struct mnl_socket *nl;
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+	struct nlmsghdr *nlh;
+	uint32_t portid, seq, family;
+	struct nft_table *t;
+	int ret;
+
+	if (argc != 3) {
+		fprintf(stderr, "%s <family> <name>\n", argv[0]);
 		exit(EXIT_FAILURE);
 	}
 
-	nft_table_attr_set(t, NFT_TABLE_ATTR_NAME, argv[2]);
+	t = table_add_parse(argc, argv);
+	if (t == NULL)
+		exit(EXIT_FAILURE);
 
+
+	seq = time(NULL);
+	family = nft_table_attr_get_u32(t, NFT_TABLE_ATTR_FAMILY);
 	nlh = nft_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, family,
 					NLM_F_ACK, seq);
 	nft_table_nlmsg_build_payload(nlh, t);

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