b5425bd30a71f10cb37a3b424dbe47dc5b2601d2
[libnl.git] / src / nl-route-get.c
1 /*
2  * src/nl-route-get.c     Get 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("Usage: nl-route-get <addr>\n");
17         exit(1);
18 }
19
20 static int cb(struct nl_msg *msg, void *arg)
21 {
22         nl_cache_parse_and_add(arg, msg);
23
24         return 0;
25 }
26
27 int main(int argc, char *argv[])
28 {
29         struct nl_handle *nlh;
30         struct nl_cache *link_cache, *route_cache;
31         struct nl_addr *dst;
32         struct nl_dump_params params = {
33                 .dp_fd = stdout,
34                 .dp_type = NL_DUMP_BRIEF
35         };
36         int err = 1;
37
38         if (argc < 2 || !strcmp(argv[1], "-h"))
39                 print_usage();
40
41         if (nltool_init(argc, argv) < 0)
42                 goto errout;
43
44         nlh = nl_handle_alloc_nondefault(nltool_cbset);
45         if (!nlh)
46                 goto errout;
47
48         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
49                 goto errout_free_handle;
50
51         link_cache = nltool_alloc_link_cache(nlh);
52         if (!link_cache)
53                 goto errout_close;
54
55         dst = nltool_addr_parse(argv[1]);
56         if (!dst)
57                 goto errout_link_cache;
58
59         route_cache = nltool_alloc_route_cache(nlh);
60         if (!route_cache)
61                 goto errout_addr_put;
62
63         {
64                 struct nl_msg *m;
65                 struct rtmsg rmsg = {
66                         .rtm_family = nl_addr_get_family(dst),
67                         .rtm_dst_len = nl_addr_get_prefixlen(dst),
68                 };
69
70                 m = nlmsg_build_simple(RTM_GETROUTE, 0);
71                 nlmsg_append(m, &rmsg, sizeof(rmsg), 1);
72                 nla_put_addr(m, RTA_DST, dst);
73
74                 if ((err = nl_send_auto_complete(nlh, m)) < 0) {
75                         nlmsg_free(m);
76                         fprintf(stderr, "%s\n", nl_geterror());
77                         goto errout_route_cache;
78                 }
79
80                 nlmsg_free(m);
81
82                 nl_cb_set(nl_handle_get_cb(nlh), NL_CB_VALID, NL_CB_CUSTOM,
83                           cb, route_cache);
84
85                 if (nl_recvmsgs_def(nlh) < 0) {
86                         fprintf(stderr, "%s\n", nl_geterror());
87                         goto errout_route_cache;
88                 }
89         }
90
91         nl_cache_dump(route_cache, &params);
92
93         err = 0;
94 errout_route_cache:
95         nl_cache_free(route_cache);
96 errout_addr_put:
97         nl_addr_put(dst);
98 errout_link_cache:
99         nl_cache_free(link_cache);
100 errout_close:
101         nl_close(nlh);
102 errout_free_handle:
103         nl_handle_destroy(nlh);
104 errout:
105         return err;
106 }