fix: addressed edge cases for external routing

This commit is contained in:
Andrew Rioux
2023-08-17 18:18:05 -04:00
parent b770d53f57
commit 1517ca6f1c
3 changed files with 20 additions and 13 deletions

View File

@@ -178,16 +178,22 @@ impl<T: From<*mut nl_object>> Iterator for CacheIter<'_, T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.cache_size || self.obj.is_null() {
return None;
loop {
if self.index >= self.cache_size {
return None;
}
self.index += 1;
let obj = self.obj;
self.obj = unsafe { nl_cache_get_next(obj) };
if obj.is_null() {
continue;
}
break Some(T::from(obj));
}
self.index += 1;
let obj = self.obj;
self.obj = unsafe { nl_cache_get_next(obj) };
Some(T::from(obj))
}
fn size_hint(&self) -> (usize, Option<usize>) {

View File

@@ -73,6 +73,9 @@ impl Link {
pub fn name(&self) -> String {
unsafe {
let name = rtnl_link_get_name(self.link);
if name.is_null() {
return "".to_string();
}
let name_rs = CStr::from_ptr(name);
std::str::from_utf8(name_rs.to_bytes()).unwrap().to_owned()
}
@@ -96,7 +99,7 @@ impl Link {
return None;
}
let ltype_rs = CStr::from_ptr(ltype);
Some(std::str::from_utf8(ltype_rs.to_bytes()).unwrap().to_owned())
Some(std::str::from_utf8(ltype_rs.to_bytes()).ok()?.to_owned())
}
}
@@ -196,8 +199,6 @@ pub fn get_macs_and_src_for_ip(
let link = netlink::get_link_by_index(links, link_ind)?;
let neighs_ = neighs.iter().collect::<Vec<_>>();
let neigh = neighs
.iter()
.find(|n| n.ifindex() == link.ifindex())