// ISP takes an IP address as a net.IP struct and returns a ISP struct and/or
// an error
14546
func (r *Reader) ISP(ipAddress net.IP) (*ISP, error) {
	if isISP&r.databaseType == 0 {
		return nil, InvalidMethodError{"ISP", r.Metadata().DatabaseType}
	}
	var val ISP
	err := r.mmdbReader.Lookup(ipAddress, &val)
	return &val, err
}
1175973
func firstIPv4Addr(addrs []net.Addr) (net.IP, error) {
	for _, a := range addrs {
		if a.Network() != "ip+net" {
			continue
		}

		ip, _, err := net.ParseCIDR(a.String())
		if err != nil {
			return nil, err
		}

		// "If ip is not an IPv4 address, To4 returns nil."
		// Reference: http://golang.org/pkg/net/#IP.To4
		if ip4 := ip.To4(); ip4 != nil {
			return ip4, nil
		}
	}

	return nil, nil
}
682620
func (n *Node) IP() net.IP {
	var ip net.IP
	n.Load((*enr.IP)(&ip))
	return ip
}
83245
func isIPV4(v interface{}) bool {
	s, ok := v.(string)
	if !ok {
		return false
	}
	groups := strings.Split(s, ".")
	if len(groups) != 4 {
		return false
	}
	for _, group := range groups {
		n, err := strconv.Atoi(group)
		if err != nil {
			return false
		}
		if n < 0 || n > 255 {
			return false
		}
	}
	return true
}
280179
func (internet Internet) IPv4(v reflect.Value) (interface{}, error) {
	return internet.ipv4(), nil
}
273757
func (a AddrSpec) Address() string {
	if 0 != len(a.IP) {
		return net.JoinHostPort(a.IP.String(), strconv.Itoa(a.Port))
	}
	return net.JoinHostPort(a.FQDN, strconv.Itoa(a.Port))
}
15276
func (h *HTTPBin) IP(w http.ResponseWriter, r *http.Request) {
	body, _ := json.Marshal(&ipResponse{
		Origin: getOrigin(r),
	})
	writeJSON(w, body, http.StatusOK)
}
1252895
func (c *Context) IP() net.IP {

	var i int
	var s string

	s = "X-Real-IP"
	for i = 0; i < 3; i++ {
		if i == 1 {
			s = strings.ToLower(s)
		}
		if i == 2 {
			s = strings.ToUpper(s)
		}
		if ip := c.Request().Header().Get(s); ip != "" {
			return net.ParseIP(ip)
		}
	}

	s = "X-Forwarded-For"
	for i = 0; i < 3; i++ {
		if i == 1 {
			s = strings.ToLower(s)
		}
		if i == 2 {
			s = strings.ToUpper(s)
		}
		if ip := c.Request().Header().Get(s); ip != "" {
			return net.ParseIP(strings.Split(ip, ", ")[0])
		}
	}

	addr := c.Request().RemoteAddr
	addr, _, _ = net.SplitHostPort(addr)
	return net.ParseIP(addr)

}
640175
func interfaceByIP(ip string) (string, int, error) {
	ifaces, err := netInterfaces()
	if err != nil {
		return "", 0, err
	}

	for _, iface := range ifaces {
		ifaceIP, mask, err := ipByInterface(iface.Name)
		if err == nil && ip == ifaceIP {
			return iface.Name, mask, nil
		}
	}

	return "", 0, fmt.Errorf("no matching interface found for IP %s", ip)
}
439847
func DefaultIP() string {
	addrs, err := netInterfaceAddrs()
	if err == nil {
		for _, addr := range addrs {
			if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() {
				if ip.IP.To4() != nil {
					return ip.IP.String()
				}
			}
		}
	}

	return ""
}
276213
func Inc(IP net.IP) net.IP {
	IP = checkIPv4(IP)
	incIP := make([]byte, len(IP))
	copy(incIP, IP)
	for j := len(incIP) - 1; j >= 0; j-- {
		incIP[j]++
		if incIP[j] > 0 {
			break
		}
	}
	return incIP
}
1109337
func GetIP() string {
	ifaces, err := net.Interfaces()
	if err != nil {
		return "127.0.0.1"
	}
	for _, i := range ifaces {
		addrs, err := i.Addrs()
		if err != nil {
			return "127.0.0.1"
		}
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPAddr:
				ip = v.IP
			case *net.IPNet:
				ip = v.IP
			}
			if ip == nil || ip.IsLoopback() {
				continue
			}
			ip = ip.To4()
			if ip == nil {
				continue // not an ipv4 address
			}
			return ip.String()
		}
	}
	return "127.0.0.1"
}
710559
func StrAddrsToStruct(addrs []string) ([]*net.IPNet, error) {
	var result []*net.IPNet
	for _, addressWithPrefix := range addrs {
		if addressWithPrefix == "" {
			continue
		}
		parsedIPWithPrefix, _, err := ParseIPWithPrefix(addressWithPrefix)
		if err != nil {
			return result, err
		}
		result = append(result, parsedIPWithPrefix)
	}

	return result, nil
}
1086640
func IsV4MulticastAddress(addr net.IP) bool {
	if len(addr) != IPv4AddressSize {
		return false
	}
	return (addr[0] & 0xf0) == 0xe0
}
999016
func (ipv6 IPv6Addr) Network() IPAddr {
	return IPv6Addr{
		Address: IPv6Address(ipv6.NetworkAddress()),
		Mask:    ipv6.Mask,
	}
}
301933
func (f ipAddressFunc) evaluate(values map[string][]string) bool {
	IPs := []net.IP{}
	requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())]
	if !ok {
		requestValue = values[f.k.Name()]
	}

	for _, s := range requestValue {
		IP := net.ParseIP(s)
		if IP == nil {
			panic(fmt.Errorf("invalid IP address '%v'", s))
		}

		IPs = append(IPs, IP)
	}

	for _, IP := range IPs {
		for _, IPNet := range f.values {
			if IPNet.Contains(IP) {
				return true
			}
		}
	}

	return false
}
128540
func NetAddrIPPROTO(addr net.Addr) int {
	switch addr := addr.(type) {
	case *net.IPAddr:
		switch {
		default:
			return IPPROTO_IP

		case addr.IP.To4() != nil:
			return IPPROTO_IPV4

		case addr.IP.To16() != nil:
			return IPPROTO_IPV6
		}

	case *net.TCPAddr:
		return IPPROTO_TCP

	case *net.UDPAddr:
		return IPPROTO_UDP

	default:
		return -1
	}
}