libnftnl  1.2.6
nft-chain-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 #include <libnftnl/chain.h>
17 
18 static int test_ok = 1;
19 
20 static void print_err(const char *msg)
21 {
22  test_ok = 0;
23  printf("\033[31mERROR:\e[0m %s\n", msg);
24 }
25 
26 static void cmp_nftnl_chain(struct nftnl_chain *a, struct nftnl_chain *b)
27 {
28 
29  if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_NAME),
30  nftnl_chain_get_str(b, NFTNL_CHAIN_NAME)) != 0)
31  print_err("Chain name mismatches");
32  if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_TABLE),
33  nftnl_chain_get_str(b, NFTNL_CHAIN_TABLE)) != 0)
34  print_err("Chain table mismatches");
35  if (nftnl_chain_get_u32(a, NFTNL_CHAIN_FAMILY) !=
36  nftnl_chain_get_u32(b, NFTNL_CHAIN_FAMILY))
37  print_err("Chain family mismatches");
38  if (nftnl_chain_get_u32(a, NFTNL_CHAIN_POLICY) !=
39  nftnl_chain_get_u32(b, NFTNL_CHAIN_POLICY))
40  print_err("Chain policy mismatches");
41  if (nftnl_chain_get_u32(a, NFTNL_CHAIN_HOOKNUM) !=
42  nftnl_chain_get_u32(b, NFTNL_CHAIN_HOOKNUM))
43  print_err("Chain hooknum mismatches");
44  if (nftnl_chain_get_s32(a, NFTNL_CHAIN_PRIO) !=
45  nftnl_chain_get_s32(b, NFTNL_CHAIN_PRIO))
46  print_err("Chain Prio mismatches");
47  if (nftnl_chain_get_u32(a, NFTNL_CHAIN_USE) !=
48  nftnl_chain_get_u32(b, NFTNL_CHAIN_USE))
49  print_err("Chain use mismatches");
50  if (nftnl_chain_get_u64(a, NFTNL_CHAIN_PACKETS) !=
51  nftnl_chain_get_u64(b, NFTNL_CHAIN_PACKETS))
52  print_err("Chain packets mismatches");
53  if (nftnl_chain_get_u64(a, NFTNL_CHAIN_BYTES) !=
54  nftnl_chain_get_u64(b, NFTNL_CHAIN_BYTES))
55  print_err("Chain bytes mismatches");
56  if (nftnl_chain_get_u64(a, NFTNL_CHAIN_HANDLE) !=
57  nftnl_chain_get_u64(b, NFTNL_CHAIN_HANDLE))
58  print_err("Chain handle mismatches");
59  if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_TYPE),
60  nftnl_chain_get_str(b, NFTNL_CHAIN_TYPE)) != 0)
61  print_err("Chain type mismatches");
62  if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_DEV),
63  nftnl_chain_get_str(b, NFTNL_CHAIN_DEV)) != 0)
64  print_err("Chain device mismatches");
65 }
66 
67 int main(int argc, char *argv[])
68 {
69  struct nftnl_chain *a, *b;
70  char buf[4096];
71  struct nlmsghdr *nlh;
72 
73  a = nftnl_chain_alloc();
74  b = nftnl_chain_alloc();
75  if (a == NULL || b == NULL)
76  print_err("OOM");
77 
78  nftnl_chain_set_str(a, NFTNL_CHAIN_NAME, "test");
79  nftnl_chain_set_u32(a, NFTNL_CHAIN_FAMILY, AF_INET);
80  nftnl_chain_set_str(a, NFTNL_CHAIN_TABLE, "Table");
81  nftnl_chain_set_u32(a, NFTNL_CHAIN_POLICY,0x12345678);
82  nftnl_chain_set_u32(a, NFTNL_CHAIN_HOOKNUM, 0x34567812);
83  nftnl_chain_set_s32(a, NFTNL_CHAIN_PRIO, 0x56781234);
84  nftnl_chain_set_u32(a, NFTNL_CHAIN_USE, 0x78123456);
85  nftnl_chain_set_u64(a, NFTNL_CHAIN_PACKETS, 0x1234567812345678);
86  nftnl_chain_set_u64(a, NFTNL_CHAIN_BYTES, 0x7812345678123456);
87  nftnl_chain_set_u64(a, NFTNL_CHAIN_HANDLE, 0x5678123456781234);
88  nftnl_chain_set_str(a, NFTNL_CHAIN_TYPE, "Prueba");
89  nftnl_chain_set_str(a, NFTNL_CHAIN_DEV, "eth0");
90 
91  /* cmd extracted from include/linux/netfilter/nf_tables.h */
92  nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWCHAIN, AF_INET, 0, 1234);
93  nftnl_chain_nlmsg_build_payload(nlh, a);
94 
95  if (nftnl_chain_nlmsg_parse(nlh, b) < 0)
96  print_err("parsing problems");
97 
98  cmp_nftnl_chain(a, b);
99 
100  nftnl_chain_free(a);
101  nftnl_chain_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 
109 }