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


Golang net.Interface類代碼示例

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


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

示例1: Addresses

func Addresses(nif net.Interface, v4 bool, v6 bool, retries int) ([]net.Addr, []net.Addr, error) {
	var v4Addrs, v6Addrs []net.Addr

	for n := retries; n >= 0; n-- {
		addrs, err := nif.Addrs()
		if err != nil {
			return nil, nil, err
		}

		v4Addrs, v6Addrs = partition(addrs)

		if v4 && v6 && len(v4Addrs) > 0 && len(v6Addrs) > 0 {
			break
		} else if v4 && len(v4Addrs) > 0 {
			v6Addrs = []net.Addr{}
			break
		} else if v6 && len(v6Addrs) > 0 {
			v4Addrs = []net.Addr{}
			break
		}

		if n > 0 {
			time.Sleep(1 * time.Second)
		}
	}

	return v4Addrs, v6Addrs, nil
}
開發者ID:scraperwiki,項目名稱:nif,代碼行數:28,代碼來源:nif.go

示例2: 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

示例3: AddressesOnInterface

func AddressesOnInterface(iface *net.Interface) (*[]net.Addr, error) {
	addrs, err := iface.Addrs()
	if err != nil {
		return nil, err
	}
	return &addrs, nil
}
開發者ID:Ropes,項目名稱:interpoller,代碼行數:7,代碼來源:ifacer.go

示例4: setSyscallIPMreq

func setSyscallIPMreq(mreq *syscall.IPMreq, ifi *net.Interface) error {
	if ifi == nil {
		return nil
	}
	ifat, err := ifi.Addrs()
	if err != nil {
		return err
	}
	for _, ifa := range ifat {
		switch v := ifa.(type) {
		case *net.IPAddr:
			if a := v.IP.To4(); a != nil {
				copy(mreq.Interface[:], a)
				goto done
			}
		case *net.IPNet:
			if a := v.IP.To4(); a != nil {
				copy(mreq.Interface[:], a)
				goto done
			}
		}
	}
done:
	if bytes.Equal(mreq.Multiaddr[:], net.IPv4zero.To4()) {
		return errNoSuchMulticastInterface
	}
	return nil
}
開發者ID:98pm,項目名稱:docker,代碼行數:28,代碼來源:helper_posix.go

示例5: updateHosts

func updateHosts(iface *net.Interface) {
	addrs, err := iface.Addrs()
	checkErr(err)
	if len(addrs) == 0 {
		return
	}
	hostname, err := os.Hostname()
	checkErr(err)

	hosts := parseHosts()

	// Remove existing ips pointing to our hostname
	toRemove := []string{}
	for ip, addrs := range hosts {
		for _, addr := range addrs {
			if addr == hostname {
				toRemove = append(toRemove, ip)
				break
			}
		}
	}
	for _, ip := range toRemove {
		delete(hosts, ip)
	}

	// Add the weave ip(s)
	for _, addr := range addrs {
		if addr, ok := addr.(*net.IPNet); ok {
			ip := addr.IP.String()
			hosts[ip] = append(hosts[ip], hostname)
		}
	}

	writeHosts(hosts)
}
開發者ID:ramitsurana,項目名稱:weave,代碼行數:35,代碼來源:main.go

示例6: isGoodForMulticast

func isGoodForMulticast(ifi *net.Interface) (net.IP, bool) {
	if ifi.Flags&net.FlagUp == 0 {
		return nil, false
	}
	// We need a unicast IPv4 address that can be used to specify
	// the IPv4 multicast interface.
	ifat, err := ifi.Addrs()
	if err != nil {
		return nil, false
	}
	if len(ifat) == 0 {
		return nil, false
	}
	var ip net.IP
	for _, ifa := range ifat {
		switch v := ifa.(type) {
		case *net.IPAddr:
			ip = v.IP
		case *net.IPNet:
			ip = v.IP
		default:
			continue
		}
		if ip.To4() == nil {
			ip = nil
			continue
		}
		break
	}
	if ip == nil {
		return nil, false
	}
	return ip, true
}
開發者ID:Bobberino,項目名稱:musings,代碼行數:34,代碼來源:mocktransponder_test.go

示例7: GetIfaceIP4Addr

func GetIfaceIP4Addr(iface *net.Interface) (net.IP, error) {
	addrs, err := iface.Addrs()
	if err != nil {
		return nil, err
	}

	// prefer non link-local addr
	var ll net.IP

	for _, addr := range addrs {
		// Attempt to parse the address in CIDR notation
		// and assert it is IPv4
		ip, _, err := net.ParseCIDR(addr.String())
		if err != nil || ip.To4() == nil {
			continue
		}

		if ip.IsGlobalUnicast() {
			return ip, nil
		}

		if ip.IsLinkLocalUnicast() {
			ll = ip
		}
	}

	if ll != nil {
		// didn't find global but found link-local. it'll do.
		return ll, nil
	}

	return nil, errors.New("No IPv4 address found for given interface")
}
開發者ID:benbjohnson,項目名稱:flannel,代碼行數:33,代碼來源:iface.go

示例8: getHostIPs

func getHostIPs(interfaceName string) ([]net.Addr, error) {
	hostInterfaces, err := net.Interfaces()
	if err != nil {
		return nil, err
	}

	var iface net.Interface
	for _, hostInterface := range hostInterfaces {
		if hostInterface.Name == interfaceName {
			iface = hostInterface
			break
		}
	}

	addrs, err := iface.Addrs()
	if err != nil {
		return nil, err
	}

	if len(addrs) == 0 {
		return nil, errors.New("no ip addresses assigned to interface")
	}

	return addrs, nil
}
開發者ID:seletskiy,項目名稱:hastur,代碼行數:25,代碼來源:network.go

