libnftnl  1.1.5
nft-expr_cmp-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 cmp_nftnl_expr(struct nftnl_expr *rule_a,
31  struct nftnl_expr *rule_b)
32 {
33  uint32_t data_lena, data_lenb;
34 
35  nftnl_expr_get(rule_a, NFTNL_EXPR_CMP_DATA, &data_lena);
36  nftnl_expr_get(rule_b, NFTNL_EXPR_CMP_DATA, &data_lenb);
37  if (data_lena != data_lenb)
38  print_err("Size of NFTNL_EXPR_CMP_DATA mismatches");
39  if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_CMP_SREG) !=
40  nftnl_expr_get_u32(rule_b, NFTNL_EXPR_CMP_SREG))
41  print_err("Expr NFTNL_EXPR_CMP_SREG mismatches");
42  if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_CMP_OP) !=
43  nftnl_expr_get_u32(rule_b, NFTNL_EXPR_CMP_OP))
44  print_err("Expr NFTNL_EXPR_CMP_OP mismatches");
45 }
46 
47 int main(int argc, char *argv[])
48 {
49  struct nftnl_rule *a, *b;
50  struct nftnl_expr *ex;
51  struct nlmsghdr *nlh;
52  char buf[4096];
53  struct nftnl_expr_iter *iter_a, *iter_b;
54  struct nftnl_expr *rule_a, *rule_b;
55  uint32_t data_len = 0x01010101;
56 
57  a = nftnl_rule_alloc();
58  b = nftnl_rule_alloc();
59  if (a == NULL || b == NULL)
60  print_err("OOM");
61  ex = nftnl_expr_alloc("cmp");
62  if (ex == NULL)
63  print_err("OOM");
64 
65  nftnl_expr_set(ex, NFTNL_EXPR_CMP_DATA, &data_len, sizeof(data_len));
66  nftnl_expr_set_u32(ex, NFTNL_EXPR_CMP_SREG, 0x12345678);
67  nftnl_expr_set_u32(ex, NFTNL_EXPR_CMP_OP, 0x78123456);
68 
69  nftnl_rule_add_expr(a, ex);
70 
71  nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
72  nftnl_rule_nlmsg_build_payload(nlh, a);
73 
74  if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
75  print_err("parsing problems");
76 
77  iter_a = nftnl_expr_iter_create(a);
78  iter_b = nftnl_expr_iter_create(b);
79  if (iter_a == NULL || iter_b == NULL)
80  print_err("OOM");
81  rule_a = nftnl_expr_iter_next(iter_a);
82  rule_b = nftnl_expr_iter_next(iter_b);
83  if (rule_a == NULL || rule_b == NULL)
84  print_err("OOM");
85 
86  cmp_nftnl_expr(rule_a, rule_b);
87 
88  if (nftnl_expr_iter_next(iter_a) != NULL ||
89  nftnl_expr_iter_next(iter_b) != NULL)
90  print_err("More 1 expr.");
91 
92  nftnl_expr_iter_destroy(iter_a);
93  nftnl_expr_iter_destroy(iter_b);
94  nftnl_rule_free(a);
95  nftnl_rule_free(b);
96 
97  if (!test_ok)
98  exit(EXIT_FAILURE);
99 
100  printf("%s: \033[32mOK\e[0m\n", argv[0]);
101  return EXIT_SUCCESS;
102 }