From 52613d952fae8080002e230788f25ae8ec4139ee Mon Sep 17 00:00:00 2001 From: Jan Danielsson Date: Mon, 16 Dec 2024 19:38:27 +0100 Subject: [PATCH 1/8] Make docs.rs source view generate links to definitions. --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index d59aadbe..d4510723 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,3 +108,4 @@ required-features = ["capture-stream"] [package.metadata.docs.rs] all-features = true +rustdoc-args = ["--generate-link-to-definition"] From 36138ca21b4767d0212f52e994e074de0870aac3 Mon Sep 17 00:00:00 2001 From: Jan Danielsson Date: Mon, 16 Dec 2024 20:04:57 +0100 Subject: [PATCH 2/8] Disable default features for eui48 crate so it will no longer pull in rustc-serialize, which has had a history of causing builds to print deprecation warnings. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d4510723..5622d774 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ mockall = "0.11.4" tempfile = "3.10" [target.'cfg(target_os = "windows")'.dev-dependencies] -eui48 = "1.1" +eui48 = { version = "1.1", default-features = false } [target.'cfg(not(target_os = "windows"))'.dev-dependencies] tun-tap = "0.1.3" From 805da286216fd4640e16df47286282e334997ad7 Mon Sep 17 00:00:00 2001 From: Jan Danielsson Date: Mon, 16 Dec 2024 22:32:04 +0100 Subject: [PATCH 3/8] Revert "Disable default features for eui48 crate so it will no longer pull in rustc-serialize, which has had a history of causing builds to print deprecation warnings." This reverts commit 36138ca21b4767d0212f52e994e074de0870aac3. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5622d774..d4510723 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ mockall = "0.11.4" tempfile = "3.10" [target.'cfg(target_os = "windows")'.dev-dependencies] -eui48 = { version = "1.1", default-features = false } +eui48 = "1.1" [target.'cfg(not(target_os = "windows"))'.dev-dependencies] tun-tap = "0.1.3" From d11c6266ddd3b9e667c7aa33baea5bacee4cbc50 Mon Sep 17 00:00:00 2001 From: Jan Danielsson Date: Tue, 17 Dec 2024 20:58:32 +0100 Subject: [PATCH 4/8] Use windows-sys 0.59.0. --- Cargo.toml | 2 +- src/capture/mod.rs | 4 ++-- src/device.rs | 6 +++--- src/stream/windows.rs | 10 ++++++++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d59aadbe..f857f312 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ futures = { version = "0.3", optional = true } gat-std = { version = "0.1.1", optional = true } [target.'cfg(target_os = "windows")'.dependencies] -windows-sys = { version = "0.36.1", features = ["Win32_Foundation", "Win32_Networking_WinSock"] } +windows-sys = { version = "0.59.0", features = ["Win32_Foundation", "Win32_Networking_WinSock"] } [dev-dependencies] etherparse = "0.13.0" diff --git a/src/capture/mod.rs b/src/capture/mod.rs index 1e67a4a3..37669b62 100644 --- a/src/capture/mod.rs +++ b/src/capture/mod.rs @@ -276,10 +276,10 @@ mod tests { let ctx = raw::pcap_getevent_context(); ctx.expect() .withf_st(move |arg1| *arg1 == pcap) - .return_once(|_| 5); + .return_once(|_| 5 as *mut std::ffi::c_void); let handle = unsafe { capture.get_event() }; - assert_eq!(handle, 5); + assert_eq!(handle, 5 as *mut std::ffi::c_void); } #[test] diff --git a/src/device.rs b/src/device.rs index 561b11fd..60a748b2 100644 --- a/src/device.rs +++ b/src/device.rs @@ -266,7 +266,7 @@ impl Address { return None; } - match (*ptr).sa_family as u32 { + match (*ptr).sa_family { WinSock::AF_INET => { let ptr: *const WinSock::SOCKADDR_IN = std::mem::transmute(ptr); let addr: [u8; 4] = ((*ptr).sin_addr.S_un.S_addr).to_ne_bytes(); @@ -372,7 +372,7 @@ mod tests { fn new() -> Self { let mut addr: Self = unsafe { std::mem::zeroed() }; // The cast is only necessary due to a bug in windows_sys@v0.36.1 - addr.sin_family = WinSock::AF_INET as u16; + addr.sin_family = WinSock::AF_INET; addr } @@ -418,7 +418,7 @@ mod tests { fn new() -> Self { let mut addr: Self = unsafe { std::mem::zeroed() }; // The cast is only necessary due to a bug in windows_sys@v0.36.1 - addr.sin6_family = WinSock::AF_INET6 as u16; + addr.sin6_family = WinSock::AF_INET6; unsafe { addr.sin6_addr.u.Byte[0] = 0xFE; addr.sin6_addr.u.Byte[1] = 0x80; diff --git a/src/stream/windows.rs b/src/stream/windows.rs index 11656c8f..454da65f 100644 --- a/src/stream/windows.rs +++ b/src/stream/windows.rs @@ -73,6 +73,11 @@ struct EventHandle { state: EventHandleState, } +/// Newtype used to wrap `HANDLE` to make it `Send`:able +struct InternalHandle(HANDLE); + +unsafe impl Send for InternalHandle {} + enum EventHandleState { /// We haven't started waiting for an event yet. Init, @@ -98,12 +103,13 @@ impl EventHandle { loop { match self.state { EventHandleState::Init => { - let handle = self.handle; + let handle = InternalHandle(self.handle); self.state = EventHandleState::Polling(tokio::task::spawn_blocking(move || { const INFINITE: u32 = !0; + let handle = handle; // avoid partial closure capture problems unsafe { - WaitForSingleObject(handle, INFINITE); + WaitForSingleObject(handle.0, INFINITE); } })); } From 5b00a9968aac0ef0b6c968afb1b9e9ac38923736 Mon Sep 17 00:00:00 2001 From: Jan Danielsson Date: Tue, 17 Dec 2024 22:35:13 +0100 Subject: [PATCH 5/8] Mark EventHandle explicitly as Send. It was made !Send as a side-effect of HANDLE being a [non-autotrait'ed] pointer in windows-sys 0.59.0. This led to the public PacketStream type to become !Send. --- src/stream/windows.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/stream/windows.rs b/src/stream/windows.rs index 454da65f..41b1c67c 100644 --- a/src/stream/windows.rs +++ b/src/stream/windows.rs @@ -73,6 +73,11 @@ struct EventHandle { state: EventHandleState, } +// SAFETY: EventHandle is Send automatically if not for HANDLE, which is a [non-autotrait'ed] pointer since windows-sys was updated to 0.59. +// The capture device owns the original handle, +// As long as the capture device isn't released before the EventHandle, this should be good. +unsafe impl Send for EventHandle {} + /// Newtype used to wrap `HANDLE` to make it `Send`:able struct InternalHandle(HANDLE); From a134848b47496b748646cb026f211a9d83077acf Mon Sep 17 00:00:00 2001 From: Jan Danielsson Date: Tue, 17 Dec 2024 22:52:41 +0100 Subject: [PATCH 6/8] Fix windows-sys version for dev-depedencies on Windows. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9720bc9e..0ecb22ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ tempfile = "3.10" [target.'cfg(target_os = "windows")'.dev-dependencies] eui48 = { version = "1.1", default-features = false } -windows-sys = { version = "0.36.1", features = ["Win32_System_Threading"] } +windows-sys = { version = "0.59.0", features = ["Win32_System_Threading"] } [target.'cfg(not(target_os = "windows"))'.dev-dependencies] tun-tap = "0.1.3" From 05271294f88b3102bee295f4c6a63f71f3f44061 Mon Sep 17 00:00:00 2001 From: Jan Danielsson Date: Fri, 20 Dec 2024 11:07:30 +0100 Subject: [PATCH 7/8] Rephrase safety comment to avoid talking about the history of the windows-sys crate. --- src/stream/windows.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/stream/windows.rs b/src/stream/windows.rs index 41b1c67c..88a57ab8 100644 --- a/src/stream/windows.rs +++ b/src/stream/windows.rs @@ -73,9 +73,10 @@ struct EventHandle { state: EventHandleState, } -// SAFETY: EventHandle is Send automatically if not for HANDLE, which is a [non-autotrait'ed] pointer since windows-sys was updated to 0.59. -// The capture device owns the original handle, -// As long as the capture device isn't released before the EventHandle, this should be good. +// SAFETY: EventHandle needs to be Send because it's passed over a thread boundary. +// It contains a HANDLE that is a raw pointer, so the Send autotrait doesn't apply. +// The capture device owns the original handle; as long as the capture device isn't +// released before the EventHandle, this should be good. unsafe impl Send for EventHandle {} /// Newtype used to wrap `HANDLE` to make it `Send`:able From c53e97c4396b462ff1458369e84ef6258d58cbfc Mon Sep 17 00:00:00 2001 From: Jan Danielsson Date: Fri, 20 Dec 2024 12:03:02 +0100 Subject: [PATCH 8/8] Note the update of the windows-sys crate in the changelog. Add a special note about HANDLE changes in windows-sys. --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36b76047..d4b7bba9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## [Unreleased] +### Changed + +- Updated `windows-sys` from `0.36.1` to `0.59.0`. + Users of `Capture::get_event()` on Windows platforms should note that + `HANDLE` is a `isize` in `0.36`, but has been made a pointer in `0.59`, + causing types containing it to no longer be autotraited with `Send`. + ## [2.2.0] - 2024-09-01 ### Added