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 | |
9 | pub(crate) fn connect(path: &Path) -> io::Result<net::UnixStream> { |
10 | let (sockaddr: sockaddr_un, socklen: u32) = socket_addr(path)?; |
11 | let sockaddr: *const sockaddr = &sockaddr as *const libc::sockaddr_un as *const libc::sockaddr; |
12 | |
13 | let fd: i32 = new_socket(domain:libc::AF_UNIX, socket_type:libc::SOCK_STREAM)?; |
14 | let socket: UnixStream = unsafe { net::UnixStream::from_raw_fd(fd) }; |
15 | match syscall!(connect(fd, sockaddr, socklen)) { |
16 | Ok(_) => {} |
17 | Err(ref err: &Error) if err.raw_os_error() == Some(libc::EINPROGRESS) => {} |
18 | Err(e: Error) => return Err(e), |
19 | } |
20 | |
21 | Ok(socket) |
22 | } |
23 | |
24 | pub(crate) fn pair() -> io::Result<(net::UnixStream, net::UnixStream)> { |
25 | super::pair(flags:libc::SOCK_STREAM) |
26 | } |
27 | |
28 | pub(crate) fn local_addr(socket: &net::UnixStream) -> io::Result<SocketAddr> { |
29 | super::local_addr(socket:socket.as_raw_fd()) |
30 | } |
31 | |
32 | pub(crate) fn peer_addr(socket: &net::UnixStream) -> io::Result<SocketAddr> { |
33 | super::peer_addr(socket:socket.as_raw_fd()) |
34 | } |
35 | |