summaryrefslogtreecommitdiffstats
path: root/examples/nft-table-add.c
diff options
context:
space:
mode:
authorArturo Borrero <arturo.borrero.glez@gmail.com>2014-08-22 11:39:40 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2014-08-24 14:50:19 +0200
commit1091cd13df1b0071a890aeb4c50041d01548bd25 (patch)
tree01f36fb5911cf527129eda64c7d62e84a25ffef5 /examples/nft-table-add.c
parentb2aac348d9750c879cc83c98a3d161c7d4ce1f4e (diff)
examples: nft-table-add: add table_add_parse()
This fucntion parses the command line options and creates the nft_table object. Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'examples/nft-table-add.c')
-rw-r--r--examples/nft-table-add.c55
1 files 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);