Skip to content

Commit a02f0b5

Browse files
authored
refactor(io,net): move cmsg to io ancillary (#730)
* refactor(io,net): move cmsg to io ancillary * fix(net): use package version
1 parent 8e40e7e commit a02f0b5

10 files changed

Lines changed: 73 additions & 28 deletions

File tree

compio-io/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,22 @@ compio-buf = { workspace = true, features = ["arrayvec", "bytes"] }
1414
futures-util = { workspace = true, features = ["sink"] }
1515
paste = { workspace = true }
1616
synchrony = { workspace = true, features = ["bilock"] }
17+
18+
cfg-if = { workspace = true, optional = true }
1719
thiserror = { workspace = true, optional = true }
1820
serde = { version = "1.0.219", optional = true }
1921
serde_json = { version = "1.0.140", optional = true }
2022

23+
[target.'cfg(unix)'.dependencies]
24+
libc = { workspace = true, optional = true }
25+
26+
[target.'cfg(windows)'.dependencies]
27+
windows-sys = { workspace = true, optional = true, features = [
28+
"Win32_Networking_WinSock",
29+
] }
30+
2131
[dev-dependencies]
32+
aligned-array = "1.0.1"
2233
tokio = { workspace = true, features = ["macros", "rt"] }
2334
serde = { version = "1.0.219", features = ["derive"] }
2435
futures-executor = "0.3.30"
@@ -27,6 +38,7 @@ futures-executor = "0.3.30"
2738
default = []
2839
compat = ["futures-util/io"]
2940
sync = []
41+
ancillary = ["dep:cfg-if", "dep:libc", "dep:windows-sys"]
3042

3143
# Codecs
3244
# Serde json codec
Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
//! Ancillary data (control message) support for connected streams.
2+
//!
3+
//! Ancillary messages are used to pass out-of-band information such as file
4+
//! descriptors (Unix domain sockets), credentials, or kTLS record types.
5+
//!
6+
//! # Types
7+
//!
8+
//! - [`AncillaryRef`]: A reference to a single ancillary data entry.
9+
//! - [`AncillaryIter`]: An iterator over a buffer of ancillary messages.
10+
//! - [`AncillaryBuilder`]: A builder for constructing ancillary messages into a
11+
//! caller-supplied send buffer.
12+
113
use std::{marker::PhantomData, mem::MaybeUninit};
214

315
cfg_if::cfg_if! {
@@ -10,10 +22,10 @@ cfg_if::cfg_if! {
1022
}
1123
}
1224

13-
/// Reference to a control message.
14-
pub struct CMsgRef<'a>(sys::CMsgRef<'a>);
25+
/// Reference to an ancillary (control) message.
26+
pub struct AncillaryRef<'a>(sys::CMsgRef<'a>);
1527

16-
impl CMsgRef<'_> {
28+
impl AncillaryRef<'_> {
1729
/// Returns the level of the control message.
1830
pub fn level(&self) -> i32 {
1931
self.0.level()
@@ -41,14 +53,14 @@ impl CMsgRef<'_> {
4153
}
4254
}
4355

44-
/// An iterator for control messages.
45-
pub struct CMsgIter<'a> {
56+
/// An iterator for ancillary (control) messages.
57+
pub struct AncillaryIter<'a> {
4658
inner: sys::CMsgIter,
4759
_p: PhantomData<&'a ()>,
4860
}
4961

50-
impl<'a> CMsgIter<'a> {
51-
/// Create [`CMsgIter`] with the given buffer.
62+
impl<'a> AncillaryIter<'a> {
63+
/// Create [`AncillaryIter`] with the given buffer.
5264
///
5365
/// # Panics
5466
///
@@ -66,28 +78,28 @@ impl<'a> CMsgIter<'a> {
6678
}
6779
}
6880

69-
impl<'a> Iterator for CMsgIter<'a> {
70-
type Item = CMsgRef<'a>;
81+
impl<'a> Iterator for AncillaryIter<'a> {
82+
type Item = AncillaryRef<'a>;
7183

7284
fn next(&mut self) -> Option<Self::Item> {
7385
unsafe {
7486
let cmsg = self.inner.current();
7587
self.inner.next();
76-
cmsg.map(CMsgRef)
88+
cmsg.map(AncillaryRef)
7789
}
7890
}
7991
}
8092

81-
/// Helper to construct control message.
82-
pub struct CMsgBuilder<'a> {
93+
/// Helper to construct ancillary (control) messages.
94+
pub struct AncillaryBuilder<'a> {
8395
inner: sys::CMsgIter,
8496
len: usize,
8597
_p: PhantomData<&'a mut ()>,
8698
}
8799

88-
impl<'a> CMsgBuilder<'a> {
89-
/// Create [`CMsgBuilder`] with the given buffer. The buffer will be zeroed
90-
/// on creation.
100+
impl<'a> AncillaryBuilder<'a> {
101+
/// Create [`AncillaryBuilder`] with the given buffer. The buffer will be
102+
/// zeroed on creation.
91103
///
92104
/// # Panics
93105
///

compio-io/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ use synchrony::unsync as sync;
128128

129129
type PinBoxFuture<T> = Pin<Box<dyn Future<Output = T>>>;
130130

131+
#[cfg(feature = "ancillary")]
132+
pub mod ancillary;
131133
mod buffer;
132134
pub mod framed;
133135

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use std::mem::MaybeUninit;
22

33
use aligned_array::{A8, Aligned};
44
use compio_buf::{IoBuf, IoBufMut};
5-
use compio_net::{CMsgBuilder, CMsgIter};
5+
use compio_io::ancillary::{AncillaryBuilder, AncillaryIter};
66

77
#[test]
88
fn test_cmsg() {
99
let mut buf: Aligned<A8, [u8; 64]> = Aligned([0u8; 64]);
10-
let mut builder = CMsgBuilder::new(buf.as_uninit());
10+
let mut builder = AncillaryBuilder::new(buf.as_uninit());
1111

1212
builder.try_push(0, 0, ()).unwrap(); // 16 / 12
1313
builder.try_push(1, 1, u32::MAX).unwrap(); // 16 + 4 + 4 / 12 + 4
@@ -17,7 +17,7 @@ fn test_cmsg() {
1717

1818
unsafe {
1919
let buf = buf.slice(..len);
20-
let mut iter = CMsgIter::new(&buf);
20+
let mut iter = AncillaryIter::new(&buf);
2121

2222
let cmsg = iter.next().unwrap();
2323
assert_eq!((cmsg.level(), cmsg.ty(), cmsg.data::<()>()), (0, 0, &()));
@@ -39,12 +39,12 @@ fn test_cmsg() {
3939
#[should_panic]
4040
fn invalid_buffer_length() {
4141
let mut buf = [MaybeUninit::new(0u8); 1];
42-
CMsgBuilder::new(&mut buf);
42+
AncillaryBuilder::new(&mut buf);
4343
}
4444

4545
#[test]
4646
#[should_panic]
4747
fn invalid_buffer_alignment() {
4848
let mut buf = [MaybeUninit::new(0u8); 64];
49-
CMsgBuilder::new(&mut buf[1..]);
49+
AncillaryBuilder::new(&mut buf[1..]);
5050
}

compio-net/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1717
# Workspace dependencies
1818
compio-buf = { workspace = true }
1919
compio-driver = { workspace = true }
20-
compio-io = { workspace = true }
20+
compio-io = { workspace = true, features = ["ancillary"] }
2121
compio-runtime = { workspace = true }
2222

2323
cfg-if = { workspace = true }
@@ -40,7 +40,6 @@ libc = { workspace = true }
4040
# Shared dev dependencies for all platforms
4141
[dev-dependencies]
4242
compio-macros = { workspace = true }
43-
aligned-array = "1.0.1"
4443
futures-util = { workspace = true }
4544
tempfile = { workspace = true }
4645

compio-net/src/lib.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
html_favicon_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg"
1414
)]
1515

16-
mod cmsg;
1716
mod opts;
1817
mod resolve;
1918
mod socket;
@@ -22,7 +21,27 @@ mod tcp;
2221
mod udp;
2322
mod unix;
2423

25-
pub use cmsg::*;
24+
/// Reference to a control message.
25+
#[deprecated(
26+
since = "0.12.0",
27+
note = "use `compio_io::ancillary::AncillaryRef` instead"
28+
)]
29+
pub type CMsgRef<'a> = compio_io::ancillary::AncillaryRef<'a>;
30+
31+
/// An iterator for control messages.
32+
#[deprecated(
33+
since = "0.12.0",
34+
note = "use `compio_io::ancillary::AncillaryIter` instead"
35+
)]
36+
pub type CMsgIter<'a> = compio_io::ancillary::AncillaryIter<'a>;
37+
38+
/// Helper to construct control message.
39+
#[deprecated(
40+
since = "0.12.0",
41+
note = "use `compio_io::ancillary::AncillaryBuilder` instead"
42+
)]
43+
pub type CMsgBuilder<'a> = compio_io::ancillary::AncillaryBuilder<'a>;
44+
2645
/// Providing functionalities to wait for readiness.
2746
#[deprecated(since = "0.12.0", note = "Use `compio::runtime::fd::PollFd` instead")]
2847
pub type PollFd<T> = compio_runtime::fd::PollFd<T>;

compio-quic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1515

1616
[dependencies]
1717
# Workspace dependencies
18-
compio-io = { workspace = true }
18+
compio-io = { workspace = true, features = ["ancillary"] }
1919
compio-buf = { workspace = true, features = ["bytes"] }
2020
compio-log = { workspace = true }
2121
compio-net = { workspace = true }

compio-quic/src/socket.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use std::{
1717
};
1818

1919
use compio_buf::{BufResult, IntoInner, IoBuf, IoBufMut, SetLen, buf_try};
20-
use compio_net::{CMsgBuilder, CMsgIter, UdpSocket};
20+
use compio_io::ancillary::{AncillaryBuilder, AncillaryIter};
21+
use compio_net::UdpSocket;
2122
use quinn_proto::{EcnCodepoint, Transmit};
2223
#[cfg(windows)]
2324
use windows_sys::Win32::Networking::WinSock;
@@ -344,7 +345,7 @@ impl Socket {
344345

345346
// SAFETY: `control` contains valid data
346347
unsafe {
347-
for cmsg in CMsgIter::new(&control) {
348+
for cmsg in AncillaryIter::new(&control) {
348349
#[cfg(windows)]
349350
const UDP_COALESCED_INFO: i32 = WinSock::UDP_COALESCED_INFO as i32;
350351

@@ -420,7 +421,7 @@ impl Socket {
420421
let ecn = transmit.ecn.map_or(0, |x| x as u8);
421422

422423
let mut control = Ancillary::<CMSG_LEN>::new();
423-
let mut builder = CMsgBuilder::new(control.as_uninit());
424+
let mut builder = AncillaryBuilder::new(control.as_uninit());
424425

425426
// ECN
426427
if is_ipv4 {

0 commit comments

Comments
 (0)