Skip to content

Commit ca909a2

Browse files
authored
Merge pull request #905 from jlogan03/jlogan/update-dhcp-example
Update DHCP example
2 parents f0d1fd9 + 1ff5bc4 commit ca909a2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

examples/dhcp_client.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::os::unix::io::AsRawFd;
77
use smoltcp::iface::{Config, Interface, SocketSet};
88
use smoltcp::socket::dhcpv4;
99
use smoltcp::time::Instant;
10-
use smoltcp::wire::{EthernetAddress, IpCidr, Ipv4Address, Ipv4Cidr};
10+
use smoltcp::wire::{EthernetAddress, IpCidr, Ipv4Cidr};
1111
use smoltcp::{
1212
phy::{wait as phy_wait, Device, Medium},
1313
time::Duration,
@@ -77,7 +77,7 @@ fn main() {
7777
}
7878
Some(dhcpv4::Event::Deconfigured) => {
7979
debug!("DHCP lost config!");
80-
set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
80+
iface.update_ip_addrs(|addrs| addrs.clear());
8181
iface.routes_mut().remove_default_ipv4_route();
8282
}
8383
}
@@ -86,9 +86,10 @@ fn main() {
8686
}
8787
}
8888

89+
/// Clear any existing IP addresses & add the new one
8990
fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
9091
iface.update_ip_addrs(|addrs| {
91-
let dest = addrs.iter_mut().next().unwrap();
92-
*dest = IpCidr::Ipv4(cidr);
92+
addrs.clear();
93+
addrs.push(IpCidr::Ipv4(cidr)).unwrap();
9394
});
9495
}

0 commit comments

Comments
 (0)