libnftnl 1.2.6
nft-object-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#include <netinet/in.h>
15
16#include <linux/netfilter/nf_tables.h>
17#include <libnftnl/object.h>
18
19static int test_ok = 1;
20
21static void print_err(const char *msg)
22{
23 test_ok = 0;
24 printf("\033[31mERROR:\e[0m %s\n", msg);
25}
26
27static void cmp_nftnl_obj(struct nftnl_obj *a, struct nftnl_obj *b)
28{
29 if (strcmp(nftnl_obj_get_str(a, NFTNL_OBJ_TABLE),
30 nftnl_obj_get_str(b, NFTNL_OBJ_TABLE)) != 0)
31 print_err("table name mismatches");
32 if (strcmp(nftnl_obj_get_str(a, NFTNL_OBJ_NAME),
33 nftnl_obj_get_str(b, NFTNL_OBJ_NAME)) != 0)
34 print_err("name mismatches");
35 if (nftnl_obj_get_u32(a, NFTNL_OBJ_FAMILY) !=
36 nftnl_obj_get_u32(b, NFTNL_OBJ_FAMILY))
37 print_err("family mismatches");
38 if (nftnl_obj_get_u32(a, NFTNL_OBJ_TYPE) !=
39 nftnl_obj_get_u32(b, NFTNL_OBJ_TYPE))
40 print_err("type mismatches");
41}
42
43int main(int argc, char *argv[])
44{
45 char buf[4096];
46 struct nlmsghdr *nlh;
47 struct nftnl_obj *a;
48 struct nftnl_obj *b;
49
50 a = nftnl_obj_alloc();
51 b = nftnl_obj_alloc();
52 if (a == NULL || b == NULL)
53 print_err("OOM");
54
55 nftnl_obj_set_str(a, NFTNL_OBJ_TABLE, "test");
56 nftnl_obj_set_str(a, NFTNL_OBJ_NAME, "test");
57 nftnl_obj_set_u32(a, NFTNL_OBJ_FAMILY, AF_INET);
58 nftnl_obj_set_u32(a, NFTNL_OBJ_USE, 1);
59 nftnl_obj_set_u64(a, NFTNL_OBJ_CTR_BYTES, 0x12345678abcd);
60 nftnl_obj_set_u64(a, NFTNL_OBJ_CTR_PKTS, 0xcd12345678ab);
61
62 /* cmd extracted from include/linux/netfilter/nf_tables.h */
63 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWOBJ, AF_INET, 0, 1234);
64 nftnl_obj_nlmsg_build_payload(nlh, a);
65
66 if (nftnl_obj_nlmsg_parse(nlh, b) < 0)
67 print_err("parsing problems");
68
69 cmp_nftnl_obj(a, b);
70
71 nftnl_obj_free(a);
72 nftnl_obj_free(b);
73 if (!test_ok)
74 exit(EXIT_FAILURE);
75
76 printf("%s: \033[32mOK\e[0m\n", argv[0]);
77 return EXIT_SUCCESS;
78}