Skip to content

Commit

Permalink
just initialize with an address and add a note that the function for …
Browse files Browse the repository at this point in the history
…setting the ip address mutates an entry
  • Loading branch information
jlogan03 committed Feb 5, 2024
1 parent dc771ec commit 43383b3
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions examples/dhcp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ 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 @@ -77,7 +84,6 @@ fn main() {
}
Some(dhcpv4::Event::Deconfigured) => {
debug!("DHCP lost config!");
iface.update_ip_addrs(|addrs| addrs.clear());
set_ipv4_addr(&mut iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0));
iface.routes_mut().remove_default_ipv4_route();
}
Expand All @@ -87,8 +93,10 @@ 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| {
addrs.push(IpCidr::Ipv4(cidr)).unwrap();
let dest = addrs.iter_mut().next().unwrap();
*dest = IpCidr::Ipv4(cidr);
});
}

0 comments on commit 43383b3

Please sign in to comment.