refactor: simplified route query code

fighting the borrow checker
This commit is contained in:
Andrew Rioux
2023-05-01 10:52:23 -04:00
parent c16bf366b7
commit cfdf8f7e86
4 changed files with 162 additions and 30 deletions

View File

@@ -29,22 +29,53 @@ async fn main() -> anyhow::Result<()> {
let socket = netlink::Socket::new()?;
let routes = socket.get_routes()?;
let mut routes_inner = routes.iter().collect::<Vec<_>>();
let neighs = socket.get_neigh()?;
let links = socket.get_links()?;
let addrs = socket.get_addrs()?;
let srcip = route::get_srcip_for_dstip(&routes, target)
routes_inner.sort_by(|r1, r2| {
r2.dst().map(|a| a.cidrlen())
.partial_cmp(&r1.dst().map(|a| a.cidrlen()))
.unwrap_or(std::cmp::Ordering::Equal)
});
println!("-=- Addrs -=-");
for addr in addrs.iter() {
println!("addr: {:?}, {}", addr.local(), addr.ifindex());
}
println!("-=- Routes -=-");
for route in routes_inner.iter() {
println!("route: {:?}", route.dst());
for hop in route.hop_iter() {
println!("\thop: {:?}, {}", hop.gateway(), hop.ifindex());
}
}
println!("-=- Neighs -=-");
for neigh in neighs.iter() {
println!("neigh: {:?}, {:?}, {}", neigh.dst(), neigh.lladdr(), neigh.ifindex());
}
println!("-=- Links -=-");
for link in links.iter() {
println!("link {:?}: {:?}, {}", link.name(), link.addr(), link.ifindex());
}
let (srcip, srcmac, dstmac) = route::get_macs_and_src_for_ip(&addrs, &routes, &neighs, &links, target)
.ok_or(anyhow!("unable to find a route to the IP"))?;
/*let srcip = route::get_srcip_for_dstip(&routes, target)
.ok_or(anyhow!("Unable to find a route to the IP"))?;
let (target_link, dst_mac) = route::get_neigh_for_addr(&routes, &neighs, &links, &srcip.into())
.ok_or(anyhow!("Unable to find local interface to use"))?;
let (srcip, target_link, dst_mac) = route::get_neigh_for_addr(&routes, &neighs, &links, &srcip.into())
.ok_or(anyhow!("Unable to find local interface to use"))?;*/
let src_mac = target_link.addr().hw_address();
dbg!(srcmac);
dbg!(dstmac);
dbg!(srcip);
dbg!(target);
(
TryInto::<[u8;6]>::try_into(src_mac).unwrap(),
TryInto::<[u8;6]>::try_into(dst_mac).unwrap(),
srcip
)
( srcmac, dstmac, srcip )
};
let mut interfaces = pcap_sys::PcapDevIterator::new()?;