Skip to content

Commit 3577011

Browse files
committed
Print duplicity when deduplicating
1 parent 515a4fd commit 3577011

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/main.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use netlink::{
1212
};
1313
use procfs::process::all_processes;
1414
use std::{
15-
collections::{BTreeMap, BTreeSet, HashMap},
15+
collections::{BTreeMap, HashMap},
1616
env::var_os,
1717
io::{stdout, BufWriter, Write},
1818
net::{IpAddr, Ipv4Addr, Ipv6Addr},
@@ -184,16 +184,25 @@ fn sockets_tree<'a>(
184184
{
185185
sout.leaf("0.0.0.0 + ::".into());
186186
} else {
187-
let socks = socks
187+
let fields = |sock: &SockInfo<'a>| (sock.family, sock.iface, sock.addr);
188+
let mut counts = socks
188189
.iter()
189190
.filter(|sock| filter.accept_addr(sock.addr))
190-
.collect::<BTreeSet<_>>();
191+
.counts_by(|sock| fields(sock));
191192
for sock in socks {
192-
match (sock.family, sock.iface) {
193-
(Family::Both, _) => sout.leaf("*".into()),
194-
(_, Some(ifname)) => sout.leaf(format!("{} ({ifname})", sock.addr)),
195-
_ => sout.leaf(format!("{}", sock.addr)),
196-
};
193+
let count = counts.remove(&fields(&*sock));
194+
if let Some(count) = count {
195+
let disp = match (sock.family, sock.iface) {
196+
(Family::Both, _) => "*".into(),
197+
(_, Some(ifname)) => format!("{} ({ifname})", sock.addr),
198+
_ => format!("{}", sock.addr),
199+
};
200+
let duplicity = match count {
201+
1 => "".into(),
202+
_ => format!(" (×{count})"),
203+
};
204+
sout.leaf(format!("{disp}{duplicity}"));
205+
}
197206
}
198207
}
199208
if filter.accept_port(port) && filter.accept_proto(proto) {

src/netlink/sock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn all_sockets<'i>(
6868
Ok(ret)
6969
}
7070

71-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
71+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7272
pub enum Family {
7373
V4,
7474
V6,

0 commit comments

Comments
 (0)