1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2009 Thomas Graf <[email protected]>
4 */
5
6 #include "nl-default.h"
7
8 #include <linux/netlink.h>
9
10 #include <netlink/cli/utils.h>
11 #include <netlink/cli/rule.h>
12 #include <netlink/cli/link.h>
13
print_usage(void)14 static void print_usage(void)
15 {
16 printf(
17 "Usage: nl-rule-list [OPTION]... [ROUTE]\n"
18 "\n"
19 "Options\n"
20 " -c, --cache List the contents of the route cache\n"
21 " -f, --format=TYPE Output format { brief | details | stats }\n"
22 " -h, --help Show this help\n"
23 " -v, --version Show versioning information\n"
24 "\n"
25 "Rule Options\n"
26 " --family Address family\n"
27 );
28 exit(0);
29 }
30
main(int argc,char * argv[])31 int main(int argc, char *argv[])
32 {
33 struct nl_sock *sock;
34 struct rtnl_rule *rule;
35 struct nl_cache *rule_cache;
36 struct nl_dump_params params = {
37 .dp_fd = stdout,
38 .dp_type = NL_DUMP_LINE,
39 };
40
41 sock = nl_cli_alloc_socket();
42 nl_cli_connect(sock, NETLINK_ROUTE);
43 nl_cli_link_alloc_cache(sock);
44 rule_cache = nl_cli_rule_alloc_cache(sock);
45 rule = nl_cli_rule_alloc();
46
47 for (;;) {
48 int c, optidx = 0;
49 enum {
50 ARG_FAMILY = 257,
51 };
52 static struct option long_opts[] = {
53 { "format", 1, 0, 'f' },
54 { "help", 0, 0, 'h' },
55 { "version", 0, 0, 'v' },
56 { "family", 1, 0, ARG_FAMILY },
57 { 0, 0, 0, 0 }
58 };
59
60 c = getopt_long(argc, argv, "f:hv", long_opts, &optidx);
61 if (c == -1)
62 break;
63
64 switch (c) {
65 case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
66 case 'h': print_usage(); break;
67 case 'v': nl_cli_print_version(); break;
68 case ARG_FAMILY: nl_cli_rule_parse_family(rule, optarg); break;
69 }
70 }
71
72 nl_cache_dump_filter(rule_cache, ¶ms, OBJ_CAST(rule));
73
74 return 0;
75 }
76