Skip to content

Commit

Permalink
Update dependencies to latest versions and adapt code to match update…
Browse files Browse the repository at this point in the history
…d Windows API prototypes. (#13)
  • Loading branch information
wiresock authored Aug 30, 2023
1 parent ac36764 commit 51df362
Show file tree
Hide file tree
Showing 20 changed files with 382 additions and 600 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ndisapi"
version = "0.5.1"
version = "0.5.2"
edition = "2021"
authors = ["Vadim Smirnov <vadim@ntkernel.com>"]
description = "Rust crate for interacting with the Windows Packet Filter driver (NDISAPI)"
Expand All @@ -18,7 +18,7 @@ uuid = { version = "1.3", features = ["v4", "serde", "v6"] }
ipnetwork = "0.20.0"

[dependencies.windows]
version = "0.48.0"
version = "0.51.1"
features = [
"Win32_Foundation",
"Win32_Security",
Expand All @@ -37,5 +37,5 @@ clap = {version = "4.0.32", features = ["derive"]}
ctrlc = "3.2.4"
futures = "0.3.28"
tokio = { version = "1.28.2", features = ["full"] }
smoltcp = "0.9.1"
smoltcp = "0.10.0"

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Add the following to your `Cargo.toml` file:

```toml
[dependencies]
ndisapi = "0.5.1"
ndisapi = "0.5.2"
```

## Usage
Expand Down Expand Up @@ -67,8 +67,8 @@ For more examples and in-depth usage, check out the [documentation](https://docs
Here is an example of how to run the `listadapters` example:

```bash
PS D:\firezone\ndisapi> cargo run --example listadapters
Compiling ndisapi v0.5.1 (D:\firezone\ndisapi)
PS D:\github.com\ndisapi-rs> cargo run --example listadapters
Compiling ndisapi v0.5.2 (D:\github.com\ndisapi-rs)
Finished dev [unoptimized + debuginfo] target(s) in 3.22s
Running `target\debug\examples\listadapters.exe`
Detected Windows Packet Filter version 3.4.3
Expand Down Expand Up @@ -104,7 +104,7 @@ Getting OID_GEN_CURRENT_PACKET_FILTER Error: Data error (cyclic redundancy check
Following is the demonstration of the async-packthru example. For this scenario, we will assume that `vEthernet (WLAN Virtual Switch)` is the default internet connection

```bash
PS D:\firezone\ndisapi> cargo run --example async-packthru -- --interface-index 12
PS D:\github.com\ndisapi-rs> cargo run --example async-packthru -- --interface-index 12
Finished dev [unoptimized + debuginfo] target(s) in 0.11s
Running `target\debug\examples\async-packthru.exe --interface-index 12`
Detected Windows Packet Filter version 3.4.3
Expand Down
16 changes: 7 additions & 9 deletions examples/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,7 @@ fn main() -> Result<()> {
// set atomic flag to exit the loop
ctrlc_pressed.store(true, Ordering::SeqCst);
// signal an event to release the loop if there are no packets in the queue
unsafe {
SetEvent(event);
}
let _ = unsafe { SetEvent(event) };
})
.expect("Error setting Ctrl-C handler");

Expand Down Expand Up @@ -707,9 +705,9 @@ fn main() -> Result<()> {
}
}

unsafe {
ResetEvent(event); // Reset the event to continue waiting for packets to arrive.
}
let _ = unsafe {
ResetEvent(event) // Reset the event to continue waiting for packets to arrive.
};
}

// Put the network interface into default mode.
Expand All @@ -718,9 +716,9 @@ fn main() -> Result<()> {
FilterFlags::default(),
)?;

unsafe {
CloseHandle(event); // Close the event handle.
}
let _ = unsafe {
CloseHandle(event) // Close the event handle.
};

Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions examples/packthru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ fn main() -> Result<()> {
}
}

unsafe {
ResetEvent(event); // Reset the event to continue waiting for packets to arrive.
}
let _ = unsafe {
ResetEvent(event) // Reset the event to continue waiting for packets to arrive.
};
}

