summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSebastian Holmin <sebastian.holmin@mullvad.net>2024-09-24 12:01:45 +0200
committerSebastian Holmin <sebastian.holmin@mullvad.net>2024-09-24 12:01:45 +0200
commitcd4f743ed5ce75e975594bf3071074756c538f53 (patch)
tree563ee43e0e287db9050c3fa8d1c7033f9bcc06ea
parent7807b09f28a83afff26d36bf4eac60dff0b020f7 (diff)
parent5a11fac6a6487e3095508923f873ddf8db918559 (diff)
downloadmullvadvpn-cd4f743ed5ce75e975594bf3071074756c538f53.tar.xz
mullvadvpn-cd4f743ed5ce75e975594bf3071074756c538f53.zip
Merge branch 'des-959-add-command-in-cli-that-removes-old-adapters'
-rw-r--r--talpid-openvpn/src/wintun.rs44
1 files changed, 27 insertions, 17 deletions
diff --git a/talpid-openvpn/src/wintun.rs b/talpid-openvpn/src/wintun.rs
index 742d4a910b..744ce9aad0 100644
--- a/talpid-openvpn/src/wintun.rs
+++ b/talpid-openvpn/src/wintun.rs
@@ -28,6 +28,8 @@ type WintunCreateAdapterFn = unsafe extern "stdcall" fn(
requested_guid: *const GUID,
) -> RawHandle;
+type WintunOpenAdapterFn = unsafe extern "stdcall" fn(name: *const u16) -> RawHandle;
+
type WintunCloseAdapterFn = unsafe extern "stdcall" fn(adapter: RawHandle);
type WintunGetAdapterLuidFn =
@@ -48,6 +50,7 @@ enum WintunLoggerLevel {
pub struct WintunDll {
handle: HMODULE,
func_create: WintunCreateAdapterFn,
+ func_open: WintunOpenAdapterFn,
func_close: WintunCloseAdapterFn,
func_get_adapter_luid: WintunGetAdapterLuidFn,
func_set_logger: WintunSetLoggerFn,
@@ -190,28 +193,19 @@ impl WintunDll {
Ok(WintunDll {
handle,
func_create: unsafe {
- *((&get_proc_fn(
- handle,
- CStr::from_bytes_with_nul(b"WintunCreateAdapter\0").unwrap(),
- )?) as *const _ as *const _)
+ *((&get_proc_fn(handle, c"WintunCreateAdapter")?) as *const _ as *const _)
+ },
+ func_open: unsafe {
+ *((&get_proc_fn(handle, c"WintunOpenAdapter")?) as *const _ as *const _)
},
func_close: unsafe {
- *((&get_proc_fn(
- handle,
- CStr::from_bytes_with_nul(b"WintunCloseAdapter\0").unwrap(),
- )?) as *const _ as *const _)
+ *((&get_proc_fn(handle, c"WintunCloseAdapter")?) as *const _ as *const _)
},
func_get_adapter_luid: unsafe {
- *((&get_proc_fn(
- handle,
- CStr::from_bytes_with_nul(b"WintunGetAdapterLUID\0").unwrap(),
- )?) as *const _ as *const _)
+ *((&get_proc_fn(handle, c"WintunGetAdapterLUID")?) as *const _ as *const _)
},
func_set_logger: unsafe {
- *((&get_proc_fn(
- handle,
- CStr::from_bytes_with_nul(b"WintunSetLogger\0").unwrap(),
- )?) as *const _ as *const _)
+ *((&get_proc_fn(handle, c"WintunSetLogger")?) as *const _ as *const _)
},
})
}
@@ -236,7 +230,23 @@ impl WintunDll {
};
let handle = unsafe { (self.func_create)(name.as_ptr(), tunnel_type.as_ptr(), guid_ptr) };
if handle.is_null() {
- return Err(io::Error::last_os_error());
+ log::error!(
+ "Failed to create Wintun adapter: {}",
+ io::Error::last_os_error()
+ );
+ // This is an attempt to fix the elusive "Failed to create Wintun adapter" error.
+ // we cannot reproduce the issue on our end, but if it is caused by an existing adapter
+ // that hasn't been cleaned up properly, it may help to open the adapter and return it.
+ log::info!(
+ "Attempting to open existing adapter with name: '{}'",
+ name.to_string_lossy()
+ );
+ let handle = unsafe { (self.func_open)(name.as_ptr()) };
+ if handle.is_null() {
+ return Err(io::Error::last_os_error());
+ } else {
+ return Ok(handle);
+ }
}
Ok(handle)
}