123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- // Copyright 2013 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
-
- package ipv6_test
-
- import (
- "bytes"
- "net"
- "os"
- "runtime"
- "testing"
- "time"
-
- "golang.org/x/net/icmp"
- "golang.org/x/net/internal/iana"
- "golang.org/x/net/internal/nettest"
- "golang.org/x/net/ipv6"
- )
-
- func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
- switch runtime.GOOS {
- case "nacl", "plan9", "windows":
- t.Skipf("not supported on %s", runtime.GOOS)
- }
- if !supportsIPv6 {
- t.Skip("ipv6 is not supported")
- }
-
- c, err := nettest.NewLocalPacketListener("udp6")
- if err != nil {
- t.Fatal(err)
- }
- defer c.Close()
- p := ipv6.NewPacketConn(c)
- defer p.Close()
-
- dst := c.LocalAddr()
- cm := ipv6.ControlMessage{
- TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
- Src: net.IPv6loopback,
- }
- cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
- ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
- if ifi != nil {
- cm.IfIndex = ifi.Index
- }
- wb := []byte("HELLO-R-U-THERE")
-
- for i, toggle := range []bool{true, false, true} {
- if err := p.SetControlMessage(cf, toggle); err != nil {
- if nettest.ProtocolNotSupported(err) {
- t.Logf("not supported on %s", runtime.GOOS)
- continue
- }
- t.Fatal(err)
- }
- cm.HopLimit = i + 1
- if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
- t.Fatal(err)
- }
- if n, err := p.WriteTo(wb, &cm, dst); err != nil {
- t.Fatal(err)
- } else if n != len(wb) {
- t.Fatalf("got %v; want %v", n, len(wb))
- }
- rb := make([]byte, 128)
- if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
- t.Fatal(err)
- }
- if n, _, _, err := p.ReadFrom(rb); err != nil {
- t.Fatal(err)
- } else if !bytes.Equal(rb[:n], wb) {
- t.Fatalf("got %v; want %v", rb[:n], wb)
- }
- }
- }
-
- func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
- switch runtime.GOOS {
- case "nacl", "plan9", "windows":
- t.Skipf("not supported on %s", runtime.GOOS)
- }
- if !supportsIPv6 {
- t.Skip("ipv6 is not supported")
- }
- if m, ok := nettest.SupportsRawIPSocket(); !ok {
- t.Skip(m)
- }
-
- c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")
- if err != nil {
- t.Fatal(err)
- }
- defer c.Close()
- p := ipv6.NewPacketConn(c)
- defer p.Close()
-
- dst, err := net.ResolveIPAddr("ip6", "::1")
- if err != nil {
- t.Fatal(err)
- }
-
- pshicmp := icmp.IPv6PseudoHeader(c.LocalAddr().(*net.IPAddr).IP, dst.IP)
- cm := ipv6.ControlMessage{
- TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
- Src: net.IPv6loopback,
- }
- cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
- ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
- if ifi != nil {
- cm.IfIndex = ifi.Index
- }
-
- var f ipv6.ICMPFilter
- f.SetAll(true)
- f.Accept(ipv6.ICMPTypeEchoReply)
- if err := p.SetICMPFilter(&f); err != nil {
- t.Fatal(err)
- }
-
- var psh []byte
- for i, toggle := range []bool{true, false, true} {
- if toggle {
- psh = nil
- if err := p.SetChecksum(true, 2); err != nil {
- // Solaris never allows to modify
- // ICMP properties.
- if runtime.GOOS != "solaris" {
- t.Fatal(err)
- }
- }
- } else {
- psh = pshicmp
- // Some platforms never allow to disable the
- // kernel checksum processing.
- p.SetChecksum(false, -1)
- }
- wb, err := (&icmp.Message{
- Type: ipv6.ICMPTypeEchoRequest, Code: 0,
- Body: &icmp.Echo{
- ID: os.Getpid() & 0xffff, Seq: i + 1,
- Data: []byte("HELLO-R-U-THERE"),
- },
- }).Marshal(psh)
- if err != nil {
- t.Fatal(err)
- }
- if err := p.SetControlMessage(cf, toggle); err != nil {
- if nettest.ProtocolNotSupported(err) {
- t.Logf("not supported on %s", runtime.GOOS)
- continue
- }
- t.Fatal(err)
- }
- cm.HopLimit = i + 1
- if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
- t.Fatal(err)
- }
- if n, err := p.WriteTo(wb, &cm, dst); err != nil {
- t.Fatal(err)
- } else if n != len(wb) {
- t.Fatalf("got %v; want %v", n, len(wb))
- }
- rb := make([]byte, 128)
- if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
- t.Fatal(err)
- }
- if n, _, _, err := p.ReadFrom(rb); err != nil {
- switch runtime.GOOS {
- case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
- t.Logf("not supported on %s", runtime.GOOS)
- continue
- }
- t.Fatal(err)
- } else {
- if m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, rb[:n]); err != nil {
- t.Fatal(err)
- } else if m.Type != ipv6.ICMPTypeEchoReply || m.Code != 0 {
- t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv6.ICMPTypeEchoReply, 0)
- }
- }
- }
- }
|