Version: v0.17.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2015 License: Apache-2.0 Imports: 6 Imported by: 0




This section is empty.


View Source
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")


func GetIndexedIP

func GetIndexedIP(subnet *net.IPNet, index int) (net.IP, error)

GetIndexedIP returns a net.IP that is subnet.IP + index in the contiguous IP space.

func RangeSize

func RangeSize(subnet *net.IPNet) int64

RangeSize returns the size of a range in valid addresses.


type Interface

type Interface interface {
	Allocate(net.IP) error
	AllocateNext() (net.IP, error)
	Release(net.IP) error

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:

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           
|                                     |
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.

func NewCIDRRange

func NewCIDRRange(cidr *net.IPNet) *Range

NewCIDRRange creates a Range over a net.IPNet.

func (*Range) Allocate

func (r *Range) Allocate(ip net.IP) error

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

func (r *Range) AllocateNext() (net.IP, error)

AllocateNext reserves one of the IPs from the pool. ErrFull may be returned if there are no addresses left.

func (*Range) Free

func (r *Range) Free() int

Free returns the count of IP addresses left in the range.

func (*Range) Has

func (r *Range) Has(ip net.IP) bool

Has returns true if the provided IP is already allocated and a call to Allocate(ip) would fail with ErrAllocated.

func (*Range) Release

func (r *Range) Release(ip net.IP) error

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

func (r *Range) Restore(net *net.IPNet, data []byte) error

Restore restores the pool to the previously captured state. ErrMismatchedNetwork is returned if the provided IPNet range doesn't exactly match the previous range.

func (*Range) Snapshot

func (r *Range) Snapshot() (*net.IPNet, []byte)

Snapshot saves the current state of the pool.

type Snapshottable

type Snapshottable interface {
	Snapshot() (*net.IPNet, []byte)
	Restore(*net.IPNet, []byte) error

Snapshottable is an Interface that can be snapshotted and restored. Snapshottable should be threadsafe.

Source Files


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL