Add logging when the process name fails to resolve

This commit is contained in:
cyqsimon
2023-10-12 17:35:43 +08:00
parent 1c996c38f4
commit a9c199f812
2 changed files with 48 additions and 14 deletions

View File

@@ -6,6 +6,8 @@ use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr},
};
use log::warn;
use crate::network::{Connection, LocalSocket, Utilization};
static RECALL_LENGTH: usize = 5;
@@ -92,21 +94,41 @@ impl UIState {
connections_to_procs: &'a HashMap<LocalSocket, String>,
local_socket: &LocalSocket,
) -> Option<&'a String> {
if let Some(process_name) = connections_to_procs.get(local_socket) {
Some(process_name)
} else if let Some(process_name) = connections_to_procs.get(&LocalSocket {
ip: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
port: local_socket.port,
protocol: local_socket.protocol,
}) {
Some(process_name)
} else {
connections_to_procs.get(&LocalSocket {
ip: IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)),
port: local_socket.port,
protocol: local_socket.protocol,
let &LocalSocket { port, protocol, .. } = local_socket;
let name = connections_to_procs
.get(local_socket)
.or_else(|| {
connections_to_procs.get(&LocalSocket {
ip: Ipv4Addr::UNSPECIFIED.into(),
port,
protocol,
})
})
.or_else(|| {
connections_to_procs.get(&LocalSocket {
ip: Ipv6Addr::UNSPECIFIED.into(),
port,
protocol,
})
});
if name.is_none() {
match connections_to_procs
.iter()
.find(|(socket, _)| socket.port == port && socket.protocol == protocol)
{
Some((lookalike, name)) => {
warn!(
r#""{name}" owns a similar looking connection, but its local ip doesn't match."#
);
warn!("Looking for: {local_socket}; found: {lookalike}");
}
None => warn!("Cannot determine which process owns {local_socket}."),
};
}
name
}
pub fn update(
&mut self,
@@ -154,7 +176,9 @@ impl UIState {
UIState::get_proc_name(connections_to_procs, &connection.local_socket)
{
connection_data.process_name = process_name.clone();
processes.entry(process_name.clone()).or_default()
processes
.entry(connection_data.process_name.clone())
.or_default()
} else {
connection_data.process_name = String::from("<UNKNOWN>");
processes

View File

@@ -43,6 +43,16 @@ pub struct LocalSocket {
pub protocol: Protocol,
}
impl fmt::Display for LocalSocket {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let LocalSocket { ip, port, protocol } = self;
match ip {
IpAddr::V4(v4) => write!(f, "{protocol}://{v4}:{port}"),
IpAddr::V6(v6) => write!(f, "{protocol}://[{v6}]:{port}"),
}
}
}
#[derive(PartialEq, Hash, Eq, Clone, PartialOrd, Ord, Debug, Copy)]
pub struct Connection {
pub remote_socket: Socket,