Skip to content

Commit 4170968

Browse files
Dallas Strouseorowith2os
Dallas Strouse
authored andcommitted
std: net: Add support for getting the system hostname
1 parent 93ba568 commit 4170968

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

library/std/src/net/hostname.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use crate::ffi::OsString;
2+
3+
/// Returns the system hostname.
4+
///
5+
/// The only time this should ever error out is if the system is broken
6+
/// in some way. Such errors depend on those cases.
7+
#[unstable(feature = "gethostname", issue = "135142")]
8+
pub fn hostname() -> crate::io::Result<OsString> {
9+
crate::sys_common::net::gethostname()
10+
}

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
//!
@@ -24,6 +25,8 @@
2425
#[stable(feature = "rust1", since = "1.0.0")]
2526
pub use core::net::AddrParseError;
2627

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

42+
mod hostname;
3943
mod ip_addr;
4044
mod socket_addr;
4145
mod tcp;

library/std/src/sys/pal/windows/c/bindings.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,7 @@ Windows.Win32.Networking.WinSock.FD_SET
19781978
Windows.Win32.Networking.WinSock.FIONBIO
19791979
Windows.Win32.Networking.WinSock.freeaddrinfo
19801980
Windows.Win32.Networking.WinSock.getaddrinfo
1981+
Windows.Win32.Networking.WinSock.gethostname
19811982
Windows.Win32.Networking.WinSock.getpeername
19821983
Windows.Win32.Networking.WinSock.getsockname
19831984
Windows.Win32.Networking.WinSock.getsockopt

library/std/src/sys/pal/windows/c/windows_sys.rs

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ windows_targets::link!("ws2_32.dll" "system" fn closesocket(s : SOCKET) -> i32);
126126
windows_targets::link!("ws2_32.dll" "system" fn connect(s : SOCKET, name : *const SOCKADDR, namelen : i32) -> i32);
127127
windows_targets::link!("ws2_32.dll" "system" fn freeaddrinfo(paddrinfo : *const ADDRINFOA));
128128
windows_targets::link!("ws2_32.dll" "system" fn getaddrinfo(pnodename : PCSTR, pservicename : PCSTR, phints : *const ADDRINFOA, ppresult : *mut *mut ADDRINFOA) -> i32);
129+
windows_targets::link!("ws2_32.dll" "system" fn gethostname(name : PSTR, namelen : i32) -> i32);
129130
windows_targets::link!("ws2_32.dll" "system" fn getpeername(s : SOCKET, name : *mut SOCKADDR, namelen : *mut i32) -> i32);
130131
windows_targets::link!("ws2_32.dll" "system" fn getsockname(s : SOCKET, name : *mut SOCKADDR, namelen : *mut i32) -> i32);
131132
windows_targets::link!("ws2_32.dll" "system" fn getsockopt(s : SOCKET, level : i32, optname : i32, optval : PSTR, optlen : *mut i32) -> i32);

library/std/src/sys/pal/windows/net.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub mod netc {
3131
IP_DROP_MEMBERSHIP, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_TTL, IPPROTO_IP, IPPROTO_IPV6,
3232
IPV6_ADD_MEMBERSHIP, IPV6_DROP_MEMBERSHIP, IPV6_MULTICAST_LOOP, IPV6_V6ONLY, SO_BROADCAST,
3333
SO_RCVTIMEO, SO_SNDTIMEO, SOCK_DGRAM, SOCK_STREAM, SOCKADDR as sockaddr,
34-
SOCKADDR_STORAGE as sockaddr_storage, SOL_SOCKET, bind, connect, freeaddrinfo, getpeername,
35-
getsockname, getsockopt, listen, setsockopt,
34+
SOCKADDR_STORAGE as sockaddr_storage, SOL_SOCKET, bind, connect, freeaddrinfo, gethostname,
35+
getpeername, getsockname, getsockopt, listen, setsockopt,
3636
};
3737

3838
#[allow(non_camel_case_types)]

library/std/src/sys_common/net.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(test)]
22
mod tests;
33

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

218+
////////////////////////////////////////////////////////////////////////////////
219+
// gethostname
220+
////////////////////////////////////////////////////////////////////////////////
221+
222+
pub fn gethostname() -> crate::io::Result<OsString> {
223+
init();
224+
// 255 bytes is the maximum allowable length for a hostname (as per the DNS spec),
225+
// so we shouldn't ever have problems with this. I (@orowith2os) considered using a constant
226+
// and letting the platform set the length, but it was determined after some discussion that
227+
// this could break things if the platform changes their length. Possible alternative is to
228+
// read the sysconf setting for the max hostname length, but that might be a bit too much work.
229+
// The 256 byte length is to allow for the NUL terminator.
230+
let mut temp_buffer: [c_char; 256] = [0; 256];
231+
232+
// SAFETY: should never be unsafe, as we're passing in a valid (0-initialized) buffer, and the
233+
// length of said buffer.
234+
unsafe {
235+
cvt(c::gethostname(temp_buffer.as_mut_ptr() as _, temp_buffer.len() as _))?;
236+
}
237+
238+
// SAFETY: we already know the pointer here is valid, we made it from safe Rust earlier.
239+
let cstring = unsafe { CStr::from_ptr(temp_buffer.as_mut_ptr()) };
240+
Ok(OsString::from(cstring.to_string_lossy().as_ref()))
241+
}
242+
218243
////////////////////////////////////////////////////////////////////////////////
219244
// TCP streams
220245
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)