From 2284e91d6e7a625307ec4db05e650ba9c10c8650 Mon Sep 17 00:00:00 2001 From: Maksim Bondarenkov Date: Tue, 30 Jul 2024 10:08:22 +0300 Subject: [PATCH] use windows-sys instead of winapi --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/columns/tcp_port.rs | 18 +++++------ src/columns/usage_mem.rs | 2 +- src/process/windows.rs | 65 ++++++++++++++++++++-------------------- 5 files changed, 43 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9381e39ca..1974514b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1719,7 +1719,7 @@ dependencies = [ "unicode-width", "uzers", "which 6.0.2", - "winapi", + "windows-sys 0.52.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index adfb76b8c..661cca3af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,7 @@ uzers = "0.12" which = "6" [target.'cfg(target_os = "windows")'.dependencies] -winapi = { version = "0.3", features = ["handleapi", "minwindef", "psapi", "securitybaseapi", "tlhelp32", "winbase", "winnt", "iphlpapi"] } +windows-sys = { version = "0.52", features = ["Win32_Foundation", "Win32_Networking_WinSock", "Win32_NetworkManagement_IpHelper", "Win32_Security", "Win32_System_Diagnostics_ToolHelp", "Win32_System_ProcessStatus", "Win32_System_Threading"] } [target.'cfg(target_os = "freebsd")'.dependencies] bsd-kvm = "0.1.5" diff --git a/src/columns/tcp_port.rs b/src/columns/tcp_port.rs index 5593ff965..f5fdce9dc 100644 --- a/src/columns/tcp_port.rs +++ b/src/columns/tcp_port.rs @@ -11,15 +11,11 @@ use std::collections::HashMap; #[cfg(target_os = "windows")] use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; #[cfg(target_os = "windows")] -use winapi::shared::tcpmib::MIB_TCP_STATE; +use windows_sys::Win32::NetworkManagement::IpHelper::{GetTcpTable2, GetTcp6Table2, MIB_TCP_STATE, MIB_TCPTABLE2, MIB_TCP_STATE_LISTEN, MIB_TCP6TABLE2}; #[cfg(target_os = "windows")] -use winapi::shared::tcpmib::{MIB_TCPTABLE2, MIB_TCP_STATE_LISTEN}; +use windows_sys::Win32::Foundation::{ERROR_INSUFFICIENT_BUFFER, NO_ERROR}; #[cfg(target_os = "windows")] -use winapi::shared::winerror::{ERROR_INSUFFICIENT_BUFFER, NO_ERROR}; -#[cfg(target_os = "windows")] -use winapi::um::{iphlpapi::GetTcpTable2, winsock2::ntohl, winsock2::ntohs}; -#[cfg(target_os = "windows")] -use winapi::{shared::tcpmib::MIB_TCP6TABLE2, um::iphlpapi::GetTcp6Table2}; +use windows_sys::Win32::Networking::WinSock::{ntohl, ntohs}; pub struct TcpPort { header: String, @@ -250,7 +246,7 @@ fn get_tcp_entry_list() -> Result, anyhow::Error> { unsafe { ntohs(entry.dwRemotePort as u16) }, )), pid: entry.dwOwningPid, - state: entry.dwState, + state: entry.dwState as i32, }); } @@ -284,13 +280,13 @@ fn get_tcp6_entry_list() -> Result, anyhow::Error> { let entry = unsafe { *tcp_table.table.as_ptr().add(i as usize) }; entry_list.push(TcpNetEntry { local_address: SocketAddr::V6(SocketAddrV6::new( - Ipv6Addr::from(unsafe { *entry.LocalAddr.u.Byte() }), + Ipv6Addr::from(u128::from_be_bytes(unsafe { entry.LocalAddr.u.Byte })), unsafe { ntohs(entry.dwLocalPort as u16) }, 0, entry.dwLocalScopeId, )), remote_address: SocketAddr::V6(SocketAddrV6::new( - Ipv6Addr::from(unsafe { *entry.RemoteAddr.u.Byte() }), + Ipv6Addr::from(u128::from_be_bytes(unsafe { entry.RemoteAddr.u.Byte })), unsafe { ntohs(entry.dwRemotePort as u16) }, 0, entry.dwRemoteScopeId, @@ -308,7 +304,7 @@ fn get_tcp6_entry_list() -> Result, anyhow::Error> { mod tests { use std::net::TcpListener; - use winapi::shared::tcpmib::MIB_TCP_STATE_LISTEN; + use windows_sys::Win32::NetworkManagement::IpHelper::MIB_TCP_STATE_LISTEN; use super::TcpPort; diff --git a/src/columns/usage_mem.rs b/src/columns/usage_mem.rs index 6a0800c73..8b30df9f7 100644 --- a/src/columns/usage_mem.rs +++ b/src/columns/usage_mem.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; #[cfg(target_os = "windows")] use std::mem::{size_of, zeroed}; #[cfg(target_os = "windows")] -use winapi::um::psapi::{GetPerformanceInfo, PERFORMANCE_INFORMATION}; +use windows_sys::Win32::System::ProcessStatus::{GetPerformanceInfo, PERFORMANCE_INFORMATION}; pub struct UsageMem { header: String, diff --git a/src/process/windows.rs b/src/process/windows.rs index 9c4af67ae..8af8d0742 100644 --- a/src/process/windows.rs +++ b/src/process/windows.rs @@ -2,29 +2,29 @@ use chrono::offset::TimeZone; use chrono::{Local, NaiveDate}; use std::cell::RefCell; use std::collections::HashMap; +use std::ffi::c_void; use std::mem::{size_of, zeroed, MaybeUninit}; use std::ptr; use std::thread; use std::time::{Duration, Instant}; -use winapi::ctypes::c_void; -use winapi::shared::minwindef::{DWORD, FALSE, FILETIME, MAX_PATH}; -use winapi::um::handleapi::CloseHandle; -use winapi::um::processthreadsapi::{ - GetCurrentProcess, GetPriorityClass, GetProcessTimes, OpenProcess, OpenProcessToken, +use windows_sys::Win32::Foundation::{ + CloseHandle, FALSE, FILETIME, HANDLE, HMODULE, MAX_PATH, PSID, }; -use winapi::um::psapi::{ - EnumProcessModulesEx, GetModuleBaseNameW, GetProcessMemoryInfo, K32EnumProcesses, - LIST_MODULES_ALL, PROCESS_MEMORY_COUNTERS, PROCESS_MEMORY_COUNTERS_EX, +use windows_sys::Win32::Security::{ + AdjustTokenPrivileges, GetTokenInformation, LookupAccountSidW, LookupPrivilegeValueW, + TokenGroups, TokenUser, SE_DEBUG_NAME, SE_PRIVILEGE_ENABLED, SID, TOKEN_ADJUST_PRIVILEGES, + TOKEN_GROUPS, TOKEN_PRIVILEGES, TOKEN_QUERY, TOKEN_USER, }; -use winapi::um::securitybaseapi::{AdjustTokenPrivileges, GetTokenInformation}; -use winapi::um::tlhelp32::{ +use windows_sys::Win32::System::Diagnostics::ToolHelp::{ CreateToolhelp32Snapshot, Process32First, Process32Next, PROCESSENTRY32, TH32CS_SNAPPROCESS, }; -use winapi::um::winbase::{GetProcessIoCounters, LookupAccountSidW, LookupPrivilegeValueW}; -use winapi::um::winnt::{ - TokenGroups, TokenUser, HANDLE, IO_COUNTERS, PROCESS_QUERY_INFORMATION, PROCESS_VM_READ, PSID, - SE_DEBUG_NAME, SE_PRIVILEGE_ENABLED, SID, TOKEN_ADJUST_PRIVILEGES, TOKEN_GROUPS, - TOKEN_PRIVILEGES, TOKEN_QUERY, TOKEN_USER, +use windows_sys::Win32::System::ProcessStatus::{ + EnumProcessModulesEx, GetModuleBaseNameW, GetProcessMemoryInfo, K32EnumProcesses, + LIST_MODULES_ALL, PROCESS_MEMORY_COUNTERS, PROCESS_MEMORY_COUNTERS_EX, +}; +use windows_sys::Win32::System::Threading::{ + GetCurrentProcess, GetPriorityClass, GetProcessIoCounters, GetProcessTimes, OpenProcess, + OpenProcessToken, IO_COUNTERS, PROCESS_QUERY_INFORMATION, PROCESS_VM_READ, }; pub struct ProcessInfo { @@ -211,7 +211,9 @@ fn set_privilege() -> bool { } let mut tps: TOKEN_PRIVILEGES = unsafe { zeroed() }; - let se_debug_name: Vec = format!("{}\0", SE_DEBUG_NAME).encode_utf16().collect(); + let se_debug_name: Vec = format!("{}\0", unsafe { *SE_DEBUG_NAME }) + .encode_utf16() + .collect(); tps.PrivilegeCount = 1; let ret = unsafe { LookupPrivilegeValueW( @@ -229,7 +231,7 @@ fn set_privilege() -> bool { AdjustTokenPrivileges( token, FALSE, - &mut tps, + &tps as *const _, 0, ptr::null::() as *mut TOKEN_PRIVILEGES, ptr::null::() as *mut u32, @@ -243,22 +245,22 @@ fn set_privilege() -> bool { } fn get_pids() -> Vec { - let dword_size = size_of::(); - let mut pids: Vec = Vec::with_capacity(10192); + let dword_size = size_of::(); + let mut pids = Vec::with_capacity(10192); let mut cb_needed = 0; unsafe { pids.set_len(10192) }; let result = unsafe { K32EnumProcesses( pids.as_mut_ptr(), - (dword_size * pids.len()) as DWORD, + (dword_size * pids.len()) as u32, &mut cb_needed, ) }; if result == 0 { return Vec::new(); } - let pids_len = cb_needed / dword_size as DWORD; + let pids_len = cb_needed / dword_size as u32; unsafe { pids.set_len(pids_len as usize) }; pids.iter().map(|x| *x as i32).collect() @@ -293,11 +295,11 @@ fn get_handle(pid: i32) -> Option { OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, - pid as DWORD, + pid as u32, ) }; - if handle.is_null() { + if handle == 0 { None } else { Some(handle) @@ -339,7 +341,7 @@ fn get_memory_info(handle: HANDLE) -> Option { handle, &mut pmc as *mut PROCESS_MEMORY_COUNTERS_EX as *mut c_void as *mut PROCESS_MEMORY_COUNTERS, - size_of::() as DWORD, + size_of::() as u32, ) }; @@ -363,15 +365,15 @@ fn get_memory_info(handle: HANDLE) -> Option { } fn get_command(handle: HANDLE) -> Option { - let mut exe_buf = [0u16; MAX_PATH + 1]; - let mut h_mod = std::ptr::null_mut(); + let mut exe_buf = [0u16; MAX_PATH as usize + 1]; + let h_mod: HMODULE = 0; let mut cb_needed = 0; let ret = unsafe { EnumProcessModulesEx( handle, - &mut h_mod, - size_of::() as DWORD, + h_mod as *mut HMODULE, + size_of::() as u32, &mut cb_needed, LIST_MODULES_ALL, ) @@ -380,8 +382,7 @@ fn get_command(handle: HANDLE) -> Option { return None; } - let ret = - unsafe { GetModuleBaseNameW(handle, h_mod, exe_buf.as_mut_ptr(), MAX_PATH as DWORD + 1) }; + let ret = unsafe { GetModuleBaseNameW(handle, h_mod, exe_buf.as_mut_ptr(), MAX_PATH + 1) }; let mut pos = 0; for x in exe_buf.iter() { @@ -599,9 +600,9 @@ fn get_name(psid: PSID) -> Option<(String, String)> { let ret = LookupAccountSidW( ptr::null::() as *mut u16, psid, - name.as_mut_ptr() as *mut u16, + name.as_mut_ptr(), &mut cc_name, - domainname.as_mut_ptr() as *mut u16, + domainname.as_mut_ptr(), &mut cc_domainname, &mut pe_use, );