1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* src/nl-route-get.c Get Route Attributes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
*/
#include "utils.h"
static void print_usage(void)
{
printf("Usage: nl-route-get <addr>\n");
exit(1);
}
static int cb(struct nl_msg *msg, void *arg)
{
nl_cache_parse_and_add(arg, msg);
return 0;
}
int main(int argc, char *argv[])
{
struct nl_handle *nlh;
struct nl_cache *link_cache, *route_cache;
struct nl_addr *dst;
struct nl_dump_params params = {
.dp_fd = stdout,
.dp_type = NL_DUMP_BRIEF
};
int err = 1;
if (argc < 2 || !strcmp(argv[1], "-h"))
print_usage();
if (nltool_init(argc, argv) < 0)
goto errout;
nlh = nltool_alloc_handle();
if (!nlh)
goto errout;
if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
goto errout_free_handle;
link_cache = nltool_alloc_link_cache(nlh);
if (!link_cache)
goto errout_close;
dst = nltool_addr_parse(argv[1]);
if (!dst)
goto errout_link_cache;
route_cache = nltool_alloc_route_cache(nlh, 0);
if (!route_cache)
goto errout_addr_put;
{
struct nl_msg *m;
struct rtmsg rmsg = {
.rtm_family = nl_addr_get_family(dst),
.rtm_dst_len = nl_addr_get_prefixlen(dst),
};
m = nlmsg_alloc_simple(RTM_GETROUTE, 0);
nlmsg_append(m, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO);
nla_put_addr(m, RTA_DST, dst);
if ((err = nl_send_auto_complete(nlh, m)) < 0) {
nlmsg_free(m);
fprintf(stderr, "%s\n", nl_geterror());
goto errout_route_cache;
}
nlmsg_free(m);
nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, cb,
route_cache);
if (nl_recvmsgs_default(nlh) < 0) {
fprintf(stderr, "%s\n", nl_geterror());
goto errout_route_cache;
}
}
nl_cache_dump(route_cache, ¶ms);
err = 0;
errout_route_cache:
nl_cache_free(route_cache);
errout_addr_put:
nl_addr_put(dst);
errout_link_cache:
nl_cache_free(link_cache);
errout_close:
nl_close(nlh);
errout_free_handle:
nl_handle_destroy(nlh);
errout:
return err;
}
|