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 5975b44
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
23 changes: 13 additions & 10 deletions src/connection/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ 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 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 +47,12 @@ 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 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
33 changes: 18 additions & 15 deletions src/connection/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ 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 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 +42,23 @@ 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 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 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 5975b44

Please sign in to comment.