unicast_test.go 6.3 KB

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