]>
git.proxmox.com Git - mirror_frr.git/blob - pimd/mtracebis_routeget.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Multicast Traceroute for FRRouting
4 * Copyright (C) 2018 Mladen Sablic
13 #include <asm/types.h>
14 #include <linux/netlink.h>
15 #include <linux/rtnetlink.h>
16 #include <sys/types.h>
18 #include <arpa/inet.h>
21 #include "mtracebis_netlink.h"
22 #include "mtracebis_routeget.h"
24 static int find_dst(struct nlmsghdr
*n
, struct in_addr
*src
, struct in_addr
*gw
)
26 struct rtmsg
*r
= NLMSG_DATA(n
);
27 int len
= n
->nlmsg_len
;
28 struct rtattr
*tb
[RTA_MAX
+ 1];
30 len
-= NLMSG_LENGTH(sizeof(*r
));
32 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
36 parse_rtattr(tb
, RTA_MAX
, RTM_RTA(r
), len
);
38 src
->s_addr
= *(uint32_t *)RTA_DATA(tb
[RTA_PREFSRC
]);
40 gw
->s_addr
= *(uint32_t *)RTA_DATA(tb
[RTA_GATEWAY
]);
42 return *(int *)RTA_DATA(tb
[RTA_OIF
]);
46 int routeget(struct in_addr dst
, struct in_addr
*src
, struct in_addr
*gw
)
54 struct rtnl_handle rth
= {.fd
= -1};
56 memset(&req
, 0, sizeof(req
));
58 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
59 req
.n
.nlmsg_flags
= NLM_F_REQUEST
;
60 req
.n
.nlmsg_type
= RTM_GETROUTE
;
61 req
.r
.rtm_family
= AF_INET
;
63 req
.r
.rtm_protocol
= 0;
66 req
.r
.rtm_src_len
= 0;
67 req
.r
.rtm_dst_len
= 0;
70 addattr_l(&req
.n
, sizeof(req
), RTA_DST
, &dst
.s_addr
, 4);
71 req
.r
.rtm_dst_len
= 32;
73 ret
= rtnl_open(&rth
, 0);
75 if (ret
< 0 || rth
.fd
<= 0)
78 if (rtnl_talk(&rth
, &req
.n
, 0, 0, &req
.n
, NULL
, NULL
) < 0) {
83 ret
= find_dst(&req
.n
, src
, gw
);
89 #endif /* __linux__ */