Skip to content

fix(driver,net)!: to/from/msg have optional address - #721

Merged
Berrysoft merged 3 commits into
compio-rs:masterfrom
fantix:socket-optional-address
Mar 3, 2026
Merged

Berrysoft merged 3 commits into
compio-rs:masterfrom
fantix:socket-optional-address

Conversation

@fantix

@fantix fantix commented Mar 2, 2026 •

Copy link
Copy Markdown
Collaborator

BREAKING CHANGE: the following OpCode APIs have changed in this proposal:

OpCode Old Return Type New Return Type
RecvFrom (usize, SockAddr) (usize, Option<SockAddr>)
RecvFromVectored (usize, SockAddr) (usize, Option<SockAddr>)
RecvMsg (usize, usize, SockAddr) (usize, usize, Option<SockAddr>)

as well as RecvFromManaged, and:

OpCode Old Constructor New Constructor
SendTo (FD, BUFFER, SockAddr, i32) UNCHANGED
SendToVectored (FD, BUFFER, SockAddr, i32) UNCHANGED
SendMsg (FD, BUFFER, CONTROL, SockAddr, i32) (FD, BUFFER, CONTROL, Option<SockAddr>, i32)

Internal Socket APIs are also affected accordingly.

For example, recv_msg() on a TCP socket was previously returning a socket2::SockAddr with len() == 0:

// compio-net/src/socket.rs

#[test]
fn test_socket() -> io::Result<()> {
    compio_runtime::Runtime::new().unwrap().block_on(async {
        let addr = "142.250.137.104:80".parse::<SocketAddr>().unwrap();
        let addr = SockAddr::from(addr);
        let socket = Socket::new(addr.domain(), Type::STREAM, None).await.unwrap();
        socket.connect_async(&addr).await.unwrap();
        socket.send(b"GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n", 0).await.0.unwrap();
        let buf = Vec::with_capacity(32);
        let res = socket.recv_from(buf, 0).await;
        let (len, addr) = res.0.unwrap();
        let html = String::from_utf8(res.1[..len].to_vec()).unwrap();
        println!("recv from {addr:?}: {html}");
    });
    Ok(())
}
recv from SockAddr { ss_len: 0, ss_family: 0, len: 0 }: HTTP/1.0 200 OK

This proposal elevates that to a semantic Option<SockAddr>.

On the sending end, this proposal simplifies this:

let null_addr = socket2::SockAddr::new(socket2::SockAddrStorage::zeroed(), 0);
socket.send_msg(buffer, control, null_addr, flags);

into just:

socket.send_msg(buffer, control, None, flags);

Refs #717

BREAKING CHANGE: Socket::recv_{from,msg*}(), send_msg* now has optional
address as well as corresponding driver OpCodes
@fantix
fantix force-pushed the socket-optional-address branch from 6a9ac8c to 6376477 Compare March 2, 2026 02:12
@George-Miao George-Miao changed the title fix(driver,net): to/from/msg have optional address fix(driver,net)!: to/from/msg have optional address Mar 2, 2026
@George-Miao George-Miao added package: net Related to compio-net package: driver Related to compio-driver breaking change labels Mar 2, 2026
@fantix
fantix marked this pull request as ready for review March 2, 2026 02:30
Berrysoft
Berrysoft previously approved these changes Mar 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates internal socket send/recv APIs to represent “no address” as Option<SockAddr> (instead of a zero-length SockAddr), aligning semantics across drivers and simplifying call sites (notably for TCP / connected sockets).

Changes:

  • Change Socket::{recv_from, recv_from_vectored, recv_msg, recv_msg_vectored} to return Option<SockAddr> for the peer address.
  • Change Socket::{send_msg, send_msg_vectored} to accept Option<&SockAddr> and propagate this through driver ops.
  • Update UdpSocket wrappers to unwrap the now-optional address (UDP expects an address on receive) and pass Some(addr) on send.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
compio-net/src/udp.rs Adapts UDP recv wrappers to unwrap Option<SockAddr> and updates send_msg calls to pass Some(&SockAddr)
compio-net/src/socket.rs Changes internal Socket recv APIs to return optional address and send_msg APIs to accept optional address
compio-driver/src/sys/unix_op.rs Updates Unix SendMsg op to store Option<SockAddr> and conditionally populate msghdr name fields
compio-driver/src/sys/iocp/op.rs Updates IOCP SendMsg op to store Option<SockAddr> and populate WSAMSG name fields conditionally
compio-driver/src/op.rs Maps recv results to Option<SockAddr> based on addr_size > 0

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread compio-driver/src/sys/unix_op.rs Resolved
Comment thread compio-driver/src/sys/iocp/op.rs Resolved
@fantix
fantix force-pushed the socket-optional-address branch from 7ab07db to 51123fe Compare March 2, 2026 03:55
@Berrysoft

Copy link
Copy Markdown
Member

Should SendTo & RecvFrom be symmetric? If SendTo requires addr to be something, why RecvFrom returns Option?

@fantix

fantix commented Mar 2, 2026

Copy link
Copy Markdown
Collaborator Author

Should SendTo & RecvFrom be symmetric? If SendTo requires addr to be something, why RecvFrom returns Option?

No? Because the RecvFrom API doesn't know enough about the underlying protocol:

If src_addr is not NULL, and the underlying protocol provides the source address, this source address is filled in.

I was thinking like, SendTo has pretty clear semantics where, the caller is explicitly wanting to specify an address (and probably only the address + buffer, or they would have used Send or SendMsg); while for RecvFrom, it is the caller's responsibility (like UDP) to say, I'm sure my protocol returns addresses (like the assertions added in this PR in udp.rs). So the Option<SockAddr> plays an enforced role of documenting something like, "hey! make sure you can get an address back!".

You could argue that, documentation is enough and one should only use RecvFrom on protocols that are guaranteed to return peer addresses. Given that socket2::SockAddr won't panic if the address is not filled (with addrlen set to 0), I'm also fine with just documentation.

@Berrysoft

Copy link
Copy Markdown
Member

Thanks @fantix , and I get your point. It's fair enough.

However, I still don't like the Option<SockAddr>... Now each side (None or len == 0) has one vote. @George-Miao what's your opinion?

@George-Miao

Copy link
Copy Markdown
Member

Thanks @fantix , and I get your point. It's fair enough.

However, I still don't like the Option<SockAddr>... Now each side (None or len == 0) has one vote. @George-Miao what's your opinion?

Yeah... I prefer explicit over implicit, so I vote for Option<SockAddr>.

@fantix

fantix commented Mar 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

Please also suggest on the API design of the AsyncRecvMsg/AsyncSendMsg trait impls for TCP/UDS - I'm thinking of AddrType = (), like:

https://github.com/compio-rs/compio/pull/717/changes#diff-d1e7b7b4aecab6fa906e7916d04894c42614043a3001d9abb58a9e8b1e675e58R388

But this somehow doesn't feel right to me.


UPDATE: ahh I think I should just rename #717 to AsyncReadAncillary and AsyncWriteAncillary without address at all.

@Berrysoft Berrysoft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Berrysoft
Berrysoft merged commit cd5c0d6 into compio-rs:master Mar 3, 2026
63 checks passed
@github-actions github-actions Bot mentioned this pull request Mar 3, 2026
@fantix
fantix deleted the socket-optional-address branch March 3, 2026 20:40
@Berrysoft Berrysoft added this to the v0.19 milestone Mar 11, 2026
This was referenced Apr 21, 2026
@github-actions github-actions Bot mentioned this pull request Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change package: driver Related to compio-driver package: net Related to compio-net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants