Split net.rs into multiple files
This commit is contained in:
parent
0b3c9d8465
commit
6fa7c3f79e
9 changed files with 3297 additions and 2635 deletions
File diff suppressed because it is too large
Load diff
226
library/std/src/sys/unix/ext/net/addr.rs
Normal file
226
library/std/src/sys/unix/ext/net/addr.rs
Normal file
|
@ -0,0 +1,226 @@
|
|||
use crate::ffi::OsStr;
|
||||
use crate::os::unix::ffi::OsStrExt;
|
||||
use crate::path::Path;
|
||||
use crate::sys::unix::cvt;
|
||||
use crate::{ascii, fmt, io, mem};
|
||||
|
||||
// FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
|
||||
#[cfg(not(unix))]
|
||||
#[allow(non_camel_case_types)]
|
||||
mod libc {
|
||||
pub use libc::c_int;
|
||||
pub type socklen_t = u32;
|
||||
pub struct sockaddr;
|
||||
#[derive(Clone)]
|
||||
pub struct sockaddr_un;
|
||||
}
|
||||
|
||||
fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
|
||||
// Work with an actual instance of the type since using a null pointer is UB
|
||||
let base = addr as *const _ as usize;
|
||||
let path = &addr.sun_path as *const _ as usize;
|
||||
path - base
|
||||
}
|
||||
|
||||
pub(super) unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
|
||||
let mut addr: libc::sockaddr_un = mem::zeroed();
|
||||
addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
|
||||
|
||||
let bytes = path.as_os_str().as_bytes();
|
||||
|
||||
if bytes.contains(&0) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"paths may not contain interior null bytes",
|
||||
));
|
||||
}
|
||||
|
||||
if bytes.len() >= addr.sun_path.len() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"path must be shorter than SUN_LEN",
|
||||
));
|
||||
}
|
||||
for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
|
||||
*dst = *src as libc::c_char;
|
||||
}
|
||||
// null byte for pathname addresses is already there because we zeroed the
|
||||
// struct
|
||||
|
||||
let mut len = sun_path_offset(&addr) + bytes.len();
|
||||
match bytes.get(0) {
|
||||
Some(&0) | None => {}
|
||||
Some(_) => len += 1,
|
||||
}
|
||||
Ok((addr, len as libc::socklen_t))
|
||||
}
|
||||
|
||||
enum AddressKind<'a> {
|
||||
Unnamed,
|
||||
Pathname(&'a Path),
|
||||
Abstract(&'a [u8]),
|
||||
}
|
||||
|
||||
struct AsciiEscaped<'a>(&'a [u8]);
|
||||
|
||||
impl<'a> fmt::Display for AsciiEscaped<'a> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(fmt, "\"")?;
|
||||
for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
|
||||
write!(fmt, "{}", byte as char)?;
|
||||
}
|
||||
write!(fmt, "\"")
|
||||
}
|
||||
}
|
||||
|
||||
/// An address associated with a Unix socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
///
|
||||
/// let socket = match UnixListener::bind("/tmp/sock") {
|
||||
/// Ok(sock) => sock,
|
||||
/// Err(e) => {
|
||||
/// println!("Couldn't bind: {:?}", e);
|
||||
/// return
|
||||
/// }
|
||||
/// };
|
||||
/// let addr = socket.local_addr().expect("Couldn't get local address");
|
||||
/// ```
|
||||
#[derive(Clone)]
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub struct SocketAddr {
|
||||
addr: libc::sockaddr_un,
|
||||
len: libc::socklen_t,
|
||||
}
|
||||
|
||||
impl SocketAddr {
|
||||
pub(super) fn new<F>(f: F) -> io::Result<SocketAddr>
|
||||
where
|
||||
F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int,
|
||||
{
|
||||
unsafe {
|
||||
let mut addr: libc::sockaddr_un = mem::zeroed();
|
||||
let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
|
||||
cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
|
||||
SocketAddr::from_parts(addr, len)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn from_parts(
|
||||
addr: libc::sockaddr_un,
|
||||
mut len: libc::socklen_t,
|
||||
) -> io::Result<SocketAddr> {
|
||||
if len == 0 {
|
||||
// When there is a datagram from unnamed unix socket
|
||||
// linux returns zero bytes of address
|
||||
len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
|
||||
} else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"file descriptor did not correspond to a Unix socket",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(SocketAddr { addr, len })
|
||||
}
|
||||
|
||||
/// Returns `true` if the address is unnamed.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// A named address:
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixListener::bind("/tmp/sock")?;
|
||||
/// let addr = socket.local_addr().expect("Couldn't get local address");
|
||||
/// assert_eq!(addr.is_unnamed(), false);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// An unnamed address:
|
||||
///
|
||||
/// ```
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixDatagram::unbound()?;
|
||||
/// let addr = socket.local_addr().expect("Couldn't get local address");
|
||||
/// assert_eq!(addr.is_unnamed(), true);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn is_unnamed(&self) -> bool {
|
||||
if let AddressKind::Unnamed = self.address() { true } else { false }
|
||||
}
|
||||
|
||||
/// Returns the contents of this address if it is a `pathname` address.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// With a pathname:
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
/// use std::path::Path;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixListener::bind("/tmp/sock")?;
|
||||
/// let addr = socket.local_addr().expect("Couldn't get local address");
|
||||
/// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Without a pathname:
|
||||
///
|
||||
/// ```
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixDatagram::unbound()?;
|
||||
/// let addr = socket.local_addr().expect("Couldn't get local address");
|
||||
/// assert_eq!(addr.as_pathname(), None);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn as_pathname(&self) -> Option<&Path> {
|
||||
if let AddressKind::Pathname(path) = self.address() { Some(path) } else { None }
|
||||
}
|
||||
|
||||
fn address(&self) -> AddressKind<'_> {
|
||||
let len = self.len as usize - sun_path_offset(&self.addr);
|
||||
let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
|
||||
|
||||
// macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
|
||||
if len == 0
|
||||
|| (cfg!(not(any(target_os = "linux", target_os = "android")))
|
||||
&& self.addr.sun_path[0] == 0)
|
||||
{
|
||||
AddressKind::Unnamed
|
||||
} else if self.addr.sun_path[0] == 0 {
|
||||
AddressKind::Abstract(&path[1..len])
|
||||
} else {
|
||||
AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl fmt::Debug for SocketAddr {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self.address() {
|
||||
AddressKind::Unnamed => write!(fmt, "(unnamed)"),
|
||||
AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
|
||||
AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
|
||||
}
|
||||
}
|
||||
}
|
614
library/std/src/sys/unix/ext/net/ancillary.rs
Normal file
614
library/std/src/sys/unix/ext/net/ancillary.rs
Normal file
|
@ -0,0 +1,614 @@
|
|||
use crate::io::{self, IoSliceMut};
|
||||
use crate::mem;
|
||||
use crate::os::unix::io::RawFd;
|
||||
use crate::path::Path;
|
||||
use crate::ptr::null_mut;
|
||||
use crate::slice::from_raw_parts;
|
||||
use crate::sys::unix::ext::net::addr::{sockaddr_un, SocketAddr};
|
||||
use crate::sys::unix::net::{add_to_ancillary_data, AncillaryDataIter, Socket};
|
||||
|
||||
pub(super) fn recv_vectored_with_ancillary_from(
|
||||
socket: &Socket,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
ancillary: &mut SocketAncillary<'_>,
|
||||
) -> io::Result<(usize, bool, io::Result<SocketAddr>)> {
|
||||
unsafe {
|
||||
let mut msg_name: libc::sockaddr_un = mem::zeroed();
|
||||
|
||||
let mut msg = libc::msghdr {
|
||||
msg_name: &mut msg_name as *mut _ as *mut _,
|
||||
msg_namelen: mem::size_of::<libc::sockaddr_un>() as libc::socklen_t,
|
||||
msg_iov: bufs.as_mut_ptr().cast(),
|
||||
msg_iovlen: bufs.len(),
|
||||
msg_control: ancillary.buffer.as_mut_ptr().cast(),
|
||||
msg_controllen: ancillary.buffer.len(),
|
||||
msg_flags: 0,
|
||||
};
|
||||
|
||||
let count = socket.recv_msg(&mut msg)?;
|
||||
|
||||
ancillary.length = msg.msg_controllen;
|
||||
ancillary.truncated = msg.msg_flags & libc::MSG_CTRUNC == libc::MSG_CTRUNC;
|
||||
|
||||
let truncated = msg.msg_flags & libc::MSG_TRUNC == libc::MSG_TRUNC;
|
||||
let addr = SocketAddr::from_parts(msg_name, msg.msg_namelen);
|
||||
|
||||
Ok((count, truncated, addr))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn send_vectored_with_ancillary_to(
|
||||
socket: &Socket,
|
||||
path: Option<&Path>,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
ancillary: &mut SocketAncillary<'_>,
|
||||
) -> io::Result<usize> {
|
||||
unsafe {
|
||||
let (mut msg_name, msg_namelen) =
|
||||
if let Some(path) = path { sockaddr_un(path)? } else { (mem::zeroed(), 0) };
|
||||
|
||||
let mut msg = libc::msghdr {
|
||||
msg_name: &mut msg_name as *mut _ as *mut _,
|
||||
msg_namelen,
|
||||
msg_iov: bufs.as_mut_ptr().cast(),
|
||||
msg_iovlen: bufs.len(),
|
||||
msg_control: ancillary.buffer.as_mut_ptr().cast(),
|
||||
msg_controllen: ancillary.length,
|
||||
msg_flags: 0,
|
||||
};
|
||||
|
||||
ancillary.truncated = false;
|
||||
|
||||
socket.send_msg(&mut msg)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub struct ScmRights<'a>(AncillaryDataIter<'a, RawFd>);
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
impl<'a> Iterator for ScmRights<'a> {
|
||||
type Item = RawFd;
|
||||
|
||||
fn next(&mut self) -> Option<RawFd> {
|
||||
self.0.next()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::ucred>);
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
impl<'a> Iterator for ScmCredentials<'a> {
|
||||
type Item = libc::ucred;
|
||||
|
||||
fn next(&mut self) -> Option<libc::ucred> {
|
||||
self.0.next()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub enum AncillaryData<'a> {
|
||||
ScmRights(ScmRights<'a>),
|
||||
#[cfg(any(
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
ScmCredentials(ScmCredentials<'a>),
|
||||
}
|
||||
|
||||
impl<'a> AncillaryData<'a> {
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
fn as_rights(data: &'a [u8]) -> Self {
|
||||
let ancillary_data_iter = AncillaryDataIter::new(data);
|
||||
let scm_rights = ScmRights(ancillary_data_iter);
|
||||
AncillaryData::ScmRights(scm_rights)
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
fn as_credentials(data: &'a [u8]) -> Self {
|
||||
let ancillary_data_iter = AncillaryDataIter::new(data);
|
||||
let scm_credentials = ScmCredentials(ancillary_data_iter);
|
||||
AncillaryData::ScmCredentials(scm_credentials)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
impl<'a> AncillaryData<'a> {
|
||||
fn from(cmsg: &'a libc::cmsghdr) -> Self {
|
||||
unsafe {
|
||||
let cmsg_len_zero = libc::CMSG_LEN(0) as usize;
|
||||
let data_len = (*cmsg).cmsg_len - cmsg_len_zero;
|
||||
let data = libc::CMSG_DATA(cmsg).cast();
|
||||
let data = from_raw_parts(data, data_len);
|
||||
|
||||
if (*cmsg).cmsg_level == libc::SOL_SOCKET {
|
||||
match (*cmsg).cmsg_type {
|
||||
libc::SCM_RIGHTS => AncillaryData::as_rights(data),
|
||||
#[cfg(any(
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
libc::SCM_CREDENTIALS => AncillaryData::as_credentials(data),
|
||||
#[cfg(any(
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
))]
|
||||
libc::SCM_CREDS => AncillaryData::as_credentials(data),
|
||||
_ => panic!("Unknown cmsg type"),
|
||||
}
|
||||
} else {
|
||||
panic!("Unknown cmsg level");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub struct Messages<'a> {
|
||||
buffer: &'a [u8],
|
||||
current: Option<&'a libc::cmsghdr>,
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
impl<'a> Iterator for Messages<'a> {
|
||||
type Item = AncillaryData<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<AncillaryData<'a>> {
|
||||
unsafe {
|
||||
let msg = libc::msghdr {
|
||||
msg_name: null_mut(),
|
||||
msg_namelen: 0,
|
||||
msg_iov: null_mut(),
|
||||
msg_iovlen: 0,
|
||||
msg_control: self.buffer.as_ptr() as *mut _,
|
||||
msg_controllen: self.buffer.len(),
|
||||
msg_flags: 0,
|
||||
};
|
||||
|
||||
let cmsg = if let Some(current) = self.current {
|
||||
libc::CMSG_NXTHDR(&msg, current)
|
||||
} else {
|
||||
libc::CMSG_FIRSTHDR(&msg)
|
||||
};
|
||||
|
||||
let cmsg = cmsg.as_ref()?;
|
||||
self.current = Some(cmsg);
|
||||
let ancillary_data = AncillaryData::from(cmsg);
|
||||
Some(ancillary_data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A Unix socket Ancillary data struct.
|
||||
///
|
||||
/// # Example
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixStream::connect("/tmp/sock")?;
|
||||
///
|
||||
/// let mut fds = [0; 8];
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
///
|
||||
/// let mut buf = [1; 8];
|
||||
/// let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
|
||||
/// sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
|
||||
///
|
||||
/// for ancillary_data in ancillary.messages() {
|
||||
/// if let AncillaryData::ScmRights(scm_rights) = ancillary_data {
|
||||
/// for fd in scm_rights {
|
||||
/// println!("receive file descriptor: {}", fd);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
#[derive(Debug)]
|
||||
pub struct SocketAncillary<'a> {
|
||||
buffer: &'a mut [u8],
|
||||
length: usize,
|
||||
truncated: bool,
|
||||
}
|
||||
|
||||
impl<'a> SocketAncillary<'a> {
|
||||
/// Create an ancillary data with the given buffer.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// # #![allow(unused_mut)]
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::SocketAncillary;
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn new(buffer: &'a mut [u8]) -> Self {
|
||||
SocketAncillary { buffer, length: 0, truncated: false }
|
||||
}
|
||||
|
||||
/// Returns the capacity of the buffer.
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn capacity(&self) -> usize {
|
||||
self.buffer.len()
|
||||
}
|
||||
|
||||
/// Returns the number of used bytes.
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn len(&self) -> usize {
|
||||
self.length
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn messages(&'a self) -> Messages<'a> {
|
||||
Messages { buffer: &self.buffer[..self.length], current: None }
|
||||
}
|
||||
|
||||
/// Is `true` if during a recv operation the ancillary was truncated.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixStream, SocketAncillary};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixStream::connect("/tmp/sock")?;
|
||||
///
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
///
|
||||
/// let mut buf = [1; 8];
|
||||
/// let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
|
||||
/// sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
|
||||
///
|
||||
/// println!("Is truncated: {}", ancillary.truncated());
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn truncated(&self) -> bool {
|
||||
self.truncated
|
||||
}
|
||||
|
||||
/// Add file descriptors to the ancillary data.
|
||||
///
|
||||
/// The function returns `true` if there was enough space in the buffer.
|
||||
/// If there was not enough space then no file descriptors was appended.
|
||||
/// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
|
||||
/// and type `SCM_RIGHTS`.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixStream, SocketAncillary};
|
||||
/// use std::os::unix::io::AsRawFd;
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixStream::connect("/tmp/sock")?;
|
||||
///
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
/// ancillary.add_fds(&[sock.as_raw_fd()][..]);
|
||||
///
|
||||
/// let mut buf = [1; 8];
|
||||
/// let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
|
||||
/// sock.send_vectored_with_ancillary(bufs, &mut ancillary)?;
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(any(
|
||||
target_os = "haiku",
|
||||
target_os = "solaris",
|
||||
target_os = "illumos",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn add_fds(&mut self, fds: &[RawFd]) -> bool {
|
||||
self.truncated = false;
|
||||
add_to_ancillary_data(
|
||||
&mut self.buffer,
|
||||
&mut self.length,
|
||||
fds,
|
||||
libc::SOL_SOCKET,
|
||||
libc::SCM_RIGHTS,
|
||||
)
|
||||
}
|
||||
|
||||
/// Add credentials to the ancillary data.
|
||||
///
|
||||
/// The function returns `true` if there was enough space in the buffer.
|
||||
/// If there was not enough space then no credentials was appended.
|
||||
/// Technically, that means this operation adds a control message with the level `SOL_SOCKET`
|
||||
/// and type `SCM_CREDENTIALS`.
|
||||
///
|
||||
#[cfg(any(
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn add_creds(&mut self, creds: &[libc::ucred]) -> bool {
|
||||
self.truncated = false;
|
||||
add_to_ancillary_data(
|
||||
&mut self.buffer,
|
||||
&mut self.length,
|
||||
creds,
|
||||
libc::SOL_SOCKET,
|
||||
#[cfg(any(
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_env = "uclibc",
|
||||
))]
|
||||
libc::SCM_CREDENTIALS,
|
||||
#[cfg(any(
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
))]
|
||||
libc::SCM_CREDS,
|
||||
)
|
||||
}
|
||||
|
||||
/// Clears the ancillary data, removing all values.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixStream::connect("/tmp/sock")?;
|
||||
///
|
||||
/// let mut fds1 = [0; 8];
|
||||
/// let mut fds2 = [0; 8];
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
///
|
||||
/// let mut buf = [1; 8];
|
||||
/// let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
|
||||
///
|
||||
/// sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
|
||||
/// for ancillary_data in ancillary.messages() {
|
||||
/// if let AncillaryData::ScmRights(scm_rights) = ancillary_data {
|
||||
/// for fd in scm_rights {
|
||||
/// println!("receive file descriptor: {}", fd);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// ancillary.clear();
|
||||
///
|
||||
/// sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
|
||||
/// for ancillary_data in ancillary.messages() {
|
||||
/// if let AncillaryData::ScmRights(scm_rights) = ancillary_data {
|
||||
/// for fd in scm_rights {
|
||||
/// println!("receive file descriptor: {}", fd);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn clear(&mut self) {
|
||||
self.length = 0;
|
||||
self.truncated = false;
|
||||
}
|
||||
}
|
819
library/std/src/sys/unix/ext/net/datagram.rs
Normal file
819
library/std/src/sys/unix/ext/net/datagram.rs
Normal file
|
@ -0,0 +1,819 @@
|
|||
use crate::io::IoSliceMut;
|
||||
use crate::net::Shutdown;
|
||||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
use crate::path::Path;
|
||||
use crate::sys::unix::cvt;
|
||||
use crate::sys::unix::ext::net::addr::{sockaddr_un, SocketAddr};
|
||||
use crate::sys::unix::ext::net::ancillary::{
|
||||
recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary,
|
||||
};
|
||||
use crate::sys::unix::net::Socket;
|
||||
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
||||
use crate::time::Duration;
|
||||
use crate::{fmt, io};
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "haiku"
|
||||
))]
|
||||
use libc::MSG_NOSIGNAL;
|
||||
#[cfg(not(any(
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "haiku"
|
||||
)))]
|
||||
const MSG_NOSIGNAL: libc::c_int = 0x0;
|
||||
|
||||
/// A Unix datagram socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixDatagram::bind("/path/to/my/socket")?;
|
||||
/// socket.send_to(b"hello world", "/path/to/other/socket")?;
|
||||
/// let mut buf = [0; 100];
|
||||
/// let (count, address) = socket.recv_from(&mut buf)?;
|
||||
/// println!("socket {:?} sent {:?}", address, &buf[..count]);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub struct UnixDatagram(Socket);
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl fmt::Debug for UnixDatagram {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut builder = fmt.debug_struct("UnixDatagram");
|
||||
builder.field("fd", self.0.as_inner());
|
||||
if let Ok(addr) = self.local_addr() {
|
||||
builder.field("local", &addr);
|
||||
}
|
||||
if let Ok(addr) = self.peer_addr() {
|
||||
builder.field("peer", &addr);
|
||||
}
|
||||
builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl UnixDatagram {
|
||||
/// Creates a Unix datagram socket bound to the given path.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// let sock = match UnixDatagram::bind("/path/to/the/socket") {
|
||||
/// Ok(sock) => sock,
|
||||
/// Err(e) => {
|
||||
/// println!("Couldn't bind: {:?}", e);
|
||||
/// return
|
||||
/// }
|
||||
/// };
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
|
||||
fn inner(path: &Path) -> io::Result<UnixDatagram> {
|
||||
unsafe {
|
||||
let socket = UnixDatagram::unbound()?;
|
||||
let (addr, len) = sockaddr_un(path)?;
|
||||
|
||||
cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len as _))?;
|
||||
|
||||
Ok(socket)
|
||||
}
|
||||
}
|
||||
inner(path.as_ref())
|
||||
}
|
||||
|
||||
/// Creates a Unix Datagram socket which is not bound to any address.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// let sock = match UnixDatagram::unbound() {
|
||||
/// Ok(sock) => sock,
|
||||
/// Err(e) => {
|
||||
/// println!("Couldn't unbound: {:?}", e);
|
||||
/// return
|
||||
/// }
|
||||
/// };
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn unbound() -> io::Result<UnixDatagram> {
|
||||
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
|
||||
Ok(UnixDatagram(inner))
|
||||
}
|
||||
|
||||
/// Creates an unnamed pair of connected sockets.
|
||||
///
|
||||
/// Returns two `UnixDatagrams`s which are connected to each other.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// let (sock1, sock2) = match UnixDatagram::pair() {
|
||||
/// Ok((sock1, sock2)) => (sock1, sock2),
|
||||
/// Err(e) => {
|
||||
/// println!("Couldn't unbound: {:?}", e);
|
||||
/// return
|
||||
/// }
|
||||
/// };
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
|
||||
let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
|
||||
Ok((UnixDatagram(i1), UnixDatagram(i2)))
|
||||
}
|
||||
|
||||
/// Connects the socket to the specified address.
|
||||
///
|
||||
/// The [`send`] method may be used to send data to the specified address.
|
||||
/// [`recv`] and [`recv_from`] will only receive data from that address.
|
||||
///
|
||||
/// [`send`]: UnixDatagram::send
|
||||
/// [`recv`]: UnixDatagram::recv
|
||||
/// [`recv_from`]: UnixDatagram::recv_from
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// match sock.connect("/path/to/the/socket") {
|
||||
/// Ok(sock) => sock,
|
||||
/// Err(e) => {
|
||||
/// println!("Couldn't connect: {:?}", e);
|
||||
/// return Err(e)
|
||||
/// }
|
||||
/// };
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
|
||||
fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
|
||||
unsafe {
|
||||
let (addr, len) = sockaddr_un(path)?;
|
||||
|
||||
cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
inner(self, path.as_ref())
|
||||
}
|
||||
|
||||
/// Creates a new independently owned handle to the underlying socket.
|
||||
///
|
||||
/// The returned `UnixDatagram` is a reference to the same socket that this
|
||||
/// object references. Both handles can be used to accept incoming
|
||||
/// connections and options set on one side will affect the other.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
|
||||
/// let sock_copy = sock.try_clone().expect("try_clone failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn try_clone(&self) -> io::Result<UnixDatagram> {
|
||||
self.0.duplicate().map(UnixDatagram)
|
||||
}
|
||||
|
||||
/// Returns the address of this socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
|
||||
/// let addr = sock.local_addr().expect("Couldn't get local address");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||
SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
|
||||
}
|
||||
|
||||
/// Returns the address of this socket's peer.
|
||||
///
|
||||
/// The [`connect`] method will connect the socket to a peer.
|
||||
///
|
||||
/// [`connect`]: UnixDatagram::connect
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.connect("/path/to/the/socket")?;
|
||||
///
|
||||
/// let addr = sock.peer_addr().expect("Couldn't get peer address");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||
SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
|
||||
}
|
||||
|
||||
fn recv_from_flags(
|
||||
&self,
|
||||
buf: &mut [u8],
|
||||
flags: libc::c_int,
|
||||
) -> io::Result<(usize, SocketAddr)> {
|
||||
let mut count = 0;
|
||||
let addr = SocketAddr::new(|addr, len| unsafe {
|
||||
count = libc::recvfrom(
|
||||
*self.0.as_inner(),
|
||||
buf.as_mut_ptr() as *mut _,
|
||||
buf.len(),
|
||||
flags,
|
||||
addr,
|
||||
len,
|
||||
);
|
||||
if count > 0 {
|
||||
1
|
||||
} else if count == 0 {
|
||||
0
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
})?;
|
||||
|
||||
Ok((count as usize, addr))
|
||||
}
|
||||
|
||||
/// Receives data from the socket.
|
||||
///
|
||||
/// On success, returns the number of bytes read and the address from
|
||||
/// whence the data came.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// let mut buf = vec![0; 10];
|
||||
/// let (size, sender) = sock.recv_from(buf.as_mut_slice())?;
|
||||
/// println!("received {} bytes from {:?}", size, sender);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
||||
self.recv_from_flags(buf, 0)
|
||||
}
|
||||
|
||||
/// Receives data from the socket.
|
||||
///
|
||||
/// On success, returns the number of bytes read.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::bind("/path/to/the/socket")?;
|
||||
/// let mut buf = vec![0; 10];
|
||||
/// sock.recv(buf.as_mut_slice()).expect("recv function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.0.read(buf)
|
||||
}
|
||||
|
||||
/// Receives data and ancillary data from socket.
|
||||
///
|
||||
/// On success, returns the number of bytes read, if the data was truncated and the address from whence the msg came.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixDatagram, SocketAncillary, AncillaryData};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// let mut buf1 = [1; 8];
|
||||
/// let mut buf2 = [2; 16];
|
||||
/// let mut buf3 = [3; 8];
|
||||
/// let mut bufs = &mut [
|
||||
/// IoSliceMut::new(&mut buf1),
|
||||
/// IoSliceMut::new(&mut buf2),
|
||||
/// IoSliceMut::new(&mut buf3),
|
||||
/// ][..];
|
||||
/// let mut fds = [0; 8];
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
/// let (size, _truncated, sender) = sock.recv_vectored_with_ancillary_from(bufs, &mut ancillary)?;
|
||||
/// println!("received {}", size);
|
||||
/// for ancillary_data in ancillary.messages() {
|
||||
/// if let AncillaryData::ScmRights(scm_rights) = ancillary_data {
|
||||
/// for fd in scm_rights {
|
||||
/// println!("receive file descriptor: {}", fd);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn recv_vectored_with_ancillary_from(
|
||||
&self,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
ancillary: &mut SocketAncillary<'_>,
|
||||
) -> io::Result<(usize, bool, SocketAddr)> {
|
||||
let (count, truncated, addr) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
|
||||
let addr = addr?;
|
||||
|
||||
Ok((count, truncated, addr))
|
||||
}
|
||||
|
||||
/// Receives data and ancillary data from socket.
|
||||
///
|
||||
/// On success, returns the number of bytes read and if the data was truncated.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixDatagram, SocketAncillary, AncillaryData};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// let mut buf1 = [1; 8];
|
||||
/// let mut buf2 = [2; 16];
|
||||
/// let mut buf3 = [3; 8];
|
||||
/// let mut bufs = &mut [
|
||||
/// IoSliceMut::new(&mut buf1),
|
||||
/// IoSliceMut::new(&mut buf2),
|
||||
/// IoSliceMut::new(&mut buf3),
|
||||
/// ][..];
|
||||
/// let mut fds = [0; 8];
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
/// let (size, _truncated) = sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
|
||||
/// println!("received {}", size);
|
||||
/// for ancillary_data in ancillary.messages() {
|
||||
/// if let AncillaryData::ScmRights(scm_rights) = ancillary_data {
|
||||
/// for fd in scm_rights {
|
||||
/// println!("receive file descriptor: {}", fd);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn recv_vectored_with_ancillary(
|
||||
&self,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
ancillary: &mut SocketAncillary<'_>,
|
||||
) -> io::Result<(usize, bool)> {
|
||||
let (count, truncated, addr) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
|
||||
addr?;
|
||||
|
||||
Ok((count, truncated))
|
||||
}
|
||||
|
||||
/// Sends data on the socket to the specified address.
|
||||
///
|
||||
/// On success, returns the number of bytes written.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
|
||||
fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
|
||||
unsafe {
|
||||
let (addr, len) = sockaddr_un(path)?;
|
||||
|
||||
let count = cvt(libc::sendto(
|
||||
*d.0.as_inner(),
|
||||
buf.as_ptr() as *const _,
|
||||
buf.len(),
|
||||
MSG_NOSIGNAL,
|
||||
&addr as *const _ as *const _,
|
||||
len,
|
||||
))?;
|
||||
Ok(count as usize)
|
||||
}
|
||||
}
|
||||
inner(self, buf, path.as_ref())
|
||||
}
|
||||
|
||||
/// Sends data on the socket to the socket's peer.
|
||||
///
|
||||
/// The peer address may be set by the `connect` method, and this method
|
||||
/// will return an error if the socket has not already been connected.
|
||||
///
|
||||
/// On success, returns the number of bytes written.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.connect("/some/sock").expect("Couldn't connect");
|
||||
/// sock.send(b"omelette au fromage").expect("send_to function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.0.write(buf)
|
||||
}
|
||||
|
||||
/// Sends data and ancillary data on the socket to the specified address.
|
||||
///
|
||||
/// On success, returns the number of bytes written.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixDatagram, SocketAncillary};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// let mut buf1 = [1; 8];
|
||||
/// let mut buf2 = [2; 16];
|
||||
/// let mut buf3 = [3; 8];
|
||||
/// let mut bufs = &mut [
|
||||
/// IoSliceMut::new(&mut buf1),
|
||||
/// IoSliceMut::new(&mut buf2),
|
||||
/// IoSliceMut::new(&mut buf3),
|
||||
/// ][..];
|
||||
/// let fds = [0, 1, 2];
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
/// ancillary.add_fds(&fds[..]);
|
||||
/// sock.send_vectored_with_ancillary_to(bufs, &mut ancillary, "/some/sock").expect("send_vectored_with_ancillary_to function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn send_vectored_with_ancillary_to<P: AsRef<Path>>(
|
||||
&self,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
ancillary: &mut SocketAncillary<'_>,
|
||||
path: P,
|
||||
) -> io::Result<usize> {
|
||||
send_vectored_with_ancillary_to(&self.0, Some(path.as_ref()), bufs, ancillary)
|
||||
}
|
||||
|
||||
/// Sends data and ancillary data on the socket.
|
||||
///
|
||||
/// On success, returns the number of bytes written.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixDatagram, SocketAncillary};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// let mut buf1 = [1; 8];
|
||||
/// let mut buf2 = [2; 16];
|
||||
/// let mut buf3 = [3; 8];
|
||||
/// let mut bufs = &mut [
|
||||
/// IoSliceMut::new(&mut buf1),
|
||||
/// IoSliceMut::new(&mut buf2),
|
||||
/// IoSliceMut::new(&mut buf3),
|
||||
/// ][..];
|
||||
/// let fds = [0, 1, 2];
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
/// ancillary.add_fds(&fds[..]);
|
||||
/// sock.send_vectored_with_ancillary(bufs, &mut ancillary).expect("send_vectored_with_ancillary function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn send_vectored_with_ancillary(
|
||||
&self,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
ancillary: &mut SocketAncillary<'_>,
|
||||
) -> io::Result<usize> {
|
||||
send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)
|
||||
}
|
||||
|
||||
/// Sets the read timeout for the socket.
|
||||
///
|
||||
/// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
|
||||
/// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
|
||||
/// is passed to this method.
|
||||
///
|
||||
/// [`recv`]: UnixDatagram::recv
|
||||
/// [`recv_from`]: UnixDatagram::recv_from
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.set_read_timeout(Some(Duration::new(1, 0)))
|
||||
/// .expect("set_read_timeout function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
|
||||
/// method:
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::io;
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixDatagram::unbound()?;
|
||||
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
|
||||
/// let err = result.unwrap_err();
|
||||
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
|
||||
self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
|
||||
}
|
||||
|
||||
/// Sets the write timeout for the socket.
|
||||
///
|
||||
/// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
|
||||
/// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
|
||||
/// method.
|
||||
///
|
||||
/// [`send`]: UnixDatagram::send
|
||||
/// [`send_to`]: UnixDatagram::send_to
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
|
||||
/// .expect("set_write_timeout function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
|
||||
/// method:
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::io;
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixDatagram::unbound()?;
|
||||
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
|
||||
/// let err = result.unwrap_err();
|
||||
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
|
||||
self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
|
||||
}
|
||||
|
||||
/// Returns the read timeout of this socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.set_read_timeout(Some(Duration::new(1, 0)))
|
||||
/// .expect("set_read_timeout function failed");
|
||||
/// assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0)));
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
|
||||
self.0.timeout(libc::SO_RCVTIMEO)
|
||||
}
|
||||
|
||||
/// Returns the write timeout of this socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.set_write_timeout(Some(Duration::new(1, 0)))
|
||||
/// .expect("set_write_timeout function failed");
|
||||
/// assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0)));
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
|
||||
self.0.timeout(libc::SO_SNDTIMEO)
|
||||
}
|
||||
|
||||
/// Moves the socket into or out of nonblocking mode.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.set_nonblocking(true).expect("set_nonblocking function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
||||
self.0.set_nonblocking(nonblocking)
|
||||
}
|
||||
|
||||
/// Returns the value of the `SO_ERROR` option.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// if let Ok(Some(err)) = sock.take_error() {
|
||||
/// println!("Got error: {:?}", err);
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
||||
self.0.take_error()
|
||||
}
|
||||
|
||||
/// Shut down the read, write, or both halves of this connection.
|
||||
///
|
||||
/// This function will cause all pending and future I/O calls on the
|
||||
/// specified portions to immediately return with an appropriate value
|
||||
/// (see the documentation of [`Shutdown`]).
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
/// use std::net::Shutdown;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let sock = UnixDatagram::unbound()?;
|
||||
/// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
|
||||
self.0.shutdown(how)
|
||||
}
|
||||
|
||||
/// Receives data on the socket from the remote address to which it is
|
||||
/// connected, without removing that data from the queue. On success,
|
||||
/// returns the number of bytes peeked.
|
||||
///
|
||||
/// Successive calls return the same data. This is accomplished by passing
|
||||
/// `MSG_PEEK` as a flag to the underlying `recv` system call.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_peek)]
|
||||
///
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixDatagram::bind("/tmp/sock")?;
|
||||
/// let mut buf = [0; 10];
|
||||
/// let len = socket.peek(&mut buf).expect("peek failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_peek", issue = "76923")]
|
||||
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.0.peek(buf)
|
||||
}
|
||||
|
||||
/// Receives a single datagram message on the socket, without removing it from the
|
||||
/// queue. On success, returns the number of bytes read and the origin.
|
||||
///
|
||||
/// The function must be called with valid byte array `buf` of sufficient size to
|
||||
/// hold the message bytes. If a message is too long to fit in the supplied buffer,
|
||||
/// excess bytes may be discarded.
|
||||
///
|
||||
/// Successive calls return the same data. This is accomplished by passing
|
||||
/// `MSG_PEEK` as a flag to the underlying `recvfrom` system call.
|
||||
///
|
||||
/// Do not use this function to implement busy waiting, instead use `libc::poll` to
|
||||
/// synchronize IO events on one or more sockets.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_peek)]
|
||||
///
|
||||
/// use std::os::unix::net::UnixDatagram;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixDatagram::bind("/tmp/sock")?;
|
||||
/// let mut buf = [0; 10];
|
||||
/// let (len, addr) = socket.peek_from(&mut buf).expect("peek failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_peek", issue = "76923")]
|
||||
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
||||
self.recv_from_flags(buf, libc::MSG_PEEK)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl AsRawFd for UnixDatagram {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
*self.0.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl FromRawFd for UnixDatagram {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
|
||||
UnixDatagram(Socket::from_inner(fd))
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl IntoRawFd for UnixDatagram {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.0.into_inner()
|
||||
}
|
||||
}
|
323
library/std/src/sys/unix/ext/net/listener.rs
Normal file
323
library/std/src/sys/unix/ext/net/listener.rs
Normal file
|
@ -0,0 +1,323 @@
|
|||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
use crate::path::Path;
|
||||
use crate::sys::net::Socket;
|
||||
use crate::sys::unix::cvt;
|
||||
use crate::sys::unix::ext::net::addr::{sockaddr_un, SocketAddr};
|
||||
use crate::sys::unix::ext::net::stream::UnixStream;
|
||||
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
||||
use crate::{fmt, io, mem};
|
||||
|
||||
/// A structure representing a Unix domain socket server.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::thread;
|
||||
/// use std::os::unix::net::{UnixStream, UnixListener};
|
||||
///
|
||||
/// fn handle_client(stream: UnixStream) {
|
||||
/// // ...
|
||||
/// }
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let listener = UnixListener::bind("/path/to/the/socket")?;
|
||||
///
|
||||
/// // accept connections and process them, spawning a new thread for each one
|
||||
/// for stream in listener.incoming() {
|
||||
/// match stream {
|
||||
/// Ok(stream) => {
|
||||
/// /* connection succeeded */
|
||||
/// thread::spawn(|| handle_client(stream));
|
||||
/// }
|
||||
/// Err(err) => {
|
||||
/// /* connection failed */
|
||||
/// break;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub struct UnixListener(Socket);
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl fmt::Debug for UnixListener {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut builder = fmt.debug_struct("UnixListener");
|
||||
builder.field("fd", self.0.as_inner());
|
||||
if let Ok(addr) = self.local_addr() {
|
||||
builder.field("local", &addr);
|
||||
}
|
||||
builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl UnixListener {
|
||||
/// Creates a new `UnixListener` bound to the specified socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
///
|
||||
/// let listener = match UnixListener::bind("/path/to/the/socket") {
|
||||
/// Ok(sock) => sock,
|
||||
/// Err(e) => {
|
||||
/// println!("Couldn't connect: {:?}", e);
|
||||
/// return
|
||||
/// }
|
||||
/// };
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
|
||||
fn inner(path: &Path) -> io::Result<UnixListener> {
|
||||
unsafe {
|
||||
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
|
||||
let (addr, len) = sockaddr_un(path)?;
|
||||
|
||||
cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len as _))?;
|
||||
cvt(libc::listen(*inner.as_inner(), 128))?;
|
||||
|
||||
Ok(UnixListener(inner))
|
||||
}
|
||||
}
|
||||
inner(path.as_ref())
|
||||
}
|
||||
|
||||
/// Accepts a new incoming connection to this listener.
|
||||
///
|
||||
/// This function will block the calling thread until a new Unix connection
|
||||
/// is established. When established, the corresponding [`UnixStream`] and
|
||||
/// the remote peer's address will be returned.
|
||||
///
|
||||
/// [`UnixStream`]: crate::os::unix::net::UnixStream
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let listener = UnixListener::bind("/path/to/the/socket")?;
|
||||
///
|
||||
/// match listener.accept() {
|
||||
/// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
|
||||
/// Err(e) => println!("accept function failed: {:?}", e),
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
|
||||
let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
|
||||
let mut len = mem::size_of_val(&storage) as libc::socklen_t;
|
||||
let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
|
||||
let addr = SocketAddr::from_parts(storage, len)?;
|
||||
Ok((UnixStream(sock), addr))
|
||||
}
|
||||
|
||||
/// Creates a new independently owned handle to the underlying socket.
|
||||
///
|
||||
/// The returned `UnixListener` is a reference to the same socket that this
|
||||
/// object references. Both handles can be used to accept incoming
|
||||
/// connections and options set on one listener will affect the other.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let listener = UnixListener::bind("/path/to/the/socket")?;
|
||||
/// let listener_copy = listener.try_clone().expect("try_clone failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn try_clone(&self) -> io::Result<UnixListener> {
|
||||
self.0.duplicate().map(UnixListener)
|
||||
}
|
||||
|
||||
/// Returns the local socket address of this listener.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let listener = UnixListener::bind("/path/to/the/socket")?;
|
||||
/// let addr = listener.local_addr().expect("Couldn't get local address");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||
SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
|
||||
}
|
||||
|
||||
/// Moves the socket into or out of nonblocking mode.
|
||||
///
|
||||
/// This will result in the `accept` operation becoming nonblocking,
|
||||
/// i.e., immediately returning from their calls. If the IO operation is
|
||||
/// successful, `Ok` is returned and no further action is required. If the
|
||||
/// IO operation could not be completed and needs to be retried, an error
|
||||
/// with kind [`io::ErrorKind::WouldBlock`] is returned.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let listener = UnixListener::bind("/path/to/the/socket")?;
|
||||
/// listener.set_nonblocking(true).expect("Couldn't set non blocking");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
||||
self.0.set_nonblocking(nonblocking)
|
||||
}
|
||||
|
||||
/// Returns the value of the `SO_ERROR` option.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixListener;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let listener = UnixListener::bind("/tmp/sock")?;
|
||||
///
|
||||
/// if let Ok(Some(err)) = listener.take_error() {
|
||||
/// println!("Got error: {:?}", err);
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Platform specific
|
||||
/// On Redox this always returns `None`.
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
||||
self.0.take_error()
|
||||
}
|
||||
|
||||
/// Returns an iterator over incoming connections.
|
||||
///
|
||||
/// The iterator will never return [`None`] and will also not yield the
|
||||
/// peer's [`SocketAddr`] structure.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::thread;
|
||||
/// use std::os::unix::net::{UnixStream, UnixListener};
|
||||
///
|
||||
/// fn handle_client(stream: UnixStream) {
|
||||
/// // ...
|
||||
/// }
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let listener = UnixListener::bind("/path/to/the/socket")?;
|
||||
///
|
||||
/// for stream in listener.incoming() {
|
||||
/// match stream {
|
||||
/// Ok(stream) => {
|
||||
/// thread::spawn(|| handle_client(stream));
|
||||
/// }
|
||||
/// Err(err) => {
|
||||
/// break;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn incoming(&self) -> Incoming<'_> {
|
||||
Incoming { listener: self }
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl AsRawFd for UnixListener {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
*self.0.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl FromRawFd for UnixListener {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
|
||||
UnixListener(Socket::from_inner(fd))
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl IntoRawFd for UnixListener {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.0.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl<'a> IntoIterator for &'a UnixListener {
|
||||
type Item = io::Result<UnixStream>;
|
||||
type IntoIter = Incoming<'a>;
|
||||
|
||||
fn into_iter(self) -> Incoming<'a> {
|
||||
self.incoming()
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over incoming connections to a [`UnixListener`].
|
||||
///
|
||||
/// It will never return [`None`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::thread;
|
||||
/// use std::os::unix::net::{UnixStream, UnixListener};
|
||||
///
|
||||
/// fn handle_client(stream: UnixStream) {
|
||||
/// // ...
|
||||
/// }
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let listener = UnixListener::bind("/path/to/the/socket")?;
|
||||
///
|
||||
/// for stream in listener.incoming() {
|
||||
/// match stream {
|
||||
/// Ok(stream) => {
|
||||
/// thread::spawn(|| handle_client(stream));
|
||||
/// }
|
||||
/// Err(err) => {
|
||||
/// break;
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub struct Incoming<'a> {
|
||||
listener: &'a UnixListener,
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl<'a> Iterator for Incoming<'a> {
|
||||
type Item = io::Result<UnixStream>;
|
||||
|
||||
fn next(&mut self) -> Option<io::Result<UnixStream>> {
|
||||
Some(self.listener.accept().map(|s| s.0))
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(usize::MAX, None)
|
||||
}
|
||||
}
|
25
library/std/src/sys/unix/ext/net/mod.rs
Normal file
25
library/std/src/sys/unix/ext/net/mod.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
//! Unix-specific networking functionality
|
||||
|
||||
#![stable(feature = "unix_socket", since = "1.10.0")]
|
||||
|
||||
mod addr;
|
||||
mod ancillary;
|
||||
mod datagram;
|
||||
mod listener;
|
||||
mod raw_fd;
|
||||
mod stream;
|
||||
#[cfg(all(test, not(target_os = "emscripten")))]
|
||||
mod test;
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub use self::addr::*;
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub use self::ancillary::*;
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub use self::datagram::*;
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub use self::listener::*;
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub use self::raw_fd::*;
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub use self::stream::*;
|
67
library/std/src/sys/unix/ext/net/raw_fd.rs
Normal file
67
library/std/src/sys/unix/ext/net/raw_fd.rs
Normal file
|
@ -0,0 +1,67 @@
|
|||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
use crate::sys_common::{self, AsInner, FromInner, IntoInner};
|
||||
use crate::{net, sys};
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl AsRawFd for net::TcpStream {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
*self.as_inner().socket().as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl AsRawFd for net::TcpListener {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
*self.as_inner().socket().as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl AsRawFd for net::UdpSocket {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
*self.as_inner().socket().as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "from_raw_os", since = "1.1.0")]
|
||||
impl FromRawFd for net::TcpStream {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
|
||||
let socket = sys::net::Socket::from_inner(fd);
|
||||
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "from_raw_os", since = "1.1.0")]
|
||||
impl FromRawFd for net::TcpListener {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
|
||||
let socket = sys::net::Socket::from_inner(fd);
|
||||
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "from_raw_os", since = "1.1.0")]
|
||||
impl FromRawFd for net::UdpSocket {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
|
||||
let socket = sys::net::Socket::from_inner(fd);
|
||||
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
||||
impl IntoRawFd for net::TcpStream {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.into_inner().into_socket().into_inner()
|
||||
}
|
||||
}
|
||||
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
||||
impl IntoRawFd for net::TcpListener {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.into_inner().into_socket().into_inner()
|
||||
}
|
||||
}
|
||||
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
||||
impl IntoRawFd for net::UdpSocket {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.into_inner().into_socket().into_inner()
|
||||
}
|
||||
}
|
615
library/std/src/sys/unix/ext/net/stream.rs
Normal file
615
library/std/src/sys/unix/ext/net/stream.rs
Normal file
|
@ -0,0 +1,615 @@
|
|||
use crate::fmt;
|
||||
use crate::io::{self, Initializer, IoSlice, IoSliceMut};
|
||||
use crate::net::Shutdown;
|
||||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
#[cfg(any(
|
||||
target_os = "android",
|
||||
target_os = "linux",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "ios",
|
||||
target_os = "macos",
|
||||
target_os = "openbsd"
|
||||
))]
|
||||
use crate::os::unix::ucred;
|
||||
use crate::path::Path;
|
||||
use crate::sys::cvt;
|
||||
use crate::sys::net::Socket;
|
||||
use crate::sys::unix::ext::net::addr::{sockaddr_un, SocketAddr};
|
||||
use crate::sys::unix::ext::net::ancillary::{
|
||||
recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary,
|
||||
};
|
||||
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
||||
use crate::time::Duration;
|
||||
|
||||
#[unstable(feature = "peer_credentials_unix_socket", issue = "42839", reason = "unstable")]
|
||||
#[cfg(any(
|
||||
target_os = "android",
|
||||
target_os = "linux",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "ios",
|
||||
target_os = "macos",
|
||||
target_os = "openbsd"
|
||||
))]
|
||||
pub use ucred::UCred;
|
||||
|
||||
/// A Unix stream socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
/// use std::io::prelude::*;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let mut stream = UnixStream::connect("/path/to/my/socket")?;
|
||||
/// stream.write_all(b"hello world")?;
|
||||
/// let mut response = String::new();
|
||||
/// stream.read_to_string(&mut response)?;
|
||||
/// println!("{}", response);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub struct UnixStream(pub(super) Socket);
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl fmt::Debug for UnixStream {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut builder = fmt.debug_struct("UnixStream");
|
||||
builder.field("fd", self.0.as_inner());
|
||||
if let Ok(addr) = self.local_addr() {
|
||||
builder.field("local", &addr);
|
||||
}
|
||||
if let Ok(addr) = self.peer_addr() {
|
||||
builder.field("peer", &addr);
|
||||
}
|
||||
builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl UnixStream {
|
||||
/// Connects to the socket named by `path`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// let socket = match UnixStream::connect("/tmp/sock") {
|
||||
/// Ok(sock) => sock,
|
||||
/// Err(e) => {
|
||||
/// println!("Couldn't connect: {:?}", e);
|
||||
/// return
|
||||
/// }
|
||||
/// };
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
|
||||
fn inner(path: &Path) -> io::Result<UnixStream> {
|
||||
unsafe {
|
||||
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
|
||||
let (addr, len) = sockaddr_un(path)?;
|
||||
|
||||
cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
|
||||
Ok(UnixStream(inner))
|
||||
}
|
||||
}
|
||||
inner(path.as_ref())
|
||||
}
|
||||
|
||||
/// Creates an unnamed pair of connected sockets.
|
||||
///
|
||||
/// Returns two `UnixStream`s which are connected to each other.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// let (sock1, sock2) = match UnixStream::pair() {
|
||||
/// Ok((sock1, sock2)) => (sock1, sock2),
|
||||
/// Err(e) => {
|
||||
/// println!("Couldn't create a pair of sockets: {:?}", e);
|
||||
/// return
|
||||
/// }
|
||||
/// };
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
|
||||
let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
|
||||
Ok((UnixStream(i1), UnixStream(i2)))
|
||||
}
|
||||
|
||||
/// Creates a new independently owned handle to the underlying socket.
|
||||
///
|
||||
/// The returned `UnixStream` is a reference to the same stream that this
|
||||
/// object references. Both handles will read and write the same stream of
|
||||
/// data, and options set on one stream will be propagated to the other
|
||||
/// stream.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn try_clone(&self) -> io::Result<UnixStream> {
|
||||
self.0.duplicate().map(UnixStream)
|
||||
}
|
||||
|
||||
/// Returns the socket address of the local half of this connection.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// let addr = socket.local_addr().expect("Couldn't get local address");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn local_addr(&self) -> io::Result<SocketAddr> {
|
||||
SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
|
||||
}
|
||||
|
||||
/// Returns the socket address of the remote half of this connection.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// let addr = socket.peer_addr().expect("Couldn't get peer address");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
|
||||
SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
|
||||
}
|
||||
|
||||
/// Gets the peer credentials for this Unix domain socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(peer_credentials_unix_socket)]
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// let peer_cred = socket.peer_cred().expect("Couldn't get peer credentials");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "peer_credentials_unix_socket", issue = "42839", reason = "unstable")]
|
||||
#[cfg(any(
|
||||
target_os = "android",
|
||||
target_os = "linux",
|
||||
target_os = "dragonfly",
|
||||
target_os = "freebsd",
|
||||
target_os = "ios",
|
||||
target_os = "macos",
|
||||
target_os = "openbsd"
|
||||
))]
|
||||
pub fn peer_cred(&self) -> io::Result<UCred> {
|
||||
ucred::peer_cred(self)
|
||||
}
|
||||
|
||||
/// Sets the read timeout for the socket.
|
||||
///
|
||||
/// If the provided value is [`None`], then [`read`] calls will block
|
||||
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
|
||||
/// method.
|
||||
///
|
||||
/// [`read`]: io::Read::read
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
|
||||
/// method:
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::io;
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
|
||||
/// let err = result.unwrap_err();
|
||||
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
|
||||
self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
|
||||
}
|
||||
|
||||
/// Sets the write timeout for the socket.
|
||||
///
|
||||
/// If the provided value is [`None`], then [`write`] calls will block
|
||||
/// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
|
||||
/// passed to this method.
|
||||
///
|
||||
/// [`read`]: io::Read::read
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// socket.set_write_timeout(Some(Duration::new(1, 0)))
|
||||
/// .expect("Couldn't set write timeout");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// An [`Err`] is returned if the zero [`Duration`] is passed to this
|
||||
/// method:
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::io;
|
||||
/// use std::net::UdpSocket;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UdpSocket::bind("127.0.0.1:34254")?;
|
||||
/// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
|
||||
/// let err = result.unwrap_err();
|
||||
/// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
|
||||
self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
|
||||
}
|
||||
|
||||
/// Returns the read timeout of this socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
|
||||
/// assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
|
||||
self.0.timeout(libc::SO_RCVTIMEO)
|
||||
}
|
||||
|
||||
/// Returns the write timeout of this socket.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
/// use std::time::Duration;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// socket.set_write_timeout(Some(Duration::new(1, 0)))
|
||||
/// .expect("Couldn't set write timeout");
|
||||
/// assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
|
||||
self.0.timeout(libc::SO_SNDTIMEO)
|
||||
}
|
||||
|
||||
/// Moves the socket into or out of nonblocking mode.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
||||
self.0.set_nonblocking(nonblocking)
|
||||
}
|
||||
|
||||
/// Returns the value of the `SO_ERROR` option.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// if let Ok(Some(err)) = socket.take_error() {
|
||||
/// println!("Got error: {:?}", err);
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Platform specific
|
||||
/// On Redox this always returns `None`.
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
||||
self.0.take_error()
|
||||
}
|
||||
|
||||
/// Shuts down the read, write, or both halves of this connection.
|
||||
///
|
||||
/// This function will cause all pending and future I/O calls on the
|
||||
/// specified portions to immediately return with an appropriate value
|
||||
/// (see the documentation of [`Shutdown`]).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
/// use std::net::Shutdown;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
|
||||
self.0.shutdown(how)
|
||||
}
|
||||
|
||||
/// Receives data on the socket from the remote address to which it is
|
||||
/// connected, without removing that data from the queue. On success,
|
||||
/// returns the number of bytes peeked.
|
||||
///
|
||||
/// Successive calls return the same data. This is accomplished by passing
|
||||
/// `MSG_PEEK` as a flag to the underlying `recv` system call.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_peek)]
|
||||
///
|
||||
/// use std::os::unix::net::UnixStream;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// let mut buf = [0; 10];
|
||||
/// let len = socket.peek(&mut buf).expect("peek failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_peek", issue = "76923")]
|
||||
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.0.peek(buf)
|
||||
}
|
||||
|
||||
/// Receives data and ancillary data from socket.
|
||||
///
|
||||
/// On success, returns the number of bytes read.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// let mut buf1 = [1; 8];
|
||||
/// let mut buf2 = [2; 16];
|
||||
/// let mut buf3 = [3; 8];
|
||||
/// let mut bufs = &mut [
|
||||
/// IoSliceMut::new(&mut buf1),
|
||||
/// IoSliceMut::new(&mut buf2),
|
||||
/// IoSliceMut::new(&mut buf3),
|
||||
/// ][..];
|
||||
/// let mut fds = [0; 8];
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
/// let size = socket.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
|
||||
/// println!("received {}", size);
|
||||
/// for ancillary_data in ancillary.messages() {
|
||||
/// if let AncillaryData::ScmRights(scm_rights) = ancillary_data {
|
||||
/// for fd in scm_rights {
|
||||
/// println!("receive file descriptor: {}", fd);
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn recv_vectored_with_ancillary(
|
||||
&self,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
ancillary: &mut SocketAncillary<'_>,
|
||||
) -> io::Result<usize> {
|
||||
let (count, _, _) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
/// Sends data and ancillary data on the socket.
|
||||
///
|
||||
/// On success, returns the number of bytes written.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// #![feature(unix_socket_ancillary_data)]
|
||||
/// use std::os::unix::net::{UnixStream, SocketAncillary};
|
||||
/// use std::io::IoSliceMut;
|
||||
///
|
||||
/// fn main() -> std::io::Result<()> {
|
||||
/// let socket = UnixStream::connect("/tmp/sock")?;
|
||||
/// let mut buf1 = [1; 8];
|
||||
/// let mut buf2 = [2; 16];
|
||||
/// let mut buf3 = [3; 8];
|
||||
/// let mut bufs = &mut [
|
||||
/// IoSliceMut::new(&mut buf1),
|
||||
/// IoSliceMut::new(&mut buf2),
|
||||
/// IoSliceMut::new(&mut buf3),
|
||||
/// ][..];
|
||||
/// let fds = [0, 1, 2];
|
||||
/// let mut ancillary_buffer = [0; 128];
|
||||
/// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
|
||||
/// ancillary.add_fds(&fds[..]);
|
||||
/// socket.send_vectored_with_ancillary(bufs, &mut ancillary).expect("send_vectored_with_ancillary function failed");
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
|
||||
pub fn send_vectored_with_ancillary(
|
||||
&self,
|
||||
bufs: &mut [IoSliceMut<'_>],
|
||||
ancillary: &mut SocketAncillary<'_>,
|
||||
) -> io::Result<usize> {
|
||||
send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl io::Read for UnixStream {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
io::Read::read(&mut &*self, buf)
|
||||
}
|
||||
|
||||
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
||||
io::Read::read_vectored(&mut &*self, bufs)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_read_vectored(&self) -> bool {
|
||||
io::Read::is_read_vectored(&&*self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn initializer(&self) -> Initializer {
|
||||
Initializer::nop()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl<'a> io::Read for &'a UnixStream {
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.0.read(buf)
|
||||
}
|
||||
|
||||
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
||||
self.0.read_vectored(bufs)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_read_vectored(&self) -> bool {
|
||||
self.0.is_read_vectored()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn initializer(&self) -> Initializer {
|
||||
Initializer::nop()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl io::Write for UnixStream {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
io::Write::write(&mut &*self, buf)
|
||||
}
|
||||
|
||||
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
||||
io::Write::write_vectored(&mut &*self, bufs)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_write_vectored(&self) -> bool {
|
||||
io::Write::is_write_vectored(&&*self)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
io::Write::flush(&mut &*self)
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl<'a> io::Write for &'a UnixStream {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.0.write(buf)
|
||||
}
|
||||
|
||||
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
||||
self.0.write_vectored(bufs)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_write_vectored(&self) -> bool {
|
||||
self.0.is_write_vectored()
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl AsRawFd for UnixStream {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
*self.0.as_inner()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl FromRawFd for UnixStream {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
|
||||
UnixStream(Socket::from_inner(fd))
|
||||
}
|
||||
}
|
||||
|
||||
#[stable(feature = "unix_socket", since = "1.10.0")]
|
||||
impl IntoRawFd for UnixStream {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.0.into_inner()
|
||||
}
|
||||
}
|
608
library/std/src/sys/unix/ext/net/test.rs
Normal file
608
library/std/src/sys/unix/ext/net/test.rs
Normal file
|
@ -0,0 +1,608 @@
|
|||
use super::*;
|
||||
use crate::io::prelude::*;
|
||||
use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
|
||||
use crate::iter::FromIterator;
|
||||
use crate::mem;
|
||||
use crate::sys::unix::ext::io::AsRawFd;
|
||||
use crate::sys_common::io::test::tmpdir;
|
||||
use crate::thread;
|
||||
use crate::time::Duration;
|
||||
|
||||
macro_rules! or_panic {
|
||||
($e:expr) => {
|
||||
match $e {
|
||||
Ok(e) => e,
|
||||
Err(e) => panic!("{}", e),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let dir = tmpdir();
|
||||
let socket_path = dir.path().join("sock");
|
||||
let msg1 = b"hello";
|
||||
let msg2 = b"world!";
|
||||
|
||||
let listener = or_panic!(UnixListener::bind(&socket_path));
|
||||
let thread = thread::spawn(move || {
|
||||
let mut stream = or_panic!(listener.accept()).0;
|
||||
let mut buf = [0; 5];
|
||||
or_panic!(stream.read(&mut buf));
|
||||
assert_eq!(&msg1[..], &buf[..]);
|
||||
or_panic!(stream.write_all(msg2));
|
||||
});
|
||||
|
||||
let mut stream = or_panic!(UnixStream::connect(&socket_path));
|
||||
assert_eq!(Some(&*socket_path), stream.peer_addr().unwrap().as_pathname());
|
||||
or_panic!(stream.write_all(msg1));
|
||||
let mut buf = vec![];
|
||||
or_panic!(stream.read_to_end(&mut buf));
|
||||
assert_eq!(&msg2[..], &buf[..]);
|
||||
drop(stream);
|
||||
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vectored() {
|
||||
let (mut s1, mut s2) = or_panic!(UnixStream::pair());
|
||||
|
||||
let len = or_panic!(s1.write_vectored(&[
|
||||
IoSlice::new(b"hello"),
|
||||
IoSlice::new(b" "),
|
||||
IoSlice::new(b"world!")
|
||||
],));
|
||||
assert_eq!(len, 12);
|
||||
|
||||
let mut buf1 = [0; 6];
|
||||
let mut buf2 = [0; 7];
|
||||
let len =
|
||||
or_panic!(s2.read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)],));
|
||||
assert_eq!(len, 12);
|
||||
assert_eq!(&buf1, b"hello ");
|
||||
assert_eq!(&buf2, b"world!\0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pair() {
|
||||
let msg1 = b"hello";
|
||||
let msg2 = b"world!";
|
||||
|
||||
let (mut s1, mut s2) = or_panic!(UnixStream::pair());
|
||||
let thread = thread::spawn(move || {
|
||||
// s1 must be moved in or the test will hang!
|
||||
let mut buf = [0; 5];
|
||||
or_panic!(s1.read(&mut buf));
|
||||
assert_eq!(&msg1[..], &buf[..]);
|
||||
or_panic!(s1.write_all(msg2));
|
||||
});
|
||||
|
||||
or_panic!(s2.write_all(msg1));
|
||||
let mut buf = vec![];
|
||||
or_panic!(s2.read_to_end(&mut buf));
|
||||
assert_eq!(&msg2[..], &buf[..]);
|
||||
drop(s2);
|
||||
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_clone() {
|
||||
let dir = tmpdir();
|
||||
let socket_path = dir.path().join("sock");
|
||||
let msg1 = b"hello";
|
||||
let msg2 = b"world";
|
||||
|
||||
let listener = or_panic!(UnixListener::bind(&socket_path));
|
||||
let thread = thread::spawn(move || {
|
||||
let mut stream = or_panic!(listener.accept()).0;
|
||||
or_panic!(stream.write_all(msg1));
|
||||
or_panic!(stream.write_all(msg2));
|
||||
});
|
||||
|
||||
let mut stream = or_panic!(UnixStream::connect(&socket_path));
|
||||
let mut stream2 = or_panic!(stream.try_clone());
|
||||
|
||||
let mut buf = [0; 5];
|
||||
or_panic!(stream.read(&mut buf));
|
||||
assert_eq!(&msg1[..], &buf[..]);
|
||||
or_panic!(stream2.read(&mut buf));
|
||||
assert_eq!(&msg2[..], &buf[..]);
|
||||
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iter() {
|
||||
let dir = tmpdir();
|
||||
let socket_path = dir.path().join("sock");
|
||||
|
||||
let listener = or_panic!(UnixListener::bind(&socket_path));
|
||||
let thread = thread::spawn(move || {
|
||||
for stream in listener.incoming().take(2) {
|
||||
let mut stream = or_panic!(stream);
|
||||
let mut buf = [0];
|
||||
or_panic!(stream.read(&mut buf));
|
||||
}
|
||||
});
|
||||
|
||||
for _ in 0..2 {
|
||||
let mut stream = or_panic!(UnixStream::connect(&socket_path));
|
||||
or_panic!(stream.write_all(&[0]));
|
||||
}
|
||||
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_path() {
|
||||
let dir = tmpdir();
|
||||
let socket_path = dir.path().join(
|
||||
"asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa\
|
||||
sasdfasdfasdasdfasdfasdfadfasdfasdfasdfasdfasdf",
|
||||
);
|
||||
match UnixStream::connect(&socket_path) {
|
||||
Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
|
||||
Err(e) => panic!("unexpected error {}", e),
|
||||
Ok(_) => panic!("unexpected success"),
|
||||
}
|
||||
|
||||
match UnixListener::bind(&socket_path) {
|
||||
Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
|
||||
Err(e) => panic!("unexpected error {}", e),
|
||||
Ok(_) => panic!("unexpected success"),
|
||||
}
|
||||
|
||||
match UnixDatagram::bind(&socket_path) {
|
||||
Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
|
||||
Err(e) => panic!("unexpected error {}", e),
|
||||
Ok(_) => panic!("unexpected success"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timeouts() {
|
||||
let dir = tmpdir();
|
||||
let socket_path = dir.path().join("sock");
|
||||
|
||||
let _listener = or_panic!(UnixListener::bind(&socket_path));
|
||||
|
||||
let stream = or_panic!(UnixStream::connect(&socket_path));
|
||||
let dur = Duration::new(15410, 0);
|
||||
|
||||
assert_eq!(None, or_panic!(stream.read_timeout()));
|
||||
|
||||
or_panic!(stream.set_read_timeout(Some(dur)));
|
||||
assert_eq!(Some(dur), or_panic!(stream.read_timeout()));
|
||||
|
||||
assert_eq!(None, or_panic!(stream.write_timeout()));
|
||||
|
||||
or_panic!(stream.set_write_timeout(Some(dur)));
|
||||
assert_eq!(Some(dur), or_panic!(stream.write_timeout()));
|
||||
|
||||
or_panic!(stream.set_read_timeout(None));
|
||||
assert_eq!(None, or_panic!(stream.read_timeout()));
|
||||
|
||||
or_panic!(stream.set_write_timeout(None));
|
||||
assert_eq!(None, or_panic!(stream.write_timeout()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_timeout() {
|
||||
let dir = tmpdir();
|
||||
let socket_path = dir.path().join("sock");
|
||||
|
||||
let _listener = or_panic!(UnixListener::bind(&socket_path));
|
||||
|
||||
let mut stream = or_panic!(UnixStream::connect(&socket_path));
|
||||
or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
|
||||
|
||||
let mut buf = [0; 10];
|
||||
let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
|
||||
assert!(
|
||||
kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
|
||||
"unexpected_error: {:?}",
|
||||
kind
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_with_timeout() {
|
||||
let dir = tmpdir();
|
||||
let socket_path = dir.path().join("sock");
|
||||
|
||||
let listener = or_panic!(UnixListener::bind(&socket_path));
|
||||
|
||||
let mut stream = or_panic!(UnixStream::connect(&socket_path));
|
||||
or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
|
||||
|
||||
let mut other_end = or_panic!(listener.accept()).0;
|
||||
or_panic!(other_end.write_all(b"hello world"));
|
||||
|
||||
let mut buf = [0; 11];
|
||||
or_panic!(stream.read(&mut buf));
|
||||
assert_eq!(b"hello world", &buf[..]);
|
||||
|
||||
let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
|
||||
assert!(
|
||||
kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
|
||||
"unexpected_error: {:?}",
|
||||
kind
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
|
||||
// when passed zero Durations
|
||||
#[test]
|
||||
fn test_unix_stream_timeout_zero_duration() {
|
||||
let dir = tmpdir();
|
||||
let socket_path = dir.path().join("sock");
|
||||
|
||||
let listener = or_panic!(UnixListener::bind(&socket_path));
|
||||
let stream = or_panic!(UnixStream::connect(&socket_path));
|
||||
|
||||
let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
|
||||
let err = result.unwrap_err();
|
||||
assert_eq!(err.kind(), ErrorKind::InvalidInput);
|
||||
|
||||
let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
|
||||
let err = result.unwrap_err();
|
||||
assert_eq!(err.kind(), ErrorKind::InvalidInput);
|
||||
|
||||
drop(listener);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unix_datagram() {
|
||||
let dir = tmpdir();
|
||||
let path1 = dir.path().join("sock1");
|
||||
let path2 = dir.path().join("sock2");
|
||||
|
||||
let sock1 = or_panic!(UnixDatagram::bind(&path1));
|
||||
let sock2 = or_panic!(UnixDatagram::bind(&path2));
|
||||
|
||||
let msg = b"hello world";
|
||||
or_panic!(sock1.send_to(msg, &path2));
|
||||
let mut buf = [0; 11];
|
||||
or_panic!(sock2.recv_from(&mut buf));
|
||||
assert_eq!(msg, &buf[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unnamed_unix_datagram() {
|
||||
let dir = tmpdir();
|
||||
let path1 = dir.path().join("sock1");
|
||||
|
||||
let sock1 = or_panic!(UnixDatagram::bind(&path1));
|
||||
let sock2 = or_panic!(UnixDatagram::unbound());
|
||||
|
||||
let msg = b"hello world";
|
||||
or_panic!(sock2.send_to(msg, &path1));
|
||||
let mut buf = [0; 11];
|
||||
let (usize, addr) = or_panic!(sock1.recv_from(&mut buf));
|
||||
assert_eq!(usize, 11);
|
||||
assert!(addr.is_unnamed());
|
||||
assert_eq!(msg, &buf[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_connect_unix_datagram() {
|
||||
let dir = tmpdir();
|
||||
let path1 = dir.path().join("sock1");
|
||||
let path2 = dir.path().join("sock2");
|
||||
|
||||
let bsock1 = or_panic!(UnixDatagram::bind(&path1));
|
||||
let bsock2 = or_panic!(UnixDatagram::bind(&path2));
|
||||
let sock = or_panic!(UnixDatagram::unbound());
|
||||
or_panic!(sock.connect(&path1));
|
||||
|
||||
// Check send()
|
||||
let msg = b"hello there";
|
||||
or_panic!(sock.send(msg));
|
||||
let mut buf = [0; 11];
|
||||
let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
|
||||
assert_eq!(usize, 11);
|
||||
assert!(addr.is_unnamed());
|
||||
assert_eq!(msg, &buf[..]);
|
||||
|
||||
// Changing default socket works too
|
||||
or_panic!(sock.connect(&path2));
|
||||
or_panic!(sock.send(msg));
|
||||
or_panic!(bsock2.recv_from(&mut buf));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unix_datagram_recv() {
|
||||
let dir = tmpdir();
|
||||
let path1 = dir.path().join("sock1");
|
||||
|
||||
let sock1 = or_panic!(UnixDatagram::bind(&path1));
|
||||
let sock2 = or_panic!(UnixDatagram::unbound());
|
||||
or_panic!(sock2.connect(&path1));
|
||||
|
||||
let msg = b"hello world";
|
||||
or_panic!(sock2.send(msg));
|
||||
let mut buf = [0; 11];
|
||||
let size = or_panic!(sock1.recv(&mut buf));
|
||||
assert_eq!(size, 11);
|
||||
assert_eq!(msg, &buf[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn datagram_pair() {
|
||||
let msg1 = b"hello";
|
||||
let msg2 = b"world!";
|
||||
|
||||
let (s1, s2) = or_panic!(UnixDatagram::pair());
|
||||
let thread = thread::spawn(move || {
|
||||
// s1 must be moved in or the test will hang!
|
||||
let mut buf = [0; 5];
|
||||
or_panic!(s1.recv(&mut buf));
|
||||
assert_eq!(&msg1[..], &buf[..]);
|
||||
or_panic!(s1.send(msg2));
|
||||
});
|
||||
|
||||
or_panic!(s2.send(msg1));
|
||||
let mut buf = [0; 6];
|
||||
or_panic!(s2.recv(&mut buf));
|
||||
assert_eq!(&msg2[..], &buf[..]);
|
||||
drop(s2);
|
||||
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
// Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
|
||||
// when passed zero Durations
|
||||
#[test]
|
||||
fn test_unix_datagram_timeout_zero_duration() {
|
||||
let dir = tmpdir();
|
||||
let path = dir.path().join("sock");
|
||||
|
||||
let datagram = or_panic!(UnixDatagram::bind(&path));
|
||||
|
||||
let result = datagram.set_write_timeout(Some(Duration::new(0, 0)));
|
||||
let err = result.unwrap_err();
|
||||
assert_eq!(err.kind(), ErrorKind::InvalidInput);
|
||||
|
||||
let result = datagram.set_read_timeout(Some(Duration::new(0, 0)));
|
||||
let err = result.unwrap_err();
|
||||
assert_eq!(err.kind(), ErrorKind::InvalidInput);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn abstract_namespace_not_allowed() {
|
||||
assert!(UnixStream::connect("\0asdf").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unix_stream_peek() {
|
||||
let (txdone, rxdone) = crate::sync::mpsc::channel();
|
||||
|
||||
let dir = tmpdir();
|
||||
let path = dir.path().join("sock");
|
||||
|
||||
let listener = or_panic!(UnixListener::bind(&path));
|
||||
let thread = thread::spawn(move || {
|
||||
let mut stream = or_panic!(listener.accept()).0;
|
||||
or_panic!(stream.write_all(&[1, 3, 3, 7]));
|
||||
or_panic!(rxdone.recv());
|
||||
});
|
||||
|
||||
let mut stream = or_panic!(UnixStream::connect(&path));
|
||||
let mut buf = [0; 10];
|
||||
for _ in 0..2 {
|
||||
assert_eq!(or_panic!(stream.peek(&mut buf)), 4);
|
||||
}
|
||||
assert_eq!(or_panic!(stream.read(&mut buf)), 4);
|
||||
|
||||
or_panic!(stream.set_nonblocking(true));
|
||||
match stream.peek(&mut buf) {
|
||||
Ok(_) => panic!("expected error"),
|
||||
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {}
|
||||
Err(e) => panic!("unexpected error: {}", e),
|
||||
}
|
||||
|
||||
or_panic!(txdone.send(()));
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unix_datagram_peek() {
|
||||
let dir = tmpdir();
|
||||
let path1 = dir.path().join("sock");
|
||||
|
||||
let sock1 = or_panic!(UnixDatagram::bind(&path1));
|
||||
let sock2 = or_panic!(UnixDatagram::unbound());
|
||||
or_panic!(sock2.connect(&path1));
|
||||
|
||||
let msg = b"hello world";
|
||||
or_panic!(sock2.send(msg));
|
||||
for _ in 0..2 {
|
||||
let mut buf = [0; 11];
|
||||
let size = or_panic!(sock1.peek(&mut buf));
|
||||
assert_eq!(size, 11);
|
||||
assert_eq!(msg, &buf[..]);
|
||||
}
|
||||
|
||||
let mut buf = [0; 11];
|
||||
let size = or_panic!(sock1.recv(&mut buf));
|
||||
assert_eq!(size, 11);
|
||||
assert_eq!(msg, &buf[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unix_datagram_peek_from() {
|
||||
let dir = tmpdir();
|
||||
let path1 = dir.path().join("sock");
|
||||
|
||||
let sock1 = or_panic!(UnixDatagram::bind(&path1));
|
||||
let sock2 = or_panic!(UnixDatagram::unbound());
|
||||
or_panic!(sock2.connect(&path1));
|
||||
|
||||
let msg = b"hello world";
|
||||
or_panic!(sock2.send(msg));
|
||||
for _ in 0..2 {
|
||||
let mut buf = [0; 11];
|
||||
let (size, _) = or_panic!(sock1.peek_from(&mut buf));
|
||||
assert_eq!(size, 11);
|
||||
assert_eq!(msg, &buf[..]);
|
||||
}
|
||||
|
||||
let mut buf = [0; 11];
|
||||
let size = or_panic!(sock1.recv(&mut buf));
|
||||
assert_eq!(size, 11);
|
||||
assert_eq!(msg, &buf[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send_vectored_fds_unix_stream() {
|
||||
let (s1, s2) = or_panic!(UnixStream::pair());
|
||||
|
||||
let mut buf1 = [1; 8];
|
||||
let mut bufs_send = &mut [IoSliceMut::new(&mut buf1[..])][..];
|
||||
|
||||
let mut ancillary1_buffer = [0; 128];
|
||||
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
|
||||
assert!(ancillary1.add_fds(&[s1.as_raw_fd()][..]));
|
||||
|
||||
let usize = or_panic!(s1.send_vectored_with_ancillary(&mut bufs_send, &mut ancillary1));
|
||||
assert_eq!(usize, 8);
|
||||
|
||||
let mut buf2 = [0; 8];
|
||||
let mut bufs_recv = &mut [IoSliceMut::new(&mut buf2[..])][..];
|
||||
|
||||
let mut ancillary2_buffer = [0; 128];
|
||||
let mut ancillary2 = SocketAncillary::new(&mut ancillary2_buffer[..]);
|
||||
|
||||
let usize = or_panic!(s2.recv_vectored_with_ancillary(&mut bufs_recv, &mut ancillary2));
|
||||
assert_eq!(usize, 8);
|
||||
assert_eq!(buf1, buf2);
|
||||
|
||||
let mut ancillary_data_vec = Vec::from_iter(ancillary2.messages());
|
||||
assert_eq!(ancillary_data_vec.len(), 1);
|
||||
if let AncillaryData::ScmRights(scm_rights) = ancillary_data_vec.pop().unwrap() {
|
||||
let fd_vec = Vec::from_iter(scm_rights);
|
||||
assert_eq!(fd_vec.len(), 1);
|
||||
unsafe {
|
||||
libc::close(fd_vec[0]);
|
||||
}
|
||||
} else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send_vectored_with_ancillary_to_unix_datagram() {
|
||||
fn getpid() -> libc::pid_t {
|
||||
unsafe { libc::getpid() }
|
||||
}
|
||||
|
||||
fn getuid() -> libc::uid_t {
|
||||
unsafe { libc::getuid() }
|
||||
}
|
||||
|
||||
fn getgid() -> libc::gid_t {
|
||||
unsafe { libc::getgid() }
|
||||
}
|
||||
|
||||
let dir = tmpdir();
|
||||
let path1 = dir.path().join("sock1");
|
||||
let path2 = dir.path().join("sock2");
|
||||
|
||||
let bsock1 = or_panic!(UnixDatagram::bind(&path1));
|
||||
let bsock2 = or_panic!(UnixDatagram::bind(&path2));
|
||||
|
||||
unsafe {
|
||||
let optval: libc::c_int = 1;
|
||||
libc::setsockopt(
|
||||
bsock2.as_raw_fd(),
|
||||
libc::SOL_SOCKET,
|
||||
libc::SO_PASSCRED,
|
||||
&optval as *const _ as *const _,
|
||||
mem::size_of::<libc::c_int>() as u32,
|
||||
);
|
||||
}
|
||||
|
||||
let mut buf1 = [1; 8];
|
||||
let mut bufs_send = &mut [IoSliceMut::new(&mut buf1[..])][..];
|
||||
|
||||
let mut ancillary1_buffer = [0; 128];
|
||||
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
|
||||
let cred1 = libc::ucred { pid: getpid(), uid: getuid(), gid: getgid() };
|
||||
assert!(ancillary1.add_creds(&[cred1][..]));
|
||||
|
||||
let usize =
|
||||
or_panic!(bsock1.send_vectored_with_ancillary_to(&mut bufs_send, &mut ancillary1, &path2));
|
||||
assert_eq!(usize, 8);
|
||||
|
||||
let mut buf2 = [0; 8];
|
||||
let mut bufs_recv = &mut [IoSliceMut::new(&mut buf2[..])][..];
|
||||
|
||||
let mut ancillary2_buffer = [0; 128];
|
||||
let mut ancillary2 = SocketAncillary::new(&mut ancillary2_buffer[..]);
|
||||
|
||||
let (usize, truncated, _addr) =
|
||||
or_panic!(bsock2.recv_vectored_with_ancillary_from(&mut bufs_recv, &mut ancillary2));
|
||||
assert_eq!(ancillary2.truncated(), false);
|
||||
assert_eq!(usize, 8);
|
||||
assert_eq!(truncated, false);
|
||||
assert_eq!(buf1, buf2);
|
||||
|
||||
let mut ancillary_data_vec = Vec::from_iter(ancillary2.messages());
|
||||
assert_eq!(ancillary_data_vec.len(), 1);
|
||||
if let AncillaryData::ScmCredentials(scm_credentials) = ancillary_data_vec.pop().unwrap() {
|
||||
let cred_vec = Vec::from_iter(scm_credentials);
|
||||
assert_eq!(cred_vec.len(), 1);
|
||||
assert_eq!(cred1.pid, cred_vec[0].pid);
|
||||
assert_eq!(cred1.uid, cred_vec[0].uid);
|
||||
assert_eq!(cred1.gid, cred_vec[0].gid);
|
||||
} else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_send_vectored_with_ancillary_unix_datagram() {
|
||||
let dir = tmpdir();
|
||||
let path1 = dir.path().join("sock1");
|
||||
let path2 = dir.path().join("sock2");
|
||||
|
||||
let bsock1 = or_panic!(UnixDatagram::bind(&path1));
|
||||
let bsock2 = or_panic!(UnixDatagram::bind(&path2));
|
||||
|
||||
let mut buf1 = [1; 8];
|
||||
let mut bufs_send = &mut [IoSliceMut::new(&mut buf1[..])][..];
|
||||
|
||||
let mut ancillary1_buffer = [0; 128];
|
||||
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
|
||||
assert!(ancillary1.add_fds(&[bsock1.as_raw_fd()][..]));
|
||||
|
||||
or_panic!(bsock1.connect(&path2));
|
||||
let usize = or_panic!(bsock1.send_vectored_with_ancillary(&mut bufs_send, &mut ancillary1));
|
||||
assert_eq!(usize, 8);
|
||||
|
||||
let mut buf2 = [0; 8];
|
||||
let mut bufs_recv = &mut [IoSliceMut::new(&mut buf2[..])][..];
|
||||
|
||||
let mut ancillary2_buffer = [0; 128];
|
||||
let mut ancillary2 = SocketAncillary::new(&mut ancillary2_buffer[..]);
|
||||
|
||||
let (usize, truncated) =
|
||||
or_panic!(bsock2.recv_vectored_with_ancillary(&mut bufs_recv, &mut ancillary2));
|
||||
assert_eq!(usize, 8);
|
||||
assert_eq!(truncated, false);
|
||||
assert_eq!(buf1, buf2);
|
||||
|
||||
let mut ancillary_data_vec = Vec::from_iter(ancillary2.messages());
|
||||
assert_eq!(ancillary_data_vec.len(), 1);
|
||||
if let AncillaryData::ScmRights(scm_rights) = ancillary_data_vec.pop().unwrap() {
|
||||
let fd_vec = Vec::from_iter(scm_rights);
|
||||
assert_eq!(fd_vec.len(), 1);
|
||||
unsafe {
|
||||
libc::close(fd_vec[0]);
|
||||
}
|
||||
} else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue