mirror of
https://github.com/ratspeak/rsReticulum
synced 2026-11-27 13:52:16 +00:00
link: add a clock that keeps counting while the system is suspended
This commit is contained in:
1 parent
379312e305
commit
4683cb2c2a
10 files changed
+395
-2
No files matched your search
@@ -24,6 +24,7 @@
|
||||
- Announces held during an ingress burst on an IFAC interface are processed when released instead of being dropped, and released or replayed announces no longer count as received traffic again, as in Python Reticulum.
|
||||
- Interface discovery announces name this stack, `rsRNS`, and its version. `rnsd-rs` auto-connects only to Backbone interfaces announced by Python Reticulum 1.5.2 or later, or to any announced Backbone interface with `autoconnect_unverified_implementations`, and no longer stores or lists discovered TCP client interfaces, as in Python Reticulum.
|
||||
- Discovery announces a KISS-framed TCP client as a KISS interface and sends and reads unset radio parameters as nil, so Python and `rnsd-rs` read each other's KISS, Weave and RNode announces, as in Python Reticulum.
|
||||
- `rns_link::clock::ContinuousInstant` reads a monotonic clock that keeps counting while the system is suspended, for deadlines that must hold in real time across a sleep.
|
||||
|
||||
## 1.4.0 - 2026-10-12
|
||||
|
||||
|
||||
Generated
+2
@@ -1702,6 +1702,7 @@ name = "rns-link"
|
||||
version = "1.4.0"
|
||||
dependencies = [
|
||||
"hex",
|
||||
"libc",
|
||||
"proptest",
|
||||
"rand 0.8.5",
|
||||
"rmpv",
|
||||
@@ -1710,6 +1711,7 @@ dependencies = [
|
||||
"rns-wire",
|
||||
"thiserror 2.0.21",
|
||||
"tracing",
|
||||
"windows-sys 0.61.2",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
|
||||
Generated
+2
@@ -1252,6 +1252,7 @@ name = "rns-link"
|
||||
version = "1.4.0"
|
||||
dependencies = [
|
||||
"hex",
|
||||
"libc",
|
||||
"rand",
|
||||
"rmpv",
|
||||
"rns-crypto",
|
||||
@@ -1259,6 +1260,7 @@ dependencies = [
|
||||
"rns-wire",
|
||||
"thiserror 2.0.20",
|
||||
"tracing",
|
||||
"windows-sys 0.61.2",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
|
||||
@@ -96,6 +96,17 @@ auto-connect rule, with `ReticulumConfig::autoconnect_unverified_implementations
|
||||
as its opt-in. The receiver no longer stores types that are never listed, so
|
||||
`discovery::Reason` gains `UnlistedType`; exhaustive matches need the arm.
|
||||
|
||||
`rns_link::clock::ContinuousInstant` reads a monotonic clock that keeps
|
||||
counting while the system is suspended: `CLOCK_BOOTTIME` on Linux and Android,
|
||||
`CLOCK_MONOTONIC_RAW` on Apple platforms and the performance counter on
|
||||
Windows, with `std::time::Instant` as a best-effort fallback elsewhere. Use it
|
||||
for a deadline that must hold in real time across a sleep, and check that
|
||||
deadline on a regular tick: tokio's timers and the operating system's waits
|
||||
count awake time only, so a timer armed for it fires late by the whole sleep.
|
||||
Readings are process-local and do not convert to or from `std::time::Instant`.
|
||||
rns-link gains the target dependencies `libc` (unix) and `windows-sys`
|
||||
(Windows), so dependents that build with `--locked` refresh their lock files.
|
||||
|
||||
The compatibility floor remains unchanged. Its cumulative exact-removal list
|
||||
also retains the already-reviewed [1.3 migration](1.3.md) for discovery ownership
|
||||
and nullable coordinates; it is not reclassified as a new 1.4 change. Additive
|
||||
|
||||
@@ -843,6 +843,30 @@
|
||||
"derive"
|
||||
],
|
||||
"sourceKind": "registry"
|
||||
},
|
||||
{
|
||||
"name": "libc",
|
||||
"rename": null,
|
||||
"kind": null,
|
||||
"target": "cfg(unix)",
|
||||
"requirement": "^0.2",
|
||||
"optional": false,
|
||||
"defaultFeatures": true,
|
||||
"features": [],
|
||||
"sourceKind": "registry"
|
||||
},
|
||||
{
|
||||
"name": "windows-sys",
|
||||
"rename": null,
|
||||
"kind": null,
|
||||
"target": "cfg(windows)",
|
||||
"requirement": "^0.61",
|
||||
"optional": false,
|
||||
"defaultFeatures": true,
|
||||
"features": [
|
||||
"Win32_System_Performance"
|
||||
],
|
||||
"sourceKind": "registry"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -1,4 +1,27 @@
|
||||
pub mod rns_link
|
||||
pub mod rns_link::clock
|
||||
pub struct rns_link::clock::ContinuousInstant(_)
|
||||
impl rns_link::clock::ContinuousInstant
|
||||
pub fn rns_link::clock::ContinuousInstant::checked_add(self, core::time::Duration) -> core::option::Option<Self>
|
||||
pub fn rns_link::clock::ContinuousInstant::checked_duration_since(self, Self) -> core::option::Option<core::time::Duration>
|
||||
pub fn rns_link::clock::ContinuousInstant::checked_sub(self, core::time::Duration) -> core::option::Option<Self>
|
||||
pub fn rns_link::clock::ContinuousInstant::duration_since(self, Self) -> core::time::Duration
|
||||
pub fn rns_link::clock::ContinuousInstant::elapsed(self) -> core::time::Duration
|
||||
pub fn rns_link::clock::ContinuousInstant::now() -> Self
|
||||
pub fn rns_link::clock::ContinuousInstant::saturating_duration_since(self, Self) -> core::time::Duration
|
||||
impl core::ops::arith::Add<core::time::Duration> for rns_link::clock::ContinuousInstant
|
||||
pub type rns_link::clock::ContinuousInstant::Output = rns_link::clock::ContinuousInstant
|
||||
pub fn rns_link::clock::ContinuousInstant::add(self, core::time::Duration) -> Self
|
||||
impl core::ops::arith::AddAssign<core::time::Duration> for rns_link::clock::ContinuousInstant
|
||||
pub fn rns_link::clock::ContinuousInstant::add_assign(&mut self, core::time::Duration)
|
||||
impl core::ops::arith::Sub for rns_link::clock::ContinuousInstant
|
||||
pub type rns_link::clock::ContinuousInstant::Output = core::time::Duration
|
||||
pub fn rns_link::clock::ContinuousInstant::sub(self, Self) -> core::time::Duration
|
||||
impl core::ops::arith::Sub<core::time::Duration> for rns_link::clock::ContinuousInstant
|
||||
pub type rns_link::clock::ContinuousInstant::Output = rns_link::clock::ContinuousInstant
|
||||
pub fn rns_link::clock::ContinuousInstant::sub(self, core::time::Duration) -> Self
|
||||
impl core::ops::arith::SubAssign<core::time::Duration> for rns_link::clock::ContinuousInstant
|
||||
pub fn rns_link::clock::ContinuousInstant::sub_assign(&mut self, core::time::Duration)
|
||||
pub mod rns_link::constants
|
||||
pub const rns_link::constants::ACCEPT_ALL: u8
|
||||
pub const rns_link::constants::ACCEPT_APP: u8
|
||||
|
||||
+2
-2
@@ -60,8 +60,8 @@
|
||||
"tier": "candidate-stable",
|
||||
"compatibility": "reviewed-snapshot",
|
||||
"snapshot": "api/snapshots/rns-link.txt",
|
||||
"itemCount": 361,
|
||||
"sha256": "d76714f3e697303dac4376e6b8a660e037cf2957f196b867ae3149151fc7cf78"
|
||||
"itemCount": 384,
|
||||
"sha256": "6c7db32623681ef8b2fdce0c54edbb992d307f0fae8b361c65109a6d262d9389"
|
||||
},
|
||||
{
|
||||
"name": "rns-protocol",
|
||||
|
||||
@@ -22,5 +22,11 @@ rand = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
hex = { workspace = true }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libc = "0.2"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.61", features = ["Win32_System_Performance"] }
|
||||
|
||||
[dev-dependencies]
|
||||
proptest = { workspace = true }
|
||||
@@ -0,0 +1,323 @@
|
||||
//! A monotonic clock that keeps counting while the system is suspended.
|
||||
//!
|
||||
//! `std::time::Instant` stops while Apple, Linux and Android systems sleep,
|
||||
//! so a deadline measured on it ends late by the whole sleep. This clock
|
||||
//! counts that time: `CLOCK_BOOTTIME` on Linux and Android,
|
||||
//! `CLOCK_MONOTONIC_RAW` (`mach_continuous_time`) on Apple platforms, and the
|
||||
//! performance counter on Windows. On other targets it is best effort,
|
||||
//! `std::time::Instant` from the first reading in the process.
|
||||
//!
|
||||
//! Timers do not run on this clock: tokio and the operating system's waits
|
||||
//! count awake time only. Check a deadline against [`ContinuousInstant::now`]
|
||||
//! on a regular tick instead of arming a timer for it.
|
||||
|
||||
use std::ops::{Add, AddAssign, Sub, SubAssign};
|
||||
use std::time::Duration;
|
||||
|
||||
/// A reading of the suspend-counting clock, in signed nanoseconds from the
|
||||
/// platform's origin, usually boot.
|
||||
///
|
||||
/// Readings are process-local: never persist one or send it to another
|
||||
/// process, which is what wall time is for. They do not convert to or from
|
||||
/// `std::time::Instant`, so the two clocks never mix.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct ContinuousInstant(i64);
|
||||
|
||||
impl ContinuousInstant {
|
||||
/// The current reading.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// If the platform clock fails, as `std::time::Instant::now` does.
|
||||
pub fn now() -> Self {
|
||||
Self(backend::now_nanos())
|
||||
}
|
||||
|
||||
/// The time from `earlier` to `self`, or zero if `earlier` is later.
|
||||
pub fn duration_since(self, earlier: Self) -> Duration {
|
||||
self.saturating_duration_since(earlier)
|
||||
}
|
||||
|
||||
/// The time from `earlier` to `self`, or zero if `earlier` is later.
|
||||
pub fn saturating_duration_since(self, earlier: Self) -> Duration {
|
||||
self.checked_duration_since(earlier).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The time from `earlier` to `self`, or `None` if `earlier` is later.
|
||||
pub fn checked_duration_since(self, earlier: Self) -> Option<Duration> {
|
||||
let nanos = i128::from(self.0) - i128::from(earlier.0);
|
||||
u64::try_from(nanos).ok().map(Duration::from_nanos)
|
||||
}
|
||||
|
||||
/// The time since this reading, including time the system was suspended.
|
||||
pub fn elapsed(self) -> Duration {
|
||||
Self::now().duration_since(self)
|
||||
}
|
||||
|
||||
/// `self + duration`, or `None` beyond the clock's range, about 292 years
|
||||
/// either side of its origin.
|
||||
pub fn checked_add(self, duration: Duration) -> Option<Self> {
|
||||
Self::from_nanos(i128::from(self.0) + nanos(duration)?)
|
||||
}
|
||||
|
||||
/// `self - duration`, or `None` beyond the clock's range, about 292 years
|
||||
/// either side of its origin.
|
||||
pub fn checked_sub(self, duration: Duration) -> Option<Self> {
|
||||
Self::from_nanos(i128::from(self.0) - nanos(duration)?)
|
||||
}
|
||||
|
||||
fn from_nanos(nanos: i128) -> Option<Self> {
|
||||
i64::try_from(nanos).ok().map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
fn nanos(duration: Duration) -> Option<i128> {
|
||||
i128::try_from(duration.as_nanos()).ok()
|
||||
}
|
||||
|
||||
/// # Panics
|
||||
///
|
||||
/// Beyond the clock's range, as `std::time::Instant` does.
|
||||
impl Add<Duration> for ContinuousInstant {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, duration: Duration) -> Self {
|
||||
self.checked_add(duration)
|
||||
.expect("overflow when adding duration to instant")
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<Duration> for ContinuousInstant {
|
||||
fn add_assign(&mut self, duration: Duration) {
|
||||
*self = *self + duration;
|
||||
}
|
||||
}
|
||||
|
||||
/// # Panics
|
||||
///
|
||||
/// Beyond the clock's range, as `std::time::Instant` does.
|
||||
impl Sub<Duration> for ContinuousInstant {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, duration: Duration) -> Self {
|
||||
self.checked_sub(duration)
|
||||
.expect("overflow when subtracting duration from instant")
|
||||
}
|
||||
}
|
||||
|
||||
impl SubAssign<Duration> for ContinuousInstant {
|
||||
fn sub_assign(&mut self, duration: Duration) {
|
||||
*self = *self - duration;
|
||||
}
|
||||
}
|
||||
|
||||
/// The time from `earlier` to `self`, or zero if `earlier` is later.
|
||||
impl Sub for ContinuousInstant {
|
||||
type Output = Duration;
|
||||
|
||||
fn sub(self, earlier: Self) -> Duration {
|
||||
self.duration_since(earlier)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))]
|
||||
mod backend {
|
||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||
const CLOCK: libc::clockid_t = libc::CLOCK_BOOTTIME;
|
||||
#[cfg(target_vendor = "apple")]
|
||||
const CLOCK: libc::clockid_t = libc::CLOCK_MONOTONIC_RAW;
|
||||
|
||||
// `time_t` and `c_long` are 32 bits wide on some of these targets.
|
||||
#[allow(clippy::useless_conversion)]
|
||||
pub(super) fn now_nanos() -> i64 {
|
||||
let mut now = std::mem::MaybeUninit::<libc::timespec>::uninit();
|
||||
// SAFETY: `now` is valid for writes of a timespec for the whole call.
|
||||
let result = unsafe { libc::clock_gettime(CLOCK, now.as_mut_ptr()) };
|
||||
assert_eq!(
|
||||
result,
|
||||
0,
|
||||
"clock_gettime failed: {}",
|
||||
std::io::Error::last_os_error()
|
||||
);
|
||||
// SAFETY: a successful `clock_gettime` filled in `now`.
|
||||
let now = unsafe { now.assume_init() };
|
||||
i64::from(now.tv_sec) * 1_000_000_000 + i64::from(now.tv_nsec)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod backend {
|
||||
use std::sync::OnceLock;
|
||||
use windows_sys::Win32::System::Performance::{
|
||||
QueryPerformanceCounter, QueryPerformanceFrequency,
|
||||
};
|
||||
|
||||
fn frequency() -> i64 {
|
||||
static FREQUENCY: OnceLock<i64> = OnceLock::new();
|
||||
*FREQUENCY.get_or_init(|| {
|
||||
let mut frequency = 0;
|
||||
// SAFETY: `frequency` is a valid, writable i64 for the whole call.
|
||||
let succeeded = unsafe { QueryPerformanceFrequency(&mut frequency) } != 0;
|
||||
assert!(
|
||||
succeeded && frequency > 0,
|
||||
"QueryPerformanceFrequency failed"
|
||||
);
|
||||
frequency
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn now_nanos() -> i64 {
|
||||
let mut counter = 0;
|
||||
// SAFETY: `counter` is a valid, writable i64 for the whole call.
|
||||
let succeeded = unsafe { QueryPerformanceCounter(&mut counter) } != 0;
|
||||
assert!(succeeded, "QueryPerformanceCounter failed");
|
||||
let nanos = i128::from(counter) * 1_000_000_000 / i128::from(frequency());
|
||||
i64::try_from(nanos).expect("performance counter beyond the clock's range")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_vendor = "apple",
|
||||
windows
|
||||
)))]
|
||||
mod backend {
|
||||
use std::sync::OnceLock;
|
||||
use std::time::Instant;
|
||||
|
||||
pub(super) fn now_nanos() -> i64 {
|
||||
static ORIGIN: OnceLock<Instant> = OnceLock::new();
|
||||
let origin = *ORIGIN.get_or_init(Instant::now);
|
||||
i64::try_from(origin.elapsed().as_nanos()).expect("clock beyond its range")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn readings_never_go_backwards() {
|
||||
let mut last = ContinuousInstant::now();
|
||||
for _ in 0..10_000 {
|
||||
let now = ContinuousInstant::now();
|
||||
assert!(now >= last, "{now:?} after {last:?}");
|
||||
last = now;
|
||||
}
|
||||
}
|
||||
|
||||
/// While the system is awake the clock keeps `Instant`'s rate, which
|
||||
/// catches a backend's unit or scaling mistake. Each reading is bracketed
|
||||
/// by two `Instant` readings, so a preemption widens the bounds instead of
|
||||
/// failing the check.
|
||||
#[test]
|
||||
fn it_runs_at_instants_rate_while_awake() {
|
||||
let bracketed = || {
|
||||
let before = std::time::Instant::now();
|
||||
let reading = ContinuousInstant::now();
|
||||
(before, reading, std::time::Instant::now())
|
||||
};
|
||||
let (start_before, start, start_after) = bracketed();
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
let (end_before, end, end_after) = bracketed();
|
||||
let advance = (end - start).as_secs_f64();
|
||||
let shortest = end_before.duration_since(start_after).as_secs_f64();
|
||||
let longest = end_after.duration_since(start_before).as_secs_f64();
|
||||
assert!(
|
||||
(shortest * 0.95..=longest * 1.05).contains(&advance),
|
||||
"advanced {advance}s while Instant advanced {shortest}s to {longest}s"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backdating_never_underflows() {
|
||||
let now = ContinuousInstant::now();
|
||||
let century = Duration::from_secs(100 * 365 * 24 * 3600);
|
||||
let past = now - century;
|
||||
assert!(past < now);
|
||||
assert_eq!(now.duration_since(past), century);
|
||||
let later = ContinuousInstant::now();
|
||||
assert_eq!(later - past - (later - now), century);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_later_earlier_reading_saturates() {
|
||||
let (earlier, later) = (ContinuousInstant(-5), ContinuousInstant(7));
|
||||
assert_eq!(later - earlier, Duration::from_nanos(12));
|
||||
assert_eq!(earlier - later, Duration::ZERO);
|
||||
assert_eq!(earlier.duration_since(later), Duration::ZERO);
|
||||
assert_eq!(earlier.saturating_duration_since(later), Duration::ZERO);
|
||||
assert_eq!(earlier.checked_duration_since(later), None);
|
||||
assert_eq!(later.checked_duration_since(later), Some(Duration::ZERO));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arithmetic_beyond_the_range_is_none() {
|
||||
let (min, max) = (ContinuousInstant(i64::MIN), ContinuousInstant(i64::MAX));
|
||||
assert_eq!(max.checked_add(Duration::from_nanos(1)), None);
|
||||
assert_eq!(min.checked_sub(Duration::from_nanos(1)), None);
|
||||
assert_eq!(ContinuousInstant(0).checked_add(Duration::MAX), None);
|
||||
assert_eq!(ContinuousInstant(0).checked_sub(Duration::MAX), None);
|
||||
assert_eq!(
|
||||
max.checked_duration_since(min),
|
||||
Some(Duration::from_nanos(u64::MAX))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "overflow when adding duration to instant")]
|
||||
fn adding_beyond_the_range_panics() {
|
||||
let _ = ContinuousInstant(i64::MAX) + Duration::from_nanos(1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "overflow when subtracting duration from instant")]
|
||||
fn subtracting_beyond_the_range_panics() {
|
||||
let _ = ContinuousInstant(i64::MIN) - Duration::from_nanos(1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assigning_operators_match_the_plain_ones() {
|
||||
let start = ContinuousInstant(1_000);
|
||||
let mut moved = start;
|
||||
moved += Duration::from_nanos(250);
|
||||
assert_eq!(moved, start + Duration::from_nanos(250));
|
||||
moved -= Duration::from_nanos(500);
|
||||
assert_eq!(moved, start - Duration::from_nanos(250));
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn a_duration_added_is_the_duration_measured(origin in any::<i64>(), nanos in any::<u64>()) {
|
||||
let start = ContinuousInstant(origin);
|
||||
let duration = Duration::from_nanos(nanos);
|
||||
match start.checked_add(duration) {
|
||||
Some(end) => {
|
||||
prop_assert_eq!(end - start, duration);
|
||||
prop_assert_eq!(end.checked_sub(duration), Some(start));
|
||||
prop_assert_eq!(start.saturating_duration_since(end), Duration::ZERO);
|
||||
prop_assert_eq!(
|
||||
start.checked_duration_since(end),
|
||||
(nanos == 0).then_some(Duration::ZERO)
|
||||
);
|
||||
}
|
||||
None => prop_assert!(i128::from(origin) + i128::from(nanos) > i128::from(i64::MAX)),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_duration_subtracted_is_the_duration_measured(origin in any::<i64>(), nanos in any::<u64>()) {
|
||||
let end = ContinuousInstant(origin);
|
||||
let duration = Duration::from_nanos(nanos);
|
||||
match end.checked_sub(duration) {
|
||||
Some(start) => {
|
||||
prop_assert_eq!(end.duration_since(start), duration);
|
||||
prop_assert_eq!(start.checked_add(duration), Some(end));
|
||||
}
|
||||
None => prop_assert!(i128::from(origin) - i128::from(nanos) < i128::from(i64::MIN)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
//! LR/LRPROOF handshake → AES-256 + HMAC-SHA256 session, MTU discovery,
|
||||
//! keepalives, request/response. Python reference: `RNS/Link.py`.
|
||||
|
||||
pub mod clock;
|
||||
pub mod constants;
|
||||
pub mod encryption;
|
||||
pub mod handshake;
|
||||
|
||||
Reference in new issue
Block a user