libnftnl 1.2.6
nft-expr_match-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
15#include <arpa/inet.h>
16#include <netinet/in.h>
17#include <netinet/ip.h>
18#include <linux/netfilter/nf_tables.h>
19#include <libmnl/libmnl.h>
20#include <libnftnl/rule.h>
21#include <libnftnl/expr.h>
22
23static int test_ok = 1;
24
25static void print_err(const char *msg)
26{
27 test_ok = 0;
28 printf("\033[31mERROR:\e[0m %s\n", msg);
29}
30
31static void print_err2(const char *msg, uint32_t a, uint32_t b)
32{
33 test_ok = 0;
34 printf("\033[31mERROR:\e[0m %s size a: %d b: %d \n", msg, a, b);
35}
36
37static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
38 struct nftnl_expr *rule_b)
39{
40 uint32_t lena, lenb;
41
42 if (strcmp(nftnl_expr_get_str(rule_a, NFTNL_EXPR_MT_NAME),
43 nftnl_expr_get_str(rule_b, NFTNL_EXPR_MT_NAME)) != 0)
44 print_err("Expr NFTNL_EXPR_MT_NAME mismatches");
45 if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_MT_REV) !=
46 nftnl_expr_get_u32(rule_b, NFTNL_EXPR_MT_REV))
47 print_err("Expr NFTNL_EXPR_MT_REV mismatches");
48 nftnl_expr_get(rule_a, NFTNL_EXPR_MT_INFO, &lena);
49 nftnl_expr_get(rule_b, NFTNL_EXPR_MT_INFO, &lenb);
50 if (lena != lenb)
51 print_err2("Expr NFTNL_EXPR_MT_INFO size mismatches", lena, lenb);
52}
53
54int main(int argc, char *argv[])
55{
56 struct nftnl_rule *a, *b;
57 struct nftnl_expr *ex;
58 struct nlmsghdr *nlh;
59 char buf[4096];
60 struct nftnl_expr_iter *iter_a, *iter_b;
61 struct nftnl_expr *rule_a, *rule_b;
62 char data[16] = "0123456789abcdef";
63
64 a = nftnl_rule_alloc();
65 b = nftnl_rule_alloc();
66 if (a == NULL || b == NULL)
67 print_err("OOM");
68 ex = nftnl_expr_alloc("match");
69 if (ex == NULL)
70 print_err("OOM");
71
72 nftnl_expr_set_str(ex, NFTNL_EXPR_MT_NAME, "Tests");
73 nftnl_expr_set_u32(ex, NFTNL_EXPR_MT_REV, 0x12345678);
74 nftnl_expr_set(ex, NFTNL_EXPR_MT_INFO, strdup(data), sizeof(data));
75 nftnl_rule_add_expr(a, ex);
76
77 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
78 nftnl_rule_nlmsg_build_payload(nlh, a);
79
80 if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
81 print_err("parsing problems");
82 iter_a = nftnl_expr_iter_create(a);
83 iter_b = nftnl_expr_iter_create(b);
84 if (iter_a == NULL || iter_b == NULL)
85 print_err("OOM");
86
87 rule_a = nftnl_expr_iter_next(iter_a);
88 rule_b = nftnl_expr_iter_next(iter_b);
89 if (rule_a == NULL || rule_b == NULL)
90 print_err("OOM");
91
92 cmp_nftnl_expr(rule_a, rule_b);
93
94 if (nftnl_expr_iter_next(iter_a) != NULL ||
95 nftnl_expr_iter_next(iter_b) != NULL)
96 print_err("More 1 expr.");
97
98 nftnl_expr_iter_destroy(iter_a);
99 nftnl_expr_iter_destroy(iter_b);
100 nftnl_rule_free(a);
101 nftnl_rule_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}