rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.79k stars 12.5k forks source link

`UdpSocket` lacks methods to operate on wildcard sockets #128704

Open Zoxc opened 1 month ago

Zoxc commented 1 month ago

UdpSocket allows binding to wildcard IPs like IpAddr::V6(Ipv6Addr::UNSPECIFIED) which means it can accept packets targeted to multiple IPs. However the send method does not allow specifying the source IP which means any reply packets may be send from the wrong interface. The recv method also doesn't allow getting the target IP which would need to be passed to send.

This is a bit of a trap which isn't warned against in the documentation either.

Similar issue for tokio which mirrors the APIs: https://github.com/tokio-rs/tokio/issues/6737

the8472 commented 1 month ago

Well that's a fairly advanced use that needs cmsg kung-fu to set the source address for each packet. I think only nix supports this right now: https://docs.rs/nix/0.29.0/nix/sys/socket/enum.ControlMessage.html

Zoxc commented 1 month ago

You might have a point if std didn't support wildcard UDP sockets, however documentation for UdpSocket::send explicitly mentions them. Yet basic functionally like replying to packets is not possible. If you're not paying attention, you might assume (like me) that send and recv work for these. tokio even has an incorrect UDP echo server example in their UdpSocket documentation.

Windows does have APIs for these operations. It's pretty much required for any operation system which support wildcard bindings.