Skip to content

Add support for abstract sockets #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1847,12 +1847,27 @@ impl Async<UnixStream> {
/// # std::io::Result::Ok(()) });
/// ```
pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<Async<UnixStream>> {
// SocketAddrUnix::new() will throw EINVAL when a path with a zero in it is passed in.
// However, some users expect to be able to pass in paths to abstract sockets, which
// triggers this error as it has a zero in it. Therefore, if a path starts with a zero,
// make it an abstract socket.
#[cfg(any(target_os = "linux", target_os = "android"))]
let address = {
use std::os::unix::ffi::OsStrExt;

let path = path.as_ref().as_os_str();
match path.as_bytes().first() {
Some(0) => rn::SocketAddrUnix::new_abstract_name(path.as_bytes())?,
_ => rn::SocketAddrUnix::new(path)?,
}
};

// Only Linux and Android support abstract sockets.
#[cfg(not(any(target_os = "linux", target_os = "android")))]
let address = rn::SocketAddrUnix::new(path.as_ref())?;

// Begin async connect.
let socket = connect(
rn::SocketAddrUnix::new(path.as_ref())?.into(),
rn::AddressFamily::UNIX,
None,
)?;
let socket = connect(address.into(), rn::AddressFamily::UNIX, None)?;
// Use new_nonblocking because connect already sets socket to non-blocking mode.
let stream = Async::new_nonblocking(UnixStream::from(socket))?;

Expand Down