libnl 3.5.0

idiag-socket-details.c
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
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
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 }
NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:22
nl_cache_mngt_provide
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
Definition: cache_mngt.c:333
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
nl_cache_dump_filter
void nl_cache_dump_filter(struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
Dump all elements of a cache (filtered).
Definition: cache.c:1217
NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:23
idiagnl_msg_alloc_cache
int idiagnl_msg_alloc_cache(struct nl_sock *sk, int family, int states, struct nl_cache **result)
Build an inetdiag cache to hold socket state information.
Definition: idiag_msg_obj.c:146
nl_cache_free
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:409
IDIAGNL_SS_ALL
#define IDIAGNL_SS_ALL
Macro to represent all socket states.
Definition: idiagnl.h:117
nl_socket_free
void nl_socket_free(struct nl_sock *sk)
Free a netlink socket.
Definition: socket.c:244
nl_cache_mngt_unprovide
void nl_cache_mngt_unprovide(struct nl_cache *cache)
Unprovide a cache for global use.
Definition: cache_mngt.c:366
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
nl_dump_params
Dumping parameters.
Definition: types.h:33
NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:24