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

examples/dhcp_client.rs is misleading/wrong #783

Closed
pdh11 opened this issue May 6, 2023 · 1 comment · Fixed by #905
Closed

examples/dhcp_client.rs is misleading/wrong #783

pdh11 opened this issue May 6, 2023 · 1 comment · Fixed by #905

Comments

@pdh11
Copy link

pdh11 commented May 6, 2023

As of 0.9.1 and current main 8b0c521, the "dhcp_client.rs" example, when it gets a Configured event, attempts to set the interface's IPv4 address as follows:

fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
    iface.update_ip_addrs(|addrs| {
        let dest = addrs.iter_mut().next().unwrap();
        *dest = IpCidr::Ipv4(cidr);
    });
}

This doesn't actually work in Smoltcp 0.9.1, because iter_mut() on a heapless::Vec iterates only over the existing items, and is incapable of adding one if the Vec is currently empty. It always panics unwrapping the error from next(). This code worked in 0.8.x because iter_mut() on a mutable slice (which is what ManagedSlice derefs to) does iterate over all elements (which were set to UNSPECIFIED if not currently in use).

For smoltcp 0.9.x, set_ipv4_addr should probably do something like:

                iface.update_ip_addrs(|addrs| {
                    addrs.push(IpCidr::Ipv4(config.address)).unwrap();
                });

and likewise on a Deconfigured event it should do addrs.clear().

@jlogan03
Copy link
Contributor

jlogan03 commented Feb 4, 2024

@pdh11 thanks for making this issue! This probably saved me another 6 hours of troubleshooting while thinking this was a problem with my network switch. I made #905 to fix this, and have a piece of firmware based on those fixes running on a board now w/ IP address acquired successfully.

pdh11 added a commit to pdh11/cotton that referenced this issue Mar 2, 2024
Everything works, despite smoltcp-rs/smoltcp#783

Git stm32-eth is needed for now (but presumably smoltcp-0.9 support will
be in its next release).

With smoltcp 0.8 it wasn't possible to write send_with correctly: we only
know how big the packet will be when f() returns, which is too late for
udp::Socket::send; we need the new udp::Socket::send_with.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants