コード例 #1
0
ファイル: tap.go プロジェクト: uileyar/helloworld
func main() {
	ifce, err := water.NewTAP("tap0")
	fmt.Printf("%v, %v\n\n", err, ifce)
	buffer := make([]byte, BUFFERSIZE)
	for {
		_, err = ifce.Read(buffer)
		if err != nil {
			fmt.Printf("err = %v\n", err)
			break
		}
		ethertype := waterutil.MACEthertype(buffer)
		fmt.Printf("ethertype = %#v\n", ethertype)
		if ethertype == waterutil.IPv4 {
			packet := waterutil.MACPayload(buffer)
			if waterutil.IsIPv4(packet) {
				fmt.Printf("IPv4 Source:      %v [%v]\n", waterutil.MACSource(buffer), waterutil.IPv4Source(packet))
				fmt.Printf("IPv4 Destination: %v [%v]\n", waterutil.MACDestination(buffer), waterutil.IPv4Destination(packet))
				fmt.Printf("IPv4 Protocol:    %v\n\n", waterutil.IPv4Protocol(packet))
			} else if waterutil.IsIPv6(packet) {
				fmt.Printf("IPv6 Source:      %v\n", waterutil.MACSource(buffer))
				fmt.Printf("IPv6 Destination: %v\n\n", waterutil.MACDestination(buffer))
			} else {
				fmt.Printf("unknow Source:      %v\n", waterutil.MACSource(buffer))
				fmt.Printf("unknow Destination: %v\n\n", waterutil.MACDestination(buffer))
			}
		}
	}
}
コード例 #2
0
ファイル: ipv4_test.go プロジェクト: songgao/water
func TestBroadcast(t *testing.T) {
	var (
		self = net.IPv4(10, 0, 42, 1)
		mask = net.IPv4Mask(255, 255, 255, 0)
		brd  = net.IPv4(10, 0, 42, 255)
	)

	ifce, err := NewTAP("test")
	if err != nil {
		t.Fatalf("creating TAP error: %v\n", err)
	}

	setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
	startBroadcast(t, brd)

	dataCh := make(chan []byte, 8)
	startRead(dataCh, ifce)

	timeout := time.NewTimer(8 * time.Second).C

readFrame:
	for {
		select {
		case buffer := <-dataCh:
			ethertype := waterutil.MACEthertype(buffer)
			if ethertype != waterutil.IPv4 {
				continue readFrame
			}
			if !waterutil.IsBroadcast(waterutil.MACDestination(buffer)) {
				continue readFrame
			}
			packet := waterutil.MACPayload(buffer)
			if !waterutil.IsIPv4(packet) {
				continue readFrame
			}
			if !waterutil.IPv4Source(packet).Equal(self) {
				continue readFrame
			}
			if !waterutil.IPv4Destination(packet).Equal(brd) {
				continue readFrame
			}
			if waterutil.IPv4Protocol(packet) != waterutil.ICMP {
				continue readFrame
			}
			t.Logf("received broadcast frame: %#v\n", buffer)
			break readFrame
		case <-timeout:
			t.Fatal("Waiting for broadcast packet timeout")
		}
	}
}