diff --git a/Cargo.lock b/Cargo.lock index a63b69b..181da6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,12 +11,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - [[package]] name = "bindgen" version = "0.65.1" @@ -36,7 +30,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.52", + "syn", "which", ] @@ -119,7 +113,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.52", + "syn", ] [[package]] @@ -193,7 +187,6 @@ version = "0.1.0" dependencies = [ "bsd-kvm", "byteorder", - "netstat2", "once_cell", "rustix", "sysctl", @@ -218,20 +211,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" -[[package]] -name = "netstat2" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0faa3f4ad230fd2bf2a5dad71476ecbaeaed904b3c7e7e5b1f266c415c03761f" -dependencies = [ - "bitflags 1.3.2", - "byteorder", - "libc", - "num-derive", - "num-traits", - "thiserror", -] - [[package]] name = "nom" version = "7.1.3" @@ -242,26 +221,6 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "num-derive" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "num-traits" -version = "0.2.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" -dependencies = [ - "autocfg", -] - [[package]] name = "once_cell" version = "1.19.0" @@ -281,7 +240,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a41cf62165e97c7f814d2221421dbb9afcbcdb0a88068e5ea206e19951c2cbb5" dependencies = [ "proc-macro2", - "syn 2.0.52", + "syn", ] [[package]] @@ -365,17 +324,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.52" @@ -418,7 +366,7 @@ checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" dependencies = [ "proc-macro2", "quote", - "syn 2.0.52", + "syn", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index e529221..929aa46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] - -#[target.'cfg(target_os = "windows")'.dependencies] -netstat2 = "0.9.1" +[target.'cfg(target_os = "windows")'.dependencies] windows = { version = "0.54.0", features = ["Win32_Foundation", "Win32_System_Diagnostics_ToolHelp"] } [target.'cfg(target_os = "macos")'.dependencies] diff --git a/src/lib.rs b/src/lib.rs index e820269..59f6e36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,6 @@ use std::net::SocketAddr; pub use platform::get_all; -// use netstat2::{get_sockets_info, AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo, TcpState}; - mod platform; type Result = std::result::Result>; @@ -13,32 +11,17 @@ type Result = std::result::Result>; #[derive(Eq, PartialEq, Hash, Debug)] pub struct Listener { /// The process ID of the listener process - pid: u32, + pub pid: u32, /// The name of the listener process - pname: String, + pub pname: String, /// The local socket this listener is listening on - socket: SocketAddr, + pub socket: SocketAddr, } impl Listener { fn new(pid: u32, pname: String, socket: SocketAddr) -> Self { Self { pid, pname, socket } } - - #[must_use] - pub fn pid(&self) -> u32 { - self.pid - } - - #[must_use] - pub fn pname(&self) -> &str { - &self.pname - } - - #[must_use] - pub fn socket(&self) -> &SocketAddr { - &self.socket - } } impl Display for Listener { @@ -50,66 +33,3 @@ impl Display for Listener { ) } } - -// #[must_use] -// pub fn get_all() -> Vec { -// get_with_filters( -// AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6, -// ProtocolFlags::TCP | ProtocolFlags::UDP, -// None, -// None, -// ) -// } -// -// #[must_use] -// pub fn get_for_nullnet(ip_addr: IpAddr) -> HashSet { -// get_with_filters( -// AddressFamilyFlags::IPV4, -// ProtocolFlags::TCP, -// Some(TcpState::Listen), -// Some(ip_addr), -// ) -// .iter() -// .map(|l| l.pname.clone()) -// .collect() -// } -// -// fn get_with_filters( -// af_flags: AddressFamilyFlags, -// proto_flags: ProtocolFlags, -// tcp_state: Option, -// ip_addr: Option, -// ) -> Vec { -// let mut listeners = Vec::new(); -// -// let sockets_info = get_sockets_info(af_flags, proto_flags).unwrap_or_default(); -// -// let mut add_listeners = |pids: Vec, ip: IpAddr, port: u16| { -// for pid in pids { -// if let Some(pname) = get_name_from_pid(pid) { -// if ip.is_unspecified() || ip_addr.is_none() || ip_addr.unwrap() == ip { -// listeners.push(Listener { -// pid, -// pname, -// socket: SocketAddr::new(ip, port), -// }); -// } -// } -// } -// }; -// -// for si in sockets_info { -// match si.protocol_socket_info { -// ProtocolSocketInfo::Tcp(tcp_si) => { -// if tcp_state.is_none() || tcp_si.state == tcp_state.unwrap() { -// add_listeners(si.associated_pids, tcp_si.local_addr, tcp_si.local_port); -// } -// } -// ProtocolSocketInfo::Udp(udp_si) => { -// add_listeners(si.associated_pids, udp_si.local_addr, udp_si.local_port); -// } -// } -// } -// -// listeners -// } diff --git a/src/main.rs b/src/main.rs index 1723d09..a5c7248 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ fn main() { - let listeners = listeners::get_all().unwrap(); - for listener in listeners { + for listener in listeners::get_all().iter().flatten() { println!("{listener}"); } } diff --git a/src/platform/macos/mod.rs b/src/platform/macos/mod.rs index 27b5678..82b52e8 100644 --- a/src/platform/macos/mod.rs +++ b/src/platform/macos/mod.rs @@ -22,9 +22,10 @@ pub fn get_all() -> crate::Result> { for pid in ProcPid::get_all().into_iter().flatten() { for fd in SocketFd::get_all_of_pid(pid).iter().flatten() { if let Ok(tcp_listener) = TcpListener::from_pid_fd(pid, fd) { - let ProcName(name) = ProcName::from_pid(pid).unwrap_or_default(); - let listener = Listener::new(pid.as_u_32()?, name, tcp_listener.socket_addr()); - listeners.insert(listener); + if let Ok(ProcName(name)) = ProcName::from_pid(pid) { + let listener = Listener::new(pid.as_u_32()?, name, tcp_listener.socket_addr()); + listeners.insert(listener); + } } } } diff --git a/src/platform/windows/tcp_listener.rs b/src/platform/windows/tcp_listener.rs index bf9735f..68fa3bd 100644 --- a/src/platform/windows/tcp_listener.rs +++ b/src/platform/windows/tcp_listener.rs @@ -2,7 +2,13 @@ use crate::platform::windows::socket_table::SocketTable; use crate::platform::windows::tcp6_table::Tcp6Table; use crate::platform::windows::tcp_table::TcpTable; use crate::Listener; +use std::mem::size_of; +use std::mem::zeroed; use std::net::{IpAddr, SocketAddr}; +use windows::Win32::Foundation::CloseHandle; +use windows::Win32::System::Diagnostics::ToolHelp::{ + CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32, TH32CS_SNAPPROCESS, +}; #[derive(Debug)] pub(super) struct TcpListener { @@ -40,17 +46,9 @@ impl TcpListener { } fn pname(&self) -> Option { - use std::mem::size_of; - use std::mem::zeroed; - use windows::Win32::Foundation::CloseHandle; - use windows::Win32::System::Diagnostics::ToolHelp::{ - CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32, - TH32CS_SNAPPROCESS, - }; - let pid = self.pid; - let h = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0).unwrap() }; + let h = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)? }; let mut process = unsafe { zeroed::() }; process.dwSize = size_of::() as u32; @@ -68,16 +66,14 @@ impl TcpListener { } } - unsafe { CloseHandle(h).unwrap() }; + unsafe { CloseHandle(h)? }; let name = process.szExeFile; - let mut temp: Vec = vec![]; - let len = name.iter().position(|&x| x == 0).unwrap(); + let len = name.iter().position(|&x| x == 0)?; - for i in name.iter() { - temp.push(*i as u8); - } - Some(String::from_utf8(temp[0..len].to_vec()).unwrap_or_default()) + Some(String::from_utf8( + name[0..len].iter().map(|e| *e as u8).collect(), + )?) } pub(super) fn to_listener(&self) -> Option {