Skip to content

Commit 93c5fb4

Browse files
committed
More cleanup.
1 parent 5ebc7ed commit 93c5fb4

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

transports/tcp/src/lib.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ use libp2p_core::{
4646
use socket2::{Domain, Socket, Type};
4747
use std::{
4848
io,
49-
iter::{self, FromIterator},
50-
net::{IpAddr, SocketAddr, TcpListener, TcpStream},
49+
net::{SocketAddr, TcpListener, TcpStream},
5150
pin::Pin,
5251
task::{Context, Poll},
5352
time::Duration,
@@ -192,10 +191,7 @@ impl Transport for TcpConfig {
192191
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
193192
let socket_addr = if let Ok(socket_addr) = multiaddr_to_socketaddr(&addr) {
194193
if socket_addr.port() == 0 || socket_addr.ip().is_unspecified() {
195-
log::debug!("Instantly refusing dialing {}, as it is invalid", addr);
196-
return Err(TransportError::Other(
197-
io::ErrorKind::ConnectionRefused.into(),
198-
));
194+
return Err(TransportError::MultiaddrNotSupported(addr));
199195
}
200196
socket_addr
201197
} else {
@@ -231,7 +227,6 @@ pub struct TcpListenStream {
231227
impl Stream for TcpListenStream {
232228
type Item = Result<TcpListenerEvent, io::Error>;
233229

234-
/// Takes ownership of the listener, and returns the next incoming event and the listener.
235230
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
236231
let me = Pin::into_inner(self);
237232
loop {
@@ -312,12 +307,9 @@ fn multiaddr_to_socketaddr(addr: &Multiaddr) -> Result<SocketAddr, ()> {
312307

313308
// Create a [`Multiaddr`] from the given IP address and port number.
314309
fn socketaddr_to_multiaddr(addr: &SocketAddr) -> Multiaddr {
315-
let proto = match addr.ip() {
316-
IpAddr::V4(ip) => Protocol::Ip4(ip),
317-
IpAddr::V6(ip) => Protocol::Ip6(ip),
318-
};
319-
let it = iter::once(proto).chain(iter::once(Protocol::Tcp(addr.port())));
320-
Multiaddr::from_iter(it)
310+
Multiaddr::empty()
311+
.with(addr.ip().into())
312+
.with(Protocol::Tcp(addr.port()))
321313
}
322314

323315
#[cfg(test)]
@@ -326,7 +318,7 @@ mod tests {
326318

327319
#[test]
328320
fn multiaddr_to_tcp_conversion() {
329-
use std::net::{Ipv4Addr, Ipv6Addr};
321+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
330322

331323
assert!(
332324
multiaddr_to_socketaddr(&"/ip4/127.0.0.1/udp/1234".parse::<Multiaddr>().unwrap())

0 commit comments

Comments
 (0)