libnl: /data/src/libnl3/src/idiag-socket-details.c Source File

libnl 3.10.0

idiag-socket-details.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2013 Sassano Systems LLC <joe@sassanosystems.com>
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
14static 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
29int 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}
nl_cache_mngt_unprovide
void nl_cache_mngt_unprovide(struct nl_cache *cache)
Unprovide a cache for global use.
Definition cache_mngt.c:365
nl_cache_mngt_provide
void nl_cache_mngt_provide(struct nl_cache *cache)
Provide a cache for global use.
Definition cache_mngt.c:332
nl_cache_free
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition cache.c:409
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_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition utils.c:71
IDIAGNL_SS_ALL
#define IDIAGNL_SS_ALL
Macro to represent all socket states.
Definition idiagnl.h:111
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:174
nl_socket_free
void nl_socket_free(struct nl_sock *sk)
Free a netlink socket.
Definition socket.c:264
NL_DUMP_STATS
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition types.h:22
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition types.h:36