Documentation
¶
Index ¶
- Variables
- func RangeSize(prefix netip.Prefix) int64
- type CIDRRangeOption
- type ErrNotInRange
- type Interface
- type Range
- func (r *Range) Allocate(ip netip.Addr) error
- func (r *Range) AllocateNext() (netip.Addr, error)
- func (r *Range) CIDR() netip.Prefix
- func (r *Range) ForEach(fn func(netip.Addr))
- func (r *Range) Free() int
- func (r *Range) Has(ip netip.Addr) bool
- func (r *Range) Release(ip netip.Addr)
- func (r *Range) Restore(prefix netip.Prefix, data []byte) error
- func (r *Range) Snapshot() (string, []byte, error)
- func (r *Range) Used() int
Constants ¶
This section is empty.
Variables ¶
Functions ¶
Types ¶
type CIDRRangeOption ¶
type CIDRRangeOption func(*cidrRangeOptions)
CIDRRangeOption is a functional option for NewCIDRRange.
func WithAllowFirstLastIPs ¶
func WithAllowFirstLastIPs() CIDRRangeOption
WithAllowFirstLastIPs configures the Range to include the first and last IPs of the CIDR (normally reserved as network and broadcast addresses). This is useful for delegated prefixes (e.g. AWS /28 prefix delegation) where the entire range is exclusively assigned and there is no shared network segment.
type ErrNotInRange ¶
type ErrNotInRange struct {
ValidRange string
}
func (*ErrNotInRange) Error ¶
func (e *ErrNotInRange) Error() string
type Interface ¶
type Interface interface {
Allocate(netip.Addr) error
AllocateNext() (netip.Addr, error)
Release(netip.Addr) error
ForEach(func(netip.Addr))
CIDR() netip.Prefix
Has(addr netip.Addr) bool
}
Interface manages the allocation of IP addresses out of a range. Interface should be threadsafe.
type Range ¶
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 addOffset(r.base, r.max-1) | | offset #0 of r.alloc last offset of r.alloc
func NewCIDRRange ¶
func NewCIDRRange(prefix netip.Prefix, opts ...CIDRRangeOption) *Range
NewCIDRRange creates a Range over a netip.Prefix, calling allocator.NewAllocationMap to construct the backing store. By default, the first (network) and last (broadcast) addresses are excluded for CIDRs with more than 2 addresses. Pass functional options (e.g. WithAllowFirstLastIPs) to alter this behavior.
func (*Range) Allocate ¶
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.
func (*Range) AllocateNext ¶
AllocateNext reserves one of the IPs from the pool. ErrFull may be returned if there are no addresses left.
func (*Range) Has ¶
Has returns true if the provided IP is already allocated and a call to Allocate(ip) would fail with ErrAllocated.
func (*Range) Release ¶
Release releases the IP back to the pool. Releasing an unallocated IP or an IP out of the range is a no-op and returns no error.
func (*Range) Restore ¶
Restore restores the pool to the previously captured state. ErrMismatchedNetwork is returned if the provided prefix doesn't exactly match the previous range.