| 1 | cfg_select! { |
| 2 | any( |
| 3 | target_os = "linux" , target_os = "android" , |
| 4 | target_os = "hurd" , |
| 5 | target_os = "dragonfly" , target_os = "freebsd" , |
| 6 | target_os = "openbsd" , target_os = "netbsd" , |
| 7 | target_os = "solaris" , target_os = "illumos" , |
| 8 | target_os = "haiku" , target_os = "nto" , |
| 9 | target_os = "cygwin" , |
| 10 | ) => { |
| 11 | use libc::MSG_NOSIGNAL; |
| 12 | } |
| 13 | _ => { |
| 14 | const MSG_NOSIGNAL: core::ffi::c_int = 0x0; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | use super::{SocketAddr, sockaddr_un}; |
| 19 | #[cfg (any(doc, target_os = "android" , target_os = "linux" , target_os = "cygwin" ))] |
| 20 | use super::{SocketAncillary, recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to}; |
| 21 | #[cfg (any( |
| 22 | target_os = "android" , |
| 23 | target_os = "linux" , |
| 24 | target_os = "dragonfly" , |
| 25 | target_os = "freebsd" , |
| 26 | target_os = "netbsd" , |
| 27 | target_os = "openbsd" , |
| 28 | target_os = "nto" , |
| 29 | target_vendor = "apple" , |
| 30 | target_os = "cygwin" |
| 31 | ))] |
| 32 | use super::{UCred, peer_cred}; |
| 33 | use crate::fmt; |
| 34 | use crate::io::{self, IoSlice, IoSliceMut}; |
| 35 | use crate::net::Shutdown; |
| 36 | use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; |
| 37 | use crate::path::Path; |
| 38 | use crate::sealed::Sealed; |
| 39 | use crate::sys::cvt; |
| 40 | use crate::sys::net::Socket; |
| 41 | use crate::sys_common::{AsInner, FromInner}; |
| 42 | use crate::time::Duration; |
| 43 | |
| 44 | /// A Unix stream socket. |
| 45 | /// |
| 46 | /// # Examples |
| 47 | /// |
| 48 | /// ```no_run |
| 49 | /// use std::os::unix::net::UnixStream; |
| 50 | /// use std::io::prelude::*; |
| 51 | /// |
| 52 | /// fn main() -> std::io::Result<()> { |
| 53 | /// let mut stream = UnixStream::connect("/path/to/my/socket" )?; |
| 54 | /// stream.write_all(b"hello world" )?; |
| 55 | /// let mut response = String::new(); |
| 56 | /// stream.read_to_string(&mut response)?; |
| 57 | /// println!("{response}" ); |
| 58 | /// Ok(()) |
| 59 | /// } |
| 60 | /// ``` |
| 61 | /// |
| 62 | /// # `SIGPIPE` |
| 63 | /// |
| 64 | /// Writes to the underlying socket in `SOCK_STREAM` mode are made with `MSG_NOSIGNAL` flag. |
| 65 | /// This suppresses the emission of the `SIGPIPE` signal when writing to disconnected socket. |
| 66 | /// In some cases getting a `SIGPIPE` would trigger process termination. |
| 67 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 68 | pub struct UnixStream(pub(super) Socket); |
| 69 | |
| 70 | /// Allows extension traits within `std`. |
| 71 | #[unstable (feature = "sealed" , issue = "none" )] |
| 72 | impl Sealed for UnixStream {} |
| 73 | |
| 74 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 75 | impl fmt::Debug for UnixStream { |
| 76 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 77 | let mut builder: DebugStruct<'_, '_> = fmt.debug_struct(name:"UnixStream" ); |
| 78 | builder.field(name:"fd" , self.0.as_inner()); |
| 79 | if let Ok(addr: SocketAddr) = self.local_addr() { |
| 80 | builder.field(name:"local" , &addr); |
| 81 | } |
| 82 | if let Ok(addr: SocketAddr) = self.peer_addr() { |
| 83 | builder.field(name:"peer" , &addr); |
| 84 | } |
| 85 | builder.finish() |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl UnixStream { |
| 90 | /// Connects to the socket named by `path`. |
| 91 | /// |
| 92 | /// # Examples |
| 93 | /// |
| 94 | /// ```no_run |
| 95 | /// use std::os::unix::net::UnixStream; |
| 96 | /// |
| 97 | /// let socket = match UnixStream::connect("/tmp/sock" ) { |
| 98 | /// Ok(sock) => sock, |
| 99 | /// Err(e) => { |
| 100 | /// println!("Couldn't connect: {e:?}" ); |
| 101 | /// return |
| 102 | /// } |
| 103 | /// }; |
| 104 | /// ``` |
| 105 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 106 | pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> { |
| 107 | unsafe { |
| 108 | let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?; |
| 109 | let (addr, len) = sockaddr_un(path.as_ref())?; |
| 110 | |
| 111 | cvt(libc::connect(inner.as_raw_fd(), (&raw const addr) as *const _, len))?; |
| 112 | Ok(UnixStream(inner)) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// Connects to the socket specified by [`address`]. |
| 117 | /// |
| 118 | /// [`address`]: crate::os::unix::net::SocketAddr |
| 119 | /// |
| 120 | /// # Examples |
| 121 | /// |
| 122 | /// ```no_run |
| 123 | /// use std::os::unix::net::{UnixListener, UnixStream}; |
| 124 | /// |
| 125 | /// fn main() -> std::io::Result<()> { |
| 126 | /// let listener = UnixListener::bind("/path/to/the/socket" )?; |
| 127 | /// let addr = listener.local_addr()?; |
| 128 | /// |
| 129 | /// let sock = match UnixStream::connect_addr(&addr) { |
| 130 | /// Ok(sock) => sock, |
| 131 | /// Err(e) => { |
| 132 | /// println!("Couldn't connect: {e:?}" ); |
| 133 | /// return Err(e) |
| 134 | /// } |
| 135 | /// }; |
| 136 | /// Ok(()) |
| 137 | /// } |
| 138 | /// ```` |
| 139 | #[stable (feature = "unix_socket_abstract" , since = "1.70.0" )] |
| 140 | pub fn connect_addr(socket_addr: &SocketAddr) -> io::Result<UnixStream> { |
| 141 | unsafe { |
| 142 | let inner = Socket::new(libc::AF_UNIX, libc::SOCK_STREAM)?; |
| 143 | cvt(libc::connect( |
| 144 | inner.as_raw_fd(), |
| 145 | (&raw const socket_addr.addr) as *const _, |
| 146 | socket_addr.len, |
| 147 | ))?; |
| 148 | Ok(UnixStream(inner)) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// Creates an unnamed pair of connected sockets. |
| 153 | /// |
| 154 | /// Returns two `UnixStream`s which are connected to each other. |
| 155 | /// |
| 156 | /// # Examples |
| 157 | /// |
| 158 | /// ```no_run |
| 159 | /// use std::os::unix::net::UnixStream; |
| 160 | /// |
| 161 | /// let (sock1, sock2) = match UnixStream::pair() { |
| 162 | /// Ok((sock1, sock2)) => (sock1, sock2), |
| 163 | /// Err(e) => { |
| 164 | /// println!("Couldn't create a pair of sockets: {e:?}" ); |
| 165 | /// return |
| 166 | /// } |
| 167 | /// }; |
| 168 | /// ``` |
| 169 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 170 | pub fn pair() -> io::Result<(UnixStream, UnixStream)> { |
| 171 | let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?; |
| 172 | Ok((UnixStream(i1), UnixStream(i2))) |
| 173 | } |
| 174 | |
| 175 | /// Creates a new independently owned handle to the underlying socket. |
| 176 | /// |
| 177 | /// The returned `UnixStream` is a reference to the same stream that this |
| 178 | /// object references. Both handles will read and write the same stream of |
| 179 | /// data, and options set on one stream will be propagated to the other |
| 180 | /// stream. |
| 181 | /// |
| 182 | /// # Examples |
| 183 | /// |
| 184 | /// ```no_run |
| 185 | /// use std::os::unix::net::UnixStream; |
| 186 | /// |
| 187 | /// fn main() -> std::io::Result<()> { |
| 188 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 189 | /// let sock_copy = socket.try_clone().expect("Couldn't clone socket" ); |
| 190 | /// Ok(()) |
| 191 | /// } |
| 192 | /// ``` |
| 193 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 194 | pub fn try_clone(&self) -> io::Result<UnixStream> { |
| 195 | self.0.duplicate().map(UnixStream) |
| 196 | } |
| 197 | |
| 198 | /// Returns the socket address of the local half of this connection. |
| 199 | /// |
| 200 | /// # Examples |
| 201 | /// |
| 202 | /// ```no_run |
| 203 | /// use std::os::unix::net::UnixStream; |
| 204 | /// |
| 205 | /// fn main() -> std::io::Result<()> { |
| 206 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 207 | /// let addr = socket.local_addr().expect("Couldn't get local address" ); |
| 208 | /// Ok(()) |
| 209 | /// } |
| 210 | /// ``` |
| 211 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 212 | pub fn local_addr(&self) -> io::Result<SocketAddr> { |
| 213 | SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.as_raw_fd(), addr, len) }) |
| 214 | } |
| 215 | |
| 216 | /// Returns the socket address of the remote half of this connection. |
| 217 | /// |
| 218 | /// # Examples |
| 219 | /// |
| 220 | /// ```no_run |
| 221 | /// use std::os::unix::net::UnixStream; |
| 222 | /// |
| 223 | /// fn main() -> std::io::Result<()> { |
| 224 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 225 | /// let addr = socket.peer_addr().expect("Couldn't get peer address" ); |
| 226 | /// Ok(()) |
| 227 | /// } |
| 228 | /// ``` |
| 229 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 230 | pub fn peer_addr(&self) -> io::Result<SocketAddr> { |
| 231 | SocketAddr::new(|addr, len| unsafe { libc::getpeername(self.as_raw_fd(), addr, len) }) |
| 232 | } |
| 233 | |
| 234 | /// Gets the peer credentials for this Unix domain socket. |
| 235 | /// |
| 236 | /// # Examples |
| 237 | /// |
| 238 | /// ```no_run |
| 239 | /// #![feature(peer_credentials_unix_socket)] |
| 240 | /// use std::os::unix::net::UnixStream; |
| 241 | /// |
| 242 | /// fn main() -> std::io::Result<()> { |
| 243 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 244 | /// let peer_cred = socket.peer_cred().expect("Couldn't get peer credentials" ); |
| 245 | /// Ok(()) |
| 246 | /// } |
| 247 | /// ``` |
| 248 | #[unstable (feature = "peer_credentials_unix_socket" , issue = "42839" , reason = "unstable" )] |
| 249 | #[cfg (any( |
| 250 | target_os = "android" , |
| 251 | target_os = "linux" , |
| 252 | target_os = "dragonfly" , |
| 253 | target_os = "freebsd" , |
| 254 | target_os = "netbsd" , |
| 255 | target_os = "openbsd" , |
| 256 | target_os = "nto" , |
| 257 | target_vendor = "apple" , |
| 258 | target_os = "cygwin" |
| 259 | ))] |
| 260 | pub fn peer_cred(&self) -> io::Result<UCred> { |
| 261 | peer_cred(self) |
| 262 | } |
| 263 | |
| 264 | /// Sets the read timeout for the socket. |
| 265 | /// |
| 266 | /// If the provided value is [`None`], then [`read`] calls will block |
| 267 | /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this |
| 268 | /// method. |
| 269 | /// |
| 270 | /// [`read`]: io::Read::read |
| 271 | /// |
| 272 | /// # Examples |
| 273 | /// |
| 274 | /// ```no_run |
| 275 | /// use std::os::unix::net::UnixStream; |
| 276 | /// use std::time::Duration; |
| 277 | /// |
| 278 | /// fn main() -> std::io::Result<()> { |
| 279 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 280 | /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout" ); |
| 281 | /// Ok(()) |
| 282 | /// } |
| 283 | /// ``` |
| 284 | /// |
| 285 | /// An [`Err`] is returned if the zero [`Duration`] is passed to this |
| 286 | /// method: |
| 287 | /// |
| 288 | /// ```no_run |
| 289 | /// use std::io; |
| 290 | /// use std::os::unix::net::UnixStream; |
| 291 | /// use std::time::Duration; |
| 292 | /// |
| 293 | /// fn main() -> std::io::Result<()> { |
| 294 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 295 | /// let result = socket.set_read_timeout(Some(Duration::new(0, 0))); |
| 296 | /// let err = result.unwrap_err(); |
| 297 | /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput); |
| 298 | /// Ok(()) |
| 299 | /// } |
| 300 | /// ``` |
| 301 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 302 | pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> { |
| 303 | self.0.set_timeout(timeout, libc::SO_RCVTIMEO) |
| 304 | } |
| 305 | |
| 306 | /// Sets the write timeout for the socket. |
| 307 | /// |
| 308 | /// If the provided value is [`None`], then [`write`] calls will block |
| 309 | /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is |
| 310 | /// passed to this method. |
| 311 | /// |
| 312 | /// [`read`]: io::Read::read |
| 313 | /// |
| 314 | /// # Examples |
| 315 | /// |
| 316 | /// ```no_run |
| 317 | /// use std::os::unix::net::UnixStream; |
| 318 | /// use std::time::Duration; |
| 319 | /// |
| 320 | /// fn main() -> std::io::Result<()> { |
| 321 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 322 | /// socket.set_write_timeout(Some(Duration::new(1, 0))) |
| 323 | /// .expect("Couldn't set write timeout" ); |
| 324 | /// Ok(()) |
| 325 | /// } |
| 326 | /// ``` |
| 327 | /// |
| 328 | /// An [`Err`] is returned if the zero [`Duration`] is passed to this |
| 329 | /// method: |
| 330 | /// |
| 331 | /// ```no_run |
| 332 | /// use std::io; |
| 333 | /// use std::os::unix::net::UnixStream; |
| 334 | /// use std::time::Duration; |
| 335 | /// |
| 336 | /// fn main() -> std::io::Result<()> { |
| 337 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 338 | /// let result = socket.set_write_timeout(Some(Duration::new(0, 0))); |
| 339 | /// let err = result.unwrap_err(); |
| 340 | /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput); |
| 341 | /// Ok(()) |
| 342 | /// } |
| 343 | /// ``` |
| 344 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 345 | pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> { |
| 346 | self.0.set_timeout(timeout, libc::SO_SNDTIMEO) |
| 347 | } |
| 348 | |
| 349 | /// Returns the read timeout of this socket. |
| 350 | /// |
| 351 | /// # Examples |
| 352 | /// |
| 353 | /// ```no_run |
| 354 | /// use std::os::unix::net::UnixStream; |
| 355 | /// use std::time::Duration; |
| 356 | /// |
| 357 | /// fn main() -> std::io::Result<()> { |
| 358 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 359 | /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout" ); |
| 360 | /// assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0))); |
| 361 | /// Ok(()) |
| 362 | /// } |
| 363 | /// ``` |
| 364 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 365 | pub fn read_timeout(&self) -> io::Result<Option<Duration>> { |
| 366 | self.0.timeout(libc::SO_RCVTIMEO) |
| 367 | } |
| 368 | |
| 369 | /// Returns the write timeout of this socket. |
| 370 | /// |
| 371 | /// # Examples |
| 372 | /// |
| 373 | /// ```no_run |
| 374 | /// use std::os::unix::net::UnixStream; |
| 375 | /// use std::time::Duration; |
| 376 | /// |
| 377 | /// fn main() -> std::io::Result<()> { |
| 378 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 379 | /// socket.set_write_timeout(Some(Duration::new(1, 0))) |
| 380 | /// .expect("Couldn't set write timeout" ); |
| 381 | /// assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0))); |
| 382 | /// Ok(()) |
| 383 | /// } |
| 384 | /// ``` |
| 385 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 386 | pub fn write_timeout(&self) -> io::Result<Option<Duration>> { |
| 387 | self.0.timeout(libc::SO_SNDTIMEO) |
| 388 | } |
| 389 | |
| 390 | /// Moves the socket into or out of nonblocking mode. |
| 391 | /// |
| 392 | /// # Examples |
| 393 | /// |
| 394 | /// ```no_run |
| 395 | /// use std::os::unix::net::UnixStream; |
| 396 | /// |
| 397 | /// fn main() -> std::io::Result<()> { |
| 398 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 399 | /// socket.set_nonblocking(true).expect("Couldn't set nonblocking" ); |
| 400 | /// Ok(()) |
| 401 | /// } |
| 402 | /// ``` |
| 403 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 404 | pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { |
| 405 | self.0.set_nonblocking(nonblocking) |
| 406 | } |
| 407 | |
| 408 | /// Set the id of the socket for network filtering purpose |
| 409 | /// |
| 410 | #[cfg_attr ( |
| 411 | any(target_os = "linux" , target_os = "freebsd" , target_os = "openbsd" ), |
| 412 | doc = "```no_run" |
| 413 | )] |
| 414 | #[cfg_attr ( |
| 415 | not(any(target_os = "linux" , target_os = "freebsd" , target_os = "openbsd" )), |
| 416 | doc = "```ignore" |
| 417 | )] |
| 418 | /// #![feature(unix_set_mark)] |
| 419 | /// use std::os::unix::net::UnixStream; |
| 420 | /// |
| 421 | /// fn main() -> std::io::Result<()> { |
| 422 | /// let sock = UnixStream::connect("/tmp/sock" )?; |
| 423 | /// sock.set_mark(32)?; |
| 424 | /// Ok(()) |
| 425 | /// } |
| 426 | /// ``` |
| 427 | #[cfg (any(doc, target_os = "linux" , target_os = "freebsd" , target_os = "openbsd" ,))] |
| 428 | #[unstable (feature = "unix_set_mark" , issue = "96467" )] |
| 429 | pub fn set_mark(&self, mark: u32) -> io::Result<()> { |
| 430 | self.0.set_mark(mark) |
| 431 | } |
| 432 | |
| 433 | /// Returns the value of the `SO_ERROR` option. |
| 434 | /// |
| 435 | /// # Examples |
| 436 | /// |
| 437 | /// ```no_run |
| 438 | /// use std::os::unix::net::UnixStream; |
| 439 | /// |
| 440 | /// fn main() -> std::io::Result<()> { |
| 441 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 442 | /// if let Ok(Some(err)) = socket.take_error() { |
| 443 | /// println!("Got error: {err:?}" ); |
| 444 | /// } |
| 445 | /// Ok(()) |
| 446 | /// } |
| 447 | /// ``` |
| 448 | /// |
| 449 | /// # Platform specific |
| 450 | /// On Redox this always returns `None`. |
| 451 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 452 | pub fn take_error(&self) -> io::Result<Option<io::Error>> { |
| 453 | self.0.take_error() |
| 454 | } |
| 455 | |
| 456 | /// Shuts down the read, write, or both halves of this connection. |
| 457 | /// |
| 458 | /// This function will cause all pending and future I/O calls on the |
| 459 | /// specified portions to immediately return with an appropriate value |
| 460 | /// (see the documentation of [`Shutdown`]). |
| 461 | /// |
| 462 | /// # Examples |
| 463 | /// |
| 464 | /// ```no_run |
| 465 | /// use std::os::unix::net::UnixStream; |
| 466 | /// use std::net::Shutdown; |
| 467 | /// |
| 468 | /// fn main() -> std::io::Result<()> { |
| 469 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 470 | /// socket.shutdown(Shutdown::Both).expect("shutdown function failed" ); |
| 471 | /// Ok(()) |
| 472 | /// } |
| 473 | /// ``` |
| 474 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 475 | pub fn shutdown(&self, how: Shutdown) -> io::Result<()> { |
| 476 | self.0.shutdown(how) |
| 477 | } |
| 478 | |
| 479 | /// Receives data on the socket from the remote address to which it is |
| 480 | /// connected, without removing that data from the queue. On success, |
| 481 | /// returns the number of bytes peeked. |
| 482 | /// |
| 483 | /// Successive calls return the same data. This is accomplished by passing |
| 484 | /// `MSG_PEEK` as a flag to the underlying `recv` system call. |
| 485 | /// |
| 486 | /// # Examples |
| 487 | /// |
| 488 | /// ```no_run |
| 489 | /// #![feature(unix_socket_peek)] |
| 490 | /// |
| 491 | /// use std::os::unix::net::UnixStream; |
| 492 | /// |
| 493 | /// fn main() -> std::io::Result<()> { |
| 494 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 495 | /// let mut buf = [0; 10]; |
| 496 | /// let len = socket.peek(&mut buf).expect("peek failed" ); |
| 497 | /// Ok(()) |
| 498 | /// } |
| 499 | /// ``` |
| 500 | #[unstable (feature = "unix_socket_peek" , issue = "76923" )] |
| 501 | pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> { |
| 502 | self.0.peek(buf) |
| 503 | } |
| 504 | |
| 505 | /// Receives data and ancillary data from socket. |
| 506 | /// |
| 507 | /// On success, returns the number of bytes read. |
| 508 | /// |
| 509 | /// # Examples |
| 510 | /// |
| 511 | #[cfg_attr ( |
| 512 | any(target_os = "android" , target_os = "linux" , target_os = "cygwin" ), |
| 513 | doc = "```no_run" |
| 514 | )] |
| 515 | #[cfg_attr ( |
| 516 | not(any(target_os = "android" , target_os = "linux" , target_os = "cygwin" )), |
| 517 | doc = "```ignore" |
| 518 | )] |
| 519 | /// #![feature(unix_socket_ancillary_data)] |
| 520 | /// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData}; |
| 521 | /// use std::io::IoSliceMut; |
| 522 | /// |
| 523 | /// fn main() -> std::io::Result<()> { |
| 524 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 525 | /// let mut buf1 = [1; 8]; |
| 526 | /// let mut buf2 = [2; 16]; |
| 527 | /// let mut buf3 = [3; 8]; |
| 528 | /// let mut bufs = &mut [ |
| 529 | /// IoSliceMut::new(&mut buf1), |
| 530 | /// IoSliceMut::new(&mut buf2), |
| 531 | /// IoSliceMut::new(&mut buf3), |
| 532 | /// ][..]; |
| 533 | /// let mut fds = [0; 8]; |
| 534 | /// let mut ancillary_buffer = [0; 128]; |
| 535 | /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); |
| 536 | /// let size = socket.recv_vectored_with_ancillary(bufs, &mut ancillary)?; |
| 537 | /// println!("received {size}" ); |
| 538 | /// for ancillary_result in ancillary.messages() { |
| 539 | /// if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() { |
| 540 | /// for fd in scm_rights { |
| 541 | /// println!("receive file descriptor: {fd}" ); |
| 542 | /// } |
| 543 | /// } |
| 544 | /// } |
| 545 | /// Ok(()) |
| 546 | /// } |
| 547 | /// ``` |
| 548 | #[cfg (any(doc, target_os = "android" , target_os = "linux" , target_os = "cygwin" ))] |
| 549 | #[unstable (feature = "unix_socket_ancillary_data" , issue = "76915" )] |
| 550 | pub fn recv_vectored_with_ancillary( |
| 551 | &self, |
| 552 | bufs: &mut [IoSliceMut<'_>], |
| 553 | ancillary: &mut SocketAncillary<'_>, |
| 554 | ) -> io::Result<usize> { |
| 555 | let (count, _, _) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?; |
| 556 | |
| 557 | Ok(count) |
| 558 | } |
| 559 | |
| 560 | /// Sends data and ancillary data on the socket. |
| 561 | /// |
| 562 | /// On success, returns the number of bytes written. |
| 563 | /// |
| 564 | /// # Examples |
| 565 | /// |
| 566 | #[cfg_attr ( |
| 567 | any(target_os = "android" , target_os = "linux" , target_os = "cygwin" ), |
| 568 | doc = "```no_run" |
| 569 | )] |
| 570 | #[cfg_attr ( |
| 571 | not(any(target_os = "android" , target_os = "linux" , target_os = "cygwin" )), |
| 572 | doc = "```ignore" |
| 573 | )] |
| 574 | /// #![feature(unix_socket_ancillary_data)] |
| 575 | /// use std::os::unix::net::{UnixStream, SocketAncillary}; |
| 576 | /// use std::io::IoSlice; |
| 577 | /// |
| 578 | /// fn main() -> std::io::Result<()> { |
| 579 | /// let socket = UnixStream::connect("/tmp/sock" )?; |
| 580 | /// let buf1 = [1; 8]; |
| 581 | /// let buf2 = [2; 16]; |
| 582 | /// let buf3 = [3; 8]; |
| 583 | /// let bufs = &[ |
| 584 | /// IoSlice::new(&buf1), |
| 585 | /// IoSlice::new(&buf2), |
| 586 | /// IoSlice::new(&buf3), |
| 587 | /// ][..]; |
| 588 | /// let fds = [0, 1, 2]; |
| 589 | /// let mut ancillary_buffer = [0; 128]; |
| 590 | /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]); |
| 591 | /// ancillary.add_fds(&fds[..]); |
| 592 | /// socket.send_vectored_with_ancillary(bufs, &mut ancillary) |
| 593 | /// .expect("send_vectored_with_ancillary function failed" ); |
| 594 | /// Ok(()) |
| 595 | /// } |
| 596 | /// ``` |
| 597 | #[cfg (any(doc, target_os = "android" , target_os = "linux" , target_os = "cygwin" ))] |
| 598 | #[unstable (feature = "unix_socket_ancillary_data" , issue = "76915" )] |
| 599 | pub fn send_vectored_with_ancillary( |
| 600 | &self, |
| 601 | bufs: &[IoSlice<'_>], |
| 602 | ancillary: &mut SocketAncillary<'_>, |
| 603 | ) -> io::Result<usize> { |
| 604 | send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary) |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 609 | impl io::Read for UnixStream { |
| 610 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 611 | io::Read::read(&mut &*self, buf) |
| 612 | } |
| 613 | |
| 614 | fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> { |
| 615 | io::Read::read_buf(&mut &*self, buf) |
| 616 | } |
| 617 | |
| 618 | fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
| 619 | io::Read::read_vectored(&mut &*self, bufs) |
| 620 | } |
| 621 | |
| 622 | #[inline ] |
| 623 | fn is_read_vectored(&self) -> bool { |
| 624 | io::Read::is_read_vectored(&&*self) |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 629 | impl<'a> io::Read for &'a UnixStream { |
| 630 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 631 | self.0.read(buf) |
| 632 | } |
| 633 | |
| 634 | fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> { |
| 635 | self.0.read_buf(buf) |
| 636 | } |
| 637 | |
| 638 | fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
| 639 | self.0.read_vectored(bufs) |
| 640 | } |
| 641 | |
| 642 | #[inline ] |
| 643 | fn is_read_vectored(&self) -> bool { |
| 644 | self.0.is_read_vectored() |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 649 | impl io::Write for UnixStream { |
| 650 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 651 | io::Write::write(&mut &*self, buf) |
| 652 | } |
| 653 | |
| 654 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
| 655 | io::Write::write_vectored(&mut &*self, bufs) |
| 656 | } |
| 657 | |
| 658 | #[inline ] |
| 659 | fn is_write_vectored(&self) -> bool { |
| 660 | io::Write::is_write_vectored(&&*self) |
| 661 | } |
| 662 | |
| 663 | fn flush(&mut self) -> io::Result<()> { |
| 664 | io::Write::flush(&mut &*self) |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 669 | impl<'a> io::Write for &'a UnixStream { |
| 670 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 671 | self.0.send_with_flags(buf, MSG_NOSIGNAL) |
| 672 | } |
| 673 | |
| 674 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
| 675 | self.0.write_vectored(bufs) |
| 676 | } |
| 677 | |
| 678 | #[inline ] |
| 679 | fn is_write_vectored(&self) -> bool { |
| 680 | self.0.is_write_vectored() |
| 681 | } |
| 682 | |
| 683 | #[inline ] |
| 684 | fn flush(&mut self) -> io::Result<()> { |
| 685 | Ok(()) |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 690 | impl AsRawFd for UnixStream { |
| 691 | #[inline ] |
| 692 | fn as_raw_fd(&self) -> RawFd { |
| 693 | self.0.as_raw_fd() |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 698 | impl FromRawFd for UnixStream { |
| 699 | #[inline ] |
| 700 | unsafe fn from_raw_fd(fd: RawFd) -> UnixStream { |
| 701 | UnixStream(Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd)))) |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | #[stable (feature = "unix_socket" , since = "1.10.0" )] |
| 706 | impl IntoRawFd for UnixStream { |
| 707 | #[inline ] |
| 708 | fn into_raw_fd(self) -> RawFd { |
| 709 | self.0.into_raw_fd() |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | #[stable (feature = "io_safety" , since = "1.63.0" )] |
| 714 | impl AsFd for UnixStream { |
| 715 | #[inline ] |
| 716 | fn as_fd(&self) -> BorrowedFd<'_> { |
| 717 | self.0.as_fd() |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | #[stable (feature = "io_safety" , since = "1.63.0" )] |
| 722 | impl From<UnixStream> for OwnedFd { |
| 723 | /// Takes ownership of a [`UnixStream`]'s socket file descriptor. |
| 724 | #[inline ] |
| 725 | fn from(unix_stream: UnixStream) -> OwnedFd { |
| 726 | unsafe { OwnedFd::from_raw_fd(unix_stream.into_raw_fd()) } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | #[stable (feature = "io_safety" , since = "1.63.0" )] |
| 731 | impl From<OwnedFd> for UnixStream { |
| 732 | #[inline ] |
| 733 | fn from(owned: OwnedFd) -> Self { |
| 734 | unsafe { Self::from_raw_fd(owned.into_raw_fd()) } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | impl AsInner<Socket> for UnixStream { |
| 739 | #[inline ] |
| 740 | fn as_inner(&self) -> &Socket { |
| 741 | &self.0 |
| 742 | } |
| 743 | } |
| 744 | |