Skip to content

Commit

Permalink
src: connection: Fix panic when DNS lookup fails
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Jan 10, 2024
1 parent d828e6e commit 343b6da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
17 changes: 7 additions & 10 deletions src/connection/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ pub fn select_protocol<M: Message>(
}

pub fn tcpout<T: ToSocketAddrs>(address: T) -> io::Result<TcpConnection> {
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");

Check failure on line 30 in src/connection/tcp.rs

View workflow job for this annotation

GitHub Actions / linting

mismatched types

error[E0308]: mismatched types --> src/connection/tcp.rs:30:16 | 30 | return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<TcpConnection, Error>`, found `Error` | = note: expected enum `std::result::Result<connection::tcp::TcpConnection, std::io::Error>` found struct `std::io::Error` help: try wrapping the expression in `Err` | 30 | 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)))?;

Expand All @@ -45,11 +44,9 @@ pub fn tcpout<T: ToSocketAddrs>(address: T) -> io::Result<TcpConnection> {
}

pub fn tcpin<T: ToSocketAddrs>(address: T) -> io::Result<TcpConnection> {
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");

Check failure on line 48 in src/connection/tcp.rs

View workflow job for this annotation

GitHub Actions / linting

mismatched types

error[E0308]: mismatched types --> src/connection/tcp.rs:48:16 | 48 | return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<TcpConnection, Error>`, found `Error` | = note: expected enum `std::result::Result<connection::tcp::TcpConnection, std::io::Error>` found struct `std::io::Error` help: try wrapping the expression in `Err` | 48 | 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
Expand Down
24 changes: 9 additions & 15 deletions src/connection/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ pub fn select_protocol<M: Message>(
}

pub fn udpbcast<T: ToSocketAddrs>(address: T) -> io::Result<UdpConnection> {
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");

Check failure on line 32 in src/connection/udp.rs

View workflow job for this annotation

GitHub Actions / linting

mismatched types

error[E0308]: mismatched types --> src/connection/udp.rs:32:16 | 32 | return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<UdpConnection, Error>`, found `Error` | = note: expected enum `std::result::Result<connection::udp::UdpConnection, std::io::Error>` found struct `std::io::Error` help: try wrapping the expression in `Err` | 32 | 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)
Expand All @@ -41,21 +39,17 @@ pub fn udpbcast<T: ToSocketAddrs>(address: T) -> io::Result<UdpConnection> {
}

pub fn udpout<T: ToSocketAddrs>(address: T) -> io::Result<UdpConnection> {
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");

Check failure on line 43 in src/connection/udp.rs

View workflow job for this annotation

GitHub Actions / linting

mismatched types

error[E0308]: mismatched types --> src/connection/udp.rs:43:16 | 43 | return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<UdpConnection, Error>`, found `Error` | = note: expected enum `std::result::Result<connection::udp::UdpConnection, std::io::Error>` found struct `std::io::Error` help: try wrapping the expression in `Err` | 43 | 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<T: ToSocketAddrs>(address: T) -> io::Result<UdpConnection> {
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");

Check failure on line 51 in src/connection/udp.rs

View workflow job for this annotation

GitHub Actions / linting

mismatched types

error[E0308]: mismatched types --> src/connection/udp.rs:51:16 | 51 | return io::Error::new(io::ErrorKind::Other, "Host address lookup failed"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<UdpConnection, Error>`, found `Error` | = note: expected enum `std::result::Result<connection::udp::UdpConnection, std::io::Error>` found struct `std::io::Error` help: try wrapping the expression in `Err` | 51 | return Err(io::Error::new(io::ErrorKind::Other, "Host address lookup failed")); | ++++ +
};
let socket = UdpSocket::bind(addr)?;
UdpConnection::new(socket, true, None)
}
Expand Down

0 comments on commit 343b6da

Please sign in to comment.