libnl 3.7.0
nl-route-list.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6#include <netlink/cli/utils.h>
7#include <netlink/cli/route.h>
8#include <netlink/cli/link.h>
9
10#include <linux/netlink.h>
11
12static void print_usage(void)
13{
14 printf(
15 "Usage: nl-route-list [OPTION]... [ROUTE]\n"
16 "\n"
17 "Options\n"
18 " -c, --cache List the contents of the route cache\n"
19 " -f, --format=TYPE Output format { brief | details | stats }\n"
20 " -h, --help Show this help\n"
21 " -v, --version Show versioning information\n"
22 "\n"
23 "Route Options\n"
24 " -d, --dst=ADDR destination prefix, e.g. 10.10.0.0/16\n"
25 " -n, --nexthop=NH nexthop configuration:\n"
26 " dev=DEV route via device\n"
27 " weight=WEIGHT weight of nexthop\n"
28 " flags=FLAGS\n"
29 " via=GATEWAY route via other node\n"
30 " realms=REALMS\n"
31 " e.g. dev=eth0,via=192.168.1.12\n"
32 " -t, --table=TABLE Routing table\n"
33 " --family=FAMILY Address family\n"
34 " --src=ADDR Source prefix\n"
35 " --iif=DEV Incomming interface\n"
36 " --pref-src=ADDR Preferred source address\n"
37 " --metrics=OPTS Metrics configurations\n"
38 " --priority=NUM Priotity\n"
39 " --scope=SCOPE Scope\n"
40 " --protocol=PROTO Protocol\n"
41 " --type=TYPE { unicast | local | broadcast | multicast }\n"
42 );
43 exit(0);
44}
45
46int main(int argc, char *argv[])
47{
48 struct nl_sock *sock;
49 struct nl_cache *link_cache, *route_cache;
50 struct rtnl_route *route;
51 struct nl_dump_params params = {
52 .dp_fd = stdout,
53 .dp_type = NL_DUMP_LINE,
54 };
55 int print_cache = 0;
56
57 sock = nl_cli_alloc_socket();
58 nl_cli_connect(sock, NETLINK_ROUTE);
59 link_cache = nl_cli_link_alloc_cache(sock);
60 route = nl_cli_route_alloc();
61
62 for (;;) {
63 int c, optidx = 0;
64 enum {
65 ARG_FAMILY = 257,
66 ARG_SRC = 258,
67 ARG_IIF,
68 ARG_PREF_SRC,
69 ARG_METRICS,
70 ARG_PRIORITY,
71 ARG_SCOPE,
72 ARG_PROTOCOL,
73 ARG_TYPE,
74 };
75 static struct option long_opts[] = {
76 { "cache", 0, 0, 'c' },
77 { "format", 1, 0, 'f' },
78 { "help", 0, 0, 'h' },
79 { "version", 0, 0, 'v' },
80 { "dst", 1, 0, 'd' },
81 { "nexthop", 1, 0, 'n' },
82 { "table", 1, 0, 't' },
83 { "family", 1, 0, ARG_FAMILY },
84 { "src", 1, 0, ARG_SRC },
85 { "iif", 1, 0, ARG_IIF },
86 { "pref-src", 1, 0, ARG_PREF_SRC },
87 { "metrics", 1, 0, ARG_METRICS },
88 { "priority", 1, 0, ARG_PRIORITY },
89 { "scope", 1, 0, ARG_SCOPE },
90 { "protocol", 1, 0, ARG_PROTOCOL },
91 { "type", 1, 0, ARG_TYPE },
92 { 0, 0, 0, 0 }
93 };
94
95 c = getopt_long(argc, argv, "cf:hvd:n:t:", long_opts, &optidx);
96 if (c == -1)
97 break;
98
99 switch (c) {
100 case 'c': print_cache = 1; break;
101 case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
102 case 'h': print_usage(); break;
103 case 'v': nl_cli_print_version(); break;
104 case 'd': nl_cli_route_parse_dst(route, optarg); break;
105 case 'n': nl_cli_route_parse_nexthop(route, optarg, link_cache); break;
106 case 't': nl_cli_route_parse_table(route, optarg); break;
107 case ARG_FAMILY: nl_cli_route_parse_family(route, optarg); break;
108 case ARG_SRC: nl_cli_route_parse_src(route, optarg); break;
109 case ARG_IIF: nl_cli_route_parse_iif(route, optarg, link_cache); break;
110 case ARG_PREF_SRC: nl_cli_route_parse_pref_src(route, optarg); break;
111 case ARG_METRICS: nl_cli_route_parse_metric(route, optarg); break;
112 case ARG_PRIORITY: nl_cli_route_parse_prio(route, optarg); break;
113 case ARG_SCOPE: nl_cli_route_parse_scope(route, optarg); break;
114 case ARG_PROTOCOL: nl_cli_route_parse_protocol(route, optarg); break;
115 case ARG_TYPE: nl_cli_route_parse_type(route, optarg); break;
116 }
117 }
118
119 route_cache = nl_cli_route_alloc_cache(sock,
120 print_cache ? ROUTE_CACHE_CONTENT : 0);
121
122 nl_cache_dump_filter(route_cache, &params, OBJ_CAST(route));
123
124 return 0;
125}
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
#define ROUTE_CACHE_CONTENT
When passed to rtnl_route_alloc_cache() the cache will correspond to the contents of the routing cach...
Definition: route.h:27
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:77