Debian Codegraph @

tests/nft-expr_fwd-test.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * (C) 2013 by Ana Rey Botello <[email protected]>
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <netinet/in.h>
10 #include <netinet/ip.h>
11
12 #include <linux/netfilter/nf_tables.h>
13 #include <libmnl/libmnl.h>
14 #include <libnftnl/rule.h>
15 #include <libnftnl/expr.h>
16
17 static int test_ok = 1;
18 static void print_err(const char *msg)
19 {
20 test_ok = 0;
21 printf("\033[31mERROR:\e[0m %s\n", msg);
22 }
23
24 static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
25 struct nftnl_expr *rule_b)
26 {
27 if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_FWD_SREG_DEV) !=
28 nftnl_expr_get_u32(rule_b, NFTNL_EXPR_FWD_SREG_DEV))
29 print_err("Expr SREG_OIF mismatches");
30 }
31
32 int main(int argc, char *argv[])
33 {
34 struct nftnl_rule *a, *b;
35 struct nftnl_expr *ex;
36 struct nlmsghdr *nlh;
37 char buf[4096];
38 struct nftnl_expr_iter *iter_a, *iter_b;
39 struct nftnl_expr *rule_a, *rule_b;
40
41 a = nftnl_rule_alloc();
42 b = nftnl_rule_alloc();
43 if (a == NULL || b == NULL)
44 print_err("OOM");
45 ex = nftnl_expr_alloc("dup");
46 if (ex == NULL)
47 print_err("OOM");
48
49 nftnl_expr_set_u32(ex, NFTNL_EXPR_FWD_SREG_DEV, 0x78123456);
50
51 nftnl_rule_add_expr(a, ex);
52
53 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
54 nftnl_rule_nlmsg_build_payload(nlh, a);
55
56 if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
57 print_err("parsing problems");
58
59 iter_a = nftnl_expr_iter_create(a);
60 iter_b = nftnl_expr_iter_create(b);
61 if (iter_a == NULL || iter_b == NULL)
62 print_err("OOM");
63
64 rule_a = nftnl_expr_iter_next(iter_a);
65 rule_b = nftnl_expr_iter_next(iter_b);
66 if (rule_a == NULL || rule_b == NULL)
67 print_err("OOM");
68
69 cmp_nftnl_expr(rule_a, rule_b);
70
71 if (nftnl_expr_iter_next(iter_a) != NULL ||
72 nftnl_expr_iter_next(iter_b) != NULL)
73 print_err("More 1 expr.");
74
75 nftnl_expr_iter_destroy(iter_a);
76 nftnl_expr_iter_destroy(iter_b);
77 nftnl_rule_free(a);
78 nftnl_rule_free(b);
79
80 if (!test_ok)
81 exit(EXIT_FAILURE);
82
83 printf("%s: \033[32mOK\e[0m\n", argv[0]);
84 return EXIT_SUCCESS;
85 }