1 #![stable(feature = "unix_socket", since = "1.10.0")]
3 //! Unix-specific networking functionality
5 // FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
7 #[allow(non_camel_case_types)]
10 pub type socklen_t = u32;
13 pub struct sockaddr_un;
17 use crate::ffi::OsStr;
19 use crate::io::{self, Initializer, IoSlice, IoSliceMut};
21 use crate::net::{self, Shutdown};
22 use crate::os::unix::ffi::OsStrExt;
23 use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
24 use crate::path::Path;
25 use crate::sys::net::Socket;
26 use crate::sys::{self, cvt};
27 use crate::sys_common::{self, AsInner, FromInner, IntoInner};
28 use crate::time::Duration;
32 target_os = "android",
33 target_os = "dragonfly",
34 target_os = "freebsd",
35 target_os = "openbsd",
39 use libc::MSG_NOSIGNAL;
42 target_os = "android",
43 target_os = "dragonfly",
44 target_os = "freebsd",
45 target_os = "openbsd",
49 const MSG_NOSIGNAL: libc::c_int = 0x0;
51 fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
52 // Work with an actual instance of the type since using a null pointer is UB
53 let base = addr as *const _ as usize;
54 let path = &addr.sun_path as *const _ as usize;
58 unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
59 let mut addr: libc::sockaddr_un = mem::zeroed();
60 addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
62 let bytes = path.as_os_str().as_bytes();
64 if bytes.contains(&0) {
65 return Err(io::Error::new(
66 io::ErrorKind::InvalidInput,
67 "paths may not contain interior null bytes",
71 if bytes.len() >= addr.sun_path.len() {
72 return Err(io::Error::new(
73 io::ErrorKind::InvalidInput,
74 "path must be shorter than SUN_LEN",
77 for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
78 *dst = *src as libc::c_char;
80 // null byte for pathname addresses is already there because we zeroed the
83 let mut len = sun_path_offset(&addr) + bytes.len();
88 Ok((addr, len as libc::socklen_t))
91 enum AddressKind<'a> {
97 /// An address associated with a Unix socket.
102 /// use std::os::unix::net::UnixListener;
104 /// let socket = match UnixListener::bind("/tmp/sock") {
105 /// Ok(sock) => sock,
107 /// println!("Couldn't bind: {:?}", e);
111 /// let addr = socket.local_addr().expect("Couldn't get local address");
114 #[stable(feature = "unix_socket", since = "1.10.0")]
115 pub struct SocketAddr {
116 addr: libc::sockaddr_un,
117 len: libc::socklen_t,
121 fn new<F>(f: F) -> io::Result<SocketAddr>
123 F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int,
126 let mut addr: libc::sockaddr_un = mem::zeroed();
127 let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
128 cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
129 SocketAddr::from_parts(addr, len)
133 fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result<SocketAddr> {
135 // When there is a datagram from unnamed unix socket
136 // linux returns zero bytes of address
137 len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
138 } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
139 return Err(io::Error::new(
140 io::ErrorKind::InvalidInput,
141 "file descriptor did not correspond to a Unix socket",
145 Ok(SocketAddr { addr, len })
148 /// Returns `true` if the address is unnamed.
155 /// use std::os::unix::net::UnixListener;
157 /// fn main() -> std::io::Result<()> {
158 /// let socket = UnixListener::bind("/tmp/sock")?;
159 /// let addr = socket.local_addr().expect("Couldn't get local address");
160 /// assert_eq!(addr.is_unnamed(), false);
165 /// An unnamed address:
168 /// use std::os::unix::net::UnixDatagram;
170 /// fn main() -> std::io::Result<()> {
171 /// let socket = UnixDatagram::unbound()?;
172 /// let addr = socket.local_addr().expect("Couldn't get local address");
173 /// assert_eq!(addr.is_unnamed(), true);
177 #[stable(feature = "unix_socket", since = "1.10.0")]
178 pub fn is_unnamed(&self) -> bool {
179 if let AddressKind::Unnamed = self.address() { true } else { false }
182 /// Returns the contents of this address if it is a `pathname` address.
189 /// use std::os::unix::net::UnixListener;
190 /// use std::path::Path;
192 /// fn main() -> std::io::Result<()> {
193 /// let socket = UnixListener::bind("/tmp/sock")?;
194 /// let addr = socket.local_addr().expect("Couldn't get local address");
195 /// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
200 /// Without a pathname:
203 /// use std::os::unix::net::UnixDatagram;
205 /// fn main() -> std::io::Result<()> {
206 /// let socket = UnixDatagram::unbound()?;
207 /// let addr = socket.local_addr().expect("Couldn't get local address");
208 /// assert_eq!(addr.as_pathname(), None);
212 #[stable(feature = "unix_socket", since = "1.10.0")]
213 pub fn as_pathname(&self) -> Option<&Path> {
214 if let AddressKind::Pathname(path) = self.address() { Some(path) } else { None }
217 fn address(&self) -> AddressKind<'_> {
218 let len = self.len as usize - sun_path_offset(&self.addr);
219 let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
221 // macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
223 || (cfg!(not(any(target_os = "linux", target_os = "android")))
224 && self.addr.sun_path[0] == 0)
227 } else if self.addr.sun_path[0] == 0 {
228 AddressKind::Abstract(&path[1..len])
230 AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
235 #[stable(feature = "unix_socket", since = "1.10.0")]
236 impl fmt::Debug for SocketAddr {
237 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
238 match self.address() {
239 AddressKind::Unnamed => write!(fmt, "(unnamed)"),
240 AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
241 AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
246 struct AsciiEscaped<'a>(&'a [u8]);
248 impl<'a> fmt::Display for AsciiEscaped<'a> {
249 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
251 for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
252 write!(fmt, "{}", byte as char)?;
258 /// A Unix stream socket.
263 /// use std::os::unix::net::UnixStream;
264 /// use std::io::prelude::*;
266 /// fn main() -> std::io::Result<()> {
267 /// let mut stream = UnixStream::connect("/path/to/my/socket")?;
268 /// stream.write_all(b"hello world")?;
269 /// let mut response = String::new();
270 /// stream.read_to_string(&mut response)?;
271 /// println!("{}", response);
275 #[stable(feature = "unix_socket", since = "1.10.0")]
276 pub struct UnixStream(Socket);
278 #[stable(feature = "unix_socket", since = "1.10.0")]
279 impl fmt::Debug for UnixStream {
280 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
281 let mut builder = fmt.debug_struct("UnixStream");
282 builder.field("fd", self.0.as_inner());
283 if let Ok(addr) = self.local_addr() {
284 builder.field("local", &addr);
286 if let Ok(addr) = self.peer_addr() {
287 builder.field("peer", &addr);
294 /// Connects to the socket named by `path`.
299 /// use std::os::unix::net::UnixStream;
301 /// let socket = match UnixStream::connect("/tmp/sock") {
302 /// Ok(sock) => sock,
304 /// println!("Couldn't connect: {:?}", e);
309 #[stable(feature = "unix_socket", since = "1.10.0")]
310 pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
311 fn inner(path: &Path) -> io::Result<UnixStream> {
313 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
314 let (addr, len) = sockaddr_un(path)?;
316 cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
317 Ok(UnixStream(inner))
323 /// Creates an unnamed pair of connected sockets.
325 /// Returns two `UnixStream`s which are connected to each other.
330 /// use std::os::unix::net::UnixStream;
332 /// let (sock1, sock2) = match UnixStream::pair() {
333 /// Ok((sock1, sock2)) => (sock1, sock2),
335 /// println!("Couldn't create a pair of sockets: {:?}", e);
340 #[stable(feature = "unix_socket", since = "1.10.0")]
341 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
342 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
343 Ok((UnixStream(i1), UnixStream(i2)))
346 /// Creates a new independently owned handle to the underlying socket.
348 /// The returned `UnixStream` is a reference to the same stream that this
349 /// object references. Both handles will read and write the same stream of
350 /// data, and options set on one stream will be propagated to the other
356 /// use std::os::unix::net::UnixStream;
358 /// fn main() -> std::io::Result<()> {
359 /// let socket = UnixStream::connect("/tmp/sock")?;
360 /// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
364 #[stable(feature = "unix_socket", since = "1.10.0")]
365 pub fn try_clone(&self) -> io::Result<UnixStream> {
366 self.0.duplicate().map(UnixStream)
369 /// Returns the socket address of the local half of this connection.
374 /// use std::os::unix::net::UnixStream;
376 /// fn main() -> std::io::Result<()> {
377 /// let socket = UnixStream::connect("/tmp/sock")?;
378 /// let addr = socket.local_addr().expect("Couldn't get local address");
382 #[stable(feature = "unix_socket", since = "1.10.0")]
383 pub fn local_addr(&self) -> io::Result<SocketAddr> {
384 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
387 /// Returns the socket address of the remote half of this connection.
392 /// use std::os::unix::net::UnixStream;
394 /// fn main() -> std::io::Result<()> {
395 /// let socket = UnixStream::connect("/tmp/sock")?;
396 /// let addr = socket.peer_addr().expect("Couldn't get peer address");
400 #[stable(feature = "unix_socket", since = "1.10.0")]
401 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
402 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
405 /// Sets the read timeout for the socket.
407 /// If the provided value is [`None`], then [`read`] calls will block
408 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
411 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
412 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
413 /// [`read`]: ../../../../std/io/trait.Read.html#tymethod.read
414 /// [`Duration`]: ../../../../std/time/struct.Duration.html
419 /// use std::os::unix::net::UnixStream;
420 /// use std::time::Duration;
422 /// fn main() -> std::io::Result<()> {
423 /// let socket = UnixStream::connect("/tmp/sock")?;
424 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
429 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
434 /// use std::os::unix::net::UnixStream;
435 /// use std::time::Duration;
437 /// fn main() -> std::io::Result<()> {
438 /// let socket = UnixStream::connect("/tmp/sock")?;
439 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
440 /// let err = result.unwrap_err();
441 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
445 #[stable(feature = "unix_socket", since = "1.10.0")]
446 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
447 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
450 /// Sets the write timeout for the socket.
452 /// If the provided value is [`None`], then [`write`] calls will block
453 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
454 /// passed to this method.
456 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
457 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
458 /// [`write`]: ../../../../std/io/trait.Write.html#tymethod.write
459 /// [`Duration`]: ../../../../std/time/struct.Duration.html
464 /// use std::os::unix::net::UnixStream;
465 /// use std::time::Duration;
467 /// fn main() -> std::io::Result<()> {
468 /// let socket = UnixStream::connect("/tmp/sock")?;
469 /// socket.set_write_timeout(Some(Duration::new(1, 0)))
470 /// .expect("Couldn't set write timeout");
475 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
480 /// use std::net::UdpSocket;
481 /// use std::time::Duration;
483 /// fn main() -> std::io::Result<()> {
484 /// let socket = UdpSocket::bind("127.0.0.1:34254")?;
485 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
486 /// let err = result.unwrap_err();
487 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
491 #[stable(feature = "unix_socket", since = "1.10.0")]
492 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
493 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
496 /// Returns the read timeout of this socket.
501 /// use std::os::unix::net::UnixStream;
502 /// use std::time::Duration;
504 /// fn main() -> std::io::Result<()> {
505 /// let socket = UnixStream::connect("/tmp/sock")?;
506 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
507 /// assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
511 #[stable(feature = "unix_socket", since = "1.10.0")]
512 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
513 self.0.timeout(libc::SO_RCVTIMEO)
516 /// Returns the write timeout of this socket.
521 /// use std::os::unix::net::UnixStream;
522 /// use std::time::Duration;
524 /// fn main() -> std::io::Result<()> {
525 /// let socket = UnixStream::connect("/tmp/sock")?;
526 /// socket.set_write_timeout(Some(Duration::new(1, 0)))
527 /// .expect("Couldn't set write timeout");
528 /// assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
532 #[stable(feature = "unix_socket", since = "1.10.0")]
533 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
534 self.0.timeout(libc::SO_SNDTIMEO)
537 /// Moves the socket into or out of nonblocking mode.
542 /// use std::os::unix::net::UnixStream;
544 /// fn main() -> std::io::Result<()> {
545 /// let socket = UnixStream::connect("/tmp/sock")?;
546 /// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
550 #[stable(feature = "unix_socket", since = "1.10.0")]
551 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
552 self.0.set_nonblocking(nonblocking)
555 /// Returns the value of the `SO_ERROR` option.
560 /// use std::os::unix::net::UnixStream;
562 /// fn main() -> std::io::Result<()> {
563 /// let socket = UnixStream::connect("/tmp/sock")?;
564 /// if let Ok(Some(err)) = socket.take_error() {
565 /// println!("Got error: {:?}", err);
571 /// # Platform specific
572 /// On Redox this always returns `None`.
573 #[stable(feature = "unix_socket", since = "1.10.0")]
574 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
578 /// Shuts down the read, write, or both halves of this connection.
580 /// This function will cause all pending and future I/O calls on the
581 /// specified portions to immediately return with an appropriate value
582 /// (see the documentation of [`Shutdown`]).
584 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
589 /// use std::os::unix::net::UnixStream;
590 /// use std::net::Shutdown;
592 /// fn main() -> std::io::Result<()> {
593 /// let socket = UnixStream::connect("/tmp/sock")?;
594 /// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
598 #[stable(feature = "unix_socket", since = "1.10.0")]
599 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
604 #[stable(feature = "unix_socket", since = "1.10.0")]
605 impl io::Read for UnixStream {
606 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
607 io::Read::read(&mut &*self, buf)
610 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
611 io::Read::read_vectored(&mut &*self, bufs)
615 fn is_read_vectored(&self) -> bool {
616 io::Read::is_read_vectored(&&*self)
620 unsafe fn initializer(&self) -> Initializer {
625 #[stable(feature = "unix_socket", since = "1.10.0")]
626 impl<'a> io::Read for &'a UnixStream {
627 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
631 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
632 self.0.read_vectored(bufs)
636 fn is_read_vectored(&self) -> bool {
637 self.0.is_read_vectored()
641 unsafe fn initializer(&self) -> Initializer {
646 #[stable(feature = "unix_socket", since = "1.10.0")]
647 impl io::Write for UnixStream {
648 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
649 io::Write::write(&mut &*self, buf)
652 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
653 io::Write::write_vectored(&mut &*self, bufs)
657 fn is_write_vectored(&self) -> bool {
658 io::Write::is_write_vectored(&&*self)
661 fn flush(&mut self) -> io::Result<()> {
662 io::Write::flush(&mut &*self)
666 #[stable(feature = "unix_socket", since = "1.10.0")]
667 impl<'a> io::Write for &'a UnixStream {
668 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
672 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
673 self.0.write_vectored(bufs)
677 fn is_write_vectored(&self) -> bool {
678 self.0.is_write_vectored()
681 fn flush(&mut self) -> io::Result<()> {
686 #[stable(feature = "unix_socket", since = "1.10.0")]
687 impl AsRawFd for UnixStream {
688 fn as_raw_fd(&self) -> RawFd {
693 #[stable(feature = "unix_socket", since = "1.10.0")]
694 impl FromRawFd for UnixStream {
695 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
696 UnixStream(Socket::from_inner(fd))
700 #[stable(feature = "unix_socket", since = "1.10.0")]
701 impl IntoRawFd for UnixStream {
702 fn into_raw_fd(self) -> RawFd {
707 #[stable(feature = "rust1", since = "1.0.0")]
708 impl AsRawFd for net::TcpStream {
709 fn as_raw_fd(&self) -> RawFd {
710 *self.as_inner().socket().as_inner()
714 #[stable(feature = "rust1", since = "1.0.0")]
715 impl AsRawFd for net::TcpListener {
716 fn as_raw_fd(&self) -> RawFd {
717 *self.as_inner().socket().as_inner()
721 #[stable(feature = "rust1", since = "1.0.0")]
722 impl AsRawFd for net::UdpSocket {
723 fn as_raw_fd(&self) -> RawFd {
724 *self.as_inner().socket().as_inner()
728 #[stable(feature = "from_raw_os", since = "1.1.0")]
729 impl FromRawFd for net::TcpStream {
730 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
731 let socket = sys::net::Socket::from_inner(fd);
732 net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
736 #[stable(feature = "from_raw_os", since = "1.1.0")]
737 impl FromRawFd for net::TcpListener {
738 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
739 let socket = sys::net::Socket::from_inner(fd);
740 net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
744 #[stable(feature = "from_raw_os", since = "1.1.0")]
745 impl FromRawFd for net::UdpSocket {
746 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
747 let socket = sys::net::Socket::from_inner(fd);
748 net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
752 #[stable(feature = "into_raw_os", since = "1.4.0")]
753 impl IntoRawFd for net::TcpStream {
754 fn into_raw_fd(self) -> RawFd {
755 self.into_inner().into_socket().into_inner()
758 #[stable(feature = "into_raw_os", since = "1.4.0")]
759 impl IntoRawFd for net::TcpListener {
760 fn into_raw_fd(self) -> RawFd {
761 self.into_inner().into_socket().into_inner()
764 #[stable(feature = "into_raw_os", since = "1.4.0")]
765 impl IntoRawFd for net::UdpSocket {
766 fn into_raw_fd(self) -> RawFd {
767 self.into_inner().into_socket().into_inner()
771 /// A structure representing a Unix domain socket server.
777 /// use std::os::unix::net::{UnixStream, UnixListener};
779 /// fn handle_client(stream: UnixStream) {
783 /// fn main() -> std::io::Result<()> {
784 /// let listener = UnixListener::bind("/path/to/the/socket")?;
786 /// // accept connections and process them, spawning a new thread for each one
787 /// for stream in listener.incoming() {
790 /// /* connection succeeded */
791 /// thread::spawn(|| handle_client(stream));
794 /// /* connection failed */
802 #[stable(feature = "unix_socket", since = "1.10.0")]
803 pub struct UnixListener(Socket);
805 #[stable(feature = "unix_socket", since = "1.10.0")]
806 impl fmt::Debug for UnixListener {
807 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
808 let mut builder = fmt.debug_struct("UnixListener");
809 builder.field("fd", self.0.as_inner());
810 if let Ok(addr) = self.local_addr() {
811 builder.field("local", &addr);
818 /// Creates a new `UnixListener` bound to the specified socket.
823 /// use std::os::unix::net::UnixListener;
825 /// let listener = match UnixListener::bind("/path/to/the/socket") {
826 /// Ok(sock) => sock,
828 /// println!("Couldn't connect: {:?}", e);
833 #[stable(feature = "unix_socket", since = "1.10.0")]
834 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
835 fn inner(path: &Path) -> io::Result<UnixListener> {
837 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
838 let (addr, len) = sockaddr_un(path)?;
840 cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len as _))?;
841 cvt(libc::listen(*inner.as_inner(), 128))?;
843 Ok(UnixListener(inner))
849 /// Accepts a new incoming connection to this listener.
851 /// This function will block the calling thread until a new Unix connection
852 /// is established. When established, the corresponding [`UnixStream`] and
853 /// the remote peer's address will be returned.
855 /// [`UnixStream`]: ../../../../std/os/unix/net/struct.UnixStream.html
860 /// use std::os::unix::net::UnixListener;
862 /// fn main() -> std::io::Result<()> {
863 /// let listener = UnixListener::bind("/path/to/the/socket")?;
865 /// match listener.accept() {
866 /// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
867 /// Err(e) => println!("accept function failed: {:?}", e),
872 #[stable(feature = "unix_socket", since = "1.10.0")]
873 pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
874 let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
875 let mut len = mem::size_of_val(&storage) as libc::socklen_t;
876 let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
877 let addr = SocketAddr::from_parts(storage, len)?;
878 Ok((UnixStream(sock), addr))
881 /// Creates a new independently owned handle to the underlying socket.
883 /// The returned `UnixListener` is a reference to the same socket that this
884 /// object references. Both handles can be used to accept incoming
885 /// connections and options set on one listener will affect the other.
890 /// use std::os::unix::net::UnixListener;
892 /// fn main() -> std::io::Result<()> {
893 /// let listener = UnixListener::bind("/path/to/the/socket")?;
894 /// let listener_copy = listener.try_clone().expect("try_clone failed");
898 #[stable(feature = "unix_socket", since = "1.10.0")]
899 pub fn try_clone(&self) -> io::Result<UnixListener> {
900 self.0.duplicate().map(UnixListener)
903 /// Returns the local socket address of this listener.
908 /// use std::os::unix::net::UnixListener;
910 /// fn main() -> std::io::Result<()> {
911 /// let listener = UnixListener::bind("/path/to/the/socket")?;
912 /// let addr = listener.local_addr().expect("Couldn't get local address");
916 #[stable(feature = "unix_socket", since = "1.10.0")]
917 pub fn local_addr(&self) -> io::Result<SocketAddr> {
918 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
921 /// Moves the socket into or out of nonblocking mode.
923 /// This will result in the `accept` operation becoming nonblocking,
924 /// i.e., immediately returning from their calls. If the IO operation is
925 /// successful, `Ok` is returned and no further action is required. If the
926 /// IO operation could not be completed and needs to be retried, an error
927 /// with kind [`io::ErrorKind::WouldBlock`] is returned.
932 /// use std::os::unix::net::UnixListener;
934 /// fn main() -> std::io::Result<()> {
935 /// let listener = UnixListener::bind("/path/to/the/socket")?;
936 /// listener.set_nonblocking(true).expect("Couldn't set non blocking");
941 /// [`io::ErrorKind::WouldBlock`]: ../../../io/enum.ErrorKind.html#variant.WouldBlock
942 #[stable(feature = "unix_socket", since = "1.10.0")]
943 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
944 self.0.set_nonblocking(nonblocking)
947 /// Returns the value of the `SO_ERROR` option.
952 /// use std::os::unix::net::UnixListener;
954 /// fn main() -> std::io::Result<()> {
955 /// let listener = UnixListener::bind("/tmp/sock")?;
957 /// if let Ok(Some(err)) = listener.take_error() {
958 /// println!("Got error: {:?}", err);
964 /// # Platform specific
965 /// On Redox this always returns `None`.
966 #[stable(feature = "unix_socket", since = "1.10.0")]
967 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
971 /// Returns an iterator over incoming connections.
973 /// The iterator will never return [`None`] and will also not yield the
974 /// peer's [`SocketAddr`] structure.
976 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
977 /// [`SocketAddr`]: struct.SocketAddr.html
983 /// use std::os::unix::net::{UnixStream, UnixListener};
985 /// fn handle_client(stream: UnixStream) {
989 /// fn main() -> std::io::Result<()> {
990 /// let listener = UnixListener::bind("/path/to/the/socket")?;
992 /// for stream in listener.incoming() {
995 /// thread::spawn(|| handle_client(stream));
1005 #[stable(feature = "unix_socket", since = "1.10.0")]
1006 pub fn incoming(&self) -> Incoming<'_> {
1007 Incoming { listener: self }
1011 #[stable(feature = "unix_socket", since = "1.10.0")]
1012 impl AsRawFd for UnixListener {
1013 fn as_raw_fd(&self) -> RawFd {
1018 #[stable(feature = "unix_socket", since = "1.10.0")]
1019 impl FromRawFd for UnixListener {
1020 unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
1021 UnixListener(Socket::from_inner(fd))
1025 #[stable(feature = "unix_socket", since = "1.10.0")]
1026 impl IntoRawFd for UnixListener {
1027 fn into_raw_fd(self) -> RawFd {
1032 #[stable(feature = "unix_socket", since = "1.10.0")]
1033 impl<'a> IntoIterator for &'a UnixListener {
1034 type Item = io::Result<UnixStream>;
1035 type IntoIter = Incoming<'a>;
1037 fn into_iter(self) -> Incoming<'a> {
1042 /// An iterator over incoming connections to a [`UnixListener`].
1044 /// It will never return [`None`].
1046 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1047 /// [`UnixListener`]: struct.UnixListener.html
1052 /// use std::thread;
1053 /// use std::os::unix::net::{UnixStream, UnixListener};
1055 /// fn handle_client(stream: UnixStream) {
1059 /// fn main() -> std::io::Result<()> {
1060 /// let listener = UnixListener::bind("/path/to/the/socket")?;
1062 /// for stream in listener.incoming() {
1065 /// thread::spawn(|| handle_client(stream));
1076 #[stable(feature = "unix_socket", since = "1.10.0")]
1077 pub struct Incoming<'a> {
1078 listener: &'a UnixListener,
1081 #[stable(feature = "unix_socket", since = "1.10.0")]
1082 impl<'a> Iterator for Incoming<'a> {
1083 type Item = io::Result<UnixStream>;
1085 fn next(&mut self) -> Option<io::Result<UnixStream>> {
1086 Some(self.listener.accept().map(|s| s.0))
1089 fn size_hint(&self) -> (usize, Option<usize>) {
1094 /// A Unix datagram socket.
1099 /// use std::os::unix::net::UnixDatagram;
1101 /// fn main() -> std::io::Result<()> {
1102 /// let socket = UnixDatagram::bind("/path/to/my/socket")?;
1103 /// socket.send_to(b"hello world", "/path/to/other/socket")?;
1104 /// let mut buf = [0; 100];
1105 /// let (count, address) = socket.recv_from(&mut buf)?;
1106 /// println!("socket {:?} sent {:?}", address, &buf[..count]);
1110 #[stable(feature = "unix_socket", since = "1.10.0")]
1111 pub struct UnixDatagram(Socket);
1113 #[stable(feature = "unix_socket", since = "1.10.0")]
1114 impl fmt::Debug for UnixDatagram {
1115 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1116 let mut builder = fmt.debug_struct("UnixDatagram");
1117 builder.field("fd", self.0.as_inner());
1118 if let Ok(addr) = self.local_addr() {
1119 builder.field("local", &addr);
1121 if let Ok(addr) = self.peer_addr() {
1122 builder.field("peer", &addr);
1129 /// Creates a Unix datagram socket bound to the given path.
1134 /// use std::os::unix::net::UnixDatagram;
1136 /// let sock = match UnixDatagram::bind("/path/to/the/socket") {
1137 /// Ok(sock) => sock,
1139 /// println!("Couldn't bind: {:?}", e);
1144 #[stable(feature = "unix_socket", since = "1.10.0")]
1145 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
1146 fn inner(path: &Path) -> io::Result<UnixDatagram> {
1148 let socket = UnixDatagram::unbound()?;
1149 let (addr, len) = sockaddr_un(path)?;
1151 cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len as _))?;
1156 inner(path.as_ref())
1159 /// Creates a Unix Datagram socket which is not bound to any address.
1164 /// use std::os::unix::net::UnixDatagram;
1166 /// let sock = match UnixDatagram::unbound() {
1167 /// Ok(sock) => sock,
1169 /// println!("Couldn't unbound: {:?}", e);
1174 #[stable(feature = "unix_socket", since = "1.10.0")]
1175 pub fn unbound() -> io::Result<UnixDatagram> {
1176 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1177 Ok(UnixDatagram(inner))
1180 /// Creates an unnamed pair of connected sockets.
1182 /// Returns two `UnixDatagrams`s which are connected to each other.
1187 /// use std::os::unix::net::UnixDatagram;
1189 /// let (sock1, sock2) = match UnixDatagram::pair() {
1190 /// Ok((sock1, sock2)) => (sock1, sock2),
1192 /// println!("Couldn't unbound: {:?}", e);
1197 #[stable(feature = "unix_socket", since = "1.10.0")]
1198 pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
1199 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1200 Ok((UnixDatagram(i1), UnixDatagram(i2)))
1203 /// Connects the socket to the specified address.
1205 /// The [`send`] method may be used to send data to the specified address.
1206 /// [`recv`] and [`recv_from`] will only receive data from that address.
1208 /// [`send`]: #method.send
1209 /// [`recv`]: #method.recv
1210 /// [`recv_from`]: #method.recv_from
1215 /// use std::os::unix::net::UnixDatagram;
1217 /// fn main() -> std::io::Result<()> {
1218 /// let sock = UnixDatagram::unbound()?;
1219 /// match sock.connect("/path/to/the/socket") {
1220 /// Ok(sock) => sock,
1222 /// println!("Couldn't connect: {:?}", e);
1229 #[stable(feature = "unix_socket", since = "1.10.0")]
1230 pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1231 fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
1233 let (addr, len) = sockaddr_un(path)?;
1235 cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
1240 inner(self, path.as_ref())
1243 /// Creates a new independently owned handle to the underlying socket.
1245 /// The returned `UnixDatagram` is a reference to the same socket that this
1246 /// object references. Both handles can be used to accept incoming
1247 /// connections and options set on one side will affect the other.
1252 /// use std::os::unix::net::UnixDatagram;
1254 /// fn main() -> std::io::Result<()> {
1255 /// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1256 /// let sock_copy = sock.try_clone().expect("try_clone failed");
1260 #[stable(feature = "unix_socket", since = "1.10.0")]
1261 pub fn try_clone(&self) -> io::Result<UnixDatagram> {
1262 self.0.duplicate().map(UnixDatagram)
1265 /// Returns the address of this socket.
1270 /// use std::os::unix::net::UnixDatagram;
1272 /// fn main() -> std::io::Result<()> {
1273 /// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1274 /// let addr = sock.local_addr().expect("Couldn't get local address");
1278 #[stable(feature = "unix_socket", since = "1.10.0")]
1279 pub fn local_addr(&self) -> io::Result<SocketAddr> {
1280 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
1283 /// Returns the address of this socket's peer.
1285 /// The [`connect`] method will connect the socket to a peer.
1287 /// [`connect`]: #method.connect
1292 /// use std::os::unix::net::UnixDatagram;
1294 /// fn main() -> std::io::Result<()> {
1295 /// let sock = UnixDatagram::unbound()?;
1296 /// sock.connect("/path/to/the/socket")?;
1298 /// let addr = sock.peer_addr().expect("Couldn't get peer address");
1302 #[stable(feature = "unix_socket", since = "1.10.0")]
1303 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
1304 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
1307 /// Receives data from the socket.
1309 /// On success, returns the number of bytes read and the address from
1310 /// whence the data came.
1315 /// use std::os::unix::net::UnixDatagram;
1317 /// fn main() -> std::io::Result<()> {
1318 /// let sock = UnixDatagram::unbound()?;
1319 /// let mut buf = vec![0; 10];
1320 /// let (size, sender) = sock.recv_from(buf.as_mut_slice())?;
1321 /// println!("received {} bytes from {:?}", size, sender);
1325 #[stable(feature = "unix_socket", since = "1.10.0")]
1326 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
1328 let addr = SocketAddr::new(|addr, len| unsafe {
1329 count = libc::recvfrom(
1331 buf.as_mut_ptr() as *mut _,
1339 } else if count == 0 {
1346 Ok((count as usize, addr))
1349 /// Receives data from the socket.
1351 /// On success, returns the number of bytes read.
1356 /// use std::os::unix::net::UnixDatagram;
1358 /// fn main() -> std::io::Result<()> {
1359 /// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1360 /// let mut buf = vec![0; 10];
1361 /// sock.recv(buf.as_mut_slice()).expect("recv function failed");
1365 #[stable(feature = "unix_socket", since = "1.10.0")]
1366 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
1370 /// Sends data on the socket to the specified address.
1372 /// On success, returns the number of bytes written.
1377 /// use std::os::unix::net::UnixDatagram;
1379 /// fn main() -> std::io::Result<()> {
1380 /// let sock = UnixDatagram::unbound()?;
1381 /// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
1385 #[stable(feature = "unix_socket", since = "1.10.0")]
1386 pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
1387 fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
1389 let (addr, len) = sockaddr_un(path)?;
1391 let count = cvt(libc::sendto(
1393 buf.as_ptr() as *const _,
1396 &addr as *const _ as *const _,
1402 inner(self, buf, path.as_ref())
1405 /// Sends data on the socket to the socket's peer.
1407 /// The peer address may be set by the `connect` method, and this method
1408 /// will return an error if the socket has not already been connected.
1410 /// On success, returns the number of bytes written.
1415 /// use std::os::unix::net::UnixDatagram;
1417 /// fn main() -> std::io::Result<()> {
1418 /// let sock = UnixDatagram::unbound()?;
1419 /// sock.connect("/some/sock").expect("Couldn't connect");
1420 /// sock.send(b"omelette au fromage").expect("send_to function failed");
1424 #[stable(feature = "unix_socket", since = "1.10.0")]
1425 pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
1429 /// Sets the read timeout for the socket.
1431 /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
1432 /// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
1433 /// is passed to this method.
1435 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1436 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
1437 /// [`recv`]: #method.recv
1438 /// [`recv_from`]: #method.recv_from
1439 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1444 /// use std::os::unix::net::UnixDatagram;
1445 /// use std::time::Duration;
1447 /// fn main() -> std::io::Result<()> {
1448 /// let sock = UnixDatagram::unbound()?;
1449 /// sock.set_read_timeout(Some(Duration::new(1, 0)))
1450 /// .expect("set_read_timeout function failed");
1455 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1460 /// use std::os::unix::net::UnixDatagram;
1461 /// use std::time::Duration;
1463 /// fn main() -> std::io::Result<()> {
1464 /// let socket = UnixDatagram::unbound()?;
1465 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
1466 /// let err = result.unwrap_err();
1467 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1471 #[stable(feature = "unix_socket", since = "1.10.0")]
1472 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1473 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
1476 /// Sets the write timeout for the socket.
1478 /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
1479 /// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
1482 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1483 /// [`send`]: #method.send
1484 /// [`send_to`]: #method.send_to
1485 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1490 /// use std::os::unix::net::UnixDatagram;
1491 /// use std::time::Duration;
1493 /// fn main() -> std::io::Result<()> {
1494 /// let sock = UnixDatagram::unbound()?;
1495 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1496 /// .expect("set_write_timeout function failed");
1501 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1506 /// use std::os::unix::net::UnixDatagram;
1507 /// use std::time::Duration;
1509 /// fn main() -> std::io::Result<()> {
1510 /// let socket = UnixDatagram::unbound()?;
1511 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
1512 /// let err = result.unwrap_err();
1513 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1517 #[stable(feature = "unix_socket", since = "1.10.0")]
1518 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1519 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
1522 /// Returns the read timeout of this socket.
1527 /// use std::os::unix::net::UnixDatagram;
1528 /// use std::time::Duration;
1530 /// fn main() -> std::io::Result<()> {
1531 /// let sock = UnixDatagram::unbound()?;
1532 /// sock.set_read_timeout(Some(Duration::new(1, 0)))
1533 /// .expect("set_read_timeout function failed");
1534 /// assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0)));
1538 #[stable(feature = "unix_socket", since = "1.10.0")]
1539 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
1540 self.0.timeout(libc::SO_RCVTIMEO)
1543 /// Returns the write timeout of this socket.
1548 /// use std::os::unix::net::UnixDatagram;
1549 /// use std::time::Duration;
1551 /// fn main() -> std::io::Result<()> {
1552 /// let sock = UnixDatagram::unbound()?;
1553 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1554 /// .expect("set_write_timeout function failed");
1555 /// assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0)));
1559 #[stable(feature = "unix_socket", since = "1.10.0")]
1560 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
1561 self.0.timeout(libc::SO_SNDTIMEO)
1564 /// Moves the socket into or out of nonblocking mode.
1569 /// use std::os::unix::net::UnixDatagram;
1571 /// fn main() -> std::io::Result<()> {
1572 /// let sock = UnixDatagram::unbound()?;
1573 /// sock.set_nonblocking(true).expect("set_nonblocking function failed");
1577 #[stable(feature = "unix_socket", since = "1.10.0")]
1578 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
1579 self.0.set_nonblocking(nonblocking)
1582 /// Returns the value of the `SO_ERROR` option.
1587 /// use std::os::unix::net::UnixDatagram;
1589 /// fn main() -> std::io::Result<()> {
1590 /// let sock = UnixDatagram::unbound()?;
1591 /// if let Ok(Some(err)) = sock.take_error() {
1592 /// println!("Got error: {:?}", err);
1597 #[stable(feature = "unix_socket", since = "1.10.0")]
1598 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
1602 /// Shut down the read, write, or both halves of this connection.
1604 /// This function will cause all pending and future I/O calls on the
1605 /// specified portions to immediately return with an appropriate value
1606 /// (see the documentation of [`Shutdown`]).
1608 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
1611 /// use std::os::unix::net::UnixDatagram;
1612 /// use std::net::Shutdown;
1614 /// fn main() -> std::io::Result<()> {
1615 /// let sock = UnixDatagram::unbound()?;
1616 /// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
1620 #[stable(feature = "unix_socket", since = "1.10.0")]
1621 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
1622 self.0.shutdown(how)
1626 #[stable(feature = "unix_socket", since = "1.10.0")]
1627 impl AsRawFd for UnixDatagram {
1628 fn as_raw_fd(&self) -> RawFd {
1633 #[stable(feature = "unix_socket", since = "1.10.0")]
1634 impl FromRawFd for UnixDatagram {
1635 unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
1636 UnixDatagram(Socket::from_inner(fd))
1640 #[stable(feature = "unix_socket", since = "1.10.0")]
1641 impl IntoRawFd for UnixDatagram {
1642 fn into_raw_fd(self) -> RawFd {
1647 #[cfg(all(test, not(target_os = "emscripten")))]
1649 use crate::io::prelude::*;
1650 use crate::io::{self, ErrorKind};
1651 use crate::sys_common::io::test::tmpdir;
1653 use crate::time::Duration;
1657 macro_rules! or_panic {
1661 Err(e) => panic!("{}", e),
1669 let socket_path = dir.path().join("sock");
1670 let msg1 = b"hello";
1671 let msg2 = b"world!";
1673 let listener = or_panic!(UnixListener::bind(&socket_path));
1674 let thread = thread::spawn(move || {
1675 let mut stream = or_panic!(listener.accept()).0;
1676 let mut buf = [0; 5];
1677 or_panic!(stream.read(&mut buf));
1678 assert_eq!(&msg1[..], &buf[..]);
1679 or_panic!(stream.write_all(msg2));
1682 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1683 assert_eq!(Some(&*socket_path), stream.peer_addr().unwrap().as_pathname());
1684 or_panic!(stream.write_all(msg1));
1685 let mut buf = vec![];
1686 or_panic!(stream.read_to_end(&mut buf));
1687 assert_eq!(&msg2[..], &buf[..]);
1690 thread.join().unwrap();
1695 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
1697 let len = or_panic!(s1.write_vectored(&[
1698 IoSlice::new(b"hello"),
1700 IoSlice::new(b"world!")
1702 assert_eq!(len, 12);
1704 let mut buf1 = [0; 6];
1705 let mut buf2 = [0; 7];
1706 let len = or_panic!(
1707 s2.read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)],)
1709 assert_eq!(len, 12);
1710 assert_eq!(&buf1, b"hello ");
1711 assert_eq!(&buf2, b"world!\0");
1716 let msg1 = b"hello";
1717 let msg2 = b"world!";
1719 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
1720 let thread = thread::spawn(move || {
1721 // s1 must be moved in or the test will hang!
1722 let mut buf = [0; 5];
1723 or_panic!(s1.read(&mut buf));
1724 assert_eq!(&msg1[..], &buf[..]);
1725 or_panic!(s1.write_all(msg2));
1728 or_panic!(s2.write_all(msg1));
1729 let mut buf = vec![];
1730 or_panic!(s2.read_to_end(&mut buf));
1731 assert_eq!(&msg2[..], &buf[..]);
1734 thread.join().unwrap();
1740 let socket_path = dir.path().join("sock");
1741 let msg1 = b"hello";
1742 let msg2 = b"world";
1744 let listener = or_panic!(UnixListener::bind(&socket_path));
1745 let thread = thread::spawn(move || {
1746 let mut stream = or_panic!(listener.accept()).0;
1747 or_panic!(stream.write_all(msg1));
1748 or_panic!(stream.write_all(msg2));
1751 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1752 let mut stream2 = or_panic!(stream.try_clone());
1754 let mut buf = [0; 5];
1755 or_panic!(stream.read(&mut buf));
1756 assert_eq!(&msg1[..], &buf[..]);
1757 or_panic!(stream2.read(&mut buf));
1758 assert_eq!(&msg2[..], &buf[..]);
1760 thread.join().unwrap();
1766 let socket_path = dir.path().join("sock");
1768 let listener = or_panic!(UnixListener::bind(&socket_path));
1769 let thread = thread::spawn(move || {
1770 for stream in listener.incoming().take(2) {
1771 let mut stream = or_panic!(stream);
1773 or_panic!(stream.read(&mut buf));
1778 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1779 or_panic!(stream.write_all(&[0]));
1782 thread.join().unwrap();
1788 let socket_path = dir.path().join(
1789 "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa\
1790 sasdfasdfasdasdfasdfasdfadfasdfasdfasdfasdfasdf",
1792 match UnixStream::connect(&socket_path) {
1793 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1794 Err(e) => panic!("unexpected error {}", e),
1795 Ok(_) => panic!("unexpected success"),
1798 match UnixListener::bind(&socket_path) {
1799 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1800 Err(e) => panic!("unexpected error {}", e),
1801 Ok(_) => panic!("unexpected success"),
1804 match UnixDatagram::bind(&socket_path) {
1805 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1806 Err(e) => panic!("unexpected error {}", e),
1807 Ok(_) => panic!("unexpected success"),
1814 let socket_path = dir.path().join("sock");
1816 let _listener = or_panic!(UnixListener::bind(&socket_path));
1818 let stream = or_panic!(UnixStream::connect(&socket_path));
1819 let dur = Duration::new(15410, 0);
1821 assert_eq!(None, or_panic!(stream.read_timeout()));
1823 or_panic!(stream.set_read_timeout(Some(dur)));
1824 assert_eq!(Some(dur), or_panic!(stream.read_timeout()));
1826 assert_eq!(None, or_panic!(stream.write_timeout()));
1828 or_panic!(stream.set_write_timeout(Some(dur)));
1829 assert_eq!(Some(dur), or_panic!(stream.write_timeout()));
1831 or_panic!(stream.set_read_timeout(None));
1832 assert_eq!(None, or_panic!(stream.read_timeout()));
1834 or_panic!(stream.set_write_timeout(None));
1835 assert_eq!(None, or_panic!(stream.write_timeout()));
1839 fn test_read_timeout() {
1841 let socket_path = dir.path().join("sock");
1843 let _listener = or_panic!(UnixListener::bind(&socket_path));
1845 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1846 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1848 let mut buf = [0; 10];
1849 let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
1851 kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
1852 "unexpected_error: {:?}",
1858 fn test_read_with_timeout() {
1860 let socket_path = dir.path().join("sock");
1862 let listener = or_panic!(UnixListener::bind(&socket_path));
1864 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1865 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1867 let mut other_end = or_panic!(listener.accept()).0;
1868 or_panic!(other_end.write_all(b"hello world"));
1870 let mut buf = [0; 11];
1871 or_panic!(stream.read(&mut buf));
1872 assert_eq!(b"hello world", &buf[..]);
1874 let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
1876 kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
1877 "unexpected_error: {:?}",
1882 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
1883 // when passed zero Durations
1885 fn test_unix_stream_timeout_zero_duration() {
1887 let socket_path = dir.path().join("sock");
1889 let listener = or_panic!(UnixListener::bind(&socket_path));
1890 let stream = or_panic!(UnixStream::connect(&socket_path));
1892 let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
1893 let err = result.unwrap_err();
1894 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1896 let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
1897 let err = result.unwrap_err();
1898 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1904 fn test_unix_datagram() {
1906 let path1 = dir.path().join("sock1");
1907 let path2 = dir.path().join("sock2");
1909 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1910 let sock2 = or_panic!(UnixDatagram::bind(&path2));
1912 let msg = b"hello world";
1913 or_panic!(sock1.send_to(msg, &path2));
1914 let mut buf = [0; 11];
1915 or_panic!(sock2.recv_from(&mut buf));
1916 assert_eq!(msg, &buf[..]);
1920 fn test_unnamed_unix_datagram() {
1922 let path1 = dir.path().join("sock1");
1924 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1925 let sock2 = or_panic!(UnixDatagram::unbound());
1927 let msg = b"hello world";
1928 or_panic!(sock2.send_to(msg, &path1));
1929 let mut buf = [0; 11];
1930 let (usize, addr) = or_panic!(sock1.recv_from(&mut buf));
1931 assert_eq!(usize, 11);
1932 assert!(addr.is_unnamed());
1933 assert_eq!(msg, &buf[..]);
1937 fn test_connect_unix_datagram() {
1939 let path1 = dir.path().join("sock1");
1940 let path2 = dir.path().join("sock2");
1942 let bsock1 = or_panic!(UnixDatagram::bind(&path1));
1943 let bsock2 = or_panic!(UnixDatagram::bind(&path2));
1944 let sock = or_panic!(UnixDatagram::unbound());
1945 or_panic!(sock.connect(&path1));
1948 let msg = b"hello there";
1949 or_panic!(sock.send(msg));
1950 let mut buf = [0; 11];
1951 let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
1952 assert_eq!(usize, 11);
1953 assert!(addr.is_unnamed());
1954 assert_eq!(msg, &buf[..]);
1956 // Changing default socket works too
1957 or_panic!(sock.connect(&path2));
1958 or_panic!(sock.send(msg));
1959 or_panic!(bsock2.recv_from(&mut buf));
1963 fn test_unix_datagram_recv() {
1965 let path1 = dir.path().join("sock1");
1967 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1968 let sock2 = or_panic!(UnixDatagram::unbound());
1969 or_panic!(sock2.connect(&path1));
1971 let msg = b"hello world";
1972 or_panic!(sock2.send(msg));
1973 let mut buf = [0; 11];
1974 let size = or_panic!(sock1.recv(&mut buf));
1975 assert_eq!(size, 11);
1976 assert_eq!(msg, &buf[..]);
1980 fn datagram_pair() {
1981 let msg1 = b"hello";
1982 let msg2 = b"world!";
1984 let (s1, s2) = or_panic!(UnixDatagram::pair());
1985 let thread = thread::spawn(move || {
1986 // s1 must be moved in or the test will hang!
1987 let mut buf = [0; 5];
1988 or_panic!(s1.recv(&mut buf));
1989 assert_eq!(&msg1[..], &buf[..]);
1990 or_panic!(s1.send(msg2));
1993 or_panic!(s2.send(msg1));
1994 let mut buf = [0; 6];
1995 or_panic!(s2.recv(&mut buf));
1996 assert_eq!(&msg2[..], &buf[..]);
1999 thread.join().unwrap();
2002 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
2003 // when passed zero Durations
2005 fn test_unix_datagram_timeout_zero_duration() {
2007 let path = dir.path().join("sock");
2009 let datagram = or_panic!(UnixDatagram::bind(&path));
2011 let result = datagram.set_write_timeout(Some(Duration::new(0, 0)));
2012 let err = result.unwrap_err();
2013 assert_eq!(err.kind(), ErrorKind::InvalidInput);
2015 let result = datagram.set_read_timeout(Some(Duration::new(0, 0)));
2016 let err = result.unwrap_err();
2017 assert_eq!(err.kind(), ErrorKind::InvalidInput);
2021 fn abstract_namespace_not_allowed() {
2022 assert!(UnixStream::connect("\0asdf").is_err());