// BindIP creates a virtual ip if it doesn't already exist.
490954
func (a *HostAgent) BindIP(ipprefix, netmask, iface string) error {
	logger := plog.WithFields(logrus.Fields{
		"ipaddress": ipprefix,
		"netmask":   netmask,
		"interface": iface,
	})

	// calculate the cidr bytes
	cidr, _ := net.IPMask(net.ParseIP(netmask).To4()).Size()
	ip := fmt.Sprintf("%s/%d", ipprefix, cidr)

	if vip := a.vip.Find(ipprefix); vip != nil {
		// return if the ips match
		if !vip.Matches(ip, iface) {
			if err := a.vip.Release(vip.Addr, vip.Device); err != nil {
				logger.WithError(err).Error("Could not release virtual ip for update")
				return err
			}
		} else {
			return nil
		}
	}

	// check the interface
	if _, err := net.InterfaceByName(iface); err != nil {
		logger.WithError(err).Error("Could not look up interface to bind virtual ip")
		return err
	}

	// bind the virtual ip
	if err := a.vip.Bind(ip, iface); err != nil {
		logger.WithError(err).Error("Could not bind virtual ip")
		return err
	}

	return nil
}
627411
func (s *GetDedicatedIpInput) SetIp(v string) *GetDedicatedIpInput {
	s.Ip = &v
	return s
}
46480
func getPortBinding(ip string, port string) []docker.PortBinding {
	return []docker.PortBinding{{HostIP: "", HostPort: port}}
}
221343
func (p *PerHost) AddIP(ip net.IP) {
	p.bypassIPs = append(p.bypassIPs, ip)
}
998047
func IsIP(ip string) bool {
	if m, _ := regexp.MatchString(ipRegexp, ip); !m {
		return false
	}

	return true
}
136560
func remoteIP(req *http.Request) string {
	realIP := req.Header.Get("X-Real-IP")
	if realIP != "" {
		return realIP
	}
	forwardedIPs := req.Header.Get("X-Forwarded-For")
	if forwardedIPs != "" {
		ips := strings.Split(forwardedIPs, ", ")
		return ips[0]
	}
	return req.RemoteAddr
}
829129
func (s *PutDedicatedIpWarmupAttributesInput) SetIp(v string) *PutDedicatedIpWarmupAttributesInput {
	s.Ip = &v
	return s
}
350825
func (ctx *Context) IP(trustedProxy ...bool) net.IP {
	trusted := ctx.Setting(SetTrustedProxy).(bool)
	if len(trustedProxy) > 0 {
		trusted = trustedProxy[0]
	}

	if trusted {
		ip := ctx.Req.Header.Get(HeaderXForwardedFor)
		if ip == "" {
			ip = ctx.Req.Header.Get(HeaderXRealIP)
		} else if i := strings.IndexByte(ip, ','); i >= 0 {
			ip = ip[0:i]
		}

		if realIP := net.ParseIP(ip); realIP != nil {
			return realIP
		}
	}

	ra := ctx.Req.RemoteAddr
	ra, _, _ = net.SplitHostPort(ra)
	return net.ParseIP(ra)
}
435255
func getOurIP() (ip string, err error) {
	hostname, err := os.Hostname()
	if err != nil {
		return
	}
	return net.LookupCNAME(hostname)
}
1041540
func lastIP(n *net.IPNet) net.IP {
	mask := n.Mask
	ip := n.IP
	lastip := make(net.IP, len(ip))
	// set bits zero in the mask to ones in ip
	for i, m := range mask {
		lastip[i] = (^m) | ip[i]
	}
	return lastip
}
1299723
func loopbackIP() net.IP {
	interfaces, err := net.Interfaces()
	if err != nil {
		return nil
	}
	for _, inf := range interfaces {
		const flagUpLoopback = net.FlagUp | net.FlagLoopback
		if inf.Flags&flagUpLoopback == flagUpLoopback {
			addrs, _ := inf.Addrs()
			for _, addr := range addrs {
				ip, _, err := net.ParseCIDR(addr.String())
				if err == nil && ip.IsLoopback() {
					return ip
				}
			}
		}
	}
	return nil
}
343588
func (s *IpAddressRequest) SetIp(v string) *IpAddressRequest {
	s.Ip = &v
	return s
}
1024638
func RealIP(req *http.Request) string {
	ip, ok := req.Context().Value(realIPKey).(string)
	if !ok {
		// Fallback to a secure resolver.
		return DefaultResolver.RealIP(req)
	}
	return ip
}
189685
func GetIP(r *http.Request, options ...Options) net.IP {
	if len(options) >= 1 && options[0].TrustForwardHeader {
		ip := r.Header.Get("X-Forwarded-For")
		if ip != "" {
			parts := strings.SplitN(ip, ",", 2)
			part := strings.TrimSpace(parts[0])
			return net.ParseIP(part)
		}

		ip = strings.TrimSpace(r.Header.Get("X-Real-IP"))
		if ip != "" {
			return net.ParseIP(ip)
		}
	}

	remoteAddr := strings.TrimSpace(r.RemoteAddr)
	host, _, err := net.SplitHostPort(remoteAddr)
	if err != nil {
		return net.ParseIP(remoteAddr)
	}

	return net.ParseIP(host)
}
1079947
func (c *Client) AttachReservedIP(ip string, serverID string) error {
	values := url.Values{
		"ip_address":   {ip},
		"attach_SUBID": {serverID},
	}
	return c.post(`reservedip/attach`, values, nil)
}
1253482
func supportedIP(ip net.IP) net.IP {
	if supportsIPv4 {
		if v4 := ip.To4(); v4 != nil {
			return v4
		}
	}
	if supportsIPv6 && len(ip) == net.IPv6len {
		return ip
	}
	return nil
}
723389
func getIP(showAddress string) string {
	list := strings.SplitN(showAddress, ":", 2)
	if len(list) != 2 {
		return ""
	}
	return strings.TrimSpace(list[1])
}