libnftnl 1.2.6
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
8static int test_ok = 1;
9
10static void print_err(const char *msg)
11{
12 test_ok = 0;
13 printf("\033[31mERROR:\e[0m %s\n", msg);
14}
15
16static 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("Flowtable 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("Flowtable table mismatches");
24 if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_FAMILY) !=
25 nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_FAMILY))
26 print_err("Flowtable family mismatches");
27 if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_HOOKNUM) !=
28 nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_HOOKNUM))
29 print_err("Flowtable hooknum mismatches");
30 if (nftnl_flowtable_get_s32(a, NFTNL_FLOWTABLE_PRIO) !=
31 nftnl_flowtable_get_s32(b, NFTNL_FLOWTABLE_PRIO))
32 print_err("Flowtable prio mismatches");
33 if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_USE) !=
34 nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_USE))
35 print_err("Flowtable use mismatches");
36#if 0
37 if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_SIZE) !=
38 nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_SIZE))
39 print_err("Flowtable size mismatches");
40#endif
41 if (nftnl_flowtable_get_u32(a, NFTNL_FLOWTABLE_FLAGS) !=
42 nftnl_flowtable_get_u32(b, NFTNL_FLOWTABLE_FLAGS))
43 print_err("Flowtable flags mismatches");
44 if (nftnl_flowtable_get_u64(a, NFTNL_FLOWTABLE_HANDLE) !=
45 nftnl_flowtable_get_u64(b, NFTNL_FLOWTABLE_HANDLE))
46 print_err("Flowtable handle mismatches");
47}
48
49int main(int argc, char *argv[])
50{
51 struct nftnl_flowtable *a, *b;
52 char buf[4096];
53 struct nlmsghdr *nlh;
54
55 a = nftnl_flowtable_alloc();
56 b = nftnl_flowtable_alloc();
57 if (a == NULL || b == NULL)
58 print_err("OOM");
59
60 nftnl_flowtable_set_str(a, NFTNL_FLOWTABLE_NAME, "test");
61 nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_FAMILY, AF_INET);
62 nftnl_flowtable_set_str(a, NFTNL_FLOWTABLE_TABLE, "Table");
63 nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_HOOKNUM, 0x34567812);
64 nftnl_flowtable_set_s32(a, NFTNL_FLOWTABLE_PRIO, 0x56781234);
65 nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_USE, 0x78123456);
66 nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_SIZE, 0x89016745);
67 nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_FLAGS, 0x45016723);
68 nftnl_flowtable_set_u64(a, NFTNL_FLOWTABLE_HANDLE, 0x2345016789);
69
70 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWFLOWTABLE, AF_INET,
71 0, 1234);
72 nftnl_flowtable_nlmsg_build_payload(nlh, a);
73
74 if (nftnl_flowtable_nlmsg_parse(nlh, b) < 0)
75 print_err("parsing problems");
76
77 cmp_nftnl_flowtable(a, b);
78
79 nftnl_flowtable_free(a);
80 nftnl_flowtable_free(b);
81
82 if (!test_ok)
83 exit(EXIT_FAILURE);
84
85 printf("%s: \033[32mOK\e[0m\n", argv[0]);
86 return EXIT_SUCCESS;
87}