1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
use std::collections::HashMap;
use netstat2::*;
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};
use crate::network::{LocalSocket, Protocol};
use crate::OpenSockets;
pub(crate) fn get_open_sockets() -> OpenSockets {
let mut open_sockets = HashMap::new();
let mut sysinfo = System::new_all();
sysinfo.refresh_processes();
let af_flags = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;
let proto_flags = ProtocolFlags::TCP | ProtocolFlags::UDP;
let sockets_info = get_sockets_info(af_flags, proto_flags);
if let Ok(sockets_info) = sockets_info {
for si in sockets_info {
let mut procname = String::new();
for pid in si.associated_pids {
if let Some(process) = sysinfo.process(Pid::from_u32(pid)) {
procname = String::from(process.name());
break;
}
}
match si.protocol_socket_info {
ProtocolSocketInfo::Tcp(tcp_si) => {
open_sockets.insert(
LocalSocket {
ip: tcp_si.local_addr,
port: tcp_si.local_port,
protocol: Protocol::Tcp,
},
procname,
);
}
ProtocolSocketInfo::Udp(udp_si) => {
open_sockets.insert(
LocalSocket {
ip: udp_si.local_addr,
port: udp_si.local_port,
protocol: Protocol::Udp,
},
procname,
);
}
}
}
}
OpenSockets {
sockets_to_procs: open_sockets,
}
}
|