INADDR_ANY for Rust UDP sockets

INADDR_ANY doesn't seem to be defined anywhere in Rust land. In C, to bind a socket in a client in preparation for sending, you'd bind to INADDR_ANY, let it bind to some or all available interfaces, and go from there. INADDR_ANY is just the zero IP address.

So I tried

let mut socket = UdpSocket("0")?;

That yields an "Invalid socket address" error. What's the "Any" address in Rust?

let mut socket = UdpSocket::bind("0.0.0.0:0")?;

Fair enough. There really should be a named constant for that, though.

3 Likes

Yeah, I agree. At least an example in the docs until then.

1 Like

Right. The docs all show talking to 127.0.0.1, which is "localhost" and only useful for talking to yourself.

It does have a name: std::net::Ipv4Addr - Rust

4 Likes

Ah, that's where it is. Thanks. That should be used in the UDP documentation examples.

I've written a PR to make this easier to find in the future: Add documentation to help people find `IpAddr::UNSPECIFIED` by joshtriplett · Pull Request #84257 · rust-lang/rust · GitHub

6 Likes

The feedback loop between the Rust users and developers is quite amazing.

3 Likes