]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/ext/net.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libstd / sys / unix / ext / net.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 #![unstable(feature = "unix_socket", reason = "newly added", issue = "32312")]
11
12 //! Unix-specific networking functionality
13
14 use libc;
15
16 use prelude::v1::*;
17 use ascii;
18 use ffi::OsStr;
19 use fmt;
20 use io;
21 use mem;
22 use net::Shutdown;
23 use os::unix::ffi::OsStrExt;
24 use os::unix::io::{RawFd, AsRawFd, FromRawFd, IntoRawFd};
25 use path::Path;
26 use time::Duration;
27 use sys::cvt;
28 use sys::net::Socket;
29 use sys_common::{AsInner, FromInner, IntoInner};
30
31 fn sun_path_offset() -> usize {
32 unsafe {
33 // Work with an actual instance of the type since using a null pointer is UB
34 let addr: libc::sockaddr_un = mem::uninitialized();
35 let base = &addr as *const _ as usize;
36 let path = &addr.sun_path as *const _ as usize;
37 path - base
38 }
39 }
40
41 unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
42 let mut addr: libc::sockaddr_un = mem::zeroed();
43 addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
44
45 let bytes = path.as_os_str().as_bytes();
46
47 if bytes.contains(&0) {
48 return Err(io::Error::new(io::ErrorKind::InvalidInput,
49 "paths may not contain interior null bytes"));
50 }
51
52 if bytes.len() >= addr.sun_path.len() {
53 return Err(io::Error::new(io::ErrorKind::InvalidInput,
54 "path must be shorter than SUN_LEN"));
55 }
56 for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
57 *dst = *src as libc::c_char;
58 }
59 // null byte for pathname addresses is already there because we zeroed the
60 // struct
61
62 let mut len = sun_path_offset() + bytes.len();
63 match bytes.get(0) {
64 Some(&0) | None => {}
65 Some(_) => len += 1,
66 }
67 Ok((addr, len as libc::socklen_t))
68 }
69
70 enum AddressKind<'a> {
71 Unnamed,
72 Pathname(&'a Path),
73 Abstract(&'a [u8]),
74 }
75
76 /// An address associated with a Unix socket.
77 #[derive(Clone)]
78 pub struct SocketAddr {
79 addr: libc::sockaddr_un,
80 len: libc::socklen_t,
81 }
82
83 impl SocketAddr {
84 fn new<F>(f: F) -> io::Result<SocketAddr>
85 where F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int
86 {
87 unsafe {
88 let mut addr: libc::sockaddr_un = mem::zeroed();
89 let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
90 cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
91 SocketAddr::from_parts(addr, len)
92 }
93 }
94
95 fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result<SocketAddr> {
96 if len == 0 {
97 // When there is a datagram from unnamed unix socket
98 // linux returns zero bytes of address
99 len = sun_path_offset() as libc::socklen_t; // i.e. zero-length address
100 } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
101 return Err(io::Error::new(io::ErrorKind::InvalidInput,
102 "file descriptor did not correspond to a Unix socket"));
103 }
104
105 Ok(SocketAddr {
106 addr: addr,
107 len: len,
108 })
109 }
110
111 /// Returns true iff the address is unnamed.
112 pub fn is_unnamed(&self) -> bool {
113 if let AddressKind::Unnamed = self.address() {
114 true
115 } else {
116 false
117 }
118 }
119
120 /// Returns the contents of this address if it is a `pathname` address.
121 pub fn as_pathname(&self) -> Option<&Path> {
122 if let AddressKind::Pathname(path) = self.address() {
123 Some(path)
124 } else {
125 None
126 }
127 }
128
129 fn address<'a>(&'a self) -> AddressKind<'a> {
130 let len = self.len as usize - sun_path_offset();
131 let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
132
133 // OSX seems to return a len of 16 and a zeroed sun_path for unnamed addresses
134 if len == 0 || (cfg!(not(target_os = "linux")) && self.addr.sun_path[0] == 0) {
135 AddressKind::Unnamed
136 } else if self.addr.sun_path[0] == 0 {
137 AddressKind::Abstract(&path[1..len])
138 } else {
139 AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
140 }
141 }
142 }
143
144 impl fmt::Debug for SocketAddr {
145 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
146 match self.address() {
147 AddressKind::Unnamed => write!(fmt, "(unnamed)"),
148 AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
149 AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
150 }
151 }
152 }
153
154 struct AsciiEscaped<'a>(&'a [u8]);
155
156 impl<'a> fmt::Display for AsciiEscaped<'a> {
157 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
158 write!(fmt, "\"")?;
159 for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
160 write!(fmt, "{}", byte as char)?;
161 }
162 write!(fmt, "\"")
163 }
164 }
165
166 /// A Unix stream socket.
167 ///
168 /// # Examples
169 ///
170 /// ```rust,no_run
171 /// #![feature(unix_socket)]
172 ///
173 /// use std::os::unix::net::UnixStream;
174 /// use std::io::prelude::*;
175 ///
176 /// let mut stream = UnixStream::connect("/path/to/my/socket").unwrap();
177 /// stream.write_all(b"hello world").unwrap();
178 /// let mut response = String::new();
179 /// stream.read_to_string(&mut response).unwrap();
180 /// println!("{}", response);
181 /// ```
182 pub struct UnixStream(Socket);
183
184 impl fmt::Debug for UnixStream {
185 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
186 let mut builder = fmt.debug_struct("UnixStream");
187 builder.field("fd", self.0.as_inner());
188 if let Ok(addr) = self.local_addr() {
189 builder.field("local", &addr);
190 }
191 if let Ok(addr) = self.peer_addr() {
192 builder.field("peer", &addr);
193 }
194 builder.finish()
195 }
196 }
197
198 impl UnixStream {
199 /// Connects to the socket named by `path`.
200 pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
201 fn inner(path: &Path) -> io::Result<UnixStream> {
202 unsafe {
203 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
204 let (addr, len) = sockaddr_un(path)?;
205
206 cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
207 Ok(UnixStream(inner))
208 }
209 }
210 inner(path.as_ref())
211 }
212
213 /// Creates an unnamed pair of connected sockets.
214 ///
215 /// Returns two `UnixStream`s which are connected to each other.
216 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
217 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
218 Ok((UnixStream(i1), UnixStream(i2)))
219 }
220
221 /// Creates a new independently owned handle to the underlying socket.
222 ///
223 /// The returned `UnixStream` is a reference to the same stream that this
224 /// object references. Both handles will read and write the same stream of
225 /// data, and options set on one stream will be propogated to the other
226 /// stream.
227 pub fn try_clone(&self) -> io::Result<UnixStream> {
228 self.0.duplicate().map(UnixStream)
229 }
230
231 /// Returns the socket address of the local half of this connection.
232 pub fn local_addr(&self) -> io::Result<SocketAddr> {
233 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
234 }
235
236 /// Returns the socket address of the remote half of this connection.
237 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
238 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
239 }
240
241 /// Sets the read timeout for the socket.
242 ///
243 /// If the provided value is `None`, then `read` calls will block
244 /// indefinitely. It is an error to pass the zero `Duration` to this
245 /// method.
246 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
247 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
248 }
249
250 /// Sets the write timeout for the socket.
251 ///
252 /// If the provided value is `None`, then `write` calls will block
253 /// indefinitely. It is an error to pass the zero `Duration` to this
254 /// method.
255 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
256 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
257 }
258
259 /// Returns the read timeout of this socket.
260 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
261 self.0.timeout(libc::SO_RCVTIMEO)
262 }
263
264 /// Returns the write timeout of this socket.
265 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
266 self.0.timeout(libc::SO_SNDTIMEO)
267 }
268
269 /// Moves the socket into or out of nonblocking mode.
270 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
271 self.0.set_nonblocking(nonblocking)
272 }
273
274 /// Returns the value of the `SO_ERROR` option.
275 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
276 self.0.take_error()
277 }
278
279 /// Shuts down the read, write, or both halves of this connection.
280 ///
281 /// This function will cause all pending and future I/O calls on the
282 /// specified portions to immediately return with an appropriate value
283 /// (see the documentation of `Shutdown`).
284 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
285 self.0.shutdown(how)
286 }
287 }
288
289 impl io::Read for UnixStream {
290 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
291 io::Read::read(&mut &*self, buf)
292 }
293
294 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
295 io::Read::read_to_end(&mut &*self, buf)
296 }
297 }
298
299 impl<'a> io::Read for &'a UnixStream {
300 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
301 self.0.read(buf)
302 }
303
304 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
305 self.0.read_to_end(buf)
306 }
307 }
308
309 impl io::Write for UnixStream {
310 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
311 io::Write::write(&mut &*self, buf)
312 }
313
314 fn flush(&mut self) -> io::Result<()> {
315 io::Write::flush(&mut &*self)
316 }
317 }
318
319 impl<'a> io::Write for &'a UnixStream {
320 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
321 self.0.write(buf)
322 }
323
324 fn flush(&mut self) -> io::Result<()> {
325 Ok(())
326 }
327 }
328
329 impl AsRawFd for UnixStream {
330 fn as_raw_fd(&self) -> RawFd {
331 *self.0.as_inner()
332 }
333 }
334
335 impl FromRawFd for UnixStream {
336 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
337 UnixStream(Socket::from_inner(fd))
338 }
339 }
340
341 impl IntoRawFd for UnixStream {
342 fn into_raw_fd(self) -> RawFd {
343 self.0.into_inner()
344 }
345 }
346
347 /// A structure representing a Unix domain socket server.
348 ///
349 /// # Examples
350 ///
351 /// ```rust,no_run
352 /// #![feature(unix_socket)]
353 ///
354 /// use std::thread;
355 /// use std::os::unix::net::{UnixStream, UnixListener};
356 ///
357 /// fn handle_client(stream: UnixStream) {
358 /// // ...
359 /// }
360 ///
361 /// let listener = UnixListener::bind("/path/to/the/socket").unwrap();
362 ///
363 /// // accept connections and process them, spawning a new thread for each one
364 /// for stream in listener.incoming() {
365 /// match stream {
366 /// Ok(stream) => {
367 /// /* connection succeeded */
368 /// thread::spawn(|| handle_client(stream));
369 /// }
370 /// Err(err) => {
371 /// /* connection failed */
372 /// break;
373 /// }
374 /// }
375 /// }
376 ///
377 /// // close the listener socket
378 /// drop(listener);
379 /// ```
380 pub struct UnixListener(Socket);
381
382 impl fmt::Debug for UnixListener {
383 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
384 let mut builder = fmt.debug_struct("UnixListener");
385 builder.field("fd", self.0.as_inner());
386 if let Ok(addr) = self.local_addr() {
387 builder.field("local", &addr);
388 }
389 builder.finish()
390 }
391 }
392
393 impl UnixListener {
394 /// Creates a new `UnixListener` bound to the specified socket.
395 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
396 fn inner(path: &Path) -> io::Result<UnixListener> {
397 unsafe {
398 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
399 let (addr, len) = sockaddr_un(path)?;
400
401 cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len))?;
402 cvt(libc::listen(*inner.as_inner(), 128))?;
403
404 Ok(UnixListener(inner))
405 }
406 }
407 inner(path.as_ref())
408 }
409
410 /// Accepts a new incoming connection to this listener.
411 ///
412 /// This function will block the calling thread until a new Unix connection
413 /// is established. When established, the corersponding `UnixStream` and
414 /// the remote peer's address will be returned.
415 pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
416 let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
417 let mut len = mem::size_of_val(&storage) as libc::socklen_t;
418 let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
419 let addr = SocketAddr::from_parts(storage, len)?;
420 Ok((UnixStream(sock), addr))
421 }
422
423 /// Creates a new independently owned handle to the underlying socket.
424 ///
425 /// The returned `UnixListener` is a reference to the same socket that this
426 /// object references. Both handles can be used to accept incoming
427 /// connections and options set on one listener will affect the other.
428 pub fn try_clone(&self) -> io::Result<UnixListener> {
429 self.0.duplicate().map(UnixListener)
430 }
431
432 /// Returns the local socket address of this listener.
433 pub fn local_addr(&self) -> io::Result<SocketAddr> {
434 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
435 }
436
437 /// Moves the socket into or out of nonblocking mode.
438 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
439 self.0.set_nonblocking(nonblocking)
440 }
441
442 /// Returns the value of the `SO_ERROR` option.
443 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
444 self.0.take_error()
445 }
446
447 /// Returns an iterator over incoming connections.
448 ///
449 /// The iterator will never return `None` and will also not yield the
450 /// peer's `SocketAddr` structure.
451 pub fn incoming<'a>(&'a self) -> Incoming<'a> {
452 Incoming { listener: self }
453 }
454 }
455
456 impl AsRawFd for UnixListener {
457 fn as_raw_fd(&self) -> RawFd {
458 *self.0.as_inner()
459 }
460 }
461
462 impl FromRawFd for UnixListener {
463 unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
464 UnixListener(Socket::from_inner(fd))
465 }
466 }
467
468 impl IntoRawFd for UnixListener {
469 fn into_raw_fd(self) -> RawFd {
470 self.0.into_inner()
471 }
472 }
473
474 impl<'a> IntoIterator for &'a UnixListener {
475 type Item = io::Result<UnixStream>;
476 type IntoIter = Incoming<'a>;
477
478 fn into_iter(self) -> Incoming<'a> {
479 self.incoming()
480 }
481 }
482
483 /// An iterator over incoming connections to a `UnixListener`.
484 ///
485 /// It will never return `None`.
486 #[derive(Debug)]
487 pub struct Incoming<'a> {
488 listener: &'a UnixListener,
489 }
490
491 impl<'a> Iterator for Incoming<'a> {
492 type Item = io::Result<UnixStream>;
493
494 fn next(&mut self) -> Option<io::Result<UnixStream>> {
495 Some(self.listener.accept().map(|s| s.0))
496 }
497
498 fn size_hint(&self) -> (usize, Option<usize>) {
499 (usize::max_value(), None)
500 }
501 }
502
503 /// A Unix datagram socket.
504 ///
505 /// # Examples
506 ///
507 /// ```rust,no_run
508 /// #![feature(unix_socket)]
509 ///
510 /// use std::os::unix::net::UnixDatagram;
511 ///
512 /// let socket = UnixDatagram::bind("/path/to/my/socket").unwrap();
513 /// socket.send_to(b"hello world", "/path/to/other/socket").unwrap();
514 /// let mut buf = [0; 100];
515 /// let (count, address) = socket.recv_from(&mut buf).unwrap();
516 /// println!("socket {:?} sent {:?}", address, &buf[..count]);
517 /// ```
518 pub struct UnixDatagram(Socket);
519
520 impl fmt::Debug for UnixDatagram {
521 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
522 let mut builder = fmt.debug_struct("UnixDatagram");
523 builder.field("fd", self.0.as_inner());
524 if let Ok(addr) = self.local_addr() {
525 builder.field("local", &addr);
526 }
527 if let Ok(addr) = self.peer_addr() {
528 builder.field("peer", &addr);
529 }
530 builder.finish()
531 }
532 }
533
534 impl UnixDatagram {
535 /// Creates a Unix datagram socket bound to the given path.
536 pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
537 fn inner(path: &Path) -> io::Result<UnixDatagram> {
538 unsafe {
539 let socket = UnixDatagram::unbound()?;
540 let (addr, len) = sockaddr_un(path)?;
541
542 cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len))?;
543
544 Ok(socket)
545 }
546 }
547 inner(path.as_ref())
548 }
549
550 /// Creates a Unix Datagram socket which is not bound to any address.
551 pub fn unbound() -> io::Result<UnixDatagram> {
552 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
553 Ok(UnixDatagram(inner))
554 }
555
556 /// Create an unnamed pair of connected sockets.
557 ///
558 /// Returns two `UnixDatagrams`s which are connected to each other.
559 pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
560 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
561 Ok((UnixDatagram(i1), UnixDatagram(i2)))
562 }
563
564 /// Connects the socket to the specified address.
565 ///
566 /// The `send` method may be used to send data to the specified address.
567 /// `recv` and `recv_from` will only receive data from that address.
568 pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
569 fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
570 unsafe {
571 let (addr, len) = sockaddr_un(path)?;
572
573 cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
574
575 Ok(())
576 }
577 }
578 inner(self, path.as_ref())
579 }
580
581 /// Creates a new independently owned handle to the underlying socket.
582 ///
583 /// The returned `UnixListener` is a reference to the same socket that this
584 /// object references. Both handles can be used to accept incoming
585 /// connections and options set on one listener will affect the other.
586 pub fn try_clone(&self) -> io::Result<UnixDatagram> {
587 self.0.duplicate().map(UnixDatagram)
588 }
589
590 /// Returns the address of this socket.
591 pub fn local_addr(&self) -> io::Result<SocketAddr> {
592 SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
593 }
594
595 /// Returns the address of this socket's peer.
596 ///
597 /// The `connect` method will connect the socket to a peer.
598 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
599 SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
600 }
601
602 /// Receives data from the socket.
603 ///
604 /// On success, returns the number of bytes read and the address from
605 /// whence the data came.
606 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
607 let mut count = 0;
608 let addr = SocketAddr::new(|addr, len| {
609 unsafe {
610 count = libc::recvfrom(*self.0.as_inner(),
611 buf.as_mut_ptr() as *mut _,
612 buf.len(),
613 0,
614 addr,
615 len);
616 if count > 0 {
617 1
618 } else if count == 0 {
619 0
620 } else {
621 -1
622 }
623 }
624 })?;
625
626 Ok((count as usize, addr))
627 }
628
629 /// Receives data from the socket.
630 ///
631 /// On success, returns the number of bytes read.
632 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
633 self.0.read(buf)
634 }
635
636 /// Sends data on the socket to the specified address.
637 ///
638 /// On success, returns the number of bytes written.
639 pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
640 fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
641 unsafe {
642 let (addr, len) = sockaddr_un(path)?;
643
644 let count = cvt(libc::sendto(*d.0.as_inner(),
645 buf.as_ptr() as *const _,
646 buf.len(),
647 0,
648 &addr as *const _ as *const _,
649 len))?;
650 Ok(count as usize)
651 }
652 }
653 inner(self, buf, path.as_ref())
654 }
655
656 /// Sends data on the socket to the socket's peer.
657 ///
658 /// The peer address may be set by the `connect` method, and this method
659 /// will return an error if the socket has not already been connected.
660 ///
661 /// On success, returns the number of bytes written.
662 pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
663 self.0.write(buf)
664 }
665
666 /// Sets the read timeout for the socket.
667 ///
668 /// If the provided value is `None`, then `recv` and `recv_from` calls will
669 /// block indefinitely. It is an error to pass the zero `Duration` to this
670 /// method.
671 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
672 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
673 }
674
675 /// Sets the write timeout for the socket.
676 ///
677 /// If the provided value is `None`, then `send` and `send_to` calls will
678 /// block indefinitely. It is an error to pass the zero `Duration` to this
679 /// method.
680 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
681 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
682 }
683
684 /// Returns the read timeout of this socket.
685 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
686 self.0.timeout(libc::SO_RCVTIMEO)
687 }
688
689 /// Returns the write timeout of this socket.
690 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
691 self.0.timeout(libc::SO_SNDTIMEO)
692 }
693
694 /// Moves the socket into or out of nonblocking mode.
695 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
696 self.0.set_nonblocking(nonblocking)
697 }
698
699 /// Returns the value of the `SO_ERROR` option.
700 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
701 self.0.take_error()
702 }
703
704 /// Shut down the read, write, or both halves of this connection.
705 ///
706 /// This function will cause all pending and future I/O calls on the
707 /// specified portions to immediately return with an appropriate value
708 /// (see the documentation of `Shutdown`).
709 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
710 self.0.shutdown(how)
711 }
712 }
713
714 impl AsRawFd for UnixDatagram {
715 fn as_raw_fd(&self) -> RawFd {
716 *self.0.as_inner()
717 }
718 }
719
720 impl FromRawFd for UnixDatagram {
721 unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
722 UnixDatagram(Socket::from_inner(fd))
723 }
724 }
725
726 impl IntoRawFd for UnixDatagram {
727 fn into_raw_fd(self) -> RawFd {
728 self.0.into_inner()
729 }
730 }
731
732 #[cfg(test)]
733 mod test {
734 use prelude::v1::*;
735 use thread;
736 use io;
737 use io::prelude::*;
738 use time::Duration;
739 use sys_common::io::test::tmpdir;
740
741 use super::*;
742
743 macro_rules! or_panic {
744 ($e:expr) => {
745 match $e {
746 Ok(e) => e,
747 Err(e) => panic!("{}", e),
748 }
749 }
750 }
751
752 #[test]
753 fn basic() {
754 let dir = tmpdir();
755 let socket_path = dir.path().join("sock");
756 let msg1 = b"hello";
757 let msg2 = b"world!";
758
759 let listener = or_panic!(UnixListener::bind(&socket_path));
760 let thread = thread::spawn(move || {
761 let mut stream = or_panic!(listener.accept()).0;
762 let mut buf = [0; 5];
763 or_panic!(stream.read(&mut buf));
764 assert_eq!(&msg1[..], &buf[..]);
765 or_panic!(stream.write_all(msg2));
766 });
767
768 let mut stream = or_panic!(UnixStream::connect(&socket_path));
769 assert_eq!(Some(&*socket_path),
770 stream.peer_addr().unwrap().as_pathname());
771 or_panic!(stream.write_all(msg1));
772 let mut buf = vec![];
773 or_panic!(stream.read_to_end(&mut buf));
774 assert_eq!(&msg2[..], &buf[..]);
775 drop(stream);
776
777 thread.join().unwrap();
778 }
779
780 #[test]
781 fn pair() {
782 let msg1 = b"hello";
783 let msg2 = b"world!";
784
785 let (mut s1, mut s2) = or_panic!(UnixStream::pair());
786 let thread = thread::spawn(move || {
787 // s1 must be moved in or the test will hang!
788 let mut buf = [0; 5];
789 or_panic!(s1.read(&mut buf));
790 assert_eq!(&msg1[..], &buf[..]);
791 or_panic!(s1.write_all(msg2));
792 });
793
794 or_panic!(s2.write_all(msg1));
795 let mut buf = vec![];
796 or_panic!(s2.read_to_end(&mut buf));
797 assert_eq!(&msg2[..], &buf[..]);
798 drop(s2);
799
800 thread.join().unwrap();
801 }
802
803 #[test]
804 fn try_clone() {
805 let dir = tmpdir();
806 let socket_path = dir.path().join("sock");
807 let msg1 = b"hello";
808 let msg2 = b"world";
809
810 let listener = or_panic!(UnixListener::bind(&socket_path));
811 let thread = thread::spawn(move || {
812 let mut stream = or_panic!(listener.accept()).0;
813 or_panic!(stream.write_all(msg1));
814 or_panic!(stream.write_all(msg2));
815 });
816
817 let mut stream = or_panic!(UnixStream::connect(&socket_path));
818 let mut stream2 = or_panic!(stream.try_clone());
819
820 let mut buf = [0; 5];
821 or_panic!(stream.read(&mut buf));
822 assert_eq!(&msg1[..], &buf[..]);
823 or_panic!(stream2.read(&mut buf));
824 assert_eq!(&msg2[..], &buf[..]);
825
826 thread.join().unwrap();
827 }
828
829 #[test]
830 fn iter() {
831 let dir = tmpdir();
832 let socket_path = dir.path().join("sock");
833
834 let listener = or_panic!(UnixListener::bind(&socket_path));
835 let thread = thread::spawn(move || {
836 for stream in listener.incoming().take(2) {
837 let mut stream = or_panic!(stream);
838 let mut buf = [0];
839 or_panic!(stream.read(&mut buf));
840 }
841 });
842
843 for _ in 0..2 {
844 let mut stream = or_panic!(UnixStream::connect(&socket_path));
845 or_panic!(stream.write_all(&[0]));
846 }
847
848 thread.join().unwrap();
849 }
850
851 #[test]
852 fn long_path() {
853 let dir = tmpdir();
854 let socket_path = dir.path()
855 .join("asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa\
856 sasdfasdfasdasdfasdfasdfadfasdfasdfasdfasdfasdf");
857 match UnixStream::connect(&socket_path) {
858 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
859 Err(e) => panic!("unexpected error {}", e),
860 Ok(_) => panic!("unexpected success"),
861 }
862
863 match UnixListener::bind(&socket_path) {
864 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
865 Err(e) => panic!("unexpected error {}", e),
866 Ok(_) => panic!("unexpected success"),
867 }
868
869 match UnixDatagram::bind(&socket_path) {
870 Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
871 Err(e) => panic!("unexpected error {}", e),
872 Ok(_) => panic!("unexpected success"),
873 }
874 }
875
876 #[test]
877 fn timeouts() {
878 let dir = tmpdir();
879 let socket_path = dir.path().join("sock");
880
881 let _listener = or_panic!(UnixListener::bind(&socket_path));
882
883 let stream = or_panic!(UnixStream::connect(&socket_path));
884 let dur = Duration::new(15410, 0);
885
886 assert_eq!(None, or_panic!(stream.read_timeout()));
887
888 or_panic!(stream.set_read_timeout(Some(dur)));
889 assert_eq!(Some(dur), or_panic!(stream.read_timeout()));
890
891 assert_eq!(None, or_panic!(stream.write_timeout()));
892
893 or_panic!(stream.set_write_timeout(Some(dur)));
894 assert_eq!(Some(dur), or_panic!(stream.write_timeout()));
895
896 or_panic!(stream.set_read_timeout(None));
897 assert_eq!(None, or_panic!(stream.read_timeout()));
898
899 or_panic!(stream.set_write_timeout(None));
900 assert_eq!(None, or_panic!(stream.write_timeout()));
901 }
902
903 #[test]
904 fn test_read_timeout() {
905 let dir = tmpdir();
906 let socket_path = dir.path().join("sock");
907
908 let _listener = or_panic!(UnixListener::bind(&socket_path));
909
910 let mut stream = or_panic!(UnixStream::connect(&socket_path));
911 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
912
913 let mut buf = [0; 10];
914 let kind = stream.read(&mut buf).err().expect("expected error").kind();
915 assert!(kind == io::ErrorKind::WouldBlock || kind == io::ErrorKind::TimedOut);
916 }
917
918 #[test]
919 fn test_read_with_timeout() {
920 let dir = tmpdir();
921 let socket_path = dir.path().join("sock");
922
923 let listener = or_panic!(UnixListener::bind(&socket_path));
924
925 let mut stream = or_panic!(UnixStream::connect(&socket_path));
926 or_panic!(stream.set_read_timeout(Some(Duration::from_millis(1000))));
927
928 let mut other_end = or_panic!(listener.accept()).0;
929 or_panic!(other_end.write_all(b"hello world"));
930
931 let mut buf = [0; 11];
932 or_panic!(stream.read(&mut buf));
933 assert_eq!(b"hello world", &buf[..]);
934
935 let kind = stream.read(&mut buf).err().expect("expected error").kind();
936 assert!(kind == io::ErrorKind::WouldBlock || kind == io::ErrorKind::TimedOut);
937 }
938
939 #[test]
940 fn test_unix_datagram() {
941 let dir = tmpdir();
942 let path1 = dir.path().join("sock1");
943 let path2 = dir.path().join("sock2");
944
945 let sock1 = or_panic!(UnixDatagram::bind(&path1));
946 let sock2 = or_panic!(UnixDatagram::bind(&path2));
947
948 let msg = b"hello world";
949 or_panic!(sock1.send_to(msg, &path2));
950 let mut buf = [0; 11];
951 or_panic!(sock2.recv_from(&mut buf));
952 assert_eq!(msg, &buf[..]);
953 }
954
955 #[test]
956 fn test_unnamed_unix_datagram() {
957 let dir = tmpdir();
958 let path1 = dir.path().join("sock1");
959
960 let sock1 = or_panic!(UnixDatagram::bind(&path1));
961 let sock2 = or_panic!(UnixDatagram::unbound());
962
963 let msg = b"hello world";
964 or_panic!(sock2.send_to(msg, &path1));
965 let mut buf = [0; 11];
966 let (usize, addr) = or_panic!(sock1.recv_from(&mut buf));
967 assert_eq!(usize, 11);
968 assert!(addr.is_unnamed());
969 assert_eq!(msg, &buf[..]);
970 }
971
972 #[test]
973 fn test_connect_unix_datagram() {
974 let dir = tmpdir();
975 let path1 = dir.path().join("sock1");
976 let path2 = dir.path().join("sock2");
977
978 let bsock1 = or_panic!(UnixDatagram::bind(&path1));
979 let bsock2 = or_panic!(UnixDatagram::bind(&path2));
980 let sock = or_panic!(UnixDatagram::unbound());
981 or_panic!(sock.connect(&path1));
982
983 // Check send()
984 let msg = b"hello there";
985 or_panic!(sock.send(msg));
986 let mut buf = [0; 11];
987 let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
988 assert_eq!(usize, 11);
989 assert!(addr.is_unnamed());
990 assert_eq!(msg, &buf[..]);
991
992 // Changing default socket works too
993 or_panic!(sock.connect(&path2));
994 or_panic!(sock.send(msg));
995 or_panic!(bsock2.recv_from(&mut buf));
996 }
997
998 #[test]
999 fn test_unix_datagram_recv() {
1000 let dir = tmpdir();
1001 let path1 = dir.path().join("sock1");
1002
1003 let sock1 = or_panic!(UnixDatagram::bind(&path1));
1004 let sock2 = or_panic!(UnixDatagram::unbound());
1005 or_panic!(sock2.connect(&path1));
1006
1007 let msg = b"hello world";
1008 or_panic!(sock2.send(msg));
1009 let mut buf = [0; 11];
1010 let size = or_panic!(sock1.recv(&mut buf));
1011 assert_eq!(size, 11);
1012 assert_eq!(msg, &buf[..]);
1013 }
1014
1015 #[test]
1016 fn datagram_pair() {
1017 let msg1 = b"hello";
1018 let msg2 = b"world!";
1019
1020 let (s1, s2) = or_panic!(UnixDatagram::pair());
1021 let thread = thread::spawn(move || {
1022 // s1 must be moved in or the test will hang!
1023 let mut buf = [0; 5];
1024 or_panic!(s1.recv(&mut buf));
1025 assert_eq!(&msg1[..], &buf[..]);
1026 or_panic!(s1.send(msg2));
1027 });
1028
1029 or_panic!(s2.send(msg1));
1030 let mut buf = [0; 6];
1031 or_panic!(s2.recv(&mut buf));
1032 assert_eq!(&msg2[..], &buf[..]);
1033 drop(s2);
1034
1035 thread.join().unwrap();
1036 }
1037
1038 #[test]
1039 fn abstract_namespace_not_allowed() {
1040 assert!(UnixStream::connect("\0asdf").is_err());
1041 }
1042 }