From 343b6da4535100562f774f0c0840bf17795691f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ant=C3=B4nio=20Cardoso?= Date: Wed, 10 Jan 2024 16:12:23 -0300 Subject: [PATCH] src: connection: Fix panic when DNS lookup fails --- src/connection/tcp.rs | 17 +++++++---------- src/connection/udp.rs | 24 +++++++++--------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/connection/tcp.rs b/src/connection/tcp.rs index 491dff0129..aacdb6b7d8 100644 --- a/src/connection/tcp.rs +++ b/src/connection/tcp.rs @@ -26,11 +26,10 @@ pub fn select_protocol( } pub fn tcpout(address: T) -> io::Result { - let addr = address - .to_socket_addrs() - .unwrap() - .next() - .expect("Host address lookup failed."); + let Some(addr) = address.to_socket_addrs()?.next() else { + return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); + }; + let socket = TcpStream::connect(addr)?; socket.set_read_timeout(Some(Duration::from_millis(100)))?; @@ -45,11 +44,9 @@ pub fn tcpout(address: T) -> io::Result { } pub fn tcpin(address: T) -> io::Result { - let addr = address - .to_socket_addrs() - .unwrap() - .next() - .expect("Invalid address"); + let Some(addr) = address.to_socket_addrs()?.next() else { + return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); + }; let listener = TcpListener::bind(addr)?; //For now we only accept one incoming stream: this blocks until we get one diff --git a/src/connection/udp.rs b/src/connection/udp.rs index 7357ca99c3..4c69d16b66 100644 --- a/src/connection/udp.rs +++ b/src/connection/udp.rs @@ -28,11 +28,9 @@ pub fn select_protocol( } pub fn udpbcast(address: T) -> io::Result { - let addr = address - .to_socket_addrs() - .unwrap() - .next() - .expect("Invalid address"); + let Some(addr) = address.to_socket_addrs()?.next() else { + return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); + }; let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); socket .set_broadcast(true) @@ -41,21 +39,17 @@ pub fn udpbcast(address: T) -> io::Result { } pub fn udpout(address: T) -> io::Result { - let addr = address - .to_socket_addrs() - .unwrap() - .next() - .expect("Invalid address"); + let Some(addr) = address.to_socket_addrs()?.next() else { + return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); + }; let socket = UdpSocket::bind("0.0.0.0:0")?; UdpConnection::new(socket, false, Some(addr)) } pub fn udpin(address: T) -> io::Result { - let addr = address - .to_socket_addrs() - .unwrap() - .next() - .expect("Invalid address"); + let Some(addr) = address.to_socket_addrs()?.next() else { + return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); + }; let socket = UdpSocket::bind(addr)?; UdpConnection::new(socket, true, None) }