Hi, On Wednesday, 18 July 2018 10:33:12 CEST Laura Garcia wrote: > On Wed, Jul 18, 2018 at 10:10 AM, Bednár Martin <martin@xxxxxxxxxxx> wrote: > > Hi, > > > > I'm trying to implement `ip route get x.x.x.x` functionality in C. > > I understand this command asks the kernel to resolve the route/gateway > > for a concrete address and report it to userspace. Please correct me if > > I'm wrong. > > > > I'm trying to do this in C using the libnl-route API, but don't see any > > combination of functions to achieve it. > > I have managed to get the default route by filtering a zero address from > > the cache. But I'm stumped at submitting a concrete address to look up... > > Could anyone point me in the right direction? > > Hi! > > I've recently implemented such feature in my code using libmnl and > following this example: Thanks! The example certainly helps. I was hoping I'd avoid rewriting what I have using pure netlink, and be able to use libnl-route. Ah well... > http://git.netfilter.org/libmnl/tree/examples/rtnl/rtnl-route-dump.c > > Firstly, you've to create a header with type RTM_GETROUTE and flag > NLM_F_REQUEST. > Later, an extra header like: > > rtm->rtm_family = AF_INET; > rtm->rtm_dst_len = 32; > rtm->rtm_src_len = 0; > rtm->rtm_tos = 0; > rtm->rtm_protocol = RTPROT_UNSPEC; > rtm->rtm_table = RT_TABLE_UNSPEC; > rtm->rtm_type = RTN_UNSPEC; > rtm->rtm_scope = RT_SCOPE_UNIVERSE; > rtm->rtm_flags = RTM_F_LOOKUP_TABLE; > > And finally, add an attribute with mnl_attr_put() inserting the > RTA_DST with the destination IP address. > > All this will generate a netlink request that gets the routing info > for such destination address. > This is definitely an awesome first stepping stone. thanks. Martin. -- To unsubscribe from this list: send the line "unsubscribe netfilter" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html