libnl 3.9.0
nl-nh-list.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2022 Stanislav Zaikin <zstaseg@gmail.com>
4 */
5
6#include "nl-default.h"
7
8#include <linux/netlink.h>
9
10#include <netlink/cli/utils.h>
11#include <netlink/cli/nh.h>
12#include <netlink/route/nh.h>
13
14static void print_usage(void)
15{
16 printf("Usage: nl-nh-list [OPTIONS]... \n"
17 "\n"
18 "OPTIONS\n"
19 " --details Show detailed information of each link\n"
20 " -h, --help Show this help text.\n"
21 " -v, --version Show versioning information.\n"
22 "\n"
23 " -n, --name=NAME Name of link\n"
24 " -i, --index Interface index (unique identifier)\n"
25 " --family=NAME Link address family\n");
26 exit(0);
27}
28
29int main(int argc, char *argv[])
30{
31 struct nl_sock *sock;
32 struct nl_cache *link_cache;
33 struct nl_dump_params params = {
35 .dp_fd = stdout,
36 };
37
38 sock = nl_cli_alloc_socket();
39 nl_cli_connect(sock, NETLINK_ROUTE);
40
41 for (;;) {
42 int c, optidx = 0;
43 enum {
44 ARG_FAMILY = 257,
45 ARG_DETAILS,
46 };
47 static struct option long_opts[] = { { "details", 0, 0,
48 ARG_DETAILS },
49 { "help", 0, 0, 'h' },
50 { "version", 0, 0, 'v' },
51 { "name", 1, 0, 'n' },
52 { 0, 0, 0, 0 } };
53
54 c = getopt_long(argc, argv, "hvn:i:", long_opts, &optidx);
55 if (c == -1)
56 break;
57
58 switch (c) {
59 case ARG_DETAILS:
60 params.dp_type = NL_DUMP_DETAILS;
61 break;
62 case 'h':
63 print_usage();
64 break;
65 case 'v':
66 nl_cli_print_version();
67 break;
68 }
69 }
70
71 link_cache = nl_cli_nh_alloc_cache(sock);
72
73 nl_cache_dump(link_cache, &params);
74
75 return 0;
76}
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements of a cache.
Definition cache.c:1203
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
Dumping parameters.
Definition types.h:32
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition types.h:36