libnftnl 1.2.6
nft-expr_objref-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
22static int test_ok = 1;
23
24static void print_err(const char *msg)
25{
26 test_ok = 0;
27 printf("\033[31mERROR:\e[0m %s\n", msg);
28}
29
30static void cmp_nftnl_expr(struct nftnl_expr *rule_a,
31 struct nftnl_expr *rule_b)
32{
33 if (nftnl_expr_get_u32(rule_a, NFTNL_EXPR_OBJREF_IMM_TYPE) !=
34 nftnl_expr_get_u32(rule_b, NFTNL_EXPR_OBJREF_IMM_TYPE))
35 print_err("Expr NFTNL_EXPR_OBJREF_IMM_TYPE mismatches");
36 if (strcmp(nftnl_expr_get_str(rule_a, NFTNL_EXPR_OBJREF_IMM_NAME),
37 nftnl_expr_get_str(rule_b, NFTNL_EXPR_OBJREF_IMM_NAME)))
38 print_err("Expr NFTNL_EXPR_OBJREF_IMM_NAME mismatches");
39}
40
41int main(int argc, char *argv[])
42{
43 struct nftnl_rule *a, *b;
44 struct nftnl_expr *ex;
45 struct nlmsghdr *nlh;
46 char buf[4096];
47 struct nftnl_expr_iter *iter_a, *iter_b;
48 struct nftnl_expr *rule_a, *rule_b;
49 const char *name = "test_set_01243";
50
51 a = nftnl_rule_alloc();
52 b = nftnl_rule_alloc();
53 if (a == NULL || b == NULL)
54 print_err("OOM");
55 ex = nftnl_expr_alloc("lookup");
56 if (ex == NULL)
57 print_err("OOM");
58
59 nftnl_expr_set_u32(ex, NFTNL_EXPR_OBJREF_IMM_TYPE, 0x78123456);
60 nftnl_expr_set_str(ex, NFTNL_EXPR_OBJREF_IMM_NAME, name);
61
62 nftnl_rule_add_expr(a, ex);
63
64 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWRULE, AF_INET, 0, 1234);
65 nftnl_rule_nlmsg_build_payload(nlh, a);
66
67 if (nftnl_rule_nlmsg_parse(nlh, b) < 0)
68 print_err("parsing problems");
69
70 iter_a = nftnl_expr_iter_create(a);
71 iter_b = nftnl_expr_iter_create(b);
72 if (iter_a == NULL || iter_b == NULL)
73 print_err("OOM");
74 rule_a = nftnl_expr_iter_next(iter_a);
75 rule_b = nftnl_expr_iter_next(iter_b);
76 if (rule_a == NULL || rule_b == NULL)
77 print_err("OOM");
78
79 cmp_nftnl_expr(rule_a, rule_b);
80
81 if (nftnl_expr_iter_next(iter_a) != NULL ||
82 nftnl_expr_iter_next(iter_b) != NULL)
83 print_err("More 1 expr.");
84
85 nftnl_expr_iter_destroy(iter_a);
86 nftnl_expr_iter_destroy(iter_b);
87 nftnl_rule_free(a);
88 nftnl_rule_free(b);
89
90 if (!test_ok)
91 exit(EXIT_FAILURE);
92
93 printf("%s: \033[32mOK\e[0m\n", argv[0]);
94
95 return EXIT_SUCCESS;
96}