xref: /aosp_15_r20/external/libnl/src/idiag-socket-details.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2013 Sassano Systems LLC <[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/idiag/idiagnl.h>
12 #include <netlink/idiag/msg.h>
13 
print_usage(void)14 static void print_usage(void)
15 {
16 	printf(
17 "Usage: idiag-socket-details [OPTION]\n"
18 "\n"
19 "Options\n"
20 "     --summary		    Show socket detail summary.\n"
21 "     --details             Show socket details on multiple lines.\n"
22 "     --stats               Show full socket statistics.\n"
23 " -h, --help                Show this help.\n"
24 " -v, --version             Show versioning information.\n"
25 	);
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 *idiag_cache;
33 	struct nl_dump_params params = {
34 		.dp_type = NL_DUMP_LINE,
35 		.dp_nl_cb = NULL,
36 		.dp_fd = stdout,
37 	};
38 	int err = 0;
39 
40 	sock = nl_cli_alloc_socket();
41 	nl_cli_connect(sock, NETLINK_INET_DIAG);
42 	for (;;) {
43 		int c, optidx = 0;
44 		enum {
45 			ARG_SUMMARY = 257,
46 			ARG_DETAILS = 258,
47 			ARG_STATS = 259,
48 			ARG_FAMILY,
49 		};
50 		static struct option long_opts[] = {
51 			{ "details", 0, 0, ARG_DETAILS },
52 			{ "summary", 0, 0, ARG_SUMMARY },
53 			{ "stats", 0, 0, ARG_STATS},
54 			{ "help", 0, 0, 'h' },
55 			{ "version", 0, 0, 'v' },
56 			{ 0, 0, 0, 0 }
57 		};
58 
59 		c = getopt_long(argc, argv, "hv", long_opts, &optidx);
60 		if (c == -1)
61 			break;
62 
63 		switch (c) {
64 		case '?': exit(NLE_INVAL);
65 		case ARG_SUMMARY: params.dp_type = NL_DUMP_LINE; break;
66 		case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
67 		case ARG_STATS:   params.dp_type = NL_DUMP_STATS; break;
68 		case 'h': print_usage(); break;
69 		case 'v': nl_cli_print_version(); break;
70 		}
71 	}
72 
73 	if ((err = idiagnl_msg_alloc_cache(sock, AF_INET, IDIAGNL_SS_ALL,
74 					&idiag_cache))) {
75 		nl_cli_fatal(err, "Unable to allocate idiag msg cache: %s",
76 				nl_geterror(err));
77 	}
78 
79 	nl_cache_mngt_provide(idiag_cache);
80 
81 	nl_cache_dump_filter(idiag_cache, &params, NULL);
82 
83 	nl_cache_mngt_unprovide(idiag_cache);
84 	nl_cache_free(idiag_cache);
85 	nl_socket_free(sock);
86 
87 	return 0;
88 }
89