Skip to content

Commit

Permalink
Try AdapterName if FriendlyName doesn't match (#29)
Browse files Browse the repository at this point in the history
* Try AdapterName if FriendlyName doesn't match

* some slight style tweaks

---------

Co-authored-by: Wesley Norris <[email protected]>
  • Loading branch information
daviessm and repnop authored Nov 14, 2023
1 parent 7044d5d commit a32d68d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub enum MacAddressError {
InternalError,
}


#[cfg(any(
target_os = "linux",
target_os = "macos",
Expand Down
23 changes: 21 additions & 2 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use core::convert::TryInto;
use std::{convert::TryFrom, ffi::OsString, os::windows::ffi::OsStringExt, ptr, slice};
use std::{
convert::{TryFrom, TryInto},
ffi::CStr,
ffi::OsString,
os::windows::ffi::OsStringExt,
ptr, slice,
};
use winapi::shared::{ntdef::ULONG, winerror::ERROR_SUCCESS, ws2def::AF_UNSPEC};
use winapi::um::{iphlpapi::GetAdaptersAddresses, iptypes::IP_ADAPTER_ADDRESSES_LH};

Expand Down Expand Up @@ -36,6 +41,20 @@ pub fn get_mac(name: Option<&str>) -> Result<Option<[u8; 6]>, MacAddressError> {

if adapter_name == name {
return Ok(Some(bytes));
} else {
#[cfg(not(target_pointer_width = "32"))]
let adapter_name = unsafe { CStr::from_ptr((*ptr).AdapterName) };

#[cfg(target_pointer_width = "32")]
let adapter_name = unsafe { CStr::from_ptr(ptr.read_unaligned().AdapterName) };

match adapter_name.to_str() {
Ok(s) if s == name => return Ok(Some(bytes)),
Ok(_) => {}
Err(_) => {
return Err(MacAddressError::InternalError);
}
}
}
} else if bytes.iter().any(|&x| x != 0) {
return Ok(Some(bytes));
Expand Down

0 comments on commit a32d68d

Please sign in to comment.