libnftnl  1.2.0
nft-table-test.c
1 /*
2  * (C) 2013 by Ana Rey Botello <anarey@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <netinet/in.h>
15 
16 #include <linux/netfilter/nf_tables.h>
17 #include <libnftnl/table.h>
18 
19 static int test_ok = 1;
20 
21 static void print_err(const char *msg)
22 {
23  test_ok = 0;
24  printf("\033[31mERROR:\e[0m %s\n", msg);
25 }
26 
27 static void cmp_nftnl_table(struct nftnl_table *a, struct nftnl_table *b)
28 {
29  if (strcmp(nftnl_table_get_str(a, NFTNL_TABLE_NAME),
30  nftnl_table_get_str(b, NFTNL_TABLE_NAME)) != 0)
31  print_err("table name mismatches");
32  if (nftnl_table_get_u32(a, NFTNL_TABLE_FLAGS) !=
33  nftnl_table_get_u32(b, NFTNL_TABLE_FLAGS))
34  print_err("table flags mismatches");
35  if (nftnl_table_get_u32(a, NFTNL_TABLE_FAMILY) !=
36  nftnl_table_get_u32(b, NFTNL_TABLE_FAMILY))
37  print_err("tabke family mismatches");
38 }
39 
40 int main(int argc, char *argv[])
41 {
42  char buf[4096];
43  struct nlmsghdr *nlh;
44 
45  struct nftnl_table *a = NULL;
46  struct nftnl_table *b = NULL;
47  a = nftnl_table_alloc();
48  b = nftnl_table_alloc();
49 
50  if (a == NULL || b == NULL)
51  print_err("OOM");
52 
53  nftnl_table_set_str(a, NFTNL_TABLE_NAME, "test");
54  nftnl_table_set_u32(a, NFTNL_TABLE_FAMILY, AF_INET);
55  nftnl_table_set_u32(a, NFTNL_TABLE_FLAGS, 0);
56 
57  /* cmd extracted from include/linux/netfilter/nf_tables.h */
58  nlh = nftnl_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, AF_INET, 0,
59  1234);
60  nftnl_table_nlmsg_build_payload(nlh, a);
61 
62  if (nftnl_table_nlmsg_parse(nlh, b) < 0)
63  print_err("parsing problems");
64 
65  cmp_nftnl_table(a,b);
66 
67  nftnl_table_free(a);
68  nftnl_table_free(b);
69  if (!test_ok)
70  exit(EXIT_FAILURE);
71 
72  printf("%s: \033[32mOK\e[0m\n", argv[0]);
73  return EXIT_SUCCESS;
74 }