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