當前位置: 首頁>>代碼示例>>Golang>>正文


Golang IP.IsLoopback方法代碼示例

本文整理匯總了Golang中net.IP.IsLoopback方法的典型用法代碼示例。如果您正苦於以下問題:Golang IP.IsLoopback方法的具體用法?Golang IP.IsLoopback怎麽用?Golang IP.IsLoopback使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.IP的用法示例。


在下文中一共展示了IP.IsLoopback方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: IsGlobalIP

// IsGlobalIP determs passed ip address is global or not.
// if ip address is global , it returns address type(ip4 or ip6).
func IsGlobalIP(trial net.IP) string {
	type localIPrange struct {
		from net.IP
		to   net.IP
	}
	locals := []localIPrange{
		localIPrange{net.ParseIP("10.0.0.0"), net.ParseIP("10.255.255.255")},
		localIPrange{net.ParseIP("172.16.0.0"), net.ParseIP("172.31.255.255")},
		localIPrange{net.ParseIP("192.168.0.0"), net.ParseIP("192.168.255.255")}}

	if trial == nil || trial.IsLoopback() {
		return ""
	}
	//for udp6
	if trial.To4() == nil {
		if trial.IsGlobalUnicast() {
			return "ip6"
		}
		return ""
	}
	//for udp4
	for _, r := range locals {
		if bytes.Compare(trial, r.from) >= 0 && bytes.Compare(trial, r.to) <= 0 {
			return ""
		}
	}
	return "ip4"
}
開發者ID:shingetsu-gou,項目名稱:go-nat,代碼行數:30,代碼來源:netstatus.go

示例2: getIPStringAddresses

// Get IP addresses
//
// http://play.golang.org/p/BDt3qEQ_2H
func getIPStringAddresses() []string {
	addrStrings := []string{}
	if ifaces, err := net.Interfaces(); err == nil {
		for _, iface := range ifaces {
			// skip
			if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
				continue
			}

			if addrs, err := iface.Addrs(); err == nil {
				for _, addr := range addrs {
					var ip net.IP
					switch v := addr.(type) {
					case *net.IPNet:
						ip = v.IP
					case *net.IPAddr:
						ip = v.IP
					}
					if ip == nil || ip.IsLoopback() {
						continue
					}
					ip = ip.To4()
					if ip == nil {
						continue
					}

					addrStrings = append(addrStrings, ip.String())
				}
			}
		}
	}

	return addrStrings
}
開發者ID:meinside,項目名稱:scroll-daemon,代碼行數:37,代碼來源:util.go

示例3: LocalIpv4Addrs

// LocalIpv4Addrs scan all ip addresses with loopback excluded.
func LocalIpv4Addrs() ([]string, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}

	ips := make([]string, 0)
	for _, addr := range addrs {
		var ip net.IP
		switch x := addr.(type) {
		case *net.IPNet:
			ip = x.IP
		case *net.IPAddr:
			ip = x.IP
		default:
			err = fmt.Errorf("unknown interface address type for: %+v", x)
			return nil, err
		}

		if ip.IsLoopback() || ip.To4() == nil {
			// loopback excluded, ipv6 excluded
			continue
		}

		ips = append(ips, ip.String())
	}

	return ips, nil
}
開發者ID:postfix,項目名稱:golib-1,代碼行數:30,代碼來源:addr.go

示例4: getv4loopback

func getv4loopback() (string, error) {
	ifaces, err := net.Interfaces()
	if err != nil {
		return "", err
	}
	for _, iface := range ifaces {
		if iface.Flags&net.FlagLoopback != 0 {
			addrs, err := iface.Addrs()
			if err != nil {
				return "", err
			}
			for _, addr := range addrs {
				var ip net.IP
				switch v := addr.(type) {
				case *net.IPNet:
					ip = v.IP
				case *net.IPAddr:
					ip = v.IP
				}
				ip = ip.To4()
				if ip == nil {
					continue
				}
				if ip.IsLoopback() {
					return ip.String(), nil
				}
			}
		}
	}
	return "", errors.New("you do not have loopback?")
}
開發者ID:masanorih,項目名稱:go-ipmsg,代碼行數:31,代碼來源:ipmsg_test.go

示例5: getIP

