Skip to content

Commit

Permalink
Log both src & dst when we fail to resolve connection to process
Browse files Browse the repository at this point in the history
- Per recommendation in #196 (comment)
  • Loading branch information
cyqsimon committed Oct 23, 2023
1 parent f1a8572 commit b59b151
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
31 changes: 19 additions & 12 deletions src/display/ui_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,38 @@ impl UIState {
total_bytes_uploaded += connection_info.total_bytes_uploaded;

let data_for_process = {
let process_name =
get_proc_name(connections_to_procs, &connection.local_socket);
let local_socket = connection.local_socket;
let process_name = get_proc_name(connections_to_procs, &local_socket);

// only log each orphan connection once
let orphan = connection.local_socket;
if process_name.is_none() && !self.known_orphan_sockets.contains(&orphan) {
if process_name.is_none() && !self.known_orphan_sockets.contains(&local_socket)
{
// newer connections go in the front so that searches are faster
// basically recency bias
self.known_orphan_sockets.push_front(orphan);
self.known_orphan_sockets.push_front(local_socket);
self.known_orphan_sockets.truncate(10_000); // arbitrary maximum backlog

match connections_to_procs.iter().find(
|(&LocalSocket { port, protocol, .. }, _)| {
port == orphan.port && protocol == orphan.protocol
},
) {
match connections_to_procs
.iter()
.find(|(&LocalSocket { port, protocol, .. }, _)| {
port == local_socket.port && protocol == local_socket.protocol
})
.and_then(|(local_conn_lookalike, name)| {
network_utilization
.connections
.keys()
.find(|conn| &conn.local_socket == local_conn_lookalike)
.map(|conn| (conn, name))
}) {
Some((lookalike, name)) => {
mt_log!(
warn,
r#""{name}" owns a similar looking connection, but its local ip doesn't match."#
);
mt_log!(warn, "Looking for: {orphan}; found: {lookalike}");
mt_log!(warn, "Looking for: {connection:?}; found: {lookalike:?}");
}
None => {
mt_log!(warn, "Cannot determine which process owns {orphan}.")
mt_log!(warn, "Cannot determine which process owns {connection:?}");
}
};
}
Expand Down
28 changes: 24 additions & 4 deletions src/network/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,30 @@ impl fmt::Display for Protocol {
}
}

#[derive(Clone, Ord, PartialOrd, PartialEq, Eq, Hash, Debug, Copy)]
#[derive(Clone, Ord, PartialOrd, PartialEq, Eq, Hash, Copy)]
pub struct Socket {
pub ip: IpAddr,
pub port: u16,
}

#[derive(PartialEq, Hash, Eq, Clone, PartialOrd, Ord, Debug, Copy)]
impl fmt::Debug for Socket {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Socket { ip, port } = self;
match ip {
IpAddr::V4(v4) => write!(f, "{v4}:{port}"),
IpAddr::V6(v6) => write!(f, "[{v6}]:{port}"),
}
}
}

#[derive(PartialEq, Hash, Eq, Clone, PartialOrd, Ord, Copy)]
pub struct LocalSocket {
pub ip: IpAddr,
pub port: u16,
pub protocol: Protocol,
}

impl fmt::Display for LocalSocket {
impl fmt::Debug for LocalSocket {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let LocalSocket { ip, port, protocol } = self;
match ip {
Expand All @@ -53,12 +63,22 @@ impl fmt::Display for LocalSocket {
}
}

#[derive(PartialEq, Hash, Eq, Clone, PartialOrd, Ord, Debug, Copy)]
#[derive(PartialEq, Hash, Eq, Clone, PartialOrd, Ord, Copy)]
pub struct Connection {
pub remote_socket: Socket,
pub local_socket: LocalSocket,
}

impl fmt::Debug for Connection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Connection {
remote_socket,
local_socket,
} = self;
write!(f, "{local_socket:?} => {remote_socket:?}")
}
}

pub fn display_ip_or_host(ip: IpAddr, ip_to_host: &HashMap<IpAddr, String>) -> String {
match ip_to_host.get(&ip) {
Some(host) => host.clone(),
Expand Down

0 comments on commit b59b151

Please sign in to comment.