示例#1
0
func benchmarkReadWriteIPv6UDP(b *testing.B, p *ipv6.PacketConn, wb, rb []byte, dst net.Addr, ifi *net.Interface) {
	cm := ipv6.ControlMessage{
		TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
		HopLimit:     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)
	}
}
示例#2
0
文件: gomul.go 项目: 42wim/gomul
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 {}
}