libnl  3.7.0
idiag-socket-details.c
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2013 Sassano Systems LLC <joe@sassanosystems.com>
4  */
5 
6 #include <netlink/cli/utils.h>
7 #include <netlink/idiag/idiagnl.h>
8 #include <netlink/idiag/msg.h>
9 #include <linux/netlink.h>
10 
11 static void print_usage(void)
12 {
13  printf(
14 "Usage: idiag-socket-details [OPTION]\n"
15 "\n"
16 "Options\n"
17 " --summary Show socket detail summary.\n"
18 " --details Show socket details on multiple lines.\n"
19 " --stats Show full socket statistics.\n"
20 " -h, --help Show this help.\n"
21 " -v, --version Show versioning information.\n"
22  );
23  exit(0);
24 }
25 
26 int main(int argc, char *argv[])
27 {
28  struct nl_sock *sock;
29  struct nl_cache *idiag_cache;
30  struct nl_dump_params params = {
32  .dp_nl_cb = NULL,
33  .dp_fd = stdout,
34  };
35  int err = 0;
36 
37  sock = nl_cli_alloc_socket();
38  nl_cli_connect(sock, NETLINK_INET_DIAG);
39  for (;;) {
40  int c, optidx = 0;
41  enum {
42  ARG_SUMMARY = 257,
43  ARG_DETAILS = 258,
44  ARG_STATS = 259,
45  ARG_FAMILY,
46  };
47  static struct option long_opts[] = {
48  { "details", 0, 0, ARG_DETAILS },
49  { "summary", 0, 0, ARG_SUMMARY },
50  { "stats", 0, 0, ARG_STATS},
51  { "help", 0, 0, 'h' },
52  { "version", 0, 0, 'v' },
53  { 0, 0, 0, 0 }
54  };
55 
56  c = getopt_long(argc, argv, "hv", long_opts, &optidx);
57  if (c == -1)
58  break;
59 
60  switch (c) {
61  case '?': exit(NLE_INVAL);
62  case ARG_SUMMARY: params.dp_type = NL_DUMP_LINE; break;
63  case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
64  case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
65  case 'h': print_usage(); break;
66  case 'v': nl_cli_print_version(); break;
67  }
68  }
69 
70  if ((err = idiagnl_msg_alloc_cache(sock, AF_INET, IDIAGNL_SS_ALL,
71  &idiag_cache))) {
72  nl_cli_fatal(err, "Unable to allocate idiag msg cache: %s",
73  nl_geterror(err));
74  }
75 
76  nl_cache_mngt_provide(idiag_cache);
77 
78  nl_cache_dump_filter(idiag_cache, &params, NULL);
79 
80  nl_cache_mngt_unprovide(idiag_cache);
81  nl_cache_free(idiag_cache);
82  nl_socket_free(sock);
83 
84  return 0;
85 }
void nl_cache_mngt_unprovide(struct nl_cache *cache)
Unprovide a cache for global use.
Definition: cache_mngt.c:359
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
Definition: cache_mngt.c:326
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:403
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:1211
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
#define IDIAGNL_SS_ALL
Macro to represent all socket states.
Definition: idiagnl.h:111
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.
void nl_socket_free(struct nl_sock *sk)
Free a netlink socket.
Definition: socket.c:238
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:18
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32