// yieldIP is used to iteratively invoke the callback with
// an available IP
556640
func (idx *NetworkIndex) yieldIP(cb func(net *NetworkResource, ip net.IP) bool) {
	inc := func(ip net.IP) {
		for j := len(ip) - 1; j >= 0; j-- {
			ip[j]++
			if ip[j] > 0 {
				break
			}
		}
	}

	for _, n := range idx.AvailNetworks {
		ip, ipnet, err := net.ParseCIDR(n.CIDR)
		if err != nil {
			continue
		}
		for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
			if cb(n, ip) {
				return
			}
		}
	}
}
980600
func (a *api) SetIP(config IPConfig) error {
	client, err := a.connectMaster()
	if err != nil {
		return err
	}

	req := addressassignment.AssignmentRequest{
		ServiceID:      config.ServiceID,
		IPAddress:      config.IPAddress,
		AutoAssignment: config.IPAddress == "",
		Port:		config.Port,
		Proto:		config.Proto,
		EndpointName:	config.EndpointName,
	}

	if err := client.SetIPs(req); err != nil {
		return err
	}

	return nil
}
1060958
func isIP(fl FieldLevel) bool {

	ip := net.ParseIP(fl.Field().String())

	return ip != nil
}
343588
func (s *IpAddressRequest) SetIp(v string) *IpAddressRequest {
	s.Ip = &v
	return s
}
808822
func forceIP(urlstr string) string {
	if i := strings.Index(urlstr, "://"); i != -1 {
		scheme := urlstr[:i+3]
		host, port, path := urlstr[len(scheme)+3:], "", ""
		if i := strings.Index(host, "/"); i != -1 {
			host, path = host[:i], host[i:]
		}
		if i := strings.Index(host, ":"); i != -1 {
			host, port = host[:i], host[i:]
		}
		if addr, err := net.ResolveIPAddr("ip", host); err == nil {
			urlstr = scheme + addr.IP.String() + port + path
		}
	}
	return urlstr
}
362129
func (ipam *IPAM) ReleaseIP(ip net.IP) error {
	ipam.allocatorMutex.Lock()
	defer ipam.allocatorMutex.Unlock()
	family := familyIPv4
	if ip.To4() != nil {
		if ipam.IPv4Allocator == nil {
			return ErrIPv4Disabled
		}

		if err := ipam.IPv4Allocator.Release(ip); err != nil {
			return err
		}
	} else {
		family = familyIPv6
		if ipam.IPv6Allocator == nil {
			return ErrIPv6Disabled
		}

		if err := ipam.IPv6Allocator.Release(ip); err != nil {
			return err
		}
	}

	owner := ipam.owner[ip.String()]
	log.WithFields(logrus.Fields{
		"ip":    ip.String(),
		"owner": owner,
	}).Debugf("Released IP")
	delete(ipam.owner, ip.String())

	metrics.IpamEvent.WithLabelValues(metricRelease, family).Inc()
	return nil
}
601339
func (s *ElasticIp) SetIp(v string) *ElasticIp {
	s.Ip = &v
	return s
}
197158
func (s *clientIOSession) RemoteIP() string {
	addr := s.RemoteAddr()
	if addr == "" {
		return ""
	}

	return strings.Split(addr, ":")[0]
}
1325572
func (u *UPnP) ExternalIP() (net.IP, error) {
	ip, err := u.device.ExternalIP()
	if err != nil {
		return nil, err
	}

	if isPrivateIP(net.ParseIP(ip)) {
		return nil, ErrMultipleNAT
	}

	return net.ParseIP(ip), nil
}
1246502
func ReleaseIP(addr net.IP, subnet net.IPNet, VNI string) bool {
	oldArray, _, ok := netAgent.Get(ipStore, VNI+"-"+subnet.String())

	if !ok {
		return false
	}

	newArray := make([]byte, len(oldArray))
	copy(newArray, oldArray)

	var num1, num2 uint32

	buf1 := bytes.NewBuffer(addr.To4())
	binary.Read(buf1, binary.BigEndian, &num1)

	buf := bytes.NewBuffer(subnet.IP)

	binary.Read(buf, binary.BigEndian, &num2)

	pos := uint(num1 - num2 - 1)

	util.Clear(newArray, pos)

	err2 := netAgent.Put(ipStore, VNI+"-"+subnet.String(), newArray, oldArray)

	if err2 == netAgent.OUTDATED {
		return ReleaseIP(addr, subnet, VNI)
	}

	return true
}
215725
func (s *TargetAddress) SetIp(v string) *TargetAddress {
	s.Ip = &v
	return s
}
1036198
func (c Context) ServerIP() string {
	addrs, err := networkInterfacesFn()
	if err != nil {
		return ""
	}

	for _, address := range addrs {
		// Validate the address and check if it's not a loopback
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil || ipnet.IP.To16() != nil {
				return ipnet.IP.String()
			}
		}
	}

	return ""
}
280968
func bigIntToIP(v *big.Int) net.IP {
	return net.IP(v.Bytes())
}
758667
func (r *DNSResolver) MatchIP(name string, matcher IPMatcherFunc) (bool, error) {
	ips, err := net.LookupIP(name)
	err = errDNS(err)
	if err != nil {
		return false, err
	}
	for _, ip := range ips {
		if m, e := matcher(ip); m || e != nil {
			return m, e
		}
	}
	return false, nil
}
821074
func (ctx *Context) ClientIP() string {
	clientIP := strings.TrimSpace(ctx.requestHeader("X-Real-Ip"))
	if len(clientIP) > 0 {
		return clientIP
	}
	clientIP = ctx.requestHeader("X-Forwarded-For")
	if index := strings.IndexByte(clientIP, ','); index >= 0 {
		clientIP = clientIP[0:index]
	}
	clientIP = strings.TrimSpace(clientIP)
	if len(clientIP) > 0 {
		return clientIP
	}
	if ip, _, err := net.SplitHostPort(strings.TrimSpace(ctx.Request.RemoteAddr)); err == nil {
		return ip
	}
	return ""
}
1005985
func DefaultLocalIP4() (net.IP, error) {
	devices, err := net.Interfaces()
	if err != nil {
		return nil, err
	}
	for _, dev := range devices {
		if (dev.Flags&net.FlagUp != 0) && (dev.Flags&net.FlagLoopback == 0) {
			addrs, err := dev.Addrs()
			if err != nil {
				continue
			}
			for i := range addrs {
				if ip, ok := addrs[i].(*net.IPNet); ok {
					if ip.IP.To4() != nil {
						return ip.IP, nil
					}
				}
			}
		}
	}
	return nil, ErrorNoDefaultIP
}
435255
func getOurIP() (ip string, err error) {
	hostname, err := os.Hostname()
	if err != nil {
		return
	}
	return net.LookupCNAME(hostname)
}