fn if_index()

in src/linux.rs [245:264]


fn if_index(remote: IpAddr, fd: &mut RouteSocket) -> Result<i32> {
    // Send RTM_GETROUTE message to get the interface index associated with the destination.
    let msg_seq = RouteSocket::new_seq();
    let msg = IfIndexMsg::new(remote, msg_seq);
    fd.write_all((&msg).into())?;

    // Receive RTM_GETROUTE response.
    let (_hdr, mut buf) = read_msg_with_seq(fd, msg_seq, RTM_NEWROUTE)?;
    debug_assert!(size_of::<rtmsg>() <= buf.len());
    let buf = buf.split_off(size_of::<rtmsg>());

    // Parse through the attributes to find the interface index.
    for attr in RtAttrs(buf.as_slice()).by_ref() {
        if attr.hdr.rta_type == RTA_OIF {
            // We have our interface index.
            return parse_c_int(attr.msg);
        }
    }
    Err(default_err())
}