Determine correct gateway for ip in rust

132 Views Asked by At

I've been working with the rtnetlink library (which I find somewhat inscrutable) to determine various information regarding mac addresses of neighbors, local addresses etc.

Currently I'm trying to determine the the equivalent of this command

$ ip route get 10.244.1.2
10.244.1.2 via 192.168.64.4 dev eth0 src 192.168.64.3 uid 1000 
    cache 

Specifically I'm trying to get the via portion. I'm would like to avoid shelling out to the ip command if possible. This is the rust code I'm currently using to try and get the information

    let (conn, handle, _) = rtnetlink::new_connection()?;
    tokio::spawn(conn);
    let mut req = handle.route().get(IpVersion::V4);
    let msg = req.message_mut();
    msg.attributes
        .push(RouteAttribute::Destination(RouteAddress::Inet(
            Ipv4Addr::new(10, 244, 1, 2),
        )));
    msg.header.destination_prefix_length = 32;
    msg.header.address_family = AddressFamily::Inet;
    msg.header.flags.push(RouteFlag::LookupTable);

    let resp = req.execute().try_collect::<Vec<_>>().await?;
    info!("resp = {resp:?}");

However instead of getting back the result for the route to that IP what I get is the list of all routes as if I had run ip route show. I've tried reading through the man page on the topic and reading the c code for the ip route get command to see what I'm doing wrong, but I'm coming up short.

0

There are 0 best solutions below