From 28b99014d98ea79ddd181e079d80e74b66b9bb4f Mon Sep 17 00:00:00 2001 From: James Logan Date: Sat, 3 Feb 2024 22:37:10 -0500 Subject: [PATCH] update dhcp example to work in the case where we do not already have an ip address from some other source --- examples/dhcp_client.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/dhcp_client.rs b/examples/dhcp_client.rs index 9ef46c27b..958624da7 100644 --- a/examples/dhcp_client.rs +++ b/examples/dhcp_client.rs @@ -38,6 +38,9 @@ fn main() { config.random_seed = rand::random(); let mut iface = Interface::new(config, &mut device, Instant::now()); + // Initialize with an unspecified address + set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0)); + // Create sockets let mut dhcp_socket = dhcpv4::Socket::new(); @@ -86,9 +89,10 @@ fn main() { } } +/// Clear any existing IP addresses & add the new one 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); + addrs.clear(); + addrs.push(IpCidr::Ipv4(cidr)).unwrap(); }); }