// allocateIP attempts to allocate the requested ip, and if that is
// not possible, allocates the next available address.
11357
func (ic *IngressIPController) allocateIP(requestedIP string) (net.IP, error) {
	if len(requestedIP) == 0 {
		// Specific ip not requested
		return ic.ipAllocator.AllocateNext()
	}
	var ip net.IP
	if ip = net.ParseIP(requestedIP); ip == nil {
		// Invalid ip
		return ic.ipAllocator.AllocateNext()
	}
	if err := ic.ipAllocator.Allocate(ip); err != nil {
		// Unable to allocate requested ip
		return ic.ipAllocator.AllocateNext()
	}
	// Allocated requested ip
	return ip, nil
}
261051
func (_m *API) AssignIP(_a0 api.IPConfig) error {
	ret := _m.Called(_a0)

	var r0 error
	if rf, ok := ret.Get(0).(func(api.IPConfig) error); ok {
		r0 = rf(_a0)
	} else {
		r0 = ret.Error(0)
	}

	return r0
}
1082800
func (r *Range) Allocate(ip net.IP) error {
	ok, offset := r.contains(ip)
	if !ok {
		return &ErrNotInRange{r.net.String()}
	}

	allocated, err := r.alloc.Allocate(offset)
	if err != nil {
		return err
	}
	if !allocated {
		return ErrAllocated
	}
	return nil
}
551814
func (s *InstanceAccessDetails) SetIpAddress(v string) *InstanceAccessDetails {
	s.IpAddress = &v
	return s
}
363301
func (a *IPAllocator) RequestIP(network *net.IPNet, ip net.IP) (net.IP, error) {
	if network == nil {
		return nil, ErrBadSubnet
	}

	a.mutex.Lock()
	defer a.mutex.Unlock()

	key := network.String()
	allocated, ok := a.allocatedIPs[key]
	if !ok {
		allocated = newAllocatedMap(network)
		a.allocatedIPs[key] = allocated
	}

	if ip == nil {
		return allocated.getNextIP()
	}
	return allocated.checkIP(ip)
}
843767
func (s *UpdateHealthCheckInput) SetIPAddress(v string) *UpdateHealthCheckInput {
	s.IPAddress = &v
	return s
}
1111446
func (assign AddressAssignment) EqualIP(b AddressAssignment) bool {
	if assign.PoolID != b.PoolID {
		return false
	} else if assign.IPAddr == b.IPAddr {
		return false
	} else if assign.Port != b.Port {
		return false
	} else if assign.ServiceID != b.ServiceID {
		return false
	} else if assign.EndpointName != b.EndpointName {
		return false
	}
	return true
}
243779
func Allocate() uint16 {
	id := pool.AllocateID()

	// Out of endpoint IDs
	if id == idpool.NoID {
		return uint16(0)
	}

	return uint16(id)
}
462792
func (c *Client) IPAMAllocateIP(ip, owner string) error {
	params := ipam.NewPostIPAMIPParams().WithIP(ip).WithOwner(&owner).WithTimeout(api.ClientTimeout)
	_, err := c.IPAM.PostIPAMIP(params)
	return Hint(err)
}
1316525
func (s *AssociateResolverEndpointIpAddressInput) SetIpAddress(v *IpAddressUpdate) *AssociateResolverEndpointIpAddressInput {
	s.IpAddress = v
	return s
}
535623
func (s *AgentNetworkInfo) SetIpAddress(v string) *AgentNetworkInfo {
	s.IpAddress = &v
	return s
}
1164896
func copyIP(src net.IP) net.IP {
	return net.IP(copyBytes(src))
}
866963
func (n *NeutronNetworking) AllocatePublicIP(instId instance.Id) (*string, error) {
	extNetworkIds := make([]string, 0)
	neutronClient := n.env.neutron()
	externalNetwork := n.env.ecfg().externalNetwork()
	if externalNetwork != "" {
		// the config specified an external network, try it first.
		netId, err := resolveNeutronNetwork(neutronClient, externalNetwork, true)
		if err != nil {
			logger.Debugf("external network %s not found, search for one", externalNetwork)
		} else {
			logger.Debugf("using external network %q", externalNetwork)
			extNetworkIds = []string{netId}
		}
	}

	if len(extNetworkIds) == 0 {
		// Create slice of network.Ids for external networks in the same AZ as
		// the instance's network, to find an existing floating ip in, or allocate
		// a new floating ip from.
		network := n.env.ecfg().network()
		netId, err := resolveNeutronNetwork(neutronClient, network, false)
		netDetails, err := neutronClient.GetNetworkV2(netId)
		if err != nil {
			return nil, errors.Trace(err)
		}

		for _, az := range netDetails.AvailabilityZones {
			extNetIds, _ := getExternalNeutronNetworksByAZ(n.env, az)
			if len(extNetIds) > 0 {
				extNetworkIds = append(extNetworkIds, extNetIds...)
			}
		}

		if len(extNetworkIds) == 0 {
			return nil, errors.NewNotFound(nil, fmt.Sprintf("could not find an external network in availability zone %s", netDetails.AvailabilityZones))
		}
	}

	// Look for FIPs in same project as the credentials.
	// Admins have visibility into other projects.
	fips, err := n.env.neutron().ListFloatingIPsV2(projectIdFilter(n.env.client().TenantId()))
	if err != nil {
		return nil, errors.Trace(err)
	}

	// Is there an unused FloatingIP on an external network in the instance's availability zone?
	for _, fip := range fips {
		if fip.FixedIP == "" {
			// Not a perfect solution.  If an external network was specified in the
			// config, it'll be at the top of the extNetworkIds, but may be not used
			// if the available FIP isn't it in.  However the instance and the
			// FIP will be in the same availability zone.
			for _, extNetId := range extNetworkIds {
				if fip.FloatingNetworkId == extNetId {
					logger.Debugf("found unassigned public ip: %v", fip.IP)
					return &fip.IP, nil
				}
			}
		}
	}

	// allocate a new IP and use it
	var lastErr error
	for _, extNetId := range extNetworkIds {
		var newfip *neutron.FloatingIPV2
		newfip, lastErr = neutronClient.AllocateFloatingIPV2(extNetId)
		if lastErr == nil {
			logger.Debugf("allocated new public IP: %s", newfip.IP)
			return &newfip.IP, nil
		}
	}

	logger.Debugf("Unable to allocate a public IP")
	return nil, lastErr
}
821307
func (a *IPRangeAllocator) Allocate() net.IP {
	a.mutex.Lock()
	defer a.mutex.Unlock()

	// ensure we have some IPs first
	if a.remaining <= 0 {
		return nil
	}

	// get a random number within the size to start with
	idx := rand.Int63n(a.size)

	// find the next available index after the random number that is available
	idx = a.findNextAvailbleIndex(idx)

	// if idx is now -1, then it couldn't find one, which is very unlikely,
	// however if that is, treat it as no more remaining and ensure remaining is
	// now 0
	if idx == -1 {
		a.remaining = 0
		return nil
	}

	// reserve the idx, get the IP bytes
	a.reserved[idx] = true
	a.remaining--
	newBig := big.NewInt(0).Add(a.startBig, big.NewInt(idx))

	return a.bigIntToIP(newBig)
}
405207
func ListAllocatedIPs(allocMap bitset.BitSet, ipPool string, subnetIP string, subnetLen uint) string {
	idx := uint(0)
	startIdx := idx
	list := []string{}
	inRange := false

	ClearReservedEntries(&allocMap, subnetLen)
	ClearBitsOutsideRange(&allocMap, ipPool, subnetLen)
	for {
		foundValue, found := allocMap.NextSet(idx)
		if !found {
			break
		}

		if !inRange { // begin of range
			startIdx = foundValue
			inRange = true
		} else if foundValue > idx { // end of range
			thisRange := getIPRange(subnetIP, subnetLen, startIdx, idx-1)
			list = append(list, thisRange)
			startIdx = foundValue
		}
		idx = foundValue + 1
	}

	// list end with allocated value
	if inRange {
		thisRange := getIPRange(subnetIP, subnetLen, startIdx, idx-1)
		list = append(list, thisRange)
	}

	return strings.Join(list, ", ")
}
27517
func ListAvailableIPs(allocMap bitset.BitSet, subnetIP string, subnetLen uint) string {
	idx := uint(0)
	startIdx := idx
	list := []string{}
	inRange := false

	for {
		foundValue, found := NextClear(allocMap, idx, subnetLen)
		if !found {
			break
		}

		if !inRange { // begin of range
			startIdx = foundValue
			inRange = true
		} else if foundValue > idx { // end of range
			thisRange := getIPRange(subnetIP, subnetLen, startIdx, idx-1)
			list = append(list, thisRange)
			startIdx = foundValue
		}
		idx = foundValue + 1
	}

	// list end with allocated value
	if inRange {
		thisRange := getIPRange(subnetIP, subnetLen, startIdx, idx-1)
		list = append(list, thisRange)
	}

	return strings.Join(list, ", ")
}
1019672
func (ipam *IPAM) AllocateIPString(ipAddr, owner string) error {
	ip := net.ParseIP(ipAddr)
	if ip == nil {
		return fmt.Errorf("Invalid IP address: %s", ipAddr)
	}

	return ipam.AllocateIP(ip, owner)
}