• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang ipv4.PacketConn类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



在下文中一共展示了PacketConn类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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: 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


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


示例4: benchmarkReadWriteIPv4UDP

func benchmarkReadWriteIPv4UDP(b *testing.B, p *ipv4.PacketConn, wb, rb []byte, dst net.Addr, ifi *net.Interface) {
	cm := ipv4.ControlMessage{TTL: 1}
	if ifi != nil {
		cm.IfIndex = ifi.Index
	}
	if n, err := p.WriteTo(wb, &cm, dst); err != nil {
		b.Fatal(err)
	} else if n != len(wb) {
		b.Fatalf("got %v; want %v", n, len(wb))
	}
	if _, _, _, err := p.ReadFrom(rb); err != nil {
		b.Fatal(err)
	}
}
开发者ID:Rudloff,项目名称:platform,代码行数:14,代码来源:readwrite_test.go


示例5: sender

func sender(p *ipv4.PacketConn, dst net.Addr) {
	ift, err := net.Interfaces()
	if err != nil {
		log.Fatal(err)
	}
	for i := 0; ; i++ {
		avail := net.FlagMulticast | net.FlagUp
		for _, ifi := range ift {
			if ifi.Flags&avail != avail {
				continue
			}
			p.SetMulticastInterface(&ifi)
			b := []byte(fmt.Sprintf("#%v HELLLO-R-U-THERE", i))
			if _, err := p.WriteTo(b, nil, dst); err != nil {
				log.Println(err, "on", ifi)
				continue
			}
			log.Printf("%v bytes sent to %v via %v\n", len(b), dst, ifi)
			time.Sleep(time.Second)
		}
	}
}
开发者ID:jordan2175,项目名称:-stdyng,代码行数:22,代码来源:main.go


示例6: main

func main() {
	var flagGroup, flagIP string
	var flagIndex int
	var c net.PacketConn
	var p6 *ipv6.PacketConn
	var p4 *ipv4.PacketConn

	iMap = make(map[string]int)

	flag.BoolVar(&flagList, "li", false, "show available interfaces")
	flag.IntVar(&flagIndex, "interface", 0, "interface to listen on (number)")
	flag.StringVar(&flagGroup, "group", "ff02::42:1 239.42.42.1", "multicast groups to join (space seperated)")
	flag.StringVar(&flagIP, "ip", "", "use interface where the specified ip is bound on")
	flag.Parse()
	if flag.NFlag() < 1 {
		flag.PrintDefaults()
		os.Exit(1)
	}
	if flagList {
		parseInterfaces()
		os.Exit(1)
	}

	parseInterfaces()
	if flagIndex == 0 {
		fmt.Print("searching interface for ip ", flagIP)
		for k, v := range iMap {
			if strings.HasPrefix(k, flagIP) {
				fmt.Println(" using interface", v, "with ip", k)
				flagIndex = v
				break
			}
		}
	}

	fmt.Println("listening on index", flagIndex)
	iface, err := net.InterfaceByIndex(flagIndex)
	if err != nil {
		log.Fatal(err)
	}

	groups := strings.Fields(flagGroup)

	if strings.Contains(flagGroup, ":") {
		c, err = net.ListenPacket("udp6", "[::]:1024")
		if err != nil {
			log.Fatal(err)
		}
		defer c.Close()
		p6 = ipv6.NewPacketConn(c)
	}
	if strings.Contains(flagGroup, ".") {
		c, err = net.ListenPacket("udp4", ":1024")
		if err != nil {
			log.Fatal(err)
		}
		defer c.Close()
		p4 = ipv4.NewPacketConn(c)
	}

	for _, group := range groups {
		IPgroup := net.ParseIP(group)
		if strings.Contains(group, ":") {
			fmt.Println("joining ipv6 group", group)
			if err := p6.JoinGroup(iface, &net.UDPAddr{IP: IPgroup}); err != nil {
				log.Fatal(err)
			}
		} else {
			fmt.Println("joining ipv4 group", group)
			if err := p4.JoinGroup(iface, &net.UDPAddr{IP: IPgroup}); err != nil {
				log.Fatal(err)
			}
		}
	}
	select {}
}
开发者ID:42wim,项目名称:gomul,代码行数:76,代码来源:gomul.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang ipv6.NewPacketConn函数代码示例发布时间:2022-05-28
下一篇:
Golang ipv4.ControlMessage类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap