Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compio-driver/src/sys/iocp/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ impl<T: IoVectoredBuf, C: IoBuf, S> SendMsg<T, C, S> {
/// This function will panic if the control message buffer is misaligned.
pub fn new(fd: S, buffer: T, control: C, addr: Option<SockAddr>, flags: i32) -> Self {
assert!(
control.buf_ptr().cast::<CMSGHDR>().is_aligned(),
control.buf_len() == 0 || control.buf_ptr().cast::<CMSGHDR>().is_aligned(),
"misaligned control message buffer"
);
Self {
Expand Down Expand Up @@ -1151,7 +1151,11 @@ unsafe impl<T: IoVectoredBuf, C: IoBuf, S: AsFd> OpCode for SendMsg<T, C, S> {
let this = self.project();

*this.slices = this.buffer.as_ref().sys_slices();
let control = this.control.as_ref().sys_slice();
let control = if this.control.buf_len() == 0 {
SysSlice::null()
} else {
this.control.as_ref().sys_slice()
};
*this.msg = match this.addr.as_ref() {
Some(addr) => WSAMSG {
name: addr.as_ptr() as _,
Expand Down
8 changes: 6 additions & 2 deletions compio-driver/src/sys/unix_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ impl<T: IoVectoredBuf, C: IoBuf, S> SendMsg<T, C, S> {
/// This function will panic if the control message buffer is misaligned.
pub fn new(fd: S, buffer: T, control: C, addr: Option<SockAddr>, flags: i32) -> Self {
assert!(
control.buf_ptr().cast::<libc::cmsghdr>().is_aligned(),
control.buf_len() == 0 || control.buf_ptr().cast::<libc::cmsghdr>().is_aligned(),
"misaligned control message buffer"
);
Self {
Expand Down Expand Up @@ -845,7 +845,11 @@ impl<T: IoVectoredBuf, C: IoBuf, S> SendMsg<T, C, S> {
}
this.msg.msg_iov = this.slices.as_ptr() as _;
this.msg.msg_iovlen = this.slices.len() as _;
this.msg.msg_control = this.control.buf_ptr() as _;
this.msg.msg_control = if this.control.buf_len() == 0 {
std::ptr::null_mut()
} else {
this.control.buf_ptr() as _
};
this.msg.msg_controllen = this.control.buf_len() as _;
}
}
Expand Down
2 changes: 2 additions & 0 deletions compio-io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ windows-sys = { workspace = true, optional = true, features = [
] }

[dev-dependencies]
compio-net = { workspace = true }
compio-runtime = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt"] }
serde = { version = "1.0.219", features = ["derive"] }
futures-executor = "0.3.30"
Expand Down
130 changes: 130 additions & 0 deletions compio-io/src/ancillary/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#[cfg(feature = "allocator_api")]
use std::alloc::Allocator;

use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut, t_alloc};

/// Trait for asynchronous read with ancillary (control) data.
/// Intended for connected stream sockets (TCP, Unix streams) where no source
/// address is needed.
pub trait AsyncReadAncillary {
/// Read data with ancillary data into an owned buffer.
async fn read_with_ancillary<T: IoBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)>;

/// Read data with ancillary data into a vectored buffer.
async fn read_vectored_with_ancillary<T: IoVectoredBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)>;
}

impl<A: AsyncReadAncillary + ?Sized> AsyncReadAncillary for &mut A {
#[inline]
async fn read_with_ancillary<T: IoBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)> {
(**self).read_with_ancillary(buffer, control).await
}

#[inline]
async fn read_vectored_with_ancillary<T: IoVectoredBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)> {
(**self).read_vectored_with_ancillary(buffer, control).await
}
}

impl<A: AsyncReadAncillary + ?Sized, #[cfg(feature = "allocator_api")] Alloc: Allocator>
AsyncReadAncillary for t_alloc!(Box, A, Alloc)
{
#[inline]
async fn read_with_ancillary<T: IoBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)> {
(**self).read_with_ancillary(buffer, control).await
}

#[inline]
async fn read_vectored_with_ancillary<T: IoVectoredBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)> {
(**self).read_vectored_with_ancillary(buffer, control).await
}
}

/// Trait for asynchronous write with ancillary (control) data.
/// Intended for connected stream sockets (TCP, Unix streams) where no
/// destination address is needed.
pub trait AsyncWriteAncillary {
/// Write data with ancillary data from an owned buffer.
async fn write_with_ancillary<T: IoBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)>;

/// Write data with ancillary data from a vectored buffer.
async fn write_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)>;
}

impl<A: AsyncWriteAncillary + ?Sized> AsyncWriteAncillary for &mut A {
#[inline]
async fn write_with_ancillary<T: IoBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)> {
(**self).write_with_ancillary(buffer, control).await
}

#[inline]
async fn write_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)> {
(**self)
.write_vectored_with_ancillary(buffer, control)
.await
}
}

impl<A: AsyncWriteAncillary + ?Sized, #[cfg(feature = "allocator_api")] Alloc: Allocator>
AsyncWriteAncillary for t_alloc!(Box, A, Alloc)
{
#[inline]
async fn write_with_ancillary<T: IoBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)> {
(**self).write_with_ancillary(buffer, control).await
}

#[inline]
async fn write_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)> {
(**self)
.write_vectored_with_ancillary(buffer, control)
.await
}
}
74 changes: 51 additions & 23 deletions compio-io/src/ancillary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
//! ancillary data payloads.
//! - [`CodecError`]: Error type for encoding/decoding operations.
//!
//! # Traits
//!
//! - [`AsyncReadAncillary`]: read data together with ancillary data
//! - [`AsyncWriteAncillary`]: write data together with ancillary data
//!
//! # Functions
//!
//! - [`ancillary_space`]: Helper function to calculate ancillary message size
Expand All @@ -27,33 +32,52 @@
//!
//! # Example
//!
//! Send and receive a file descriptor over a Unix socket pair using
//! `SCM_RIGHTS`:
//!
//! ```
//! use compio_io::ancillary::{AncillaryBuf, AncillaryIter, CodecError, ancillary_space};
//! # #[cfg(unix)] {
//! use std::os::unix::io::RawFd;
//!
//! use compio_io::ancillary::*;
//! use compio_net::UnixStream;
//!
//! const BUF_SIZE: usize = ancillary_space::<RawFd>();
//!
//! const LEVEL: i32 = 1;
//! const TYPE: i32 = 2;
//! # compio_runtime::Runtime::new().unwrap().block_on(async {
//! // Create a socket pair.
//! let (std_a, std_b) = std::os::unix::net::UnixStream::pair().unwrap();
//! let mut a = UnixStream::from_std(std_a).unwrap();
//! let mut b = UnixStream::from_std(std_b).unwrap();
//!
//! // Build a buffer containing two `u32` ancillary messages.
//! let mut buf = AncillaryBuf::<{ ancillary_space::<u32>() * 2 }>::new();
//! let mut builder = buf.builder();
//! builder.push(LEVEL, TYPE, &42u32).unwrap();
//! builder.push(LEVEL, TYPE, &43u32).unwrap();
//! // Buffer is full, cannot add more messages.
//! assert!(matches!(
//! builder.push(LEVEL, TYPE, &44u32),
//! Err(CodecError::BufferTooSmall)
//! ));
//! // Pass fd 0 (stdin) as ancillary data via SCM_RIGHTS.
//! let mut ctrl_send = AncillaryBuf::<BUF_SIZE>::new();
//! let mut builder = ctrl_send.builder();
//! builder
//! .push(libc::SOL_SOCKET, libc::SCM_RIGHTS, &(0 as RawFd))
//! .unwrap();
//!
//! // Read back the messages.
//! unsafe {
//! let mut iter = AncillaryIter::new(&buf);
//! let msg = iter.next().unwrap();
//! assert_eq!(msg.level(), LEVEL);
//! assert_eq!(msg.ty(), TYPE);
//! assert_eq!(msg.data::<u32>().unwrap(), 42u32);
//! assert_eq!(iter.next().unwrap().data::<u32>().unwrap(), 43u32);
//! assert!(iter.next().is_none());
//! }
//! // Send the payload together with the ancillary data.
//! a.write_with_ancillary(b"hello", ctrl_send).await.0.unwrap();
//!
//! // Receive on the other end.
//! let payload = Vec::with_capacity(5);
//! let ctrl_recv = AncillaryBuf::<BUF_SIZE>::new();
//! let ((_, ctrl_len), (payload, ctrl_recv)) =
//! b.read_with_ancillary(payload, ctrl_recv).await.unwrap();
//!
//! assert_eq!(&payload[..], b"hello");
//!
//! // Parse the received ancillary messages.
//! let mut iter = unsafe { AncillaryIter::new(&ctrl_recv[..ctrl_len]) };
//! let msg = iter.next().unwrap();
//! assert_eq!(msg.level(), libc::SOL_SOCKET);
//! assert_eq!(msg.ty(), libc::SCM_RIGHTS);
//! // The kernel duplicates the fd, so the received value may differ.
//! let _received_fd = unsafe { msg.data::<RawFd>() };
//! assert!(iter.next().is_none());
//! # });
//! # }
//! ```

use std::{
Expand All @@ -67,6 +91,10 @@ use compio_buf::{IoBuf, IoBufMut, SetLen};
#[cfg(windows)]
use windows_sys::Win32::Networking::WinSock;

mod io;

pub use self::io::*;

cfg_if::cfg_if! {
if #[cfg(windows)] {
#[path = "windows.rs"]
Expand Down
94 changes: 93 additions & 1 deletion compio-net/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use std::{

use compio_buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
use compio_driver::impl_raw_fd;
use compio_io::{AsyncRead, AsyncReadManaged, AsyncWrite, util::Splittable};
use compio_io::{
AsyncRead, AsyncReadManaged, AsyncWrite,
ancillary::{AsyncReadAncillary, AsyncWriteAncillary},
util::Splittable,
};
use compio_runtime::{BorrowedBuffer, BufferPool, fd::PollFd};
use futures_util::{Stream, StreamExt, stream::FusedStream};
use socket2::{Protocol, SockAddr, Socket as Socket2, Type};
Expand Down Expand Up @@ -451,6 +455,52 @@ impl AsyncReadManaged for &TcpStream {
}
}

impl AsyncReadAncillary for TcpStream {
#[inline]
async fn read_with_ancillary<T: IoBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)> {
(&*self).read_with_ancillary(buffer, control).await
}

#[inline]
async fn read_vectored_with_ancillary<T: IoVectoredBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)> {
(&*self).read_vectored_with_ancillary(buffer, control).await
}
}

impl AsyncReadAncillary for &TcpStream {
#[inline]
async fn read_with_ancillary<T: IoBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)> {
self.inner
.recv_msg(buffer, control, 0)
.await
.map_res(|(res, len, _addr)| (res, len))
}

#[inline]
async fn read_vectored_with_ancillary<T: IoVectoredBufMut, C: IoBufMut>(
&mut self,
buffer: T,
control: C,
) -> BufResult<(usize, usize), (T, C)> {
self.inner
.recv_msg_vectored(buffer, control, 0)
.await
.map_res(|(res, len, _addr)| (res, len))
}
}

impl AsyncWrite for TcpStream {
#[inline]
async fn write<T: IoBuf>(&mut self, buf: T) -> BufResult<usize, T> {
Expand Down Expand Up @@ -495,6 +545,48 @@ impl AsyncWrite for &TcpStream {
}
}

impl AsyncWriteAncillary for TcpStream {
#[inline]
async fn write_with_ancillary<T: IoBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)> {
(&*self).write_with_ancillary(buffer, control).await
}

#[inline]
async fn write_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)> {
(&*self)
.write_vectored_with_ancillary(buffer, control)
.await
}
}

impl AsyncWriteAncillary for &TcpStream {
#[inline]
async fn write_with_ancillary<T: IoBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)> {
self.inner.send_msg(buffer, control, None, 0).await
}

#[inline]
async fn write_vectored_with_ancillary<T: IoVectoredBuf, C: IoBuf>(
&mut self,
buffer: T,
control: C,
) -> BufResult<usize, (T, C)> {
self.inner.send_msg_vectored(buffer, control, None, 0).await
}
}

impl Splittable for TcpStream {
type ReadHalf = OwnedReadHalf<Self>;
type WriteHalf = OwnedWriteHalf<Self>;
Expand Down
Loading
Loading