libnftnl  1.0.2
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 <linux/netfilter/xt_iprange.h>
19 #include <linux/netfilter/xt_LOG.h>
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/rule.h>
22 #include <libnftnl/expr.h>
23 
24 static int test_ok = 1;
25 
26 static void print_err(const char *msg)
27 {
28  test_ok = 0;
29  printf("\033[31mERROR:\e[0m %s\n", msg);
30 }
31 
32 static void print_err2(const char *msg, uint32_t a, uint32_t b)
33 {
34  test_ok = 0;
35  printf("\033[31mERROR:\e[0m %s size a: %d b: %d \n",msg, a, b);
36 }
37 
38 static void cmp_nft_rule_expr(struct nft_rule_expr *rule_a,
39  struct nft_rule_expr *rule_b)
40 {
41  uint32_t lena, lenb;
42 
43  if (strcmp(nft_rule_expr_get_str(rule_a, NFT_EXPR_TG_NAME),
44  nft_rule_expr_get_str(rule_b, NFT_EXPR_TG_NAME)) != 0)
45  print_err("Expr NFT_EXPR_TG_NAME mismatches");
46  if (nft_rule_expr_get_u32(rule_a, NFT_EXPR_TG_REV) !=
47  nft_rule_expr_get_u32(rule_b, NFT_EXPR_TG_REV))
48  print_err("Expr NFT_EXPR_TG_REV mismatches");
49  nft_rule_expr_get(rule_a, NFT_EXPR_TG_INFO, &lena);
50  nft_rule_expr_get(rule_b, NFT_EXPR_TG_INFO, &lenb);
51  if (lena != lenb)
52  print_err2("Expr NFT_EXPR_TG_INFO size mismatches", lena, lenb);
53 }
54 
55 int main(int argc, char *argv[])
56 {
57  struct nft_rule *a, *b;
58  struct nft_rule_expr *ex;
59  struct nlmsghdr *nlh;
60  struct xt_log_info *info;
61  char buf[4096];
62  struct nft_rule_expr_iter *iter_a, *iter_b;
63  struct nft_rule_expr *rule_a, *rule_b;
64 
65  a = nft_rule_alloc();
66  b = nft_rule_alloc();
67  if (a == NULL || b == NULL)
68  print_err("OOM");
69 
70  ex = nft_rule_expr_alloc("target");
71  if (ex == NULL)
72  print_err("OOM");
73  nft_rule_expr_set(ex, NFT_EXPR_TG_NAME, "test", strlen("test"));
74  nft_rule_expr_set_u32(ex, NFT_EXPR_TG_REV, 0x12345678);
75 
76  info = calloc(1, sizeof(struct xt_log_info));
77  if (info == NULL)
78  print_err("OOM");
79  sprintf(info->prefix, "test: ");
80  info->prefix[sizeof(info->prefix)-1] = '\0';
81  info->logflags = 0x0f;
82  info->level = 5;
83  nft_rule_expr_set(ex, NFT_EXPR_TG_INFO, info, sizeof(*info));
84 
85  nft_rule_add_expr(a, ex);
86 
87  nlh = nft_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
88  nft_rule_nlmsg_build_payload(nlh, a);
89 
90  if (nft_rule_nlmsg_parse(nlh, b) < 0)
91  print_err("parsing problems");
92 
93  iter_a = nft_rule_expr_iter_create(a);
94  iter_b = nft_rule_expr_iter_create(b);
95  if (iter_a == NULL || iter_b == NULL)
96  print_err("OOM");
97 
98  rule_a = nft_rule_expr_iter_next(iter_a);
99  rule_b = nft_rule_expr_iter_next(iter_b);
100  if (rule_a == NULL || rule_b == NULL)
101  print_err("OOM");
102 
103  cmp_nft_rule_expr(rule_a, rule_b);
104 
105  if (nft_rule_expr_iter_next(iter_a) != NULL ||
106  nft_rule_expr_iter_next(iter_b) != NULL)
107  print_err("More 1 expr.");
108 
109  nft_rule_expr_iter_destroy(iter_a);
110  nft_rule_expr_iter_destroy(iter_b);
111  nft_rule_free(a);
112  nft_rule_free(b);
113 
114  if (!test_ok)
115  exit(EXIT_FAILURE);
116  printf("%s: \033[32mOK\e[0m\n", argv[0]);
117  return EXIT_SUCCESS;
118 }
Definition: rule.c:34