Procházet zdrojové kódy

update dhcp example to work in the case where we do not already have an ip address from some other source

James Logan před 1 rokem
rodič
revize
28b99014d9
1 změnil soubory, kde provedl 6 přidání a 2 odebrání
  1. 6 2
      examples/dhcp_client.rs

+ 6 - 2
examples/dhcp_client.rs

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