transports/tcp: Bind to port, not local IP, when dialing (#2382)

When using PortReuse::Enabled, bind to INADDR_ANY to avoid picking the wrong IP.

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Roland Kuhn
2022-01-01 18:45:46 +01:00
committed by GitHub
parent 3749dd56f1
commit a29c35f8e7
2 changed files with 9 additions and 2 deletions

View File

@@ -4,6 +4,9 @@
- Migrate to Rust edition 2021 (see [PR 2339]).
- When using PortReuse::Enabled, bind to INADDR_ANY to avoid picking the wrong IP (see [PR 2382]).
[PR 2382]: https://github.com/libp2p/rust-libp2p/pull/2382
[PR 2339]: https://github.com/libp2p/rust-libp2p/pull/2339
# 0.30.0 [2021-11-01]

View File

@@ -57,7 +57,7 @@ use socket2::{Domain, Socket, Type};
use std::{
collections::HashSet,
io,
net::{IpAddr, SocketAddr, TcpListener},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener},
pin::Pin,
sync::{Arc, RwLock},
task::{Context, Poll},
@@ -151,7 +151,11 @@ impl PortReuse {
if ip.is_ipv4() == remote_ip.is_ipv4()
&& ip.is_loopback() == remote_ip.is_loopback()
{
return Some(SocketAddr::new(*ip, *port));
if remote_ip.is_ipv4() {
return Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), *port));
} else {
return Some(SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), *port));
}
}
}
}