]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/ext/net.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / src / libstd / sys / unix / ext / net.rs
1 #![stable(feature = "unix_socket", since = "1.10.0")]
2
3 //! Unix-specific networking functionality
4
5 // FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
6 #[cfg(not(unix))]
7 #[allow(non_camel_case_types)]
8 mod libc {
9 pub use libc::c_int;
10 pub type socklen_t = u32;
11 pub struct sockaddr;
12 #[derive(Clone)]
13 pub struct sockaddr_un;
14 }
15
16 use crate::ascii;
17 use crate::ffi::OsStr;
18 use crate::fmt;
19 use crate::io::{self, Initializer, IoSlice, IoSliceMut};
20 use crate::mem;
21 use crate::net::{self, Shutdown};
22 use crate::os::unix::ffi::OsStrExt;
23 use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
24 use crate::path::Path;
25 use crate::sys::net::Socket;
26 use crate::sys::{self, cvt};
27 use crate::sys_common::{self, AsInner, FromInner, IntoInner};
28 use crate::time::Duration;
29
30 #[cfg(any(
31 target_os = "linux",
32 target_os = "android",
33 target_os = "dragonfly",
34 target_os = "freebsd",
35 target_os = "openbsd",
36 target_os = "netbsd",
37 target_os = "haiku"
38 ))]
39 use libc::MSG_NOSIGNAL;
40 #[cfg(not(any(
41 target_os = "linux",
42 target_os = "android",
43 target_os = "dragonfly",
44 target_os = "freebsd",
45 target_os = "openbsd",
46 target_os = "netbsd",
47 target_os = "haiku"
48 )))]
49 const MSG_NOSIGNAL: libc::c_int = 0x0;
50
51 fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
52 // Work with an actual instance of the type since using a null pointer is UB
53 let base = addr as *const _ as usize;
54 let path = &addr.sun_path as *const _ as usize;
55 path - base
56 }
57
58 unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
59 let mut addr: libc::sockaddr_un = mem::zeroed();
60 addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
61
62 let bytes = path.as_os_str().as_bytes();
63
64 if bytes.contains(&0) {
65 return Err(io::Error::new(
66 io::ErrorKind::InvalidInput,
67 "paths may not contain interior null bytes",
68 ));
69 }
70
71 if bytes.len() >= addr.sun_path.len() {
72 return Err(io::Error::new(
73 io::ErrorKind::InvalidInput,
74 "path must be shorter than SUN_LEN",
75 ));
76 }
77 for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
78 *dst = *src as libc::c_char;
79 }
80 // null byte for pathname addresses is already there because we zeroed the
81 // struct
82
83 let mut len = sun_path_offset(&addr) + bytes.len();
84 match bytes.get(0) {
85 Some(&0) | None => {}
86 Some(_) => len += 1,
87 }
88 Ok((addr, len as libc::socklen_t))
89 }
90
91 enum AddressKind<'a> {
92 Unnamed,
93 Pathname(&'a Path),
94 Abstract(&'a [u8]),
95 }
96
97 /// An address associated with a Unix socket.
98 ///
99 /// # Examples
100 ///
101 /// ```
102 /// use std::os::unix::net::UnixListener;
103 ///
104 /// let socket = match UnixListener::bind("/tmp/sock") {
105 /// Ok(sock) => sock,
106 /// Err(e) => {
107 /// println!("Couldn't bind: {:?}", e);
108 /// return
109 /// }
110 /// };
111 /// let addr = socket.local_addr().expect("Couldn't get local address");
112 /// ```
113 #[derive(Clone)]
114 #[stable(feature = "unix_socket", since = "1.10.0")]
115 pub struct SocketAddr {
116 addr: libc::sockaddr_un,
117 len: libc::socklen_t,
118 }
119
120 impl SocketAddr {
121 fn new<F>(f: F) -> io::Result<SocketAddr>
122 where
123 F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int,
124 {
125 unsafe {
126 let mut addr: libc::sockaddr_un = mem::zeroed();
127 let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
128 cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
129 SocketAddr::from_parts(addr, len)
130 }
131 }
132
133 fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result<SocketAddr> {
134 if len == 0 {
135 // When there is a datagram from unnamed unix socket
136 // linux returns zero bytes of address
137 len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
138 } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
139 return Err(io::Error::new(
140 io::ErrorKind::InvalidInput,
141 "file descriptor did not correspond to a Unix socket",
142 ));
143 }
144
145 Ok(SocketAddr { addr, len })
146 }
147
148 /// Returns `true` if the address is unnamed.
149 ///
150 /// # Examples
151 ///
152 /// A named address:
153 ///
154 /// ```no_run
155 /// use std::os::unix::net::UnixListener;
156 ///
157 /// fn main() -> std::io::Result<()> {
158 /// let socket = UnixListener::bind("/tmp/sock")?;
159 /// let addr = socket.local_addr().expect("Couldn't get local address");
160 /// assert_eq!(addr.is_unnamed(), false);
161 /// Ok(())
162 /// }
163 /// ```
164 ///
165 /// An unnamed address:
166 ///
167 /// ```
168 /// use std::os::unix::net::UnixDatagram;
169 ///
170 /// fn main() -> std::io::Result<()> {
171 /// let socket = UnixDatagram::unbound()?;
172 /// let addr = socket.local_addr().expect("Couldn't get local address");
173 /// assert_eq!(addr.is_unnamed(), true);
174 /// Ok(())
175 /// }
176 /// ```
177 #[stable(feature = "unix_socket", since = "1.10.0")]
178 pub fn is_unnamed(&self) -> bool {
179 if let AddressKind::Unnamed = self.address() { true } else { false }
180 }
181
182 /// Returns the contents of this address if it is a `pathname` address.
183 ///
184 /// # Examples
185 ///
186 /// With a pathname:
187 ///
188 /// ```no_run
189 /// use std::os::unix::net::UnixListener;
190 /// use std::path::Path;
191 ///
192 /// fn main() -> std::io::Result<()> {
193 /// let socket = UnixListener::bind("/tmp/sock")?;
194 /// let addr = socket.local_addr().expect("Couldn't get local address");
195 /// assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
196 /// Ok(())
197 /// }
198 /// ```
199 ///
200 /// Without a pathname:
201 ///
202 /// ```
203 /// use std::os::unix::net::UnixDatagram;
204 ///
205 /// fn main() -> std::io::Result<()> {
206 /// let socket = UnixDatagram::unbound()?;
207 /// let addr = socket.local_addr().expect("Couldn't get local address");
208 /// assert_eq!(addr.as_pathname(), None);
209 /// Ok(())
210 /// }
211 /// ```
212 #[stable(feature = "unix_socket", since = "1.10.0")]
213 pub fn as_pathname(&self) -> Option<&Path> {
214 if let AddressKind::Pathname(path) = self.address() { Some(path) } else { None }
215 }
216
217 fn address(&self) -> AddressKind<'_> {
218 let len = self.len as usize - sun_path_offset(&self.addr);
219 let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
220
221 // macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
222 if len == 0
223 || (cfg!(not(any(target_os = "linux", target_os = "android")))
224 && self.addr.sun_path[0] == 0)
225 {
226 AddressKind::Unnamed
227 } else if self.addr.sun_path[0] == 0 {
228 AddressKind::Abstract(&path[1..len])
229 } else {
230 AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
231 }
232 }
233 }
234
235 #[stable(feature = "unix_socket", since = "1.10.0")]
236 impl fmt::Debug for SocketAddr {
237 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
238 match self.address() {
239 AddressKind::Unnamed => write!(fmt, "(unnamed)"),
240 AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
241 AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
242 }
243 }
244 }
245
246 struct AsciiEscaped<'a>(&'a [u8]);
247
248 impl<'a> fmt::Display for AsciiEscaped<'a> {
249 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
250 write!(fmt, "\"")?;
251 for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
252 write!(fmt, "{}", byte as char)?;
253 }
254 write!(fmt, "\"")
255 }
256 }
257
258 /// A Unix stream socket.
259 ///
260 /// # Examples
261 ///
262 /// ```no_run
263 /// use std::os::unix::net::UnixStream;
264 /// use std::io::prelude::*;
265 ///
266 /// fn main() -> std::io::Result<()> {
267 /// let mut stream = UnixStream::connect("/path/to/my/socket")?;
268 /// stream.write_all(b"hello world")?;
269 /// let mut response = String::new();
270 /// stream.read_to_string(&mut response)?;
271 /// println!("{}", response);
272 /// Ok(())
273 /// }
274 /// ```
275 #[stable(feature = "unix_socket", since = "1.10.0")]
276 pub struct UnixStream(Socket);
277
278 #[stable(feature = "unix_socket", since = "1.10.0")]
279 impl fmt::Debug for UnixStream {
280 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
281 let mut builder = fmt.debug_struct("UnixStream");
282 builder.field("fd", self.0.as_inner());
283 if let Ok(addr) = self.local_addr() {
284 builder.field("local", &addr);
285 }
286 if let Ok(addr) = self.peer_addr() {
287 builder.field("peer", &addr);
288 }
289 builder.finish()
290 }
291 }
292
293 impl UnixStream {
294 /// Connects to the socket named by `path`.
295 ///
296 /// # Examples
297 ///
298 /// ```no_run
299 /// use std::os::unix::net::UnixStream;
300 ///
301 /// let socket = match UnixStream::connect("/tmp/sock") {
302 /// Ok(sock) => sock,
303 /// Err(e) => {
304 /// println!("Couldn't connect: {:?}", e);
305 /// return
306 /// }
307 /// };
308 /// ```
309 #[stable(feature = "unix_socket", since = "1.10.0")]
310 pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
311 fn inner(path: &Path) -> io::Result<UnixStream> {
312 unsafe {
313 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
314 let (addr, len) = sockaddr_un(path)?;
315
316 cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
317 Ok(UnixStream(inner))
318 }
319 }
320 inner(path.as_ref())
321 }
322
323 /// Creates an unnamed pair of connected sockets.
324 ///
325 /// Returns two `UnixStream`s which are connected to each other.
326 ///
327 /// # Examples
328 ///
329 /// ```no_run
330 /// use std::os::unix::net::UnixStream;
331 ///
332 /// let (sock1, sock2) = match UnixStream::pair() {
333 /// Ok((sock1, sock2)) => (sock1, sock2),
334 /// Err(e) => {
335 /// println!("Couldn't create a pair of sockets: {:?}", e);
336 /// return
337 /// }
338 /// };
339 /// ```
340 #[stable(feature = "unix_socket", since = "1.10.0")]
341 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
342 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
343 Ok((UnixStream(i1), UnixStream(i2)))
344 }
345
346 /// Creates a new independently owned handle to the underlying socket.
347 ///
348 /// The returned `UnixStream` is a reference to the same stream that this
349 /// object references. Both handles will read and write the same stream of
350 /// data, and options set on one stream will be propagated to the other
351 /// stream.
352 ///
353 /// # Examples
354 ///
355 /// ```no_run
356 /// use std::os::unix::net::UnixStream;
357 ///
358 /// fn main() -> std::io::Result<()> {
359 /// let socket = UnixStream::connect("/tmp/sock")?;
360 /// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
361 /// Ok(())
362 /// }
363 /// ```
364 #[stable(feature = "unix_socket", since = "1.10.0")]
365 pub fn try_clone(&self) -> io::Result<UnixStream> {
366 self.0.duplicate().map(UnixStream)
367 }
368
369 /// Returns the socket address of the local half of this connection.
370 ///
371 /// # Examples
372 ///
373 /// ```no_run
374 /// use std::os::unix::net::UnixStream;
375 ///
376 /// fn main() -> std::io::Result<()> {
377 /// let socket = UnixStream::connect("/tmp/sock")?;
378 /// let addr = socket.local_addr().expect("Couldn't get local address");
379 /// Ok(())
380 /// }
381 /// ```
382 #[stable(feature = "unix_socket", since = "1.10.0")]
383 pub fn local_addr(&self) -> io::Result<SocketAddr> {
384 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
385 }
386
387 /// Returns the socket address of the remote half of this connection.
388 ///
389 /// # Examples
390 ///
391 /// ```no_run
392 /// use std::os::unix::net::UnixStream;
393 ///
394 /// fn main() -> std::io::Result<()> {
395 /// let socket = UnixStream::connect("/tmp/sock")?;
396 /// let addr = socket.peer_addr().expect("Couldn't get peer address");
397 /// Ok(())
398 /// }
399 /// ```
400 #[stable(feature = "unix_socket", since = "1.10.0")]
401 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
402 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
403 }
404
405 /// Sets the read timeout for the socket.
406 ///
407 /// If the provided value is [`None`], then [`read`] calls will block
408 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
409 /// method.
410 ///
411 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
412 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
413 /// [`read`]: ../../../../std/io/trait.Read.html#tymethod.read
414 /// [`Duration`]: ../../../../std/time/struct.Duration.html
415 ///
416 /// # Examples
417 ///
418 /// ```no_run
419 /// use std::os::unix::net::UnixStream;
420 /// use std::time::Duration;
421 ///
422 /// fn main() -> std::io::Result<()> {
423 /// let socket = UnixStream::connect("/tmp/sock")?;
424 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
425 /// Ok(())
426 /// }
427 /// ```
428 ///
429 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
430 /// method:
431 ///
432 /// ```no_run
433 /// use std::io;
434 /// use std::os::unix::net::UnixStream;
435 /// use std::time::Duration;
436 ///
437 /// fn main() -> std::io::Result<()> {
438 /// let socket = UnixStream::connect("/tmp/sock")?;
439 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
440 /// let err = result.unwrap_err();
441 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
442 /// Ok(())
443 /// }
444 /// ```
445 #[stable(feature = "unix_socket", since = "1.10.0")]
446 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
447 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
448 }
449
450 /// Sets the write timeout for the socket.
451 ///
452 /// If the provided value is [`None`], then [`write`] calls will block
453 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
454 /// passed to this method.
455 ///
456 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
457 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
458 /// [`write`]: ../../../../std/io/trait.Write.html#tymethod.write
459 /// [`Duration`]: ../../../../std/time/struct.Duration.html
460 ///
461 /// # Examples
462 ///
463 /// ```no_run
464 /// use std::os::unix::net::UnixStream;
465 /// use std::time::Duration;
466 ///
467 /// fn main() -> std::io::Result<()> {
468 /// let socket = UnixStream::connect("/tmp/sock")?;
469 /// socket.set_write_timeout(Some(Duration::new(1, 0)))
470 /// .expect("Couldn't set write timeout");
471 /// Ok(())
472 /// }
473 /// ```
474 ///
475 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
476 /// method:
477 ///
478 /// ```no_run
479 /// use std::io;
480 /// use std::net::UdpSocket;
481 /// use std::time::Duration;
482 ///
483 /// fn main() -> std::io::Result<()> {
484 /// let socket = UdpSocket::bind("127.0.0.1:34254")?;
485 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
486 /// let err = result.unwrap_err();
487 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
488 /// Ok(())
489 /// }
490 /// ```
491 #[stable(feature = "unix_socket", since = "1.10.0")]
492 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
493 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
494 }
495
496 /// Returns the read timeout of this socket.
497 ///
498 /// # Examples
499 ///
500 /// ```no_run
501 /// use std::os::unix::net::UnixStream;
502 /// use std::time::Duration;
503 ///
504 /// fn main() -> std::io::Result<()> {
505 /// let socket = UnixStream::connect("/tmp/sock")?;
506 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
507 /// assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
508 /// Ok(())
509 /// }
510 /// ```
511 #[stable(feature = "unix_socket", since = "1.10.0")]
512 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
513 self.0.timeout(libc::SO_RCVTIMEO)
514 }
515
516 /// Returns the write timeout of this socket.
517 ///
518 /// # Examples
519 ///
520 /// ```no_run
521 /// use std::os::unix::net::UnixStream;
522 /// use std::time::Duration;
523 ///
524 /// fn main() -> std::io::Result<()> {
525 /// let socket = UnixStream::connect("/tmp/sock")?;
526 /// socket.set_write_timeout(Some(Duration::new(1, 0)))
527 /// .expect("Couldn't set write timeout");
528 /// assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
529 /// Ok(())
530 /// }
531 /// ```
532 #[stable(feature = "unix_socket", since = "1.10.0")]
533 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
534 self.0.timeout(libc::SO_SNDTIMEO)
535 }
536
537 /// Moves the socket into or out of nonblocking mode.
538 ///
539 /// # Examples
540 ///
541 /// ```no_run
542 /// use std::os::unix::net::UnixStream;
543 ///
544 /// fn main() -> std::io::Result<()> {
545 /// let socket = UnixStream::connect("/tmp/sock")?;
546 /// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
547 /// Ok(())
548 /// }
549 /// ```
550 #[stable(feature = "unix_socket", since = "1.10.0")]
551 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
552 self.0.set_nonblocking(nonblocking)
553 }
554
555 /// Returns the value of the `SO_ERROR` option.
556 ///
557 /// # Examples
558 ///
559 /// ```no_run
560 /// use std::os::unix::net::UnixStream;
561 ///
562 /// fn main() -> std::io::Result<()> {
563 /// let socket = UnixStream::connect("/tmp/sock")?;
564 /// if let Ok(Some(err)) = socket.take_error() {
565 /// println!("Got error: {:?}", err);
566 /// }
567 /// Ok(())
568 /// }
569 /// ```
570 ///
571 /// # Platform specific
572 /// On Redox this always returns `None`.
573 #[stable(feature = "unix_socket", since = "1.10.0")]
574 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
575 self.0.take_error()
576 }
577
578 /// Shuts down the read, write, or both halves of this connection.
579 ///
580 /// This function will cause all pending and future I/O calls on the
581 /// specified portions to immediately return with an appropriate value
582 /// (see the documentation of [`Shutdown`]).
583 ///
584 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
585 ///
586 /// # Examples
587 ///
588 /// ```no_run
589 /// use std::os::unix::net::UnixStream;
590 /// use std::net::Shutdown;
591 ///
592 /// fn main() -> std::io::Result<()> {
593 /// let socket = UnixStream::connect("/tmp/sock")?;
594 /// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
595 /// Ok(())
596 /// }
597 /// ```
598 #[stable(feature = "unix_socket", since = "1.10.0")]
599 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
600 self.0.shutdown(how)
601 }
602 }
603
604 #[stable(feature = "unix_socket", since = "1.10.0")]
605 impl io::Read for UnixStream {
606 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
607 io::Read::read(&mut &*self, buf)
608 }
609
610 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
611 io::Read::read_vectored(&mut &*self, bufs)
612 }
613
614 #[inline]
615 fn is_read_vectored(&self) -> bool {
616 io::Read::is_read_vectored(&&*self)
617 }
618
619 #[inline]
620 unsafe fn initializer(&self) -> Initializer {
621 Initializer::nop()
622 }
623 }
624
625 #[stable(feature = "unix_socket", since = "1.10.0")]
626 impl<'a> io::Read for &'a UnixStream {
627 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
628 self.0.read(buf)
629 }
630
631 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
632 self.0.read_vectored(bufs)
633 }
634
635 #[inline]
636 fn is_read_vectored(&self) -> bool {
637 self.0.is_read_vectored()
638 }
639
640 #[inline]
641 unsafe fn initializer(&self) -> Initializer {
642 Initializer::nop()
643 }
644 }
645
646 #[stable(feature = "unix_socket", since = "1.10.0")]
647 impl io::Write for UnixStream {
648 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
649 io::Write::write(&mut &*self, buf)
650 }
651
652 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
653 io::Write::write_vectored(&mut &*self, bufs)
654 }
655
656 #[inline]
657 fn is_write_vectored(&self) -> bool {
658 io::Write::is_write_vectored(&&*self)
659 }
660
661 fn flush(&mut self) -> io::Result<()> {
662 io::Write::flush(&mut &*self)
663 }
664 }
665
666 #[stable(feature = "unix_socket", since = "1.10.0")]
667 impl<'a> io::Write for &'a UnixStream {
668 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
669 self.0.write(buf)
670 }
671
672 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
673 self.0.write_vectored(bufs)
674 }
675
676 #[inline]
677 fn is_write_vectored(&self) -> bool {
678 self.0.is_write_vectored()
679 }
680
681 fn flush(&mut self) -> io::Result<()> {
682 Ok(())
683 }
684 }
685
686 #[stable(feature = "unix_socket", since = "1.10.0")]
687 impl AsRawFd for UnixStream {
688 fn as_raw_fd(&self) -> RawFd {
689 *self.0.as_inner()
690 }
691 }
692
693 #[stable(feature = "unix_socket", since = "1.10.0")]
694 impl FromRawFd for UnixStream {
695 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
696 UnixStream(Socket::from_inner(fd))
697 }
698 }
699
700 #[stable(feature = "unix_socket", since = "1.10.0")]
701 impl IntoRawFd for UnixStream {
702 fn into_raw_fd(self) -> RawFd {
703 self.0.into_inner()
704 }
705 }
706
707 #[stable(feature = "rust1", since = "1.0.0")]
708 impl AsRawFd for net::TcpStream {
709 fn as_raw_fd(&self) -> RawFd {
710 *self.as_inner().socket().as_inner()
711 }
712 }
713
714 #[stable(feature = "rust1", since = "1.0.0")]
715 impl AsRawFd for net::TcpListener {
716 fn as_raw_fd(&self) -> RawFd {
717 *self.as_inner().socket().as_inner()
718 }
719 }
720
721 #[stable(feature = "rust1", since = "1.0.0")]
722 impl AsRawFd for net::UdpSocket {
723 fn as_raw_fd(&self) -> RawFd {
724 *self.as_inner().socket().as_inner()
725 }
726 }
727
728 #[stable(feature = "from_raw_os", since = "1.1.0")]
729 impl FromRawFd for net::TcpStream {
730 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
731 let socket = sys::net::Socket::from_inner(fd);
732 net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
733 }
734 }
735
736 #[stable(feature = "from_raw_os", since = "1.1.0")]
737 impl FromRawFd for net::TcpListener {
738 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
739 let socket = sys::net::Socket::from_inner(fd);
740 net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
741 }
742 }
743
744 #[stable(feature = "from_raw_os", since = "1.1.0")]
745 impl FromRawFd for net::UdpSocket {
746 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
747 let socket = sys::net::Socket::from_inner(fd);
748 net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
749 }
750 }
751
752 #[stable(feature = "into_raw_os", since = "1.4.0")]
753 impl IntoRawFd for net::TcpStream {
754 fn into_raw_fd(self) -> RawFd {
755 self.into_inner().into_socket().into_inner()
756 }
757 }
758 #[stable(feature = "into_raw_os", since = "1.4.0")]
759 impl IntoRawFd for net::TcpListener {
760 fn into_raw_fd(self) -> RawFd {
761 self.into_inner().into_socket().into_inner()
762 }
763 }
764 #[stable(feature = "into_raw_os", since = "1.4.0")]
765 impl IntoRawFd for net::UdpSocket {
766 fn into_raw_fd(self) -> RawFd {
767 self.into_inner().into_socket().into_inner()
768 }
769 }
770
771 /// A structure representing a Unix domain socket server.
772 ///
773 /// # Examples
774 ///
775 /// ```no_run
776 /// use std::thread;
777 /// use std::os::unix::net::{UnixStream, UnixListener};
778 ///
779 /// fn handle_client(stream: UnixStream) {
780 /// // ...
781 /// }
782 ///
783 /// fn main() -> std::io::Result<()> {
784 /// let listener = UnixListener::bind("/path/to/the/socket")?;
785 ///
786 /// // accept connections and process them, spawning a new thread for each one
787 /// for stream in listener.incoming() {
788 /// match stream {
789 /// Ok(stream) => {
790 /// /* connection succeeded */
791 /// thread::spawn(|| handle_client(stream));
792 /// }
793 /// Err(err) => {
794 /// /* connection failed */
795 /// break;
796 /// }
797 /// }
798 /// }
799 /// Ok(())
800 /// }
801 /// ```
802 #[stable(feature = "unix_socket", since = "1.10.0")]
803 pub struct UnixListener(Socket);
804
805 #[stable(feature = "unix_socket", since = "1.10.0")]
806 impl fmt::Debug for UnixListener {
807 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
808 let mut builder = fmt.debug_struct("UnixListener");
809 builder.field("fd", self.0.as_inner());
810 if let Ok(addr) = self.local_addr() {
811 builder.field("local", &addr);
812 }
813 builder.finish()
814 }
815 }
816
817 impl UnixListener {
818 /// Creates a new `UnixListener` bound to the specified socket.
819 ///
820 /// # Examples
821 ///
822 /// ```no_run
823 /// use std::os::unix::net::UnixListener;
824 ///
825 /// let listener = match UnixListener::bind("/path/to/the/socket") {
826 /// Ok(sock) => sock,
827 /// Err(e) => {
828 /// println!("Couldn't connect: {:?}", e);
829 /// return
830 /// }
831 /// };
832 /// ```
833 #[stable(feature = "unix_socket", since = "1.10.0")]
834 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
835 fn inner(path: &Path) -> io::Result<UnixListener> {
836 unsafe {
837 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
838 let (addr, len) = sockaddr_un(path)?;
839
840 cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len as _))?;
841 cvt(libc::listen(*inner.as_inner(), 128))?;
842
843 Ok(UnixListener(inner))
844 }
845 }
846 inner(path.as_ref())
847 }
848
849 /// Accepts a new incoming connection to this listener.
850 ///
851 /// This function will block the calling thread until a new Unix connection
852 /// is established. When established, the corresponding [`UnixStream`] and
853 /// the remote peer's address will be returned.
854 ///
855 /// [`UnixStream`]: ../../../../std/os/unix/net/struct.UnixStream.html
856 ///
857 /// # Examples
858 ///
859 /// ```no_run
860 /// use std::os::unix::net::UnixListener;
861 ///
862 /// fn main() -> std::io::Result<()> {
863 /// let listener = UnixListener::bind("/path/to/the/socket")?;
864 ///
865 /// match listener.accept() {
866 /// Ok((socket, addr)) => println!("Got a client: {:?}", addr),
867 /// Err(e) => println!("accept function failed: {:?}", e),
868 /// }
869 /// Ok(())
870 /// }
871 /// ```
872 #[stable(feature = "unix_socket", since = "1.10.0")]
873 pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
874 let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
875 let mut len = mem::size_of_val(&storage) as libc::socklen_t;
876 let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
877 let addr = SocketAddr::from_parts(storage, len)?;
878 Ok((UnixStream(sock), addr))
879 }
880
881 /// Creates a new independently owned handle to the underlying socket.
882 ///
883 /// The returned `UnixListener` is a reference to the same socket that this
884 /// object references. Both handles can be used to accept incoming
885 /// connections and options set on one listener will affect the other.
886 ///
887 /// # Examples
888 ///
889 /// ```no_run
890 /// use std::os::unix::net::UnixListener;
891 ///
892 /// fn main() -> std::io::Result<()> {
893 /// let listener = UnixListener::bind("/path/to/the/socket")?;
894 /// let listener_copy = listener.try_clone().expect("try_clone failed");
895 /// Ok(())
896 /// }
897 /// ```
898 #[stable(feature = "unix_socket", since = "1.10.0")]
899 pub fn try_clone(&self) -> io::Result<UnixListener> {
900 self.0.duplicate().map(UnixListener)
901 }
902
903 /// Returns the local socket address of this listener.
904 ///
905 /// # Examples
906 ///
907 /// ```no_run
908 /// use std::os::unix::net::UnixListener;
909 ///
910 /// fn main() -> std::io::Result<()> {
911 /// let listener = UnixListener::bind("/path/to/the/socket")?;
912 /// let addr = listener.local_addr().expect("Couldn't get local address");
913 /// Ok(())
914 /// }
915 /// ```
916 #[stable(feature = "unix_socket", since = "1.10.0")]
917 pub fn local_addr(&self) -> io::Result<SocketAddr> {
918 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
919 }
920
921 /// Moves the socket into or out of nonblocking mode.
922 ///
923 /// This will result in the `accept` operation becoming nonblocking,
924 /// i.e., immediately returning from their calls. If the IO operation is
925 /// successful, `Ok` is returned and no further action is required. If the
926 /// IO operation could not be completed and needs to be retried, an error
927 /// with kind [`io::ErrorKind::WouldBlock`] is returned.
928 ///
929 /// # Examples
930 ///
931 /// ```no_run
932 /// use std::os::unix::net::UnixListener;
933 ///
934 /// fn main() -> std::io::Result<()> {
935 /// let listener = UnixListener::bind("/path/to/the/socket")?;
936 /// listener.set_nonblocking(true).expect("Couldn't set non blocking");
937 /// Ok(())
938 /// }
939 /// ```
940 ///
941 /// [`io::ErrorKind::WouldBlock`]: ../../../io/enum.ErrorKind.html#variant.WouldBlock
942 #[stable(feature = "unix_socket", since = "1.10.0")]
943 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
944 self.0.set_nonblocking(nonblocking)
945 }
946
947 /// Returns the value of the `SO_ERROR` option.
948 ///
949 /// # Examples
950 ///
951 /// ```no_run
952 /// use std::os::unix::net::UnixListener;
953 ///
954 /// fn main() -> std::io::Result<()> {
955 /// let listener = UnixListener::bind("/tmp/sock")?;
956 ///
957 /// if let Ok(Some(err)) = listener.take_error() {
958 /// println!("Got error: {:?}", err);
959 /// }
960 /// Ok(())
961 /// }
962 /// ```
963 ///
964 /// # Platform specific
965 /// On Redox this always returns `None`.
966 #[stable(feature = "unix_socket", since = "1.10.0")]
967 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
968 self.0.take_error()
969 }
970
971 /// Returns an iterator over incoming connections.
972 ///
973 /// The iterator will never return [`None`] and will also not yield the
974 /// peer's [`SocketAddr`] structure.
975 ///
976 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
977 /// [`SocketAddr`]: struct.SocketAddr.html
978 ///
979 /// # Examples
980 ///
981 /// ```no_run
982 /// use std::thread;
983 /// use std::os::unix::net::{UnixStream, UnixListener};
984 ///
985 /// fn handle_client(stream: UnixStream) {
986 /// // ...
987 /// }
988 ///
989 /// fn main() -> std::io::Result<()> {
990 /// let listener = UnixListener::bind("/path/to/the/socket")?;
991 ///
992 /// for stream in listener.incoming() {
993 /// match stream {
994 /// Ok(stream) => {
995 /// thread::spawn(|| handle_client(stream));
996 /// }
997 /// Err(err) => {
998 /// break;
999 /// }
1000 /// }
1001 /// }
1002 /// Ok(())
1003 /// }
1004 /// ```
1005 #[stable(feature = "unix_socket", since = "1.10.0")]
1006 pub fn incoming(&self) -> Incoming<'_> {
1007 Incoming { listener: self }
1008 }
1009 }
1010
1011 #[stable(feature = "unix_socket", since = "1.10.0")]
1012 impl AsRawFd for UnixListener {
1013 fn as_raw_fd(&self) -> RawFd {
1014 *self.0.as_inner()
1015 }
1016 }
1017
1018 #[stable(feature = "unix_socket", since = "1.10.0")]
1019 impl FromRawFd for UnixListener {
1020 unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
1021 UnixListener(Socket::from_inner(fd))
1022 }
1023 }
1024
1025 #[stable(feature = "unix_socket", since = "1.10.0")]
1026 impl IntoRawFd for UnixListener {
1027 fn into_raw_fd(self) -> RawFd {
1028 self.0.into_inner()
1029 }
1030 }
1031
1032 #[stable(feature = "unix_socket", since = "1.10.0")]
1033 impl<'a> IntoIterator for &'a UnixListener {
1034 type Item = io::Result<UnixStream>;
1035 type IntoIter = Incoming<'a>;
1036
1037 fn into_iter(self) -> Incoming<'a> {
1038 self.incoming()
1039 }
1040 }
1041
1042 /// An iterator over incoming connections to a [`UnixListener`].
1043 ///
1044 /// It will never return [`None`].
1045 ///
1046 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1047 /// [`UnixListener`]: struct.UnixListener.html
1048 ///
1049 /// # Examples
1050 ///
1051 /// ```no_run
1052 /// use std::thread;
1053 /// use std::os::unix::net::{UnixStream, UnixListener};
1054 ///
1055 /// fn handle_client(stream: UnixStream) {
1056 /// // ...
1057 /// }
1058 ///
1059 /// fn main() -> std::io::Result<()> {
1060 /// let listener = UnixListener::bind("/path/to/the/socket")?;
1061 ///
1062 /// for stream in listener.incoming() {
1063 /// match stream {
1064 /// Ok(stream) => {
1065 /// thread::spawn(|| handle_client(stream));
1066 /// }
1067 /// Err(err) => {
1068 /// break;
1069 /// }
1070 /// }
1071 /// }
1072 /// Ok(())
1073 /// }
1074 /// ```
1075 #[derive(Debug)]
1076 #[stable(feature = "unix_socket", since = "1.10.0")]
1077 pub struct Incoming<'a> {
1078 listener: &'a UnixListener,
1079 }
1080
1081 #[stable(feature = "unix_socket", since = "1.10.0")]
1082 impl<'a> Iterator for Incoming<'a> {
1083 type Item = io::Result<UnixStream>;
1084
1085 fn next(&mut self) -> Option<io::Result<UnixStream>> {
1086 Some(self.listener.accept().map(|s| s.0))
1087 }
1088
1089 fn size_hint(&self) -> (usize, Option<usize>) {
1090 (usize::MAX, None)
1091 }
1092 }
1093
1094 /// A Unix datagram socket.
1095 ///
1096 /// # Examples
1097 ///
1098 /// ```no_run
1099 /// use std::os::unix::net::UnixDatagram;
1100 ///
1101 /// fn main() -> std::io::Result<()> {
1102 /// let socket = UnixDatagram::bind("/path/to/my/socket")?;
1103 /// socket.send_to(b"hello world", "/path/to/other/socket")?;
1104 /// let mut buf = [0; 100];
1105 /// let (count, address) = socket.recv_from(&mut buf)?;
1106 /// println!("socket {:?} sent {:?}", address, &buf[..count]);
1107 /// Ok(())
1108 /// }
1109 /// ```
1110 #[stable(feature = "unix_socket", since = "1.10.0")]
1111 pub struct UnixDatagram(Socket);
1112
1113 #[stable(feature = "unix_socket", since = "1.10.0")]
1114 impl fmt::Debug for UnixDatagram {
1115 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1116 let mut builder = fmt.debug_struct("UnixDatagram");
1117 builder.field("fd", self.0.as_inner());
1118 if let Ok(addr) = self.local_addr() {
1119 builder.field("local", &addr);
1120 }
1121 if let Ok(addr) = self.peer_addr() {
1122 builder.field("peer", &addr);
1123 }
1124 builder.finish()
1125 }
1126 }
1127
1128 impl UnixDatagram {
1129 /// Creates a Unix datagram socket bound to the given path.
1130 ///
1131 /// # Examples
1132 ///
1133 /// ```no_run
1134 /// use std::os::unix::net::UnixDatagram;
1135 ///
1136 /// let sock = match UnixDatagram::bind("/path/to/the/socket") {
1137 /// Ok(sock) => sock,
1138 /// Err(e) => {
1139 /// println!("Couldn't bind: {:?}", e);
1140 /// return
1141 /// }
1142 /// };
1143 /// ```
1144 #[stable(feature = "unix_socket", since = "1.10.0")]
1145 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
1146 fn inner(path: &Path) -> io::Result<UnixDatagram> {
1147 unsafe {
1148 let socket = UnixDatagram::unbound()?;
1149 let (addr, len) = sockaddr_un(path)?;
1150
1151 cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len as _))?;
1152
1153 Ok(socket)
1154 }
1155 }
1156 inner(path.as_ref())
1157 }
1158
1159 /// Creates a Unix Datagram socket which is not bound to any address.
1160 ///
1161 /// # Examples
1162 ///
1163 /// ```no_run
1164 /// use std::os::unix::net::UnixDatagram;
1165 ///
1166 /// let sock = match UnixDatagram::unbound() {
1167 /// Ok(sock) => sock,
1168 /// Err(e) => {
1169 /// println!("Couldn't unbound: {:?}", e);
1170 /// return
1171 /// }
1172 /// };
1173 /// ```
1174 #[stable(feature = "unix_socket", since = "1.10.0")]
1175 pub fn unbound() -> io::Result<UnixDatagram> {
1176 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1177 Ok(UnixDatagram(inner))
1178 }
1179
1180 /// Creates an unnamed pair of connected sockets.
1181 ///
1182 /// Returns two `UnixDatagrams`s which are connected to each other.
1183 ///
1184 /// # Examples
1185 ///
1186 /// ```no_run
1187 /// use std::os::unix::net::UnixDatagram;
1188 ///
1189 /// let (sock1, sock2) = match UnixDatagram::pair() {
1190 /// Ok((sock1, sock2)) => (sock1, sock2),
1191 /// Err(e) => {
1192 /// println!("Couldn't unbound: {:?}", e);
1193 /// return
1194 /// }
1195 /// };
1196 /// ```
1197 #[stable(feature = "unix_socket", since = "1.10.0")]
1198 pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
1199 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1200 Ok((UnixDatagram(i1), UnixDatagram(i2)))
1201 }
1202
1203 /// Connects the socket to the specified address.
1204 ///
1205 /// The [`send`] method may be used to send data to the specified address.
1206 /// [`recv`] and [`recv_from`] will only receive data from that address.
1207 ///
1208 /// [`send`]: #method.send
1209 /// [`recv`]: #method.recv
1210 /// [`recv_from`]: #method.recv_from
1211 ///
1212 /// # Examples
1213 ///
1214 /// ```no_run
1215 /// use std::os::unix::net::UnixDatagram;
1216 ///
1217 /// fn main() -> std::io::Result<()> {
1218 /// let sock = UnixDatagram::unbound()?;
1219 /// match sock.connect("/path/to/the/socket") {
1220 /// Ok(sock) => sock,
1221 /// Err(e) => {
1222 /// println!("Couldn't connect: {:?}", e);
1223 /// return Err(e)
1224 /// }
1225 /// };
1226 /// Ok(())
1227 /// }
1228 /// ```
1229 #[stable(feature = "unix_socket", since = "1.10.0")]
1230 pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1231 fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
1232 unsafe {
1233 let (addr, len) = sockaddr_un(path)?;
1234
1235 cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
1236
1237 Ok(())
1238 }
1239 }
1240 inner(self, path.as_ref())
1241 }
1242
1243 /// Creates a new independently owned handle to the underlying socket.
1244 ///
1245 /// The returned `UnixDatagram` is a reference to the same socket that this
1246 /// object references. Both handles can be used to accept incoming
1247 /// connections and options set on one side will affect the other.
1248 ///
1249 /// # Examples
1250 ///
1251 /// ```no_run
1252 /// use std::os::unix::net::UnixDatagram;
1253 ///
1254 /// fn main() -> std::io::Result<()> {
1255 /// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1256 /// let sock_copy = sock.try_clone().expect("try_clone failed");
1257 /// Ok(())
1258 /// }
1259 /// ```
1260 #[stable(feature = "unix_socket", since = "1.10.0")]
1261 pub fn try_clone(&self) -> io::Result<UnixDatagram> {
1262 self.0.duplicate().map(UnixDatagram)
1263 }
1264
1265 /// Returns the address of this socket.
1266 ///
1267 /// # Examples
1268 ///
1269 /// ```no_run
1270 /// use std::os::unix::net::UnixDatagram;
1271 ///
1272 /// fn main() -> std::io::Result<()> {
1273 /// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1274 /// let addr = sock.local_addr().expect("Couldn't get local address");
1275 /// Ok(())
1276 /// }
1277 /// ```
1278 #[stable(feature = "unix_socket", since = "1.10.0")]
1279 pub fn local_addr(&self) -> io::Result<SocketAddr> {
1280 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
1281 }
1282
1283 /// Returns the address of this socket's peer.
1284 ///
1285 /// The [`connect`] method will connect the socket to a peer.
1286 ///
1287 /// [`connect`]: #method.connect
1288 ///
1289 /// # Examples
1290 ///
1291 /// ```no_run
1292 /// use std::os::unix::net::UnixDatagram;
1293 ///
1294 /// fn main() -> std::io::Result<()> {
1295 /// let sock = UnixDatagram::unbound()?;
1296 /// sock.connect("/path/to/the/socket")?;
1297 ///
1298 /// let addr = sock.peer_addr().expect("Couldn't get peer address");
1299 /// Ok(())
1300 /// }
1301 /// ```
1302 #[stable(feature = "unix_socket", since = "1.10.0")]
1303 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
1304 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
1305 }
1306
1307 /// Receives data from the socket.
1308 ///
1309 /// On success, returns the number of bytes read and the address from
1310 /// whence the data came.
1311 ///
1312 /// # Examples
1313 ///
1314 /// ```no_run
1315 /// use std::os::unix::net::UnixDatagram;
1316 ///
1317 /// fn main() -> std::io::Result<()> {
1318 /// let sock = UnixDatagram::unbound()?;
1319 /// let mut buf = vec![0; 10];
1320 /// let (size, sender) = sock.recv_from(buf.as_mut_slice())?;
1321 /// println!("received {} bytes from {:?}", size, sender);
1322 /// Ok(())
1323 /// }
1324 /// ```
1325 #[stable(feature = "unix_socket", since = "1.10.0")]
1326 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
1327 let mut count = 0;
1328 let addr = SocketAddr::new(|addr, len| unsafe {
1329 count = libc::recvfrom(
1330 *self.0.as_inner(),
1331 buf.as_mut_ptr() as *mut _,
1332 buf.len(),
1333 0,
1334 addr,
1335 len,
1336 );
1337 if count > 0 {
1338 1
1339 } else if count == 0 {
1340 0
1341 } else {
1342 -1
1343 }
1344 })?;
1345
1346 Ok((count as usize, addr))
1347 }
1348
1349 /// Receives data from the socket.
1350 ///
1351 /// On success, returns the number of bytes read.
1352 ///
1353 /// # Examples
1354 ///
1355 /// ```no_run
1356 /// use std::os::unix::net::UnixDatagram;
1357 ///
1358 /// fn main() -> std::io::Result<()> {
1359 /// let sock = UnixDatagram::bind("/path/to/the/socket")?;
1360 /// let mut buf = vec![0; 10];
1361 /// sock.recv(buf.as_mut_slice()).expect("recv function failed");
1362 /// Ok(())
1363 /// }
1364 /// ```
1365 #[stable(feature = "unix_socket", since = "1.10.0")]
1366 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
1367 self.0.read(buf)
1368 }
1369
1370 /// Sends data on the socket to the specified address.
1371 ///
1372 /// On success, returns the number of bytes written.
1373 ///
1374 /// # Examples
1375 ///
1376 /// ```no_run
1377 /// use std::os::unix::net::UnixDatagram;
1378 ///
1379 /// fn main() -> std::io::Result<()> {
1380 /// let sock = UnixDatagram::unbound()?;
1381 /// sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
1382 /// Ok(())
1383 /// }
1384 /// ```
1385 #[stable(feature = "unix_socket", since = "1.10.0")]
1386 pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
1387 fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
1388 unsafe {
1389 let (addr, len) = sockaddr_un(path)?;
1390
1391 let count = cvt(libc::sendto(
1392 *d.0.as_inner(),
1393 buf.as_ptr() as *const _,
1394 buf.len(),
1395 MSG_NOSIGNAL,
1396 &addr as *const _ as *const _,
1397 len,
1398 ))?;
1399 Ok(count as usize)
1400 }
1401 }
1402 inner(self, buf, path.as_ref())
1403 }
1404
1405 /// Sends data on the socket to the socket's peer.
1406 ///
1407 /// The peer address may be set by the `connect` method, and this method
1408 /// will return an error if the socket has not already been connected.
1409 ///
1410 /// On success, returns the number of bytes written.
1411 ///
1412 /// # Examples
1413 ///
1414 /// ```no_run
1415 /// use std::os::unix::net::UnixDatagram;
1416 ///
1417 /// fn main() -> std::io::Result<()> {
1418 /// let sock = UnixDatagram::unbound()?;
1419 /// sock.connect("/some/sock").expect("Couldn't connect");
1420 /// sock.send(b"omelette au fromage").expect("send_to function failed");
1421 /// Ok(())
1422 /// }
1423 /// ```
1424 #[stable(feature = "unix_socket", since = "1.10.0")]
1425 pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
1426 self.0.write(buf)
1427 }
1428
1429 /// Sets the read timeout for the socket.
1430 ///
1431 /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
1432 /// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
1433 /// is passed to this method.
1434 ///
1435 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1436 /// [`Err`]: ../../../../std/result/enum.Result.html#variant.Err
1437 /// [`recv`]: #method.recv
1438 /// [`recv_from`]: #method.recv_from
1439 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1440 ///
1441 /// # Examples
1442 ///
1443 /// ```
1444 /// use std::os::unix::net::UnixDatagram;
1445 /// use std::time::Duration;
1446 ///
1447 /// fn main() -> std::io::Result<()> {
1448 /// let sock = UnixDatagram::unbound()?;
1449 /// sock.set_read_timeout(Some(Duration::new(1, 0)))
1450 /// .expect("set_read_timeout function failed");
1451 /// Ok(())
1452 /// }
1453 /// ```
1454 ///
1455 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1456 /// method:
1457 ///
1458 /// ```no_run
1459 /// use std::io;
1460 /// use std::os::unix::net::UnixDatagram;
1461 /// use std::time::Duration;
1462 ///
1463 /// fn main() -> std::io::Result<()> {
1464 /// let socket = UnixDatagram::unbound()?;
1465 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
1466 /// let err = result.unwrap_err();
1467 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1468 /// Ok(())
1469 /// }
1470 /// ```
1471 #[stable(feature = "unix_socket", since = "1.10.0")]
1472 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1473 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
1474 }
1475
1476 /// Sets the write timeout for the socket.
1477 ///
1478 /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
1479 /// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
1480 /// method.
1481 ///
1482 /// [`None`]: ../../../../std/option/enum.Option.html#variant.None
1483 /// [`send`]: #method.send
1484 /// [`send_to`]: #method.send_to
1485 /// [`Duration`]: ../../../../std/time/struct.Duration.html
1486 ///
1487 /// # Examples
1488 ///
1489 /// ```
1490 /// use std::os::unix::net::UnixDatagram;
1491 /// use std::time::Duration;
1492 ///
1493 /// fn main() -> std::io::Result<()> {
1494 /// let sock = UnixDatagram::unbound()?;
1495 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1496 /// .expect("set_write_timeout function failed");
1497 /// Ok(())
1498 /// }
1499 /// ```
1500 ///
1501 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1502 /// method:
1503 ///
1504 /// ```no_run
1505 /// use std::io;
1506 /// use std::os::unix::net::UnixDatagram;
1507 /// use std::time::Duration;
1508 ///
1509 /// fn main() -> std::io::Result<()> {
1510 /// let socket = UnixDatagram::unbound()?;
1511 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
1512 /// let err = result.unwrap_err();
1513 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1514 /// Ok(())
1515 /// }
1516 /// ```
1517 #[stable(feature = "unix_socket", since = "1.10.0")]
1518 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1519 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
1520 }
1521
1522 /// Returns the read timeout of this socket.
1523 ///
1524 /// # Examples
1525 ///
1526 /// ```
1527 /// use std::os::unix::net::UnixDatagram;
1528 /// use std::time::Duration;
1529 ///
1530 /// fn main() -> std::io::Result<()> {
1531 /// let sock = UnixDatagram::unbound()?;
1532 /// sock.set_read_timeout(Some(Duration::new(1, 0)))
1533 /// .expect("set_read_timeout function failed");
1534 /// assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0)));
1535 /// Ok(())
1536 /// }
1537 /// ```
1538 #[stable(feature = "unix_socket", since = "1.10.0")]
1539 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
1540 self.0.timeout(libc::SO_RCVTIMEO)
1541 }
1542
1543 /// Returns the write timeout of this socket.
1544 ///
1545 /// # Examples
1546 ///
1547 /// ```
1548 /// use std::os::unix::net::UnixDatagram;
1549 /// use std::time::Duration;
1550 ///
1551 /// fn main() -> std::io::Result<()> {
1552 /// let sock = UnixDatagram::unbound()?;
1553 /// sock.set_write_timeout(Some(Duration::new(1, 0)))
1554 /// .expect("set_write_timeout function failed");
1555 /// assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0)));
1556 /// Ok(())
1557 /// }
1558 /// ```
1559 #[stable(feature = "unix_socket", since = "1.10.0")]
1560 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
1561 self.0.timeout(libc::SO_SNDTIMEO)
1562 }
1563
1564 /// Moves the socket into or out of nonblocking mode.
1565 ///
1566 /// # Examples
1567 ///
1568 /// ```
1569 /// use std::os::unix::net::UnixDatagram;
1570 ///
1571 /// fn main() -> std::io::Result<()> {
1572 /// let sock = UnixDatagram::unbound()?;
1573 /// sock.set_nonblocking(true).expect("set_nonblocking function failed");
1574 /// Ok(())
1575 /// }
1576 /// ```
1577 #[stable(feature = "unix_socket", since = "1.10.0")]
1578 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
1579 self.0.set_nonblocking(nonblocking)
1580 }
1581
1582 /// Returns the value of the `SO_ERROR` option.
1583 ///
1584 /// # Examples
1585 ///
1586 /// ```no_run
1587 /// use std::os::unix::net::UnixDatagram;
1588 ///
1589 /// fn main() -> std::io::Result<()> {
1590 /// let sock = UnixDatagram::unbound()?;
1591 /// if let Ok(Some(err)) = sock.take_error() {
1592 /// println!("Got error: {:?}", err);
1593 /// }
1594 /// Ok(())
1595 /// }
1596 /// ```
1597 #[stable(feature = "unix_socket", since = "1.10.0")]
1598 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
1599 self.0.take_error()
1600 }
1601
1602 /// Shut down the read, write, or both halves of this connection.
1603 ///
1604 /// This function will cause all pending and future I/O calls on the
1605 /// specified portions to immediately return with an appropriate value
1606 /// (see the documentation of [`Shutdown`]).
1607 ///
1608 /// [`Shutdown`]: ../../../../std/net/enum.Shutdown.html
1609 ///
1610 /// ```no_run
1611 /// use std::os::unix::net::UnixDatagram;
1612 /// use std::net::Shutdown;
1613 ///
1614 /// fn main() -> std::io::Result<()> {
1615 /// let sock = UnixDatagram::unbound()?;
1616 /// sock.shutdown(Shutdown::Both).expect("shutdown function failed");
1617 /// Ok(())
1618 /// }
1619 /// ```
1620 #[stable(feature = "unix_socket", since = "1.10.0")]
1621 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
1622 self.0.shutdown(how)
1623 }
1624 }
1625
1626 #[stable(feature = "unix_socket", since = "1.10.0")]
1627 impl AsRawFd for UnixDatagram {
1628 fn as_raw_fd(&self) -> RawFd {
1629 *self.0.as_inner()
1630 }
1631 }
1632
1633 #[stable(feature = "unix_socket", since = "1.10.0")]
1634 impl FromRawFd for UnixDatagram {
1635 unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
1636 UnixDatagram(Socket::from_inner(fd))
1637 }
1638 }
1639
1640 #[stable(feature = "unix_socket", since = "1.10.0")]
1641 impl IntoRawFd for UnixDatagram {
1642 fn into_raw_fd(self) -> RawFd {
1643 self.0.into_inner()
1644 }
1645 }
1646
1647 #[cfg(all(test, not(target_os = "emscripten")))]
1648 mod test {
1649 use crate::io::prelude::*;
1650 use crate::io::{self, ErrorKind};
1651 use crate::sys_common::io::test::tmpdir;
1652 use crate::thread;
1653 use crate::time::Duration;
1654
1655 use super::*;
1656
1657 macro_rules! or_panic {
1658 ($e:expr) => {
1659 match $e {
1660 Ok(e) => e,
1661 Err(e) => panic!("{}", e),
1662 }
1663 };
1664 }
1665
1666 #[test]
1667 fn basic() {
1668 let dir = tmpdir();
1669 let socket_path = dir.path().join("sock");
1670 let msg1 = b"hello";
1671 let msg2 = b"world!";
1672
1673 let listener = or_panic!(UnixListener::bind(&socket_path));
1674 let thread = thread::spawn(move || {
1675 let mut stream = or_panic!(listener.accept()).0;
1676 let mut buf = [0; 5];
1677 or_panic!(stream.read(&mut buf));
1678 assert_eq!(&msg1[..], &buf[..]);
1679 or_panic!(stream.write_all(msg2));
1680 });
1681
1682 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1683 assert_eq!(Some(&*socket_path), stream.peer_addr().unwrap().as_pathname());
1684 or_panic!(stream.write_all(msg1));
1685 let mut buf = vec![];
1686 or_panic!(stream.read_to_end(&mut buf));
1687 assert_eq!(&msg2[..], &buf[..]);
1688 drop(stream);
1689
1690 thread.join().unwrap();
1691 }
1692
1693 #[test]
1694 fn vectored() {
1695 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
1696
1697 let len = or_panic!(s1.write_vectored(&[
1698 IoSlice::new(b"hello"),
1699 IoSlice::new(b" "),
1700 IoSlice::new(b"world!")
1701 ],));
1702 assert_eq!(len, 12);
1703
1704 let mut buf1 = [0; 6];
1705 let mut buf2 = [0; 7];
1706 let len = or_panic!(
1707 s2.read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)],)
1708 );
1709 assert_eq!(len, 12);
1710 assert_eq!(&buf1, b"hello ");
1711 assert_eq!(&buf2, b"world!\0");
1712 }
1713
1714 #[test]
1715 fn pair() {
1716 let msg1 = b"hello";
1717 let msg2 = b"world!";
1718
1719 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
1720 let thread = thread::spawn(move || {
1721 // s1 must be moved in or the test will hang!
1722 let mut buf = [0; 5];
1723 or_panic!(s1.read(&mut buf));
1724 assert_eq!(&msg1[..], &buf[..]);
1725 or_panic!(s1.write_all(msg2));
1726 });
1727
1728 or_panic!(s2.write_all(msg1));
1729 let mut buf = vec![];
1730 or_panic!(s2.read_to_end(&mut buf));
1731 assert_eq!(&msg2[..], &buf[..]);
1732 drop(s2);
1733
1734 thread.join().unwrap();
1735 }
1736
1737 #[test]
1738 fn try_clone() {
1739 let dir = tmpdir();
1740 let socket_path = dir.path().join("sock");
1741 let msg1 = b"hello";
1742 let msg2 = b"world";
1743
1744 let listener = or_panic!(UnixListener::bind(&socket_path));
1745 let thread = thread::spawn(move || {
1746 let mut stream = or_panic!(listener.accept()).0;
1747 or_panic!(stream.write_all(msg1));
1748 or_panic!(stream.write_all(msg2));
1749 });
1750
1751 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1752 let mut stream2 = or_panic!(stream.try_clone());
1753
1754 let mut buf = [0; 5];
1755 or_panic!(stream.read(&mut buf));
1756 assert_eq!(&msg1[..], &buf[..]);
1757 or_panic!(stream2.read(&mut buf));
1758 assert_eq!(&msg2[..], &buf[..]);
1759
1760 thread.join().unwrap();
1761 }
1762
1763 #[test]
1764 fn iter() {
1765 let dir = tmpdir();
1766 let socket_path = dir.path().join("sock");
1767
1768 let listener = or_panic!(UnixListener::bind(&socket_path));
1769 let thread = thread::spawn(move || {
1770 for stream in listener.incoming().take(2) {
1771 let mut stream = or_panic!(stream);
1772 let mut buf = [0];
1773 or_panic!(stream.read(&mut buf));
1774 }
1775 });
1776
1777 for _ in 0..2 {
1778 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1779 or_panic!(stream.write_all(&[0]));
1780 }
1781
1782 thread.join().unwrap();
1783 }
1784
1785 #[test]
1786 fn long_path() {
1787 let dir = tmpdir();
1788 let socket_path = dir.path().join(
1789 "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa\
1790 sasdfasdfasdasdfasdfasdfadfasdfasdfasdfasdfasdf",
1791 );
1792 match UnixStream::connect(&socket_path) {
1793 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1794 Err(e) => panic!("unexpected error {}", e),
1795 Ok(_) => panic!("unexpected success"),
1796 }
1797
1798 match UnixListener::bind(&socket_path) {
1799 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1800 Err(e) => panic!("unexpected error {}", e),
1801 Ok(_) => panic!("unexpected success"),
1802 }
1803
1804 match UnixDatagram::bind(&socket_path) {
1805 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1806 Err(e) => panic!("unexpected error {}", e),
1807 Ok(_) => panic!("unexpected success"),
1808 }
1809 }
1810
1811 #[test]
1812 fn timeouts() {
1813 let dir = tmpdir();
1814 let socket_path = dir.path().join("sock");
1815
1816 let _listener = or_panic!(UnixListener::bind(&socket_path));
1817
1818 let stream = or_panic!(UnixStream::connect(&socket_path));
1819 let dur = Duration::new(15410, 0);
1820
1821 assert_eq!(None, or_panic!(stream.read_timeout()));
1822
1823 or_panic!(stream.set_read_timeout(Some(dur)));
1824 assert_eq!(Some(dur), or_panic!(stream.read_timeout()));
1825
1826 assert_eq!(None, or_panic!(stream.write_timeout()));
1827
1828 or_panic!(stream.set_write_timeout(Some(dur)));
1829 assert_eq!(Some(dur), or_panic!(stream.write_timeout()));
1830
1831 or_panic!(stream.set_read_timeout(None));
1832 assert_eq!(None, or_panic!(stream.read_timeout()));
1833
1834 or_panic!(stream.set_write_timeout(None));
1835 assert_eq!(None, or_panic!(stream.write_timeout()));
1836 }
1837
1838 #[test]
1839 fn test_read_timeout() {
1840 let dir = tmpdir();
1841 let socket_path = dir.path().join("sock");
1842
1843 let _listener = or_panic!(UnixListener::bind(&socket_path));
1844
1845 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1846 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1847
1848 let mut buf = [0; 10];
1849 let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
1850 assert!(
1851 kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
1852 "unexpected_error: {:?}",
1853 kind
1854 );
1855 }
1856
1857 #[test]
1858 fn test_read_with_timeout() {
1859 let dir = tmpdir();
1860 let socket_path = dir.path().join("sock");
1861
1862 let listener = or_panic!(UnixListener::bind(&socket_path));
1863
1864 let mut stream = or_panic!(UnixStream::connect(&socket_path));
1865 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
1866
1867 let mut other_end = or_panic!(listener.accept()).0;
1868 or_panic!(other_end.write_all(b"hello world"));
1869
1870 let mut buf = [0; 11];
1871 or_panic!(stream.read(&mut buf));
1872 assert_eq!(b"hello world", &buf[..]);
1873
1874 let kind = stream.read_exact(&mut buf).err().expect("expected error").kind();
1875 assert!(
1876 kind == ErrorKind::WouldBlock || kind == ErrorKind::TimedOut,
1877 "unexpected_error: {:?}",
1878 kind
1879 );
1880 }
1881
1882 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
1883 // when passed zero Durations
1884 #[test]
1885 fn test_unix_stream_timeout_zero_duration() {
1886 let dir = tmpdir();
1887 let socket_path = dir.path().join("sock");
1888
1889 let listener = or_panic!(UnixListener::bind(&socket_path));
1890 let stream = or_panic!(UnixStream::connect(&socket_path));
1891
1892 let result = stream.set_write_timeout(Some(Duration::new(0, 0)));
1893 let err = result.unwrap_err();
1894 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1895
1896 let result = stream.set_read_timeout(Some(Duration::new(0, 0)));
1897 let err = result.unwrap_err();
1898 assert_eq!(err.kind(), ErrorKind::InvalidInput);
1899
1900 drop(listener);
1901 }
1902
1903 #[test]
1904 fn test_unix_datagram() {
1905 let dir = tmpdir();
1906 let path1 = dir.path().join("sock1");
1907 let path2 = dir.path().join("sock2");
1908
1909 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1910 let sock2 = or_panic!(UnixDatagram::bind(&path2));
1911
1912 let msg = b"hello world";
1913 or_panic!(sock1.send_to(msg, &path2));
1914 let mut buf = [0; 11];
1915 or_panic!(sock2.recv_from(&mut buf));
1916 assert_eq!(msg, &buf[..]);
1917 }
1918
1919 #[test]
1920 fn test_unnamed_unix_datagram() {
1921 let dir = tmpdir();
1922 let path1 = dir.path().join("sock1");
1923
1924 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1925 let sock2 = or_panic!(UnixDatagram::unbound());
1926
1927 let msg = b"hello world";
1928 or_panic!(sock2.send_to(msg, &path1));
1929 let mut buf = [0; 11];
1930 let (usize, addr) = or_panic!(sock1.recv_from(&mut buf));
1931 assert_eq!(usize, 11);
1932 assert!(addr.is_unnamed());
1933 assert_eq!(msg, &buf[..]);
1934 }
1935
1936 #[test]
1937 fn test_connect_unix_datagram() {
1938 let dir = tmpdir();
1939 let path1 = dir.path().join("sock1");
1940 let path2 = dir.path().join("sock2");
1941
1942 let bsock1 = or_panic!(UnixDatagram::bind(&path1));
1943 let bsock2 = or_panic!(UnixDatagram::bind(&path2));
1944 let sock = or_panic!(UnixDatagram::unbound());
1945 or_panic!(sock.connect(&path1));
1946
1947 // Check send()
1948 let msg = b"hello there";
1949 or_panic!(sock.send(msg));
1950 let mut buf = [0; 11];
1951 let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
1952 assert_eq!(usize, 11);
1953 assert!(addr.is_unnamed());
1954 assert_eq!(msg, &buf[..]);
1955
1956 // Changing default socket works too
1957 or_panic!(sock.connect(&path2));
1958 or_panic!(sock.send(msg));
1959 or_panic!(bsock2.recv_from(&mut buf));
1960 }
1961
1962 #[test]
1963 fn test_unix_datagram_recv() {
1964 let dir = tmpdir();
1965 let path1 = dir.path().join("sock1");
1966
1967 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1968 let sock2 = or_panic!(UnixDatagram::unbound());
1969 or_panic!(sock2.connect(&path1));
1970
1971 let msg = b"hello world";
1972 or_panic!(sock2.send(msg));
1973 let mut buf = [0; 11];
1974 let size = or_panic!(sock1.recv(&mut buf));
1975 assert_eq!(size, 11);
1976 assert_eq!(msg, &buf[..]);
1977 }
1978
1979 #[test]
1980 fn datagram_pair() {
1981 let msg1 = b"hello";
1982 let msg2 = b"world!";
1983
1984 let (s1, s2) = or_panic!(UnixDatagram::pair());
1985 let thread = thread::spawn(move || {
1986 // s1 must be moved in or the test will hang!
1987 let mut buf = [0; 5];
1988 or_panic!(s1.recv(&mut buf));
1989 assert_eq!(&msg1[..], &buf[..]);
1990 or_panic!(s1.send(msg2));
1991 });
1992
1993 or_panic!(s2.send(msg1));
1994 let mut buf = [0; 6];
1995 or_panic!(s2.recv(&mut buf));
1996 assert_eq!(&msg2[..], &buf[..]);
1997 drop(s2);
1998
1999 thread.join().unwrap();
2000 }
2001
2002 // Ensure the `set_read_timeout` and `set_write_timeout` calls return errors
2003 // when passed zero Durations
2004 #[test]
2005 fn test_unix_datagram_timeout_zero_duration() {
2006 let dir = tmpdir();
2007 let path = dir.path().join("sock");
2008
2009 let datagram = or_panic!(UnixDatagram::bind(&path));
2010
2011 let result = datagram.set_write_timeout(Some(Duration::new(0, 0)));
2012 let err = result.unwrap_err();
2013 assert_eq!(err.kind(), ErrorKind::InvalidInput);
2014
2015 let result = datagram.set_read_timeout(Some(Duration::new(0, 0)));
2016 let err = result.unwrap_err();
2017 assert_eq!(err.kind(), ErrorKind::InvalidInput);
2018 }
2019
2020 #[test]
2021 fn abstract_namespace_not_allowed() {
2022 assert!(UnixStream::connect("\0asdf").is_err());
2023 }
2024 }