libnftnl  1.2.0
nft-expr_target-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 <netinet/in.h>
16 #include <netinet/ip.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <libmnl/libmnl.h>
19 #include <libnftnl/rule.h>
20 #include <libnftnl/expr.h>
21 
22 static int test_ok = 1;
23 
24 static void print_err(const char *msg)
25 {
26  test_ok = 0;
27  printf("\033[31mERROR:\e[0m %s\n", msg);
28 }
29 
30 static void print_err2(const char *msg, uint32_t a, uint32_t b)
31 {
32  test_ok = 0;
33  printf("\033[31mERROR:\e[0m %s size a: %d b: %d \n",msg, a, b);
34 }
35 
36 static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
37  struct nftnl_expr *rule_b)
38 {
39  uint32_t lena, lenb;
40 
41  if (strcmp(nftnl_expr_get_str(rule_a, NFTNL_EXPR_TG_NAME),
42  nftnl_expr_get_str(rule_b, NFTNL_EXPR_TG_NAME)) != 0)
43  print_err("Expr NFTNL_EXPR_TG_NAME mismatches");
44  if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_TG_REV) !=
45  nftnl_expr_get_u32(rule_b, NFTNL_EXPR_TG_REV))
46  print_err("Expr NFTNL_EXPR_TG_REV mismatches");
47  nftnl_expr_get(rule_a, NFTNL_EXPR_TG_INFO, &lena);
48  nftnl_expr_get(rule_b, NFTNL_EXPR_TG_INFO, &lenb);
49  if (lena != lenb)
50  print_err2("Expr NFTNL_EXPR_TG_INFO size mismatches", lena, lenb);
51 }
52 
53 int main(int argc, char *argv[])
54 {
55  struct nftnl_rule *a, *b;
56  struct nftnl_expr *ex;
57  struct nlmsghdr *nlh;
58  char buf[4096];
59  struct nftnl_expr_iter *iter_a, *iter_b;
60  struct nftnl_expr *rule_a, *rule_b;
61  char data[16] = "0123456789abcdef";
62 
63  a = nftnl_rule_alloc();
64  b = nftnl_rule_alloc();
65  if (a == NULL || b == NULL)
66  print_err("OOM");
67 
68  ex = nftnl_expr_alloc("target");
69  if (ex == NULL)
70  print_err("OOM");
71 
72  nftnl_expr_set(ex, NFTNL_EXPR_TG_NAME, "test", strlen("test"));
73  nftnl_expr_set_u32(ex, NFTNL_EXPR_TG_REV, 0x56781234);
74  nftnl_expr_set(ex, NFTNL_EXPR_TG_INFO, strdup(data), sizeof(data));
75  nftnl_rule_add_expr(a, ex);
76 
77  nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
78  nftnl_rule_nlmsg_build_payload(nlh, a);
79 
80  if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
81  print_err("parsing problems");
82 
83  iter_a = nftnl_expr_iter_create(a);
84  iter_b = nftnl_expr_iter_create(b);
85  if (iter_a == NULL || iter_b == NULL)
86  print_err("OOM");
87 
88  rule_a = nftnl_expr_iter_next(iter_a);
89  rule_b = nftnl_expr_iter_next(iter_b);
90  if (rule_a == NULL || rule_b == NULL)
91  print_err("OOM");
92 
93  cmp_nftnl_expr(rule_a, rule_b);
94 
95  if (nftnl_expr_iter_next(iter_a) != NULL ||
96  nftnl_expr_iter_next(iter_b) != NULL)
97  print_err("More 1 expr.");
98 
99  nftnl_expr_iter_destroy(iter_a);
100  nftnl_expr_iter_destroy(iter_b);
101  nftnl_rule_free(a);
102  nftnl_rule_free(b);
103 
104  if (!test_ok)
105  exit(EXIT_FAILURE);
106  printf("%s: \033[32mOK\e[0m\n", argv[0]);
107  return EXIT_SUCCESS;
108 }