]>
Commit | Line | Data |
---|---|---|
4d9ad5dc MS |
1 | /* |
2 | * Multicast Traceroute for FRRouting | |
3 | * Copyright (C) 2018 Mladen Sablic | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #ifdef __linux__ | |
21 | ||
22 | #include <asm/types.h> | |
23 | #include <linux/netlink.h> | |
24 | #include <linux/rtnetlink.h> | |
25 | #include <sys/types.h> | |
26 | #include <stdio.h> | |
27 | #include <arpa/inet.h> | |
28 | #include <string.h> | |
29 | ||
30 | #include "mtracebis_netlink.h" | |
31 | #include "mtracebis_routeget.h" | |
32 | ||
33 | static int find_dst(struct nlmsghdr *n, struct in_addr *src, struct in_addr *gw) | |
34 | { | |
35 | struct rtmsg *r = NLMSG_DATA(n); | |
36 | int len = n->nlmsg_len; | |
37 | struct rtattr *tb[RTA_MAX + 1]; | |
38 | ||
39 | len -= NLMSG_LENGTH(sizeof(*r)); | |
40 | if (len < 0) { | |
41 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
42 | return -1; | |
43 | } | |
44 | ||
45 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); | |
46 | if (tb[RTA_PREFSRC]) | |
47 | src->s_addr = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]); | |
48 | if (tb[RTA_GATEWAY]) | |
49 | gw->s_addr = *(uint32_t *)RTA_DATA(tb[RTA_GATEWAY]); | |
50 | if (tb[RTA_OIF]) | |
51 | return *(int *)RTA_DATA(tb[RTA_OIF]); | |
52 | return 0; | |
53 | } | |
54 | ||
55 | int routeget(struct in_addr dst, struct in_addr *src, struct in_addr *gw) | |
56 | { | |
57 | struct { | |
58 | struct nlmsghdr n; | |
59 | struct rtmsg r; | |
60 | char buf[1024]; | |
61 | } req; | |
62 | int ret; | |
63 | struct rtnl_handle rth = {.fd = -1}; | |
64 | ||
65 | memset(&req, 0, sizeof(req)); | |
66 | ||
67 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | |
68 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
69 | req.n.nlmsg_type = RTM_GETROUTE; | |
70 | req.r.rtm_family = AF_INET; | |
71 | req.r.rtm_table = 0; | |
72 | req.r.rtm_protocol = 0; | |
73 | req.r.rtm_scope = 0; | |
74 | req.r.rtm_type = 0; | |
75 | req.r.rtm_src_len = 0; | |
76 | req.r.rtm_dst_len = 0; | |
77 | req.r.rtm_tos = 0; | |
78 | ||
79 | addattr_l(&req.n, sizeof(req), RTA_DST, &dst.s_addr, 4); | |
80 | req.r.rtm_dst_len = 32; | |
81 | ||
82 | ret = rtnl_open(&rth, 0); | |
83 | ||
25521d49 | 84 | if (ret < 0 || rth.fd <= 0) |
4d9ad5dc MS |
85 | return ret; |
86 | ||
87 | if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) { | |
88 | ret = -1; | |
89 | goto close_rth; | |
90 | } | |
91 | ||
92 | ret = find_dst(&req.n, src, gw); | |
93 | close_rth: | |
94 | rtnl_close(&rth); | |
95 | return ret; | |
96 | } | |
97 | ||
98 | #endif /* __linux__ */ |