1 #include "../src/utils.h"
2 #include <signal.h>
3 
4 static int quit = 0;
5 
change_cb(struct nl_cache * cache,struct nl_object * obj,int action)6 static void change_cb(struct nl_cache *cache, struct nl_object *obj,
7 		      int action)
8 {
9 	struct nl_dump_params dp = {
10 		.dp_type = NL_DUMP_LINE,
11 		.dp_fd = stdout,
12 	};
13 
14 	if (action == NL_ACT_NEW)
15 		printf("NEW ");
16 	else if (action == NL_ACT_DEL)
17 		printf("DEL ");
18 	else if (action == NL_ACT_CHANGE)
19 		printf("CHANGE ");
20 
21 	nl_object_dump(obj, &dp);
22 }
23 
sigint(int arg)24 static void sigint(int arg)
25 {
26 	quit = 1;
27 }
28 
main(int argc,char * argv[])29 int main(int argc, char *argv[])
30 {
31 	struct nl_cache_mngr *mngr;
32 	struct nl_cache *lc, *nc, *ac, *rc;
33 	struct nl_sock *sock;
34 	int err;
35 
36 	signal(SIGINT, sigint);
37 
38 	sock = nlt_alloc_socket();
39 	err = nl_cache_mngr_alloc(sock, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr);
40 	if (err < 0)
41 		fatal(err, "Unable to allocate cache manager: %s",
42 		      nl_geterror(err));
43 
44 	if ((err = nl_cache_mngr_add(mngr, "route/link", &change_cb, &lc)) < 0)
45 		fatal(err, "Unable to add cache route/link: %s",
46 		      nl_geterror(err));
47 
48 	if ((err = nl_cache_mngr_add(mngr, "route/neigh", &change_cb, &nc)) < 0)
49 		fatal(err, "Unable to add cache route/neigh: %s",
50 		      nl_geterror(err));
51 
52 	if ((err = nl_cache_mngr_add(mngr, "route/addr", &change_cb, &ac)) < 0)
53 		fatal(err, "Unable to add cache route/addr: %s",
54 		      nl_geterror(err));
55 
56 	if ((err = nl_cache_mngr_add(mngr, "route/route", &change_cb, &rc)) < 0)
57 		fatal(err, "Unable to add cache route/route: %s",
58 		      nl_geterror(err));
59 
60 	while (!quit) {
61 		int err = nl_cache_mngr_poll(mngr, 5000);
62 		if (err < 0 && err != -NLE_INTR)
63 			fatal(err, "Polling failed: %s", nl_geterror(err));
64 
65 	}
66 
67 	nl_cache_mngr_free(mngr);
68 	nl_socket_free(sock);
69 
70 	return 0;
71 }
72