1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![stable(feature = "unix_socket", since = "1.10.0")]
13 //! Unix-specific networking functionality
18 // FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
22 pub type socklen_t = u32;
25 pub struct sockaddr_un;
31 use io::{self, Initializer};
33 use net::{self, Shutdown};
34 use os::unix::ffi::OsStrExt;
35 use os::unix::io::{RawFd, AsRawFd, FromRawFd, IntoRawFd};
40 use sys_common::{self, AsInner, FromInner, IntoInner};
42 #[cfg(any(target_os = "linux", target_os = "android",
43 target_os = "dragonfly", target_os = "freebsd",
44 target_os = "openbsd", target_os = "netbsd",
45 target_os = "haiku", target_os = "bitrig"))]
46 use libc::MSG_NOSIGNAL;
47 #[cfg(not(any(target_os = "linux", target_os = "android",
48 target_os = "dragonfly", target_os = "freebsd",
49 target_os = "openbsd", target_os = "netbsd",
50 target_os = "haiku", target_os = "bitrig")))]
51 const MSG_NOSIGNAL: libc::c_int = 0x0;
53 fn sun_path_offset() -> usize {
54 // Work with an actual instance of the type since using a null pointer is UB
55 let addr: libc::sockaddr_un = unsafe { mem::uninitialized() };
56 let base = &addr as *const _ as usize;
57 let path = &addr.sun_path as *const _ as usize;
61 unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
62 let mut addr: libc::sockaddr_un = mem::zeroed();
63 addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
65 let bytes = path.as_os_str().as_bytes();
67 if bytes.contains(&0) {
68 return Err(io::Error::new(io::ErrorKind::InvalidInput,
69 "paths may not contain interior null bytes"));
72 if bytes.len() >= addr.sun_path.len() {
73 return Err(io::Error::new(io::ErrorKind::InvalidInput,
74 "path must be shorter than SUN_LEN"));
76 for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
77 *dst = *src as libc::c_char;
79 // null byte for pathname addresses is already there because we zeroed the
82 let mut len = sun_path_offset() + bytes.len();
87 Ok((addr, len as libc::socklen_t))
90 enum AddressKind<'a> {
96 /// An address associated with a Unix socket.
101 /// use std::os::unix::net::UnixListener;
103 /// let socket = match UnixListener::bind("/tmp/sock") {
104 /// Ok(sock) => sock,
106 /// println!("Couldn't bind: {:?}", e);
110 /// let addr = socket.local_addr().expect("Couldn't get local address");
113 #[stable(feature = "unix_socket", since = "1.10.0")]
114 pub struct SocketAddr {
115 addr: libc::sockaddr_un,
116 len: libc::socklen_t,
120 fn new<F>(f: F) -> io::Result<SocketAddr>
121 where F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int
124 let mut addr: libc::sockaddr_un = mem::zeroed();
125 let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
126 cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
127 SocketAddr::from_parts(addr, len)
131 fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result<SocketAddr> {
133 // When there is a datagram from unnamed unix socket
134 // linux returns zero bytes of address
135 len = sun_path_offset() as libc::socklen_t; // i.e., zero-length address
136 } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
137 return Err(io::Error::new(io::ErrorKind::InvalidInput,
138 "file descriptor did not correspond to a Unix socket"));
147 /// Returns true if and only if the address is unnamed.
154 /// use std::os::unix::net::UnixListener;
156 /// let socket = UnixListener::bind("/tmp/sock").unwrap();
157 /// let addr = socket.local_addr().expect("Couldn't get local address");
158 /// assert_eq!(addr.is_unnamed(), false);
161 /// An unnamed address:
164 /// use std::os::unix::net::UnixDatagram;
166 /// let socket = UnixDatagram::unbound().unwrap();
167 /// let addr = socket.local_addr().expect("Couldn't get local address");
168 /// assert_eq!(addr.is_unnamed(), true);
170 #[stable(feature = "unix_socket", since = "1.10.0")]
171 pub fn is_unnamed(&self) -> bool {
172 if let AddressKind::Unnamed = self.address() {
179 /// Returns the contents of this address if it is a `pathname` address.
186 /// use std::os::unix::net::UnixListener;
187 /// use std::path::Path;
189 /// let socket = UnixListener::bind("/tmp/sock").unwrap();
190 /// let addr = socket.local_addr().expect("Couldn't get local address");
191 /// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
194 /// Without a pathname:
197 /// use std::os::unix::net::UnixDatagram;
199 /// let socket = UnixDatagram::unbound().unwrap();
200 /// let addr = socket.local_addr().expect("Couldn't get local address");
201 /// assert_eq!(addr.as_pathname(), None);
203 #[stable(feature = "unix_socket", since = "1.10.0")]
204 pub fn as_pathname(&self) -> Option<&Path> {
205 if let AddressKind::Pathname(path) = self.address() {
212 fn address<'a>(&'a self) -> AddressKind<'a> {
213 let len = self.len as usize - sun_path_offset();
214 let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
216 // macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
218 || (cfg!(not(any(target_os = "linux", target_os = "android")))
219 && self.addr.sun_path[0] == 0)
222 } else if self.addr.sun_path[0] == 0 {
223 AddressKind::Abstract(&path[1..len])
225 AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
230 #[stable(feature = "unix_socket", since = "1.10.0")]
231 impl fmt::Debug for SocketAddr {
232 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
233 match self.address() {
234 AddressKind::Unnamed => write!(fmt, "(unnamed)"),
235 AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
236 AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
241 struct AsciiEscaped<'a>(&'a [u8]);
243 impl<'a> fmt::Display for AsciiEscaped<'a> {
244 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
246 for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
247 write!(fmt, "{}", byte as char)?;
253 /// A Unix stream socket.
258 /// use std::os::unix::net::UnixStream;
259 /// use std::io::prelude::*;
261 /// let mut stream = UnixStream::connect("/path/to/my/socket").unwrap();
262 /// stream.write_all(b"hello world").unwrap();
263 /// let mut response = String::new();
264 /// stream.read_to_string(&mut response).unwrap();
265 /// println!("{}", response);
267 #[stable(feature = "unix_socket", since = "1.10.0")]
268 pub struct UnixStream(Socket);
270 #[stable(feature = "unix_socket", since = "1.10.0")]
271 impl fmt::Debug for UnixStream {
272 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
273 let mut builder = fmt.debug_struct("UnixStream");
274 builder.field("fd", self.0.as_inner());
275 if let Ok(addr) = self.local_addr() {
276 builder.field("local", &addr);
278 if let Ok(addr) = self.peer_addr() {
279 builder.field("peer", &addr);
286 /// Connects to the socket named by `path`.
291 /// use std::os::unix::net::UnixStream;
293 /// let socket = match UnixStream::connect("/tmp/sock") {
294 /// Ok(sock) => sock,
296 /// println!("Couldn't connect: {:?}", e);
301 #[stable(feature = "unix_socket", since = "1.10.0")]
302 pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
303 fn inner(path: &Path) -> io::Result<UnixStream> {
305 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
306 let (addr, len) = sockaddr_un(path)?;
308 cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
309 Ok(UnixStream(inner))
315 /// Creates an unnamed pair of connected sockets.
317 /// Returns two `UnixStream`s which are connected to each other.
322 /// use std::os::unix::net::UnixStream;
324 /// let (sock1, sock2) = match UnixStream::pair() {
325 /// Ok((sock1, sock2)) => (sock1, sock2),
327 /// println!("Couldn't create a pair of sockets: {:?}", e);
332 #[stable(feature = "unix_socket", since = "1.10.0")]
333 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
334 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
335 Ok((UnixStream(i1), UnixStream(i2)))
338 /// Creates a new independently owned handle to the underlying socket.
340 /// The returned `UnixStream` is a reference to the same stream that this
341 /// object references. Both handles will read and write the same stream of
342 /// data, and options set on one stream will be propagated to the other
348 /// use std::os::unix::net::UnixStream;
350 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
351 /// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
353 #[stable(feature = "unix_socket", since = "1.10.0")]
354 pub fn try_clone(&self) -> io::Result<UnixStream> {
355 self.0.duplicate().map(UnixStream)
358 /// Returns the socket address of the local half of this connection.
363 /// use std::os::unix::net::UnixStream;
365 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
366 /// let addr = socket.local_addr().expect("Couldn't get local address");
368 #[stable(feature = "unix_socket", since = "1.10.0")]
369 pub fn local_addr(&self) -> io::Result<SocketAddr> {
370 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
373 /// Returns the socket address of the remote half of this connection.
378 /// use std::os::unix::net::UnixStream;
380 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
381 /// let addr = socket.peer_addr().expect("Couldn't get peer address");
383 #[stable(feature = "unix_socket", since = "1.10.0")]
384 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
385 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
388 /// Sets the read timeout for the socket.
390 /// If the provided value is [`None`], then [`read`] calls will block
391 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
394 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
395 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
396 /// [`read`]: ../../../../std/io/trait.Read.html#tymethod.read
397 /// [`Duration`]: ../../../../std/time/struct.Duration.html
402 /// use std::os::unix::net::UnixStream;
403 /// use std::time::Duration;
405 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
406 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
409 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
414 /// use std::os::unix::net::UnixStream;
415 /// use std::time::Duration;
417 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
418 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
419 /// let err = result.unwrap_err();
420 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
422 #[stable(feature = "unix_socket", since = "1.10.0")]
423 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
424 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
427 /// Sets the write timeout for the socket.
429 /// If the provided value is [`None`], then [`write`] calls will block
430 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
431 /// passed to this method.
433 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
434 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
435 /// [`write`]: ../../../../std/io/trait.Write.html#tymethod.write
436 /// [`Duration`]: ../../../../std/time/struct.Duration.html
441 /// use std::os::unix::net::UnixStream;
442 /// use std::time::Duration;
444 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
445 /// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
448 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
453 /// use std::net::UdpSocket;
454 /// use std::time::Duration;
456 /// let socket = UdpSocket::bind("127.0.0.1:34254").unwrap();
457 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
458 /// let err = result.unwrap_err();
459 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
461 #[stable(feature = "unix_socket", since = "1.10.0")]
462 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
463 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
466 /// Returns the read timeout of this socket.
471 /// use std::os::unix::net::UnixStream;
472 /// use std::time::Duration;
474 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
475 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
476 /// assert_eq!(socket.read_timeout().unwrap(), Some(Duration::new(1, 0)));
478 #[stable(feature = "unix_socket", since = "1.10.0")]
479 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
480 self.0.timeout(libc::SO_RCVTIMEO)
483 /// Returns the write timeout of this socket.
488 /// use std::os::unix::net::UnixStream;
489 /// use std::time::Duration;
491 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
492 /// socket.set_write_timeout(Some(Duration::new(1, 0))).expect("Couldn't set write timeout");
493 /// assert_eq!(socket.write_timeout().unwrap(), Some(Duration::new(1, 0)));
495 #[stable(feature = "unix_socket", since = "1.10.0")]
496 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
497 self.0.timeout(libc::SO_SNDTIMEO)
500 /// Moves the socket into or out of nonblocking mode.
505 /// use std::os::unix::net::UnixStream;
507 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
508 /// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
510 #[stable(feature = "unix_socket", since = "1.10.0")]
511 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
512 self.0.set_nonblocking(nonblocking)
515 /// Returns the value of the `SO_ERROR` option.
520 /// use std::os::unix::net::UnixStream;
522 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
523 /// if let Ok(Some(err)) = socket.take_error() {
524 /// println!("Got error: {:?}", err);
528 /// # Platform specific
529 /// On Redox this always returns None.
530 #[stable(feature = "unix_socket", since = "1.10.0")]
531 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
535 /// Shuts down the read, write, or both halves of this connection.
537 /// This function will cause all pending and future I/O calls on the
538 /// specified portions to immediately return with an appropriate value
539 /// (see the documentation of [`Shutdown`]).
541 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
546 /// use std::os::unix::net::UnixStream;
547 /// use std::net::Shutdown;
549 /// let socket = UnixStream::connect("/tmp/sock").unwrap();
550 /// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
552 #[stable(feature = "unix_socket", since = "1.10.0")]
553 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
558 #[stable(feature = "unix_socket", since = "1.10.0")]
559 impl io::Read for UnixStream {
560 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
561 io::Read::read(&mut &*self, buf)
565 unsafe fn initializer(&self) -> Initializer {
570 #[stable(feature = "unix_socket", since = "1.10.0")]
571 impl<'a> io::Read for &'a UnixStream {
572 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
577 unsafe fn initializer(&self) -> Initializer {
582 #[stable(feature = "unix_socket", since = "1.10.0")]
583 impl io::Write for UnixStream {
584 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
585 io::Write::write(&mut &*self, buf)
588 fn flush(&mut self) -> io::Result<()> {
589 io::Write::flush(&mut &*self)
593 #[stable(feature = "unix_socket", since = "1.10.0")]
594 impl<'a> io::Write for &'a UnixStream {
595 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
599 fn flush(&mut self) -> io::Result<()> {
604 #[stable(feature = "unix_socket", since = "1.10.0")]
605 impl AsRawFd for UnixStream {
606 fn as_raw_fd(&self) -> RawFd {
611 #[stable(feature = "unix_socket", since = "1.10.0")]
612 impl FromRawFd for UnixStream {
613 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
614 UnixStream(Socket::from_inner(fd))
618 #[stable(feature = "unix_socket", since = "1.10.0")]
619 impl IntoRawFd for UnixStream {
620 fn into_raw_fd(self) -> RawFd {
625 #[stable(feature = "rust1", since = "1.0.0")]
626 impl AsRawFd for net::TcpStream {
627 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
630 #[stable(feature = "rust1", since = "1.0.0")]
631 impl AsRawFd for net::TcpListener {
632 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
635 #[stable(feature = "rust1", since = "1.0.0")]
636 impl AsRawFd for net::UdpSocket {
637 fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
640 #[stable(feature = "from_raw_os", since = "1.1.0")]
641 impl FromRawFd for net::TcpStream {
642 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
643 let socket = sys::net::Socket::from_inner(fd);
644 net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
648 #[stable(feature = "from_raw_os", since = "1.1.0")]
649 impl FromRawFd for net::TcpListener {
650 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
651 let socket = sys::net::Socket::from_inner(fd);
652 net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
656 #[stable(feature = "from_raw_os", since = "1.1.0")]
657 impl FromRawFd for net::UdpSocket {
658 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
659 let socket = sys::net::Socket::from_inner(fd);
660 net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
664 #[stable(feature = "into_raw_os", since = "1.4.0")]
665 impl IntoRawFd for net::TcpStream {
666 fn into_raw_fd(self) -> RawFd {
667 self.into_inner().into_socket().into_inner()
670 #[stable(feature = "into_raw_os", since = "1.4.0")]
671 impl IntoRawFd for net::TcpListener {
672 fn into_raw_fd(self) -> RawFd {
673 self.into_inner().into_socket().into_inner()
676 #[stable(feature = "into_raw_os", since = "1.4.0")]
677 impl IntoRawFd for net::UdpSocket {
678 fn into_raw_fd(self) -> RawFd {
679 self.into_inner().into_socket().into_inner()
683 /// A structure representing a Unix domain socket server.
689 /// use std::os::unix::net::{UnixStream, UnixListener};
691 /// fn handle_client(stream: UnixStream) {
695 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
697 /// // accept connections and process them, spawning a new thread for each one
698 /// for stream in listener.incoming() {
701 /// /* connection succeeded */
702 /// thread::spawn(|| handle_client(stream));
705 /// /* connection failed */
711 #[stable(feature = "unix_socket", since = "1.10.0")]
712 pub struct UnixListener(Socket);
714 #[stable(feature = "unix_socket", since = "1.10.0")]
715 impl fmt::Debug for UnixListener {
716 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
717 let mut builder = fmt.debug_struct("UnixListener");
718 builder.field("fd", self.0.as_inner());
719 if let Ok(addr) = self.local_addr() {
720 builder.field("local", &addr);
727 /// Creates a new `UnixListener` bound to the specified socket.
732 /// use std::os::unix::net::UnixListener;
734 /// let listener = match UnixListener::bind("/path/to/the/socket") {
735 /// Ok(sock) => sock,
737 /// println!("Couldn't connect: {:?}", e);
742 #[stable(feature = "unix_socket", since = "1.10.0")]
743 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
744 fn inner(path: &Path) -> io::Result<UnixListener> {
746 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
747 let (addr, len) = sockaddr_un(path)?;
749 cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len as _))?;
750 cvt(libc::listen(*inner.as_inner(), 128))?;
752 Ok(UnixListener(inner))
758 /// Accepts a new incoming connection to this listener.
760 /// This function will block the calling thread until a new Unix connection
761 /// is established. When established, the corresponding [`UnixStream`] and
762 /// the remote peer's address will be returned.
764 /// [`UnixStream`]: ../../../../std/os/unix/net/struct.UnixStream.html
769 /// use std::os::unix::net::UnixListener;
771 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
773 /// match listener.accept() {
774 /// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
775 /// Err(e) => println!("accept function failed: {:?}", e),
778 #[stable(feature = "unix_socket", since = "1.10.0")]
779 pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
780 let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
781 let mut len = mem::size_of_val(&storage) as libc::socklen_t;
782 let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
783 let addr = SocketAddr::from_parts(storage, len)?;
784 Ok((UnixStream(sock), addr))
787 /// Creates a new independently owned handle to the underlying socket.
789 /// The returned `UnixListener` is a reference to the same socket that this
790 /// object references. Both handles can be used to accept incoming
791 /// connections and options set on one listener will affect the other.
796 /// use std::os::unix::net::UnixListener;
798 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
800 /// let listener_copy = listener.try_clone().expect("try_clone failed");
802 #[stable(feature = "unix_socket", since = "1.10.0")]
803 pub fn try_clone(&self) -> io::Result<UnixListener> {
804 self.0.duplicate().map(UnixListener)
807 /// Returns the local socket address of this listener.
812 /// use std::os::unix::net::UnixListener;
814 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
816 /// let addr = listener.local_addr().expect("Couldn't get local address");
818 #[stable(feature = "unix_socket", since = "1.10.0")]
819 pub fn local_addr(&self) -> io::Result<SocketAddr> {
820 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
823 /// Moves the socket into or out of nonblocking mode.
828 /// use std::os::unix::net::UnixListener;
830 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
832 /// listener.set_nonblocking(true).expect("Couldn't set non blocking");
834 #[stable(feature = "unix_socket", since = "1.10.0")]
835 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
836 self.0.set_nonblocking(nonblocking)
839 /// Returns the value of the `SO_ERROR` option.
844 /// use std::os::unix::net::UnixListener;
846 /// let listener = UnixListener::bind("/tmp/sock").unwrap();
848 /// if let Ok(Some(err)) = listener.take_error() {
849 /// println!("Got error: {:?}", err);
853 /// # Platform specific
854 /// On Redox this always returns None.
855 #[stable(feature = "unix_socket", since = "1.10.0")]
856 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
860 /// Returns an iterator over incoming connections.
862 /// The iterator will never return [`None`] and will also not yield the
863 /// peer's [`SocketAddr`] structure.
865 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
866 /// [`SocketAddr`]: struct.SocketAddr.html
872 /// use std::os::unix::net::{UnixStream, UnixListener};
874 /// fn handle_client(stream: UnixStream) {
878 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
880 /// for stream in listener.incoming() {
883 /// thread::spawn(|| handle_client(stream));
891 #[stable(feature = "unix_socket", since = "1.10.0")]
892 pub fn incoming<'a>(&'a self) -> Incoming<'a> {
893 Incoming { listener: self }
897 #[stable(feature = "unix_socket", since = "1.10.0")]
898 impl AsRawFd for UnixListener {
899 fn as_raw_fd(&self) -> RawFd {
904 #[stable(feature = "unix_socket", since = "1.10.0")]
905 impl FromRawFd for UnixListener {
906 unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
907 UnixListener(Socket::from_inner(fd))
911 #[stable(feature = "unix_socket", since = "1.10.0")]
912 impl IntoRawFd for UnixListener {
913 fn into_raw_fd(self) -> RawFd {
918 #[stable(feature = "unix_socket", since = "1.10.0")]
919 impl<'a> IntoIterator for &'a UnixListener {
920 type Item = io::Result<UnixStream>;
921 type IntoIter = Incoming<'a>;
923 fn into_iter(self) -> Incoming<'a> {
928 /// An iterator over incoming connections to a [`UnixListener`].
930 /// It will never return [`None`].
932 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
933 /// [`UnixListener`]: struct.UnixListener.html
939 /// use std::os::unix::net::{UnixStream, UnixListener};
941 /// fn handle_client(stream: UnixStream) {
945 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
947 /// for stream in listener.incoming() {
950 /// thread::spawn(|| handle_client(stream));
959 #[stable(feature = "unix_socket", since = "1.10.0")]
960 pub struct Incoming<'a> {
961 listener: &'a UnixListener,
964 #[stable(feature = "unix_socket", since = "1.10.0")]
965 impl<'a> Iterator for Incoming<'a> {
966 type Item = io::Result<UnixStream>;
968 fn next(&mut self) -> Option<io::Result<UnixStream>> {
969 Some(self.listener.accept().map(|s| s.0))
972 fn size_hint(&self) -> (usize, Option<usize>) {
973 (usize::max_value(), None)
977 /// A Unix datagram socket.
982 /// use std::os::unix::net::UnixDatagram;
984 /// let socket = UnixDatagram::bind("/path/to/my/socket").unwrap();
985 /// socket.send_to(b"hello world", "/path/to/other/socket").unwrap();
986 /// let mut buf = [0; 100];
987 /// let (count, address) = socket.recv_from(&mut buf).unwrap();
988 /// println!("socket {:?} sent {:?}", address, &buf[..count]);
990 #[stable(feature = "unix_socket", since = "1.10.0")]
991 pub struct UnixDatagram(Socket);
993 #[stable(feature = "unix_socket", since = "1.10.0")]
994 impl fmt::Debug for UnixDatagram {
995 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
996 let mut builder = fmt.debug_struct("UnixDatagram");
997 builder.field("fd", self.0.as_inner());
998 if let Ok(addr) = self.local_addr() {
999 builder.field("local", &addr);
1001 if let Ok(addr) = self.peer_addr() {
1002 builder.field("peer", &addr);
1009 /// Creates a Unix datagram socket bound to the given path.
1014 /// use std::os::unix::net::UnixDatagram;
1016 /// let sock = match UnixDatagram::bind("/path/to/the/socket") {
1017 /// Ok(sock) => sock,
1019 /// println!("Couldn't bind: {:?}", e);
1024 #[stable(feature = "unix_socket", since = "1.10.0")]
1025 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
1026 fn inner(path: &Path) -> io::Result<UnixDatagram> {
1028 let socket = UnixDatagram::unbound()?;
1029 let (addr, len) = sockaddr_un(path)?;
1031 cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len as _))?;
1036 inner(path.as_ref())
1039 /// Creates a Unix Datagram socket which is not bound to any address.
1044 /// use std::os::unix::net::UnixDatagram;
1046 /// let sock = match UnixDatagram::unbound() {
1047 /// Ok(sock) => sock,
1049 /// println!("Couldn't unbound: {:?}", e);
1054 #[stable(feature = "unix_socket", since = "1.10.0")]
1055 pub fn unbound() -> io::Result<UnixDatagram> {
1056 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1057 Ok(UnixDatagram(inner))
1060 /// Create an unnamed pair of connected sockets.
1062 /// Returns two `UnixDatagrams`s which are connected to each other.
1067 /// use std::os::unix::net::UnixDatagram;
1069 /// let (sock1, sock2) = match UnixDatagram::pair() {
1070 /// Ok((sock1, sock2)) => (sock1, sock2),
1072 /// println!("Couldn't unbound: {:?}", e);
1077 #[stable(feature = "unix_socket", since = "1.10.0")]
1078 pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
1079 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1080 Ok((UnixDatagram(i1), UnixDatagram(i2)))
1083 /// Connects the socket to the specified address.
1085 /// The [`send`] method may be used to send data to the specified address.
1086 /// [`recv`] and [`recv_from`] will only receive data from that address.
1088 /// [`send`]: #method.send
1089 /// [`recv`]: #method.recv
1090 /// [`recv_from`]: #method.recv_from
1095 /// use std::os::unix::net::UnixDatagram;
1097 /// let sock = UnixDatagram::unbound().unwrap();
1098 /// match sock.connect("/path/to/the/socket") {
1099 /// Ok(sock) => sock,
1101 /// println!("Couldn't connect: {:?}", e);
1106 #[stable(feature = "unix_socket", since = "1.10.0")]
1107 pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1108 fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
1110 let (addr, len) = sockaddr_un(path)?;
1112 cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
1117 inner(self, path.as_ref())
1120 /// Creates a new independently owned handle to the underlying socket.
1122 /// The returned `UnixDatagram` is a reference to the same socket that this
1123 /// object references. Both handles can be used to accept incoming
1124 /// connections and options set on one side will affect the other.
1129 /// use std::os::unix::net::UnixDatagram;
1131 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1133 /// let sock_copy = sock.try_clone().expect("try_clone failed");
1135 #[stable(feature = "unix_socket", since = "1.10.0")]
1136 pub fn try_clone(&self) -> io::Result<UnixDatagram> {
1137 self.0.duplicate().map(UnixDatagram)
1140 /// Returns the address of this socket.
1145 /// use std::os::unix::net::UnixDatagram;
1147 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1149 /// let addr = sock.local_addr().expect("Couldn't get local address");
1151 #[stable(feature = "unix_socket", since = "1.10.0")]
1152 pub fn local_addr(&self) -> io::Result<SocketAddr> {
1153 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
1156 /// Returns the address of this socket's peer.
1158 /// The [`connect`] method will connect the socket to a peer.
1160 /// [`connect`]: #method.connect
1165 /// use std::os::unix::net::UnixDatagram;
1167 /// let sock = UnixDatagram::unbound().unwrap();
1168 /// sock.connect("/path/to/the/socket").unwrap();
1170 /// let addr = sock.peer_addr().expect("Couldn't get peer address");
1172 #[stable(feature = "unix_socket", since = "1.10.0")]
1173 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
1174 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
1177 /// Receives data from the socket.
1179 /// On success, returns the number of bytes read and the address from
1180 /// whence the data came.
1185 /// use std::os::unix::net::UnixDatagram;
1187 /// let sock = UnixDatagram::unbound().unwrap();
1188 /// let mut buf = vec![0; 10];
1189 /// match sock.recv_from(buf.as_mut_slice()) {
1190 /// Ok((size, sender)) => println!("received {} bytes from {:?}", size, sender),
1191 /// Err(e) => println!("recv_from function failed: {:?}", e),
1194 #[stable(feature = "unix_socket", since = "1.10.0")]
1195 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
1197 let addr = SocketAddr::new(|addr, len| {
1199 count = libc::recvfrom(*self.0.as_inner(),
1200 buf.as_mut_ptr() as *mut _,
1207 } else if count == 0 {
1215 Ok((count as usize, addr))
1218 /// Receives data from the socket.
1220 /// On success, returns the number of bytes read.
1225 /// use std::os::unix::net::UnixDatagram;
1227 /// let sock = UnixDatagram::bind("/path/to/the/socket").unwrap();
1228 /// let mut buf = vec![0; 10];
1229 /// sock.recv(buf.as_mut_slice()).expect("recv function failed");
1231 #[stable(feature = "unix_socket", since = "1.10.0")]
1232 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
1236 /// Sends data on the socket to the specified address.
1238 /// On success, returns the number of bytes written.
1243 /// use std::os::unix::net::UnixDatagram;
1245 /// let sock = UnixDatagram::unbound().unwrap();
1246 /// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
1248 #[stable(feature = "unix_socket", since = "1.10.0")]
1249 pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
1250 fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
1252 let (addr, len) = sockaddr_un(path)?;
1254 let count = cvt(libc::sendto(*d.0.as_inner(),
1255 buf.as_ptr() as *const _,
1258 &addr as *const _ as *const _,
1263 inner(self, buf, path.as_ref())
1266 /// Sends data on the socket to the socket's peer.
1268 /// The peer address may be set by the `connect` method, and this method
1269 /// will return an error if the socket has not already been connected.
1271 /// On success, returns the number of bytes written.
1276 /// use std::os::unix::net::UnixDatagram;
1278 /// let sock = UnixDatagram::unbound().unwrap();
1279 /// sock.connect("/some/sock").expect("Couldn't connect");
1280 /// sock.send(b"omelette au fromage").expect("send_to function failed");
1282 #[stable(feature = "unix_socket", since = "1.10.0")]
1283 pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
1287 /// Sets the read timeout for the socket.
1289 /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
1290 /// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
1291 /// is passed to this method.
1293 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1294 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
1295 /// [`recv`]: #method.recv
1296 /// [`recv_from`]: #method.recv_from
1297 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1302 /// use std::os::unix::net::UnixDatagram;
1303 /// use std::time::Duration;
1305 /// let sock = UnixDatagram::unbound().unwrap();
1306 /// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
1309 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1314 /// use std::os::unix::net::UnixDatagram;
1315 /// use std::time::Duration;
1317 /// let socket = UnixDatagram::unbound().unwrap();
1318 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
1319 /// let err = result.unwrap_err();
1320 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1322 #[stable(feature = "unix_socket", since = "1.10.0")]
1323 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1324 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
1327 /// Sets the write timeout for the socket.
1329 /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
1330 /// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
1333 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1334 /// [`send`]: #method.send
1335 /// [`send_to`]: #method.send_to
1336 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1341 /// use std::os::unix::net::UnixDatagram;
1342 /// use std::time::Duration;
1344 /// let sock = UnixDatagram::unbound().unwrap();
1345 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1346 /// .expect("set_write_timeout function failed");
1349 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1354 /// use std::os::unix::net::UnixDatagram;
1355 /// use std::time::Duration;
1357 /// let socket = UnixDatagram::unbound().unwrap();
1358 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
1359 /// let err = result.unwrap_err();
1360 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput)
1362 #[stable(feature = "unix_socket", since = "1.10.0")]
1363 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1364 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
1367 /// Returns the read timeout of this socket.
1372 /// use std::os::unix::net::UnixDatagram;
1373 /// use std::time::Duration;
1375 /// let sock = UnixDatagram::unbound().unwrap();
1376 /// sock.set_read_timeout(Some(Duration::new(1, 0))).expect("set_read_timeout function failed");
1377 /// assert_eq!(sock.read_timeout().unwrap(), Some(Duration::new(1, 0)));
1379 #[stable(feature = "unix_socket", since = "1.10.0")]
1380 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
1381 self.0.timeout(libc::SO_RCVTIMEO)
1384 /// Returns the write timeout of this socket.
1389 /// use std::os::unix::net::UnixDatagram;
1390 /// use std::time::Duration;
1392 /// let sock = UnixDatagram::unbound().unwrap();
1393 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1394 /// .expect("set_write_timeout function failed");
1395 /// assert_eq!(sock.write_timeout().unwrap(), Some(Duration::new(1, 0)));
1397 #[stable(feature = "unix_socket", since = "1.10.0")]
1398 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
1399 self.0.timeout(libc::SO_SNDTIMEO)
1402 /// Moves the socket into or out of nonblocking mode.
1407 /// use std::os::unix::net::UnixDatagram;
1409 /// let sock = UnixDatagram::unbound().unwrap();
1410 /// sock.set_nonblocking(true).expect("set_nonblocking function failed");
1412 #[stable(feature = "unix_socket", since = "1.10.0")]
1413 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
1414 self.0.set_nonblocking(nonblocking)
1417 /// Returns the value of the `SO_ERROR` option.
1422 /// use std::os::unix::net::UnixDatagram;
1424 /// let sock = UnixDatagram::unbound().unwrap();
1425 /// if let Ok(Some(err)) = sock.take_error() {
1426 /// println!("Got error: {:?}", err);
1429 #[stable(feature = "unix_socket", since = "1.10.0")]
1430 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
1434 /// Shut down the read, write, or both halves of this connection.
1436 /// This function will cause all pending and future I/O calls on the
1437 /// specified portions to immediately return with an appropriate value
1438 /// (see the documentation of [`Shutdown`]).
1440 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
1443 /// use std::os::unix::net::UnixDatagram;
1444 /// use std::net::Shutdown;
1446 /// let sock = UnixDatagram::unbound().unwrap();
1447 /// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
1449 #[stable(feature = "unix_socket", since = "1.10.0")]
1450 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
1451 self.0.shutdown(how)
1455 #[stable(feature = "unix_socket", since = "1.10.0")]
1456 impl AsRawFd for UnixDatagram {
1457 fn as_raw_fd(&self) -> RawFd {
1462 #[stable(feature = "unix_socket", since = "1.10.0")]
1463 impl FromRawFd for UnixDatagram {
1464 unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
1465 UnixDatagram(Socket::from_inner(fd))
1469 #[stable(feature = "unix_socket", since = "1.10.0")]
1470 impl IntoRawFd for UnixDatagram {
1471 fn into_raw_fd(self) -> RawFd {
1476 #[cfg(all(test, not(target_os = "emscripten")))]
1479 use io::{self, ErrorKind};
1482 use sys_common::io::test::tmpdir;
1486 macro_rules! or_panic {
1490 Err(e) => panic!("{}", e),
1498 let socket_path = dir.path().join("sock");
1499 let msg1 = b"hello";
1500 let msg2 = b"world!";
1502 let listener = or_panic!(UnixListener::bind(&socket_path));
1503 let thread = thread::spawn(move || {
1504 let mut stream = or_panic!(listener.accept()).0;
1505 let mut buf = [0; 5];
1506 or_panic!(stream.read(&mut buf));
1507 assert_eq!(&msg1[..], &buf[..]);
1508 or_panic!(stream.write_all(msg2));
1511 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1512 assert_eq!(Some(&*socket_path),
1513 stream.peer_addr().unwrap().as_pathname());
1514 or_panic!(stream.write_all(msg1));
1515 let mut buf = vec![];
1516 or_panic!(stream.read_to_end(&mut buf));
1517 assert_eq!(&msg2[..], &buf[..]);
1520 thread.join().unwrap();
1525 let msg1 = b"hello";
1526 let msg2 = b"world!";
1528 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
1529 let thread = thread::spawn(move || {
1530 // s1 must be moved in or the test will hang!
1531 let mut buf = [0; 5];
1532 or_panic!(s1.read(&mut buf));
1533 assert_eq!(&msg1[..], &buf[..]);
1534 or_panic!(s1.write_all(msg2));
1537 or_panic!(s2.write_all(msg1));
1538 let mut buf = vec![];
1539 or_panic!(s2.read_to_end(&mut buf));
1540 assert_eq!(&msg2[..], &buf[..]);
1543 thread.join().unwrap();
1549 let socket_path = dir.path().join("sock");
1550 let msg1 = b"hello";
1551 let msg2 = b"world";
1553 let listener = or_panic!(UnixListener::bind(&socket_path));
1554 let thread = thread::spawn(move || {
1555 let mut stream = or_panic!(listener.accept()).0;
1556 or_panic!(stream.write_all(msg1));
1557 or_panic!(stream.write_all(msg2));
1560 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1561 let mut stream2 = or_panic!(stream.try_clone());
1563 let mut buf = [0; 5];
1564 or_panic!(stream.read(&mut buf));
1565 assert_eq!(&msg1[..], &buf[..]);
1566 or_panic!(stream2.read(&mut buf));
1567 assert_eq!(&msg2[..], &buf[..]);
1569 thread.join().unwrap();
1575 let socket_path = dir.path().join("sock");
1577 let listener = or_panic!(UnixListener::bind(&socket_path));
1578 let thread = thread::spawn(move || {
1579 for stream in listener.incoming().take(2) {
1580 let mut stream = or_panic!(stream);
1582 or_panic!(stream.read(&mut buf));
1587 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1588 or_panic!(stream.write_all(&[0]));
1591 thread.join().unwrap();
1597 let socket_path = dir.path()
1598 .join("asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa\
1599 sasdfasdfasdasdfasdfasdfadfasdfasdfasdfasdfasdf");
1600 match UnixStream::connect(&socket_path) {
1601 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1602 Err(e) => panic!("unexpected error {}", e),
1603 Ok(_) => panic!("unexpected success"),
1606 match UnixListener::bind(&socket_path) {
1607 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1608 Err(e) => panic!("unexpected error {}", e),
1609 Ok(_) => panic!("unexpected success"),
1612 match UnixDatagram::bind(&socket_path) {
1613 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1614 Err(e) => panic!("unexpected error {}", e),
1615 Ok(_) => panic!("unexpected success"),
1622 let socket_path = dir.path().join("sock");
1624 let _listener = or_panic!(UnixListener::bind(&socket_path));
1626 let stream = or_panic!(UnixStream::connect(&socket_path));
1627 let dur = Duration::new(15410, 0);
1629 assert_eq!(None, or_panic!(stream.read_timeout()));
1631 or_panic!(stream.set_read_timeout(Some(dur)));
1632 assert_eq!(Some(dur), or_panic!(stream.read_timeout()));
1634 assert_eq!(None, or_panic!(stream.write_timeout()));
1636 or_panic!(stream.set_write_timeout(Some(dur)));
1637 assert_eq!(Some(dur), or_panic!(stream.write_timeout()));
1639 or_panic!(stream.set_read_timeout(None));
1640 assert_eq!(None, or_panic!(stream.read_timeout()));
1642 or_panic!(stream.set_write_timeout(None));
1643 assert_eq!(None, or_panic!(stream.write_timeout()));
1647 fn test_read_timeout() {
1649 let socket_path = dir.path().join("sock");
1651 let _listener = or_panic!(UnixListener::bind(&socket_path));
1653 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1654 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1656 let mut buf = [0; 10];
1657 let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
1658 assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
1659 "unexpected_error: {:?}", kind);
1663 fn test_read_with_timeout() {
1665 let socket_path = dir.path().join("sock");
1667 let listener = or_panic!(UnixListener::bind(&socket_path));
1669 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1670 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1672 let mut other_end = or_panic!(listener.accept()).0;
1673 or_panic!(other_end.write_all(b"hello world"));
1675 let mut buf = [0; 11];
1676 or_panic!(stream.read(&mut buf));
1677 assert_eq!(b"hello world", &buf[..]);
1679 let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
1680 assert!(kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
1681 "unexpected_error: {:?}", kind);
1684 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
1685 // when passed zero Durations
1687 fn test_unix_stream_timeout_zero_duration() {
1689 let socket_path = dir.path().join("sock");
1691 let listener = or_panic!(UnixListener::bind(&socket_path));
1692 let stream = or_panic!(UnixStream::connect(&socket_path));
1694 let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
1695 let err = result.unwrap_err();
1696 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1698 let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
1699 let err = result.unwrap_err();
1700 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1706 fn test_unix_datagram() {
1708 let path1 = dir.path().join("sock1");
1709 let path2 = dir.path().join("sock2");
1711 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1712 let sock2 = or_panic!(UnixDatagram::bind(&path2));
1714 let msg = b"hello world";
1715 or_panic!(sock1.send_to(msg, &path2));
1716 let mut buf = [0; 11];
1717 or_panic!(sock2.recv_from(&mut buf));
1718 assert_eq!(msg, &buf[..]);
1722 fn test_unnamed_unix_datagram() {
1724 let path1 = dir.path().join("sock1");
1726 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1727 let sock2 = or_panic!(UnixDatagram::unbound());
1729 let msg = b"hello world";
1730 or_panic!(sock2.send_to(msg, &path1));
1731 let mut buf = [0; 11];
1732 let (usize, addr) = or_panic!(sock1.recv_from(&mut buf));
1733 assert_eq!(usize, 11);
1734 assert!(addr.is_unnamed());
1735 assert_eq!(msg, &buf[..]);
1739 fn test_connect_unix_datagram() {
1741 let path1 = dir.path().join("sock1");
1742 let path2 = dir.path().join("sock2");
1744 let bsock1 = or_panic!(UnixDatagram::bind(&path1));
1745 let bsock2 = or_panic!(UnixDatagram::bind(&path2));
1746 let sock = or_panic!(UnixDatagram::unbound());
1747 or_panic!(sock.connect(&path1));
1750 let msg = b"hello there";
1751 or_panic!(sock.send(msg));
1752 let mut buf = [0; 11];
1753 let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
1754 assert_eq!(usize, 11);
1755 assert!(addr.is_unnamed());
1756 assert_eq!(msg, &buf[..]);
1758 // Changing default socket works too
1759 or_panic!(sock.connect(&path2));
1760 or_panic!(sock.send(msg));
1761 or_panic!(bsock2.recv_from(&mut buf));
1765 fn test_unix_datagram_recv() {
1767 let path1 = dir.path().join("sock1");
1769 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1770 let sock2 = or_panic!(UnixDatagram::unbound());
1771 or_panic!(sock2.connect(&path1));
1773 let msg = b"hello world";
1774 or_panic!(sock2.send(msg));
1775 let mut buf = [0; 11];
1776 let size = or_panic!(sock1.recv(&mut buf));
1777 assert_eq!(size, 11);
1778 assert_eq!(msg, &buf[..]);
1782 fn datagram_pair() {
1783 let msg1 = b"hello";
1784 let msg2 = b"world!";
1786 let (s1, s2) = or_panic!(UnixDatagram::pair());
1787 let thread = thread::spawn(move || {
1788 // s1 must be moved in or the test will hang!
1789 let mut buf = [0; 5];
1790 or_panic!(s1.recv(&mut buf));
1791 assert_eq!(&msg1[..], &buf[..]);
1792 or_panic!(s1.send(msg2));
1795 or_panic!(s2.send(msg1));
1796 let mut buf = [0; 6];
1797 or_panic!(s2.recv(&mut buf));
1798 assert_eq!(&msg2[..], &buf[..]);
1801 thread.join().unwrap();
1804 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
1805 // when passed zero Durations
1807 fn test_unix_datagram_timeout_zero_duration() {
1809 let path = dir.path().join("sock");
1811 let datagram = or_panic!(UnixDatagram::bind(&path));
1813 let result = datagram.set_write_timeout(Some(Duration::new(0, 0)));
1814 let err = result.unwrap_err();
1815 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1817 let result = datagram.set_read_timeout(Some(Duration::new(0, 0)));
1818 let err = result.unwrap_err();
1819 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1823 fn abstract_namespace_not_allowed() {
1824 assert!(UnixStream::connect("\0asdf").is_err());