libnftnl  1.2.9
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_devices(const char * const *adevs,
17  const char * const *bdevs)
18 {
19  int i;
20 
21  if (!adevs && !bdevs)
22  return;
23  if (!!adevs ^ !!bdevs)
24  print_err("Flowtable devices mismatches");
25  for (i = 0; adevs[i] && bdevs[i]; i++) {
26  if (strcmp(adevs[i], bdevs[i]))
27  break;
28  }
29  if (adevs[i] || bdevs[i])
30  print_err("Flowtable devices mismatches");
31 }
32 
33 static void cmp_nftnl_flowtable(struct nftnl_flowtable *a, struct nftnl_flowtable *b)
34 {
35  if (strcmp(nftnl_flowtable_get_str(a, NFTNL_FLOWTABLE_NAME),
36  nftnl_flowtable_get_str(b, NFTNL_FLOWTABLE_NAME)) != 0)
37  print_err("Flowtable name mismatches");
38  if (strcmp(nftnl_flowtable_get_str(a, NFTNL_FLOWTABLE_TABLE),
39  nftnl_flowtable_get_str(b, NFTNL_FLOWTABLE_TABLE)) != 0)
40  print_err("Flowtable table mismatches");
41  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_FAMILY) !=
42  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_FAMILY))
43  print_err("Flowtable family mismatches");
44  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_HOOKNUM) !=
45  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_HOOKNUM))
46  print_err("Flowtable hooknum mismatches");
47  if (nftnl_flowtable_get_s32(a, NFTNL_FLOWTABLE_PRIO) !=
48  nftnl_flowtable_get_s32(b, NFTNL_FLOWTABLE_PRIO))
49  print_err("Flowtable prio mismatches");
50  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_USE) !=
51  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_USE))
52  print_err("Flowtable use mismatches");
53 #if 0
54  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_SIZE) !=
55  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_SIZE))
56  print_err("Flowtable size mismatches");
57 #endif
58  if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_FLAGS) !=
59  nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_FLAGS))
60  print_err("Flowtable flags mismatches");
61  if (nftnl_flowtable_get_u64(a, NFTNL_FLOWTABLE_HANDLE) !=
62  nftnl_flowtable_get_u64(b, NFTNL_FLOWTABLE_HANDLE))
63  print_err("Flowtable handle mismatches");
64  cmp_devices(nftnl_flowtable_get_array(a, NFTNL_FLOWTABLE_DEVICES),
65  nftnl_flowtable_get_array(b, NFTNL_FLOWTABLE_DEVICES));
66 }
67 
68 int main(int argc, char *argv[])
69 {
70  const char *devs[] = { "eth0", "eth1", "eth2", NULL };
71  struct nftnl_flowtable *a, *b;
72  char buf[4096];
73  struct nlmsghdr *nlh;
74 
75  a = nftnl_flowtable_alloc();
76  b = nftnl_flowtable_alloc();
77  if (a == NULL || b == NULL)
78  print_err("OOM");
79 
80  nftnl_flowtable_set_str(a, NFTNL_FLOWTABLE_NAME, "test");
81  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_FAMILY, AF_INET);
82  nftnl_flowtable_set_str(a, NFTNL_FLOWTABLE_TABLE, "Table");
83  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_HOOKNUM, 0x34567812);
84  nftnl_flowtable_set_s32(a, NFTNL_FLOWTABLE_PRIO, 0x56781234);
85  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_USE, 0x78123456);
86  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_SIZE, 0x89016745);
87  nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_FLAGS, 0x45016723);
88  nftnl_flowtable_set_u64(a, NFTNL_FLOWTABLE_HANDLE, 0x2345016789);
89  nftnl_flowtable_set_array(a, NFTNL_FLOWTABLE_DEVICES, devs);
90 
91  nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWFLOWTABLE, AF_INET,
92  0, 1234);
93  nftnl_flowtable_nlmsg_build_payload(nlh, a);
94 
95  if (nftnl_flowtable_nlmsg_parse(nlh, b) < 0)
96  print_err("parsing problems");
97 
98  cmp_nftnl_flowtable(a, b);
99 
100  nftnl_flowtable_free(a);
101  nftnl_flowtable_free(b);
102 
103  if (!test_ok)
104  exit(EXIT_FAILURE);
105 
106  printf("%s: \033[32mOK\e[0m\n", argv[0]);
107  return EXIT_SUCCESS;
108 }