libnftnl  1.0.5
nft-expr_masq-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 <linux/netfilter/nf_tables.h>
16 #include <libmnl/libmnl.h>
17 #include <libnftnl/rule.h>
18 #include <libnftnl/expr.h>
19 
20 static int test_ok = 1;
21 
22 static void print_err(const char *msg)
23 {
24  test_ok = 0;
25  printf("\033[31mERROR:\e[0m %s\n", msg);
26 }
27 
28 static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
29  struct nftnl_expr *rule_b)
30 {
31  if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_MASQ_FLAGS) !=
32  nftnl_expr_get_u32(rule_b, NFTNL_EXPR_MASQ_FLAGS))
33  print_err("Expr NFTNL_EXPR_MASQ_FLAGS mismatches");
34 }
35 
36 int main(int argc, char *argv[])
37 {
38  struct nftnl_rule *a, *b;
39  struct nftnl_expr *ex;
40  struct nlmsghdr *nlh;
41  char buf[4096];
42  struct nftnl_expr_iter *iter_a, *iter_b;
43  struct nftnl_expr *rule_a, *rule_b;
44 
45  a = nftnl_rule_alloc();
46  b = nftnl_rule_alloc();
47  if (a == NULL || b == NULL)
48  print_err("OOM");
49  ex = nftnl_expr_alloc("nat");
50  if (ex == NULL)
51  print_err("OOM");
52 
53  nftnl_expr_set_u32(ex, NFTNL_EXPR_MASQ_FLAGS, 0x1234568);
54 
55  nftnl_rule_add_expr(a, ex);
56 
57  nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
58  nftnl_rule_nlmsg_build_payload(nlh, a);
59 
60  if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
61  print_err("parsing problems");
62 
63  iter_a = nftnl_expr_iter_create(a);
64  iter_b = nftnl_expr_iter_create(b);
65  if (iter_a == NULL || iter_b == NULL)
66  print_err("OOM");
67 
68  rule_a = nftnl_expr_iter_next(iter_a);
69  rule_b = nftnl_expr_iter_next(iter_b);
70  if (rule_a == NULL || rule_b == NULL)
71  print_err("OOM");
72 
73  cmp_nftnl_expr(rule_a, rule_b);
74 
75  if (nftnl_expr_iter_next(iter_a) != NULL ||
76  nftnl_expr_iter_next(iter_b) != NULL)
77  print_err("More 1 expr.");
78 
79  nftnl_expr_iter_destroy(iter_a);
80  nftnl_expr_iter_destroy(iter_b);
81  nftnl_rule_free(a);
82  nftnl_rule_free(b);
83 
84  if (!test_ok)
85  exit(EXIT_FAILURE);
86 
87  printf("%s: \033[32mOK\e[0m\n", argv[0]);
88  return EXIT_SUCCESS;
89 }