From 44dba87e908a91ca9df5db92190408cc62d54280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ant=C3=B4nio=20Cardoso?= Date: Wed, 10 Jan 2024 16:37:50 -0300 Subject: [PATCH] src: connection: Fix panic when DNS lookup fails --- src/connection/tcp.rs | 29 +++++++++++++++++++---------- src/connection/udp.rs | 42 +++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/connection/tcp.rs b/src/connection/tcp.rs index 491dff0129..7f4f7eaf8e 100644 --- a/src/connection/tcp.rs +++ b/src/connection/tcp.rs @@ -26,11 +26,16 @@ 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 addr = match address.to_socket_addrs()?.next() { + Some(addr) => addr, + None => { + return Err(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 +50,15 @@ 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 addr = match address.to_socket_addrs()?.next() { + Some(addr) => addr, + None => { + return Err(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..d8f88a314e 100644 --- a/src/connection/udp.rs +++ b/src/connection/udp.rs @@ -28,11 +28,15 @@ pub fn select_protocol( } pub fn udpbcast(address: T) -> io::Result { - let addr = address - .to_socket_addrs() - .unwrap() - .next() - .expect("Invalid address"); + let addr = match address.to_socket_addrs()?.next() { + Some(addr) => addr, + None => { + return Err(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 +45,29 @@ 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 addr = match address.to_socket_addrs()?.next() { + Some(addr) => addr, + None => { + return Err(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 addr = match address.to_socket_addrs()?.next() { + Some(addr) => addr, + None => { + return Err(io::Error::new( + io::ErrorKind::Other, + "Host address lookup failed", + )); + } + }; let socket = UdpSocket::bind(addr)?; UdpConnection::new(socket, true, None) }