// CheckIPInRange check input `ip` is between `start` and `end` IP range
179100
func CheckIPInRange(ip string, start net.IP, end net.IP) bool {
	//sanity check
	input := net.ParseIP(ip)
	if input.To4() == nil {
		log.Printf("%v is not a valid IPv4 address", input)
		return false
	}

	if bytes.Compare(input, start) >= 0 && bytes.Compare(input, end) <= 0 {
		return true
	}

	log.Printf("%v is NOT between %v and %v", input, start, end)
	return false
}
610415
func sortedIPsAreEqual(a, b []net.IP) bool {
	// the IP set is definitely different if the lengths are different
	if len(a) != len(b) {
		return false
	}

	// lengths are equal, so each member in one set must be in the other
	// Note: we sorted fullNewIPs above, and sorted oldIPs when they were
	// inserted in this function, previously.
	// If any IPs at the same index differ, updated = true.
	for i := range a {
		if !a[i].Equal(b[i]) {
			return false
		}
	}
	return true
}
1235452
func IPLockChecker() (string, checkers.Func) {
	return "ipaddr", func(ctx context.Context, cond, arg string) error {
		// Get peer info and extract IP address from it for macaroon
		// check.
		pr, ok := peer.FromContext(ctx)
		if !ok {
			return fmt.Errorf("unable to get peer info from context")
		}
		peerAddr, _, err := net.SplitHostPort(pr.Addr.String())
		if err != nil {
			return fmt.Errorf("unable to parse peer address")
		}

		if !net.ParseIP(arg).Equal(net.ParseIP(peerAddr)) {
			msg := "macaroon locked to different IP address"
			return fmt.Errorf(msg)
		}
		return nil
	}
}
1053351
func (tf tFiles) getRange(icmp *iComparer) (imin, imax internalKey) {
	for i, t := range tf {
		if i == 0 {
			imin, imax = t.imin, t.imax
			continue
		}
		if icmp.Compare(t.imin, imin) < 0 {
			imin = t.imin
		}
		if icmp.Compare(t.imax, imax) > 0 {
			imax = t.imax
		}
	}

	return
}
311444
func ClearIPAddrRange(ipAllocMap *bitset.BitSet, ipPool string, nwSubnetIP string, nwSubnetLen uint) error {

	addrRangeList := strings.Split(ipPool, "-")

	hostMin, err := GetIPNumber(nwSubnetIP, nwSubnetLen, 32, addrRangeList[0])
	if err != nil {
		log.Errorf("Error parsing first address %s. Err: %v", addrRangeList[0], err)
		return err
	}

	hostMax, err := GetIPNumber(nwSubnetIP, nwSubnetLen, 32, addrRangeList[1])
	if err != nil {
		log.Errorf("Error parsing last address %s. Err: %v", addrRangeList[1], err)
		return err
	}

	// Clear a range
	for i := hostMin; i <= hostMax; i++ {
		ipAllocMap.Clear(uint(i))
	}

	return nil
}
270300
func (c *Client) GetRange(key string, start, end int) (string, error) {
	v, err := c.execWithKey(true, "GETRANGE", key, start, end)
	if err != nil {
		return "", err
	}
	switch v.(type) {
	case string:
		return v.(string), nil
	}
	return "", ErrServerError
}
1214236
func (bc *bitmapContainer) iaddRange(firstOfRange, lastOfRange int) container {
	bc.cardinality += setBitmapRangeAndCardinalityChange(bc.bitmap, firstOfRange, lastOfRange)
	return bc
}
834344
func LongitudeInRange(min, max float64) (float64, error) {
	if min > max || min < -180 || min > 180 || max < -180 || max > 180 {
		return 0, errors.New("input range is invalid")
	}
	return randFloat64Range(min, max), nil
}
718875
func (s *WebhookAuthConfiguration) SetAllowedIPRange(v string) *WebhookAuthConfiguration {
	s.AllowedIPRange = &v
	return s
}
638795
func HourInRange(t time.Time, minHour, maxHour int) bool {
	return (minHour <= t.Hour()) && (t.Hour() <= maxHour)
}
480211
func IPv4Range(block string) (IPv4, int64, error) {
	ip, subnet, err := net.ParseCIDR(block)
	if err != nil {
		return 0, 0, errors.Reason("invalid CIDR block %q", block).Err()
	}
	ipv4 := ip.Mask(subnet.Mask).To4()
	if ipv4 == nil {
		return 0, 0, errors.Reason("invalid IPv4 CIDR block %q", block).Err()
	}
	ones, _ := subnet.Mask.Size()
	return IPv4(binary.BigEndian.Uint32(ipv4)), 1 << uint32(32-ones), nil
}
1046348
func (s *DBSecurityGroup) SetIPRanges(v []*IPRange) *DBSecurityGroup {
	s.IPRanges = v
	return s
}
680806
func StatusRange(start, end int) *Mux {
	return Match(func(ctx *c.Context) bool {
		return ctx.GetString("$phase") == "response" && ctx.Response.StatusCode >= start && ctx.Response.StatusCode <= end
	})
}
705994
func (hc *DNSChecker) Check(timeout time.Duration) *Result {
	if !strings.HasSuffix(hc.Question.Name, ".") {
		hc.Question.Name += "."
	}

	msg := fmt.Sprintf("DNS %s query to port %d", questionToString(hc.Question), hc.Port)
	start := time.Now()
	if timeout == time.Duration(0) {
		timeout = defaultDNSTimeout
	}
	deadline := start.Add(timeout)

	var aIP net.IP
	switch hc.Question.Qtype {
	case dns.TypeA:
		if aIP = net.ParseIP(hc.Answer); aIP == nil || aIP.To4() == nil {
			msg = fmt.Sprintf("%s; %q is not a valid IPv4 address", msg, hc.Answer)
			return complete(start, msg, false, nil)
		}
	case dns.TypeAAAA:
		if aIP = net.ParseIP(hc.Answer); aIP == nil {
			msg = fmt.Sprintf("%s; %q is not a valid IPv6 address", msg, hc.Answer)
			return complete(start, msg, false, nil)
		}
	}

	// Build DNS query.
	q := &dns.Msg{
		MsgHdr: dns.MsgHdr{
			Id:               dns.Id(),
			RecursionDesired: true,
		},
		Question: []dns.Question{hc.Question},
	}

	// TODO(mharo): don't assume UDP
	conn, err := dialUDP(hc.network(), hc.addr(), timeout, hc.Mark)
	if err != nil {
		return complete(start, msg, false, err)
	}
	defer conn.Close()

	err = conn.SetDeadline(deadline)
	if err != nil {
		msg = fmt.Sprintf("%s; failed to set deadline", msg)
		return complete(start, msg, false, err)
	}

	dnsConn := &dns.Conn{Conn: conn}
	if err := dnsConn.WriteMsg(q); err != nil {
		msg = fmt.Sprintf("%s; failed to send request", msg)
		return complete(start, msg, false, err)
	}

	r, err := dnsConn.ReadMsg()
	if err != nil {
		msg = fmt.Sprintf("%s; failed to read response", msg)
		return complete(start, msg, false, err)
	}

	// Check reply.
	if !r.Response {
		msg = fmt.Sprintf("%s; not a query response", msg)
		return complete(start, msg, false, nil)
	}
	if rc := r.Rcode; rc != dns.RcodeSuccess {
		msg = fmt.Sprintf("%s; non-zero response code - %s", msg, rc)
		return complete(start, msg, false, nil)
	}
	if len(r.Answer) < 1 {
		msg = fmt.Sprintf("%s; no answers received for query %s", msg, questionToString(hc.Question))
		return complete(start, msg, false, nil)
	}

	// TODO(jsing): Compare query to original?
	// if q.Question != r.Question ...

	for _, rr := range r.Answer {
		if rr.Header().Name != hc.Question.Name {
			continue
		}
		if rr.Header().Class != hc.Question.Qclass {
			continue
		}
		if rr.Header().Rrtype != hc.Question.Qtype {
			// TODO(jsing): Follow CNAMEs for A/AAAA queries.
			continue
		}

		switch rr := rr.(type) {
		case *dns.A:
			if aIP.Equal(rr.A) {
				msg = fmt.Sprintf("%s; received answer %s", msg, rr.A)
				return complete(start, msg, true, err)
			}
		case *dns.AAAA:
			if aIP.Equal(rr.AAAA) {
				msg = fmt.Sprintf("%s; received answer %s", msg, rr.AAAA)
				return complete(start, msg, true, err)
			}
		case *dns.CNAME:
			// TODO(jsing): Implement comparision.
		case *dns.NS:
			// TODO(jsing): Implement comparision.
		case *dns.SOA:
			// TODO(jsing): Implement comparision.
		}
	}

	msg = fmt.Sprintf("%s; failed to match answer", msg)
	return complete(start, msg, false, err)
}
373454
func (hc *PingChecker) Check(timeout time.Duration) *Result {
	msg := fmt.Sprintf("ICMP ping to host %v", hc.IP)
	seq := hc.Seqnum
	hc.Seqnum++
	echo := newICMPEchoRequest(hc.Proto, hc.ID, seq, 64, []byte("Healthcheck"))
	start := time.Now()
	if timeout == time.Duration(0) {
		timeout = defaultPingTimeout
	}
	err := exchangeICMPEcho(hc.network(), hc.IP, timeout, echo)
	success := err == nil
	return complete(start, msg, success, err)
}
174176
func InRange(value interface{}, left interface{}, right interface{}) bool {

	reflectValue := reflect.TypeOf(value).Kind()
	reflectLeft := reflect.TypeOf(left).Kind()
	reflectRight := reflect.TypeOf(right).Kind()

	if reflectValue == reflect.Int && reflectLeft == reflect.Int && reflectRight == reflect.Int {
		return InRangeInt(value.(int), left.(int), right.(int))
	} else if reflectValue == reflect.Float32 && reflectLeft == reflect.Float32 && reflectRight == reflect.Float32 {
		return InRangeFloat32(value.(float32), left.(float32), right.(float32))
	} else if reflectValue == reflect.Float64 && reflectLeft == reflect.Float64 && reflectRight == reflect.Float64 {
		return InRangeFloat64(value.(float64), left.(float64), right.(float64))
	} else {
		return false
	}
}
22207
func (subnet HTTPProxyCIDRCheck) Check() (warnings, errorList []error) {
	klog.V(1).Infoln("validating http connectivity to first IP address in the CIDR")
	if len(subnet.CIDR) == 0 {
		return nil, nil
	}

	_, cidr, err := net.ParseCIDR(subnet.CIDR)
	if err != nil {
		return nil, []error{errors.Wrapf(err, "error parsing CIDR %q", subnet.CIDR)}
	}

	testIP, err := ipallocator.GetIndexedIP(cidr, 1)
	if err != nil {
		return nil, []error{errors.Wrapf(err, "unable to get first IP address from the given CIDR (%s)", cidr.String())}
	}

	testIPstring := testIP.String()
	if len(testIP) == net.IPv6len {
		testIPstring = fmt.Sprintf("[%s]:1234", testIP)
	}
	url := fmt.Sprintf("%s://%s/", subnet.Proto, testIPstring)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, []error{err}
	}

	// Utilize same transport defaults as it will be used by API server
	proxy, err := netutil.SetOldTransportDefaults(&http.Transport{}).Proxy(req)
	if err != nil {
		return nil, []error{err}
	}
	if proxy != nil {
		return []error{errors.Errorf("connection to %q uses proxy %q. This may lead to malfunctional cluster setup. Make sure that Pod and Services IP ranges specified correctly as exceptions in proxy configuration", subnet.CIDR, proxy)}, nil
	}
	return nil, nil
}