libnftnl  1.1.4
nft-flowtable-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <netinet/in.h>
5 #include <linux/netfilter/nf_tables.h>
6 #include <libnftnl/flowtable.h>
7 
8 static int test_ok = 1;
9 
10 static void print_err(const char *msg)
11 {
12  test_ok = 0;
13  printf("\033[31mERROR:\e[0m %s\n", msg);
14 }
15 
16 static void cmp_nftnl_flowtable(struct nftnl_flowtable *a, struct nftnl_flowtable *b)
17 {
18  if (strcmp(nftnl_flowtable_get_str(a, NFTNL_FLOWTABLE_NAME),
19  nftnl_flowtable_get_str(b, NFTNL_FLOWTABLE_NAME)) != 0)
20  print_err("Chain name mismatches");
21  if (strcmp(nftnl_flowtable_get_str(a, NFTNL_FLOWTABLE_TABLE),
22  nftnl_flowtable_get_str(b, NFTNL_FLOWTABLE_TABLE)) != 0)
23  print_err("Chain table mismatches");
24  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_FAMILY) !=
25  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_FAMILY))
26  print_err("Chain family mismatches");
27  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_HOOKNUM) !=
28  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_HOOKNUM))
29  print_err("Chain hooknum mismatches");
30  if (nftnl_flowtable_get_s32(a, NFTNL_FLOWTABLE_PRIO) !=
31  nftnl_flowtable_get_s32(b, NFTNL_FLOWTABLE_PRIO))
32  print_err("Chain Prio mismatches");
33  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_USE) !=
34  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_USE))
35  print_err("Chain use mismatches");
36  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_SIZE) !=
37  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_SIZE))
38  print_err("Chain use mismatches");
39  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_FLAGS) !=
40  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_FLAGS))
41  print_err("Chain use mismatches");
42 }
43 
44 int main(int argc, char *argv[])
45 {
46  struct nftnl_flowtable *a, *b;
47  char buf[4096];
48  struct nlmsghdr *nlh;
49 
50  a = nftnl_flowtable_alloc();
51  b = nftnl_flowtable_alloc();
52  if (a == NULL || b == NULL)
53  print_err("OOM");
54 
55  nftnl_flowtable_set_str(a, NFTNL_FLOWTABLE_NAME, "test");
56  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_FAMILY, AF_INET);
57  nftnl_flowtable_set_str(a, NFTNL_FLOWTABLE_TABLE, "Table");
58  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_HOOKNUM, 0x34567812);
59  nftnl_flowtable_set_s32(a, NFTNL_FLOWTABLE_PRIO, 0x56781234);
60  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_USE, 0x78123456);
61  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_SIZE, 0x89016745);
62  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_FLAGS, 0x45016723);
63 
64  nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWFLOWTABLE, AF_INET,
65  0, 1234);
66  nftnl_flowtable_nlmsg_build_payload(nlh, a);
67 
68  if (nftnl_flowtable_nlmsg_parse(nlh, b) < 0)
69  print_err("parsing problems");
70 
71  cmp_nftnl_flowtable(a, b);
72 
73  nftnl_flowtable_free(a);
74  nftnl_flowtable_free(b);
75 
76  if (!test_ok)
77  exit(EXIT_FAILURE);
78 
79  printf("%s: \033[32mOK\e[0m\n", argv[0]);
80  return EXIT_SUCCESS;
81 }