libnftnl  1.2.9
nft-table-test.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * (C) 2013 by Ana Rey Botello <anarey@gmail.com>
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <netinet/in.h>
10 
11 #include <linux/netfilter/nf_tables.h>
12 #include <libnftnl/table.h>
13 
14 static int test_ok = 1;
15 
16 static void print_err(const char *msg)
17 {
18  test_ok = 0;
19  printf("\033[31mERROR:\e[0m %s\n", msg);
20 }
21 
22 static void cmp_nftnl_table(struct nftnl_table *a, struct nftnl_table *b)
23 {
24  if (strcmp(nftnl_table_get_str(a, NFTNL_TABLE_NAME),
25  nftnl_table_get_str(b, NFTNL_TABLE_NAME)) != 0)
26  print_err("table name mismatches");
27  if (nftnl_table_get_u32(a, NFTNL_TABLE_FLAGS) !=
28  nftnl_table_get_u32(b, NFTNL_TABLE_FLAGS))
29  print_err("table flags mismatches");
30  if (nftnl_table_get_u32(a, NFTNL_TABLE_FAMILY) !=
31  nftnl_table_get_u32(b, NFTNL_TABLE_FAMILY))
32  print_err("table family mismatches");
33 }
34 
35 int main(int argc, char *argv[])
36 {
37  char buf[4096];
38  struct nlmsghdr *nlh;
39 
40  struct nftnl_table *a = NULL;
41  struct nftnl_table *b = NULL;
42  a = nftnl_table_alloc();
43  b = nftnl_table_alloc();
44 
45  if (a == NULL || b == NULL)
46  print_err("OOM");
47 
48  nftnl_table_set_str(a, NFTNL_TABLE_NAME, "test");
49  nftnl_table_set_u32(a, NFTNL_TABLE_FAMILY, AF_INET);
50  nftnl_table_set_u32(a, NFTNL_TABLE_FLAGS, 0);
51 
52  /* cmd extracted from include/linux/netfilter/nf_tables.h */
53  nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, AF_INET, 0, 1234);
54  nftnl_table_nlmsg_build_payload(nlh, a);
55 
56  if (nftnl_table_nlmsg_parse(nlh, b) < 0)
57  print_err("parsing problems");
58 
59  cmp_nftnl_table(a,b);
60 
61  nftnl_table_free(a);
62  nftnl_table_free(b);
63  if (!test_ok)
64  exit(EXIT_FAILURE);
65 
66  printf("%s: \033[32mOK\e[0m\n", argv[0]);
67  return EXIT_SUCCESS;
68 }