diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index ccead35..734f785 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -92,7 +92,7 @@ jobs: strategy: fail-fast: false matrix: - msrv: ["1.68"] + msrv: ["1.82"] steps: - uses: actions/checkout@v4 with: diff --git a/windivert-sys/Cargo.toml b/windivert-sys/Cargo.toml index ebe181b..5473f52 100644 --- a/windivert-sys/Cargo.toml +++ b/windivert-sys/Cargo.toml @@ -11,7 +11,7 @@ categories = ["external-ffi-bindings"] readme = "../README.md" license = "LGPL-3.0-or-later" edition = "2021" -rust-version = "1.68" +rust-version = "1.82" links = "WinDivert" build = "build/main.rs" @@ -24,10 +24,10 @@ vendored = [] static = ["vendored"] [dependencies] -thiserror = "1" +thiserror = "2" [dependencies.windows] -version = "0.48" +version = "0.62" features = [ "Win32_Foundation", "Win32_System_IO", diff --git a/windivert-sys/src/bindings/mod.rs b/windivert-sys/src/bindings/mod.rs index c299d10..2195658 100644 --- a/windivert-sys/src/bindings/mod.rs +++ b/windivert-sys/src/bindings/mod.rs @@ -15,8 +15,9 @@ pub use error::*; mod newtypes; pub use newtypes::*; +use windows::core::BOOL; use windows::Win32::{ - Foundation::{BOOL, HANDLE}, + Foundation::HANDLE, System::IO::OVERLAPPED, }; /// Default value for queue length parameter. diff --git a/windivert/Cargo.toml b/windivert/Cargo.toml index 5ca5012..5b21f9a 100644 --- a/windivert/Cargo.toml +++ b/windivert/Cargo.toml @@ -18,11 +18,11 @@ static = ["vendored", "windivert-sys/static"] [dependencies] etherparse = "0.13" -thiserror = "1" +thiserror = "2" windivert-sys = { version = "0.10.0", path = "../windivert-sys" } [dependencies.windows] -version = "0.48" +version = "0.62" features = [ "Devices_Custom", "Win32_Devices", diff --git a/windivert/src/divert/mod.rs b/windivert/src/divert/mod.rs index 66e65a9..90d4c0f 100644 --- a/windivert/src/divert/mod.rs +++ b/windivert/src/divert/mod.rs @@ -18,7 +18,7 @@ use windows::{ System::{ Services::{ CloseServiceHandle, ControlService, OpenSCManagerA, OpenServiceA, - SC_MANAGER_ALL_ACCESS, SERVICE_CONTROL_STOP, SERVICE_STATUS, + SC_MANAGER_ALL_ACCESS, SERVICE_CONTROL_STOP, }, Threading::{CreateEventA, TlsAlloc, TlsGetValue, TlsSetValue}, }, @@ -33,6 +33,11 @@ pub struct WinDivert { _layer: PhantomData, } +// SAFETY: HANDLE contains a *mut c_void (as of windows 0.58+) but represents +// a Win32 kernel handle which is safe to send and share across threads. +unsafe impl Send for WinDivert {} +unsafe impl Sync for WinDivert {} + /// Recv implementations impl WinDivert { /// Open a handle using the specified parameters. @@ -60,10 +65,10 @@ impl WinDivert { pub(crate) fn _get_event(tls_idx: u32) -> Result { let mut event = HANDLE::default(); unsafe { - event.0 = TlsGetValue(tls_idx) as isize; + event.0 = TlsGetValue(tls_idx); if event.is_invalid() { event = CreateEventA(None, false, false, None)?; - TlsSetValue(tls_idx, Some(event.0 as *mut c_void)); + TlsSetValue(tls_idx, Some(event.0 as *const c_void))?; } } Ok(event) @@ -189,26 +194,18 @@ impl WinDivert<()> { /// Method that tries to uninstall WinDivert driver. pub fn uninstall() -> WinResult<()> { - let status: *mut SERVICE_STATUS = MaybeUninit::uninit().as_mut_ptr(); + let mut status = MaybeUninit::uninit(); unsafe { + let status = status.as_mut_ptr(); let manager = OpenSCManagerA(None, None, SC_MANAGER_ALL_ACCESS)?; let service = OpenServiceA( manager, PCSTR::from_raw("WinDivert".as_ptr()), SC_MANAGER_ALL_ACCESS, )?; - let res = ControlService(service, SERVICE_CONTROL_STOP, status); - if !res.as_bool() { - return Err(WinError::from(GetLastError())); - } - let res = CloseServiceHandle(service); - if !res.as_bool() { - return Err(WinError::from(GetLastError())); - } - let res = CloseServiceHandle(manager); - if !res.as_bool() { - return Err(WinError::from(GetLastError())); - } + ControlService(service, SERVICE_CONTROL_STOP, status)?; + CloseServiceHandle(service)?; + CloseServiceHandle(manager)?; } Ok(()) } diff --git a/windivert/src/packet.rs b/windivert/src/packet.rs index ea81fe9..4cf8dc8 100644 --- a/windivert/src/packet.rs +++ b/windivert/src/packet.rs @@ -41,7 +41,7 @@ impl<'a> WinDivertPacket<'a, layer::NetworkLayer> { ) }; if !res.as_bool() { - return Err(WinDivertError::from(windows::core::Error::from_win32())); + return Err(WinDivertError::from(windows::core::Error::from_thread())); } } Ok(()) @@ -72,7 +72,7 @@ impl<'a> WinDivertPacket<'a, layer::ForwardLayer> { ) }; if !res.as_bool() { - return Err(WinDivertError::from(windows::core::Error::from_win32())); + return Err(WinDivertError::from(windows::core::Error::from_thread())); } } Ok(())