当前位置: 首页>>代码示例>>Golang>>正文


Golang net.InterfaceByIndex函数代码示例

本文整理汇总了Golang中net.InterfaceByIndex函数的典型用法代码示例。如果您正苦于以下问题:Golang InterfaceByIndex函数的具体用法?Golang InterfaceByIndex怎么用?Golang InterfaceByIndex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了InterfaceByIndex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: readLoop

func readLoop(c *ipv4.PacketConn) {

	log.Printf("readLoop: reading")

	buf := make([]byte, 10000)

	for {
		n, cm, _, err1 := c.ReadFrom(buf)
		if err1 != nil {
			log.Printf("readLoop: ReadFrom: error %v", err1)
			break
		}

		var name string

		ifi, err2 := net.InterfaceByIndex(cm.IfIndex)
		if err2 != nil {
			log.Printf("readLoop: unable to solve ifIndex=%d: error: %v", cm.IfIndex, err2)
		}

		if ifi == nil {
			name = "ifname?"
		} else {
			name = ifi.Name
		}

		log.Printf("readLoop: recv %d bytes from %s to %s on %s", n, cm.Src, cm.Dst, name)
	}

	log.Printf("readLoop: exiting")
}
开发者ID:udhos,项目名称:nexthop,代码行数:31,代码来源:mcast_listener.go

示例2: getDefaultGatewayIface

func getDefaultGatewayIface() *net.Interface {
	log.Debug("Attempting to retrieve IP route info from netlink")

	routes, err := netlink.RouteList(nil, 0)
	if err != nil {
		log.Debugf("Unable to detect default interface: %v", err)
		return nil
	}

	if len(routes) == 0 {
		log.Debugf("Netlink returned zero routes")
		return nil
	}

	for _, route := range routes {
		// a nil Dst means that this is the default route.
		if route.Dst == nil {
			i, err := net.InterfaceByIndex(route.LinkIndex)
			if err != nil {
				log.Debugf("Found default route but could not determine interface")
				continue
			}
			log.Debugf("Found default route with interface %v", i)
			return i
		}
	}

	log.Debugf("Unable to find default route")
	return nil
}
开发者ID:jonboulle,项目名称:fleet,代码行数:30,代码来源:coreos.go

示例3: udpReader

func udpReader(c *ipv4.PacketConn, ifname, hostPort string) {

	log.Printf("udpReader: reading multicast")

	defer c.Close()

	buf := make([]byte, 10000)

	for {
		n, cm, _, err1 := c.ReadFrom(buf)
		if err1 != nil {
			log.Printf("udpReader: ReadFrom: error %v", err1)
			break
		}

		ifi, err2 := net.InterfaceByIndex(cm.IfIndex)
		if err2 != nil {
			log.Printf("udpReader: could not solve ifindex=%d: %v", cm.IfIndex, err2)
		}

		ifname := "ifname?"
		if ifi != nil {
			ifname = ifi.Name
		}

		log.Printf("udpReader: recv %d bytes from %s to %s on %s (ifindex=%d)", n, cm.Src, cm.Dst, ifname, cm.IfIndex)
	}

	log.Printf("udpReader: exiting")
}
开发者ID:udhos,项目名称:nexthop,代码行数:30,代码来源:rip_mcast_listener.go

示例4: findPairInterfaceOfContainerInterface

func findPairInterfaceOfContainerInterface(e exec.Interface, containerInterfaceName, containerDesc string, nsenterArgs []string) (string, error) {
	nsenterPath, err := e.LookPath("nsenter")
	if err != nil {
		return "", err
	}
	ethtoolPath, err := e.LookPath("ethtool")
	if err != nil {
		return "", err
	}

	nsenterArgs = append(nsenterArgs, "-F", "--", ethtoolPath, "--statistics", containerInterfaceName)
	output, err := e.Command(nsenterPath, nsenterArgs...).CombinedOutput()
	if err != nil {
		return "", fmt.Errorf("Unable to query interface %s of container %s: %v: %s", containerInterfaceName, containerDesc, err, string(output))
	}
	// look for peer_ifindex
	match := ethtoolOutputRegex.FindSubmatch(output)
	if match == nil {
		return "", fmt.Errorf("No peer_ifindex in interface statistics for %s of container %s", containerInterfaceName, containerDesc)
	}
	peerIfIndex, err := strconv.Atoi(string(match[1]))
	if err != nil { // seems impossible (\d+ not numeric)
		return "", fmt.Errorf("peer_ifindex wasn't numeric: %s: %v", match[1], err)
	}
	iface, err := net.InterfaceByIndex(peerIfIndex)
	if err != nil {
		return "", err
	}
	return iface.Name, nil
}
开发者ID:alex-mohr,项目名称:kubernetes,代码行数:30,代码来源:hairpin.go

