| 1 |
|
| 2 |
|
| 3 |
|
| 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_CT_KEY) != |
| 28 |
nftnl_expr_get_u32(rule_b, NFTNL_EXPR_CT_KEY)) |
| 29 |
print_err("Expr CT_KEY mismatches"); |
| 30 |
if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_CT_DIR) != |
| 31 |
nftnl_expr_get_u32(rule_b, NFTNL_EXPR_CT_DIR)) |
| 32 |
print_err("Expr CT_DIR mismatches"); |
| 33 |
if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_CT_DREG) != |
| 34 |
nftnl_expr_get_u32(rule_b, NFTNL_EXPR_CT_DREG)) |
| 35 |
print_err("Expr CT_DREG mismatches"); |
| 36 |
} |
| 37 |
int 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("ct"); |
| 51 |
if (ex == NULL) |
| 52 |
print_err("OOM"); |
| 53 |
|
| 54 |
nftnl_expr_set_u32(ex, NFTNL_EXPR_CT_KEY, 0x1234568); |
| 55 |
nftnl_expr_set_u32(ex, NFTNL_EXPR_CT_DIR, 0x12); |
| 56 |
nftnl_expr_set_u32(ex, NFTNL_EXPR_CT_DREG, 0x78123456); |
| 57 |
|
| 58 |
nftnl_rule_add_expr(a, ex); |
| 59 |
|
| 60 |
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234); |
| 61 |
nftnl_rule_nlmsg_build_payload(nlh, a); |
| 62 |
|
| 63 |
if (nftnl_rule_nlmsg_parse(nlh, b) < 0) |
| 64 |
print_err("parsing problems"); |
| 65 |
|
| 66 |
iter_a = nftnl_expr_iter_create(a); |
| 67 |
iter_b = nftnl_expr_iter_create(b); |
| 68 |
if (iter_a == NULL || iter_b == NULL) |
| 69 |
print_err("OOM"); |
| 70 |
|
| 71 |
rule_a = nftnl_expr_iter_next(iter_a); |
| 72 |
rule_b = nftnl_expr_iter_next(iter_b); |
| 73 |
if (rule_a == NULL || rule_b == NULL) |
| 74 |
print_err("OOM"); |
| 75 |
|
| 76 |
cmp_nftnl_expr(rule_a, rule_b); |
| 77 |
|
| 78 |
if (nftnl_expr_iter_next(iter_a) != NULL || |
| 79 |
nftnl_expr_iter_next(iter_b) != NULL) |
| 80 |
print_err("More 1 expr."); |
| 81 |
|
| 82 |
nftnl_expr_iter_destroy(iter_a); |
| 83 |
nftnl_expr_iter_destroy(iter_b); |
| 84 |
nftnl_rule_free(a); |
| 85 |
nftnl_rule_free(b); |
| 86 |
|
| 87 |
if (!test_ok) |
| 88 |
exit(EXIT_FAILURE); |
| 89 |
|
| 90 |
printf("%s: \033[32mOK\e[0m\n", argv[0]); |
| 91 |
return EXIT_SUCCESS; |
| 92 |
} |