Skip to content

Commit

Permalink
Add support for abstract sockets
Browse files Browse the repository at this point in the history
In #146, I introduced a bug that prevented abstract sockets from
working. I passed the path straight into
rustix::net::SocketAddrUnix::new, which fails if it receives an abstract
socket.

This commit fixes this issue by explicitly checking for abstract
sockets. If it sees that the path it's receiving is abstract, it will
pass the path's bytes to new_abstract_socket() instead.

This should fix the issue that is occurring in dbus2/zbus#517

Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Dec 6, 2023
1 parent ccdb956 commit 5ae296d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1847,12 +1847,20 @@ impl Async<UnixStream> {
/// # std::io::Result::Ok(()) });
/// ```
pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<Async<UnixStream>> {
use std::os::unix::ffi::OsStrExt;

// 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.
let path = path.as_ref().as_os_str();
let address = match path.as_bytes().first() {
Some(0) => rn::SocketAddrUnix::new_abstract_name(path.as_bytes())?,
_ => rn::SocketAddrUnix::new(path)?,
};

// 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

0 comments on commit 5ae296d

Please sign in to comment.