libnftnl  1.0.5
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 
19 static int test_ok = 1;
20 
21 static void print_err(const char *msg)
22 {
23  test_ok = 0;
24  printf("\033[31mERROR:\e[0m %s\n", msg);
25 }
26 
27 static 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 }
51 
52 int main(int argc, char *argv[])
53 {
54  struct nftnl_set *a, *b = NULL;
55  char buf[4096];
56  struct nlmsghdr *nlh;
57 
58  a = nftnl_set_alloc();
59  b = nftnl_set_alloc();
60  if (a == NULL || b == NULL)
61  print_err("OOM");
62 
63  nftnl_set_set_str(a, NFTNL_SET_TABLE, "test-table");
64  nftnl_set_set_str(a, NFTNL_SET_NAME, "test-name");
65  nftnl_set_set_u32(a, NFTNL_SET_FLAGS, 0x12345678);
66  nftnl_set_set_u32(a, NFTNL_SET_KEY_TYPE, 0x12345678);
67  nftnl_set_set_u32(a, NFTNL_SET_KEY_LEN, 0x12345678);
68  nftnl_set_set_u32(a, NFTNL_SET_DATA_TYPE, 0x12345678);
69  nftnl_set_set_u32(a, NFTNL_SET_DATA_LEN, 0x12345678);
70  nftnl_set_set_u32(a, NFTNL_SET_FAMILY, 0x12345678);
71 
72  /* cmd extracted from include/linux/netfilter/nf_tables.h */
73  nlh = nftnl_set_nlmsg_build_hdr(buf, NFT_MSG_NEWSET, AF_INET, 0, 1234);
74  nftnl_set_nlmsg_build_payload(nlh, a);
75 
76  if (nftnl_set_nlmsg_parse(nlh, b) < 0)
77  print_err("parsing problems");
78 
79  cmp_nftnl_set(a,b);
80 
81  nftnl_set_free(a); nftnl_set_free(b);
82 
83  if (!test_ok)
84  exit(EXIT_FAILURE);
85 
86  printf("%s: \033[32mOK\e[0m\n", argv[0]);
87  return EXIT_SUCCESS;
88 }