diff --git a/src/networking/manage_packets.rs b/src/networking/manage_packets.rs index 1449455f..e933c145 100644 --- a/src/networking/manage_packets.rs +++ b/src/networking/manage_packets.rs @@ -349,11 +349,6 @@ fn get_traffic_direction( dest_port: Option, my_interface_addresses: &[Address], ) -> TrafficDirection { - let my_interface_addresses_ip: Vec = my_interface_addresses - .iter() - .map(|address| address.addr) - .collect(); - // first let's handle TCP and UDP loopback if source_ip.is_loopback() && destination_ip.is_loopback() @@ -367,15 +362,15 @@ fn get_traffic_direction( } // if interface_addresses is empty, check if the IP is a bogon (useful when importing pcap files) - let is_local = |interface_addresses: &Vec, ip: &IpAddr| -> bool { - if interface_addresses.is_empty() { + let is_local = |ip: &IpAddr| -> bool { + if my_interface_addresses.is_empty() { is_bogon(ip).is_some() } else { - interface_addresses.contains(ip) + my_interface_addresses.iter().any(|a| a.addr == *ip) } }; - if is_local(&my_interface_addresses_ip, source_ip) { + if is_local(source_ip) { // source is local TrafficDirection::Outgoing } else if source_ip.ne(&IpAddr::V4(Ipv4Addr::UNSPECIFIED)) @@ -383,7 +378,7 @@ fn get_traffic_direction( { // source not local and different from 0.0.0.0 and different from :: TrafficDirection::Incoming - } else if !is_local(&my_interface_addresses_ip, destination_ip) { + } else if !is_local(destination_ip) { // source is 0.0.0.0 or :: (local not yet assigned an IP) and destination is not local TrafficDirection::Outgoing } else { @@ -421,18 +416,11 @@ fn is_broadcast_address(address: &IpAddr, my_interface_addresses: &[Address]) -> return true; } // check if directed broadcast - let my_broadcast_addresses: Vec = my_interface_addresses - .iter() - .map(|address| { - address - .broadcast_addr - .unwrap_or_else(|| IpAddr::from([255, 255, 255, 255])) - }) - .collect(); - if my_broadcast_addresses.contains(address) { - return true; - } - false + my_interface_addresses.iter().any(|a| { + a.broadcast_addr + .unwrap_or_else(|| IpAddr::from([255, 255, 255, 255])) + == *address + }) } /// Determines if the connection is local @@ -452,16 +440,14 @@ pub fn is_local_connection( } // is the same subnet? else if let Some(IpAddr::V4(netmask)) = address.netmask { - let mut local_subnet = Vec::new(); - let mut remote_subnet = Vec::new(); let netmask_digits = netmask.octets(); let local_addr_digits = local_addr.octets(); let remote_addr_digits = address_to_lookup_v4.octets(); - for (i, netmask_digit) in netmask_digits.iter().enumerate() { - local_subnet.push(netmask_digit & local_addr_digits[i]); - remote_subnet.push(netmask_digit & remote_addr_digits[i]); - } - if local_subnet == remote_subnet { + if netmask_digits + .iter() + .enumerate() + .all(|(i, m)| (m & local_addr_digits[i]) == (m & remote_addr_digits[i])) + { ret_val = true; } } @@ -475,16 +461,14 @@ pub fn is_local_connection( } // is the same subnet? else if let Some(IpAddr::V6(netmask)) = address.netmask { - let mut local_subnet = Vec::new(); - let mut remote_subnet = Vec::new(); let netmask_digits = netmask.octets(); let local_addr_digits = local_addr.octets(); let remote_addr_digits = address_to_lookup_v6.octets(); - for (i, netmask_digit) in netmask_digits.iter().enumerate() { - local_subnet.push(netmask_digit & local_addr_digits[i]); - remote_subnet.push(netmask_digit & remote_addr_digits[i]); - } - if local_subnet == remote_subnet { + if netmask_digits + .iter() + .enumerate() + .all(|(i, m)| (m & local_addr_digits[i]) == (m & remote_addr_digits[i])) + { ret_val = true; } }