示例5: ExampleWriteIPOSPFHello

func ExampleWriteIPOSPFHello() {
	var ifs []*net.Interface
	en0, err := net.InterfaceByName("en0")
	if err != nil {
		log.Fatal(err)
	}
	ifs = append(ifs, en0)
	en1, err := net.InterfaceByIndex(911)
	if err != nil {
		log.Fatal(err)
	}
	ifs = append(ifs, en1)

	c, err := net.ListenPacket("ip4:89", "0.0.0.0")
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()

	r, err := ipv4.NewRawConn(c)
	if err != nil {
		log.Fatal(err)
	}
	for _, ifi := range ifs {
		err := r.JoinGroup(ifi, &net.IPAddr{IP: AllSPFRouters})
		if err != nil {
			log.Fatal(err)
		}
		err = r.JoinGroup(ifi, &net.IPAddr{IP: AllDRouters})
		if err != nil {
			log.Fatal(err)
		}
	}

	hello := make([]byte, OSPFHelloHeaderLen)
	ospf := make([]byte, OSPFHeaderLen)
	ospf[0] = OSPF_VERSION
	ospf[1] = OSPF_TYPE_HELLO
	ospf = append(ospf, hello...)
	iph := &ipv4.Header{}
	iph.Version = ipv4.Version
	iph.Len = ipv4.HeaderLen
	iph.TOS = ipv4.DSCP_CS6
	iph.TotalLen = ipv4.HeaderLen + len(ospf)
	iph.TTL = 1
	iph.Protocol = 89
	iph.Dst = AllSPFRouters

	for _, ifi := range ifs {
		err := r.SetMulticastInterface(ifi)
		if err != nil {
			return
		}
		err = r.WriteTo(iph, ospf, nil)
		if err != nil {
			return
		}
	}
}
开发者ID:houcy,项目名称:go-push-server,代码行数:59,代码来源:example_test.go

示例6: IP6ZoneToString

// IP6ZoneToString converts an IP6 Zone syscall int to a net string
// returns "" if zone is 0
func IP6ZoneToString(zone int) string {
	if zone == 0 {
		return ""
	}
	if ifi, err := net.InterfaceByIndex(zone); err == nil {
		return ifi.Name
	}
	return itod(uint(zone))
}
开发者ID:andradeandrey,项目名称:go-ipfs,代码行数:11,代码来源:net.go

示例7: udpReader

func udpReader(c *ipv4.PacketConn, input chan<- *udpInfo, ifname string, readerDone chan<- int, listenPort int) {

	log.Printf("udpReader: reading from '%s'", ifname)

	defer c.Close()

	buf := make([]byte, 10000)

LOOP:
	for {
		n, cm, srcAddr, err1 := c.ReadFrom(buf)
		if err1 != nil {
			log.Printf("udpReader: ReadFrom: error %v", err1)
			break LOOP
		}

		var udpSrc *net.UDPAddr

		switch srcAddr.(type) {
		case *net.UDPAddr:
			udpSrc = srcAddr.(*net.UDPAddr)
		}

		var name string

		var ifi *net.Interface
		var err2 error

		if cm != nil {
			ifi, err2 = net.InterfaceByIndex(cm.IfIndex)
			if err2 != nil {
				log.Printf("udpReader: unable to solve ifIndex=%d: error: %v", cm.IfIndex, err2)
			}
		}

		if ifi == nil {
			name = "ifname?"
		} else {
			name = ifi.Name
		}

		udpDst := net.UDPAddr{IP: cm.Dst, Port: listenPort}

		//log.Printf("udpReader: recv %d bytes from %v to %v on %s ifIndex=%d", n, udpSrc, &udpDst, name, cm.IfIndex)

		// make a copy because we will overwrite buf
		b := make([]byte, n)
		copy(b, buf)

		// deliver udp packet to main rip goroutine
		input <- &udpInfo{info: b, src: *udpSrc, dst: udpDst, ifIndex: cm.IfIndex, ifName: name}
	}

	log.Printf("udpReader: exiting '%s' -- trying", ifname)
	readerDone <- 1 // tell rip router goroutine
	log.Printf("udpReader: exiting '%s'", ifname)
}
开发者ID:udhos,项目名称:nexthop,代码行数:57,代码来源:router.go

示例8: ipv4MulticastInterface

func ipv4MulticastInterface(fd int) (*net.Interface, error) {
	mreqn, err := syscall.GetsockoptIPMreqn(fd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_IF)
	if err != nil {
		return nil, os.NewSyscallError("getsockopt", err)
	}
	if int(mreqn.Ifindex) == 0 {
		return nil, nil
	}
	return net.InterfaceByIndex(int(mreqn.Ifindex))
}
开发者ID:Bobberino,项目名称:musings,代码行数:10,代码来源:sockopt_linux.go

