Skip to content

Commit 53d7d0f

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 53d7d0f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

examples/dhcp_client.rs

+10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ 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+
});
49+
4150
// Create sockets
4251
let mut dhcp_socket = dhcpv4::Socket::new();
4352

@@ -86,6 +95,7 @@ fn main() {
8695
}
8796
}
8897

98+
/// Mutate the first IP address to match the one supplied
8999
fn set_ipv4_addr(iface: &mut Interface, cidr: Ipv4Cidr) {
90100
iface.update_ip_addrs(|addrs| {
91101
let dest = addrs.iter_mut().next().unwrap();

0 commit comments

Comments
 (0)