Skip to content

Commit a6af22f

Browse files
committed
std: net: Add function to get the system hostname
1 parent b3b368a commit a6af22f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

library/std/src/net/hostname.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use libc::gethostname;
2+
use crate::ffi::CStr;
3+
use crate::ffi::OsString;
4+
use crate::os::raw::c_char;
5+
6+
/// Returns the system hostname.
7+
///
8+
/// The returned result will, on success, return the same result [`libc::gethostname`] would return
9+
/// (as it is implemented using the very same function), and on error, what `errno` contains, also
10+
/// set by [`libc::gethostname`].
11+
#[unstable(feature = "gethostname", issue = "135142")]
12+
pub fn hostname() -> crate::io::Result<OsString> {
13+
// 255 bytes is the maximum allowable length for a hostname (as per the DNS spec),
14+
// so we shouldn't ever have problems with this. I (@orowith2os) considered using a constant
15+
// and letting the platform set the length, but it was determined after some discussion that
16+
// this could break things if the platform changes their length. Possible alternative is to
17+
// read the sysconf setting for the max hostname length, but that might be a bit too much work.
18+
// The 256 byte length is to allow for the NUL terminator.
19+
let mut temp_buffer: [c_char; 256] = [0; 256];
20+
// 0 = no problem, and there isn't any other relevant error code to check for. Only stuff for
21+
// sethostname, and ENAMETOOLONG, which is only relevant for glibc 2.1 or newer. With the
22+
// previous information given in mind, we shouldn't ever encounter any error other than the
23+
// fact that the system *somehow* failed to get the hostname.
24+
// SAFETY: should never be unsafe, as we're passing in a valid (0-initialized) buffer, and the
25+
// length of said buffer.
26+
let gethostname_result = unsafe { gethostname(&mut temp_buffer as _, temp_buffer.len()) };
27+
28+
match gethostname_result {
29+
0 => {
30+
// SAFETY: we already know the pointer here is valid, we made it from safe Rust earlier.
31+
let cstring = unsafe { CStr::from_ptr(&mut temp_buffer as _) };
32+
return Ok(OsString::from(cstring.to_string_lossy().as_ref()));
33+
}
34+
_ => Err(crate::io::Error::last_os_error()),
35+
}
36+
}

library/std/src/net/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Networking primitives for TCP/UDP communication.
22
//!
33
//! This module provides networking functionality for the Transmission Control and User
4-
//! Datagram Protocols, as well as types for IP and socket addresses.
4+
//! Datagram Protocols, as well as types for IP and socket addresses, and functions related
5+
//! to network properties.
56
//!
67
//! # Organization
78
//!
@@ -35,13 +36,16 @@ pub use self::tcp::{Incoming, TcpListener, TcpStream};
3536
#[stable(feature = "rust1", since = "1.0.0")]
3637
pub use self::udp::UdpSocket;
3738
use crate::io::{self, ErrorKind};
39+
#[unstable(feature = "gethostname", issue = "135142")]
40+
pub use self::hostname::hostname;
3841

3942
mod ip_addr;
4043
mod socket_addr;
4144
mod tcp;
4245
#[cfg(test)]
4346
pub(crate) mod test;
4447
mod udp;
48+
mod hostname;
4549

4650
/// Possible values which can be passed to the [`TcpStream::shutdown`] method.
4751
#[derive(Copy, Clone, PartialEq, Eq, Debug)]

0 commit comments

Comments
 (0)