• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/idiag-socket-details.c     List socket details
4  *
5  *	This library is free software; you can redistribute it and/or
6  *	modify it under the terms of the GNU General Public License as
7  *	published by the Free Software Foundation version 2 of the License.
8  *
9  * Copyright (c) 2013 Sassano Systems LLC <joe@sassanosystems.com>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/idiag/idiagnl.h>
14 #include <netlink/idiag/msg.h>
15 #include <linux/netlink.h>
16 
print_usage(void)17 static void print_usage(void)
18 {
19 	printf(
20 "Usage: idiag-socket-details [OPTION]\n"
21 "\n"
22 "Options\n"
23 "     --summary		    Show socket detail summary.\n"
24 "     --details             Show socket details on multiple lines.\n"
25 "     --stats               Show full socket statistics.\n"
26 " -h, --help                Show this help.\n"
27 " -v, --version             Show versioning information.\n"
28 	);
29 	exit(0);
30 }
31 
main(int argc,char * argv[])32 int main(int argc, char *argv[])
33 {
34 	struct nl_sock *sock;
35 	struct nl_cache *idiag_cache;
36 	struct nl_dump_params params = {
37 		.dp_type = NL_DUMP_LINE,
38 		.dp_nl_cb = NULL,
39 		.dp_fd = stdout,
40 	};
41 	int err = 0;
42 
43 	sock = nl_cli_alloc_socket();
44 	nl_cli_connect(sock, NETLINK_INET_DIAG);
45 	for (;;) {
46 		int c, optidx = 0;
47 		enum {
48 			ARG_SUMMARY = 257,
49 			ARG_DETAILS = 258,
50 			ARG_STATS = 259,
51 			ARG_FAMILY,
52 		};
53 		static struct option long_opts[] = {
54 			{ "details", 0, 0, ARG_DETAILS },
55 			{ "summary", 0, 0, ARG_SUMMARY },
56 			{ "stats", 0, 0, ARG_STATS},
57 			{ "help", 0, 0, 'h' },
58 			{ "version", 0, 0, 'v' },
59 			{ 0, 0, 0, 0 }
60 		};
61 
62 		c = getopt_long(argc, argv, "hv", long_opts, &optidx);
63 		if (c == -1)
64 			break;
65 
66 		switch (c) {
67 		case '?': exit(NLE_INVAL);
68 		case ARG_SUMMARY: params.dp_type = NL_DUMP_LINE; break;
69 		case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
70 		case ARG_STATS:   params.dp_type = NL_DUMP_STATS; break;
71 		case 'h': print_usage(); break;
72 		case 'v': nl_cli_print_version(); break;
73 		}
74 	}
75 
76 	if ((err = idiagnl_msg_alloc_cache(sock, AF_INET, IDIAGNL_SS_ALL,
77 					&idiag_cache))) {
78 		nl_cli_fatal(err, "Unable to allocate idiag msg cache: %s",
79 				nl_geterror(err));
80 	}
81 
82 	nl_cache_mngt_provide(idiag_cache);
83 
84 	nl_cache_dump_filter(idiag_cache, &params, NULL);
85 
86 	nl_cache_mngt_unprovide(idiag_cache);
87 	nl_cache_free(idiag_cache);
88 	nl_socket_free(sock);
89 
90 	return 0;
91 }
92