xref: /aosp_15_r20/external/libnl/src/nl-nh-list.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2022 Stanislav Zaikin <[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/nh.h>
12 #include <netlink/route/nh.h>
13 
print_usage(void)14 static void print_usage(void)
15 {
16 	printf("Usage: nl-nh-list [OPTIONS]... \n"
17 	       "\n"
18 	       "OPTIONS\n"
19 	       "     --details             Show detailed information of each link\n"
20 	       " -h, --help                Show this help text.\n"
21 	       " -v, --version             Show versioning information.\n"
22 	       "\n"
23 	       " -n, --name=NAME	    Name of link\n"
24 	       " -i, --index               Interface index (unique identifier)\n"
25 	       "     --family=NAME         Link address family\n");
26 	exit(0);
27 }
28 
main(int argc,char * argv[])29 int main(int argc, char *argv[])
30 {
31 	struct nl_sock *sock;
32 	struct nl_cache *link_cache;
33 	struct nl_dump_params params = {
34 		.dp_type = NL_DUMP_LINE,
35 		.dp_fd = stdout,
36 	};
37 
38 	sock = nl_cli_alloc_socket();
39 	nl_cli_connect(sock, NETLINK_ROUTE);
40 
41 	for (;;) {
42 		int c, optidx = 0;
43 		enum {
44 			ARG_FAMILY = 257,
45 			ARG_DETAILS,
46 		};
47 		static struct option long_opts[] = { { "details", 0, 0,
48 						       ARG_DETAILS },
49 						     { "help", 0, 0, 'h' },
50 						     { "version", 0, 0, 'v' },
51 						     { "name", 1, 0, 'n' },
52 						     { 0, 0, 0, 0 } };
53 
54 		c = getopt_long(argc, argv, "hvn:i:", long_opts, &optidx);
55 		if (c == -1)
56 			break;
57 
58 		switch (c) {
59 		case ARG_DETAILS:
60 			params.dp_type = NL_DUMP_DETAILS;
61 			break;
62 		case 'h':
63 			print_usage();
64 			break;
65 		case 'v':
66 			nl_cli_print_version();
67 			break;
68 		}
69 	}
70 
71 	link_cache = nl_cli_nh_alloc_cache(sock);
72 
73 	nl_cache_dump(link_cache, &params);
74 
75 	return 0;
76 }
77