Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review: how to listen on both ipv4 and ipv6? #628

Closed
carneeki opened this issue Jun 28, 2020 · 5 comments
Closed

Review: how to listen on both ipv4 and ipv6? #628

carneeki opened this issue Jun 28, 2020 · 5 comments

Comments

@carneeki
Copy link

carneeki commented Jun 28, 2020

In #456, @nbari asked how it is possible to listen on both ipv4 and ipv6 at the same time, and the issue stalled.

I have a main.rs file that is something like this:

async fn main() {
    let config = Some(configuration); // getting config from a .toml file
    let routes = warp::get(); // snipping the routes for brevity

    let listen4 = SocketAddr::from_str(config.ipv4).expect('Invalid ipv4 config');
    let listen6 = SocketAddr::from_str(config.ipv6).expect('Invalid ipv6 config');

    let server4 = warp::serve(routes.clone()).run(listen4);
    let server6 = warp::serve(routes.clone()).run(listen6);

    futures::join!(server4, server6);
}

This seems to be working, but I'm not sure if this is a good way to do what was desired and would like some feedback on how it could be done.

Originally posted by @nbari in #456 (comment)

Thanks!

EDIT: I forgot to mention, I'm doing this in stable rather than nightly.

@jxs
Copy link
Collaborator

jxs commented Jul 7, 2020

you can also bind to both Ipv6 and Ipv4:

    let addr = IpAddr::from_str("::0").unwrap();
    warp::serve(endpoints).run((addr, 3030)).await;

@jxs jxs closed this as completed Jul 7, 2020
@nbari
Copy link

nbari commented Jul 7, 2020

hi @jxs using let addr = IpAddr::from_str("::0").unwrap(); I get:

no variant or associated item named `from_str` found for enum `std::net::IpAddr` in the current scope

@jxs
Copy link
Collaborator

jxs commented Jul 7, 2020

you need to import it:

use std::net::IpAddr;
use std::str::FromStr;

@cmcqueen
Copy link

@jxs wrote:

you can also bind to both Ipv6 and Ipv4:

   let addr = IpAddr::from_str("::0").unwrap();
   warp::serve(endpoints).run((addr, 3030)).await;

This depends on whether the OS you're running on has the IPV6_V6ONLY socket option turned on or off by default.

I'm doing the above, running on a Linux system that is configured for net.ipv6.bindv6only = 0, so it works.

But I have colleagues testing a Windows build, and are reporting that they can't connect to eg 127.0.0.1.

So I'm trying to work out the most reliable solution that works for both Linux, regardless of net.ipv6.bindv6only, and for Windows.

It is possible to use the socket2 crate to explicitly control the IPV6_V6ONLY option (something like this, although that should change to listening on just [::] to work), but I haven't yet figured out how to set up such a socket with warp yet.

@cmcqueen
Copy link

This has been asked on StackOverflow: How to listen on multiple ports with warp?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants