var (
ErrFull = errors.New("range is full")
ErrNotInRange = errors.New("provided IP is not in the valid range")
ErrAllocated = errors.New("provided IP is already allocated")
ErrMismatchedNetwork = errors.New("the provided network does not match the current range")
ErrAllocationDisabled = errors.New("IP addresses cannot be allocated at this time")
)
type Range struct {
// contains filtered or unexported fields
}
Range is a contiguous block of IPs that can be allocated atomically.
The internal structure of the range is:
For CIDR 10.0.0.0/24
254 addresses usable out of 256 total (minus base and broadcast IPs)
The number of usable addresses is r.max
CIDR base IP CIDR broadcast IP
10.0.0.0 10.0.0.255
| |
0 1 2 3 4 5 ... ... 253 254 255
| |
r.base r.base + r.max
| |
first bit of r.allocated last bit of r.allocated
If an address is taken, the bit at offset:
bit offset := IP - r.base
is set to one. r.count is always equal to the number of set bits and
can be recalculated at any time by counting the set bits in r.allocated.
TODO: use RLE and compact the allocator to minimize space.
Allocate attempts to reserve the provided IP. ErrNotInRange or
ErrAllocated will be returned if the IP is not valid for this range
or has already been reserved. ErrFull will be returned if there
are no addresses left.
Restore restores the pool to the previously captured state. ErrMismatchedNetwork
is returned if the provided IPNet range doesn't exactly match the previous range.