feat: added test tcp client

This commit is contained in:
Andrew Rioux
2023-09-19 10:24:51 -04:00
parent e5f6c2aa7e
commit 35bcf5352b
6 changed files with 251 additions and 67 deletions

View File

@@ -170,7 +170,9 @@ pub fn get_macs_and_src_for_ip(
0
};
let Ok(dst_addr): Result<Ipv4Addr, _> = (&dst).try_into() else { return false };
let Ok(dst_addr): Result<Ipv4Addr, _> = (&dst).try_into() else {
return false;
};
let dst_addr: u32 = dst_addr.into();
(mask & dst_addr) == (mask & ip_int)
@@ -181,7 +183,12 @@ pub fn get_macs_and_src_for_ip(
#[cfg(debug_assertions)]
{
for link in links.iter() {
println!("Link {}: {:?} ({})", link.name(), link.addr(), link.ifindex());
println!(
"Link {}: {:?} ({})",
link.name(),
link.addr(),
link.ifindex()
);
println!("\tAddrs:");
for addr in addrs.iter().filter(|addr| addr.ifindex() == link.ifindex()) {
@@ -191,7 +198,11 @@ pub fn get_macs_and_src_for_ip(
}
println!("\tNeighbors:");
for neigh in neighs.iter().filter(|neigh| neigh.ifindex() == link.ifindex()) {
for neigh in neighs
.iter()
.filter(|neigh| neigh.ifindex() == link.ifindex())
{
println!("\t\ttest {:?}, {:?}", neigh.ifindex(), neigh.dst());
println!("\t\t{:?}, {:?}", neigh.dst(), neigh.lladdr());
}
}
@@ -224,7 +235,9 @@ pub fn get_neigh_for_addr(
addr: &Addr,
) -> Option<(Ipv4Addr, Link, [u8; 6])> {
for link in links.iter() {
let Some(neigh) = link.get_neigh(&neighs, addr) else { continue; };
let Some(neigh) = link.get_neigh(&neighs, addr) else {
continue;
};
return Some((addr.try_into().ok()?, link, neigh));
}
@@ -239,7 +252,7 @@ pub fn get_neigh_for_addr(
};
let Some(first_hop) = def_neigh.hop_iter().next() else {
return None
return None;
};
if n.ifindex() != first_hop.ifindex() {
@@ -322,8 +335,12 @@ impl Addr {
}
// Determines the type of data in [`Addr::hw_address`]
pub fn atype(&self) -> c_int {
unsafe { nl_addr_get_family(self.addr) }
pub fn atype(&self) -> Option<c_int> {
if self.addr.is_null() {
None
} else {
Some(unsafe { nl_addr_get_family(self.addr) })
}
}
/// Returns the length of the subnet mask applying to this address
@@ -334,8 +351,8 @@ impl Addr {
impl Debug for Addr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.atype() {
AF_INET => {
let res = match self.atype() {
Some(AF_INET) => {
let octets = self.hw_address();
f.debug_struct("Addr")
.field(
@@ -351,7 +368,7 @@ impl Debug for Addr {
)
.finish()
}
AF_LLC => {
Some(AF_LLC) => {
let octets = self.hw_address();
f.debug_struct("Addr")
@@ -359,22 +376,23 @@ impl Debug for Addr {
"addr",
&format!(
"{:02X?}:{:02X?}:{:02X?}:{:02X?}:{:02X?}:{:02X?}",
octets[0],
octets[1],
octets[2],
octets[3],
octets[4],
octets[5],
)
octets[0], octets[1], octets[2], octets[3], octets[4], octets[5],
),
)
.finish()
}
None => f
.debug_struct("Addr")
.field("addr", &"unknown")
.field("atype", &"unknown")
.finish(),
_ => f
.debug_struct("Addr")
.field("addr", &self.hw_address())
.field("atype", &self.atype())
.finish(),
}
};
res
}
}
@@ -551,7 +569,9 @@ pub fn get_srcip_for_dstip(routes: &Cache<Route>, ip: Ipv4Addr) -> Option<Ipv4Ad
0
};
let Ok(dst_addr): Result<Ipv4Addr, _> = (&dst).try_into() else { return false };
let Ok(dst_addr): Result<Ipv4Addr, _> = (&dst).try_into() else {
return false;
};
let dst_addr: u32 = dst_addr.into();
(mask & dst_addr) == (mask & ip_int)