Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If it fails, close the handle #3

Merged
merged 1 commit into from
Oct 12, 2024
Merged
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
72 changes: 45 additions & 27 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,32 @@ impl Adapter {
if result.is_null() {
return Err("Failed to create adapter".into());
}
let luid = crate::ffi::alias_to_luid(name)?;
let index = crate::ffi::luid_to_index(&luid)?;
let real_guid = util::win_guid_to_u128(&crate::ffi::luid_to_guid(&luid)?);
if guid != real_guid {
let real_guid_s = util::guid_to_win_style_string(&GUID::from_u128(real_guid))?;
let guid_s = util::guid_to_win_style_string(&GUID::from_u128(guid))?;
let (major, minor, build) = util::get_windows_version()?;
log::warn!("Windows {major}.{minor}.{build} internal bug cause the GUID mismatch: Expected {guid_s}, got {real_guid_s}");
guid = real_guid;
let mut call = || -> Result<Arc<Adapter>, Error> {
let luid = crate::ffi::alias_to_luid(name)?;
let index = crate::ffi::luid_to_index(&luid)?;
let real_guid = util::win_guid_to_u128(&crate::ffi::luid_to_guid(&luid)?);
if guid != real_guid {
let real_guid_s = util::guid_to_win_style_string(&GUID::from_u128(real_guid))?;
let guid_s = util::guid_to_win_style_string(&GUID::from_u128(guid))?;
let (major, minor, build) = util::get_windows_version()?;
log::warn!("Windows {major}.{minor}.{build} internal bug cause the GUID mismatch: Expected {guid_s}, got {real_guid_s}");
guid = real_guid;
}
Ok(Arc::new(Adapter {
adapter: UnsafeHandle(result),
wintun: wintun.clone(),
guid,
index,
luid,
}))
};
match call() {
Ok(adapter) => Ok(adapter),
Err(e) => {
unsafe { wintun.WintunCloseAdapter(result) };
Err(e)
}
}
Ok(Arc::new(Adapter {
adapter: UnsafeHandle(result),
wintun: wintun.clone(),
guid,
index,
luid,
}))
}

/// Attempts to open an existing wintun interface name `name`.
Expand All @@ -116,17 +125,26 @@ impl Adapter {
if result.is_null() {
return Err("WintunOpenAdapter failed".into());
}
let luid = crate::ffi::alias_to_luid(name)?;
let index = crate::ffi::luid_to_index(&luid)?;
let guid = crate::ffi::luid_to_guid(&luid)?;
let guid = util::win_guid_to_u128(&guid);
Ok(Arc::new(Adapter {
adapter: UnsafeHandle(result),
wintun: wintun.clone(),
guid,
index,
luid,
}))
let call = || -> Result<Arc<Adapter>, Error> {
let luid = crate::ffi::alias_to_luid(name)?;
let index = crate::ffi::luid_to_index(&luid)?;
let guid = crate::ffi::luid_to_guid(&luid)?;
let guid = util::win_guid_to_u128(&guid);
Ok(Arc::new(Adapter {
adapter: UnsafeHandle(result),
wintun: wintun.clone(),
guid,
index,
luid,
}))
};
match call() {
Ok(adapter) => Ok(adapter),
Err(e) => {
unsafe { wintun.WintunCloseAdapter(result) };
Err(e)
}
}
}

/// Delete an adapter, consuming it in the process
Expand Down