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