Skip to content

Commit 1191598

Browse files
committed
go back to clearing and pushing new address
1 parent 53d7d0f commit 1191598

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

examples/dhcp_client.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,8 @@ fn main() {
3838
config.random_seed = rand::random();
3939
let mut iface = Interface::new(config, &mut device, Instant::now());
4040

41-
// Initialize with an address so that there is something to mutate later.
42-
// This also allows sockets made before a real address is negotiated
43-
// via DHCP to update properly afterward.
44-
iface.update_ip_addrs(|addrs| {
45-
addrs
46-
.push(IpCidr::Ipv4(Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0)))
47-
.unwrap();
48-
});
41+
// Initialize with an unspecified address
42+
set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
4943

5044
// Create sockets
5145
let mut dhcp_socket = dhcpv4::Socket::new();
@@ -95,10 +89,10 @@ fn main() {
9589
}
9690
}
9791

98-
/// Mutate the first IP address to match the one supplied
92+
/// Clear any existing IP addresses & add the new one
9993
fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
10094
iface.update_ip_addrs(|addrs| {
101-
let dest = addrs.iter_mut().next().unwrap();
102-
*dest = IpCidr::Ipv4(cidr);
95+
addrs.clear();
96+
addrs.push(IpCidr::Ipv4(cidr)).unwrap();
10397
});
10498
}

0 commit comments

Comments
 (0)