libnftnl 1.2.6
nft-expr_fwd-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#include <netinet/ip.h>
16
17#include <linux/netfilter/nf_tables.h>
18#include <libmnl/libmnl.h>
19#include <libnftnl/rule.h>
20#include <libnftnl/expr.h>
21
22static int test_ok = 1;
23static void print_err(const char *msg)
24{
25 test_ok = 0;
26 printf("\033[31mERROR:\e[0m %s\n", msg);
27}
28
29static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
30 struct nftnl_expr *rule_b)
31{
32 if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_FWD_SREG_DEV) !=
33 nftnl_expr_get_u32(rule_b, NFTNL_EXPR_FWD_SREG_DEV))
34 print_err("Expr SREG_OIF mismatches");
35}
36
37int main(int argc, char *argv[])
38{
39 struct nftnl_rule *a, *b;
40 struct nftnl_expr *ex;
41 struct nlmsghdr *nlh;
42 char buf[4096];
43 struct nftnl_expr_iter *iter_a, *iter_b;
44 struct nftnl_expr *rule_a, *rule_b;
45
46 a = nftnl_rule_alloc();
47 b = nftnl_rule_alloc();
48 if (a == NULL || b == NULL)
49 print_err("OOM");
50 ex = nftnl_expr_alloc("dup");
51 if (ex == NULL)
52 print_err("OOM");
53
54 nftnl_expr_set_u32(ex, NFTNL_EXPR_FWD_SREG_DEV, 0x78123456);
55
56 nftnl_rule_add_expr(a, ex);
57
58 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
59 nftnl_rule_nlmsg_build_payload(nlh, a);
60
61 if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
62 print_err("parsing problems");
63
64 iter_a = nftnl_expr_iter_create(a);
65 iter_b = nftnl_expr_iter_create(b);
66 if (iter_a == NULL || iter_b == NULL)
67 print_err("OOM");
68
69 rule_a = nftnl_expr_iter_next(iter_a);
70 rule_b = nftnl_expr_iter_next(iter_b);
71 if (rule_a == NULL || rule_b == NULL)
72 print_err("OOM");
73
74 cmp_nftnl_expr(rule_a, rule_b);
75
76 if (nftnl_expr_iter_next(iter_a) != NULL ||
77 nftnl_expr_iter_next(iter_b) != NULL)
78 print_err("More 1 expr.");
79
80 nftnl_expr_iter_destroy(iter_a);
81 nftnl_expr_iter_destroy(iter_b);
82 nftnl_rule_free(a);
83 nftnl_rule_free(b);
84
85 if (!test_ok)
86 exit(EXIT_FAILURE);
87
88 printf("%s: \033[32mOK\e[0m\n", argv[0]);
89 return EXIT_SUCCESS;
90}