func getIP() net.IP {
	ifaces, err := net.Interfaces()
	if err != nil {
		return nil
	}

	for _, iface := range ifaces {
		if iface.Flags&net.FlagUp == 0 {
			continue // interface down
		}
		if iface.Flags&net.FlagLoopback != 0 {
			continue // loopback interface
		}
		addrs, err := iface.Addrs()
		if err != nil {
			return nil
		}
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			if ip == nil || ip.IsLoopback() {
				continue
			}
			return ip
		}
	}

	return net.IPv6loopback

}
開發者ID:Cistern,項目名稱:gohsflowd,代碼行數:35,代碼來源:main.go

示例6: getFirstLocalIPAddr

// getFirstLocalIPAddr returns the first available IP address of the local machine
// This is a fix for Beaglebone Black where net.LookupIP(hostname) return no IP address.
func getFirstLocalIPAddr() (net.IP, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}

	for _, addr := range addrs {
		var ip net.IP
		switch v := addr.(type) {
		case *net.IPNet:
			ip = v.IP
		case *net.IPAddr:
			ip = v.IP
		}
		if ip == nil || ip.IsLoopback() || ip.IsUnspecified() {
			continue
		}
		ip = ip.To4()
		if ip == nil {
			continue // not an ipv4 address
		}
		return ip, nil
	}

	return nil, errors.New("Could not determine ip address")
}
開發者ID:brutella,項目名稱:hklifx,代碼行數:28,代碼來源:config.go

示例7: findLoopbackDevice

// findLoopbackDevice iterates through all the interfaces on a machine and
// returns the ip addr, mask of the loopback device
func (a *Agent) findLoopbackDevice() (string, string, string, error) {
	var ifcs []net.Interface
	var err error
	ifcs, err = net.Interfaces()
	if err != nil {
		return "", "", "", err
	}
	for _, ifc := range ifcs {
		addrs, err := ifc.Addrs()
		if err != nil {
			return "", "", "", err
		}
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			if ip.IsLoopback() {
				if ip.To4() == nil {
					continue
				}
				return ifc.Name, ip.String(), addr.String(), nil
			}
		}
	}

	return "", "", "", fmt.Errorf("no loopback devices with IPV4 addr found")
}
開發者ID:nak3,項目名稱:nomad,代碼行數:33,代碼來源:agent.go

示例8: validateRemoteAddr

func validateRemoteAddr(ip net.IP) bool {
	if ip == nil {
		return false
	}
	if ip.IsInterfaceLocalMulticast() {
		return false
	}
	if ip.IsLinkLocalMulticast() {
		return false
	}
	if ip.IsLinkLocalUnicast() {
		return false
	}
	if ip.IsLoopback() {
		return false
	}
	if ip.IsMulticast() {
		return false
	}
	if ip.IsUnspecified() {
		return false
	}
	if isBroadcasty(ip) {
		return false
	}

	return true
}
開發者ID:neviim,項目名稱:scope,代碼行數:28,代碼來源:net.go

示例9: getIfaceIP

func getIfaceIP(iface string) string {

	i, err := net.InterfaceByName(iface)
	if err != nil {
		time.Sleep(time.Second * 5)
		panic(err)
	}

	addrs, err := i.Addrs()
	if err != nil {
		time.Sleep(time.Second * 5)
		panic(err)
	}
	for _, addr := range addrs {
		var ip net.IP
		switch v := addr.(type) {
		case *net.IPNet:
			ip = v.IP
		case *net.IPAddr:
			ip = v.IP
		}
		if ip == nil || ip.IsLoopback() {
			continue
		}
		ip = ip.To4()
		if ip == nil {
			continue // not an ipv4 address
		}
		return ip.String()
	}

	panic("No IP for " + iface)

}
開發者ID:ninjasphere,項目名稱:go-daikin-ac,代碼行數:34,代碼來源:proxy.go

示例10: addrsOfOneInterface

// 一個接口上的所有ip4地址
func addrsOfOneInterface(iface net.Interface) (addrs []*net.IPAddr) {
	ifaddrs, err := iface.Addrs()

	if (err != nil) || (len(ifaddrs) == 0) {
		return
	}

	for _, ifaddr := range ifaddrs {
		var ip net.IP
		switch v := ifaddr.(type) {
		case *net.IPNet:
			ip = v.IP
		case *net.IPAddr:
			ip = v.IP
		default:
			continue
		}
		if ip.IsLoopback() {
			return
		}
		ip = ip.To4()
		if ip != nil {
			addr, _ := net.ResolveIPAddr("ip", ip.String())
			addrs = append(addrs, addr)
		}
	}
	return
}
開發者ID:adoyee,項目名稱:probe,代碼行數:29,代碼來源:interfaces.go

