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