libnftnl 1.2.6
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
25static int test_ok = 1;
26
27static void print_err(const char *msg)
28{
29 test_ok = 0;
30 printf("\033[31mERROR:\e[0m %s\n", msg);
31}
32
33static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
34 struct nftnl_expr *rule_b)
35{
36 if (nftnl_expr_get_u16(rule_a, NFTNL_EXPR_QUEUE_NUM) !=
37 nftnl_expr_get_u16(rule_b, NFTNL_EXPR_QUEUE_NUM))
38 print_err("Expr NFTNL_EXPR_QUEUE_NUM mismatches");
39 if (nftnl_expr_get_u16(rule_a, NFTNL_EXPR_QUEUE_TOTAL) !=
40 nftnl_expr_get_u16(rule_b, NFTNL_EXPR_QUEUE_TOTAL))
41 print_err("Expr NFTNL_EXPR_QUEUE_TOTAL mismatches");
42 if (nftnl_expr_get_u16(rule_a, NFTNL_EXPR_QUEUE_FLAGS) !=
43 nftnl_expr_get_u16(rule_b, NFTNL_EXPR_QUEUE_FLAGS))
44 print_err("Expr NFTNL_EXPR_QUEUE_FLAGS mismatches");
45}
46
47int 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
56 a = nftnl_rule_alloc();
57 b = nftnl_rule_alloc();
58 if (a == NULL || b == NULL)
59 print_err("OOM");
60 ex = nftnl_expr_alloc("queue");
61 if (ex == NULL)
62 print_err("OOM");
63
64 nftnl_expr_set_u16(ex, NFTNL_EXPR_QUEUE_NUM, 0x01010);
65 nftnl_expr_set_u16(ex, NFTNL_EXPR_QUEUE_TOTAL, 0x1234);
66 nftnl_expr_set_u16(ex, NFTNL_EXPR_QUEUE_FLAGS, 0x4321);
67
68 nftnl_rule_add_expr(a, ex);
69
70 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
71 nftnl_rule_nlmsg_build_payload(nlh, a);
72
73 if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
74 print_err("parsing problems");
75
76 iter_a = nftnl_expr_iter_create(a);
77 iter_b = nftnl_expr_iter_create(b);
78 if (iter_a == NULL || iter_b == NULL)
79 print_err("OOM");
80
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}