toe_beans/v4/server/config/lease_time.rs
1use serde::{Deserialize, Serialize};
2use crate::v4::error::Result;
3
4/// The lowest valid value for lease_time (in seconds)
5const MIN_LEASE_TIME: u32 = 60;
6/// Used to adjust for clock drift between client and server (in seconds)
7const DRIFT: u32 = 10;
8
9/// The time, in seconds, before a lease expires.
10/// A lower value will increase dhcp traffic between client and server.
11/// A higher value will increase the chances that all ip addresses are assigned.
12/// The maximum value (4,294,967,295) is reserved to represent Infinity.
13///
14/// Lease time must be at least a minute because:
15/// 1. The server must subtract some time to account for drift.
16/// 2. The default value for T1 (renewal time) is half of the lease_time.
17#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
18pub struct LeaseTime(u32);
19
20impl LeaseTime {
21 /// Construct a valid lease_time field.
22 pub fn new(lease_time: u32) -> Result<Self> {
23 if lease_time < MIN_LEASE_TIME {
24 return Err("Lease time cant be less than the minimum lease time (60 seconds)");
25 }
26
27 Ok(Self(lease_time))
28 }
29
30 /// The actual lease_time, not adjusted for drift.
31 pub fn as_server(&self) -> u32 {
32 self.0
33 }
34
35 /// The lease_time, with a small amount subtracted to adjust for drift
36 pub fn as_client(&self) -> u32 {
37 self.0 - DRIFT
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn test_min_lease_time_greater_than_drift() {
47 assert!(MIN_LEASE_TIME > DRIFT);
48 }
49}