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) }