Debian Codegraph @

src/test/test-nft-set.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-netlink.h"
6
7 #include "firewall-util.h"
8 #include "in-addr-util.h"
9 #include "netlink-internal.h"
10 #include "parse-util.h"
11 #include "string-util.h"
12 #include "tests.h"
13
14 int main(int argc, char **argv) {
15 int r;
16
17 assert_se(argc == 7);
18
19 test_setup_logging(LOG_DEBUG);
20
21 if (getuid() != 0)
22 return log_tests_skipped("not root");
23
24 int nfproto;
25 nfproto = nfproto_from_string(argv[2]);
26 assert_se(nfproto_is_valid(nfproto));
27
28 const char *table = argv[3], *set = argv[4];
29
30 _cleanup_(sd_netlink_unrefp) sd_netlink *nfnl = NULL;
31 ASSERT_OK(sd_nfnl_socket_open(&nfnl));
32
33 bool add;
34 if (streq(argv[1], "add"))
35 add = true;
36 else
37 add = false;
38
39 if (streq(argv[5], "uint32")) {
40 uint32_t element;
41
42 r = safe_atou32(argv[6], &element);
43 assert_se(r == 0);
44
45 r = nft_set_element_modify_any(nfnl, add, nfproto, table, set, &element, sizeof(element));
46 assert_se(r == 0);
47 } else if (streq(argv[5], "uint64")) {
48 uint64_t element;
49
50 r = safe_atou64(argv[6], &element);
51 assert_se(r == 0);
52
53 r = nft_set_element_modify_any(nfnl, add, nfproto, table, set, &element, sizeof(element));
54 assert_se(r == 0);
55 } else if (streq(argv[5], "in_addr")) {
56 union in_addr_union addr;
57 int af;
58
59 r = in_addr_from_string_auto(argv[6], &af, &addr);
60 assert_se(r == 0);
61
62 r = nft_set_element_modify_ip(nfnl, add, nfproto, af, table, set, &addr);
63 assert_se(r == 0);
64 } else if (streq(argv[5], "network")) {
65 union in_addr_union addr;
66 int af;
67 unsigned char prefixlen;
68
69 r = in_addr_prefix_from_string_auto(argv[6], &af, &addr, &prefixlen);
70 assert_se(r == 0);
71
72 r = nft_set_element_modify_iprange(nfnl, add, nfproto, af, table, set, &addr, prefixlen);
73 assert_se(r == 0);
74 }
75
76 return 0;
77 }