Skip to content

Commit 28b9901

Browse files
committed
update dhcp example to work in the case where we do not already have an ip address from some other source
1 parent 5eced2a commit 28b9901

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

examples/dhcp_client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ fn main() {
3838
config.random_seed = rand::random();
3939
let mut iface = Interface::new(config, &mut device, Instant::now());
4040

41+
// Initialize with an unspecified address
42+
set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
43+
4144
// Create sockets
4245
let mut dhcp_socket = dhcpv4::Socket::new();
4346

@@ -86,9 +89,10 @@ fn main() {
8689
}
8790
}
8891

92+
/// Clear any existing IP addresses & add the new one
8993
fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
9094
iface.update_ip_addrs(|addrs| {
91-
let dest = addrs.iter_mut().next().unwrap();
92-
*dest = IpCidr::Ipv4(cidr);
95+
addrs.clear();
96+
addrs.push(IpCidr::Ipv4(cidr)).unwrap();
9397
});
9498
}

0 commit comments

Comments
 (0)