18e6f4a4f124caa3c782aad0ac8bb3ab4f432e68
[libnl.git] / src / nl-route-dump.c
1 /*
2  * src/nl-route-dump.c     Dump 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-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13
14 static void print_usage(void)
15 {
16         printf(
17         "Usage: nl-route-dump <mode> [<filter>]\n"
18         "  mode := { brief | detailed | stats | xml }\n");
19         exit(1);
20 }
21
22 #include "f_route.c"
23
24 int main(int argc, char *argv[])
25 {
26         struct nl_handle *nlh;
27         struct nl_cache *link_cache, *route_cache;
28         struct rtnl_route *route;
29         struct nl_dump_params params = {
30                 .dp_fd = stdout,
31                 .dp_type = NL_DUMP_BRIEF
32         };
33         int err = 1;
34
35         if (nltool_init(argc, argv) < 0)
36                 return -1;
37
38         if (argc < 2 || !strcmp(argv[1], "-h"))
39                 print_usage();
40
41         nlh = nl_handle_alloc_nondefault(nltool_cbset);
42         if (!nlh)
43                 goto errout;
44
45         route = rtnl_route_alloc();
46         if (!route)
47                 goto errout;
48
49         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
50                 goto errout_free;
51
52         link_cache = nltool_alloc_link_cache(nlh);
53         if (!link_cache)
54                 goto errout_close;
55
56         route_cache = nltool_alloc_route_cache(nlh);
57         if (!route_cache)
58                 goto errout_link_cache;
59
60         params.dp_type = nltool_parse_dumptype(argv[1]);
61         if (params.dp_type < 0)
62                 goto errout_route_cache;
63
64         get_filter(route, argc, argv, 2, route_cache, link_cache);
65
66         nl_cache_dump_filter(route_cache, &params, (struct nl_object *) route);
67
68         err = 0;
69
70 errout_route_cache:
71         nl_cache_free(route_cache);
72 errout_link_cache:
73         nl_cache_free(link_cache);
74 errout_close:
75         nl_close(nlh);
76 errout_free:
77         rtnl_route_put(route);
78 errout:
79         nl_handle_destroy(nlh);
80         return err;
81 }