xref: /aosp_15_r20/external/libnl/src/nl-neigh-list.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2009 Thomas Graf <[email protected]>
4  */
5 
6 #include "nl-default.h"
7 
8 #include <linux/netlink.h>
9 
10 #include <netlink/cli/utils.h>
11 #include <netlink/cli/neigh.h>
12 #include <netlink/cli/link.h>
13 
print_usage(void)14 static void print_usage(void)
15 {
16 	printf(
17 	"Usage: nl-neigh-list [OPTION]... [NEIGHBOUR]\n"
18 	"\n"
19 	"Options\n"
20 	" -f, --format=TYPE     Output format { brief | details | stats }\n"
21 	" -h, --help            Show this help\n"
22 	" -v, --version         Show versioning information\n"
23 	"\n"
24 	"Neighbour Options\n"
25 	" -a, --addr=ADDR       Destination address of neighbour\n"
26 	" -l, --lladdr=ADDR     Link layer address of neighbour\n"
27 	" -d, --dev=DEV         Device the neighbour is connected to\n"
28 	"     --family=FAMILY   Destination address family\n"
29 	"     --state=STATE     Neighbour state, (default = permanent)\n"
30 	);
31 	exit(0);
32 }
33 
main(int argc,char * argv[])34 int main(int argc, char *argv[])
35 {
36 	struct nl_sock *sock;
37 	struct rtnl_neigh *neigh;
38 	struct nl_cache *link_cache, *neigh_cache;
39 	struct nl_dump_params params = {
40 		.dp_type = NL_DUMP_LINE,
41 		.dp_fd = stdout,
42 	};
43 
44 	sock = nl_cli_alloc_socket();
45 	nl_cli_connect(sock, NETLINK_ROUTE);
46 	link_cache = nl_cli_link_alloc_cache_flags(sock, NL_CACHE_AF_ITER);
47 	neigh_cache = nl_cli_neigh_alloc_cache(sock);
48 	neigh = nl_cli_neigh_alloc();
49 
50 	for (;;) {
51 		int c, optidx = 0;
52 		enum {
53 			ARG_FAMILY = 257,
54 			ARG_STATE = 258,
55 		};
56 		static struct option long_opts[] = {
57 			{ "format", 1, 0, 'f' },
58 			{ "help", 0, 0, 'h' },
59 			{ "version", 0, 0, 'v' },
60 			{ "addr", 1, 0, 'a' },
61 			{ "lladdr", 1, 0, 'l' },
62 			{ "dev", 1, 0, 'd' },
63 			{ "family", 1, 0, ARG_FAMILY },
64 			{ "state", 1, 0, ARG_STATE },
65 			{ 0, 0, 0, 0 }
66 		};
67 
68 		c = getopt_long(argc, argv, "f:hva:l:d:", long_opts, &optidx);
69 		if (c == -1)
70 			break;
71 
72 		switch (c) {
73 		case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
74 		case 'h': print_usage(); break;
75 		case 'v': nl_cli_print_version(); break;
76 		case 'a': nl_cli_neigh_parse_dst(neigh, optarg); break;
77 		case 'l': nl_cli_neigh_parse_lladdr(neigh, optarg); break;
78 		case 'd': nl_cli_neigh_parse_dev(neigh, link_cache, optarg); break;
79 		case ARG_FAMILY: nl_cli_neigh_parse_family(neigh, optarg); break;
80 		case ARG_STATE: nl_cli_neigh_parse_state(neigh, optarg); break;
81 		}
82 	}
83 
84 	nl_cache_dump_filter(neigh_cache, &params, OBJ_CAST(neigh));
85 
86 	rtnl_neigh_put(neigh);
87 	nl_cache_put(neigh_cache);
88 	nl_cache_put(link_cache);
89 	nl_socket_free(sock);
90 
91 	return 0;
92 }
93