xref: /aosp_15_r20/external/libnl/tests/test-nf-cache-mngr.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 
3 #include "nl-default.h"
4 
5 #include <linux/netlink.h>
6 
7 #include <netlink/cli/utils.h>
8 
change_cb(struct nl_cache * cache,struct nl_object * obj,int action,void * data)9 static void change_cb(struct nl_cache *cache, struct nl_object *obj,
10 		      int action, void *data)
11 {
12 	struct nfnl_ct *ct = (struct nfnl_ct *) obj;
13 	static struct nl_addr *hack = NULL;
14 
15 	if (!hack)
16 		nl_addr_parse("194.88.212.233", AF_INET, &hack);
17 
18 	if (!nl_addr_cmp(hack, nfnl_ct_get_src(ct, 1)) ||
19 	    !nl_addr_cmp(hack, nfnl_ct_get_dst(ct, 1))) {
20 		struct nl_dump_params dp = {
21 			.dp_type = NL_DUMP_LINE,
22 			.dp_fd = stdout,
23 		};
24 
25 		printf("UPDATE ");
26 		nl_object_dump(obj, &dp);
27 	}
28 }
29 
main(int argc,char * argv[])30 int main(int argc, char *argv[])
31 {
32 	struct nl_cache_mngr *mngr;
33 	struct nl_sock *sock;
34 	struct nl_cache *ct;
35 	int err;
36 
37 	sock = nl_cli_alloc_socket();
38 
39 	err = nl_cache_mngr_alloc(sock, NETLINK_NETFILTER, NL_AUTO_PROVIDE, &mngr);
40 	if (err < 0) {
41 		nl_perror(err, "nl_cache_mngr_alloc");
42 		return -1;
43 	}
44 
45 	err = nl_cache_mngr_add(mngr, "netfilter/ct", &change_cb, NULL, &ct);
46 	if (err < 0) {
47 		nl_perror(err, "nl_cache_mngr_add(netfilter/ct)");
48 		return -1;
49 	}
50 
51 	for (;;) {
52 		int err = nl_cache_mngr_poll(mngr, 5000);
53 		if (err < 0) {
54 			nl_perror(err, "nl_cache_mngr_poll()");
55 			return -1;
56 		}
57 
58 	}
59 
60 	nl_cache_mngr_free(mngr);
61 
62 	return 0;
63 }
64