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

fix(quic): allow listening on ipv4 and ipv6 separately #4289

Merged
merged 11 commits into from
Aug 8, 2023
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions transports/quic/Cargo.toml
mxinden marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ rand = "0.8.5"
rustls = { version = "0.21.2", default-features = false }
thiserror = "1.0.44"
tokio = { version = "1.29.1", default-features = false, features = ["net", "rt", "time"], optional = true }
socket2 = { version = "0.5.3" }
mxinden marked this conversation as resolved.
Show resolved Hide resolved

[features]
tokio = ["dep:tokio", "if-watch/tokio", "quinn/runtime-tokio"]
Expand Down
22 changes: 20 additions & 2 deletions transports/quic/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ use libp2p_core::{
Transport,
};
use libp2p_identity::PeerId;
use socket2::{Domain, Socket, Type};
use std::collections::hash_map::{DefaultHasher, Entry};
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, UdpSocket};
use std::time::Duration;
use std::{fmt, io};
use std::{
net::SocketAddr,
pin::Pin,
Expand Down Expand Up @@ -172,6 +173,21 @@ impl<P: Provider> GenTransport<P> {
}
}
}

fn create_socket(&self, socket_addr: SocketAddr) -> io::Result<Socket> {
let domain = if socket_addr.is_ipv4() {
Domain::IPV4
} else {
Domain::IPV6
};
let socket = Socket::new(domain, Type::DGRAM, Some(socket2::Protocol::UDP))?;
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
mxinden marked this conversation as resolved.
Show resolved Hide resolved
if socket_addr.is_ipv6() {
socket.set_only_v6(true)?;
}

socket.bind(&socket_addr.into())?;
Ok(socket)
}
}

impl<P: Provider> Transport for GenTransport<P> {
Expand All @@ -188,7 +204,9 @@ impl<P: Provider> Transport for GenTransport<P> {
let (socket_addr, version, _peer_id) = self.remote_multiaddr_to_socketaddr(addr, false)?;
let endpoint_config = self.quinn_config.endpoint_config.clone();
let server_config = self.quinn_config.server_config.clone();
let socket = UdpSocket::bind(socket_addr).map_err(Self::Error::from)?;
let socket = self.create_socket(socket_addr).map_err(Self::Error::from)?;

let socket: UdpSocket = socket.into();
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
let socket_c = socket.try_clone().map_err(Self::Error::from)?;
let endpoint = Self::new_endpoint(endpoint_config, Some(server_config), socket)?;
let listener = Listener::new(
Expand Down