diff options
| author | David Lönnhager <david.l@mullvad.net> | 2021-02-15 16:24:44 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2021-02-17 12:21:24 +0100 |
| commit | ec6e06755afb7083068023bf9240e41ba0c0f5a7 (patch) | |
| tree | 73c3267c94f103671a94c70139307bfba9157def | |
| parent | 081a5426279477de06905201642b398970a5bc85 (diff) | |
| download | mullvadvpn-ec6e06755afb7083068023bf9240e41ba0c0f5a7.tar.xz mullvadvpn-ec6e06755afb7083068023bf9240e41ba0c0f5a7.zip | |
Add method for obtaining Wintun adapter alias
| -rw-r--r-- | talpid-core/src/tunnel/openvpn/windows.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/talpid-core/src/tunnel/openvpn/windows.rs b/talpid-core/src/tunnel/openvpn/windows.rs index f87efc3967..ba5f2b8821 100644 --- a/talpid-core/src/tunnel/openvpn/windows.rs +++ b/talpid-core/src/tunnel/openvpn/windows.rs @@ -7,7 +7,7 @@ use std::{ sync::Arc, }; use talpid_types::ErrorExt; -use widestring::U16CStr; +use widestring::{U16CStr, U16CString}; use winapi::{ shared::{ guiddef::GUID, @@ -23,6 +23,9 @@ use winapi::{ use winreg::{enums::HKEY_LOCAL_MACHINE, RegKey}; +/// Longest possible adapter name (in characters), including null terminator +const MAX_ADAPTER_NAME: usize = 128; + type WintunOpenAdapterFn = unsafe extern "stdcall" fn(pool: *const u16, name: *const u16) -> RawHandle; @@ -41,6 +44,9 @@ type WintunDeleteAdapterFn = unsafe extern "stdcall" fn( reboot_required: *mut BOOL, ) -> BOOL; +type WintunGetAdapterNameFn = + unsafe extern "stdcall" fn(adapter: RawHandle, name: *mut u16) -> BOOL; + pub struct WintunDll { handle: HINSTANCE, @@ -48,6 +54,7 @@ pub struct WintunDll { func_create: WintunCreateAdapterFn, func_free: WintunFreeAdapterFn, func_delete: WintunDeleteAdapterFn, + func_get_adapter_name: WintunGetAdapterNameFn, } unsafe impl Send for WintunDll {} @@ -72,6 +79,10 @@ impl TemporaryWintunAdapter { WintunAdapter::create(dll_handle, pool, name, requested_guid)?; Ok((TemporaryWintunAdapter { adapter }, reboot_required)) } + + pub fn adapter(&self) -> &WintunAdapter { + &self.adapter + } } impl Drop for TemporaryWintunAdapter { @@ -129,6 +140,10 @@ impl WintunAdapter { .delete_adapter(self.handle, force_close_sessions) } } + + pub fn name(&self) -> io::Result<U16CString> { + unsafe { self.dll_handle.get_adapter_name(self.handle) } + } } impl Drop for WintunAdapter { @@ -183,6 +198,12 @@ impl WintunDll { CStr::from_bytes_with_nul(b"WintunFreeAdapter\0").unwrap(), )?) }, + func_get_adapter_name: unsafe { + std::mem::transmute(Self::get_proc_address( + handle, + CStr::from_bytes_with_nul(b"WintunGetAdapterName\0").unwrap(), + )?) + }, }) } @@ -239,6 +260,16 @@ impl WintunDll { pub unsafe fn free_adapter(&self, adapter: RawHandle) { (self.func_free)(adapter); } + + pub unsafe fn get_adapter_name(&self, adapter: RawHandle) -> io::Result<U16CString> { + let mut alias_buffer = vec![0u16; MAX_ADAPTER_NAME]; + let result = (self.func_get_adapter_name)(adapter, alias_buffer.as_mut_ptr()); + if result == 0 { + return Err(io::Error::last_os_error()); + } + Ok(U16CString::from_vec_with_nul(alias_buffer) + .map_err(|_| io::Error::new(io::ErrorKind::Other, "missing null terminator"))?) + } } impl Drop for WintunDll { |
