diff --git a/Cargo.toml b/Cargo.toml index c29ec2e..8565e2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,13 +12,13 @@ readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bitflags = "2.1.0" -futures = "0.3.28" -uuid = { version = "1.3", features = ["v4", "serde", "v6"] } +bitflags = "2.4.2" +futures = "0.3.30" +uuid = { version = "1.7", features = ["v4", "serde", "v6"] } ipnetwork = "0.20.0" [dependencies.windows] -version = "0.52.0" +version = "0.54.0" features = [ "Win32_Foundation", "Win32_Security", @@ -33,10 +33,10 @@ features = [ ] [dev-dependencies] -clap = {version = "4.0.32", features = ["derive"]} -ctrlc = "3.2.4" -futures = "0.3.28" -tokio = { version = "1.28.2", features = ["full"] } +clap = {version = "4.5.2", features = ["derive"]} +ctrlc = "3.4.4" +futures = "0.3.30" +tokio = { version = "1.36.0", features = ["full"] } smoltcp = "0.11.0" prettytable-rs = "0.10.0" crossterm = "0.27.0" diff --git a/examples/listadapters.rs b/examples/listadapters.rs index e80564c..873c97c 100644 --- a/examples/listadapters.rs +++ b/examples/listadapters.rs @@ -48,7 +48,7 @@ fn main() -> Result<()> { match driver.get_hw_packet_filter(adapter.get_handle()) { Err(err) => println!( "Getting OID_GEN_CURRENT_PACKET_FILTER Error: {}", - err.message().to_string_lossy() + err.message() ), Ok(current_packet_filter) => { println!("\t OID_GEN_CURRENT_PACKET_FILTER: 0x{current_packet_filter:08X}") @@ -62,10 +62,7 @@ fn main() -> Result<()> { MacAddress::default(), ); if let Err(err) = driver.ndis_get_request::<_>(&mut current_address_request) { - println!( - "Getting OID_802_3_CURRENT_ADDRESS Error: {}", - err.message().to_string_lossy() - ) + println!("Getting OID_802_3_CURRENT_ADDRESS Error: {}", err.message(),) } else { println!( "\t OID_802_3_CURRENT_ADDRESS: {}", diff --git a/src/ndisapi/static_api.rs b/src/ndisapi/static_api.rs index 67b14a9..39f5a7f 100644 --- a/src/ndisapi/static_api.rs +++ b/src/ndisapi/static_api.rs @@ -92,6 +92,7 @@ impl Ndisapi { KEY_READ, &mut target_key, ) + .ok() }; if result.is_err() { @@ -313,7 +314,8 @@ impl Ndisapi { KEY_READ, &mut hkey, ) - }; + } + .ok(); let mut value_type = REG_VALUE_TYPE::default(); let mut data = vec![0u16; 256]; @@ -330,7 +332,8 @@ impl Ndisapi { Some(data.as_mut_ptr() as *const u8 as *mut u8), Some(&mut data_size), ) - }; + } + .ok(); if result.is_ok() { friendly_name = if let Ok(name) = String::from_utf16(&data[..data_size as usize]) { @@ -373,7 +376,8 @@ impl Ndisapi { KEY_WRITE, &mut hkey, ) - }; + } + .ok(); if result.is_ok() { result = unsafe { @@ -384,7 +388,8 @@ impl Ndisapi { REG_DWORD, Some(mtu_decrement.to_ne_bytes().as_ref()), ) - }; + } + .ok(); } result @@ -471,7 +476,7 @@ impl Ndisapi { }; } - result + result.ok() } /// Returns the current default filter mode value applied to each adapter when it appears in the system. @@ -559,7 +564,7 @@ impl Ndisapi { }; } - result + result.ok() } /// Retrieves the pool size multiplier for the Windows Packet Filter driver from the Windows registry. diff --git a/src/netlib/ip_helper/network_adapter_info.rs b/src/netlib/ip_helper/network_adapter_info.rs index 512721f..3cf5f5c 100644 --- a/src/netlib/ip_helper/network_adapter_info.rs +++ b/src/netlib/ip_helper/network_adapter_info.rs @@ -286,7 +286,7 @@ impl IphlpNetworkAdapterInfo { }; } - result + result.ok() } /// Checks if IP address information in the provided network_adapter_info is different diff --git a/src/netlib/ip_helper/network_adapter_info/address.rs b/src/netlib/ip_helper/network_adapter_info/address.rs index 88ff482..eeaeb1b 100644 --- a/src/netlib/ip_helper/network_adapter_info/address.rs +++ b/src/netlib/ip_helper/network_adapter_info/address.rs @@ -67,7 +67,7 @@ impl IphlpNetworkAdapterInfo { address_row.DadState = IpDadStatePreferred; // Call the CreateUnicastIpAddressEntry function (you need to implement this function) - match unsafe { CreateUnicastIpAddressEntry(&address_row) } { + match unsafe { CreateUnicastIpAddressEntry(&address_row).ok() } { Ok(_) => Some(address_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { @@ -130,7 +130,7 @@ impl IphlpNetworkAdapterInfo { address_row.OnLinkPrefixLength = prefix_length; address_row.DadState = IpDadStatePreferred; - match unsafe { CreateUnicastIpAddressEntry(&address_row) } { + match unsafe { CreateUnicastIpAddressEntry(&address_row) }.ok() { Ok(_) => Some(address_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { @@ -180,7 +180,7 @@ impl IphlpNetworkAdapterInfo { pub fn reset_unicast_addresses(&self) -> bool { let mut table: *mut MIB_UNICASTIPADDRESS_TABLE = std::ptr::null_mut(); - match unsafe { GetUnicastIpAddressTable(AF_UNSPEC, &mut table) } { + match unsafe { GetUnicastIpAddressTable(AF_UNSPEC, &mut table) }.ok() { Ok(_) => { for i in 0..unsafe { (*table).NumEntries } { let entry = unsafe { &mut (*table).Table[i as usize] }; @@ -188,7 +188,7 @@ impl IphlpNetworkAdapterInfo { let _ = unsafe { DeleteUnicastIpAddressEntry(entry) }; } } - let _ = unsafe { FreeMibTable(table as *mut _) }; + unsafe { FreeMibTable(table as *mut _) }; true } Err(_) => false, @@ -213,7 +213,7 @@ impl IphlpNetworkAdapterInfo { pub fn delete_unicast_address_ipv4(&self, address: Ipv4Addr) -> bool { let mut table: *mut MIB_UNICASTIPADDRESS_TABLE = std::ptr::null_mut(); - match unsafe { GetUnicastIpAddressTable(AF_INET, &mut table) } { + match unsafe { GetUnicastIpAddressTable(AF_INET, &mut table) }.ok() { Ok(_) => { for i in 0..unsafe { (*table).NumEntries } { let entry = unsafe { &(*table).Table[i as usize] }; @@ -227,7 +227,7 @@ impl IphlpNetworkAdapterInfo { } } - let _ = unsafe { FreeMibTable(table as *mut _) }; + unsafe { FreeMibTable(table as *mut _) }; true } Err(_) => false, @@ -252,7 +252,7 @@ impl IphlpNetworkAdapterInfo { pub fn delete_unicast_address_ipv6(&self, address: Ipv6Addr) -> bool { let mut table: *mut MIB_UNICASTIPADDRESS_TABLE = std::ptr::null_mut(); - match unsafe { GetUnicastIpAddressTable(AF_INET6, &mut table) } { + match unsafe { GetUnicastIpAddressTable(AF_INET6, &mut table) }.ok() { Ok(_) => { for i in 0..unsafe { (*table).NumEntries } { let entry = unsafe { &(*table).Table[i as usize] }; @@ -264,7 +264,7 @@ impl IphlpNetworkAdapterInfo { } } - let _ = unsafe { FreeMibTable(table as *mut _) }; + unsafe { FreeMibTable(table as *mut _) }; true } Err(_) => false, diff --git a/src/netlib/ip_helper/network_adapter_info/gateway.rs b/src/netlib/ip_helper/network_adapter_info/gateway.rs index 468a012..a3dabc6 100644 --- a/src/netlib/ip_helper/network_adapter_info/gateway.rs +++ b/src/netlib/ip_helper/network_adapter_info/gateway.rs @@ -41,7 +41,7 @@ impl IphlpNetworkAdapterInfo { forward_row.Protocol = MIB_IPPROTO_NT_STATIC; forward_row.Origin = NlroManual; - match unsafe { CreateIpForwardEntry2(&forward_row) } { + match unsafe { CreateIpForwardEntry2(&forward_row) }.ok() { Ok(_) => Some(forward_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { @@ -88,7 +88,7 @@ impl IphlpNetworkAdapterInfo { forward_row.Protocol = MIB_IPPROTO_NT_STATIC; forward_row.Origin = NlroManual; - match unsafe { CreateIpForwardEntry2(&forward_row) } { + match unsafe { CreateIpForwardEntry2(&forward_row) }.ok() { Ok(_) => Some(forward_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { @@ -131,7 +131,7 @@ impl IphlpNetworkAdapterInfo { forward_row.Protocol = MIB_IPPROTO_NT_STATIC; forward_row.Origin = NlroManual; - match unsafe { CreateIpForwardEntry2(&forward_row) } { + match unsafe { CreateIpForwardEntry2(&forward_row) }.ok() { Ok(_) => Some(forward_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { @@ -174,7 +174,7 @@ impl IphlpNetworkAdapterInfo { forward_row.Protocol = MIB_IPPROTO_NT_STATIC; forward_row.Origin = NlroManual; - match unsafe { CreateIpForwardEntry2(&forward_row) } { + match unsafe { CreateIpForwardEntry2(&forward_row) }.ok() { Ok(_) => Some(forward_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { diff --git a/src/netlib/ip_helper/network_adapter_info/ndp.rs b/src/netlib/ip_helper/network_adapter_info/ndp.rs index c471702..2422566 100644 --- a/src/netlib/ip_helper/network_adapter_info/ndp.rs +++ b/src/netlib/ip_helper/network_adapter_info/ndp.rs @@ -47,7 +47,7 @@ impl IphlpNetworkAdapterInfo { set_is_unreachable(&mut net_row, true); } - match unsafe { CreateIpNetEntry2(&net_row) } { + match unsafe { CreateIpNetEntry2(&net_row) }.ok() { Ok(_) => Some(net_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { @@ -99,7 +99,7 @@ impl IphlpNetworkAdapterInfo { set_is_unreachable(&mut net_row, true); } - match unsafe { CreateIpNetEntry2(&net_row) } { + match unsafe { CreateIpNetEntry2(&net_row) }.ok() { Ok(_) => Some(net_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { diff --git a/src/netlib/ip_helper/network_adapter_info/routing.rs b/src/netlib/ip_helper/network_adapter_info/routing.rs index 6f24315..9290336 100644 --- a/src/netlib/ip_helper/network_adapter_info/routing.rs +++ b/src/netlib/ip_helper/network_adapter_info/routing.rs @@ -59,7 +59,7 @@ impl IphlpNetworkAdapterInfo { forward_row.Protocol = MIB_IPPROTO_NT_STATIC; forward_row.Origin = NlroManual; - match unsafe { CreateIpForwardEntry2(&forward_row) } { + match unsafe { CreateIpForwardEntry2(&forward_row) }.ok() { Ok(_) => ret_val.push_back(forward_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { @@ -111,7 +111,7 @@ impl IphlpNetworkAdapterInfo { forward_row.Protocol = MIB_IPPROTO_NT_STATIC; forward_row.Origin = NlroManual; - match unsafe { CreateIpForwardEntry2(&forward_row) } { + match unsafe { CreateIpForwardEntry2(&forward_row) }.ok() { Ok(_) => return_value.push_back(forward_row), Err(err) => { if err == ERROR_OBJECT_ALREADY_EXISTS.into() { @@ -181,7 +181,7 @@ impl IphlpNetworkAdapterInfo { pub fn reset_adapter_routes(&self) -> bool { let mut table: *mut MIB_IPFORWARD_TABLE2 = std::ptr::null_mut(); - match unsafe { GetIpForwardTable2(AF_UNSPEC, &mut table) } { + match unsafe { GetIpForwardTable2(AF_UNSPEC, &mut table) }.ok() { Ok(_) => { let num_entries = unsafe { (*table).NumEntries }; @@ -193,7 +193,7 @@ impl IphlpNetworkAdapterInfo { } } - let _ = unsafe { FreeMibTable(table as *mut _) }; + unsafe { FreeMibTable(table as *mut _) }; true } Err(_) => false, diff --git a/src/netlib/ip_helper/network_adapter_info/statics.rs b/src/netlib/ip_helper/network_adapter_info/statics.rs index 7043d6c..8ea42da 100644 --- a/src/netlib/ip_helper/network_adapter_info/statics.rs +++ b/src/netlib/ip_helper/network_adapter_info/statics.rs @@ -117,7 +117,7 @@ impl IphlpNetworkAdapterInfo { } // Free interface table - let _ = unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; + unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; ret_val } @@ -196,8 +196,7 @@ impl IphlpNetworkAdapterInfo { if IfLuid::from(unsafe { (*entry_ptr).InterfaceLuid }) == luid { let result = unsafe { IphlpNetworkAdapterInfo::new(current, entry_ptr) }; - let _ = - unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; + unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; return Some(result); } } @@ -221,7 +220,7 @@ impl IphlpNetworkAdapterInfo { } // Free interface table - let _ = unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; + unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; None } @@ -304,8 +303,7 @@ impl IphlpNetworkAdapterInfo { { let result = unsafe { IphlpNetworkAdapterInfo::new(current, entry_ptr) }; - let _ = - unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; + unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; return Some(result); } } @@ -329,7 +327,7 @@ impl IphlpNetworkAdapterInfo { } // Free interface table - let _ = unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; + unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; None } @@ -414,8 +412,7 @@ impl IphlpNetworkAdapterInfo { { let result = unsafe { IphlpNetworkAdapterInfo::new(current, entry_ptr) }; - let _ = - unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; + unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; return Some(result); } } @@ -439,7 +436,7 @@ impl IphlpNetworkAdapterInfo { } // Free interface table - let _ = unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; + unsafe { FreeMibTable(mib_table as *const core::ffi::c_void) }; None }