toe_beans/v4/server/leases/config.rs
1use ip_network::Ipv4Network;
2use mac_address::MacAddress;
3use serde::{Deserialize, Serialize};
4
5use super::LeaseTime;
6use std::{collections::HashMap, net::Ipv4Addr, path::PathBuf};
7
8/// All configuration for a `Leases`.
9#[derive(Serialize, Deserialize, Clone, Debug)]
10pub struct LeaseConfig {
11 /// The path where the leases files is written to and read from.
12 /// The name, however, is always the same and not configurable.
13 ///
14 /// This will only be used when the leases file is used.
15 #[serde(skip)]
16 pub leases_file_path: PathBuf,
17 /// See: [LeaseTime](LeaseTime)
18 pub lease_time: LeaseTime,
19 /// A range of IP addresses for the server to lease. Specified in CIDR notation. For example: `10.1.9.32/16`
20 pub network_cidr: Ipv4Network,
21 /// The server will always assign these ip adddresses to these mac addresses.
22 /// All static leases must be within the network_cidr range.
23 pub static_leases: HashMap<MacAddress, Ipv4Addr>,
24 /// Whether to read and write leases to a file to maintain state between server restarts.
25 /// Defaults to `true` (when not in a benchmark or integration test) but you might want to set to `false` for performance, lack of storage, etc.
26 pub use_leases_file: bool,
27}
28
29impl Default for LeaseConfig {
30 fn default() -> Self {
31 Self {
32 lease_time: LeaseTime::ONE_DAY,
33 network_cidr: Ipv4Network::new(Ipv4Addr::new(10, 0, 0, 0), 16).unwrap(), // 2^(32-16) addresses
34 static_leases: HashMap::new(),
35 use_leases_file: cfg!(all(
36 not(feature = "benchmark"),
37 not(feature = "integration")
38 )),
39 leases_file_path: PathBuf::new(), // empty, current directory
40 }
41 }
42}