diff options
Diffstat (limited to 'vendor/rustix/src/backend/libc/net')
-rw-r--r-- | vendor/rustix/src/backend/libc/net/send_recv.rs | 10 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/syscalls.rs | 118 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/types.rs | 26 |
3 files changed, 99 insertions, 55 deletions
diff --git a/vendor/rustix/src/backend/libc/net/send_recv.rs b/vendor/rustix/src/backend/libc/net/send_recv.rs index 114807808..49c6f2c22 100644 --- a/vendor/rustix/src/backend/libc/net/send_recv.rs +++ b/vendor/rustix/src/backend/libc/net/send_recv.rs @@ -2,7 +2,10 @@ use super::super::c; use bitflags::bitflags; bitflags! { - /// `MSG_*` + /// `MSG_* flags for use with [`send`], [`send_to`], and related functions. + /// + /// [`send`]: crate::net::send + /// [`sendto`]: crate::net::sendto pub struct SendFlags: i32 { /// `MSG_CONFIRM` #[cfg(not(any( @@ -37,7 +40,10 @@ bitflags! { } bitflags! { - /// `MSG_*` + /// `MSG_* flags for use with [`recv`], [`recvfrom`], and related functions. + /// + /// [`recv`]: crate::net::recv + /// [`recvfrom`]: crate::net::recvfrom pub struct RecvFlags: i32 { #[cfg(not(any(apple, solarish, windows, target_os = "haiku")))] /// `MSG_CMSG_CLOEXEC` diff --git a/vendor/rustix/src/backend/libc/net/syscalls.rs b/vendor/rustix/src/backend/libc/net/syscalls.rs index ed4494394..ac260e552 100644 --- a/vendor/rustix/src/backend/libc/net/syscalls.rs +++ b/vendor/rustix/src/backend/libc/net/syscalls.rs @@ -12,7 +12,7 @@ use super::read_sockaddr::{maybe_read_sockaddr_os, read_sockaddr_os}; #[cfg(not(any(target_os = "redox", target_os = "wasi")))] use super::send_recv::{RecvFlags, SendFlags}; #[cfg(not(any(target_os = "redox", target_os = "wasi")))] -use super::types::{AcceptFlags, AddressFamily, Protocol, Shutdown, SocketFlags, SocketType}; +use super::types::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType}; #[cfg(not(any(target_os = "redox", target_os = "wasi")))] use super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6}; use crate::fd::{BorrowedFd, OwnedFd}; @@ -26,28 +26,26 @@ use core::ptr::null_mut; #[cfg(not(any(target_os = "redox", target_os = "wasi")))] pub(crate) fn recv(fd: BorrowedFd<'_>, buf: &mut [u8], flags: RecvFlags) -> io::Result<usize> { - let nrecv = unsafe { + unsafe { ret_send_recv(c::recv( borrowed_fd(fd), buf.as_mut_ptr().cast(), send_recv_len(buf.len()), flags.bits(), - ))? - }; - Ok(nrecv as usize) + )) + } } #[cfg(not(any(target_os = "redox", target_os = "wasi")))] pub(crate) fn send(fd: BorrowedFd<'_>, buf: &[u8], flags: SendFlags) -> io::Result<usize> { - let nwritten = unsafe { + unsafe { ret_send_recv(c::send( borrowed_fd(fd), buf.as_ptr().cast(), send_recv_len(buf.len()), flags.bits(), - ))? - }; - Ok(nwritten as usize) + )) + } } #[cfg(not(any(target_os = "redox", target_os = "wasi")))] @@ -65,18 +63,20 @@ pub(crate) fn recvfrom( // `AF_UNSPEC` so that we can detect this case. initialize_family_to_unspec(storage.as_mut_ptr()); - let nread = ret_send_recv(c::recvfrom( + ret_send_recv(c::recvfrom( borrowed_fd(fd), buf.as_mut_ptr().cast(), send_recv_len(buf.len()), flags.bits(), storage.as_mut_ptr().cast(), &mut len, - ))?; - Ok(( - nread as usize, - maybe_read_sockaddr_os(storage.as_ptr(), len.try_into().unwrap()), )) + .map(|nread| { + ( + nread, + maybe_read_sockaddr_os(storage.as_ptr(), len.try_into().unwrap()), + ) + }) } } @@ -87,7 +87,7 @@ pub(crate) fn sendto_v4( flags: SendFlags, addr: &SocketAddrV4, ) -> io::Result<usize> { - let nwritten = unsafe { + unsafe { ret_send_recv(c::sendto( borrowed_fd(fd), buf.as_ptr().cast(), @@ -95,9 +95,8 @@ pub(crate) fn sendto_v4( flags.bits(), as_ptr(&encode_sockaddr_v4(addr)).cast::<c::sockaddr>(), size_of::<SocketAddrV4>() as _, - ))? - }; - Ok(nwritten as usize) + )) + } } #[cfg(not(any(target_os = "redox", target_os = "wasi")))] @@ -107,7 +106,7 @@ pub(crate) fn sendto_v6( flags: SendFlags, addr: &SocketAddrV6, ) -> io::Result<usize> { - let nwritten = unsafe { + unsafe { ret_send_recv(c::sendto( borrowed_fd(fd), buf.as_ptr().cast(), @@ -115,9 +114,8 @@ pub(crate) fn sendto_v6( flags.bits(), as_ptr(&encode_sockaddr_v6(addr)).cast::<c::sockaddr>(), size_of::<SocketAddrV6>() as _, - ))? - }; - Ok(nwritten as usize) + )) + } } #[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))] @@ -127,7 +125,7 @@ pub(crate) fn sendto_unix( flags: SendFlags, addr: &SocketAddrUnix, ) -> io::Result<usize> { - let nwritten = unsafe { + unsafe { ret_send_recv(c::sendto( borrowed_fd(fd), buf.as_ptr().cast(), @@ -135,9 +133,8 @@ pub(crate) fn sendto_unix( flags.bits(), as_ptr(&addr.unix).cast(), addr.addr_len(), - ))? - }; - Ok(nwritten as usize) + )) + } } #[cfg(not(any(target_os = "redox", target_os = "wasi")))] @@ -257,7 +254,7 @@ pub(crate) fn accept(sockfd: BorrowedFd<'_>) -> io::Result<OwnedFd> { target_os = "redox", target_os = "wasi", )))] -pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, flags: AcceptFlags) -> io::Result<OwnedFd> { +pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, flags: SocketFlags) -> io::Result<OwnedFd> { unsafe { let owned_fd = ret_owned_fd(c::accept4( borrowed_fd(sockfd), @@ -295,7 +292,7 @@ pub(crate) fn acceptfrom(sockfd: BorrowedFd<'_>) -> io::Result<(OwnedFd, Option< )))] pub(crate) fn acceptfrom_with( sockfd: BorrowedFd<'_>, - flags: AcceptFlags, + flags: SocketFlags, ) -> io::Result<(OwnedFd, Option<SocketAddrAny>)> { unsafe { let mut storage = MaybeUninit::<c::sockaddr_storage>::uninit(); @@ -314,18 +311,18 @@ pub(crate) fn acceptfrom_with( } /// Darwin lacks `accept4`, but does have `accept`. We define -/// `AcceptFlags` to have no flags, so we can discard it here. +/// `SocketFlags` to have no flags, so we can discard it here. #[cfg(any(apple, windows, target_os = "haiku"))] -pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, _flags: AcceptFlags) -> io::Result<OwnedFd> { +pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, _flags: SocketFlags) -> io::Result<OwnedFd> { accept(sockfd) } /// Darwin lacks `accept4`, but does have `accept`. We define -/// `AcceptFlags` to have no flags, so we can discard it here. +/// `SocketFlags` to have no flags, so we can discard it here. #[cfg(any(apple, windows, target_os = "haiku"))] pub(crate) fn acceptfrom_with( sockfd: BorrowedFd<'_>, - _flags: AcceptFlags, + _flags: SocketFlags, ) -> io::Result<(OwnedFd, Option<SocketAddrAny>)> { acceptfrom(sockfd) } @@ -555,7 +552,7 @@ pub(crate) mod sockopt { // Rust's musl libc bindings deprecated `time_t` while they // transition to 64-bit `time_t`. What we want here is just - // "whatever type `timeval`'s `tv_sec` is", so we're ok using + // “whatever type `timeval`'s `tv_sec` is”, so we're ok using // the deprecated type. #[allow(deprecated)] let tv_sec = timeout.as_secs().try_into().unwrap_or(c::time_t::MAX); @@ -647,16 +644,53 @@ pub(crate) mod sockopt { } #[inline] - pub(crate) fn get_socket_error(fd: BorrowedFd<'_>) -> io::Result<Result<(), crate::io::Errno>> { + pub(crate) fn get_socket_error(fd: BorrowedFd<'_>) -> io::Result<Result<(), io::Errno>> { let err: c::c_int = getsockopt(fd, c::SOL_SOCKET as _, c::SO_ERROR)?; Ok(if err == 0 { Ok(()) } else { - Err(crate::io::Errno::from_raw_os_error(err)) + Err(io::Errno::from_raw_os_error(err)) }) } #[inline] + pub(crate) fn set_socket_keepalive(fd: BorrowedFd<'_>, keepalive: bool) -> io::Result<()> { + setsockopt( + fd, + c::SOL_SOCKET as _, + c::SO_KEEPALIVE, + from_bool(keepalive), + ) + } + + #[inline] + pub(crate) fn get_socket_keepalive(fd: BorrowedFd<'_>) -> io::Result<bool> { + getsockopt(fd, c::SOL_SOCKET as _, c::SO_KEEPALIVE).map(to_bool) + } + + #[inline] + pub(crate) fn set_socket_recv_buffer_size(fd: BorrowedFd<'_>, size: usize) -> io::Result<()> { + let size: c::c_int = size.try_into().map_err(|_| io::Errno::INVAL)?; + setsockopt(fd, c::SOL_SOCKET as _, c::SO_RCVBUF, size) + } + + #[inline] + pub(crate) fn get_socket_recv_buffer_size(fd: BorrowedFd<'_>) -> io::Result<usize> { + getsockopt(fd, c::SOL_SOCKET as _, c::SO_RCVBUF).map(|size: u32| size as usize) + } + + #[inline] + pub(crate) fn set_socket_send_buffer_size(fd: BorrowedFd<'_>, size: usize) -> io::Result<()> { + let size: c::c_int = size.try_into().map_err(|_| io::Errno::INVAL)?; + setsockopt(fd, c::SOL_SOCKET as _, c::SO_SNDBUF, size) + } + + #[inline] + pub(crate) fn get_socket_send_buffer_size(fd: BorrowedFd<'_>) -> io::Result<usize> { + getsockopt(fd, c::SOL_SOCKET as _, c::SO_SNDBUF).map(|size: u32| size as usize) + } + + #[inline] pub(crate) fn set_ip_ttl(fd: BorrowedFd<'_>, ttl: u32) -> io::Result<()> { setsockopt(fd, c::IPPROTO_IP as _, c::IP_TTL, ttl) } @@ -791,6 +825,20 @@ pub(crate) mod sockopt { } #[inline] + pub(crate) fn get_ipv6_unicast_hops(fd: BorrowedFd<'_>) -> io::Result<u8> { + getsockopt(fd, c::IPPROTO_IPV6 as _, c::IPV6_UNICAST_HOPS).map(|hops: c::c_int| hops as u8) + } + + #[inline] + pub(crate) fn set_ipv6_unicast_hops(fd: BorrowedFd<'_>, hops: Option<u8>) -> io::Result<()> { + let hops = match hops { + Some(hops) => hops as c::c_int, + None => -1, + }; + setsockopt(fd, c::IPPROTO_IPV6 as _, c::IPV6_UNICAST_HOPS, hops) + } + + #[inline] pub(crate) fn set_tcp_nodelay(fd: BorrowedFd<'_>, nodelay: bool) -> io::Result<()> { setsockopt(fd, c::IPPROTO_TCP as _, c::TCP_NODELAY, from_bool(nodelay)) } diff --git a/vendor/rustix/src/backend/libc/net/types.rs b/vendor/rustix/src/backend/libc/net/types.rs index 54f60ca50..d1d769cb4 100644 --- a/vendor/rustix/src/backend/libc/net/types.rs +++ b/vendor/rustix/src/backend/libc/net/types.rs @@ -53,6 +53,7 @@ pub type RawAddressFamily = c::sa_family_t; pub struct AddressFamily(pub(crate) RawAddressFamily); #[rustfmt::skip] +#[allow(non_upper_case_globals)] impl AddressFamily { /// `AF_UNSPEC` pub const UNSPEC: Self = Self(c::AF_UNSPEC as _); @@ -123,7 +124,6 @@ impl AddressFamily { )))] pub const ROSE: Self = Self(c::AF_ROSE as _); /// `AF_DECnet` - #[allow(non_upper_case_globals)] #[cfg(not(target_os = "haiku"))] pub const DECnet: Self = Self(c::AF_DECnet as _); /// `AF_NETBEUI` @@ -294,7 +294,10 @@ impl AddressFamily { #[doc(hidden)] pub type RawProtocol = i32; -/// `IPPROTO_*` +/// `IPPROTO_*` constants for use with [`socket`] and [`socket_with`]. +/// +/// [`socket`]: crate::net::socket +/// [`socket_with`]: crate::net::socket_with #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] #[repr(transparent)] pub struct Protocol(pub(crate) RawProtocol); @@ -464,25 +467,12 @@ pub enum Shutdown { } bitflags! { - /// `SOCK_*` constants for use with [`accept_with`] and [`acceptfrom_with`]. + /// `SOCK_*` constants for use with [`socket_with`], [`accept_with`] and + /// [`acceptfrom_with`]. /// + /// [`socket_with`]: crate::net::socket_with /// [`accept_with`]: crate::net::accept_with /// [`acceptfrom_with`]: crate::net::acceptfrom_with - pub struct AcceptFlags: c::c_int { - /// `SOCK_NONBLOCK` - #[cfg(not(any(apple, windows, target_os = "haiku")))] - const NONBLOCK = c::SOCK_NONBLOCK; - - /// `SOCK_CLOEXEC` - #[cfg(not(any(apple, windows, target_os = "haiku")))] - const CLOEXEC = c::SOCK_CLOEXEC; - } -} - -bitflags! { - /// `SOCK_*` constants for use with [`socket`]. - /// - /// [`socket`]: crate::net::socket pub struct SocketFlags: c::c_int { /// `SOCK_NONBLOCK` #[cfg(not(any(apple, windows, target_os = "haiku")))] |