示例9: ifaceToIp

func ifaceToIp(iface *net.Interface) (string, error) {
	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() {
			continue
		}
		ip = ip.To4()
		if ip == nil {
			continue // not an ipv4 address
		}
		return ip.String(), nil
	}

	return "", errors.New("Node not connected to the network.")
}
開發者ID:alwang1234,項目名稱:openstorage,代碼行數:25,代碼來源:manager.go

示例10: isMulticastAvailable

// isMulticastAvailable returns true if ifi is a multicast access
// enabled network interface.  It also returns a unicast IPv4 address
// that can be used for listening on ifi.
func isMulticastAvailable(ifi *net.Interface) (net.IP, bool) {
	if ifi.Flags&net.FlagUp == 0 || ifi.Flags&net.FlagMulticast == 0 {
		return nil, false
	}
	ifat, err := ifi.Addrs()
	if err != nil {
		return nil, false
	}
	if len(ifat) == 0 {
		return nil, false
	}
	var ip net.IP
	for _, ifa := range ifat {
		switch v := ifa.(type) {
		case *net.IPAddr:
			ip = v.IP
		case *net.IPNet:
			ip = v.IP
		default:
			continue
		}
		if ip.To4() == nil {
			ip = nil
			continue
		}
		break
	}
	return ip, true
}
開發者ID:98pm,項目名稱:docker,代碼行數:32,代碼來源:mocktransponder_test.go

示例11: ssdpUnicastDiscoverImpl

func (this *ssdpDefaultManager) ssdpUnicastDiscoverImpl(ifi *net.Interface, port string) (err error) {
	addrs, err := ifi.Addrs()
	if nil != err {
		return
	} else if 0 == len(addrs) {
		err = errors.New(fmt.Sprintf("No addresses found for interface %s", ifi.Name))
		return
	}
	var lip net.IP
	for _, addr := range addrs {
		if nil != addr.(*net.IPNet).IP.DefaultMask() {
			lip = addr.(*net.IPNet).IP
			break
		}
	}
	laddr, err := net.ResolveUDPAddr(ssdpBroadcastVersion, net.JoinHostPort(lip.String(), port))
	if nil != err {
		return
	}
	uc, err := net.ListenUDP(ssdpBroadcastVersion, laddr)
	if nil != err {
		return
	}
	this.unicast.addr = laddr
	this.unicast.conn = uc
	go this.ssdpDiscoverLoop(uc)
	<-this.readyChan
	return
}
開發者ID:ianr0bkny,項目名稱:go-sonos,代碼行數:29,代碼來源:ssdp.go

示例12: getAddresses

func getAddresses(iface net.Interface) (addresses map[string]interface{}, err error) {
	addresses = make(map[string]interface{})

	addrs, err := iface.Addrs()
	if err != nil {
		return nil, err
	}
	for _, addr := range addrs {
		var ip net.IP
		addressInfo := make(map[string]string)
		switch v := addr.(type) {
		case *net.IPNet:
			ip = v.IP
		case *net.IPAddr:
			ip = v.IP
		}

		netmask := ip.DefaultMask()
		if netmask != nil {
			addressInfo["family"] = "inet"
		} else {
			addressInfo["family"] = "inet6"
		}
		addresses[ip.String()] = addressInfo
	}

	mac := iface.HardwareAddr
	if mac != nil {
		addressInfo := make(map[string]string)
		addressInfo["family"] = "lladdr"
		addresses[mac.String()] = addressInfo
	}

	return
}
開發者ID:kentaro,項目名稱:verity,代碼行數:35,代碼來源:network.go

示例13: Addrs

func (_ networkInterface) Addrs(intf *net.Interface) ([]net.Addr, error) {
	addrs, err := intf.Addrs()
	if err != nil {
		return nil, err
	}
	return addrs, nil
}
開發者ID:johndmulhausen,項目名稱:kubernetes,代碼行數:7,代碼來源:util.go

示例14: interfaceIP

func interfaceIP(iface *net.Interface) (net.IP, error) {
	addrs, err := iface.Addrs()
	if err != nil {
		return nil, err
	}
	fs := [](func(net.IP) bool){
		net.IP.IsGlobalUnicast,
		net.IP.IsLinkLocalUnicast,
		net.IP.IsLoopback,
	}
	for _, f := range fs {
		for _, a := range addrs {
			ipaddr, ok := a.(*net.IPNet)
			if !ok {
				continue
			}
			ip := ipaddr.IP.To4()
			if ip == nil {
				continue
			}
			if f(ip) {
				return ip, nil
			}
		}
	}
	return nil, fmt.Errorf("interface %s has no usable unicast addresses", iface.Name)
}
開發者ID:colonelmo,項目名稱:blacksmith,代碼行數:27,代碼來源:blacksmith.go

示例15: getInterface

func getInterface(in net.Interface) ([]net.Addr, string) {
	addrs, err := in.Addrs()
	if err != nil {
		return nil, ""
	}
	if len(addrs) == 0 {
		return nil, ""
	}
	return addrs, in.Name
}
開發者ID:fcavani,項目名稱:discover,代碼行數:10,代碼來源:common.go


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