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