示例11: GetLocalIP

// GetLocalIP return the first external-IP4 configured for the first
// interface connected to this node.
func GetLocalIP() (net.IP, error) {
	interfaces, err := net.Interfaces()
	if err != nil {
		return nil, err
	}
	for _, iface := range interfaces {
		if (iface.Flags & net.FlagUp) == 0 {
			continue // interface down
		}
		if (iface.Flags & net.FlagLoopback) != 0 {
			continue // loopback interface
		}
		addrs, err := iface.Addrs()
		if err != nil {
			return nil, err
		}
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			if ip != nil && !ip.IsLoopback() {
				if ip = ip.To4(); ip != nil {
					return ip, nil
				}
			}
		}
	}
	return nil, errors.New("cannot find local IP address")
}
開發者ID:jchris,項目名稱:indexing,代碼行數:35,代碼來源:util.go

示例12: macAddress

func macAddress() (string, error) {
	ifaces, err := net.Interfaces()

	if err != nil {
		return "", err
	}

	for _, iface := range ifaces {
		if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
			// interface down or loopback interface
			continue
		}
		addrs, err := iface.Addrs()
		if err != nil {
			return "", err
		}
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			if ip == nil || ip.IsLoopback() || ip.To4() == nil {
				continue
			}
			return iface.HardwareAddr.String(), nil
		}
	}
	return "", errors.New("not connected to the network")
}
開發者ID:startover,項目名稱:gohai,代碼行數:32,代碼來源:network_common.go

示例13: printInterfaces

// print interfaces to know where the proxy is listening
func printInterfaces() {
	addrs, err := net.InterfaceAddrs()

	if err != nil {
		fmt.Println("Can't get interfaces. You have to have at least one network connection.")
		log.Fatal("No interface found")
	}

	for _, addr := range addrs {

		var ip net.IP
		switch v := addr.(type) {
		case *net.IPAddr:
		case *net.IPNet:
			ip = v.IP
		}

		if ip == nil || ip.IsLoopback() {
			continue
		}

		ip = ip.To4()
		if ip == nil {
			continue // not an ipv4 address
		}
		fmt.Println("http://" + ip.String() + Config.Service.Listen)
	}
}
開發者ID:edi-design,項目名稱:kd-go,代碼行數:29,代碼來源:service.go

示例14: GetIPAddress

// GetIPAddress : Used to get IP of the running machine
func GetIPAddress() string {
	ifaces, _ := net.Interfaces()
	// handle err
	for _, i := range ifaces {
		addrs, _ := i.Addrs()
		// handle err
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			if ip == nil || ip.IsLoopback() {
				continue
			}
			ip = ip.To4()
			if ip == nil {
				continue // not an ipv4 address
			}
			return ip.String()
			// process IP address
		}
	}

	return ""
}
開發者ID:sjoshi6,項目名稱:go-lang-Projects,代碼行數:29,代碼來源:util.go

示例15: GetFirstInterface

func GetFirstInterface() (name string, ip string) {
	ifaces, _ := net.Interfaces()
	for _, iface := range ifaces {
		addrs, _ := iface.Addrs()

		ipV4 := false
		ipAddrs := []string{}
		for _, addr := range addrs {
			var ip net.IP
			if ipnet, ok := addr.(*net.IPNet); ok {
				ip = ipnet.IP
			} else if ipaddr, ok := addr.(*net.IPAddr); ok {
				ip = ipaddr.IP
			}
			if ip != nil && ip.To4() != nil && !ip.IsLoopback() {
				ipstr := addr.String()
				idx := strings.Index(ipstr, "/")
				if idx >= 0 {
					ipstr = ipstr[:idx]
				}
				ipAddrs = append(ipAddrs, ipstr)
				ipV4 = true
			}
		}
		if !ipV4 {
			continue
		}

		return iface.Name, ipAddrs[0]
	}

	return "", "0.0.0.0"
}
開發者ID:xxoxx,項目名稱:httpcap,代碼行數:33,代碼來源:input_raw.go


注:本文中的net.IP.IsLoopback方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。