]>
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 | ||
b45ac5f5 DL |
20 | #ifdef HAVE_CONFIG_H |
21 | #include "config.h" | |
22 | #endif | |
23 | ||
4d9ad5dc MS |
24 | #ifdef __linux__ |
25 | ||
26 | #include <asm/types.h> | |
27 | #include <linux/netlink.h> | |
28 | #include <linux/rtnetlink.h> | |
29 | #include <sys/types.h> | |
30 | #include <stdio.h> | |
31 | #include <arpa/inet.h> | |
32 | #include <string.h> | |
33 | ||
34 | #include "mtracebis_netlink.h" | |
35 | #include "mtracebis_routeget.h" | |
36 | ||
37 | static int find_dst(struct nlmsghdr *n, struct in_addr *src, struct in_addr *gw) | |
38 | { | |
39 | struct rtmsg *r = NLMSG_DATA(n); | |
40 | int len = n->nlmsg_len; | |
41 | struct rtattr *tb[RTA_MAX + 1]; | |
42 | ||
43 | len -= NLMSG_LENGTH(sizeof(*r)); | |
44 | if (len < 0) { | |
45 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
46 | return -1; | |
47 | } | |
48 | ||
49 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); | |
50 | if (tb[RTA_PREFSRC]) | |
51 | src->s_addr = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]); | |
52 | if (tb[RTA_GATEWAY]) | |
53 | gw->s_addr = *(uint32_t *)RTA_DATA(tb[RTA_GATEWAY]); | |
54 | if (tb[RTA_OIF]) | |
55 | return *(int *)RTA_DATA(tb[RTA_OIF]); | |
56 | return 0; | |
57 | } | |
58 | ||
59 | int routeget(struct in_addr dst, struct in_addr *src, struct in_addr *gw) | |
60 | { | |
61 | struct { | |
62 | struct nlmsghdr n; | |
63 | struct rtmsg r; | |
64 | char buf[1024]; | |
65 | } req; | |
66 | int ret; | |
67 | struct rtnl_handle rth = {.fd = -1}; | |
68 | ||
69 | memset(&req, 0, sizeof(req)); | |
70 | ||
71 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | |
72 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
73 | req.n.nlmsg_type = RTM_GETROUTE; | |
74 | req.r.rtm_family = AF_INET; | |
75 | req.r.rtm_table = 0; | |
76 | req.r.rtm_protocol = 0; | |
77 | req.r.rtm_scope = 0; | |
78 | req.r.rtm_type = 0; | |
79 | req.r.rtm_src_len = 0; | |
80 | req.r.rtm_dst_len = 0; | |
81 | req.r.rtm_tos = 0; | |
82 | ||
83 | addattr_l(&req.n, sizeof(req), RTA_DST, &dst.s_addr, 4); | |
84 | req.r.rtm_dst_len = 32; | |
85 | ||
86 | ret = rtnl_open(&rth, 0); | |
87 | ||
25521d49 | 88 | if (ret < 0 || rth.fd <= 0) |
4d9ad5dc MS |
89 | return ret; |
90 | ||
91 | if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) { | |
92 | ret = -1; | |
93 | goto close_rth; | |
94 | } | |
95 | ||
96 | ret = find_dst(&req.n, src, gw); | |
97 | close_rth: | |
98 | rtnl_close(&rth); | |
99 | return ret; | |
100 | } | |
101 | ||
102 | #endif /* __linux__ */ |