The unix_socket crate provides bindings for Unix domain sockets.
The UnixStream
and UnixListener
types provide an API for SOCK_STREAM
type sockets that is basically equivalent to TcpStream
and TcpListener
. The UnixDatagram
type provides an API for SOCK_DGRAM
type sockets that is basically equivalent to UdpSocket
. UnixDatagram
actually goes a bit further than UdpSocket
by providing support for connected sockets, but I believe that is supported on UdpSocket
by the net2
crate.
I’d like the crate to eventually end up in libstd, where it will be somewhat unique in being the only large piece of platform specific functionality, as Unix sockets do not exist on Windows. It would end up in std::sys::unix
, where I believe there has always been an intent to build out with larger features such as Unix socket support. The rough equivalent in Windows, named pipes would presumably end with platform specific support in libstd eventually as well.
The API is in a solid place, but there a few open questions:
- Linux offers an “abstract namespace” as a nonportable extension. Where Unix sockets normally correspond to files in the filesystem, the abstract namespace contains arbitrary identifiers that have no relation to the filesystem.
- At the C level, and abstract namespace is identified by a leading null byte, and this method is used in the crate as well. The various constructors take an
AsRef<Path>
argument which will be passed verbatim to the OS, so an abstract namespace socket can be created via e.g.UnixStream::connect("\0some name")
. This is a bit weird, and may be a thing, like 0 duration socket timeouts, that the Rust wrapper should handle translation for. - Even though the abstract namespace is a Linux-only extension, the
AddressKind
enum contains anAbstract
variant on all platforms even if such an address can never exist.
- At the C level, and abstract namespace is identified by a leading null byte, and this method is used in the crate as well. The various constructors take an
- The
SOCK_SEQPACKET
socket type is a third variant that exists but is not currently supported. - The
sendmsg
andrecvmsg
functions can be used to perform more advanced tasks with Unix sockets, such as sending and receiving file descriptors, user credentials, or reading and writing from and to a series of buffers at once. An open PR adds support for these features, but it’s very unclear what the API should look like.