diff options
Diffstat (limited to 'vendor/rustix/src/backend/libc/net')
-rw-r--r-- | vendor/rustix/src/backend/libc/net/addr.rs | 49 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/ext.rs | 83 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/mod.rs | 3 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/msghdr.rs | 125 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/read_sockaddr.rs | 81 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/send_recv.rs | 38 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/syscalls.rs | 199 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/types.rs | 500 | ||||
-rw-r--r-- | vendor/rustix/src/backend/libc/net/write_sockaddr.rs | 2 |
9 files changed, 388 insertions, 692 deletions
diff --git a/vendor/rustix/src/backend/libc/net/addr.rs b/vendor/rustix/src/backend/libc/net/addr.rs index d00a48626..bf7d239de 100644 --- a/vendor/rustix/src/backend/libc/net/addr.rs +++ b/vendor/rustix/src/backend/libc/net/addr.rs @@ -1,18 +1,16 @@ -//! IPv4, IPv6, and Socket addresses. +//! Socket address utilities. -use super::super::c; +use crate::backend::c; #[cfg(unix)] -use crate::ffi::CStr; -#[cfg(unix)] -use crate::io; -#[cfg(unix)] -use crate::path; -#[cfg(not(windows))] -use core::convert::TryInto; -#[cfg(unix)] -use core::fmt; -#[cfg(unix)] -use core::slice; +use { + crate::ffi::CStr, + crate::io, + crate::path, + core::cmp::Ordering, + core::fmt, + core::hash::{Hash, Hasher}, + core::slice, +}; /// `struct sockaddr_un` #[cfg(unix)] @@ -56,14 +54,14 @@ impl SocketAddrUnix { } /// Construct a new abstract Unix-domain address from a byte slice. - #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg(linux_kernel)] #[inline] pub fn new_abstract_name(name: &[u8]) -> io::Result<Self> { let mut unix = Self::init(); if 1 + name.len() > unix.sun_path.len() { return Err(io::Errno::NAMETOOLONG); } - unix.sun_path[0] = b'\0' as c::c_char; + unix.sun_path[0] = 0; for (i, b) in name.iter().enumerate() { unix.sun_path[1 + i] = *b as c::c_char; } @@ -94,11 +92,12 @@ impl SocketAddrUnix { #[inline] pub fn path(&self) -> Option<&CStr> { let len = self.len(); - if len != 0 && self.unix.sun_path[0] != b'\0' as c::c_char { + if len != 0 && self.unix.sun_path[0] != 0 { let end = len as usize - offsetof_sun_path(); let bytes = &self.unix.sun_path[..end]; - // SAFETY: `from_raw_parts` to convert from `&[c_char]` to `&[u8]`. And - // `from_bytes_with_nul_unchecked` since the string is NUL-terminated. + // SAFETY: `from_raw_parts` to convert from `&[c_char]` to `&[u8]`. + // And `from_bytes_with_nul_unchecked` since the string is + // NUL-terminated. unsafe { Some(CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts( bytes.as_ptr().cast(), @@ -111,11 +110,11 @@ impl SocketAddrUnix { } /// For an abstract address, return the identifier. - #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg(linux_kernel)] #[inline] pub fn abstract_name(&self) -> Option<&[u8]> { let len = self.len(); - if len != 0 && self.unix.sun_path[0] == b'\0' as c::c_char { + if len != 0 && self.unix.sun_path[0] == 0 { let end = len as usize - offsetof_sun_path(); let bytes = &self.unix.sun_path[1..end]; // SAFETY: `from_raw_parts` to convert from `&[c_char]` to `&[u8]`. @@ -159,7 +158,7 @@ impl Eq for SocketAddrUnix {} #[cfg(unix)] impl PartialOrd for SocketAddrUnix { #[inline] - fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { let self_len = self.len() - offsetof_sun_path(); let other_len = other.len() - offsetof_sun_path(); self.unix.sun_path[..self_len].partial_cmp(&other.unix.sun_path[..other_len]) @@ -169,7 +168,7 @@ impl PartialOrd for SocketAddrUnix { #[cfg(unix)] impl Ord for SocketAddrUnix { #[inline] - fn cmp(&self, other: &Self) -> core::cmp::Ordering { + fn cmp(&self, other: &Self) -> Ordering { let self_len = self.len() - offsetof_sun_path(); let other_len = other.len() - offsetof_sun_path(); self.unix.sun_path[..self_len].cmp(&other.unix.sun_path[..other_len]) @@ -177,9 +176,9 @@ impl Ord for SocketAddrUnix { } #[cfg(unix)] -impl core::hash::Hash for SocketAddrUnix { +impl Hash for SocketAddrUnix { #[inline] - fn hash<H: core::hash::Hasher>(&self, state: &mut H) { + fn hash<H: Hasher>(&self, state: &mut H) { let self_len = self.len() - offsetof_sun_path(); self.unix.sun_path[..self_len].hash(state) } @@ -191,7 +190,7 @@ impl fmt::Debug for SocketAddrUnix { if let Some(path) = self.path() { path.fmt(fmt) } else { - #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg(linux_kernel)] if let Some(name) = self.abstract_name() { return name.fmt(fmt); } diff --git a/vendor/rustix/src/backend/libc/net/ext.rs b/vendor/rustix/src/backend/libc/net/ext.rs index f4ad316dd..4b2d10756 100644 --- a/vendor/rustix/src/backend/libc/net/ext.rs +++ b/vendor/rustix/src/backend/libc/net/ext.rs @@ -1,4 +1,4 @@ -use super::super::c; +use crate::backend::c; /// The windows `sockaddr_in6` type is a union with accessor functions which /// are not `const fn`. Define our own layout-compatible version so that we @@ -19,7 +19,6 @@ pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 { addr.s_addr } -#[cfg(not(feature = "std"))] #[cfg(windows)] #[inline] pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 { @@ -27,103 +26,52 @@ pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 { unsafe { core::mem::transmute(addr) } } -// TODO: With Rust 1.55, we can use the above `in_addr_s_addr` definition that -// uses a const-fn transmute. -#[cfg(feature = "std")] -#[cfg(windows)] -#[inline] -pub(crate) fn in_addr_s_addr(addr: c::in_addr) -> u32 { - // This should be `*addr.S_un.S_addr()`, except that isn't a `const fn`. - unsafe { core::mem::transmute(addr) } -} - #[cfg(not(windows))] #[inline] pub(crate) const fn in_addr_new(s_addr: u32) -> c::in_addr { c::in_addr { s_addr } } -#[cfg(not(feature = "std"))] #[cfg(windows)] #[inline] pub(crate) const fn in_addr_new(s_addr: u32) -> c::in_addr { unsafe { core::mem::transmute(s_addr) } } -// TODO: With Rust 1.55, we can use the above `in_addr_new` definition that -// uses a const-fn transmute. -#[cfg(feature = "std")] -#[cfg(windows)] -#[inline] -pub(crate) fn in_addr_new(s_addr: u32) -> c::in_addr { - unsafe { core::mem::transmute(s_addr) } -} - #[cfg(not(windows))] #[inline] pub(crate) const fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] { addr.s6_addr } -#[cfg(not(feature = "std"))] #[cfg(windows)] #[inline] pub(crate) const fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] { unsafe { core::mem::transmute(addr) } } -// TODO: With Rust 1.55, we can use the above `in6_addr_s6_addr` definition -// that uses a const-fn transmute. -#[cfg(feature = "std")] -#[cfg(windows)] -#[inline] -pub(crate) fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] { - unsafe { core::mem::transmute(addr) } -} - #[cfg(not(windows))] #[inline] pub(crate) const fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr { c::in6_addr { s6_addr } } -#[cfg(not(feature = "std"))] #[cfg(windows)] #[inline] pub(crate) const fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr { unsafe { core::mem::transmute(s6_addr) } } -// TODO: With Rust 1.55, we can use the above `in6_addr_new` definition that -// uses a const-fn transmute. -#[cfg(feature = "std")] -#[cfg(windows)] -#[inline] -pub(crate) fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr { - unsafe { core::mem::transmute(s6_addr) } -} - #[cfg(not(windows))] #[inline] -pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 { - addr.sin6_scope_id -} - -#[cfg(not(feature = "std"))] -#[cfg(windows)] -#[inline] -pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 { - let addr: sockaddr_in6 = unsafe { core::mem::transmute(addr) }; +pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: &c::sockaddr_in6) -> u32 { addr.sin6_scope_id } -// TODO: With Rust 1.55, we can use the above `sockaddr_in6_sin6_scope_id` -// definition that uses a const-fn transmute. -#[cfg(feature = "std")] #[cfg(windows)] #[inline] -pub(crate) fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 { - let addr: sockaddr_in6 = unsafe { core::mem::transmute(addr) }; +pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: &c::sockaddr_in6) -> u32 { + let addr: &sockaddr_in6 = unsafe { core::mem::transmute(addr) }; addr.sin6_scope_id } @@ -150,7 +98,6 @@ pub(crate) const fn sockaddr_in6_new( } } -#[cfg(not(feature = "std"))] #[cfg(windows)] #[inline] pub(crate) const fn sockaddr_in6_new( @@ -169,25 +116,3 @@ pub(crate) const fn sockaddr_in6_new( }; unsafe { core::mem::transmute(addr) } } - -// TODO: With Rust 1.55, we can use the above `sockaddr_in6_new` definition -// that uses a const-fn transmute. -#[cfg(feature = "std")] -#[cfg(windows)] -#[inline] -pub(crate) fn sockaddr_in6_new( - sin6_family: u16, - sin6_port: u16, - sin6_flowinfo: u32, - sin6_addr: c::in6_addr, - sin6_scope_id: u32, -) -> c::sockaddr_in6 { - let addr = sockaddr_in6 { - sin6_family, - sin6_port, - sin6_flowinfo, - sin6_addr, - sin6_scope_id, - }; - unsafe { core::mem::transmute(addr) } -} diff --git a/vendor/rustix/src/backend/libc/net/mod.rs b/vendor/rustix/src/backend/libc/net/mod.rs index f7196ae4f..1b68f1b26 100644 --- a/vendor/rustix/src/backend/libc/net/mod.rs +++ b/vendor/rustix/src/backend/libc/net/mod.rs @@ -1,7 +1,8 @@ pub(crate) mod addr; pub(crate) mod ext; +#[cfg(not(any(target_os = "redox", target_os = "wasi", windows)))] +pub(crate) mod msghdr; pub(crate) mod read_sockaddr; pub(crate) mod send_recv; pub(crate) mod syscalls; -pub(crate) mod types; pub(crate) mod write_sockaddr; diff --git a/vendor/rustix/src/backend/libc/net/msghdr.rs b/vendor/rustix/src/backend/libc/net/msghdr.rs new file mode 100644 index 000000000..e3f873747 --- /dev/null +++ b/vendor/rustix/src/backend/libc/net/msghdr.rs @@ -0,0 +1,125 @@ +//! Utilities for dealing with message headers. +//! +//! These take closures rather than returning a `c::msghdr` directly because +//! the message headers may reference stack-local data. + +use crate::backend::c; +use crate::backend::conv::{msg_control_len, msg_iov_len}; +use crate::backend::net::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6}; + +use crate::io::{self, IoSlice, IoSliceMut}; +use crate::net::{RecvAncillaryBuffer, SendAncillaryBuffer, SocketAddrV4, SocketAddrV6}; +use crate::utils::as_ptr; + +use core::mem::{size_of, zeroed, MaybeUninit}; + +/// Create a message header intended to receive a datagram. +pub(crate) fn with_recv_msghdr<R>( + name: &mut MaybeUninit<c::sockaddr_storage>, + iov: &mut [IoSliceMut<'_>], + control: &mut RecvAncillaryBuffer<'_>, + f: impl FnOnce(&mut c::msghdr) -> io::Result<R>, +) -> io::Result<R> { + control.clear(); + + let namelen = size_of::<c::sockaddr_storage>() as c::socklen_t; + let mut msghdr = { + let mut h: c::msghdr = unsafe { zeroed() }; + h.msg_name = name.as_mut_ptr().cast(); + h.msg_namelen = namelen; + h.msg_iov = iov.as_mut_ptr().cast(); + h.msg_iovlen = msg_iov_len(iov.len()); + h.msg_control = control.as_control_ptr().cast(); + h.msg_controllen = msg_control_len(control.control_len()); + h + }; + + let res = f(&mut msghdr); + + // Reset the control length. + if res.is_ok() { + unsafe { + control.set_control_len(msghdr.msg_controllen.try_into().unwrap_or(usize::MAX)); + } + } + + res +} + +/// Create a message header intended to send without an address. +pub(crate) fn with_noaddr_msghdr<R>( + iov: &[IoSlice<'_>], + control: &mut SendAncillaryBuffer<'_, '_, '_>, + f: impl FnOnce(c::msghdr) -> R, +) -> R { + f({ + let mut h: c::msghdr = unsafe { zeroed() }; + h.msg_iov = iov.as_ptr() as _; + h.msg_iovlen = msg_iov_len(iov.len()); + h.msg_control = control.as_control_ptr().cast(); + h.msg_controllen = msg_control_len(control.control_len()); + h + }) +} + +/// Create a message header intended to send with an IPv4 address. +pub(crate) fn with_v4_msghdr<R>( + addr: &SocketAddrV4, + iov: &[IoSlice<'_>], + control: &mut SendAncillaryBuffer<'_, '_, '_>, + f: impl FnOnce(c::msghdr) -> R, +) -> R { + let encoded = unsafe { encode_sockaddr_v4(addr) }; + + f({ + let mut h: c::msghdr = unsafe { zeroed() }; + h.msg_name = as_ptr(&encoded) as _; + h.msg_namelen = size_of::<SocketAddrV4>() as _; + h.msg_iov = iov.as_ptr() as _; + h.msg_iovlen = msg_iov_len(iov.len()); + h.msg_control = control.as_control_ptr().cast(); + h.msg_controllen = msg_control_len(control.control_len()); + h + }) +} + +/// Create a message header intended to send with an IPv6 address. +pub(crate) fn with_v6_msghdr<R>( + addr: &SocketAddrV6, + iov: &[IoSlice<'_>], + control: &mut SendAncillaryBuffer<'_, '_, '_>, + f: impl FnOnce(c::msghdr) -> R, +) -> R { + let encoded = unsafe { encode_sockaddr_v6(addr) }; + + f({ + let mut h: c::msghdr = unsafe { zeroed() }; + h.msg_name = as_ptr(&encoded) as _; + h.msg_namelen = size_of::<SocketAddrV6>() as _; + h.msg_iov = iov.as_ptr() as _; + h.msg_iovlen = msg_iov_len(iov.len()); + h.msg_control = control.as_control_ptr().cast(); + h.msg_controllen = msg_control_len(control.control_len()); + h + }) +} + +/// Create a message header intended to send with a Unix address. +#[cfg(all(unix, not(target_os = "redox")))] +pub(crate) fn with_unix_msghdr<R>( + addr: &crate::net::SocketAddrUnix, + iov: &[IoSlice<'_>], + control: &mut SendAncillaryBuffer<'_, '_, '_>, + f: impl FnOnce(c::msghdr) -> R, +) -> R { + f({ + let mut h: c::msghdr = unsafe { zeroed() }; + h.msg_name = as_ptr(addr) as _; + h.msg_namelen = addr.addr_len(); + h.msg_iov = iov.as_ptr() as _; + h.msg_iovlen = msg_iov_len(iov.len()); + h.msg_control = control.as_control_ptr().cast(); + h.msg_controllen = msg_control_len(control.control_len()); + h + }) +} diff --git a/vendor/rustix/src/backend/libc/net/read_sockaddr.rs b/vendor/rustix/src/backend/libc/net/read_sockaddr.rs index 575102c27..c3b23e8c2 100644 --- a/vendor/rustix/src/backend/libc/net/read_sockaddr.rs +++ b/vendor/rustix/src/backend/libc/net/read_sockaddr.rs @@ -1,13 +1,14 @@ -use super::super::c; +//! The BSD sockets API requires us to read the `ss_family` field before +//! we can interpret the rest of a `sockaddr` produced by the kernel. + #[cfg(unix)] use super::addr::SocketAddrUnix; use super::ext::{in6_addr_s6_addr, in_addr_s_addr, sockaddr_in6_sin6_scope_id}; +use crate::backend::c; #[cfg(not(windows))] use crate::ffi::CStr; use crate::io; use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddrAny, SocketAddrV4, SocketAddrV6}; -#[cfg(not(windows))] -use alloc::vec::Vec; use core::mem::size_of; // This must match the header of `sockaddr`. @@ -46,6 +47,11 @@ pub(crate) unsafe fn initialize_family_to_unspec(storage: *mut c::sockaddr_stora (*storage.cast::<sockaddr_header>()).ss_family = c::AF_UNSPEC as _; } +/// Read a socket address encoded in a platform-specific format. +/// +/// # Safety +/// +/// `storage` must point to valid socket address storage. pub(crate) unsafe fn read_sockaddr( storage: *const c::sockaddr_storage, len: usize, @@ -61,7 +67,7 @@ pub(crate) unsafe fn read_sockaddr( if len < size_of::<c::sockaddr_in>() { return Err(io::Errno::INVAL); } - let decode = *storage.cast::<c::sockaddr_in>(); + let decode = &*storage.cast::<c::sockaddr_in>(); Ok(SocketAddrAny::V4(SocketAddrV4::new( Ipv4Addr::from(u32::from_be(in_addr_s_addr(decode.sin_addr))), u16::from_be(decode.sin_port), @@ -71,7 +77,7 @@ pub(crate) unsafe fn read_sockaddr( if len < size_of::<c::sockaddr_in6>() { return Err(io::Errno::INVAL); } - let decode = *storage.cast::<c::sockaddr_in6>(); + let decode = &*storage.cast::<c::sockaddr_in6>(); #[cfg(not(windows))] let s6_addr = decode.sin6_addr.s6_addr; #[cfg(windows)] @@ -93,20 +99,40 @@ pub(crate) unsafe fn read_sockaddr( return Err(io::Errno::INVAL); } if len == offsetof_sun_path { - Ok(SocketAddrAny::Unix(SocketAddrUnix::new(&[][..]).unwrap())) + SocketAddrUnix::new(&[][..]).map(SocketAddrAny::Unix) } else { - let decode = *storage.cast::<c::sockaddr_un>(); + let decode = &*storage.cast::<c::sockaddr_un>(); + + // On Linux check for Linux's [abstract namespace]. + // + // [abstract namespace]: https://man7.org/linux/man-pages/man7/unix.7.html + #[cfg(linux_kernel)] + if decode.sun_path[0] == 0 { + return SocketAddrUnix::new_abstract_name(core::mem::transmute::< + &[c::c_char], + &[u8], + >( + &decode.sun_path[1..len - offsetof_sun_path], + )) + .map(SocketAddrAny::Unix); + } + + // Otherwise we expect a NUL-terminated filesystem path. // Trim off unused bytes from the end of `path_bytes`. let path_bytes = if cfg!(target_os = "freebsd") { // FreeBSD sometimes sets the length to longer than the length // of the NUL-terminated string. Find the NUL and truncate the // string accordingly. - &decode.sun_path[..decode.sun_path.iter().position(|b| *b == 0).unwrap()] + &decode.sun_path[..decode + .sun_path + .iter() + .position(|b| *b == 0) + .ok_or(io::Errno::INVAL)?] } else { // Otherwise, use the provided length. let provided_len = len - 1 - offsetof_sun_path; - if decode.sun_path[provided_len] != b'\0' as c::c_char { + if decode.sun_path[provided_len] != 0 { return Err(io::Errno::INVAL); } debug_assert_eq!( @@ -116,10 +142,8 @@ pub(crate) unsafe fn read_sockaddr( &decode.sun_path[..provided_len] }; - Ok(SocketAddrAny::Unix( - SocketAddrUnix::new(path_bytes.iter().map(|c| *c as u8).collect::<Vec<u8>>()) - .unwrap(), - )) + SocketAddrUnix::new(core::mem::transmute::<&[c::c_char], &[u8]>(path_bytes)) + .map(SocketAddrAny::Unix) } } _ => Err(io::Errno::INVAL), @@ -164,7 +188,7 @@ unsafe fn inner_read_sockaddr_os( match family { c::AF_INET => { assert!(len >= size_of::<c::sockaddr_in>()); - let decode = *storage.cast::<c::sockaddr_in>(); + let decode = &*storage.cast::<c::sockaddr_in>(); SocketAddrAny::V4(SocketAddrV4::new( Ipv4Addr::from(u32::from_be(in_addr_s_addr(decode.sin_addr))), u16::from_be(decode.sin_port), @@ -172,7 +196,7 @@ unsafe fn inner_read_sockaddr_os( } c::AF_INET6 => { assert!(len >= size_of::<c::sockaddr_in6>()); - let decode = *storage.cast::<c::sockaddr_in6>(); + let decode = &*storage.cast::<c::sockaddr_in6>(); SocketAddrAny::V6(SocketAddrV6::new( Ipv6Addr::from(in6_addr_s6_addr(decode.sin6_addr)), u16::from_be(decode.sin6_port), @@ -186,11 +210,26 @@ unsafe fn inner_read_sockaddr_os( if len == offsetof_sun_path { SocketAddrAny::Unix(SocketAddrUnix::new(&[][..]).unwrap()) } else { - let decode = *storage.cast::<c::sockaddr_un>(); - assert_eq!( - decode.sun_path[len - 1 - offsetof_sun_path], - b'\0' as c::c_char - ); + let decode = &*storage.cast::<c::sockaddr_un>(); + + // On Linux check for Linux's [abstract namespace]. + // + // [abstract namespace]: https://man7.org/linux/man-pages/man7/unix.7.html + #[cfg(linux_kernel)] + if decode.sun_path[0] == 0 { + return SocketAddrAny::Unix( + SocketAddrUnix::new_abstract_name(core::mem::transmute::< + &[c::c_char], + &[u8], + >( + &decode.sun_path[1..len - offsetof_sun_path], + )) + .unwrap(), + ); + } + + // Otherwise we expect a NUL-terminated filesystem path. + assert_eq!(decode.sun_path[len - 1 - offsetof_sun_path], 0); let path_bytes = &decode.sun_path[..len - 1 - offsetof_sun_path]; // FreeBSD sometimes sets the length to longer than the length @@ -200,7 +239,7 @@ unsafe fn inner_read_sockaddr_os( let path_bytes = &path_bytes[..path_bytes.iter().position(|b| *b == 0).unwrap()]; SocketAddrAny::Unix( - SocketAddrUnix::new(path_bytes.iter().map(|c| *c as u8).collect::<Vec<u8>>()) + SocketAddrUnix::new(core::mem::transmute::<&[c::c_char], &[u8]>(path_bytes)) .unwrap(), ) } diff --git a/vendor/rustix/src/backend/libc/net/send_recv.rs b/vendor/rustix/src/backend/libc/net/send_recv.rs index 49c6f2c22..e91017e97 100644 --- a/vendor/rustix/src/backend/libc/net/send_recv.rs +++ b/vendor/rustix/src/backend/libc/net/send_recv.rs @@ -1,4 +1,4 @@ -use super::super::c; +use crate::backend::c; use bitflags::bitflags; bitflags! { @@ -6,7 +6,9 @@ bitflags! { /// /// [`send`]: crate::net::send /// [`sendto`]: crate::net::sendto - pub struct SendFlags: i32 { + #[repr(transparent)] + #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] + pub struct SendFlags: u32 { /// `MSG_CONFIRM` #[cfg(not(any( bsd, @@ -14,15 +16,15 @@ bitflags! { windows, target_os = "haiku", )))] - const CONFIRM = c::MSG_CONFIRM; + const CONFIRM = bitcast!(c::MSG_CONFIRM); /// `MSG_DONTROUTE` - const DONTROUTE = c::MSG_DONTROUTE; + const DONTROUTE = bitcast!(c::MSG_DONTROUTE); /// `MSG_DONTWAIT` #[cfg(not(windows))] - const DONTWAIT = c::MSG_DONTWAIT; + const DONTWAIT = bitcast!(c::MSG_DONTWAIT); /// `MSG_EOR` #[cfg(not(windows))] - const EOT = c::MSG_EOR; + const EOT = bitcast!(c::MSG_EOR); /// `MSG_MORE` #[cfg(not(any( bsd, @@ -30,12 +32,12 @@ bitflags! { windows, target_os = "haiku", )))] - const MORE = c::MSG_MORE; + const MORE = bitcast!(c::MSG_MORE); #[cfg(not(any(apple, windows)))] /// `MSG_NOSIGNAL` - const NOSIGNAL = c::MSG_NOSIGNAL; + const NOSIGNAL = bitcast!(c::MSG_NOSIGNAL); /// `MSG_OOB` - const OOB = c::MSG_OOB; + const OOB = bitcast!(c::MSG_OOB); } } @@ -44,13 +46,15 @@ bitflags! { /// /// [`recv`]: crate::net::recv /// [`recvfrom`]: crate::net::recvfrom - pub struct RecvFlags: i32 { + #[repr(transparent)] + #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] + pub struct RecvFlags: u32 { #[cfg(not(any(apple, solarish, windows, target_os = "haiku")))] /// `MSG_CMSG_CLOEXEC` - const CMSG_CLOEXEC = c::MSG_CMSG_CLOEXEC; + const CMSG_CLOEXEC = bitcast!(c::MSG_CMSG_CLOEXEC); /// `MSG_DONTWAIT` #[cfg(not(windows))] - const DONTWAIT = c::MSG_DONTWAIT; + const DONTWAIT = bitcast!(c::MSG_DONTWAIT); /// `MSG_ERRQUEUE` #[cfg(not(any( bsd, @@ -58,14 +62,14 @@ bitflags! { windows, target_os = "haiku", )))] - const ERRQUEUE = c::MSG_ERRQUEUE; + const ERRQUEUE = bitcast!(c::MSG_ERRQUEUE); /// `MSG_OOB` - const OOB = c::MSG_OOB; + const OOB = bitcast!(c::MSG_OOB); /// `MSG_PEEK` - const PEEK = c::MSG_PEEK; + const PEEK = bitcast!(c::MSG_PEEK); /// `MSG_TRUNC` - const TRUNC = c::MSG_TRUNC as c::c_int; + const TRUNC = bitcast!(c::MSG_TRUNC); /// `MSG_WAITALL` - const WAITALL = c::MSG_WAITALL; + const WAITALL = bitcast!(c::MSG_WAITALL); } } diff --git a/vendor/rustix/src/backend/libc/net/syscalls.rs b/vendor/rustix/src/backend/libc/net/syscalls.rs index ac260e552..63067ff38 100644 --- a/vendor/rustix/src/backend/libc/net/syscalls.rs +++ b/vendor/rustix/src/backend/libc/net/syscalls.rs @@ -1,28 +1,29 @@ //! libc syscalls supporting `rustix::net`. -use super::super::c; -use super::super::conv::{borrowed_fd, ret, ret_owned_fd, ret_send_recv, send_recv_len}; #[cfg(unix)] use super::addr::SocketAddrUnix; use super::ext::{in6_addr_new, in_addr_new}; -#[cfg(not(any(target_os = "redox", target_os = "wasi")))] -use super::read_sockaddr::initialize_family_to_unspec; -#[cfg(not(any(target_os = "redox", target_os = "wasi")))] -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::{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::backend::c; +use crate::backend::conv::{borrowed_fd, ret, ret_owned_fd, ret_send_recv, send_recv_len}; use crate::fd::{BorrowedFd, OwnedFd}; use crate::io; use crate::net::{SocketAddrAny, SocketAddrV4, SocketAddrV6}; use crate::utils::as_ptr; -use core::convert::TryInto; use core::mem::{size_of, MaybeUninit}; +#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))] +use { + super::msghdr::{with_noaddr_msghdr, with_recv_msghdr, with_v4_msghdr, with_v6_msghdr}, + crate::io::{IoSlice, IoSliceMut}, + crate::net::{RecvAncillaryBuffer, RecvMsgReturn, SendAncillaryBuffer}, +}; #[cfg(not(any(target_os = "redox", target_os = "wasi")))] -use core::ptr::null_mut; +use { + super::read_sockaddr::{initialize_family_to_unspec, maybe_read_sockaddr_os, read_sockaddr_os}, + super::send_recv::{RecvFlags, SendFlags}, + super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6}, + crate::net::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType}, + 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> { @@ -31,7 +32,7 @@ pub(crate) fn recv(fd: BorrowedFd<'_>, buf: &mut [u8], flags: RecvFlags) -> io:: borrowed_fd(fd), buf.as_mut_ptr().cast(), send_recv_len(buf.len()), - flags.bits(), + bitflags_bits!(flags), )) } } @@ -43,7 +44,7 @@ pub(crate) fn send(fd: BorrowedFd<'_>, buf: &[u8], flags: SendFlags) -> io::Resu borrowed_fd(fd), buf.as_ptr().cast(), send_recv_len(buf.len()), - flags.bits(), + bitflags_bits!(flags), )) } } @@ -67,7 +68,7 @@ pub(crate) fn recvfrom( borrowed_fd(fd), buf.as_mut_ptr().cast(), send_recv_len(buf.len()), - flags.bits(), + bitflags_bits!(flags), storage.as_mut_ptr().cast(), &mut len, )) @@ -92,7 +93,7 @@ pub(crate) fn sendto_v4( borrowed_fd(fd), buf.as_ptr().cast(), send_recv_len(buf.len()), - flags.bits(), + bitflags_bits!(flags), as_ptr(&encode_sockaddr_v4(addr)).cast::<c::sockaddr>(), size_of::<SocketAddrV4>() as _, )) @@ -111,7 +112,7 @@ pub(crate) fn sendto_v6( borrowed_fd(fd), buf.as_ptr().cast(), send_recv_len(buf.len()), - flags.bits(), + bitflags_bits!(flags), as_ptr(&encode_sockaddr_v6(addr)).cast::<c::sockaddr>(), size_of::<SocketAddrV6>() as _, )) @@ -130,7 +131,7 @@ pub(crate) fn sendto_unix( borrowed_fd(fd), buf.as_ptr().cast(), send_recv_len(buf.len()), - flags.bits(), + bitflags_bits!(flags), as_ptr(&addr.unix).cast(), addr.addr_len(), )) @@ -141,13 +142,17 @@ pub(crate) fn sendto_unix( pub(crate) fn socket( domain: AddressFamily, type_: SocketType, - protocol: Protocol, + protocol: Option<Protocol>, ) -> io::Result<OwnedFd> { + let raw_protocol = match protocol { + Some(p) => p.0.get(), + None => 0, + }; unsafe { ret_owned_fd(c::socket( domain.0 as c::c_int, type_.0 as c::c_int, - protocol.0, + raw_protocol as c::c_int, )) } } @@ -157,13 +162,17 @@ pub(crate) fn socket_with( domain: AddressFamily, type_: SocketType, flags: SocketFlags, - protocol: Protocol, + protocol: Option<Protocol>, ) -> io::Result<OwnedFd> { + let raw_protocol = match protocol { + Some(p) => p.0.get(), + None => 0, + }; unsafe { ret_owned_fd(c::socket( domain.0 as c::c_int, - type_.0 as c::c_int | flags.bits(), - protocol.0, + (type_.0 | flags.bits()) as c::c_int, + raw_protocol as c::c_int, )) } } @@ -247,6 +256,105 @@ pub(crate) fn accept(sockfd: BorrowedFd<'_>) -> io::Result<OwnedFd> { } } +#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))] +pub(crate) fn recvmsg( + sockfd: BorrowedFd<'_>, + iov: &mut [IoSliceMut<'_>], + control: &mut RecvAncillaryBuffer<'_>, + msg_flags: RecvFlags, +) -> io::Result<RecvMsgReturn> { + let mut storage = MaybeUninit::<c::sockaddr_storage>::uninit(); + + with_recv_msghdr(&mut storage, iov, control, |msghdr| { + let result = unsafe { + ret_send_recv(c::recvmsg( + borrowed_fd(sockfd), + msghdr, + bitflags_bits!(msg_flags), + )) + }; + + result.map(|bytes| { + // Get the address of the sender, if any. + let addr = + unsafe { maybe_read_sockaddr_os(msghdr.msg_name as _, msghdr.msg_namelen as _) }; + + RecvMsgReturn { + bytes, + address: addr, + flags: RecvFlags::from_bits_retain(bitcast!(msghdr.msg_flags)), + } + }) + }) +} + +#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))] +pub(crate) fn sendmsg( + sockfd: BorrowedFd<'_>, + iov: &[IoSlice<'_>], + control: &mut SendAncillaryBuffer<'_, '_, '_>, + msg_flags: SendFlags, +) -> io::Result<usize> { + with_noaddr_msghdr(iov, control, |msghdr| unsafe { + ret_send_recv(c::sendmsg( + borrowed_fd(sockfd), + &msghdr, + bitflags_bits!(msg_flags), + )) + }) +} + +#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))] +pub(crate) fn sendmsg_v4( + sockfd: BorrowedFd<'_>, + addr: &SocketAddrV4, + iov: &[IoSlice<'_>], + control: &mut SendAncillaryBuffer<'_, '_, '_>, + msg_flags: SendFlags, +) -> io::Result<usize> { + with_v4_msghdr(addr, iov, control, |msghdr| unsafe { + ret_send_recv(c::sendmsg( + borrowed_fd(sockfd), + &msghdr, + bitflags_bits!(msg_flags), + )) + }) +} + +#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))] +pub(crate) fn sendmsg_v6( + sockfd: BorrowedFd<'_>, + addr: &SocketAddrV6, + iov: &[IoSlice<'_>], + control: &mut SendAncillaryBuffer<'_, '_, '_>, + msg_flags: SendFlags, +) -> io::Result<usize> { + with_v6_msghdr(addr, iov, control, |msghdr| unsafe { + ret_send_recv(c::sendmsg( + borrowed_fd(sockfd), + &msghdr, + bitflags_bits!(msg_flags), + )) + }) +} + +#[cfg(all(unix, not(target_os = "redox")))] +pub(crate) fn sendmsg_unix( + sockfd: BorrowedFd<'_>, + addr: &SocketAddrUnix, + iov: &[IoSlice<'_>], + control: &mut SendAncillaryBuffer<'_, '_, '_>, + msg_flags: SendFlags, +) -> io::Result<usize> { + super::msghdr::with_unix_msghdr(addr, iov, control, |msghdr| unsafe { + ret_send_recv(c::sendmsg( + borrowed_fd(sockfd), + &msghdr, + bitflags_bits!(msg_flags), + )) + }) +} + #[cfg(not(any( apple, windows, @@ -260,7 +368,7 @@ pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, flags: SocketFlags) -> io::Res borrowed_fd(sockfd), null_mut(), null_mut(), - flags.bits(), + flags.bits() as c::c_int, ))?; Ok(owned_fd) } @@ -301,7 +409,7 @@ pub(crate) fn acceptfrom_with( borrowed_fd(sockfd), storage.as_mut_ptr().cast(), &mut len, - flags.bits(), + flags.bits() as c::c_int, ))?; Ok(( owned_fd, @@ -368,14 +476,18 @@ pub(crate) fn socketpair( domain: AddressFamily, type_: SocketType, flags: SocketFlags, - protocol: Protocol, + protocol: Option<Protocol>, ) -> io::Result<(OwnedFd, OwnedFd)> { + let raw_protocol = match protocol { + Some(p) => p.0.get(), + None => 0, + }; unsafe { let mut fds = MaybeUninit::<[OwnedFd; 2]>::uninit(); ret(c::socketpair( c::c_int::from(domain.0), - type_.0 as c::c_int | flags.bits(), - protocol.0, + (type_.0 | flags.bits()) as c::c_int, + raw_protocol as c::c_int, fds.as_mut_ptr().cast::<c::c_int>(), ))?; @@ -391,14 +503,10 @@ pub(crate) mod sockopt { use crate::net::sockopt::Timeout; use crate::net::{Ipv4Addr, Ipv6Addr, SocketType}; use crate::utils::as_mut_ptr; - use core::convert::TryInto; use core::time::Duration; #[cfg(windows)] use windows_sys::Win32::Foundation::BOOL; - // TODO: With Rust 1.53 we can use `Duration::ZERO` instead. - const DURATION_ZERO: Duration = Duration::from_secs(0); - #[inline] fn getsockopt<T: Copy>(fd: BorrowedFd<'_>, level: i32, optname: i32) -> io::Result<T> { use super::*; @@ -512,21 +620,16 @@ pub(crate) mod sockopt { #[inline] pub(crate) fn get_socket_linger(fd: BorrowedFd<'_>) -> io::Result<Option<Duration>> { let linger: c::linger = getsockopt(fd, c::SOL_SOCKET as _, c::SO_LINGER)?; - // TODO: With Rust 1.50, this could use `.then`. - Ok(if linger.l_onoff != 0 { - Some(Duration::from_secs(linger.l_linger as u64)) - } else { - None - }) + Ok((linger.l_onoff != 0).then(|| Duration::from_secs(linger.l_linger as u64))) } - #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg(linux_kernel)] #[inline] pub(crate) fn set_socket_passcred(fd: BorrowedFd<'_>, passcred: bool) -> io::Result<()> { setsockopt(fd, c::SOL_SOCKET as _, c::SO_PASSCRED, from_bool(passcred)) } - #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg(linux_kernel)] #[inline] pub(crate) fn get_socket_passcred(fd: BorrowedFd<'_>) -> io::Result<bool> { getsockopt(fd, c::SOL_SOCKET as _, c::SO_PASSCRED).map(to_bool) @@ -546,7 +649,7 @@ pub(crate) mod sockopt { #[cfg(not(windows))] let timeout = match timeout { Some(timeout) => { - if timeout == DURATION_ZERO { + if timeout == Duration::ZERO { return Err(io::Errno::INVAL); } @@ -577,7 +680,7 @@ pub(crate) mod sockopt { #[cfg(windows)] let timeout: u32 = match timeout { Some(timeout) => { - if timeout == DURATION_ZERO { + if timeout == Duration::ZERO { return Err(io::Errno::INVAL); } @@ -633,13 +736,13 @@ pub(crate) mod sockopt { #[cfg(any(apple, target_os = "freebsd"))] #[inline] - pub(crate) fn getsockopt_nosigpipe(fd: BorrowedFd<'_>) -> io::Result<bool> { + pub(crate) fn get_socket_nosigpipe(fd: BorrowedFd<'_>) -> io::Result<bool> { getsockopt(fd, c::SOL_SOCKET, c::SO_NOSIGPIPE).map(to_bool) } #[cfg(any(apple, target_os = "freebsd"))] #[inline] - pub(crate) fn setsockopt_nosigpipe(fd: BorrowedFd<'_>, val: bool) -> io::Result<()> { + pub(crate) fn set_socket_nosigpipe(fd: BorrowedFd<'_>, val: bool) -> io::Result<()> { setsockopt(fd, c::SOL_SOCKET, c::SO_NOSIGPIPE, from_bool(val)) } @@ -764,14 +867,14 @@ pub(crate) mod sockopt { setsockopt( fd, c::IPPROTO_IP as _, - c::IPV6_MULTICAST_LOOP, + c::IPV6_MULTICAST_HOPS, multicast_hops, ) } #[inline] pub(crate) fn get_ipv6_multicast_hops(fd: BorrowedFd<'_>) -> io::Result<u32> { - getsockopt(fd, c::IPPROTO_IP as _, c::IPV6_MULTICAST_LOOP) + getsockopt(fd, c::IPPROTO_IP as _, c::IPV6_MULTICAST_HOPS) } #[inline] diff --git a/vendor/rustix/src/backend/libc/net/types.rs b/vendor/rustix/src/backend/libc/net/types.rs deleted file mode 100644 index d1d769cb4..000000000 --- a/vendor/rustix/src/backend/libc/net/types.rs +++ /dev/null @@ -1,500 +0,0 @@ -use super::super::c; -use bitflags::bitflags; - -/// A type for holding raw integer socket types. -#[doc(hidden)] -pub type RawSocketType = u32; - -/// `SOCK_*` constants for use with [`socket`]. -/// -/// [`socket`]: crate::net::socket -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -#[repr(transparent)] -pub struct SocketType(pub(crate) RawSocketType); - -#[rustfmt::skip] -impl SocketType { - /// `SOCK_STREAM` - pub const STREAM: Self = Self(c::SOCK_STREAM as u32); - - /// `SOCK_DGRAM` - pub const DGRAM: Self = Self(c::SOCK_DGRAM as u32); - - /// `SOCK_SEQPACKET` - pub const SEQPACKET: Self = Self(c::SOCK_SEQPACKET as u32); - - /// `SOCK_RAW` - pub const RAW: Self = Self(c::SOCK_RAW as u32); - - /// `SOCK_RDM` - #[cfg(not(target_os = "haiku"))] - pub const RDM: Self = Self(c::SOCK_RDM as u32); - - /// Constructs a `SocketType` from a raw integer. - #[inline] - pub const fn from_raw(raw: RawSocketType) -> Self { - Self(raw) - } - - /// Returns the raw integer for this `SocketType`. - #[inline] - pub const fn as_raw(self) -> RawSocketType { - self.0 - } -} - -/// A type for holding raw integer address families. -#[doc(hidden)] -pub type RawAddressFamily = c::sa_family_t; - -/// `AF_*` constants. -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -#[repr(transparent)] -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 _); - /// `AF_INET` - pub const INET: Self = Self(c::AF_INET as _); - /// `AF_INET6` - pub const INET6: Self = Self(c::AF_INET6 as _); - /// `AF_NETLINK` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const NETLINK: Self = Self(c::AF_NETLINK as _); - /// `AF_UNIX`, aka `AF_LOCAL` - #[doc(alias = "LOCAL")] - pub const UNIX: Self = Self(c::AF_UNIX as _); - /// `AF_AX25` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const AX25: Self = Self(c::AF_AX25 as _); - /// `AF_IPX` - pub const IPX: Self = Self(c::AF_IPX as _); - /// `AF_APPLETALK` - pub const APPLETALK: Self = Self(c::AF_APPLETALK as _); - /// `AF_NETROM` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const NETROM: Self = Self(c::AF_NETROM as _); - /// `AF_BRIDGE` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const BRIDGE: Self = Self(c::AF_BRIDGE as _); - /// `AF_ATMPVC` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const ATMPVC: Self = Self(c::AF_ATMPVC as _); - /// `AF_X25` - #[cfg(not(any( - bsd, - windows, - target_os = "haiku", - )))] - pub const X25: Self = Self(c::AF_X25 as _); - /// `AF_ROSE` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const ROSE: Self = Self(c::AF_ROSE as _); - /// `AF_DECnet` - #[cfg(not(target_os = "haiku"))] - pub const DECnet: Self = Self(c::AF_DECnet as _); - /// `AF_NETBEUI` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const NETBEUI: Self = Self(c::AF_NETBEUI as _); - /// `AF_SECURITY` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const SECURITY: Self = Self(c::AF_SECURITY as _); - /// `AF_KEY` - #[cfg(not(any( - bsd, - windows, - target_os = "haiku", - )))] - pub const KEY: Self = Self(c::AF_KEY as _); - /// `AF_PACKET` - #[cfg(not(any( - bsd, - windows, - target_os = "haiku", - )))] - pub const PACKET: Self = Self(c::AF_PACKET as _); - /// `AF_ASH` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const ASH: Self = Self(c::AF_ASH as _); - /// `AF_ECONET` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const ECONET: Self = Self(c::AF_ECONET as _); - /// `AF_ATMSVC` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const ATMSVC: Self = Self(c::AF_ATMSVC as _); - /// `AF_RDS` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const RDS: Self = Self(c::AF_RDS as _); - /// `AF_SNA` - #[cfg(not(target_os = "haiku"))] - pub const SNA: Self = Self(c::AF_SNA as _); - /// `AF_IRDA` - #[cfg(not(any( - bsd, - solarish, - target_os = "haiku", - )))] - pub const IRDA: Self = Self(c::AF_IRDA as _); - /// `AF_PPPOX` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const PPPOX: Self = Self(c::AF_PPPOX as _); - /// `AF_WANPIPE` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const WANPIPE: Self = Self(c::AF_WANPIPE as _); - /// `AF_LLC` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const LLC: Self = Self(c::AF_LLC as _); - /// `AF_CAN` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const CAN: Self = Self(c::AF_CAN as _); - /// `AF_TIPC` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const TIPC: Self = Self(c::AF_TIPC as _); - /// `AF_BLUETOOTH` - #[cfg(not(any(apple, solarish, windows)))] - pub const BLUETOOTH: Self = Self(c::AF_BLUETOOTH as _); - /// `AF_IUCV` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const IUCV: Self = Self(c::AF_IUCV as _); - /// `AF_RXRPC` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const RXRPC: Self = Self(c::AF_RXRPC as _); - /// `AF_ISDN` - #[cfg(not(any(solarish, windows, target_os = "haiku")))] - pub const ISDN: Self = Self(c::AF_ISDN as _); - /// `AF_PHONET` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const PHONET: Self = Self(c::AF_PHONET as _); - /// `AF_IEEE802154` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const IEEE802154: Self = Self(c::AF_IEEE802154 as _); - - /// Constructs a `AddressFamily` from a raw integer. - #[inline] - pub const fn from_raw(raw: RawAddressFamily) -> Self { - Self(raw) - } - - /// Returns the raw integer for this `AddressFamily`. - #[inline] - pub const fn as_raw(self) -> RawAddressFamily { - self.0 - } -} - -/// A type for holding raw integer protocols. -#[doc(hidden)] -pub type RawProtocol = i32; - -/// `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); - -#[rustfmt::skip] -impl Protocol { - /// `IPPROTO_IP` - pub const IP: Self = Self(c::IPPROTO_IP as _); - /// `IPPROTO_ICMP` - pub const ICMP: Self = Self(c::IPPROTO_ICMP as _); - /// `IPPROTO_IGMP` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const IGMP: Self = Self(c::IPPROTO_IGMP as _); - /// `IPPROTO_IPIP` - #[cfg(not(any(solarish, windows, target_os = "haiku")))] - pub const IPIP: Self = Self(c::IPPROTO_IPIP as _); - /// `IPPROTO_TCP` - pub const TCP: Self = Self(c::IPPROTO_TCP as _); - /// `IPPROTO_EGP` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const EGP: Self = Self(c::IPPROTO_EGP as _); - /// `IPPROTO_PUP` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const PUP: Self = Self(c::IPPROTO_PUP as _); - /// `IPPROTO_UDP` - pub const UDP: Self = Self(c::IPPROTO_UDP as _); - /// `IPPROTO_IDP` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const IDP: Self = Self(c::IPPROTO_IDP as _); - /// `IPPROTO_TP` - #[cfg(not(any(solarish, windows, target_os = "haiku")))] - pub const TP: Self = Self(c::IPPROTO_TP as _); - /// `IPPROTO_DCCP` - #[cfg(not(any( - apple, - solarish, - windows, - target_os = "dragonfly", - target_os = "haiku", - target_os = "openbsd", - )))] - pub const DCCP: Self = Self(c::IPPROTO_DCCP as _); - /// `IPPROTO_IPV6` - pub const IPV6: Self = Self(c::IPPROTO_IPV6 as _); - /// `IPPROTO_RSVP` - #[cfg(not(any(solarish, windows, target_os = "haiku")))] - pub const RSVP: Self = Self(c::IPPROTO_RSVP as _); - /// `IPPROTO_GRE` - #[cfg(not(any(solarish, windows, target_os = "haiku")))] - pub const GRE: Self = Self(c::IPPROTO_GRE as _); - /// `IPPROTO_ESP` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const ESP: Self = Self(c::IPPROTO_ESP as _); - /// `IPPROTO_AH` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const AH: Self = Self(c::IPPROTO_AH as _); - /// `IPPROTO_MTP` - #[cfg(not(any( - solarish, - netbsdlike, - windows, - target_os = "haiku", - )))] - pub const MTP: Self = Self(c::IPPROTO_MTP as _); - /// `IPPROTO_BEETPH` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const BEETPH: Self = Self(c::IPPROTO_BEETPH as _); - /// `IPPROTO_ENCAP` - #[cfg(not(any(solarish, windows, target_os = "haiku")))] - pub const ENCAP: Self = Self(c::IPPROTO_ENCAP as _); - /// `IPPROTO_PIM` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const PIM: Self = Self(c::IPPROTO_PIM as _); - /// `IPPROTO_COMP` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "haiku", - )))] - pub const COMP: Self = Self(c::IPPROTO_COMP as _); - /// `IPPROTO_SCTP` - #[cfg(not(any(solarish, target_os = "dragonfly", target_os = "haiku", target_os = "openbsd")))] - pub const SCTP: Self = Self(c::IPPROTO_SCTP as _); - /// `IPPROTO_UDPLITE` - #[cfg(not(any( - apple, - netbsdlike, - solarish, - windows, - target_os = "dragonfly", - target_os = "haiku", - )))] - pub const UDPLITE: Self = Self(c::IPPROTO_UDPLITE as _); - /// `IPPROTO_MPLS` - #[cfg(not(any( - apple, - solarish, - windows, - target_os = "dragonfly", - target_os = "haiku", - target_os = "netbsd", - )))] - pub const MPLS: Self = Self(c::IPPROTO_MPLS as _); - /// `IPPROTO_RAW` - pub const RAW: Self = Self(c::IPPROTO_RAW as _); - /// `IPPROTO_MPTCP` - #[cfg(not(any( - bsd, - solarish, - windows, - target_os = "android", - target_os = "emscripten", - target_os = "fuchsia", - target_os = "haiku", - )))] - pub const MPTCP: Self = Self(c::IPPROTO_MPTCP as _); - /// `IPPROTO_FRAGMENT` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const FRAGMENT: Self = Self(c::IPPROTO_FRAGMENT as _); - /// `IPPROTO_ICMPV6` - pub const ICMPV6: Self = Self(c::IPPROTO_ICMPV6 as _); - /// `IPPROTO_MH` - #[cfg(not(any( - apple, - netbsdlike, - solarish, - windows, - target_os = "dragonfly", - target_os = "haiku", - )))] - pub const MH: Self = Self(c::IPPROTO_MH as _); - /// `IPPROTO_ROUTING` - #[cfg(not(any(solarish, target_os = "haiku")))] - pub const ROUTING: Self = Self(c::IPPROTO_ROUTING as _); - - /// Constructs a `Protocol` from a raw integer. - #[inline] - pub const fn from_raw(raw: RawProtocol) -> Self { - Self(raw) - } - - /// Returns the raw integer for this `Protocol`. - #[inline] - pub const fn as_raw(self) -> RawProtocol { - self.0 - } -} - -/// `SHUT_*` constants for use with [`shutdown`]. -/// -/// [`shutdown`]: crate::net::shutdown -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -#[repr(i32)] -pub enum Shutdown { - /// `SHUT_RD`—Disable further read operations. - Read = c::SHUT_RD, - /// `SHUT_WR`—Disable further write operations. - Write = c::SHUT_WR, - /// `SHUT_RDWR`—Disable further read and write operations. - ReadWrite = c::SHUT_RDWR, -} - -bitflags! { - /// `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 SocketFlags: 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; - } -} - -/// Timeout identifier for use with [`set_socket_timeout`] and -/// [`get_socket_timeout`]. -/// -/// [`set_socket_timeout`]: crate::net::sockopt::set_socket_timeout. -/// [`get_socket_timeout`]: crate::net::sockopt::get_socket_timeout. -#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -#[repr(i32)] -pub enum Timeout { - /// `SO_RCVTIMEO`—Timeout for receiving. - Recv = c::SO_RCVTIMEO, - - /// `SO_SNDTIMEO`—Timeout for sending. - Send = c::SO_SNDTIMEO, -} diff --git a/vendor/rustix/src/backend/libc/net/write_sockaddr.rs b/vendor/rustix/src/backend/libc/net/write_sockaddr.rs index f8ab62966..efb5a4e14 100644 --- a/vendor/rustix/src/backend/libc/net/write_sockaddr.rs +++ b/vendor/rustix/src/backend/libc/net/write_sockaddr.rs @@ -1,11 +1,11 @@ //! The BSD sockets API requires us to read the `ss_family` field before //! we can interpret the rest of a `sockaddr` produced by the kernel. -use super::super::c; use super::addr::SocketAddrStorage; #[cfg(unix)] use super::addr::SocketAddrUnix; use super::ext::{in6_addr_new, in_addr_new, sockaddr_in6_new}; +use crate::backend::c; use crate::net::{SocketAddrAny, SocketAddrV4, SocketAddrV6}; use core::mem::size_of; |