示例9: Interface

// Interface returns the associated network interface.
func (c *Conn) Interface() (*net.Interface, error) {
	if !c.ok() {
		return nil, syscall.EINVAL
	}
	ifi, err := net.InterfaceByIndex(c.ifindex)
	if err != nil {
		return nil, err
	}
	return ifi, nil
}
开发者ID:NanXiao,项目名称:tunnel,代码行数:11,代码来源:conn.go

示例10: ipv4MulticastInterface

func ipv4MulticastInterface(fd int) (*net.Interface, error) {
	mreqn, err := syscall.GetsockoptIPMreqn(fd, ianaProtocolIP, sysSockoptMulticastInterface)
	if err != nil {
		return nil, os.NewSyscallError("getsockopt", err)
	}
	if mreqn.Ifindex == 0 {
		return nil, nil
	}
	return net.InterfaceByIndex(int(mreqn.Ifindex))
}
开发者ID:Christopheraburns,项目名称:clive,代码行数:10,代码来源:sockopt_linux.go

示例11: ipv6MulticastInterface

func ipv6MulticastInterface(fd int) (*net.Interface, error) {
	v, err := syscall.GetsockoptInt(fd, ianaProtocolIPv6, syscall.IPV6_MULTICAST_IF)
	if err != nil {
		return nil, os.NewSyscallError("getsockopt", err)
	}
	if v == 0 {
		return nil, nil
	}
	ifi, err := net.InterfaceByIndex(v)
	if err != nil {
		return nil, err
	}
	return ifi, nil
}
开发者ID:98pm,项目名称:docker,代码行数:14,代码来源:sockopt_rfc3493_unix.go

示例12: getMacAddr

func getMacAddr() (mac [6]uint8, err error) {
	ifaces, _ := net.Interfaces()
	for i := range ifaces {

		iface, err := net.InterfaceByIndex(i + 1)
		if err != nil {
			log.Println(err, i)
			break
		}
		log.Println(iface.HardwareAddr)

	}
	return
}
开发者ID:terual,项目名称:slimgo,代码行数:14,代码来源:main.go

示例13: getsockoptIPMreqn

func getsockoptIPMreqn(s uintptr, name int) (*net.Interface, error) {
	var mreqn ipMreqn
	l := uint32(sizeofIPMreqn)
	if err := getsockopt(s, iana.ProtocolIP, name, unsafe.Pointer(&mreqn), &l); err != nil {
		return nil, os.NewSyscallError("getsockopt", err)
	}
	if mreqn.Ifindex == 0 {
		return nil, nil
	}
	ifi, err := net.InterfaceByIndex(int(mreqn.Ifindex))
	if err != nil {
		return nil, err
	}
	return ifi, nil
}
开发者ID:Requilence,项目名称:integram,代码行数:15,代码来源:sockopt_asmreqn_unix.go

示例14: ipv6MulticastInterface

func ipv6MulticastInterface(fd syscall.Handle) (*net.Interface, error) {
	var v int32
	l := int32(4)
	if err := syscall.Getsockopt(fd, int32(ianaProtocolIPv6), int32(syscall.IPV6_MULTICAST_IF), (*byte)(unsafe.Pointer(&v)), &l); err != nil {
		return nil, os.NewSyscallError("getsockopt", err)
	}
	if v == 0 {
		return nil, nil
	}
	ifi, err := net.InterfaceByIndex(int(v))
	if err != nil {
		return nil, err
	}
	return ifi, nil
}
开发者ID:rocksolidlabs,项目名称:go.net,代码行数:15,代码来源:sockopt_rfc3493_windows.go

示例15: broadcastChange

//Perform RARP reassign
func broadcastChange(li netlink.Link, en endpoint) error {
	cif, err := net.InterfaceByIndex(li.Attrs().Index)
	if err != nil {
		return fmt.Errorf("could not rediscover interface by index %d (%s): %v", li.Attrs().Index, li.Attrs().Name, err)
	}
	carp, err := arp.NewClient(cif)
	if err != nil {
		return fmt.Errorf("could not bind arp client to interface %s: %v", li.Attrs().Name, err)
	}
	//Broadcast change of ip mac pair
	Log.Infof("Doing broadcast for ip: %v, mac: %v", en.addr, en.mac)
	defer carp.Close()

	return carp.BroadcastChange(en.addr, en.mac)
}
开发者ID:xytis,项目名称:polyp,代码行数:16,代码来源:endpoints.go


注:本文中的net.InterfaceByIndex函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。