Skip to content

Commit 7dbd371

Browse files
authored
Fix IPv6 socket detect logic (#383)
* Fix IPv6 socket detect logic * Write changelog
1 parent 451daea commit 7dbd371

File tree

2 files changed

+17
-33
lines changed

2 files changed

+17
-33
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1111
* CI: use GitHub API to exempt dependabot from changelog requirement #378 - @cyqsimon
1212
* Remove unnecessary logging synchronisation #381 - @cyqsimon
1313
* Apply suggestions from new clippy lint clippy::assigning_clones #382 - @cyqsimon
14+
* Fix IPv6 socket detect logic #383 - @cyqsimon
1415

1516
## Added
1617
* CI: include generated assets in release archive #359 - @cyqsimon

src/os/linux.rs

+16-33
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010

1111
pub(crate) fn get_open_sockets() -> OpenSockets {
1212
let mut open_sockets = HashMap::new();
13-
let mut inode_to_procname = HashMap::new();
13+
let mut inode_to_proc = HashMap::new();
1414

1515
if let Ok(all_procs) = procfs::process::all_processes() {
1616
for process in all_procs.filter_map(|res| res.ok()) {
@@ -20,47 +20,30 @@ pub(crate) fn get_open_sockets() -> OpenSockets {
2020
let proc_info = ProcessInfo::new(&proc_name, stat.pid as u32);
2121
for fd in fds.filter_map(|res| res.ok()) {
2222
if let FDTarget::Socket(inode) = fd.target {
23-
inode_to_procname.insert(inode, proc_info.clone());
23+
inode_to_proc.insert(inode, proc_info.clone());
2424
}
2525
}
2626
}
2727
}
2828

29-
if let Ok(mut tcp) = procfs::net::tcp() {
30-
if let Ok(mut tcp6) = procfs::net::tcp6() {
31-
tcp.append(&mut tcp6);
32-
}
33-
for entry in tcp.into_iter() {
34-
if let Some(proc_info) = inode_to_procname.get(&entry.inode) {
35-
open_sockets.insert(
36-
LocalSocket {
29+
macro_rules! insert_proto {
30+
($source: expr, $proto: expr) => {
31+
let entries = $source.into_iter().filter_map(|res| res.ok()).flatten();
32+
for entry in entries {
33+
if let Some(proc_info) = inode_to_proc.get(&entry.inode) {
34+
let socket = LocalSocket {
3735
ip: entry.local_address.ip(),
3836
port: entry.local_address.port(),
39-
protocol: Protocol::Tcp,
40-
},
41-
proc_info.clone(),
42-
);
43-
};
44-
}
37+
protocol: $proto,
38+
};
39+
open_sockets.insert(socket, proc_info.clone());
40+
}
41+
}
42+
};
4543
}
4644

47-
if let Ok(mut udp) = procfs::net::udp() {
48-
if let Ok(mut udp6) = procfs::net::udp6() {
49-
udp.append(&mut udp6);
50-
}
51-
for entry in udp.into_iter() {
52-
if let Some(proc_info) = inode_to_procname.get(&entry.inode) {
53-
open_sockets.insert(
54-
LocalSocket {
55-
ip: entry.local_address.ip(),
56-
port: entry.local_address.port(),
57-
protocol: Protocol::Udp,
58-
},
59-
proc_info.clone(),
60-
);
61-
};
62-
}
63-
}
45+
insert_proto!([procfs::net::tcp(), procfs::net::tcp6()], Protocol::Tcp);
46+
insert_proto!([procfs::net::udp(), procfs::net::udp6()], Protocol::Udp);
6447

6548
OpenSockets {
6649
sockets_to_procs: open_sockets,

0 commit comments

Comments
 (0)