4 target_os = "dragonfly",
5 target_os = "emscripten",
11 use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
12 use super::{sockaddr_un, SocketAddr};
14 use crate::io::{self, IoSlice, IoSliceMut};
15 use crate::net::Shutdown;
16 use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
18 target_os = "android",
20 target_os = "dragonfly",
21 target_os = "freebsd",
27 use crate::os::unix::ucred;
28 use crate::path::Path;
30 use crate::sys::net::Socket;
31 use crate::sys_common::{AsInner, FromInner};
32 use crate::time::Duration;
34 #[unstable(feature = "peer_credentials_unix_socket", issue = "42839", reason = "unstable")]
36 target_os = "android",
38 target_os = "dragonfly",
39 target_os = "freebsd",
47 /// A Unix stream socket.
52 /// use std::os::unix::net::UnixStream;
53 /// use std::io::prelude::*;
55 /// fn main() -> std::io::Result<()> {
56 /// let mut stream = UnixStream::connect("/path/to/my/socket")?;
57 /// stream.write_all(b"hello world")?;
58 /// let mut response = String::new();
59 /// stream.read_to_string(&mut response)?;
60 /// println!("{response}");
64 #[stable(feature = "unix_socket", since = "1.10.0")]
65 pub struct UnixStream(pub(super) Socket);
67 #[stable(feature = "unix_socket", since = "1.10.0")]
68 impl fmt::Debug for UnixStream {
69 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
70 let mut builder = fmt.debug_struct("UnixStream");
71 builder.field("fd", self.0.as_inner());
72 if let Ok(addr) = self.local_addr() {
73 builder.field("local", &addr);
75 if let Ok(addr) = self.peer_addr() {
76 builder.field("peer", &addr);
83 /// Connects to the socket named by `path`.
88 /// use std::os::unix::net::UnixStream;
90 /// let socket = match UnixStream::connect("/tmp/sock") {
93 /// println!("Couldn't connect: {e:?}");
98 #[stable(feature = "unix_socket", since = "1.10.0")]
99 pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
101 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
102 let (addr, len) = sockaddr_un(path.as_ref())?;
104 cvt(libc::connect(inner.as_raw_fd(), &addr as *const _ as *const _, len))?;
105 Ok(UnixStream(inner))
109 /// Connects to the socket specified by [`address`].
111 /// [`address`]: crate::os::unix::net::SocketAddr
116 /// #![feature(unix_socket_abstract)]
117 /// use std::os::unix::net::{UnixListener, UnixStream};
119 /// fn main() -> std::io::Result<()> {
120 /// let listener = UnixListener::bind("/path/to/the/socket")?;
121 /// let addr = listener.local_addr()?;
123 /// let sock = match UnixStream::connect_addr(&addr) {
124 /// Ok(sock) => sock,
126 /// println!("Couldn't connect: {e:?}");
133 #[unstable(feature = "unix_socket_abstract", issue = "85410")]
134 pub fn connect_addr(socket_addr: &SocketAddr) -> io::Result<UnixStream> {
136 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
139 &socket_addr.addr as *const _ as *const _,
142 Ok(UnixStream(inner))
146 /// Creates an unnamed pair of connected sockets.
148 /// Returns two `UnixStream`s which are connected to each other.
153 /// use std::os::unix::net::UnixStream;
155 /// let (sock1, sock2) = match UnixStream::pair() {
156 /// Ok((sock1, sock2)) => (sock1, sock2),
158 /// println!("Couldn't create a pair of sockets: {e:?}");
163 #[stable(feature = "unix_socket", since = "1.10.0")]
164 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
165 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
166 Ok((UnixStream(i1), UnixStream(i2)))
169 /// Creates a new independently owned handle to the underlying socket.
171 /// The returned `UnixStream` is a reference to the same stream that this
172 /// object references. Both handles will read and write the same stream of
173 /// data, and options set on one stream will be propagated to the other
179 /// use std::os::unix::net::UnixStream;
181 /// fn main() -> std::io::Result<()> {
182 /// let socket = UnixStream::connect("/tmp/sock")?;
183 /// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
187 #[stable(feature = "unix_socket", since = "1.10.0")]
188 pub fn try_clone(&self) -> io::Result<UnixStream> {
189 self.0.duplicate().map(UnixStream)
192 /// Returns the socket address of the local half of this connection.
197 /// use std::os::unix::net::UnixStream;
199 /// fn main() -> std::io::Result<()> {
200 /// let socket = UnixStream::connect("/tmp/sock")?;
201 /// let addr = socket.local_addr().expect("Couldn't get local address");
205 #[stable(feature = "unix_socket", since = "1.10.0")]
206 pub fn local_addr(&self) -> io::Result<SocketAddr> {
207 SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.as_raw_fd(), addr, len) })
210 /// Returns the socket address of the remote half of this connection.
215 /// use std::os::unix::net::UnixStream;
217 /// fn main() -> std::io::Result<()> {
218 /// let socket = UnixStream::connect("/tmp/sock")?;
219 /// let addr = socket.peer_addr().expect("Couldn't get peer address");
223 #[stable(feature = "unix_socket", since = "1.10.0")]
224 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
225 SocketAddr::new(|addr, len| unsafe { libc::getpeername(self.as_raw_fd(), addr, len) })
228 /// Gets the peer credentials for this Unix domain socket.
233 /// #![feature(peer_credentials_unix_socket)]
234 /// use std::os::unix::net::UnixStream;
236 /// fn main() -> std::io::Result<()> {
237 /// let socket = UnixStream::connect("/tmp/sock")?;
238 /// let peer_cred = socket.peer_cred().expect("Couldn't get peer credentials");
242 #[unstable(feature = "peer_credentials_unix_socket", issue = "42839", reason = "unstable")]
244 target_os = "android",
246 target_os = "dragonfly",
247 target_os = "freebsd",
250 target_os = "netbsd",
251 target_os = "openbsd"
253 pub fn peer_cred(&self) -> io::Result<UCred> {
254 ucred::peer_cred(self)
257 /// Sets the read timeout for the socket.
259 /// If the provided value is [`None`], then [`read`] calls will block
260 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
263 /// [`read`]: io::Read::read
268 /// use std::os::unix::net::UnixStream;
269 /// use std::time::Duration;
271 /// fn main() -> std::io::Result<()> {
272 /// let socket = UnixStream::connect("/tmp/sock")?;
273 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
278 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
283 /// use std::os::unix::net::UnixStream;
284 /// use std::time::Duration;
286 /// fn main() -> std::io::Result<()> {
287 /// let socket = UnixStream::connect("/tmp/sock")?;
288 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
289 /// let err = result.unwrap_err();
290 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
294 #[stable(feature = "unix_socket", since = "1.10.0")]
295 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
296 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
299 /// Sets the write timeout for the socket.
301 /// If the provided value is [`None`], then [`write`] calls will block
302 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
303 /// passed to this method.
305 /// [`read`]: io::Read::read
310 /// use std::os::unix::net::UnixStream;
311 /// use std::time::Duration;
313 /// fn main() -> std::io::Result<()> {
314 /// let socket = UnixStream::connect("/tmp/sock")?;
315 /// socket.set_write_timeout(Some(Duration::new(1, 0)))
316 /// .expect("Couldn't set write timeout");
321 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
326 /// use std::net::UdpSocket;
327 /// use std::time::Duration;
329 /// fn main() -> std::io::Result<()> {
330 /// let socket = UdpSocket::bind("127.0.0.1:34254")?;
331 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
332 /// let err = result.unwrap_err();
333 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
337 #[stable(feature = "unix_socket", since = "1.10.0")]
338 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
339 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
342 /// Returns the read timeout of this socket.
347 /// use std::os::unix::net::UnixStream;
348 /// use std::time::Duration;
350 /// fn main() -> std::io::Result<()> {
351 /// let socket = UnixStream::connect("/tmp/sock")?;
352 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
353 /// assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
357 #[stable(feature = "unix_socket", since = "1.10.0")]
358 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
359 self.0.timeout(libc::SO_RCVTIMEO)
362 /// Returns the write timeout of this socket.
367 /// use std::os::unix::net::UnixStream;
368 /// use std::time::Duration;
370 /// fn main() -> std::io::Result<()> {
371 /// let socket = UnixStream::connect("/tmp/sock")?;
372 /// socket.set_write_timeout(Some(Duration::new(1, 0)))
373 /// .expect("Couldn't set write timeout");
374 /// assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
378 #[stable(feature = "unix_socket", since = "1.10.0")]
379 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
380 self.0.timeout(libc::SO_SNDTIMEO)
383 /// Moves the socket into or out of nonblocking mode.
388 /// use std::os::unix::net::UnixStream;
390 /// fn main() -> std::io::Result<()> {
391 /// let socket = UnixStream::connect("/tmp/sock")?;
392 /// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
396 #[stable(feature = "unix_socket", since = "1.10.0")]
397 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
398 self.0.set_nonblocking(nonblocking)
401 /// Moves the socket to pass unix credentials as control message in [`SocketAncillary`].
403 /// Set the socket option `SO_PASSCRED`.
407 #[cfg_attr(any(target_os = "android", target_os = "linux"), doc = "```no_run")]
408 #[cfg_attr(not(any(target_os = "android", target_os = "linux")), doc = "```ignore")]
409 /// #![feature(unix_socket_ancillary_data)]
410 /// use std::os::unix::net::UnixStream;
412 /// fn main() -> std::io::Result<()> {
413 /// let socket = UnixStream::connect("/tmp/sock")?;
414 /// socket.set_passcred(true).expect("Couldn't set passcred");
418 #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
419 #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
420 pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
421 self.0.set_passcred(passcred)
424 /// Get the current value of the socket for passing unix credentials in [`SocketAncillary`].
425 /// This value can be change by [`set_passcred`].
427 /// Get the socket option `SO_PASSCRED`.
429 /// [`set_passcred`]: UnixStream::set_passcred
430 #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "netbsd",))]
431 #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
432 pub fn passcred(&self) -> io::Result<bool> {
436 /// Returns the value of the `SO_ERROR` option.
441 /// use std::os::unix::net::UnixStream;
443 /// fn main() -> std::io::Result<()> {
444 /// let socket = UnixStream::connect("/tmp/sock")?;
445 /// if let Ok(Some(err)) = socket.take_error() {
446 /// println!("Got error: {err:?}");
452 /// # Platform specific
453 /// On Redox this always returns `None`.
454 #[stable(feature = "unix_socket", since = "1.10.0")]
455 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
459 /// Shuts down the read, write, or both halves of this connection.
461 /// This function will cause all pending and future I/O calls on the
462 /// specified portions to immediately return with an appropriate value
463 /// (see the documentation of [`Shutdown`]).
468 /// use std::os::unix::net::UnixStream;
469 /// use std::net::Shutdown;
471 /// fn main() -> std::io::Result<()> {
472 /// let socket = UnixStream::connect("/tmp/sock")?;
473 /// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
477 #[stable(feature = "unix_socket", since = "1.10.0")]
478 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
482 /// Receives data on the socket from the remote address to which it is
483 /// connected, without removing that data from the queue. On success,
484 /// returns the number of bytes peeked.
486 /// Successive calls return the same data. This is accomplished by passing
487 /// `MSG_PEEK` as a flag to the underlying `recv` system call.
492 /// #![feature(unix_socket_peek)]
494 /// use std::os::unix::net::UnixStream;
496 /// fn main() -> std::io::Result<()> {
497 /// let socket = UnixStream::connect("/tmp/sock")?;
498 /// let mut buf = [0; 10];
499 /// let len = socket.peek(&mut buf).expect("peek failed");
503 #[unstable(feature = "unix_socket_peek", issue = "76923")]
504 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
508 /// Receives data and ancillary data from socket.
510 /// On success, returns the number of bytes read.
515 /// #![feature(unix_socket_ancillary_data)]
516 /// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
517 /// use std::io::IoSliceMut;
519 /// fn main() -> std::io::Result<()> {
520 /// let socket = UnixStream::connect("/tmp/sock")?;
521 /// let mut buf1 = [1; 8];
522 /// let mut buf2 = [2; 16];
523 /// let mut buf3 = [3; 8];
524 /// let mut bufs = &mut [
525 /// IoSliceMut::new(&mut buf1),
526 /// IoSliceMut::new(&mut buf2),
527 /// IoSliceMut::new(&mut buf3),
529 /// let mut fds = [0; 8];
530 /// let mut ancillary_buffer = [0; 128];
531 /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
532 /// let size = socket.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
533 /// println!("received {size}");
534 /// for ancillary_result in ancillary.messages() {
535 /// if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
536 /// for fd in scm_rights {
537 /// println!("receive file descriptor: {fd}");
545 target_os = "android",
546 target_os = "dragonfly",
547 target_os = "emscripten",
548 target_os = "freebsd",
550 target_os = "netbsd",
551 target_os = "openbsd",
553 #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
554 pub fn recv_vectored_with_ancillary(
556 bufs: &mut [IoSliceMut<'_>],
557 ancillary: &mut SocketAncillary<'_>,
558 ) -> io::Result<usize> {
559 let (count, _, _) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
564 /// Sends data and ancillary data on the socket.
566 /// On success, returns the number of bytes written.
571 /// #![feature(unix_socket_ancillary_data)]
572 /// use std::os::unix::net::{UnixStream, SocketAncillary};
573 /// use std::io::IoSlice;
575 /// fn main() -> std::io::Result<()> {
576 /// let socket = UnixStream::connect("/tmp/sock")?;
577 /// let buf1 = [1; 8];
578 /// let buf2 = [2; 16];
579 /// let buf3 = [3; 8];
581 /// IoSlice::new(&buf1),
582 /// IoSlice::new(&buf2),
583 /// IoSlice::new(&buf3),
585 /// let fds = [0, 1, 2];
586 /// let mut ancillary_buffer = [0; 128];
587 /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
588 /// ancillary.add_fds(&fds[..]);
589 /// socket.send_vectored_with_ancillary(bufs, &mut ancillary)
590 /// .expect("send_vectored_with_ancillary function failed");
595 target_os = "android",
596 target_os = "dragonfly",
597 target_os = "emscripten",
598 target_os = "freebsd",
600 target_os = "netbsd",
601 target_os = "openbsd",
603 #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
604 pub fn send_vectored_with_ancillary(
606 bufs: &[IoSlice<'_>],
607 ancillary: &mut SocketAncillary<'_>,
608 ) -> io::Result<usize> {
609 send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)
613 #[stable(feature = "unix_socket", since = "1.10.0")]
614 impl io::Read for UnixStream {
615 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
616 io::Read::read(&mut &*self, buf)
619 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
620 io::Read::read_vectored(&mut &*self, bufs)
624 fn is_read_vectored(&self) -> bool {
625 io::Read::is_read_vectored(&&*self)
629 #[stable(feature = "unix_socket", since = "1.10.0")]
630 impl<'a> io::Read for &'a UnixStream {
631 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
635 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
636 self.0.read_vectored(bufs)
640 fn is_read_vectored(&self) -> bool {
641 self.0.is_read_vectored()
645 #[stable(feature = "unix_socket", since = "1.10.0")]
646 impl io::Write for UnixStream {
647 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
648 io::Write::write(&mut &*self, buf)
651 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
652 io::Write::write_vectored(&mut &*self, bufs)
656 fn is_write_vectored(&self) -> bool {
657 io::Write::is_write_vectored(&&*self)
660 fn flush(&mut self) -> io::Result<()> {
661 io::Write::flush(&mut &*self)
665 #[stable(feature = "unix_socket", since = "1.10.0")]
666 impl<'a> io::Write for &'a UnixStream {
667 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
671 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
672 self.0.write_vectored(bufs)
676 fn is_write_vectored(&self) -> bool {
677 self.0.is_write_vectored()
680 fn flush(&mut self) -> io::Result<()> {
685 #[stable(feature = "unix_socket", since = "1.10.0")]
686 impl AsRawFd for UnixStream {
688 fn as_raw_fd(&self) -> RawFd {
693 #[stable(feature = "unix_socket", since = "1.10.0")]
694 impl FromRawFd for UnixStream {
696 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
697 UnixStream(Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd))))
701 #[stable(feature = "unix_socket", since = "1.10.0")]
702 impl IntoRawFd for UnixStream {
704 fn into_raw_fd(self) -> RawFd {
709 #[unstable(feature = "io_safety", issue = "87074")]
710 impl AsFd for UnixStream {
712 fn as_fd(&self) -> BorrowedFd<'_> {
717 #[unstable(feature = "io_safety", issue = "87074")]
718 impl From<UnixStream> for OwnedFd {
720 fn from(unix_stream: UnixStream) -> OwnedFd {
721 unsafe { OwnedFd::from_raw_fd(unix_stream.into_raw_fd()) }
725 #[unstable(feature = "io_safety", issue = "87074")]
726 impl From<OwnedFd> for UnixStream {
728 fn from(owned: OwnedFd) -> Self {
729 unsafe { Self::from_raw_fd(owned.into_raw_fd()) }