libnftnl 1.2.6
nft-set-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 <linux/netfilter/nf_tables.h>
16
17#include <libnftnl/set.h>
18
19static int test_ok = 1;
20
21static void print_err(const char *msg)
22{
23 test_ok = 0;
24 printf("\033[31mERROR:\e[0m %s\n", msg);
25}
26
27static void cmp_nftnl_set(struct nftnl_set *a, struct nftnl_set *b)
28{
29 if (strcmp(nftnl_set_get_str(a, NFTNL_SET_TABLE),
30 nftnl_set_get_str(b, NFTNL_SET_TABLE)) != 0)
31 print_err("Set table mismatches");
32 if (strcmp(nftnl_set_get_str(a, NFTNL_SET_NAME),
33 nftnl_set_get_str(b, NFTNL_SET_NAME)) != 0)
34 print_err("Set name mismatches");
35 if (nftnl_set_get_u32(a, NFTNL_SET_FLAGS) !=
36 nftnl_set_get_u32(b, NFTNL_SET_FLAGS))
37 print_err("Set flags mismatches");
38 if (nftnl_set_get_u32(a, NFTNL_SET_KEY_TYPE) !=
39 nftnl_set_get_u32(b, NFTNL_SET_KEY_TYPE))
40 print_err("Set key-type mismatches");
41 if (nftnl_set_get_u32(a, NFTNL_SET_KEY_LEN) !=
42 nftnl_set_get_u32(b, NFTNL_SET_KEY_LEN))
43 print_err("Set key-len mismatches");
44 if (nftnl_set_get_u32(a, NFTNL_SET_DATA_TYPE) !=
45 nftnl_set_get_u32(b, NFTNL_SET_DATA_TYPE))
46 print_err("Set data-type mismatches");
47 if (nftnl_set_get_u32(a, NFTNL_SET_DATA_LEN) !=
48 nftnl_set_get_u32(b, NFTNL_SET_DATA_LEN))
49 print_err("Set data-len mismatches");
50 if (strcmp(nftnl_set_get_str(a, NFTNL_SET_USERDATA),
51 nftnl_set_get_str(b, NFTNL_SET_USERDATA)) != 0)
52 print_err("Set userdata mismatches");
53}
54
55int main(int argc, char *argv[])
56{
57 struct nftnl_set *a, *b = NULL;
58 char buf[4096];
59 struct nlmsghdr *nlh;
60
61 a = nftnl_set_alloc();
62 b = nftnl_set_alloc();
63 if (a == NULL || b == NULL)
64 print_err("OOM");
65
66 nftnl_set_set_str(a, NFTNL_SET_TABLE, "test-table");
67 nftnl_set_set_str(a, NFTNL_SET_NAME, "test-name");
68 nftnl_set_set_u32(a, NFTNL_SET_FLAGS, 0x12345678);
69 nftnl_set_set_u32(a, NFTNL_SET_KEY_TYPE, 0x12345678);
70 nftnl_set_set_u32(a, NFTNL_SET_KEY_LEN, 0x12345678);
71 nftnl_set_set_u32(a, NFTNL_SET_DATA_TYPE, 0x12345678);
72 nftnl_set_set_u32(a, NFTNL_SET_DATA_LEN, 0x12345678);
73 nftnl_set_set_u32(a, NFTNL_SET_FAMILY, 0x12345678);
74 nftnl_set_set_str(a, NFTNL_SET_USERDATA, "testing user data");
75
76 /* cmd extracted from include/linux/netfilter/nf_tables.h */
77 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWSET, AF_INET, 0, 1234);
78 nftnl_set_nlmsg_build_payload(nlh, a);
79
80 if (nftnl_set_nlmsg_parse(nlh, b) < 0)
81 print_err("parsing problems");
82
83 cmp_nftnl_set(a,b);
84
85 nftnl_set_free(a); nftnl_set_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}