예제 #1
0
func newAllocatedMap(network *net.IPNet) *allocatedMap {
	firstIP, lastIP := netutils.NetworkRange(network)
	begin := big.NewInt(0).Add(ipToBigInt(firstIP), big.NewInt(1))
	end := big.NewInt(0).Sub(ipToBigInt(lastIP), big.NewInt(1))

	return &allocatedMap{
		p:     make(map[string]struct{}),
		begin: begin,
		end:   end,
		last:  big.NewInt(0).Sub(begin, big.NewInt(1)), // so first allocated will be begin
	}
}
예제 #2
0
// RegisterSubnet registers network in global allocator with bounds
// defined by subnet. If you want to use network range you must call
// this method before first RequestIP, otherwise full network range will be used
func (a *IPAllocator) RegisterSubnet(network *net.IPNet, subnet *net.IPNet) error {
	a.mutex.Lock()
	defer a.mutex.Unlock()

	nw := &net.IPNet{IP: network.IP.Mask(network.Mask), Mask: network.Mask}
	key := nw.String()
	if _, ok := a.allocatedIPs[key]; ok {
		return ErrNetworkAlreadyRegistered
	}

	// Check that subnet is within network
	beginIP, endIP := netutils.NetworkRange(subnet)
	if !(network.Contains(beginIP) && network.Contains(endIP)) {
		return ErrBadSubnet
	}

	n := newAllocatedMap(subnet)
	a.allocatedIPs[key] = n
	return nil
}