Skip to content

Commit

Permalink
refine FnHolder module
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Oct 7, 2024
1 parent c009f76 commit 97bd1da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/fn_holder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub trait FnCast: Sized + Copy {}
pub trait FnCast: Copy {}

pub struct FnHolder<F: FnCast> {
_lib: ::libloading::Library,
Expand Down Expand Up @@ -28,6 +28,7 @@ macro_rules! define_fn_dynamic_load {
static $static_var: std::sync::OnceLock<Option<$crate::fn_holder::FnHolder<$fn_type>>> =
std::sync::OnceLock::new();

#[allow(non_snake_case)]
pub fn $load_fn() -> Option<$fn_type> {
$static_var
.get_or_init(|| $crate::fn_holder::load_function($module_name, $fn_name))
Expand All @@ -41,12 +42,12 @@ macro_rules! define_fn_dynamic_load {
// usage
use windows_sys::Win32::Foundation::BOOL;
define_fn_dynamic_load!(
ProcessPrngFn,
ProcessPrngDeclear,
unsafe extern "system" fn(pbdata: *mut u8, cbdata: usize) -> BOOL,
PROCESS_PRNG,
get_fn_process_prng,
ProcessPrng,
"bcryptprimitives.dll",
"ProcessPrng"
);
let func = get_fn_process_prng().ok_or("Failed to load function ProcessPrng")?;
let func = ProcessPrng().ok_or("Failed to load function ProcessPrng")?;
*/
23 changes: 15 additions & 8 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ pub fn get_wintun_bin_pattern_path() -> std::io::Result<std::path::PathBuf> {
//
// WINAPI VOID RtlGetNtVersionNumbers (DWORD *MajorVersion, DWORD *MinorVersion, DWORD *BuildNumber);
//
pub(crate) fn get_windows_version() -> Result<(u32, u32, u32), libloading::Error> {
let library = unsafe { ::libloading::Library::new("ntdll")? };
type TheFn = unsafe extern "system" fn(*mut u32, *mut u32, *mut u32);
let func: TheFn = unsafe { library.get(b"RtlGetNtVersionNumbers\0").map(|sym| *sym)? };
crate::define_fn_dynamic_load!(
RtlGetNtVersionNumbersDeclear,
unsafe extern "system" fn(*mut u32, *mut u32, *mut u32),
RTL_GET_NT_VERSION_NUMBERS,
RtlGetNtVersionNumbers,
"ntdll",
"RtlGetNtVersionNumbers"
);

pub(crate) fn get_windows_version() -> Result<(u32, u32, u32), Error> {
let func = RtlGetNtVersionNumbers().ok_or("Failed to load function RtlGetNtVersionNumbers")?;
let (mut major, mut minor, mut build) = (0, 0, 0);
unsafe { func(&mut major, &mut minor, &mut build) };
Ok((major, minor, build))
Expand Down Expand Up @@ -139,16 +146,16 @@ pub fn get_active_network_interface_gateways() -> std::io::Result<Vec<IpAddr>> {
}

crate::define_fn_dynamic_load!(
SetInterfaceDnsSettingsFn,
SetInterfaceDnsSettingsDeclear,
unsafe extern "system" fn(GUID, *const DNS_INTERFACE_SETTINGS) -> WIN32_ERROR,
g_SetInterfaceDnsSettings,
get_set_interface_dns_settings_fn,
SET_INTERFACE_DNS_SETTINGS,
SetInterfaceDnsSettings,
"iphlpapi.dll",
"SetInterfaceDnsSettings"
);

pub(crate) fn set_interface_dns_servers(interface: GUID, dns: &[IpAddr]) -> crate::Result<()> {
let func = get_set_interface_dns_settings_fn().ok_or("Failed to load function SetInterfaceDnsSettings")?;
let func = SetInterfaceDnsSettings().ok_or("Failed to load function SetInterfaceDnsSettings")?;

// format L"1.1.1.1,8.8.8.8", or L"1.1.1.1 8.8.8.8".
let dns = dns.iter().map(|ip| ip.to_string()).collect::<Vec<_>>().join(",");
Expand Down

0 comments on commit 97bd1da

Please sign in to comment.