Skip to content

Commit

Permalink
std: net: Add support for getting the system hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
Dallas Strouse authored and Dallas Strouse committed Jan 7, 2025
1 parent 243d2ca commit 10ff018
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
10 changes: 10 additions & 0 deletions library/std/src/net/hostname.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::ffi::OsString;

/// Returns the system hostname.
///
/// The only time this should ever error out is if the system is broken
/// in some way. Such errors depend on those cases.
#[unstable(feature = "gethostname", issue = "135142")]
pub fn hostname() -> crate::io::Result<OsString> {
crate::sys_common::net::gethostname()
}
6 changes: 5 additions & 1 deletion library/std/src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Networking primitives for TCP/UDP communication.
//!
//! This module provides networking functionality for the Transmission Control and User
//! Datagram Protocols, as well as types for IP and socket addresses.
//! Datagram Protocols, as well as types for IP and socket addresses, and functions related
//! to network properties.
//!
//! # Organization
//!
Expand All @@ -24,6 +25,8 @@
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::net::AddrParseError;

#[unstable(feature = "gethostname", issue = "135142")]
pub use self::hostname::hostname;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::ip_addr::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -36,6 +39,7 @@ pub use self::tcp::{Incoming, TcpListener, TcpStream};
pub use self::udp::UdpSocket;
use crate::io::{self, ErrorKind};

mod hostname;
mod ip_addr;
mod socket_addr;
mod tcp;
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/pal/windows/c/bindings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,7 @@ Windows.Win32.Networking.WinSock.FD_SET
Windows.Win32.Networking.WinSock.FIONBIO
Windows.Win32.Networking.WinSock.freeaddrinfo
Windows.Win32.Networking.WinSock.getaddrinfo
Windows.Win32.Networking.WinSock.gethostname
Windows.Win32.Networking.WinSock.getpeername
Windows.Win32.Networking.WinSock.getsockname
Windows.Win32.Networking.WinSock.getsockopt
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/pal/windows/c/windows_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ windows_targets::link!("ws2_32.dll" "system" fn closesocket(s : SOCKET) -> i32);
windows_targets::link!("ws2_32.dll" "system" fn connect(s : SOCKET, name : *const SOCKADDR, namelen : i32) -> i32);
windows_targets::link!("ws2_32.dll" "system" fn freeaddrinfo(paddrinfo : *const ADDRINFOA));
windows_targets::link!("ws2_32.dll" "system" fn getaddrinfo(pnodename : PCSTR, pservicename : PCSTR, phints : *const ADDRINFOA, ppresult : *mut *mut ADDRINFOA) -> i32);
windows_targets::link!("ws2_32.dll" "system" fn gethostname(name : PSTR, namelen : i32) -> i32);
windows_targets::link!("ws2_32.dll" "system" fn getpeername(s : SOCKET, name : *mut SOCKADDR, namelen : *mut i32) -> i32);
windows_targets::link!("ws2_32.dll" "system" fn getsockname(s : SOCKET, name : *mut SOCKADDR, namelen : *mut i32) -> i32);
windows_targets::link!("ws2_32.dll" "system" fn getsockopt(s : SOCKET, level : i32, optname : i32, optval : PSTR, optlen : *mut i32) -> i32);
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/pal/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub mod netc {
IP_DROP_MEMBERSHIP, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_TTL, IPPROTO_IP, IPPROTO_IPV6,
IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_MULTICAST_LOOP, IPV6_V6ONLY, SO_BROADCAST,
SO_RCVTIMEO, SO_SNDTIMEO, SOCK_DGRAM, SOCK_STREAM, SOCKADDR as sockaddr,
SOCKADDR_STORAGE as sockaddr_storage, SOL_SOCKET, bind, connect, freeaddrinfo, getpeername,
getsockname, getsockopt, listen, setsockopt,
SOCKADDR_STORAGE as sockaddr_storage, SOL_SOCKET, bind, connect, freeaddrinfo, gethostname,
getpeername, getsockname, getsockopt, listen, setsockopt,
};

#[allow(non_camel_case_types)]
Expand Down
27 changes: 26 additions & 1 deletion library/std/src/sys_common/net.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests;

use crate::ffi::{c_int, c_void};
use crate::ffi::{CStr, OsString, c_char, c_int, c_void};
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut};
use crate::net::{Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};
use crate::sys::common::small_c_string::run_with_cstr;
Expand Down Expand Up @@ -215,6 +215,31 @@ impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
}
}

////////////////////////////////////////////////////////////////////////////////
// gethostname
////////////////////////////////////////////////////////////////////////////////

pub fn gethostname() -> crate::io::Result<OsString> {
init();
// 255 bytes is the maximum allowable length for a hostname (as per the DNS spec),
// so we shouldn't ever have problems with this. I (@orowith2os) considered using a constant
// and letting the platform set the length, but it was determined after some discussion that
// this could break things if the platform changes their length. Possible alternative is to
// read the sysconf setting for the max hostname length, but that might be a bit too much work.
// The 256 byte length is to allow for the NUL terminator.
let mut temp_buffer: [c_char; 256] = [0; 256];

// SAFETY: should never be unsafe, as we're passing in a valid (0-initialized) buffer, and the
// length of said buffer.
unsafe {
cvt(c::gethostname(temp_buffer.as_mut_ptr() as _, temp_buffer.len() as _))?;
}

// SAFETY: we already know the pointer here is valid, we made it from safe Rust earlier.
let cstring = unsafe { CStr::from_ptr(temp_buffer.as_mut_ptr()) };
Ok(OsString::from(cstring.to_string_lossy().as_ref()))
}

////////////////////////////////////////////////////////////////////////////////
// TCP streams
////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 10ff018

Please sign in to comment.