diff --git a/Cargo.lock b/Cargo.lock index 1c522fe..96b34d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -401,6 +401,17 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" +[[package]] +name = "coarsetime" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e58eb270476aa4fc7843849f8a35063e8743b4dbcdf6dd0f8ea0886980c204c2" +dependencies = [ + "libc", + "wasix", + "wasm-bindgen", +] + [[package]] name = "colorchoice" version = "1.0.2" @@ -2296,6 +2307,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "coarsetime", "dhcproto", "ip_network", "ipnet", @@ -2853,6 +2865,15 @@ dependencies = [ "wit-bindgen-rt", ] +[[package]] +name = "wasix" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1757e0d1f8456693c7e5c6c629bdb54884e032aa0bb53c155f6a39f94440d332" +dependencies = [ + "wasi 0.11.0+wasi-snapshot-preview1", +] + [[package]] name = "wasm-bindgen" version = "0.2.106" diff --git a/Cargo.toml b/Cargo.toml index 90bad01..961caeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ ipnet = "2" oslog = "0.2.0" log = "0.4.29" serial_test = "3" +coarsetime = "0.1.37" [profile.release] debug = true diff --git a/lib/dhcp_snooper.rs b/lib/dhcp_snooper.rs index a47dc64..e40cdb2 100644 --- a/lib/dhcp_snooper.rs +++ b/lib/dhcp_snooper.rs @@ -2,7 +2,7 @@ use dhcproto::Decodable; use dhcproto::v4::{DhcpOption, MessageType, OptionCode}; use smoltcp::wire::Ipv4Address; use std::collections::HashSet; -use std::time::{Duration, Instant}; +use std::time::Duration; #[derive(Default)] pub struct DhcpSnooper { @@ -66,7 +66,7 @@ impl DhcpSnooper { #[derive(Debug)] pub struct Lease { address: Ipv4Address, - valid_until: Instant, + valid_until: coarsetime::Instant, dns_ips: HashSet, } @@ -74,7 +74,7 @@ impl Lease { pub fn new(address: Ipv4Address, lease_time: Duration, dns_ips: HashSet) -> Lease { Lease { address, - valid_until: Instant::now() + lease_time, + valid_until: coarsetime::Instant::recent() + lease_time.into(), dns_ips, } } @@ -84,7 +84,7 @@ impl Lease { } pub fn valid(&self) -> bool { - Instant::now() < self.valid_until + coarsetime::Instant::recent() < self.valid_until } pub fn valid_ip_source(&self, address: Ipv4Address) -> bool { diff --git a/lib/proxy/mod.rs b/lib/proxy/mod.rs index e5f6655..6f61a52 100644 --- a/lib/proxy/mod.rs +++ b/lib/proxy/mod.rs @@ -90,6 +90,9 @@ impl Proxy<'_> { self.poller.arm()?; loop { + // Update coarse time for the DHCP snooper + coarsetime::Instant::update(); + let (vm_readable, host_readable, interrupt) = self.poller.wait()?; if vm_readable {