libnftnl  1.0.2
nft-expr_queue-test.c
1 /*
2  * (C) 2013 by Eric Leblond <eric@regit.org>
3  *
4  * Based on test framework by Ana Rey Botello <anarey@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  */
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 #include <netinet/in.h>
18 #include <netinet/ip.h>
19 #include <linux/netfilter/nf_tables.h>
20 #include <linux/netfilter/xt_iprange.h>
21 #include <libmnl/libmnl.h>
22 #include <libnftnl/rule.h>
23 #include <libnftnl/expr.h>
24 
25 static int test_ok = 1;
26 
27 static void print_err(const char *msg)
28 {
29  test_ok = 0;
30  printf("\033[31mERROR:\e[0m %s\n", msg);
31 }
32 
33 static void cmp_nft_rule_expr(struct nft_rule_expr *rule_a,
34  struct nft_rule_expr *rule_b)
35 {
36  if (nft_rule_expr_get_u16(rule_a, NFT_EXPR_QUEUE_NUM) !=
37  nft_rule_expr_get_u16(rule_b, NFT_EXPR_QUEUE_NUM))
38  print_err("Expr NFT_EXPR_QUEUE_NUM mismatches");
39  if (nft_rule_expr_get_u16(rule_a, NFT_EXPR_QUEUE_TOTAL) !=
40  nft_rule_expr_get_u16(rule_b, NFT_EXPR_QUEUE_TOTAL))
41  print_err("Expr NFT_EXPR_QUEUE_TOTAL mismatches");
42 }
43 
44 int main(int argc, char *argv[])
45 {
46  struct nft_rule *a, *b;
47  struct nft_rule_expr *ex;
48  struct nlmsghdr *nlh;
49  char buf[4096];
50  struct nft_rule_expr_iter *iter_a, *iter_b;
51  struct nft_rule_expr *rule_a, *rule_b;
52 
53  a = nft_rule_alloc();
54  b = nft_rule_alloc();
55  if (a == NULL || b == NULL)
56  print_err("OOM");
57  ex = nft_rule_expr_alloc("queue");
58  if (ex == NULL)
59  print_err("OOM");
60 
61  nft_rule_expr_set_u16(ex, NFT_EXPR_QUEUE_NUM, 0x123);
62  nft_rule_expr_set_u16(ex, NFT_EXPR_QUEUE_TOTAL, 0x2);
63  nft_rule_expr_set_u16(ex, NFT_EXPR_QUEUE_FLAGS, 0x2);
64 
65  nft_rule_add_expr(a, ex);
66 
67  nlh = nft_rule_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
68  nft_rule_nlmsg_build_payload(nlh, a);
69 
70  if (nft_rule_nlmsg_parse(nlh, b) < 0)
71  print_err("parsing problems");
72 
73  iter_a = nft_rule_expr_iter_create(a);
74  iter_b = nft_rule_expr_iter_create(b);
75  if (iter_a == NULL || iter_b == NULL)
76  print_err("OOM");
77 
78  rule_a = nft_rule_expr_iter_next(iter_a);
79  rule_b = nft_rule_expr_iter_next(iter_b);
80  if (rule_a == NULL || rule_b == NULL)
81  print_err("OOM");
82 
83  cmp_nft_rule_expr(rule_a, rule_b);
84 
85  if (nft_rule_expr_iter_next(iter_a) != NULL ||
86  nft_rule_expr_iter_next(iter_b) != NULL)
87  print_err("More 1 expr.");
88 
89  nft_rule_expr_iter_destroy(iter_a);
90  nft_rule_expr_iter_destroy(iter_b);
91  nft_rule_free(a);
92  nft_rule_free(b);
93 
94  if (!test_ok)
95  exit(EXIT_FAILURE);
96 
97  printf("%s: \033[32mOK\e[0m\n", argv[0]);
98  return EXIT_SUCCESS;
99 }
Definition: rule.c:34