1 | use super::{socket_addr, SocketAddr}; |
2 | use crate::sys::unix::net::new_socket; |
3 | |
4 | use std::io; |
5 | use std::os::unix::ffi::OsStrExt; |
6 | use std::os::unix::io::{AsRawFd, FromRawFd}; |
7 | use std::os::unix::net; |
8 | use std::path::Path; |
9 | |
10 | pub(crate) fn connect(path: &Path) -> io::Result<net::UnixStream> { |
11 | let socket_address: SocketAddr = { |
12 | let (sockaddr: sockaddr_un, socklen: u32) = socket_addr(path.as_os_str().as_bytes())?; |
13 | SocketAddr::from_parts(sockaddr, socklen) |
14 | }; |
15 | |
16 | connect_addr(&socket_address) |
17 | } |
18 | |
19 | pub(crate) fn connect_addr(address: &SocketAddr) -> io::Result<net::UnixStream> { |
20 | let fd: i32 = new_socket(domain:libc::AF_UNIX, socket_type:libc::SOCK_STREAM)?; |
21 | let socket: UnixStream = unsafe { net::UnixStream::from_raw_fd(fd) }; |
22 | let sockaddr: *const sockaddr = address.raw_sockaddr() as *const libc::sockaddr_un as *const libc::sockaddr; |
23 | |
24 | match syscall!(connect(fd, sockaddr, *address.raw_socklen())) { |
25 | Ok(_) => {} |
26 | Err(ref err: &Error) if err.raw_os_error() == Some(libc::EINPROGRESS) => {} |
27 | Err(e: Error) => return Err(e), |
28 | } |
29 | |
30 | Ok(socket) |
31 | } |
32 | |
33 | pub(crate) fn pair() -> io::Result<(net::UnixStream, net::UnixStream)> { |
34 | super::pair(flags:libc::SOCK_STREAM) |
35 | } |
36 | |
37 | pub(crate) fn local_addr(socket: &net::UnixStream) -> io::Result<SocketAddr> { |
38 | super::local_addr(socket:socket.as_raw_fd()) |
39 | } |
40 | |
41 | pub(crate) fn peer_addr(socket: &net::UnixStream) -> io::Result<SocketAddr> { |
42 | super::peer_addr(socket:socket.as_raw_fd()) |
43 | } |
44 | |