// Put the network interface into default mode.
Expand All @@ -173,9 +173,9 @@ fn main() -> Result<()> {
FilterFlags::default(),
)?;

unsafe {
CloseHandle(event); // Close the event handle.
}
let _ = unsafe {
CloseHandle(event) // Close the event handle.
};

Ok(())
}
Expand Down
12 changes: 6 additions & 6 deletions examples/passthru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ fn main() -> Result<()> {
}
}

unsafe {
ResetEvent(event); // Reset the event to continue waiting for packets to arrive.
}
let _ = unsafe {
ResetEvent(event) // Reset the event to continue waiting for packets to arrive.
};
}

// Put the network interface into default mode.
Expand All @@ -147,9 +147,9 @@ fn main() -> Result<()> {
FilterFlags::default(),
)?;

unsafe {
CloseHandle(event); // Close the event handle.
}
let _ = unsafe {
CloseHandle(event) // Close the event handle.
};

// Return the result.
Ok(())
Expand Down
50 changes: 17 additions & 33 deletions src/async_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::sync::Arc;
use windows::{
core::Result,
Win32::{
Foundation::{GetLastError, HANDLE, WIN32_ERROR},
Foundation::{HANDLE, WIN32_ERROR},
System::Threading::CreateEventW,
},
};
Expand Down Expand Up @@ -147,13 +147,10 @@ impl AsyncNdisapiAdapter {
// Wait for packet event
match self.notif.next().await {
Some(result) => match result {
Ok(_) => {
if driver.read_packet(&mut request).is_ok() {
Ok(())
} else {
Err(unsafe { GetLastError() }.into())
}
}
Ok(_) => match driver.read_packet(&mut request) {
Ok(_) => Ok(()),
Err(e) => Err(e),
},
Err(e) => Err(e),
},
None => {
Expand Down Expand Up @@ -206,13 +203,10 @@ impl AsyncNdisapiAdapter {
// Wait for packet event
match self.notif.next().await {
Some(result) => match result {
Ok(_) => {
if driver.read_packets(&mut request).ok().is_some() {
Ok(request.get_packet_success() as usize)
} else {
Err(unsafe { GetLastError() }.into())
}
}
Ok(_) => match driver.read_packets(&mut request) {
Ok(_) => Ok(request.get_packet_success() as usize),
Err(err) => Err(err),
},
Err(e) => Err(e),
},
None => {
Expand Down Expand Up @@ -254,11 +248,7 @@ impl AsyncNdisapiAdapter {
request.set_packet(packet);

// Try to send packet to the network adapter.
if self.driver.send_packet_to_adapter(&request).is_ok() {
Ok(())
} else {
Err(unsafe { GetLastError() }.into())
}
self.driver.send_packet_to_adapter(&request)
}

/// Sends a specified number of Ethernet packets to the network adapter synchronously.
Expand Down Expand Up @@ -295,10 +285,9 @@ impl AsyncNdisapiAdapter {
let request = ndisapi::EthMRequest::<N>::from_iter(self.adapter_handle, packets);

// Try to send packets to the network adapter.
if self.driver.send_packets_to_adapter(&request).is_ok() {
Ok(request.get_packet_success() as usize)
} else {
Err(unsafe { GetLastError() }.into())
match self.driver.send_packets_to_adapter(&request) {
Ok(_) => Ok(request.get_packet_success() as usize),
Err(err) => Err(err),
}
}

Expand Down Expand Up @@ -328,11 +317,7 @@ impl AsyncNdisapiAdapter {
request.set_packet(packet);

// Try to send packet upwards the network stack.
if self.driver.send_packet_to_mstcp(&request).is_ok() {
Ok(())
} else {
Err(unsafe { GetLastError() }.into())
}
self.driver.send_packet_to_mstcp(&request)
}

/// Sends a sequence of Ethernet packets upwards through the network stack to the Microsoft TCP/IP protocol driver synchronously.
Expand Down Expand Up @@ -368,10 +353,9 @@ impl AsyncNdisapiAdapter {
let request = ndisapi::EthMRequest::<N>::from_iter(self.adapter_handle, packets);

// Try to send packets upwards the network stack.
if self.driver.send_packets_to_mstcp(&request).is_ok() {
Ok(request.get_packet_success() as usize)
} else {
Err(unsafe { GetLastError() }.into())
match self.driver.send_packets_to_mstcp(&request) {
Ok(_) => Ok(request.get_packet_success() as usize),
Err(err) => Err(err),
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/async_api/win32_event_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use std::{
use windows::{
core::Result,
Win32::{
Foundation::{CloseHandle, GetLastError, BOOLEAN, HANDLE},
Foundation::{CloseHandle, BOOLEAN, HANDLE},
System::Threading::{
RegisterWaitForSingleObject, ResetEvent, UnregisterWaitEx, INFINITE,
WT_EXECUTEINWAITTHREAD,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl Win32EventStream {
Box::new(move |_| {
ready.store(true, Ordering::SeqCst);
waker.wake();
unsafe { ResetEvent(event_handle) };
let _ = unsafe { ResetEvent(event_handle) };
}),
)?,
})
Expand Down Expand Up @@ -142,15 +142,16 @@ impl Win32EventNotification {
};

// Check if the registration was successful.
if rc.as_bool() {
Ok(Self {
match rc {
Ok(_) => Ok(Self {
callback,
win32_event,
wait_object,
})
} else {
drop(unsafe { Box::from_raw(callback) }); // Dropping the callback function.
Err(unsafe { GetLastError() }.into())
}),
Err(e) => {
drop(unsafe { Box::from_raw(callback) }); // Dropping the callback function.
Err(e)
}
}
}
}
Expand All @@ -160,16 +161,16 @@ impl Drop for Win32EventNotification {
fn drop(&mut self) {
unsafe {
// Deregistering the wait object.
if !UnregisterWaitEx(self.wait_object, self.win32_event).as_bool() {
if UnregisterWaitEx(self.wait_object, self.win32_event).is_err() {
//log::error!("error deregistering notification: {}", GetLastError);
}
drop(Box::from_raw(self.callback)); // Dropping the callback function.
}

unsafe {
let _ = unsafe {
// Closing the handle to the event.
CloseHandle(self.win32_event);
}
CloseHandle(self.win32_event)
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/driver/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl<'a, const N: usize> EthMRequest<'a, N> {

/// Returns an iterator that yields `Some(IntermediateBuffer)` for each non-empty buffer in `packets`, in order,
/// up to `packet_success`.
///
///
/// # Description
///
/// This function, `drain_success`, operates on mutable reference to the current instance of the struct.
Expand Down
17 changes: 6 additions & 11 deletions src/ndisapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use windows::{
core::{Result, PCWSTR},
Win32::Foundation::CloseHandle,
Win32::Foundation::{GetLastError, HANDLE},
Win32::Foundation::HANDLE,
Win32::Storage::FileSystem::{
CreateFileW, FILE_FLAG_OVERLAPPED, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
},
Expand Down Expand Up @@ -63,9 +63,7 @@ impl Drop for Ndisapi {
// Provides a custom implementation for the `drop` method
fn drop(&mut self) {
// Closes the driver_handle when the `Ndisapi` instance goes out of scope
unsafe {
CloseHandle(self.driver_handle);
}
let _ = unsafe { CloseHandle(self.driver_handle) };
}
}

Expand Down Expand Up @@ -97,7 +95,7 @@ impl Ndisapi {
registry_key.push(0);

// Attempts to create a file handle for the NDIS filter driver
if let Ok(driver_handle) = unsafe {
match unsafe {
CreateFileW(
PCWSTR::from_raw(filename.as_ptr()),
0u32,
Expand All @@ -108,14 +106,11 @@ impl Ndisapi {
None,
)
} {
// Returns a new Ndisapi instance with the created handle if successful
Ok(Self {
Ok(driver_handle) => Ok(Self {
driver_handle,
registry_key,
})
} else {
// Returns an error if the file handle creation fails
Err(unsafe { GetLastError() }.into())
}),
Err(e) => Err(e),
}
}

Expand Down
Loading

0 comments on commit 51df362

Please sign in to comment.