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 44dba87
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
29 changes: 19 additions & 10 deletions src/connection/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ 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 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)))?;

Expand All @@ -45,11 +50,15 @@ 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 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
Expand Down
42 changes: 27 additions & 15 deletions src/connection/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ 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 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)
Expand All @@ -41,21 +45,29 @@ 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 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<T: ToSocketAddrs>(address: T) -> io::Result<UdpConnection> {
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)
}
Expand Down

0 comments on commit 44dba87

Please sign in to comment.