Skip to content

Commit

Permalink
update dhcp example to work in the case where we do not already have …
Browse files Browse the repository at this point in the history
…an ip address from some other source
  • Loading branch information
jlogan03 committed Feb 9, 2024
1 parent 5eced2a commit 28b9901
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions examples/dhcp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
});
}

0 comments on commit 28b9901

Please sign in to comment.