tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
26.4k stars 2.43k forks source link

Wrap function `only_v6` for UDP and TCP sockets. #6282

Open bamidev opened 8 months ago

bamidev commented 8 months ago

Is your feature request related to a problem? Please describe. Yes, with tokio sockets I can't bind two independent sockets both on 0.0.0.0 and :: using the same port, like this:

use std::net::*;

use tokio::net::UdpSocket;

#[tokio::main]
async fn main() {
    let socket4 = UdpSocket::bind(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 10000)).await.unwrap();
    let socket6 = UdpSocket::bind(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 10000, 0, 0)).await.unwrap();
}

I want to use two sockets, one for each IP version, because not all platforms support using one socket for both versions. On Linux, IPV6_V6ONLY is turned off by default, and it maps IPv4 packets to the v6 socket as well (and maybe also on others platform too, idk), and because of it, I can't bind socket6 in this example. I get an address already in use error, because I'm on Linux. Which makes sense, but I'd like to enable the IPV6_V6ONLY option when it's disabled by default.

Describe the solution you'd like I see mio is used internally by tokio. mio has a function for setting that option: https://docs.rs/mio/latest/mio/net/struct.UdpSocket.html#method.only_v6 So I'd like the TcpSocket and UdpSocket to wrap that function as well.

Describe alternatives you've considered I could obtain the file descriptor and use C functions to set it maybe, haven't looked into it. I'd rather add the only_v6 myself if I have some time again.

bamidev commented 8 months ago

Not sure though if mio exposes the only_v6 function for tcp, can't find it...

Darksonn commented 8 months ago

I expect that the socket2 crate can do this.

Is there an issue for adding this method to the standard library? I couldn't find one.