Async version of sendmsg() in rust

Hi Everyone,

I am a beginner with rust programming language. I am currently trying to implement a asynchronous raw socket programming in rust. For this purpose, I am looking for an async version of sendmsg() Unix system call in order to send ancillary control information (so_txtime) along the the raw socket buffer data. Kindly, let me know if there is any available crates in rust implementing asynchronous version of sendmsg.

Thank you and Best Regards.

If tokio::net::UnixDatagram doesn't provide a method for the kind of message you want to send, then you can use the UnixDatagram::try_io method to perform the call manually. It would look something like this:

socket.writable().await;
socket.try_io(Interest::WRITABLE, || {
    let fd = socket.as_raw_fd();
    Ok(nix::sys::socket::sendmsg(fd, ...)?)
});

This example uses the nix crate to perform the sendmsg call, but you can also use the libc crate directly.

See the documentation for writable for more information on how to use it in an asynchronous setting.

1 Like