libnftnl 1.2.4
nft-rule-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 <netinet/in.h>
16#include <linux/netfilter/nf_tables.h>
17#include <libnftnl/rule.h>
18#include <libnftnl/udata.h>
19
20static int test_ok = 1;
21
22static void print_err(const char *msg)
23{
24 test_ok = 0;
25 printf("\033[31mERROR:\e[0m %s\n", msg);
26}
27
28static void cmp_nftnl_rule(struct nftnl_rule *a, struct nftnl_rule *b)
29{
30 const void *udata_a, *udata_b;
31 uint32_t len_a, len_b;
32
33 if (nftnl_rule_get_u32(a, NFTNL_RULE_FAMILY) !=
34 nftnl_rule_get_u32(b, NFTNL_RULE_FAMILY))
35 print_err("Rule family mismatches");
36 if (strcmp(nftnl_rule_get_str(a, NFTNL_RULE_TABLE),
37 nftnl_rule_get_str(b, NFTNL_RULE_TABLE)) != 0)
38 print_err("Rule table mismatches");
39 if (strcmp(nftnl_rule_get_str(a, NFTNL_RULE_CHAIN),
40 nftnl_rule_get_str(b, NFTNL_RULE_CHAIN)) != 0)
41 print_err("Rule table mismatches");
42 if (nftnl_rule_get_u64(a, NFTNL_RULE_HANDLE) !=
43 nftnl_rule_get_u64(b, NFTNL_RULE_HANDLE))
44 print_err("Rule handle mismatches");
45 if (nftnl_rule_get_u32(a, NFTNL_RULE_COMPAT_PROTO) !=
46 nftnl_rule_get_u32(b, NFTNL_RULE_COMPAT_PROTO))
47 print_err("Rule compat_proto mismatches");
48 if (nftnl_rule_get_u32(a, NFTNL_RULE_COMPAT_FLAGS) !=
49 nftnl_rule_get_u32(b, NFTNL_RULE_COMPAT_FLAGS))
50 print_err("Rule compat_flags mismatches");
51 if (nftnl_rule_get_u64(a, NFTNL_RULE_POSITION) !=
52 nftnl_rule_get_u64(b, NFTNL_RULE_POSITION))
53 print_err("Rule compat_position mismatches");
54
55 udata_a = nftnl_rule_get_data(a, NFTNL_RULE_USERDATA, &len_a);
56 udata_b = nftnl_rule_get_data(b, NFTNL_RULE_USERDATA, &len_b);
57
58 if (len_a != len_b || memcmp(udata_a, udata_b, len_a) != 0)
59 print_err("Rule userdata mismatches");
60}
61
62int main(int argc, char *argv[])
63{
64 struct nftnl_udata_buf *udata;
65 struct nftnl_rule *a, *b;
66 char buf[4096];
67 struct nlmsghdr *nlh;
68
69 a = nftnl_rule_alloc();
70 b = nftnl_rule_alloc();
71 if (a == NULL || b == NULL)
72 print_err("OOM");
73
74 udata = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
75 if (!udata)
76 print_err("OOM");
77
78 if (!nftnl_udata_put_strz(udata, 0, "hello world"))
79 print_err("User data too big");
80
81 nftnl_rule_set_u32(a, NFTNL_RULE_FAMILY, AF_INET);
82 nftnl_rule_set_str(a, NFTNL_RULE_TABLE, "table");
83 nftnl_rule_set_str(a, NFTNL_RULE_CHAIN, "chain");
84 nftnl_rule_set_u64(a, NFTNL_RULE_HANDLE, 0x1234567812345678);
85 nftnl_rule_set_u32(a, NFTNL_RULE_COMPAT_PROTO, 0x12345678);
86 nftnl_rule_set_u32(a, NFTNL_RULE_COMPAT_FLAGS, 0x12345678);
87 nftnl_rule_set_u64(a, NFTNL_RULE_POSITION, 0x1234567812345678);
88 nftnl_rule_set_data(a, NFTNL_RULE_USERDATA,
89 nftnl_udata_buf_data(udata),
90 nftnl_udata_buf_len(udata));
91 nftnl_udata_buf_free(udata);
92
93 nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
94 nftnl_rule_nlmsg_build_payload(nlh, a);
95
96 if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
97 print_err("parsing problems");
98
99 cmp_nftnl_rule(a,b);
100
101 nftnl_rule_free(a);
102 nftnl_rule_free(b);
103 if (!test_ok)
104 exit(EXIT_FAILURE);
105
106 printf("%s: \033[32mOK\e[0m\n", argv[0]);
107 return EXIT_SUCCESS;
108}