oops
[libnl.git] / src / nl-neigh-dump.c
1 /*
2  * src/nl-neigh-dump.c     Dump neighbour 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-neigh-dump <mode> [<filter>]\n"
18         "  mode := { brief | detailed | stats | xml }\n"
19         "  filter := [dev DEV] [dst ADDR] [lladdr ADDR]\n");
20         exit(1);
21 }
22
23 #include "f_neigh.c"
24
25 int main(int argc, char *argv[])
26 {
27         struct nl_handle *nlh;
28         struct nl_cache *link_cache, *neigh_cache;
29         struct rtnl_neigh *neigh;
30         struct nl_dump_params params = {
31                 .dp_fd = stdout,
32                 .dp_type = NL_DUMP_BRIEF
33         };
34         int err = 1;
35
36         if (nltool_init(argc, argv) < 0)
37                 return -1;
38
39         if (argc < 2 || !strcmp(argv[1], "-h"))
40                 print_usage();
41         
42         nlh = nl_handle_alloc_nondefault(nltool_cbset);
43         if (!nlh)
44                 return -1;
45
46         neigh = rtnl_neigh_alloc();
47         if (neigh == NULL)
48                 goto errout;
49
50         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
51                 goto errout_free;
52
53         link_cache = nltool_alloc_link_cache(nlh);
54         if (!link_cache)
55                 goto errout_close;
56
57         neigh_cache = nltool_alloc_neigh_cache(nlh);
58         if (!neigh_cache)
59                 goto errout_link_cache;
60
61         params.dp_type = nltool_parse_dumptype(argv[1]);
62         if (params.dp_type < 0)
63                 goto errout_neigh_cache;
64
65         get_filter(neigh, argc, argv, 2, neigh_cache);
66
67         nl_cache_dump_filter(neigh_cache, &params, (struct nl_object *) neigh);
68
69         err = 0;
70
71 errout_neigh_cache:
72         nl_cache_free(neigh_cache);
73 errout_link_cache:
74         nl_cache_free(link_cache);
75 errout_close:
76         nl_close(nlh);
77 errout_free:
78         rtnl_neigh_put(neigh);
79 errout:
80         nl_handle_destroy(nlh);
81         return err;
82 }