1 #![stable(feature = "unix_socket", since = "1.10.0")]
3 //! Unix-specific networking functionality
11 use crate::io::{self, Initializer, IoSlice, IoSliceMut};
13 use crate::net::{self, Shutdown};
14 use crate::os::unix::ffi::OsStrExt;
15 use crate::os::unix::io::{RawFd, AsRawFd, FromRawFd, IntoRawFd};
16 use crate::path::Path;
17 use crate::time::Duration;
18 use crate::sys::{self, cvt};
19 use crate::sys::net::Socket;
20 use crate::sys_common::{self, AsInner, FromInner, IntoInner};
22 const MSG_NOSIGNAL: libc::c_int = 0x0;
24 fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
25 // Work with an actual instance of the type since using a null pointer is UB
26 let base = addr as *const _ as usize;
27 let path = &addr.sun_path as *const _ as usize;
31 unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
32 let mut addr: libc::sockaddr_un = mem::zeroed();
33 addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
35 let bytes = path.as_os_str().as_bytes();
37 if bytes.contains(&0) {
38 return Err(io::Error::new(io::ErrorKind::InvalidInput,
39 "paths may not contain interior null bytes"));
42 if bytes.len() >= addr.sun_path.len() {
43 return Err(io::Error::new(io::ErrorKind::InvalidInput,
44 "path must be shorter than SUN_LEN"));
46 for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
47 *dst = *src as libc::c_char;
49 // null byte for pathname addresses is already there because we zeroed the
52 let mut len = sun_path_offset(&addr) + bytes.len();
57 Ok((addr, len as libc::socklen_t))
60 enum AddressKind<'a> {
66 /// An address associated with a Unix socket.
71 /// use std::os::unix::net::UnixListener;
73 /// let socket = match UnixListener::bind("/tmp/sock") {
76 /// println!("Couldn't bind: {:?}", e);
80 /// let addr = socket.local_addr().expect("Couldn't get local address");
83 #[stable(feature = "unix_socket", since = "1.10.0")]
84 pub struct SocketAddr {
85 addr: libc::sockaddr_un,
90 fn new<F>(f: F) -> io::Result<SocketAddr>
91 where F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int
94 let mut addr: libc::sockaddr_un = mem::zeroed();
95 let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
96 cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
97 SocketAddr::from_parts(addr, len)
101 fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result<SocketAddr> {
103 // When there is a datagram from unnamed unix socket
104 // linux returns zero bytes of address
105 len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
106 } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
107 return Err(io::Error::new(io::ErrorKind::InvalidInput,
108 "file descriptor did not correspond to a Unix socket"));
117 /// Returns `true` if the address is unnamed.
124 /// use std::os::unix::net::UnixListener;
126 /// let socket = UnixListener::bind("/tmp/sock").unwrap();
127 /// let addr = socket.local_addr().expect("Couldn't get local address");
128 /// assert_eq!(addr.is_unnamed(), false);
131 /// An unnamed address:
134 /// use std::os::unix::net::UnixDatagram;
136 /// let socket = UnixDatagram::unbound().unwrap();
137 /// let addr = socket.local_addr().expect("Couldn't get local address");
138 /// assert_eq!(addr.is_unnamed(), true);
140 #[stable(feature = "unix_socket", since = "1.10.0")]
141 pub fn is_unnamed(&self) -> bool {
142 if let AddressKind::Unnamed = self.address() {
149 /// Returns the contents of this address if it is a `pathname` address.
156 /// use std::os::unix::net::UnixListener;
157 /// use std::path::Path;
159 /// let socket = UnixListener::bind("/tmp/sock").unwrap();
160 /// let addr = socket.local_addr().expect("Couldn't get local address");
161 /// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
164 /// Without a pathname:
167 /// use std::os::unix::net::UnixDatagram;
169 /// let socket = UnixDatagram::unbound().unwrap();
170 /// let addr = socket.local_addr().expect("Couldn't get local address");
171 /// assert_eq!(addr.as_pathname(), None);
173 #[stable(feature = "unix_socket", since = "1.10.0")]
174 pub fn as_pathname(&self) -> Option<&Path> {
175 if let AddressKind::Pathname(path) = self.address() {
182 fn address<'a>(&'a self) -> AddressKind<'a> {
183 let len = self.len as usize - sun_path_offset(&self.addr);
184 let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
186 if self.addr.sun_path[0] == 0 {
187 AddressKind::Abstract(&path[1..len])
189 AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
194 #[stable(feature = "unix_socket", since = "1.10.0")]
195 impl fmt::Debug for SocketAddr {
196 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
197 match self.address() {
198 AddressKind::Unnamed => write!(fmt, "(unnamed)"),
199 AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
200 AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
205 struct AsciiEscaped<'a>(&'a [u8]);
207 impl<'a> fmt::Display for AsciiEscaped<'a> {
208 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
210 for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
211 write!(fmt, "{}", byte as char)?;
217 /// A Unix stream socket.
222 /// use std::os::unix::net::UnixStream;
223 /// use std::io::prelude::*;
225 /// let mut stream = UnixStream::connect("/path/to/my/socket").unwrap();
226 /// stream.write_all(b"hello world").unwrap();
227 /// let mut response = String::new();
228 /// stream.read_to_string(&mut response).unwrap();
229 /// println!("{}", response);
231 #[stable(feature = "unix_socket", since = "1.10.0")]
232 pub struct UnixStream(Socket);
234 #[stable(feature = "unix_socket", since = "1.10.0")]
235 impl fmt::Debug for UnixStream {
236 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
237 let mut builder = fmt.debug_struct("UnixStream");
238 builder.field("fd", self.0.as_inner());
239 if let Ok(addr) = self.local_addr() {
240 builder.field("local", &addr);
242 if let Ok(addr) = self.peer_addr() {
243 builder.field("peer", &addr);
250 /// Connects to the socket named by `path`.
255 /// use std::os::unix::net::UnixStream;
257 /// let socket = match UnixStream::connect("/tmp/sock") {
258 /// Ok(sock) => sock,
260 /// println!("Couldn't connect: {:?}", e);
265 #[stable(feature = "unix_socket", since = "1.10.0")]
266 pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
267 fn inner(path: &Path) -> io::Result<UnixStream> {
269 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
270 let (addr, len) = sockaddr_un(path)?;
272 cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
273 Ok(UnixStream(inner))
279 /// Creates an unnamed pair of connected sockets.
281 /// Returns two `UnixStream`s which are connected to each other.
286 /// use std::os::unix::net::UnixStream;
288 /// let (sock1, sock2) = match UnixStream::pair() {
289 /// Ok((sock1, sock2)) => (sock1, sock2),
291 /// println!("Couldn't create a pair of sockets: {:?}", e);
296 #[stable(feature = "unix_socket", since = "1.10.0")]
297 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
298 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
299 Ok((UnixStream(i1), UnixStream(i2)))
302 /// Creates a new independently owned handle to the underlying socket.
304 /// The returned `UnixStream` is a reference to the same stream that this
305 /// object references. Both handles will read and write the same stream of
306 /// data, and options set on one stream will be propagated to the other
312 /// use std::os::unix::net::UnixStream;
314 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
315 /// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
317 #[stable(feature = "unix_socket", since = "1.10.0")]
318 pub fn try_clone(&self) -> io::Result<UnixStream> {
319 self.0.duplicate().map(UnixStream)
322 /// Returns the socket address of the local half of this connection.
327 /// use std::os::unix::net::UnixStream;
329 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
330 /// let addr = socket.local_addr().expect("Couldn't get local address");
332 #[stable(feature = "unix_socket", since = "1.10.0")]
333 pub fn local_addr(&self) -> io::Result<SocketAddr> {
334 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
337 /// Returns the socket address of the remote half of this connection.
342 /// use std::os::unix::net::UnixStream;
344 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
345 /// let addr = socket.peer_addr().expect("Couldn't get peer address");
347 #[stable(feature = "unix_socket", since = "1.10.0")]
348 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
349 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
352 /// Sets the read timeout for the socket.
354 /// If the provided value is [`None`], then [`read`] calls will block
355 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
358 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
359 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
360 /// [`read`]: ../../../../std/io/trait.Read.html#tymethod.read
361 /// [`Duration`]: ../../../../std/time/struct.Duration.html
366 /// use std::os::unix::net::UnixStream;
367 /// use std::time::Duration;
369 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
370 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
373 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
378 /// use std::os::unix::net::UnixStream;
379 /// use std::time::Duration;
381 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
382 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
383 /// let err = result.unwrap_err();
384 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
386 #[stable(feature = "unix_socket", since = "1.10.0")]
387 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
388 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
391 /// Sets the write timeout for the socket.
393 /// If the provided value is [`None`], then [`write`] calls will block
394 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
395 /// passed to this method.
397 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
398 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
399 /// [`write`]: ../../../../std/io/trait.Write.html#tymethod.write
400 /// [`Duration`]: ../../../../std/time/struct.Duration.html
405 /// use std::os::unix::net::UnixStream;
406 /// use std::time::Duration;
408 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
409 /// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
412 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
417 /// use std::net::UdpSocket;
418 /// use std::time::Duration;
420 /// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
421 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
422 /// let err = result.unwrap_err();
423 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
425 #[stable(feature = "unix_socket", since = "1.10.0")]
426 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
427 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
430 /// Returns the read timeout of this socket.
435 /// use std::os::unix::net::UnixStream;
436 /// use std::time::Duration;
438 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
439 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
440 /// assert_eq!(socket.read_timeout().unwrap(), Some(Duration::new(1, 0)));
442 #[stable(feature = "unix_socket", since = "1.10.0")]
443 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
444 self.0.timeout(libc::SO_RCVTIMEO)
447 /// Returns the write timeout of this socket.
452 /// use std::os::unix::net::UnixStream;
453 /// use std::time::Duration;
455 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
456 /// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
457 /// assert_eq!(socket.write_timeout().unwrap(), Some(Duration::new(1, 0)));
459 #[stable(feature = "unix_socket", since = "1.10.0")]
460 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
461 self.0.timeout(libc::SO_SNDTIMEO)
464 /// Moves the socket into or out of nonblocking mode.
469 /// use std::os::unix::net::UnixStream;
471 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
472 /// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
474 #[stable(feature = "unix_socket", since = "1.10.0")]
475 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
476 self.0.set_nonblocking(nonblocking)
479 /// Returns the value of the `SO_ERROR` option.
484 /// use std::os::unix::net::UnixStream;
486 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
487 /// if let Ok(Some(err)) = socket.take_error() {
488 /// println!("Got error: {:?}", err);
492 /// # Platform specific
493 /// On Redox this always returns `None`.
494 #[stable(feature = "unix_socket", since = "1.10.0")]
495 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
499 /// Shuts down the read, write, or both halves of this connection.
501 /// This function will cause all pending and future I/O calls on the
502 /// specified portions to immediately return with an appropriate value
503 /// (see the documentation of [`Shutdown`]).
505 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
510 /// use std::os::unix::net::UnixStream;
511 /// use std::net::Shutdown;
513 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
514 /// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
516 #[stable(feature = "unix_socket", since = "1.10.0")]
517 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
522 #[stable(feature = "unix_socket", since = "1.10.0")]
523 impl io::Read for UnixStream {
524 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
525 io::Read::read(&mut &*self, buf)
528 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
529 io::Read::read_vectored(&mut &*self, bufs)
533 unsafe fn initializer(&self) -> Initializer {
538 #[stable(feature = "unix_socket", since = "1.10.0")]
539 impl<'a> io::Read for &'a UnixStream {
540 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
544 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
545 self.0.read_vectored(bufs)
549 unsafe fn initializer(&self) -> Initializer {
554 #[stable(feature = "unix_socket", since = "1.10.0")]
555 impl io::Write for UnixStream {
556 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
557 io::Write::write(&mut &*self, buf)
560 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
561 io::Write::write_vectored(&mut &*self, bufs)
564 fn flush(&mut self) -> io::Result<()> {
565 io::Write::flush(&mut &*self)
569 #[stable(feature = "unix_socket", since = "1.10.0")]
570 impl<'a> io::Write for &'a UnixStream {
571 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
575 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
576 self.0.write_vectored(bufs)
579 fn flush(&mut self) -> io::Result<()> {
584 #[stable(feature = "unix_socket", since = "1.10.0")]
585 impl AsRawFd for UnixStream {
586 fn as_raw_fd(&self) -> RawFd {
591 #[stable(feature = "unix_socket", since = "1.10.0")]
592 impl FromRawFd for UnixStream {
593 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
594 UnixStream(Socket::from_inner(fd))
598 #[stable(feature = "unix_socket", since = "1.10.0")]
599 impl IntoRawFd for UnixStream {
600 fn into_raw_fd(self) -> RawFd {
605 #[stable(feature = "rust1", since = "1.0.0")]
606 impl AsRawFd for net::TcpStream {
607 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
610 #[stable(feature = "rust1", since = "1.0.0")]
611 impl AsRawFd for net::TcpListener {
612 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
615 #[stable(feature = "rust1", since = "1.0.0")]
616 impl AsRawFd for net::UdpSocket {
617 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
620 #[stable(feature = "from_raw_os", since = "1.1.0")]
621 impl FromRawFd for net::TcpStream {
622 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
623 let socket = sys::net::Socket::from_inner(fd);
624 net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
628 #[stable(feature = "from_raw_os", since = "1.1.0")]
629 impl FromRawFd for net::TcpListener {
630 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
631 let socket = sys::net::Socket::from_inner(fd);
632 net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
636 #[stable(feature = "from_raw_os", since = "1.1.0")]
637 impl FromRawFd for net::UdpSocket {
638 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
639 let socket = sys::net::Socket::from_inner(fd);
640 net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
644 #[stable(feature = "into_raw_os", since = "1.4.0")]
645 impl IntoRawFd for net::TcpStream {
646 fn into_raw_fd(self) -> RawFd {
647 self.into_inner().into_socket().into_inner()
650 #[stable(feature = "into_raw_os", since = "1.4.0")]
651 impl IntoRawFd for net::TcpListener {
652 fn into_raw_fd(self) -> RawFd {
653 self.into_inner().into_socket().into_inner()
656 #[stable(feature = "into_raw_os", since = "1.4.0")]
657 impl IntoRawFd for net::UdpSocket {
658 fn into_raw_fd(self) -> RawFd {
659 self.into_inner().into_socket().into_inner()
663 /// A structure representing a Unix domain socket server.
669 /// use std::os::unix::net::{UnixStream, UnixListener};
671 /// fn handle_client(stream: UnixStream) {
675 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
677 /// // accept connections and process them, spawning a new thread for each one
678 /// for stream in listener.incoming() {
681 /// /* connection succeeded */
682 /// thread::spawn(|| handle_client(stream));
685 /// /* connection failed */
691 #[stable(feature = "unix_socket", since = "1.10.0")]
692 pub struct UnixListener(Socket);
694 #[stable(feature = "unix_socket", since = "1.10.0")]
695 impl fmt::Debug for UnixListener {
696 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
697 let mut builder = fmt.debug_struct("UnixListener");
698 builder.field("fd", self.0.as_inner());
699 if let Ok(addr) = self.local_addr() {
700 builder.field("local", &addr);
707 /// Creates a new `UnixListener` bound to the specified socket.
712 /// use std::os::unix::net::UnixListener;
714 /// let listener = match UnixListener::bind("/path/to/the/socket") {
715 /// Ok(sock) => sock,
717 /// println!("Couldn't connect: {:?}", e);
722 #[stable(feature = "unix_socket", since = "1.10.0")]
723 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
724 fn inner(path: &Path) -> io::Result<UnixListener> {
726 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
727 let (addr, len) = sockaddr_un(path)?;
729 cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len as _))?;
730 cvt(libc::listen(*inner.as_inner(), 128))?;
732 Ok(UnixListener(inner))
738 /// Accepts a new incoming connection to this listener.
740 /// This function will block the calling thread until a new Unix connection
741 /// is established. When established, the corresponding [`UnixStream`] and
742 /// the remote peer's address will be returned.
744 /// [`UnixStream`]: ../../../../std/os/unix/net/struct.UnixStream.html
749 /// use std::os::unix::net::UnixListener;
751 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
753 /// match listener.accept() {
754 /// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
755 /// Err(e) => println!("accept function failed: {:?}", e),
758 #[stable(feature = "unix_socket", since = "1.10.0")]
759 pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
760 let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
761 let mut len = mem::size_of_val(&storage) as libc::socklen_t;
762 let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
763 let addr = SocketAddr::from_parts(storage, len)?;
764 Ok((UnixStream(sock), addr))
767 /// Creates a new independently owned handle to the underlying socket.
769 /// The returned `UnixListener` is a reference to the same socket that this
770 /// object references. Both handles can be used to accept incoming
771 /// connections and options set on one listener will affect the other.
776 /// use std::os::unix::net::UnixListener;
778 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
780 /// let listener_copy = listener.try_clone().expect("try_clone failed");
782 #[stable(feature = "unix_socket", since = "1.10.0")]
783 pub fn try_clone(&self) -> io::Result<UnixListener> {
784 self.0.duplicate().map(UnixListener)
787 /// Returns the local socket address of this listener.
792 /// use std::os::unix::net::UnixListener;
794 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
796 /// let addr = listener.local_addr().expect("Couldn't get local address");
798 #[stable(feature = "unix_socket", since = "1.10.0")]
799 pub fn local_addr(&self) -> io::Result<SocketAddr> {
800 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
803 /// Moves the socket into or out of nonblocking mode.
808 /// use std::os::unix::net::UnixListener;
810 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
812 /// listener.set_nonblocking(true).expect("Couldn't set non blocking");
814 #[stable(feature = "unix_socket", since = "1.10.0")]
815 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
816 self.0.set_nonblocking(nonblocking)
819 /// Returns the value of the `SO_ERROR` option.
824 /// use std::os::unix::net::UnixListener;
826 /// let listener = UnixListener::bind("/tmp/sock").unwrap();
828 /// if let Ok(Some(err)) = listener.take_error() {
829 /// println!("Got error: {:?}", err);
833 /// # Platform specific
834 /// On Redox this always returns `None`.
835 #[stable(feature = "unix_socket", since = "1.10.0")]
836 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
840 /// Returns an iterator over incoming connections.
842 /// The iterator will never return [`None`] and will also not yield the
843 /// peer's [`SocketAddr`] structure.
845 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
846 /// [`SocketAddr`]: struct.SocketAddr.html
852 /// use std::os::unix::net::{UnixStream, UnixListener};
854 /// fn handle_client(stream: UnixStream) {
858 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
860 /// for stream in listener.incoming() {
863 /// thread::spawn(|| handle_client(stream));
871 #[stable(feature = "unix_socket", since = "1.10.0")]
872 pub fn incoming<'a>(&'a self) -> Incoming<'a> {
873 Incoming { listener: self }
877 #[stable(feature = "unix_socket", since = "1.10.0")]
878 impl AsRawFd for UnixListener {
879 fn as_raw_fd(&self) -> RawFd {
884 #[stable(feature = "unix_socket", since = "1.10.0")]
885 impl FromRawFd for UnixListener {
886 unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
887 UnixListener(Socket::from_inner(fd))
891 #[stable(feature = "unix_socket", since = "1.10.0")]
892 impl IntoRawFd for UnixListener {
893 fn into_raw_fd(self) -> RawFd {
898 #[stable(feature = "unix_socket", since = "1.10.0")]
899 impl<'a> IntoIterator for &'a UnixListener {
900 type Item = io::Result<UnixStream>;
901 type IntoIter = Incoming<'a>;
903 fn into_iter(self) -> Incoming<'a> {
908 /// An iterator over incoming connections to a [`UnixListener`].
910 /// It will never return [`None`].
912 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
913 /// [`UnixListener`]: struct.UnixListener.html
919 /// use std::os::unix::net::{UnixStream, UnixListener};
921 /// fn handle_client(stream: UnixStream) {
925 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
927 /// for stream in listener.incoming() {
930 /// thread::spawn(|| handle_client(stream));
939 #[stable(feature = "unix_socket", since = "1.10.0")]
940 pub struct Incoming<'a> {
941 listener: &'a UnixListener,
944 #[stable(feature = "unix_socket", since = "1.10.0")]
945 impl<'a> Iterator for Incoming<'a> {
946 type Item = io::Result<UnixStream>;
948 fn next(&mut self) -> Option<io::Result<UnixStream>> {
949 Some(self.listener.accept().map(|s| s.0))
952 fn size_hint(&self) -> (usize, Option<usize>) {
953 (usize::max_value(), None)
957 /// A Unix datagram socket.
962 /// use std::os::unix::net::UnixDatagram;
964 /// let socket = UnixDatagram::bind("/path/to/my/socket").unwrap();
965 /// socket.send_to(b"hello world", "/path/to/other/socket").unwrap();
966 /// let mut buf = [0; 100];
967 /// let (count, address) = socket.recv_from(&mut buf).unwrap();
968 /// println!("socket {:?} sent {:?}", address, &buf[..count]);
970 #[stable(feature = "unix_socket", since = "1.10.0")]
971 pub struct UnixDatagram(Socket);
973 #[stable(feature = "unix_socket", since = "1.10.0")]
974 impl fmt::Debug for UnixDatagram {
975 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
976 let mut builder = fmt.debug_struct("UnixDatagram");
977 builder.field("fd", self.0.as_inner());
978 if let Ok(addr) = self.local_addr() {
979 builder.field("local", &addr);
981 if let Ok(addr) = self.peer_addr() {
982 builder.field("peer", &addr);
989 /// Creates a Unix datagram socket bound to the given path.
994 /// use std::os::unix::net::UnixDatagram;
996 /// let sock = match UnixDatagram::bind("/path/to/the/socket") {
997 /// Ok(sock) => sock,
999 /// println!("Couldn't bind: {:?}", e);
1004 #[stable(feature = "unix_socket", since = "1.10.0")]
1005 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
1006 fn inner(path: &Path) -> io::Result<UnixDatagram> {
1008 let socket = UnixDatagram::unbound()?;
1009 let (addr, len) = sockaddr_un(path)?;
1011 cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len as _))?;
1016 inner(path.as_ref())
1019 /// Creates a Unix Datagram socket which is not bound to any address.
1024 /// use std::os::unix::net::UnixDatagram;
1026 /// let sock = match UnixDatagram::unbound() {
1027 /// Ok(sock) => sock,
1029 /// println!("Couldn't unbound: {:?}", e);
1034 #[stable(feature = "unix_socket", since = "1.10.0")]
1035 pub fn unbound() -> io::Result<UnixDatagram> {
1036 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1037 Ok(UnixDatagram(inner))
1040 /// Creates an unnamed pair of connected sockets.
1042 /// Returns two `UnixDatagrams`s which are connected to each other.
1047 /// use std::os::unix::net::UnixDatagram;
1049 /// let (sock1, sock2) = match UnixDatagram::pair() {
1050 /// Ok((sock1, sock2)) => (sock1, sock2),
1052 /// println!("Couldn't unbound: {:?}", e);
1057 #[stable(feature = "unix_socket", since = "1.10.0")]
1058 pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
1059 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1060 Ok((UnixDatagram(i1), UnixDatagram(i2)))
1063 /// Connects the socket to the specified address.
1065 /// The [`send`] method may be used to send data to the specified address.
1066 /// [`recv`] and [`recv_from`] will only receive data from that address.
1068 /// [`send`]: #method.send
1069 /// [`recv`]: #method.recv
1070 /// [`recv_from`]: #method.recv_from
1075 /// use std::os::unix::net::UnixDatagram;
1077 /// let sock = UnixDatagram::unbound().unwrap();
1078 /// match sock.connect("/path/to/the/socket") {
1079 /// Ok(sock) => sock,
1081 /// println!("Couldn't connect: {:?}", e);
1086 #[stable(feature = "unix_socket", since = "1.10.0")]
1087 pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1088 fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
1090 let (addr, len) = sockaddr_un(path)?;
1092 cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
1097 inner(self, path.as_ref())
1100 /// Creates a new independently owned handle to the underlying socket.
1102 /// The returned `UnixDatagram` is a reference to the same socket that this
1103 /// object references. Both handles can be used to accept incoming
1104 /// connections and options set on one side will affect the other.
1109 /// use std::os::unix::net::UnixDatagram;
1111 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1113 /// let sock_copy = sock.try_clone().expect("try_clone failed");
1115 #[stable(feature = "unix_socket", since = "1.10.0")]
1116 pub fn try_clone(&self) -> io::Result<UnixDatagram> {
1117 self.0.duplicate().map(UnixDatagram)
1120 /// Returns the address of this socket.
1125 /// use std::os::unix::net::UnixDatagram;
1127 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1129 /// let addr = sock.local_addr().expect("Couldn't get local address");
1131 #[stable(feature = "unix_socket", since = "1.10.0")]
1132 pub fn local_addr(&self) -> io::Result<SocketAddr> {
1133 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
1136 /// Returns the address of this socket's peer.
1138 /// The [`connect`] method will connect the socket to a peer.
1140 /// [`connect`]: #method.connect
1145 /// use std::os::unix::net::UnixDatagram;
1147 /// let sock = UnixDatagram::unbound().unwrap();
1148 /// sock.connect("/path/to/the/socket").unwrap();
1150 /// let addr = sock.peer_addr().expect("Couldn't get peer address");
1152 #[stable(feature = "unix_socket", since = "1.10.0")]
1153 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
1154 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
1157 /// Receives data from the socket.
1159 /// On success, returns the number of bytes read and the address from
1160 /// whence the data came.
1165 /// use std::os::unix::net::UnixDatagram;
1167 /// let sock = UnixDatagram::unbound().unwrap();
1168 /// let mut buf = vec![0; 10];
1169 /// match sock.recv_from(buf.as_mut_slice()) {
1170 /// Ok((size, sender)) => println!("received {} bytes from {:?}", size, sender),
1171 /// Err(e) => println!("recv_from function failed: {:?}", e),
1174 #[stable(feature = "unix_socket", since = "1.10.0")]
1175 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
1177 let addr = SocketAddr::new(|addr, len| {
1179 count = libc::recvfrom(*self.0.as_inner(),
1180 buf.as_mut_ptr() as *mut _,
1187 } else if count == 0 {
1195 Ok((count as usize, addr))
1198 /// Receives data from the socket.
1200 /// On success, returns the number of bytes read.
1205 /// use std::os::unix::net::UnixDatagram;
1207 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1208 /// let mut buf = vec![0; 10];
1209 /// sock.recv(buf.as_mut_slice()).expect("recv function failed");
1211 #[stable(feature = "unix_socket", since = "1.10.0")]
1212 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
1216 /// Sends data on the socket to the specified address.
1218 /// On success, returns the number of bytes written.
1223 /// use std::os::unix::net::UnixDatagram;
1225 /// let sock = UnixDatagram::unbound().unwrap();
1226 /// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
1228 #[stable(feature = "unix_socket", since = "1.10.0")]
1229 pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
1230 fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
1232 let (addr, len) = sockaddr_un(path)?;
1234 let count = cvt(libc::sendto(*d.0.as_inner(),
1235 buf.as_ptr() as *const _,
1238 &addr as *const _ as *const _,
1243 inner(self, buf, path.as_ref())
1246 /// Sends data on the socket to the socket's peer.
1248 /// The peer address may be set by the `connect` method, and this method
1249 /// will return an error if the socket has not already been connected.
1251 /// On success, returns the number of bytes written.
1256 /// use std::os::unix::net::UnixDatagram;
1258 /// let sock = UnixDatagram::unbound().unwrap();
1259 /// sock.connect("/some/sock").expect("Couldn't connect");
1260 /// sock.send(b"omelette au fromage").expect("send_to function failed");
1262 #[stable(feature = "unix_socket", since = "1.10.0")]
1263 pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
1267 /// Sets the read timeout for the socket.
1269 /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
1270 /// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
1271 /// is passed to this method.
1273 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1274 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
1275 /// [`recv`]: #method.recv
1276 /// [`recv_from`]: #method.recv_from
1277 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1282 /// use std::os::unix::net::UnixDatagram;
1283 /// use std::time::Duration;
1285 /// let sock = UnixDatagram::unbound().unwrap();
1286 /// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
1289 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1294 /// use std::os::unix::net::UnixDatagram;
1295 /// use std::time::Duration;
1297 /// let socket = UnixDatagram::unbound().unwrap();
1298 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
1299 /// let err = result.unwrap_err();
1300 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1302 #[stable(feature = "unix_socket", since = "1.10.0")]
1303 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1304 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
1307 /// Sets the write timeout for the socket.
1309 /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
1310 /// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
1313 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1314 /// [`send`]: #method.send
1315 /// [`send_to`]: #method.send_to
1316 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1321 /// use std::os::unix::net::UnixDatagram;
1322 /// use std::time::Duration;
1324 /// let sock = UnixDatagram::unbound().unwrap();
1325 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1326 /// .expect("set_write_timeout function failed");
1329 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1334 /// use std::os::unix::net::UnixDatagram;
1335 /// use std::time::Duration;
1337 /// let socket = UnixDatagram::unbound().unwrap();
1338 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
1339 /// let err = result.unwrap_err();
1340 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1342 #[stable(feature = "unix_socket", since = "1.10.0")]
1343 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1344 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
1347 /// Returns the read timeout of this socket.
1352 /// use std::os::unix::net::UnixDatagram;
1353 /// use std::time::Duration;
1355 /// let sock = UnixDatagram::unbound().unwrap();
1356 /// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
1357 /// assert_eq!(sock.read_timeout().unwrap(), Some(Duration::new(1, 0)));
1359 #[stable(feature = "unix_socket", since = "1.10.0")]
1360 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
1361 self.0.timeout(libc::SO_RCVTIMEO)
1364 /// Returns the write timeout of this socket.
1369 /// use std::os::unix::net::UnixDatagram;
1370 /// use std::time::Duration;
1372 /// let sock = UnixDatagram::unbound().unwrap();
1373 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1374 /// .expect("set_write_timeout function failed");
1375 /// assert_eq!(sock.write_timeout().unwrap(), Some(Duration::new(1, 0)));
1377 #[stable(feature = "unix_socket", since = "1.10.0")]
1378 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
1379 self.0.timeout(libc::SO_SNDTIMEO)
1382 /// Moves the socket into or out of nonblocking mode.
1387 /// use std::os::unix::net::UnixDatagram;
1389 /// let sock = UnixDatagram::unbound().unwrap();
1390 /// sock.set_nonblocking(true).expect("set_nonblocking function failed");
1392 #[stable(feature = "unix_socket", since = "1.10.0")]
1393 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
1394 self.0.set_nonblocking(nonblocking)
1397 /// Returns the value of the `SO_ERROR` option.
1402 /// use std::os::unix::net::UnixDatagram;
1404 /// let sock = UnixDatagram::unbound().unwrap();
1405 /// if let Ok(Some(err)) = sock.take_error() {
1406 /// println!("Got error: {:?}", err);
1409 #[stable(feature = "unix_socket", since = "1.10.0")]
1410 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
1414 /// Shut down the read, write, or both halves of this connection.
1416 /// This function will cause all pending and future I/O calls on the
1417 /// specified portions to immediately return with an appropriate value
1418 /// (see the documentation of [`Shutdown`]).
1420 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
1423 /// use std::os::unix::net::UnixDatagram;
1424 /// use std::net::Shutdown;
1426 /// let sock = UnixDatagram::unbound().unwrap();
1427 /// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
1429 #[stable(feature = "unix_socket", since = "1.10.0")]
1430 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
1431 self.0.shutdown(how)
1435 #[stable(feature = "unix_socket", since = "1.10.0")]
1436 impl AsRawFd for UnixDatagram {
1437 fn as_raw_fd(&self) -> RawFd {
1442 #[stable(feature = "unix_socket", since = "1.10.0")]
1443 impl FromRawFd for UnixDatagram {
1444 unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
1445 UnixDatagram(Socket::from_inner(fd))
1449 #[stable(feature = "unix_socket", since = "1.10.0")]
1450 impl IntoRawFd for UnixDatagram {
1451 fn into_raw_fd(self) -> RawFd {
1456 #[cfg(all(test, not(target_os = "emscripten")))]
1459 use crate::io::{self, ErrorKind};
1460 use crate::io::prelude::*;
1461 use crate::time::Duration;
1462 use crate::sys_common::io::test::tmpdir;
1466 macro_rules! or_panic {
1470 Err(e) => panic!("{}", e),
1478 let socket_path = dir.path().join("sock");
1479 let msg1 = b"hello";
1480 let msg2 = b"world!";
1482 let listener = or_panic!(UnixListener::bind(&socket_path));
1483 let thread = thread::spawn(move || {
1484 let mut stream = or_panic!(listener.accept()).0;
1485 let mut buf = [0; 5];
1486 or_panic!(stream.read(&mut buf));
1487 assert_eq!(&msg1[..], &buf[..]);
1488 or_panic!(stream.write_all(msg2));
1491 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1492 assert_eq!(Some(&*socket_path),
1493 stream.peer_addr().unwrap().as_pathname());
1494 or_panic!(stream.write_all(msg1));
1495 let mut buf = vec![];
1496 or_panic!(stream.read_to_end(&mut buf));
1497 assert_eq!(&msg2[..], &buf[..]);
1500 thread.join().unwrap();
1505 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
1507 let len = or_panic!(s1.write_vectored(
1508 &[IoSlice::new(b"hello"), IoSlice::new(b" "), IoSlice::new(b"world!")],
1510 assert_eq!(len, 12);
1512 let mut buf1 = [0; 6];
1513 let mut buf2 = [0; 7];
1514 let len = or_panic!(s2.read_vectored(
1515 &mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)],
1517 assert_eq!(len, 12);
1518 assert_eq!(&buf1, b"hello ");
1519 assert_eq!(&buf2, b"world!\0");
1524 let msg1 = b"hello";
1525 let msg2 = b"world!";
1527 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
1528 let thread = thread::spawn(move || {
1529 // s1 must be moved in or the test will hang!
1530 let mut buf = [0; 5];
1531 or_panic!(s1.read(&mut buf));
1532 assert_eq!(&msg1[..], &buf[..]);
1533 or_panic!(s1.write_all(msg2));
1536 or_panic!(s2.write_all(msg1));
1537 let mut buf = vec![];
1538 or_panic!(s2.read_to_end(&mut buf));
1539 assert_eq!(&msg2[..], &buf[..]);
1542 thread.join().unwrap();
1548 let socket_path = dir.path().join("sock");
1549 let msg1 = b"hello";
1550 let msg2 = b"world";
1552 let listener = or_panic!(UnixListener::bind(&socket_path));
1553 let thread = thread::spawn(move || {
1554 let mut stream = or_panic!(listener.accept()).0;
1555 or_panic!(stream.write_all(msg1));
1556 or_panic!(stream.write_all(msg2));
1559 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1560 let mut stream2 = or_panic!(stream.try_clone());
1562 let mut buf = [0; 5];
1563 or_panic!(stream.read(&mut buf));
1564 assert_eq!(&msg1[..], &buf[..]);
1565 or_panic!(stream2.read(&mut buf));
1566 assert_eq!(&msg2[..], &buf[..]);
1568 thread.join().unwrap();
1574 let socket_path = dir.path().join("sock");
1576 let listener = or_panic!(UnixListener::bind(&socket_path));
1577 let thread = thread::spawn(move || {
1578 for stream in listener.incoming().take(2) {
1579 let mut stream = or_panic!(stream);
1581 or_panic!(stream.read(&mut buf));
1586 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1587 or_panic!(stream.write_all(&[0]));
1590 thread.join().unwrap();
1596 let socket_path = dir.path()
1597 .join("asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa\
1598 sasdfasdfasdasdfasdfasdfadfasdfasdfasdfasdfasdf");
1599 match UnixStream::connect(&socket_path) {
1600 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1601 Err(e) => panic!("unexpected error {}", e),
1602 Ok(_) => panic!("unexpected success"),
1605 match UnixListener::bind(&socket_path) {
1606 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1607 Err(e) => panic!("unexpected error {}", e),
1608 Ok(_) => panic!("unexpected success"),
1611 match UnixDatagram::bind(&socket_path) {
1612 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1613 Err(e) => panic!("unexpected error {}", e),
1614 Ok(_) => panic!("unexpected success"),
1621 let socket_path = dir.path().join("sock");
1623 let _listener = or_panic!(UnixListener::bind(&socket_path));
1625 let stream = or_panic!(UnixStream::connect(&socket_path));
1626 let dur = Duration::new(15410, 0);
1628 assert_eq!(None, or_panic!(stream.read_timeout()));
1630 or_panic!(stream.set_read_timeout(Some(dur)));
1631 assert_eq!(Some(dur), or_panic!(stream.read_timeout()));
1633 assert_eq!(None, or_panic!(stream.write_timeout()));
1635 or_panic!(stream.set_write_timeout(Some(dur)));
1636 assert_eq!(Some(dur), or_panic!(stream.write_timeout()));
1638 or_panic!(stream.set_read_timeout(None));
1639 assert_eq!(None, or_panic!(stream.read_timeout()));
1641 or_panic!(stream.set_write_timeout(None));
1642 assert_eq!(None, or_panic!(stream.write_timeout()));
1646 fn test_read_timeout() {
1648 let socket_path = dir.path().join("sock");
1650 let _listener = or_panic!(UnixListener::bind(&socket_path));
1652 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1653 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1655 let mut buf = [0; 10];
1656 let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
1657 assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
1658 "unexpected_error: {:?}", kind);
1662 fn test_read_with_timeout() {
1664 let socket_path = dir.path().join("sock");
1666 let listener = or_panic!(UnixListener::bind(&socket_path));
1668 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1669 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1671 let mut other_end = or_panic!(listener.accept()).0;
1672 or_panic!(other_end.write_all(b"hello world"));
1674 let mut buf = [0; 11];
1675 or_panic!(stream.read(&mut buf));
1676 assert_eq!(b"hello world", &buf[..]);
1678 let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
1679 assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
1680 "unexpected_error: {:?}", kind);
1683 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
1684 // when passed zero Durations
1686 fn test_unix_stream_timeout_zero_duration() {
1688 let socket_path = dir.path().join("sock");
1690 let listener = or_panic!(UnixListener::bind(&socket_path));
1691 let stream = or_panic!(UnixStream::connect(&socket_path));
1693 let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
1694 let err = result.unwrap_err();
1695 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1697 let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
1698 let err = result.unwrap_err();
1699 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1705 fn test_unix_datagram() {
1707 let path1 = dir.path().join("sock1");
1708 let path2 = dir.path().join("sock2");
1710 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1711 let sock2 = or_panic!(UnixDatagram::bind(&path2));
1713 let msg = b"hello world";
1714 or_panic!(sock1.send_to(msg, &path2));
1715 let mut buf = [0; 11];
1716 or_panic!(sock2.recv_from(&mut buf));
1717 assert_eq!(msg, &buf[..]);
1721 fn test_unnamed_unix_datagram() {
1723 let path1 = dir.path().join("sock1");
1725 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1726 let sock2 = or_panic!(UnixDatagram::unbound());
1728 let msg = b"hello world";
1729 or_panic!(sock2.send_to(msg, &path1));
1730 let mut buf = [0; 11];
1731 let (usize, addr) = or_panic!(sock1.recv_from(&mut buf));
1732 assert_eq!(usize, 11);
1733 assert!(addr.is_unnamed());
1734 assert_eq!(msg, &buf[..]);
1738 fn test_connect_unix_datagram() {
1740 let path1 = dir.path().join("sock1");
1741 let path2 = dir.path().join("sock2");
1743 let bsock1 = or_panic!(UnixDatagram::bind(&path1));
1744 let bsock2 = or_panic!(UnixDatagram::bind(&path2));
1745 let sock = or_panic!(UnixDatagram::unbound());
1746 or_panic!(sock.connect(&path1));
1749 let msg = b"hello there";
1750 or_panic!(sock.send(msg));
1751 let mut buf = [0; 11];
1752 let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
1753 assert_eq!(usize, 11);
1754 assert!(addr.is_unnamed());
1755 assert_eq!(msg, &buf[..]);
1757 // Changing default socket works too
1758 or_panic!(sock.connect(&path2));
1759 or_panic!(sock.send(msg));
1760 or_panic!(bsock2.recv_from(&mut buf));
1764 fn test_unix_datagram_recv() {
1766 let path1 = dir.path().join("sock1");
1768 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1769 let sock2 = or_panic!(UnixDatagram::unbound());
1770 or_panic!(sock2.connect(&path1));
1772 let msg = b"hello world";
1773 or_panic!(sock2.send(msg));
1774 let mut buf = [0; 11];
1775 let size = or_panic!(sock1.recv(&mut buf));
1776 assert_eq!(size, 11);
1777 assert_eq!(msg, &buf[..]);
1781 fn datagram_pair() {
1782 let msg1 = b"hello";
1783 let msg2 = b"world!";
1785 let (s1, s2) = or_panic!(UnixDatagram::pair());
1786 let thread = thread::spawn(move || {
1787 // s1 must be moved in or the test will hang!
1788 let mut buf = [0; 5];
1789 or_panic!(s1.recv(&mut buf));
1790 assert_eq!(&msg1[..], &buf[..]);
1791 or_panic!(s1.send(msg2));
1794 or_panic!(s2.send(msg1));
1795 let mut buf = [0; 6];
1796 or_panic!(s2.recv(&mut buf));
1797 assert_eq!(&msg2[..], &buf[..]);
1800 thread.join().unwrap();
1803 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
1804 // when passed zero Durations
1806 fn test_unix_datagram_timeout_zero_duration() {
1808 let path = dir.path().join("sock");
1810 let datagram = or_panic!(UnixDatagram::bind(&path));
1812 let result = datagram.set_write_timeout(Some(Duration::new(0, 0)));
1813 let err = result.unwrap_err();
1814 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1816 let result = datagram.set_read_timeout(Some(Duration::new(0, 0)));
1817 let err = result.unwrap_err();
1818 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1822 fn abstract_namespace_not_allowed() {
1823 assert!(UnixStream::connect("\0asdf").is_err());