pub struct IpAllocator { /* private fields */ }Expand description
IP allocator for overlay network addresses
Tracks allocated IP addresses and provides next-available allocation from a configured CIDR range. Supports both IPv4 and IPv6 networks.
Implementations§
Source§impl IpAllocator
impl IpAllocator
Sourcepub fn new(cidr: &str) -> Result<Self>
pub fn new(cidr: &str) -> Result<Self>
Create a new IP allocator for the given CIDR range
Supports both IPv4 (e.g., “10.200.0.0/16”) and IPv6 (e.g., fd00::/48).
§Arguments
cidr- Network CIDR notation
§Errors
Returns OverlayError::InvalidCidr if the CIDR string cannot be parsed.
§Example
use zlayer_overlay::allocator::IpAllocator;
let v4 = IpAllocator::new("10.200.0.0/16").unwrap();
let v6 = IpAllocator::new("fd00::/48").unwrap();Sourcepub fn from_state(state: IpAllocatorState) -> Result<Self>
pub fn from_state(state: IpAllocatorState) -> Result<Self>
Create an allocator from persisted state
§Errors
Returns an error if the CIDR is invalid or any IP is out of range.
Sourcepub fn to_state(&self) -> IpAllocatorState
pub fn to_state(&self) -> IpAllocatorState
Get the current state for persistence
Sourcepub async fn load(path: &Path) -> Result<Self>
pub async fn load(path: &Path) -> Result<Self>
Load allocator state from a file
§Errors
Returns an error if the file cannot be read or the state is invalid.
Sourcepub async fn save(&self, path: &Path) -> Result<()>
pub async fn save(&self, path: &Path) -> Result<()>
Save allocator state to a file
§Errors
Returns an error if the file cannot be written or serialization fails.
Sourcepub fn allocate(&mut self) -> Option<IpAddr>
pub fn allocate(&mut self) -> Option<IpAddr>
Allocate the next available IP address
For IPv4, skips the network and broadcast addresses. For IPv6, skips the network address.
Returns None if all addresses in the CIDR range are allocated.
§Example
use zlayer_overlay::allocator::IpAllocator;
let mut allocator = IpAllocator::new("10.200.0.0/24").unwrap();
let ip = allocator.allocate().unwrap();
assert_eq!(ip.to_string(), "10.200.0.1");Sourcepub fn allocate_specific(&mut self, ip: IpAddr) -> Result<()>
pub fn allocate_specific(&mut self, ip: IpAddr) -> Result<()>
Allocate a specific IP address
§Errors
Returns an error if the IP is already allocated or not in the CIDR range.
Sourcepub fn allocate_first(&mut self) -> Result<IpAddr>
pub fn allocate_first(&mut self) -> Result<IpAddr>
Allocate the first usable IP in the range (typically for the leader)
§Example
use zlayer_overlay::allocator::IpAllocator;
let mut allocator = IpAllocator::new("10.200.0.0/24").unwrap();
let ip = allocator.allocate_first().unwrap();
assert_eq!(ip.to_string(), "10.200.0.1");§Errors
Returns an error if no IPs are available or the first IP is already allocated.
Sourcepub fn mark_allocated(&mut self, ip: IpAddr) -> Result<()>
pub fn mark_allocated(&mut self, ip: IpAddr) -> Result<()>
Mark an IP address as allocated (for restoring state)
§Errors
Returns an error if the IP is not in the CIDR range.
Sourcepub fn release(&mut self, ip: IpAddr) -> bool
pub fn release(&mut self, ip: IpAddr) -> bool
Release an IP address back to the pool
Returns true if the IP was released, false if it wasn’t allocated.
Sourcepub fn is_allocated(&self, ip: IpAddr) -> bool
pub fn is_allocated(&self, ip: IpAddr) -> bool
Check if an IP address is allocated
Sourcepub fn allocated_count(&self) -> usize
pub fn allocated_count(&self) -> usize
Get the number of allocated addresses
Sourcepub fn total_hosts(&self) -> u32
pub fn total_hosts(&self) -> u32
Get the total number of usable addresses in the range
For IPv6 networks with large host spaces, this saturates at u32::MAX.
Sourcepub fn available_count(&self) -> u32
pub fn available_count(&self) -> u32
Get the number of available addresses
Sourcepub fn network_addr(&self) -> IpAddr
pub fn network_addr(&self) -> IpAddr
Get the network address
Sourcepub fn broadcast_addr(&self) -> IpAddr
pub fn broadcast_addr(&self) -> IpAddr
Get the broadcast address
For IPv6, returns the last address in the range (all host bits set to 1).
Sourcepub fn prefix_len(&self) -> u8
pub fn prefix_len(&self) -> u8
Get the prefix length
Sourcepub fn host_prefix_len(&self) -> u8
pub fn host_prefix_len(&self) -> u8
Get the host prefix length (32 for IPv4, 128 for IPv6)
Sourcepub fn allocated_ips(&self) -> Vec<IpAddr>
pub fn allocated_ips(&self) -> Vec<IpAddr>
Get all allocated IPs
Trait Implementations§
Source§impl Clone for IpAllocator
impl Clone for IpAllocator
Source§fn clone(&self) -> IpAllocator
fn clone(&self) -> IpAllocator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more