Skip to content
Merged
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
5 changes: 4 additions & 1 deletion examples/async-packthru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ async fn update_display(shared_table: Arc<Mutex<HashMap<PacketInfo, u32>>>) {
.iter()
.map(|(pi, &count)| (pi, count))
.collect();
counts.sort_by(|a, b| b.1.cmp(&a.1));
counts.sort_by_key(|c| std::cmp::Reverse(c.1));
let top_entries = &counts[..std::cmp::min(10, counts.len())];

for (packet_info, count) in top_entries {
Expand Down Expand Up @@ -668,6 +668,9 @@ async fn main() -> Result<()> {
interface_index -= 1;

// Create a new Ndisapi driver instance.
// The async API takes an `Arc<Ndisapi>`; `Ndisapi` is intentionally not `Send`/`Sync`
// (it owns a raw `HANDLE`), so clippy's `Arc<!Send + !Sync>` suggestion does not apply here.
#[allow(clippy::arc_with_non_send_sync)]
let driver = Arc::new(
Ndisapi::new("NDISRD").expect("WinpkFilter driver is not installed or failed to load!"),
);
Expand Down
5 changes: 4 additions & 1 deletion examples/async-passthru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn async_loop(adapter: &mut AsyncNdisapiAdapter) -> Result<()> {
Err(err) => println!("Error sending packet to adapter. Error code = {err}"),
};
} else {
match adapter.send_packet_to_mstcp(&mut packet) {
match adapter.send_packet_to_mstcp(&packet) {
Ok(_) => {}
Err(err) => println!("Error sending packet to mstcp. Error code = {err}"),
}
Expand Down Expand Up @@ -104,6 +104,9 @@ async fn main() -> Result<()> {
interface_index -= 1;

// Create a new Ndisapi driver instance.
// The async API takes an `Arc<Ndisapi>`; `Ndisapi` is intentionally not `Send`/`Sync`
// (it owns a raw `HANDLE`), so clippy's `Arc<!Send + !Sync>` suggestion does not apply here.
#[allow(clippy::arc_with_non_send_sync)]
let driver = Arc::new(
Ndisapi::new("NDISRD").expect("WinpkFilter driver is not installed or failed to load!"),
);
Expand Down
7 changes: 2 additions & 5 deletions examples/listadapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
/// adapter name conversion functions, and `Ndisapi::get_mtu_decrement` and etc.. It retrieves information about the
/// network interfaces, including their indexes, which can be passed to the `packthru` and `passthru`
/// examples. The collected information is dumped to the console screen.
use std::{
mem::{self, size_of},
ptr::write_bytes,
};
use std::{mem::size_of, ptr::write_bytes};

use ndisapi::{IphlpNetworkAdapterInfo, MacAddress, Ndisapi, PacketOidData, RasLinks};
use windows::core::Result;
Expand Down Expand Up @@ -102,7 +99,7 @@ fn main() -> Result<()> {
// zero initialize the vector allocated memory and then set a vector length to one
unsafe {
write_bytes::<u8>(
mem::transmute(ras_links_vec.as_mut_ptr()),
ras_links_vec.as_mut_ptr().cast::<u8>(),
0,
Comment thread
wiresock marked this conversation as resolved.
size_of::<RasLinks>(),
);
Expand Down
14 changes: 7 additions & 7 deletions examples/packthru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,35 +122,35 @@ fn main() -> Result<()> {
packets_number = packets_number.saturating_sub(packets_read);

// Process each packet.
for i in 0..packets_read {
let direction_flags = packets[i].get_device_flags();
for (i, packet) in packets.iter().take(packets_read).enumerate() {
let direction_flags = packet.get_device_flags();

if verbose {
// Print packet direction and remaining packets.
if direction_flags == DirectionFlags::PACKET_FLAG_ON_SEND {
println!(
"\nMSTCP --> Interface ({} bytes) remaining packets {}\n",
packets[i].get_length(),
packet.get_length(),
packets_number + (packets_read - i)
);
} else {
println!(
"\nInterface --> MSTCP ({} bytes) remaining packets {}\n",
packets[i].get_length(),
packet.get_length(),
packets_number + (packets_read - i)
);
}
}

if verbose {
// Print packet information
print_packet_info(&packets[i]);
print_packet_info(packet);
}

if direction_flags == DirectionFlags::PACKET_FLAG_ON_SEND {
to_adapter.push(&packets[i])?;
to_adapter.push(packet)?;
} else {
to_mstcp.push(&packets[i])?;
to_mstcp.push(packet)?;
}
}

Expand Down
10 changes: 5 additions & 5 deletions examples/unsorted-packthru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,26 @@ fn main() -> Result<()> {

if verbose {
// Process each packet.
for i in 0..packets_read {
let direction_flags = packets[i].get_device_flags();
for (i, packet) in packets.iter().take(packets_read).enumerate() {
let direction_flags = packet.get_device_flags();

// Print packet direction and remaining packets.
if direction_flags == DirectionFlags::PACKET_FLAG_ON_SEND {
println!(
"\nMSTCP --> Interface ({} bytes) remaining packets {}\n",
packets[i].get_length(),
packet.get_length(),
packets_number + (packets_read - i)
);
} else {
println!(
"\nInterface --> MSTCP ({} bytes) remaining packets {}\n",
packets[i].get_length(),
packet.get_length(),
packets_number + (packets_read - i)
);
}

// Print packet information
print_packet_info(&packets[i]);
print_packet_info(packet);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ndisapi/static_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl Ndisapi {

let friendly_name_key = format!(
"SYSTEM\\CurrentControlSet\\Control\\Network\\{{4D36E972-E325-11CE-BFC1-08002BE10318}}\\{}\\Connection",
&adapter_name
adapter_name
);

// Convert the string to UTF16 array and get a pointer to it as PCWSTR
Expand Down
6 changes: 3 additions & 3 deletions src/netlib/ip_helper/network_adapter_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ impl IphlpNetworkAdapterInfo {

// create the registry key path for the adapter's connection settings
let friendly_name_key = format!(
"SYSTEM\\CurrentControlSet\\Control\\Network\\{{4D36E972-E325-11CE-BFC1-08002BE10318}}\\{}\\Connection",
&self.adapter_name
);
"SYSTEM\\CurrentControlSet\\Control\\Network\\{{4D36E972-E325-11CE-BFC1-08002BE10318}}\\{}\\Connection",
self.adapter_name
);

// Convert the string to UTF16 array and get a pointer to it as PCWSTR
let mut friendly_name_key = friendly_name_key.encode_utf16().collect::<Vec<u16>>();
Expand Down
4 changes: 2 additions & 2 deletions src/netlib/ip_helper/sockaddr_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ mod tests {
let sockaddr_in = SOCKADDR_IN {
sin_family: AF_INET,
sin_port: 0,
sin_addr: unsafe { mem::transmute(ipv4) },
sin_addr: ipv4.into(),
sin_zero: [0; 8],
};
let ip_address_info = SockAddrStorage::from_sockaddr_in(sockaddr_in);
Expand All @@ -553,7 +553,7 @@ mod tests {
sin6_family: AF_INET6,
sin6_port: 0,
sin6_flowinfo: 0,
sin6_addr: unsafe { mem::transmute(ipv6) },
sin6_addr: ipv6.into(),
Anonymous: SOCKADDR_IN6_0 { sin6_scope_id: 0 },
};
let ip_address_info = SockAddrStorage::from_sockaddr_in6(sockaddr_in6);
Expand Down
Loading