• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use super::{socket_addr, SocketAddr};
2 use crate::sys::unix::net::new_socket;
3 
4 use std::io;
5 use std::os::unix::io::{AsRawFd, FromRawFd};
6 use std::os::unix::net;
7 use std::path::Path;
8 
connect(path: &Path) -> io::Result<net::UnixStream>9 pub(crate) fn connect(path: &Path) -> io::Result<net::UnixStream> {
10     let socket = new_socket(libc::AF_UNIX, libc::SOCK_STREAM)?;
11     let (sockaddr, socklen) = socket_addr(path)?;
12     let sockaddr = &sockaddr as *const libc::sockaddr_un as *const libc::sockaddr;
13 
14     match syscall!(connect(socket, sockaddr, socklen)) {
15         Ok(_) => {}
16         Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {}
17         Err(e) => {
18             // Close the socket if we hit an error, ignoring the error
19             // from closing since we can't pass back two errors.
20             let _ = unsafe { libc::close(socket) };
21 
22             return Err(e);
23         }
24     }
25 
26     Ok(unsafe { net::UnixStream::from_raw_fd(socket) })
27 }
28 
pair() -> io::Result<(net::UnixStream, net::UnixStream)>29 pub(crate) fn pair() -> io::Result<(net::UnixStream, net::UnixStream)> {
30     super::pair(libc::SOCK_STREAM)
31 }
32 
local_addr(socket: &net::UnixStream) -> io::Result<SocketAddr>33 pub(crate) fn local_addr(socket: &net::UnixStream) -> io::Result<SocketAddr> {
34     super::local_addr(socket.as_raw_fd())
35 }
36 
peer_addr(socket: &net::UnixStream) -> io::Result<SocketAddr>37 pub(crate) fn peer_addr(socket: &net::UnixStream) -> io::Result<SocketAddr> {
38     super::peer_addr(socket.as_raw_fd())
39 }
40