unicast_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ipv6_test
  5. import (
  6. "bytes"
  7. "net"
  8. "os"
  9. "runtime"
  10. "testing"
  11. "time"
  12. "golang.org/x/net/icmp"
  13. "golang.org/x/net/internal/iana"
  14. "golang.org/x/net/internal/nettest"
  15. "golang.org/x/net/ipv6"
  16. )
  17. func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
  18. switch runtime.GOOS {
  19. case "nacl", "plan9", "windows":
  20. t.Skipf("not supported on %s", runtime.GOOS)
  21. }
  22. if !supportsIPv6 {
  23. t.Skip("ipv6 is not supported")
  24. }
  25. c, err := net.ListenPacket("udp6", "[::1]:0")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. defer c.Close()
  30. p := ipv6.NewPacketConn(c)
  31. defer p.Close()
  32. dst, err := net.ResolveUDPAddr("udp6", c.LocalAddr().String())
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. cm := ipv6.ControlMessage{
  37. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  38. Src: net.IPv6loopback,
  39. }
  40. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  41. ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  42. if ifi != nil {
  43. cm.IfIndex = ifi.Index
  44. }
  45. wb := []byte("HELLO-R-U-THERE")
  46. for i, toggle := range []bool{true, false, true} {
  47. if err := p.SetControlMessage(cf, toggle); err != nil {
  48. if nettest.ProtocolNotSupported(err) {
  49. t.Skipf("not supported on %s", runtime.GOOS)
  50. }
  51. t.Fatal(err)
  52. }
  53. cm.HopLimit = i + 1
  54. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  55. t.Fatal(err)
  56. }
  57. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  58. t.Fatal(err)
  59. } else if n != len(wb) {
  60. t.Fatalf("got %v; want %v", n, len(wb))
  61. }
  62. rb := make([]byte, 128)
  63. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  64. t.Fatal(err)
  65. }
  66. if n, _, _, err := p.ReadFrom(rb); err != nil {
  67. t.Fatal(err)
  68. } else if !bytes.Equal(rb[:n], wb) {
  69. t.Fatalf("got %v; want %v", rb[:n], wb)
  70. }
  71. }
  72. }
  73. func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
  74. switch runtime.GOOS {
  75. case "nacl", "plan9", "windows":
  76. t.Skipf("not supported on %s", runtime.GOOS)
  77. }
  78. if !supportsIPv6 {
  79. t.Skip("ipv6 is not supported")
  80. }
  81. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  82. t.Skip(m)
  83. }
  84. c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. defer c.Close()
  89. p := ipv6.NewPacketConn(c)
  90. defer p.Close()
  91. dst, err := net.ResolveIPAddr("ip6", "::1")
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. pshicmp := icmp.IPv6PseudoHeader(c.LocalAddr().(*net.IPAddr).IP, dst.IP)
  96. cm := ipv6.ControlMessage{
  97. TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced,
  98. Src: net.IPv6loopback,
  99. }
  100. cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU
  101. ifi := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback)
  102. if ifi != nil {
  103. cm.IfIndex = ifi.Index
  104. }
  105. var f ipv6.ICMPFilter
  106. f.SetAll(true)
  107. f.Accept(ipv6.ICMPTypeEchoReply)
  108. if err := p.SetICMPFilter(&f); err != nil {
  109. t.Fatal(err)
  110. }
  111. var psh []byte
  112. for i, toggle := range []bool{true, false, true} {
  113. if toggle {
  114. psh = nil
  115. if err := p.SetChecksum(true, 2); err != nil {
  116. // Solaris never allows to modify
  117. // ICMP properties.
  118. if runtime.GOOS != "solaris" {
  119. t.Fatal(err)
  120. }
  121. }
  122. } else {
  123. psh = pshicmp
  124. // Some platforms never allow to disable the
  125. // kernel checksum processing.
  126. p.SetChecksum(false, -1)
  127. }
  128. wb, err := (&icmp.Message{
  129. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  130. Body: &icmp.Echo{
  131. ID: os.Getpid() & 0xffff, Seq: i + 1,
  132. Data: []byte("HELLO-R-U-THERE"),
  133. },
  134. }).Marshal(psh)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. if err := p.SetControlMessage(cf, toggle); err != nil {
  139. if nettest.ProtocolNotSupported(err) {
  140. t.Skipf("not supported on %s", runtime.GOOS)
  141. }
  142. t.Fatal(err)
  143. }
  144. cm.HopLimit = i + 1
  145. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  146. t.Fatal(err)
  147. }
  148. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  149. t.Fatal(err)
  150. } else if n != len(wb) {
  151. t.Fatalf("got %v; want %v", n, len(wb))
  152. }
  153. rb := make([]byte, 128)
  154. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  155. t.Fatal(err)
  156. }
  157. if n, _, _, err := p.ReadFrom(rb); err != nil {
  158. switch runtime.GOOS {
  159. case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
  160. t.Logf("not supported on %s", runtime.GOOS)
  161. continue
  162. }
  163. t.Fatal(err)
  164. } else {
  165. if m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, rb[:n]); err != nil {
  166. t.Fatal(err)
  167. } else if m.Type != ipv6.ICMPTypeEchoReply || m.Code != 0 {
  168. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv6.ICMPTypeEchoReply, 0)
  169. }
  170. }
  171. }
  172. }