Skip to content

Commit 5ae296d

Browse files
committed
Add support for abstract sockets
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]>
1 parent ccdb956 commit 5ae296d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,12 +1847,20 @@ impl Async<UnixStream> {
18471847
/// # std::io::Result::Ok(()) });
18481848
/// ```
18491849
pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<Async<UnixStream>> {
1850+
use std::os::unix::ffi::OsStrExt;
1851+
1852+
// SocketAddrUnix::new() will throw EINVAL when a path with a zero in it is passed in.
1853+
// However, some users expect to be able to pass in paths to abstract sockets, which
1854+
// triggers this error as it has a zero in it. Therefore, if a path starts with a zero,
1855+
// make it an abstract socket.
1856+
let path = path.as_ref().as_os_str();
1857+
let address = match path.as_bytes().first() {
1858+
Some(0) => rn::SocketAddrUnix::new_abstract_name(path.as_bytes())?,
1859+
_ => rn::SocketAddrUnix::new(path)?,
1860+
};
1861+
18501862
// Begin async connect.
1851-
let socket = connect(
1852-
rn::SocketAddrUnix::new(path.as_ref())?.into(),
1853-
rn::AddressFamily::UNIX,
1854-
None,
1855-
)?;
1863+
let socket = connect(address.into(), rn::AddressFamily::UNIX, None)?;
18561864
// Use new_nonblocking because connect already sets socket to non-blocking mode.
18571865
let stream = Async::new_nonblocking(UnixStream::from(socket))?;
18581866

0 commit comments

Comments
 (0)