100 lines
3.6 KiB
Rust
100 lines
3.6 KiB
Rust
//! RFC 1982 serial number arithmetic (32-bit SOA serials).
|
|
use std::cmp::Ordering;
|
|
|
|
/// Compare two SOA serials according to RFC 1982.
|
|
///
|
|
/// Returns `None` when the comparison is undefined (the values are exactly
|
|
/// 2^31 apart), in which case callers must treat the serials as incomparable.
|
|
pub fn compare(a: u32, b: u32) -> Option<Ordering> {
|
|
if a == b {
|
|
return Some(Ordering::Equal);
|
|
}
|
|
let diff = b.wrapping_sub(a);
|
|
if diff == 1 << 31 {
|
|
return None;
|
|
}
|
|
if diff < (1 << 31) { Some(Ordering::Less) } else { Some(Ordering::Greater) }
|
|
}
|
|
|
|
/// `true` when `new` is strictly newer than `old` (RFC 1982).
|
|
pub fn is_newer(new: u32, old: u32) -> bool {
|
|
compare(new, old) == Some(Ordering::Greater)
|
|
}
|
|
|
|
/// Serial number generation strategy used when a primary zone changes.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum SerialStrategy {
|
|
/// serial + 1
|
|
#[default]
|
|
Increment,
|
|
/// UNIX time in seconds (never decreases below serial + 1)
|
|
Unixtime,
|
|
/// YYYYMMDDnn
|
|
Date,
|
|
}
|
|
|
|
/// Compute the next serial for `current` using `strategy`; `now_unix` is the
|
|
/// current UNIX time. The result is always RFC 1982-newer than `current`.
|
|
pub fn next_serial(current: u32, strategy: SerialStrategy, now_unix: u64) -> u32 {
|
|
let candidate = match strategy {
|
|
SerialStrategy::Increment => current.wrapping_add(1),
|
|
SerialStrategy::Unixtime => now_unix as u32,
|
|
SerialStrategy::Date => {
|
|
let days = now_unix / 86_400;
|
|
let (y, m, d) = civil_from_days(days as i64);
|
|
let base = (y as u32) * 1_000_000 + m * 10_000 + d * 100;
|
|
let cur_base = current - current % 100;
|
|
if cur_base == base && current % 100 < 99 { current + 1 } else { base }
|
|
}
|
|
};
|
|
if is_newer(candidate, current) { candidate } else { current.wrapping_add(1) }
|
|
}
|
|
|
|
/// Convert days since 1970-01-01 into (year, month, day). (H. Hinnant's algorithm)
|
|
fn civil_from_days(z: i64) -> (i64, u32, u32) {
|
|
let z = z + 719_468;
|
|
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
|
|
let doe = (z - era * 146_097) as u64;
|
|
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
|
|
let y = yoe as i64 + era * 400;
|
|
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
|
|
let mp = (5 * doy + 2) / 153;
|
|
let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
|
|
let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
|
|
(if m <= 2 { y + 1 } else { y }, m, d)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn rfc1982_basic() {
|
|
assert_eq!(compare(1, 2), Some(Ordering::Less));
|
|
assert_eq!(compare(2, 1), Some(Ordering::Greater));
|
|
assert_eq!(compare(5, 5), Some(Ordering::Equal));
|
|
}
|
|
|
|
#[test]
|
|
fn rfc1982_wrap() {
|
|
assert!(is_newer(0, u32::MAX));
|
|
assert!(is_newer(10, 4_294_967_290));
|
|
assert!(!is_newer(4_294_967_290, 10));
|
|
assert_eq!(compare(0, 1 << 31), None);
|
|
}
|
|
|
|
#[test]
|
|
fn next_serial_strategies() {
|
|
assert_eq!(next_serial(41, SerialStrategy::Increment, 0), 42);
|
|
assert_eq!(next_serial(u32::MAX, SerialStrategy::Increment, 0), 0);
|
|
// 2026-11-21 00:00:00 UTC
|
|
let t = 1_795_219_200;
|
|
assert_eq!(next_serial(1, SerialStrategy::Date, t), 2026112100);
|
|
assert_eq!(next_serial(2026112100, SerialStrategy::Date, t), 2026112101);
|
|
assert_eq!(next_serial(5, SerialStrategy::Unixtime, t), t as u32);
|
|
// unixtime never goes backwards
|
|
assert_eq!(next_serial(t as u32 + 10, SerialStrategy::Unixtime, t), t as u32 + 11);
|
|
}
|
|
}
|