diff options
Diffstat (limited to 'vendor/rustix/src/backend/libc/net/syscalls.rs')
-rw-r--r-- | vendor/rustix/src/backend/libc/net/syscalls.rs | 106 |
1 files changed, 101 insertions, 5 deletions
diff --git a/vendor/rustix/src/backend/libc/net/syscalls.rs b/vendor/rustix/src/backend/libc/net/syscalls.rs index b4550583c..2e968ba3e 100644 --- a/vendor/rustix/src/backend/libc/net/syscalls.rs +++ b/vendor/rustix/src/backend/libc/net/syscalls.rs @@ -358,6 +358,7 @@ pub(crate) fn sendmsg_unix( #[cfg(not(any( apple, windows, + target_os = "aix", target_os = "espidf", target_os = "haiku", target_os = "redox", @@ -396,6 +397,7 @@ pub(crate) fn acceptfrom(sockfd: BorrowedFd<'_>) -> io::Result<(OwnedFd, Option< #[cfg(not(any( apple, windows, + target_os = "aix", target_os = "espidf", target_os = "haiku", target_os = "nto", @@ -427,6 +429,7 @@ pub(crate) fn acceptfrom_with( #[cfg(any( apple, windows, + target_os = "aix", target_os = "espidf", target_os = "haiku", target_os = "nto" @@ -440,6 +443,7 @@ pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, _flags: SocketFlags) -> io::Re #[cfg(any( apple, windows, + target_os = "aix", target_os = "espidf", target_os = "haiku", target_os = "nto" @@ -517,8 +521,24 @@ pub(crate) mod sockopt { use super::{c, in6_addr_new, in_addr_new, BorrowedFd}; use crate::io; use crate::net::sockopt::Timeout; + #[cfg(not(any( + apple, + windows, + target_os = "aix", + target_os = "dragonfly", + target_os = "emscripten", + target_os = "espidf", + target_os = "haiku", + target_os = "netbsd", + target_os = "nto", + )))] + use crate::net::AddressFamily; use crate::net::{Ipv4Addr, Ipv6Addr, SocketType}; use crate::utils::as_mut_ptr; + #[cfg(apple)] + use c::TCP_KEEPALIVE as TCP_KEEPIDLE; + #[cfg(not(any(apple, target_os = "openbsd", target_os = "haiku", target_os = "nto")))] + use c::TCP_KEEPIDLE; use core::time::Duration; #[cfg(windows)] use windows_sys::Win32::Foundation::BOOL; @@ -597,6 +617,11 @@ pub(crate) mod sockopt { } #[inline] + pub(crate) fn get_socket_reuseaddr(fd: BorrowedFd<'_>) -> io::Result<bool> { + getsockopt(fd, c::SOL_SOCKET as _, c::SO_REUSEADDR).map(to_bool) + } + + #[inline] pub(crate) fn set_socket_broadcast(fd: BorrowedFd<'_>, broadcast: bool) -> io::Result<()> { setsockopt( fd, @@ -618,11 +643,7 @@ pub(crate) mod sockopt { ) -> io::Result<()> { // Convert `linger` to seconds, rounding up. let l_linger = if let Some(linger) = linger { - let mut l_linger = linger.as_secs(); - if linger.subsec_nanos() != 0 { - l_linger = l_linger.checked_add(1).ok_or(io::Errno::INVAL)?; - } - l_linger.try_into().map_err(|_e| io::Errno::INVAL)? + duration_to_secs(linger)? } else { 0 }; @@ -810,6 +831,31 @@ pub(crate) mod sockopt { } #[inline] + #[cfg(not(any( + apple, + windows, + target_os = "aix", + target_os = "dragonfly", + target_os = "emscripten", + target_os = "espidf", + target_os = "haiku", + target_os = "netbsd", + target_os = "nto", + )))] + pub(crate) fn get_socket_domain(fd: BorrowedFd<'_>) -> io::Result<AddressFamily> { + let domain: c::c_int = getsockopt(fd, c::SOL_SOCKET as _, c::SO_DOMAIN)?; + Ok(AddressFamily( + domain.try_into().map_err(|_| io::Errno::OPNOTSUPP)?, + )) + } + + #[inline] + #[cfg(not(apple))] // Apple platforms declare the constant, but do not actually implement it. + pub(crate) fn get_socket_acceptconn(fd: BorrowedFd<'_>) -> io::Result<bool> { + getsockopt(fd, c::SOL_SOCKET as _, c::SO_ACCEPTCONN).map(to_bool) + } + + #[inline] pub(crate) fn set_ip_ttl(fd: BorrowedFd<'_>, ttl: u32) -> io::Result<()> { setsockopt(fd, c::IPPROTO_IP as _, c::IP_TTL, ttl) } @@ -992,6 +1038,46 @@ pub(crate) mod sockopt { } #[inline] + #[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "nto")))] + pub(crate) fn set_tcp_keepcnt(fd: BorrowedFd<'_>, count: u32) -> io::Result<()> { + setsockopt(fd, c::IPPROTO_TCP as _, c::TCP_KEEPCNT, count) + } + + #[inline] + #[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "nto")))] + pub(crate) fn get_tcp_keepcnt(fd: BorrowedFd<'_>) -> io::Result<u32> { + getsockopt(fd, c::IPPROTO_TCP as _, c::TCP_KEEPCNT) + } + + #[inline] + #[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "nto")))] + pub(crate) fn set_tcp_keepidle(fd: BorrowedFd<'_>, duration: Duration) -> io::Result<()> { + let secs: c::c_uint = duration_to_secs(duration)?; + setsockopt(fd, c::IPPROTO_TCP as _, TCP_KEEPIDLE, secs) + } + + #[inline] + #[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "nto")))] + pub(crate) fn get_tcp_keepidle(fd: BorrowedFd<'_>) -> io::Result<Duration> { + let secs: c::c_uint = getsockopt(fd, c::IPPROTO_TCP as _, TCP_KEEPIDLE)?; + Ok(Duration::from_secs(secs as u64)) + } + + #[inline] + #[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "nto")))] + pub(crate) fn set_tcp_keepintvl(fd: BorrowedFd<'_>, duration: Duration) -> io::Result<()> { + let secs: c::c_uint = duration_to_secs(duration)?; + setsockopt(fd, c::IPPROTO_TCP as _, c::TCP_KEEPINTVL, secs) + } + + #[inline] + #[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "nto")))] + pub(crate) fn get_tcp_keepintvl(fd: BorrowedFd<'_>) -> io::Result<Duration> { + let secs: c::c_uint = getsockopt(fd, c::IPPROTO_TCP as _, c::TCP_KEEPINTVL)?; + Ok(Duration::from_secs(secs as u64)) + } + + #[inline] fn to_imr(multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> c::ip_mreq { c::ip_mreq { imr_multiaddr: to_imr_addr(multiaddr), @@ -1051,4 +1137,14 @@ pub(crate) mod sockopt { fn to_bool(value: SocketBool) -> bool { value.0 != 0 } + + /// Convert to seconds, rounding up if necessary. + #[inline] + fn duration_to_secs<T: TryFrom<u64>>(duration: Duration) -> io::Result<T> { + let mut secs = duration.as_secs(); + if duration.subsec_nanos() != 0 { + secs = secs.checked_add(1).ok_or(io::Errno::INVAL)?; + } + T::try_from(secs).map_err(|_e| io::Errno::INVAL) + } } |