Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

## [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`.

### Added

- Added `packet_header_size()` for applications that need to know the size of internal type
`pcap_pkthdr` (for instance to calculate exact send queue sizes).

want to precalculate exact queue sizes.

## [2.2.0] - 2024-09-01

### Added
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/capture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,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]
Expand Down
6 changes: 3 additions & 3 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 [email protected]
addr.sin_family = WinSock::AF_INET as u16;
addr.sin_family = WinSock::AF_INET;
addr
}

Expand Down Expand Up @@ -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 [email protected]
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;
Expand Down
16 changes: 14 additions & 2 deletions src/stream/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ struct EventHandle {
state: EventHandleState,
}

// 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
struct InternalHandle(HANDLE);

unsafe impl Send for InternalHandle {}

enum EventHandleState {
/// We haven't started waiting for an event yet.
Init,
Expand All @@ -98,12 +109,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);
}
}));
}
Expand Down
Loading