#[cfg(all(any(unix, windows), feature = "std"))]
pub fn gethostname() -> std::ffi::OsString {
gethostname_impl()
}
#[cfg(all(unix, feature = "std"))]
#[inline]
fn gethostname_impl() -> std::ffi::OsString {
use libc::{c_char, sysconf, _SC_HOST_NAME_MAX};
use std::os::unix::ffi::OsStringExt;
let hostname_max = unsafe { sysconf(_SC_HOST_NAME_MAX) };
let mut buffer = vec![0; (hostname_max as usize) + 1];
let returncode = unsafe { libc::gethostname(buffer.as_mut_ptr() as *mut c_char, buffer.len()) };
if returncode != 0 {
panic!(
"gethostname failed: {}
Please report an issue to <https://github.com/swsnr/gethostname.rs/issues>!",
std::io::Error::last_os_error()
);
}
let end = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len());
buffer.resize(end, 0);
std::ffi::OsString::from_vec(buffer)
}
#[cfg(all(windows, feature = "std"))]
#[inline]
fn gethostname_impl() -> OsString {
use std::os::windows::ffi::OsStringExt;
pub const COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME: i32 = 5;
::windows_targets::link!("kernel32.dll" "system" fn GetComputerNameExW(nametype: i32, lpbuffer: *mut u16, nsize: *mut u32) -> i32);
let mut buffer_size: u32 = 0;
unsafe {
GetComputerNameExW(
COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME,
std::ptr::null_mut(),
&mut buffer_size,
)
};
assert!(
0 < buffer_size,
"GetComputerNameExW did not provide buffer size"
);
let mut buffer = vec![0_u16; buffer_size as usize];
unsafe {
if GetComputerNameExW(
COMPUTER_NAME_PHYSICAL_DNS_HOSTNAME,
buffer.as_mut_ptr(),
&mut buffer_size,
) == 0
{
panic!(
"GetComputerNameExW failed to read hostname.
Please report this issue to <https://github.com/swsnr/gethostname.rs/issues>!"
);
}
}
assert!(
buffer_size as usize == buffer.len() - 1,
"GetComputerNameExW changed the buffer size unexpectedly"
);
let end = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len());
OsString::from_wide(&buffer[0..end])
}
#[cfg(all(test, any(unix, windows), feature = "std"))]
mod tests {
use std::process::Command;
#[test]
fn gethostname_matches_system_hostname() {
let output = Command::new("hostname")
.output()
.expect("failed to get hostname");
if output.status.success() {
let hostname = String::from_utf8_lossy(&output.stdout);
assert!(
!hostname.is_empty(),
"Failed to get hostname: hostname empty?"
);
assert_eq!(
super::gethostname().into_string().unwrap().to_lowercase(),
hostname.trim_end().to_lowercase()
);
} else {
panic!(
"Failed to get hostname! {}",
String::from_utf8_lossy(&output.stderr)
);
}
}
#[test]
#[ignore]
fn gethostname_matches_fixed_hostname() {
assert_eq!(
super::gethostname().into_string().unwrap().to_lowercase(),
"hostname-for-testing"
);
}
}