unicast_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2012 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 ipv4_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/ipv4"
  15. "golang.org/x/net/nettest"
  16. )
  17. func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
  18. switch runtime.GOOS {
  19. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  20. t.Skipf("not supported on %s", runtime.GOOS)
  21. }
  22. if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
  23. t.Skipf("not available on %s", runtime.GOOS)
  24. }
  25. c, err := nettest.NewLocalPacketListener("udp4")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. defer c.Close()
  30. p := ipv4.NewPacketConn(c)
  31. defer p.Close()
  32. dst := c.LocalAddr()
  33. cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
  34. wb := []byte("HELLO-R-U-THERE")
  35. for i, toggle := range []bool{true, false, true} {
  36. if err := p.SetControlMessage(cf, toggle); err != nil {
  37. if protocolNotSupported(err) {
  38. t.Logf("not supported on %s", runtime.GOOS)
  39. continue
  40. }
  41. t.Fatal(err)
  42. }
  43. p.SetTTL(i + 1)
  44. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  45. t.Fatal(err)
  46. }
  47. if n, err := p.WriteTo(wb, nil, dst); err != nil {
  48. t.Fatal(err)
  49. } else if n != len(wb) {
  50. t.Fatalf("got %v; want %v", n, len(wb))
  51. }
  52. rb := make([]byte, 128)
  53. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  54. t.Fatal(err)
  55. }
  56. if n, _, _, err := p.ReadFrom(rb); err != nil {
  57. t.Fatal(err)
  58. } else if !bytes.Equal(rb[:n], wb) {
  59. t.Fatalf("got %v; want %v", rb[:n], wb)
  60. }
  61. }
  62. }
  63. func TestPacketConnReadWriteUnicastICMP(t *testing.T) {
  64. switch runtime.GOOS {
  65. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  66. t.Skipf("not supported on %s", runtime.GOOS)
  67. }
  68. if !nettest.SupportsRawSocket() {
  69. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  70. }
  71. if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
  72. t.Skipf("not available on %s", runtime.GOOS)
  73. }
  74. c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. defer c.Close()
  79. dst, err := net.ResolveIPAddr("ip4", "127.0.0.1")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. p := ipv4.NewPacketConn(c)
  84. defer p.Close()
  85. cf := ipv4.FlagDst | ipv4.FlagInterface
  86. if runtime.GOOS != "illumos" && runtime.GOOS != "solaris" {
  87. // Illumos and Solaris never allow modification of ICMP properties.
  88. cf |= ipv4.FlagTTL
  89. }
  90. for i, toggle := range []bool{true, false, true} {
  91. wb, err := (&icmp.Message{
  92. Type: ipv4.ICMPTypeEcho, Code: 0,
  93. Body: &icmp.Echo{
  94. ID: os.Getpid() & 0xffff, Seq: i + 1,
  95. Data: []byte("HELLO-R-U-THERE"),
  96. },
  97. }).Marshal(nil)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. if err := p.SetControlMessage(cf, toggle); err != nil {
  102. if protocolNotSupported(err) {
  103. t.Logf("not supported on %s", runtime.GOOS)
  104. continue
  105. }
  106. t.Fatal(err)
  107. }
  108. p.SetTTL(i + 1)
  109. if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  110. t.Fatal(err)
  111. }
  112. if n, err := p.WriteTo(wb, nil, dst); err != nil {
  113. t.Fatal(err)
  114. } else if n != len(wb) {
  115. t.Fatalf("got %v; want %v", n, len(wb))
  116. }
  117. rb := make([]byte, 128)
  118. loop:
  119. if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  120. t.Fatal(err)
  121. }
  122. if n, _, _, err := p.ReadFrom(rb); err != nil {
  123. switch runtime.GOOS {
  124. case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
  125. t.Logf("not supported on %s", runtime.GOOS)
  126. continue
  127. }
  128. t.Fatal(err)
  129. } else {
  130. m, err := icmp.ParseMessage(iana.ProtocolICMP, rb[:n])
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if runtime.GOOS == "linux" && m.Type == ipv4.ICMPTypeEcho {
  135. // On Linux we must handle own sent packets.
  136. goto loop
  137. }
  138. if m.Type != ipv4.ICMPTypeEchoReply || m.Code != 0 {
  139. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv4.ICMPTypeEchoReply, 0)
  140. }
  141. }
  142. }
  143. }
  144. func TestRawConnReadWriteUnicastICMP(t *testing.T) {
  145. switch runtime.GOOS {
  146. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  147. t.Skipf("not supported on %s", runtime.GOOS)
  148. }
  149. if !nettest.SupportsRawSocket() {
  150. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  151. }
  152. if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil {
  153. t.Skipf("not available on %s", runtime.GOOS)
  154. }
  155. c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. defer c.Close()
  160. dst, err := net.ResolveIPAddr("ip4", "127.0.0.1")
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. r, err := ipv4.NewRawConn(c)
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. defer r.Close()
  169. cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
  170. for i, toggle := range []bool{true, false, true} {
  171. wb, err := (&icmp.Message{
  172. Type: ipv4.ICMPTypeEcho, Code: 0,
  173. Body: &icmp.Echo{
  174. ID: os.Getpid() & 0xffff, Seq: i + 1,
  175. Data: []byte("HELLO-R-U-THERE"),
  176. },
  177. }).Marshal(nil)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. wh := &ipv4.Header{
  182. Version: ipv4.Version,
  183. Len: ipv4.HeaderLen,
  184. TOS: i + 1,
  185. TotalLen: ipv4.HeaderLen + len(wb),
  186. TTL: i + 1,
  187. Protocol: 1,
  188. Dst: dst.IP,
  189. }
  190. if err := r.SetControlMessage(cf, toggle); err != nil {
  191. if protocolNotSupported(err) {
  192. t.Logf("not supported on %s", runtime.GOOS)
  193. continue
  194. }
  195. t.Fatal(err)
  196. }
  197. if err := r.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  198. t.Fatal(err)
  199. }
  200. if err := r.WriteTo(wh, wb, nil); err != nil {
  201. t.Fatal(err)
  202. }
  203. rb := make([]byte, ipv4.HeaderLen+128)
  204. loop:
  205. if err := r.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
  206. t.Fatal(err)
  207. }
  208. if _, b, _, err := r.ReadFrom(rb); err != nil {
  209. switch runtime.GOOS {
  210. case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket
  211. t.Logf("not supported on %s", runtime.GOOS)
  212. continue
  213. }
  214. t.Fatal(err)
  215. } else {
  216. m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
  217. if err != nil {
  218. t.Fatal(err)
  219. }
  220. if runtime.GOOS == "linux" && m.Type == ipv4.ICMPTypeEcho {
  221. // On Linux we must handle own sent packets.
  222. goto loop
  223. }
  224. if m.Type != ipv4.ICMPTypeEchoReply || m.Code != 0 {
  225. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv4.ICMPTypeEchoReply, 0)
  226. }
  227. }
  228. }
  229. }