2019-11-27 18:28:39 +00:00
|
|
|
use crate::cmp;
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::ffi::CStr;
|
2019-04-27 15:34:08 +00:00
|
|
|
use crate::io::{self, IoSlice, IoSliceMut};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::mem;
|
2019-11-27 18:28:39 +00:00
|
|
|
use crate::net::{Shutdown, SocketAddr};
|
2021-07-01 04:44:30 +00:00
|
|
|
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::str;
|
|
|
|
use crate::sys::fd::FileDesc;
|
|
|
|
use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
|
2019-11-27 18:28:39 +00:00
|
|
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::time::{Duration, Instant};
|
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
use libc::{c_int, c_void, size_t, sockaddr, socklen_t, MSG_PEEK};
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2021-08-30 17:02:15 +00:00
|
|
|
cfg_if::cfg_if! {
|
|
|
|
if #[cfg(target_vendor = "apple")] {
|
|
|
|
use libc::SO_LINGER_SEC as SO_LINGER;
|
|
|
|
} else {
|
|
|
|
use libc::SO_LINGER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 07:15:56 +00:00
|
|
|
pub use crate::sys::{cvt, cvt_r};
|
2019-02-10 19:23:21 +00:00
|
|
|
|
|
|
|
#[allow(unused_extern_crates)]
|
2016-02-02 09:39:59 +00:00
|
|
|
pub extern crate libc as netc;
|
2015-02-06 00:50:11 +00:00
|
|
|
|
|
|
|
pub type wrlen_t = size_t;
|
|
|
|
|
|
|
|
pub struct Socket(FileDesc);
|
|
|
|
|
|
|
|
pub fn init() {}
|
|
|
|
|
|
|
|
pub fn cvt_gai(err: c_int) -> io::Result<()> {
|
2016-09-24 22:00:00 +00:00
|
|
|
if err == 0 {
|
2019-11-27 18:28:39 +00:00
|
|
|
return Ok(());
|
2016-09-24 22:00:00 +00:00
|
|
|
}
|
2018-01-10 09:13:03 +00:00
|
|
|
|
|
|
|
// We may need to trigger a glibc workaround. See on_resolver_failure() for details.
|
|
|
|
on_resolver_failure();
|
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(not(target_os = "espidf"))]
|
|
|
|
if err == libc::EAI_SYSTEM {
|
2019-11-27 18:28:39 +00:00
|
|
|
return Err(io::Error::last_os_error());
|
2016-09-24 22:00:00 +00:00
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(not(target_os = "espidf"))]
|
2015-02-06 00:50:11 +00:00
|
|
|
let detail = unsafe {
|
2019-11-27 18:28:39 +00:00
|
|
|
str::from_utf8(CStr::from_ptr(libc::gai_strerror(err)).to_bytes()).unwrap().to_owned()
|
2015-02-06 00:50:11 +00:00
|
|
|
};
|
2021-07-29 17:18:22 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "espidf")]
|
|
|
|
let detail = "";
|
|
|
|
|
2019-11-27 18:28:39 +00:00
|
|
|
Err(io::Error::new(
|
2021-06-15 12:27:31 +00:00
|
|
|
io::ErrorKind::Uncategorized,
|
2022-02-12 19:16:17 +00:00
|
|
|
&format!("failed to lookup address information: {detail}")[..],
|
2019-11-27 18:28:39 +00:00
|
|
|
))
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Socket {
|
|
|
|
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 21:22:33 +00:00
|
|
|
let fam = match *addr {
|
|
|
|
SocketAddr::V4(..) => libc::AF_INET,
|
|
|
|
SocketAddr::V6(..) => libc::AF_INET6,
|
2015-02-06 00:50:11 +00:00
|
|
|
};
|
2016-03-17 03:50:45 +00:00
|
|
|
Socket::new_raw(fam, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_raw(fam: c_int, ty: c_int) -> io::Result<Socket> {
|
2015-02-06 00:50:11 +00:00
|
|
|
unsafe {
|
2020-07-21 21:32:36 +00:00
|
|
|
cfg_if::cfg_if! {
|
2020-10-30 13:05:53 +00:00
|
|
|
if #[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "netbsd",
|
2021-05-27 06:17:13 +00:00
|
|
|
target_os = "openbsd",
|
2020-10-30 13:05:53 +00:00
|
|
|
))] {
|
|
|
|
// On platforms that support it we pass the SOCK_CLOEXEC
|
|
|
|
// flag to atomically create the socket and set it as
|
|
|
|
// CLOEXEC. On Linux this was added in 2.6.27.
|
2020-07-21 21:32:36 +00:00
|
|
|
let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?;
|
2021-07-01 04:44:30 +00:00
|
|
|
Ok(Socket(FileDesc::from_raw_fd(fd)))
|
2020-07-21 21:32:36 +00:00
|
|
|
} else {
|
|
|
|
let fd = cvt(libc::socket(fam, ty, 0))?;
|
2021-07-01 04:44:30 +00:00
|
|
|
let fd = FileDesc::from_raw_fd(fd);
|
2020-07-21 21:32:36 +00:00
|
|
|
fd.set_cloexec()?;
|
|
|
|
let socket = Socket(fd);
|
|
|
|
|
|
|
|
// macOS and iOS use `SO_NOSIGPIPE` as a `setsockopt`
|
|
|
|
// flag to disable `SIGPIPE` emission on socket.
|
|
|
|
#[cfg(target_vendor = "apple")]
|
|
|
|
setsockopt(&socket, libc::SOL_SOCKET, libc::SO_NOSIGPIPE, 1)?;
|
|
|
|
|
|
|
|
Ok(socket)
|
2016-02-04 21:30:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:38:25 +00:00
|
|
|
#[cfg(not(target_os = "vxworks"))]
|
2016-03-17 03:50:45 +00:00
|
|
|
pub fn new_pair(fam: c_int, ty: c_int) -> io::Result<(Socket, Socket)> {
|
|
|
|
unsafe {
|
|
|
|
let mut fds = [0, 0];
|
|
|
|
|
2020-07-21 21:32:36 +00:00
|
|
|
cfg_if::cfg_if! {
|
2020-10-30 13:05:53 +00:00
|
|
|
if #[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "netbsd",
|
2021-05-27 06:17:13 +00:00
|
|
|
target_os = "openbsd",
|
2020-10-30 13:05:53 +00:00
|
|
|
))] {
|
2020-07-21 21:32:36 +00:00
|
|
|
// Like above, set cloexec atomically
|
|
|
|
cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?;
|
2021-07-01 04:44:30 +00:00
|
|
|
Ok((Socket(FileDesc::from_raw_fd(fds[0])), Socket(FileDesc::from_raw_fd(fds[1]))))
|
2020-07-21 21:32:36 +00:00
|
|
|
} else {
|
|
|
|
cvt(libc::socketpair(fam, ty, 0, fds.as_mut_ptr()))?;
|
2021-07-01 04:44:30 +00:00
|
|
|
let a = FileDesc::from_raw_fd(fds[0]);
|
|
|
|
let b = FileDesc::from_raw_fd(fds[1]);
|
2020-07-21 21:32:36 +00:00
|
|
|
a.set_cloexec()?;
|
|
|
|
b.set_cloexec()?;
|
|
|
|
Ok((Socket(a), Socket(b)))
|
2016-03-17 03:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 18:38:25 +00:00
|
|
|
#[cfg(target_os = "vxworks")]
|
|
|
|
pub fn new_pair(_fam: c_int, _ty: c_int) -> io::Result<(Socket, Socket)> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:46:24 +00:00
|
|
|
pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
|
|
|
|
self.set_nonblocking(true)?;
|
|
|
|
let r = unsafe {
|
|
|
|
let (addrp, len) = addr.into_inner();
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt(libc::connect(self.as_raw_fd(), addrp, len))
|
2017-07-05 06:46:24 +00:00
|
|
|
};
|
|
|
|
self.set_nonblocking(false)?;
|
|
|
|
|
|
|
|
match r {
|
|
|
|
Ok(_) => return Ok(()),
|
|
|
|
// there's no ErrorKind for EINPROGRESS :(
|
|
|
|
Err(ref e) if e.raw_os_error() == Some(libc::EINPROGRESS) => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
|
2021-07-01 04:44:30 +00:00
|
|
|
let mut pollfd = libc::pollfd { fd: self.as_raw_fd(), events: libc::POLLOUT, revents: 0 };
|
2017-07-05 06:46:24 +00:00
|
|
|
|
|
|
|
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
|
2021-08-08 08:04:33 +00:00
|
|
|
return Err(io::const_io_error!(
|
2019-11-27 18:28:39 +00:00
|
|
|
io::ErrorKind::InvalidInput,
|
2021-08-08 08:04:33 +00:00
|
|
|
"cannot set a 0 duration timeout",
|
2019-11-27 18:28:39 +00:00
|
|
|
));
|
2017-07-05 06:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
if elapsed >= timeout {
|
2021-08-08 08:04:33 +00:00
|
|
|
return Err(io::const_io_error!(io::ErrorKind::TimedOut, "connection timed out"));
|
2017-07-05 06:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let timeout = timeout - elapsed;
|
2019-11-27 18:28:39 +00:00
|
|
|
let mut timeout = timeout
|
|
|
|
.as_secs()
|
2017-07-05 06:46:24 +00:00
|
|
|
.saturating_mul(1_000)
|
|
|
|
.saturating_add(timeout.subsec_nanos() as u64 / 1_000_000);
|
|
|
|
if timeout == 0 {
|
|
|
|
timeout = 1;
|
|
|
|
}
|
|
|
|
|
2020-06-02 07:59:11 +00:00
|
|
|
let timeout = cmp::min(timeout, c_int::MAX as u64) as c_int;
|
2017-07-05 06:46:24 +00:00
|
|
|
|
|
|
|
match unsafe { libc::poll(&mut pollfd, 1, timeout) } {
|
|
|
|
-1 => {
|
|
|
|
let err = io::Error::last_os_error();
|
|
|
|
if err.kind() != io::ErrorKind::Interrupted {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
0 => {}
|
|
|
|
_ => {
|
2017-10-14 02:22:33 +00:00
|
|
|
// linux returns POLLOUT|POLLERR|POLLHUP for refused connections (!), so look
|
|
|
|
// for POLLHUP rather than read readiness
|
|
|
|
if pollfd.revents & libc::POLLHUP != 0 {
|
2019-11-27 18:28:39 +00:00
|
|
|
let e = self.take_error()?.unwrap_or_else(|| {
|
2021-08-08 08:04:33 +00:00
|
|
|
io::const_io_error!(
|
2021-06-15 12:27:31 +00:00
|
|
|
io::ErrorKind::Uncategorized,
|
2021-08-08 08:04:33 +00:00
|
|
|
"no error set after POLLHUP",
|
2021-03-21 19:22:38 +00:00
|
|
|
)
|
2019-11-27 18:28:39 +00:00
|
|
|
});
|
2017-10-14 02:22:33 +00:00
|
|
|
return Err(e);
|
2017-07-05 06:46:24 +00:00
|
|
|
}
|
2017-10-14 02:22:33 +00:00
|
|
|
|
2017-07-05 06:46:24 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:28:39 +00:00
|
|
|
pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
|
2016-02-04 23:22:41 +00:00
|
|
|
// Unfortunately the only known way right now to accept a socket and
|
|
|
|
// atomically set the CLOEXEC flag is to use the `accept4` syscall on
|
2020-10-30 13:05:53 +00:00
|
|
|
// platforms that support it. On Linux, this was added in 2.6.28,
|
|
|
|
// glibc 2.10 and musl 0.9.5.
|
2020-07-21 21:32:36 +00:00
|
|
|
cfg_if::cfg_if! {
|
2020-10-30 13:05:53 +00:00
|
|
|
if #[cfg(any(
|
2021-02-24 11:24:36 +00:00
|
|
|
target_os = "android",
|
2020-10-30 13:05:53 +00:00
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "illumos",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "netbsd",
|
2021-05-27 06:17:13 +00:00
|
|
|
target_os = "openbsd",
|
2020-10-30 13:05:53 +00:00
|
|
|
))] {
|
2021-07-01 04:44:30 +00:00
|
|
|
unsafe {
|
|
|
|
let fd = cvt_r(|| libc::accept4(self.as_raw_fd(), storage, len, libc::SOCK_CLOEXEC))?;
|
|
|
|
Ok(Socket(FileDesc::from_raw_fd(fd)))
|
|
|
|
}
|
2020-07-21 21:32:36 +00:00
|
|
|
} else {
|
2021-07-01 04:44:30 +00:00
|
|
|
unsafe {
|
|
|
|
let fd = cvt_r(|| libc::accept(self.as_raw_fd(), storage, len))?;
|
|
|
|
let fd = FileDesc::from_raw_fd(fd);
|
|
|
|
fd.set_cloexec()?;
|
|
|
|
Ok(Socket(fd))
|
|
|
|
}
|
2016-02-04 23:22:41 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<Socket> {
|
2016-01-21 05:24:23 +00:00
|
|
|
self.0.duplicate().map(Socket)
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 03:11:56 +00:00
|
|
|
fn recv_with_flags(&self, buf: &mut [u8], flags: c_int) -> io::Result<usize> {
|
|
|
|
let ret = cvt(unsafe {
|
2021-07-01 04:44:30 +00:00
|
|
|
libc::recv(self.as_raw_fd(), buf.as_mut_ptr() as *mut c_void, buf.len(), flags)
|
2017-01-11 03:11:56 +00:00
|
|
|
})?;
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
2017-01-11 03:11:56 +00:00
|
|
|
self.recv_with_flags(buf, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
self.recv_with_flags(buf, MSG_PEEK)
|
|
|
|
}
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
self.0.read_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
|
|
|
self.0.is_read_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 18:28:39 +00:00
|
|
|
fn recv_from_with_flags(
|
|
|
|
&self,
|
|
|
|
buf: &mut [u8],
|
|
|
|
flags: c_int,
|
|
|
|
) -> io::Result<(usize, SocketAddr)> {
|
2017-01-11 03:11:56 +00:00
|
|
|
let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
|
|
|
|
let mut addrlen = mem::size_of_val(&storage) as libc::socklen_t;
|
|
|
|
|
|
|
|
let n = cvt(unsafe {
|
2019-11-27 18:28:39 +00:00
|
|
|
libc::recvfrom(
|
2021-07-01 04:44:30 +00:00
|
|
|
self.as_raw_fd(),
|
2019-11-27 18:28:39 +00:00
|
|
|
buf.as_mut_ptr() as *mut c_void,
|
|
|
|
buf.len(),
|
|
|
|
flags,
|
|
|
|
&mut storage as *mut _ as *mut _,
|
|
|
|
&mut addrlen,
|
|
|
|
)
|
2017-01-11 03:11:56 +00:00
|
|
|
})?;
|
|
|
|
Ok((n as usize, sockaddr_to_addr(&storage, addrlen as usize)?))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
|
|
|
self.recv_from_with_flags(buf, 0)
|
|
|
|
}
|
|
|
|
|
2020-09-22 11:17:12 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
))]
|
2020-08-21 12:52:10 +00:00
|
|
|
pub fn recv_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
|
2021-07-01 04:44:30 +00:00
|
|
|
let n = cvt(unsafe { libc::recvmsg(self.as_raw_fd(), msg, libc::MSG_CMSG_CLOEXEC) })?;
|
2020-08-21 12:52:10 +00:00
|
|
|
Ok(n as usize)
|
|
|
|
}
|
|
|
|
|
2017-01-11 03:11:56 +00:00
|
|
|
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
|
|
|
self.recv_from_with_flags(buf, MSG_PEEK)
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
2015-05-27 06:47:03 +00:00
|
|
|
|
2016-03-17 03:50:45 +00:00
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.write(buf)
|
|
|
|
}
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
self.0.write_vectored(bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
|
|
|
self.0.is_write_vectored()
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 11:17:12 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
))]
|
2020-08-21 12:52:10 +00:00
|
|
|
pub fn send_msg(&self, msg: &mut libc::msghdr) -> io::Result<usize> {
|
2021-07-01 04:44:30 +00:00
|
|
|
let n = cvt(unsafe { libc::sendmsg(self.as_raw_fd(), msg, 0) })?;
|
2020-08-21 12:52:10 +00:00
|
|
|
Ok(n as usize)
|
|
|
|
}
|
|
|
|
|
2015-05-27 06:47:03 +00:00
|
|
|
pub fn set_timeout(&self, dur: Option<Duration>, kind: libc::c_int) -> io::Result<()> {
|
|
|
|
let timeout = match dur {
|
|
|
|
Some(dur) => {
|
2015-07-06 06:20:00 +00:00
|
|
|
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
|
2021-08-08 08:04:33 +00:00
|
|
|
return Err(io::const_io_error!(
|
2019-11-27 18:28:39 +00:00
|
|
|
io::ErrorKind::InvalidInput,
|
2021-08-08 08:04:33 +00:00
|
|
|
"cannot set a 0 duration timeout",
|
2019-11-27 18:28:39 +00:00
|
|
|
));
|
2015-05-27 06:47:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 07:59:11 +00:00
|
|
|
let secs = if dur.as_secs() > libc::time_t::MAX as u64 {
|
|
|
|
libc::time_t::MAX
|
2015-05-27 06:47:03 +00:00
|
|
|
} else {
|
2015-07-06 06:20:00 +00:00
|
|
|
dur.as_secs() as libc::time_t
|
2015-05-27 06:47:03 +00:00
|
|
|
};
|
|
|
|
let mut timeout = libc::timeval {
|
|
|
|
tv_sec: secs,
|
2020-03-01 19:09:14 +00:00
|
|
|
tv_usec: dur.subsec_micros() as libc::suseconds_t,
|
2015-05-27 06:47:03 +00:00
|
|
|
};
|
|
|
|
if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
|
|
|
|
timeout.tv_usec = 1;
|
|
|
|
}
|
|
|
|
timeout
|
|
|
|
}
|
2019-11-27 18:28:39 +00:00
|
|
|
None => libc::timeval { tv_sec: 0, tv_usec: 0 },
|
2015-05-27 06:47:03 +00:00
|
|
|
};
|
|
|
|
setsockopt(self, libc::SOL_SOCKET, kind, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timeout(&self, kind: libc::c_int) -> io::Result<Option<Duration>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: libc::timeval = getsockopt(self, libc::SOL_SOCKET, kind)?;
|
2015-05-27 06:47:03 +00:00
|
|
|
if raw.tv_sec == 0 && raw.tv_usec == 0 {
|
|
|
|
Ok(None)
|
|
|
|
} else {
|
|
|
|
let sec = raw.tv_sec as u64;
|
|
|
|
let nsec = (raw.tv_usec as u32) * 1000;
|
|
|
|
Ok(Some(Duration::new(sec, nsec)))
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 00:23:22 +00:00
|
|
|
|
|
|
|
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
|
|
|
|
let how = match how {
|
|
|
|
Shutdown::Write => libc::SHUT_WR,
|
|
|
|
Shutdown::Read => libc::SHUT_RD,
|
|
|
|
Shutdown::Both => libc::SHUT_RDWR,
|
|
|
|
};
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt(unsafe { libc::shutdown(self.as_raw_fd(), how) })?;
|
2015-11-03 00:23:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2016-02-27 22:15:19 +00:00
|
|
|
|
2021-08-30 17:02:15 +00:00
|
|
|
pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
|
|
|
|
let linger = libc::linger {
|
|
|
|
l_onoff: linger.is_some() as libc::c_int,
|
2021-08-30 18:00:21 +00:00
|
|
|
l_linger: linger.unwrap_or_default().as_secs() as libc::c_int,
|
2021-08-30 17:02:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
setsockopt(self, libc::SOL_SOCKET, SO_LINGER, linger)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn linger(&self) -> io::Result<Option<Duration>> {
|
|
|
|
let val: libc::linger = getsockopt(self, libc::SOL_SOCKET, SO_LINGER)?;
|
|
|
|
|
|
|
|
Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
|
|
|
|
}
|
|
|
|
|
2016-02-28 05:05:32 +00:00
|
|
|
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
|
|
|
|
setsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY, nodelay as c_int)
|
2016-02-27 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
2016-02-28 05:05:32 +00:00
|
|
|
pub fn nodelay(&self) -> io::Result<bool> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: c_int = getsockopt(self, libc::IPPROTO_TCP, libc::TCP_NODELAY)?;
|
2016-02-28 05:05:32 +00:00
|
|
|
Ok(raw != 0)
|
2016-02-27 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 21:32:53 +00:00
|
|
|
#[cfg(any(target_os = "android", target_os = "linux",))]
|
2020-09-07 16:00:01 +00:00
|
|
|
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
|
2020-09-17 09:32:20 +00:00
|
|
|
setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)
|
2020-09-07 16:00:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 21:32:53 +00:00
|
|
|
#[cfg(any(target_os = "android", target_os = "linux",))]
|
2020-09-07 16:00:01 +00:00
|
|
|
pub fn passcred(&self) -> io::Result<bool> {
|
|
|
|
let passcred: libc::c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)?;
|
|
|
|
Ok(passcred != 0)
|
|
|
|
}
|
|
|
|
|
2021-08-14 09:42:17 +00:00
|
|
|
#[cfg(target_os = "netbsd")]
|
|
|
|
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
|
|
|
|
setsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS, passcred as libc::c_int)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "netbsd")]
|
|
|
|
pub fn passcred(&self) -> io::Result<bool> {
|
|
|
|
let passcred: libc::c_int = getsockopt(self, 0 as libc::c_int, libc::LOCAL_CREDS)?;
|
|
|
|
Ok(passcred != 0)
|
|
|
|
}
|
|
|
|
|
2020-04-15 00:44:06 +00:00
|
|
|
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
|
2016-02-27 22:15:19 +00:00
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
2016-09-07 15:21:10 +00:00
|
|
|
let mut nonblocking = nonblocking as libc::c_int;
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt(unsafe { libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking) }).map(drop)
|
2016-02-27 22:15:19 +00:00
|
|
|
}
|
2016-03-17 03:50:45 +00:00
|
|
|
|
2020-04-15 00:44:06 +00:00
|
|
|
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
|
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
|
|
|
// FIONBIO is inadequate for sockets on illumos/Solaris, so use the
|
|
|
|
// fcntl(F_[GS]ETFL)-based method provided by FileDesc instead.
|
|
|
|
self.0.set_nonblocking(nonblocking)
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:50:45 +00:00
|
|
|
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
|
2019-11-27 18:28:39 +00:00
|
|
|
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
|
2016-03-17 03:50:45 +00:00
|
|
|
}
|
2021-07-01 04:44:30 +00:00
|
|
|
|
|
|
|
// This is used by sys_common code to abstract over Windows and Unix.
|
|
|
|
pub fn as_raw(&self) -> RawFd {
|
|
|
|
self.as_raw_fd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsInner<FileDesc> for Socket {
|
|
|
|
fn as_inner(&self) -> &FileDesc {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoInner<FileDesc> for Socket {
|
|
|
|
fn into_inner(self) -> FileDesc {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromInner<FileDesc> for Socket {
|
|
|
|
fn from_inner(file_desc: FileDesc) -> Self {
|
|
|
|
Self(file_desc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsFd for Socket {
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
self.0.as_fd()
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 04:44:30 +00:00
|
|
|
impl AsRawFd for Socket {
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
self.0.as_raw_fd()
|
2019-11-27 18:28:39 +00:00
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
|
2021-07-01 04:44:30 +00:00
|
|
|
impl IntoRawFd for Socket {
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
|
|
|
self.0.into_raw_fd()
|
2019-11-27 18:28:39 +00:00
|
|
|
}
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 23:18:29 +00:00
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
|
2021-07-01 04:44:30 +00:00
|
|
|
impl FromRawFd for Socket {
|
|
|
|
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
|
|
|
|
Self(FromRawFd::from_raw_fd(raw_fd))
|
2019-11-27 18:28:39 +00:00
|
|
|
}
|
2015-07-16 06:31:24 +00:00
|
|
|
}
|
2017-10-01 21:42:18 +00:00
|
|
|
|
|
|
|
// In versions of glibc prior to 2.26, there's a bug where the DNS resolver
|
|
|
|
// will cache the contents of /etc/resolv.conf, so changes to that file on disk
|
|
|
|
// can be ignored by a long-running program. That can break DNS lookups on e.g.
|
|
|
|
// laptops where the network comes and goes. See
|
|
|
|
// https://sourceware.org/bugzilla/show_bug.cgi?id=984. Note however that some
|
|
|
|
// distros including Debian have patched glibc to fix this for a long time.
|
|
|
|
//
|
|
|
|
// A workaround for this bug is to call the res_init libc function, to clear
|
|
|
|
// the cached configs. Unfortunately, while we believe glibc's implementation
|
|
|
|
// of res_init is thread-safe, we know that other implementations are not
|
|
|
|
// (https://github.com/rust-lang/rust/issues/43592). Code here in libstd could
|
|
|
|
// try to synchronize its res_init calls with a Mutex, but that wouldn't
|
|
|
|
// protect programs that call into libc in other ways. So instead of calling
|
|
|
|
// res_init unconditionally, we call it only when we detect we're linking
|
|
|
|
// against glibc version < 2.26. (That is, when we both know its needed and
|
|
|
|
// believe it's thread-safe).
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 20:58:38 +00:00
|
|
|
#[cfg(all(target_os = "linux", target_env = "gnu"))]
|
2018-01-10 09:13:03 +00:00
|
|
|
fn on_resolver_failure() {
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::sys;
|
2018-03-02 20:50:07 +00:00
|
|
|
|
2017-10-01 21:42:18 +00:00
|
|
|
// If the version fails to parse, we treat it the same as "not glibc".
|
2018-03-02 20:50:07 +00:00
|
|
|
if let Some(version) = sys::os::glibc_version() {
|
|
|
|
if version < (2, 26) {
|
|
|
|
unsafe { libc::res_init() };
|
2017-10-01 21:42:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 20:58:38 +00:00
|
|
|
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
|
2018-01-10 09:13:03 +00:00
|
|
|
fn on_resolver_failure() {}
|