Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing panic that comes from opening sockets before DHCP is complete #50

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This document describes the changes to smoltcp-nal between releases.

# Unreleased

## Fixed
* Fixed an issue where attempting to open sockets before DHCP was completed wwould result in an
internal panic.

# [0.5.0] - 2024-04-22

## Changed
Expand Down
19 changes: 11 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub enum NetworkError {
UdpWriteFailure(smoltcp::socket::udp::SendError),
Unsupported,
NotConnected,
NoAddress,
}

impl embedded_nal::TcpError for NetworkError {
Expand Down Expand Up @@ -561,15 +562,16 @@ where
// Select a random port to bind to locally.
let local_port = self.get_ephemeral_port();

let local_address = self
let Some(cidr) = self
.network_interface
.ip_addrs()
.iter()
.find(|item| matches!(item, smoltcp::wire::IpCidr::Ipv4(_)))
.unwrap()
.address();
else {
return Err(NetworkError::NoAddress);
};

let local_endpoint = IpEndpoint::new(local_address, local_port);
let local_endpoint = IpEndpoint::new(cidr.address(), local_port);

let internal_socket: &mut smoltcp::socket::udp::Socket =
self.sockets.get_mut(socket.handle);
Expand Down Expand Up @@ -639,15 +641,16 @@ where
{
/// Bind a UDP socket to a specific port.
fn bind(&mut self, socket: &mut UdpSocket, local_port: u16) -> Result<(), NetworkError> {
let local_address = self
let Some(cidr) = self
.network_interface
.ip_addrs()
.iter()
.find(|item| matches!(item, smoltcp::wire::IpCidr::Ipv4(_)))
.unwrap()
.address();
else {
return Err(NetworkError::NoAddress);
};

let local_endpoint = IpEndpoint::new(local_address, local_port);
let local_endpoint = IpEndpoint::new(cidr.address(), local_port);

let internal_socket: &mut smoltcp::socket::udp::Socket =
self.sockets.get_mut(socket.handle);
Expand Down
Loading