nl-route-list.c revision 85808860b6174aecc901c34c90968911ad013280
1/*
2 * src/nl-route-list.c     List route 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-2008 Thomas Graf <tgraf@suug.ch>
10 */
11
12#include "route-utils.h"
13
14static void print_version(void)
15{
16	fprintf(stderr, "%s\n", LIBNL_STRING);
17	exit(0);
18}
19
20static void print_usage(void)
21{
22	printf(
23	"Usage: nl-route-list [OPTION]... [ROUTE]\n"
24	"\n"
25	"Options\n"
26	" -c, --cache           List the contents of the route cache\n"
27	" -f, --format=TYPE	Output format { brief | details | stats }\n"
28	" -h, --help            Show this help\n"
29	" -v, --version		Show versioning information\n"
30	"\n"
31	"Route Options\n"
32	" -d, --dst=ADDR        destination prefix, e.g. 10.10.0.0/16\n"
33	" -n, --nexthop=NH      nexthop configuration:\n"
34	"                         dev=DEV         route via device\n"
35	"                         weight=WEIGHT   weight of nexthop\n"
36	"                         flags=FLAGS\n"
37	"                         via=GATEWAY     route via other node\n"
38	"                         realms=REALMS\n"
39	"                         e.g. dev=eth0,via=192.168.1.12\n"
40	" -t, --table=TABLE     Routing table\n"
41	"     --family=FAMILY	Address family\n"
42	"     --src=ADDR        Source prefix\n"
43	"     --iif=DEV         Incomming interface\n"
44	"     --pref-src=ADDR   Preferred source address\n"
45	"     --metrics=OPTS    Metrics configurations\n"
46	"     --priority=NUM    Priotity\n"
47	"     --scope=SCOPE     Scope\n"
48	"     --protocol=PROTO  Protocol\n"
49	"     --type=TYPE       { unicast | local | broadcast | multicast }\n"
50	);
51	exit(0);
52}
53
54int main(int argc, char *argv[])
55{
56	struct nl_handle *nlh;
57	struct nl_cache *link_cache, *route_cache;
58	struct rtnl_route *route;
59	struct nl_dump_params params = {
60		.dp_fd = stdout,
61		.dp_type = NL_DUMP_BRIEF
62	};
63	int err = 1, print_cache = 0;
64
65	nlh = nltool_alloc_handle();
66	nltool_connect(nlh, NETLINK_ROUTE);
67	link_cache = nltool_alloc_link_cache(nlh);
68
69	route = rtnl_route_alloc();
70	if (!route)
71		goto errout;
72
73	for (;;) {
74		int c, optidx = 0;
75		enum {
76			ARG_FAMILY = 257,
77			ARG_SRC = 258,
78			ARG_IIF,
79			ARG_PREF_SRC,
80			ARG_METRICS,
81			ARG_PRIORITY,
82			ARG_SCOPE,
83			ARG_PROTOCOL,
84			ARG_TYPE,
85		};
86		static struct option long_opts[] = {
87			{ "cache", 0, 0, 'c' },
88			{ "format", 1, 0, 'f' },
89			{ "help", 0, 0, 'h' },
90			{ "version", 0, 0, 'v' },
91			{ "dst", 1, 0, 'd' },
92			{ "nexthop", 1, 0, 'n' },
93			{ "table", 1, 0, 't' },
94			{ "family", 1, 0, ARG_FAMILY },
95			{ "src", 1, 0, ARG_SRC },
96			{ "iif", 1, 0, ARG_IIF },
97			{ "pref-src", 1, 0, ARG_PREF_SRC },
98			{ "metrics", 1, 0, ARG_METRICS },
99			{ "priority", 1, 0, ARG_PRIORITY },
100			{ "scope", 1, 0, ARG_SCOPE },
101			{ "protocol", 1, 0, ARG_PROTOCOL },
102			{ "type", 1, 0, ARG_TYPE },
103			{ 0, 0, 0, 0 }
104		};
105
106		c = getopt_long(argc, argv, "cf:hvd:n:t:", long_opts, &optidx);
107		if (c == -1)
108			break;
109
110		switch (c) {
111		case 'c': print_cache = 1; break;
112		case 'f': params.dp_type = nltool_parse_dumptype(optarg); break;
113		case 'h': print_usage(); break;
114		case 'v': print_version(); break;
115		case 'd': parse_dst(route, optarg); break;
116		case 'n': parse_nexthop(route, optarg, link_cache); break;
117		case 't': parse_table(route, optarg); break;
118		case ARG_FAMILY: parse_family(route, optarg); break;
119		case ARG_SRC: parse_src(route, optarg); break;
120		case ARG_IIF: parse_iif(route, optarg, link_cache); break;
121		case ARG_PREF_SRC: parse_pref_src(route, optarg); break;
122		case ARG_METRICS: parse_metric(route, optarg); break;
123		case ARG_PRIORITY: parse_prio(route, optarg); break;
124		case ARG_SCOPE: parse_scope(route, optarg); break;
125		case ARG_PROTOCOL: parse_protocol(route, optarg); break;
126		case ARG_TYPE: parse_type(route, optarg); break;
127		}
128	}
129
130	route_cache = nltool_alloc_route_cache(nlh,
131				print_cache ? ROUTE_CACHE_CONTENT : 0);
132
133	nl_cache_dump_filter(route_cache, &params, OBJ_CAST(route));
134
135	err = 0;
136
137	rtnl_route_put(route);
138	nl_cache_free(route_cache);
139errout:
140	nl_cache_free(link_cache);
141	nl_close(nlh);
142	nl_handle_destroy(nlh);
143
144	return err;
145}
146