// MakeSurrogateIP takes an IP like 127.0.0.1 and munges it to 0.0.0.1 so we can
// sort it more easily. Note that we don't actually want to change the value,
// so we use value copies here (not pointers).
163833
func MakeSurrogateIP(IP net.IP) net.IP {
	if len(IP.String()) > 3 && IP.String()[0:3] == "127" {
		return net.ParseIP("0" + IP.String()[3:])
	}
	return IP
}
829129
func (s *PutDedicatedIpWarmupAttributesInput) SetIp(v string) *PutDedicatedIpWarmupAttributesInput {
	s.Ip = &v
	return s
}
921463
func (s *DedicatedIp) SetIp(v string) *DedicatedIp {
	s.Ip = &v
	return s
}
197158
func (s *clientIOSession) RemoteIP() string {
	addr := s.RemoteAddr()
	if addr == "" {
		return ""
	}

	return strings.Split(addr, ":")[0]
}
295910
func (api *SIMAPI) AssignIP(id int64, ip string) (bool, error) {
	var (
		method = "PUT"
		uri    = fmt.Sprintf("%s/%d/sim/ip", api.getResourceURL(), id)
	)

	return api.modify(method, uri, map[string]interface{}{
		"sim": map[string]interface{}{
			"ip": ip,
		},
	})
}
901598
func (s *RegisterInstanceInput) SetPrivateIp(v string) *RegisterInstanceInput {
	s.PrivateIp = &v
	return s
}
574501
func (b IPv4) SetSourceAddress(addr net.IP) {
	copy(b[srcAddr:srcAddr+IPv4AddressSize], addr.To4())
}
523742
func GetOriginalIP(r *http.Request) string {
	x := r.Header.Get("X-Forwarded-For")
	if x != "" && strings.Contains(r.RemoteAddr, "127.0.0.1") {
		// format is comma separated
		return strings.Split(x, ",")[0]
	}
	// it also contains the port
	return strings.Split(r.RemoteAddr, ":")[0]
}
1164415
func (r *Request) RealIP() string {
	if len(r.realIP) > 0 {
		return r.realIP
	}
	r.realIP = r.RemoteAddress()
	if ip := r.header.Get(echo.HeaderXForwardedFor); len(ip) > 0 {
		ip = strings.TrimSpace(strings.SplitN(ip, ",", 2)[0])
		r.realIP = ip
	} else if ip := r.header.Get(echo.HeaderXRealIP); len(ip) > 0 {
		r.realIP = ip
	} else {
		r.realIP, _, _ = net.SplitHostPort(r.realIP)
	}
	return r.realIP
}
1027551
func RemoteAddr(r *http.Request) string {
	if prior := r.Header.Get("X-Forwarded-For"); prior != "" {
		proxies := strings.Split(prior, ",")
		if len(proxies) > 0 {
			remoteAddr := strings.Trim(proxies[0], " ")
			if parseIP(remoteAddr) != nil {
				return remoteAddr
			}
		}
	}
	// X-Real-Ip is less supported, but worth checking in the
	// absence of X-Forwarded-For
	if realIP := r.Header.Get("X-Real-Ip"); realIP != "" {
		if parseIP(realIP) != nil {
			return realIP
		}
	}

	return r.RemoteAddr
}
39968
func RemoteIP(ipLookups []string, r engine.Request) string {

	for _, lookup := range ipLookups {
		if lookup == "RemoteAddr" {
			return ipAddrFromRemoteAddr(r.RemoteAddress())
		}
		if lookup == "X-Forwarded-For" {
			if forwardedFor := r.Header().Get("X-Forwarded-For"); len(forwardedFor) > 0 {
				// X-Forwarded-For is potentially a list of addresses separated with ","
				return strings.TrimSpace(strings.SplitN(forwardedFor, ",", 2)[0])
			}
		}
		if lookup == "X-Real-IP" {
			if realIP := r.Header().Get("X-Real-IP"); len(realIP) > 0 {
				return realIP
			}
		}
	}

	return ""
}
1158359
func (r *Resolver) RealIP(req *http.Request) string {
	var hdrRealIP string
	if r.XRealIp {
		hdrRealIP = req.Header.Get("X-Real-Ip")
	}

	var hdrForwardedFor string
	if r.XForwardedFor {
		hdrForwardedFor = req.Header.Get("X-Forwarded-For")
	}

	if len(hdrForwardedFor) == 0 && len(hdrRealIP) == 0 {
		return ipAddrFromRemoteAddr(req.RemoteAddr)
	}

	// X-Forwarded-For is potentially a list of addresses separated with ","
	forwarded := sort.StringSlice(strings.Split(hdrForwardedFor, ","))

	// This will ignore the X-Forwarded-For entries matching a load balancer
	// and use the first (from right to left) untrused address as the real
	// ip. This is done to prevent spoofing X-Forwarded-For.
	//
	// For example, let's say you wanted try to spoof your ip to make it
	// look like a request came from an office ip (204.28.121.211). You
	// would make a request like this:
	//
	//   curl https://www.example.com/debug -H "X-Forwarded-For: 204.28.121.211"
	//
	// The load balancer would then tag on the connecting ip, as well as the
	// ip address of the load balancer itself. The application would receive
	// an X-Forwarded-For header like the following:
	//
	//   "X-Forwarded-For": [
	//     "204.28.121.211, 49.228.250.246, 10.128.21.180"
	//   ]
	//
	// This will look at each ip from right to left, and use the first
	// "untrusted" address as the real ip.
	//
	// 1. The first ip, 10.128.21.180, is the loadbalancer ip address and is
	//    considered trusted because it's a LAN cidr.
	// 2. The second ip, 49.228.250.246, is untrusted, so this is determined to
	//    be the real ip address.
	//
	// By doing this, the spoofed ip (204.28.121.211) is ignored.
	reverse(forwarded)

	for _, addr := range forwarded {
		// return first non-local address
		addr = strings.TrimSpace(addr)
		if len(addr) > 0 && !isLocalAddress(addr) {
			return addr
		}
	}

	return hdrRealIP
}
476666
func (c *Client) CreateReservedIP(regionID int, ipType string, label string) (string, error) {
	values := url.Values{
		"DCID":    {fmt.Sprintf("%v", regionID)},
		"ip_type": {ipType},
	}
	if len(label) > 0 {
		values.Add("label", label)
	}

	result := IP{}
	err := c.post(`reservedip/create`, values, &result)
	if err != nil {
		return "", err
	}
	return result.ID, nil
}
627411
func (s *GetDedicatedIpInput) SetIp(v string) *GetDedicatedIpInput {
	s.Ip = &v
	return s
}
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
}
903859
func (c *Ctx) ClientIP() (clientIP string) {

	var values []string

	if values, _ = c.request.Header[XRealIP]; len(values) > 0 {

		clientIP = strings.TrimSpace(values[0])
		if clientIP != blank {
			return
		}
	}

	if values, _ = c.request.Header[XForwardedFor]; len(values) > 0 {
		clientIP = values[0]

		if index := strings.IndexByte(clientIP, ','); index >= 0 {
			clientIP = clientIP[0:index]
		}

		clientIP = strings.TrimSpace(clientIP)
		if clientIP != blank {
			return
		}
	}

	clientIP, _, _ = net.SplitHostPort(strings.TrimSpace(c.request.RemoteAddr))

	return
}
439619
func (s *IpAddressResponse) SetIp(v string) *IpAddressResponse {
	s.Ip = &v
	return s
}