uds
[!IMPORTANT]
I am not original author. A GitHub and Crates are links to original crate. This crate is forked!
[!IMPORTANT]
Since an author is not responding on issues at his github page, I decided to fork the crate.
A unix domain sockets Rust library that supports abstract addresses, fd-passing, SOCK_SEQPACKET sockets SOCK_STREAM for the Windows and more.
A AF_UNIX SOCK_STREAM is implemented for Windows in windows_unixstream.rs as an experiment. WSA veriosn 2.2 is required.
When possible, features are implemented via extension traits for std::os::unix::net types (and optionally mio's uds types) instead of exposing new structs.
The only new socket structs this crate exposes are those for seqpacket sockets.
Ancillary credentials and timestamps are not yet supported.
Changelog
- The crate's
addr.rsis used as dependancy, so this crate can be used on Windows to utilize theUnixSocketAddr. - Added experimental support for unixstream in Windows i.e AF_UNIX SOCK_STREAM.
- Added missing implementation of
DerefforNonblockingUnixSeqpacketListener. - Fixed
address already in useproblems in docs.
- Added from OwnedFd and from RawFd and from Self to OwnedFd. Functions which performs conversion from OwnedFd to Self perform check of the socket type.
NonblockingUnixSeqpacketListenerandNonblockingUnixSeqpacketConnis now have inside an instance ofUnixSeqpacketConnandUnixSeqpacketListenerrespectivly which is set to non-blocking.- Reorganised some functions
- Fixed compilation errors on OpenBSD
- Fixed one test problem (related to what kernel returns)
- Fixed compilation errors on FreeBSD.
- ! FreeBSD 15 demonstrates strange behaviour with message trancation and empty messages which is not compatiable Linux and FreeBSD 14.
- send_ancillary() memory managment modified
- A crate was forked due to stale status.
- Moved to Rust 2024
- Added OwnedFd and BorrowedFd to comply with Rust specifications.
- Removed MIO and Tokio. Use AsyncFD and implement MIO for the type youself. (Maybe MIO will be readded later).
Examples
(only runs sucessfully on Linux)
extern crate uds_fork;
use ;
let addr = from_abstract
.expect;
let listener = bind_unix_addr
.expect;
let client = connect_unix_addr
.expect;
let = pair.expect;
let = pair.expect;
client.send_fds
.expect;
let = listener.accept_unix_addr
.expect;
let creds: ConnCredentials = server_side.initial_peer_credentials
.expect;
if creds.euid == 0 else
(only runs sucessfully on Windows)
extern crate uds_fork;
use ;
let path = "server3.sock";
rem_sock;
let wul = bind.unwrap;
let client = connect.unwrap;
let = wul.accept_unix_addr.unwrap;
println!;
assert_eq!;
let sa_fam = get_socket_family.unwrap;
let sa_type = get_socket_type.unwrap;
assert_eq!;
assert_eq!;
println!;
let data: = ;
let srv_n = client.send.unwrap;
assert_eq!;
let mut rcv_data = ;
let rcv_n = accp_client.recv.unwrap;
assert_eq!;
assert_eq!;
// accp_client -> client
let data: = ;
let srv_n = accp_client.send.unwrap;
assert_eq!;
let mut rcv_data = ;
let rcv_n = client.recv.unwrap;
assert_eq!;
assert_eq!;
rem_sock;
Portability
macOS doesn't support SOCK_SEQPACKET sockets, and abstract socket addresses is Linux-only, so if you don't want to bother with supporting non-portable features you are probably better off only using what std or mio provides. If you're writing a datagram server though, using std or mio means you can't respond to abstract adresses, forcing clients to use path addresses and deal with cleaning up the socket file after themselves.
Even when all operating systems you care about supports something, they might behave differently:
On Linux file descriptors are cloned when they are sent, but macOS and the BSDs first clones them when they are received. This means that if a FD is closed before the peer receives it you have a problem.
Also, some OSes might return the original file descriptor without cloning it if it's received within the same process as it was sent from. (DragonFly BSD, possibly macOS and maybe FreeBSD).
| Linux | macOS | FreeBSD | OpenBSD | DragonFly BSD | NetBSD | Illumos | |
|---|---|---|---|---|---|---|---|
| Seqpacket | Yes | N/A | Yes | Yes | Yes | Yes | N/A |
| fd-passing | Yes | Yes | Yes | Yes | Yes | Yes | No |
| abstract addresses | Yes | N/A | N/A | N/A | N/A | N/A | N/A |
| Tested? | Manually* | Manually* | Manually* | Manually* | Manually* | Manually* | Manually* |
*: Not tested since v0.2.6. (but (cross)checked on CI.)
Other OSes
- FreeBSD 15 (from version to version) behaves differently on msg truncation and sending empty messages.
- Android: I haven't tested on it, but I assume there are no differences from regular Linux.
- Windows 10: While it added some unix socket features, Windows support is not a priority. (PRs are welcome though).
- Solaris: Treated identically as Illumos. mio 0.8 doesn't support it.
Minimum Rust version
The minimum Rust version is 1.63.
Older versions might work, but might break in a minor release.
unsafe usage
This crate calls many C functions, which are all unsafe (even ones as simple as socket()).
The public interface complies with Rust's FD managment recomendations.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.