当前位置: 首页>>代码示例>>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;未经允许,请勿转载。