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 5, 2024
1 parent 5eced2a commit 53d7d0f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/dhcp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ fn main() {
config.random_seed = rand::random();
let mut iface = Interface::new(config, &mut device, Instant::now());

// Initialize with an address so that there is something to mutate later.
// This also allows sockets made before a real address is negotiated
// via DHCP to update properly afterward.
iface.update_ip_addrs(|addrs| {
addrs
.push(IpCidr::Ipv4(Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0)))
.unwrap();
});

// Create sockets
let mut dhcp_socket = dhcpv4::Socket::new();

Expand Down Expand Up @@ -86,6 +95,7 @@ fn main() {
}
}

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

0 comments on commit 53d7d0f

Please sign in to comment.