25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

300 satır
6.5 KiB

  1. // Copyright 2014 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 icmp_test
  5. import (
  6. "errors"
  7. "flag"
  8. "fmt"
  9. "net"
  10. "os"
  11. "runtime"
  12. "sync"
  13. "testing"
  14. "time"
  15. "golang.org/x/net/icmp"
  16. "golang.org/x/net/internal/iana"
  17. "golang.org/x/net/internal/nettest"
  18. "golang.org/x/net/ipv4"
  19. "golang.org/x/net/ipv6"
  20. )
  21. var testDiag = flag.Bool("diag", false, "whether to test ICMP message exchange with external network")
  22. type diagTest struct {
  23. network, address string
  24. protocol int
  25. m icmp.Message
  26. }
  27. func TestDiag(t *testing.T) {
  28. if !*testDiag {
  29. t.Skip("avoid external network")
  30. }
  31. t.Run("Ping/NonPrivileged", func(t *testing.T) {
  32. if m, ok := supportsNonPrivilegedICMP(); !ok {
  33. t.Skip(m)
  34. }
  35. for i, dt := range []diagTest{
  36. {
  37. "udp4", "0.0.0.0", iana.ProtocolICMP,
  38. icmp.Message{
  39. Type: ipv4.ICMPTypeEcho, Code: 0,
  40. Body: &icmp.Echo{
  41. ID: os.Getpid() & 0xffff,
  42. Data: []byte("HELLO-R-U-THERE"),
  43. },
  44. },
  45. },
  46. {
  47. "udp6", "::", iana.ProtocolIPv6ICMP,
  48. icmp.Message{
  49. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  50. Body: &icmp.Echo{
  51. ID: os.Getpid() & 0xffff,
  52. Data: []byte("HELLO-R-U-THERE"),
  53. },
  54. },
  55. },
  56. } {
  57. if err := doDiag(dt, i); err != nil {
  58. t.Error(err)
  59. }
  60. }
  61. })
  62. t.Run("Ping/Privileged", func(t *testing.T) {
  63. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  64. t.Skip(m)
  65. }
  66. for i, dt := range []diagTest{
  67. {
  68. "ip4:icmp", "0.0.0.0", iana.ProtocolICMP,
  69. icmp.Message{
  70. Type: ipv4.ICMPTypeEcho, Code: 0,
  71. Body: &icmp.Echo{
  72. ID: os.Getpid() & 0xffff,
  73. Data: []byte("HELLO-R-U-THERE"),
  74. },
  75. },
  76. },
  77. {
  78. "ip6:ipv6-icmp", "::", iana.ProtocolIPv6ICMP,
  79. icmp.Message{
  80. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  81. Body: &icmp.Echo{
  82. ID: os.Getpid() & 0xffff,
  83. Data: []byte("HELLO-R-U-THERE"),
  84. },
  85. },
  86. },
  87. } {
  88. if err := doDiag(dt, i); err != nil {
  89. t.Error(err)
  90. }
  91. }
  92. })
  93. t.Run("Probe/Privileged", func(t *testing.T) {
  94. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  95. t.Skip(m)
  96. }
  97. for i, dt := range []diagTest{
  98. {
  99. "ip4:icmp", "0.0.0.0", iana.ProtocolICMP,
  100. icmp.Message{
  101. Type: ipv4.ICMPTypeExtendedEchoRequest, Code: 0,
  102. Body: &icmp.ExtendedEchoRequest{
  103. ID: os.Getpid() & 0xffff,
  104. Local: true,
  105. Extensions: []icmp.Extension{
  106. &icmp.InterfaceIdent{
  107. Class: 3, Type: 1,
  108. Name: "doesnotexist",
  109. },
  110. },
  111. },
  112. },
  113. },
  114. {
  115. "ip6:ipv6-icmp", "::", iana.ProtocolIPv6ICMP,
  116. icmp.Message{
  117. Type: ipv6.ICMPTypeExtendedEchoRequest, Code: 0,
  118. Body: &icmp.ExtendedEchoRequest{
  119. ID: os.Getpid() & 0xffff,
  120. Local: true,
  121. Extensions: []icmp.Extension{
  122. &icmp.InterfaceIdent{
  123. Class: 3, Type: 1,
  124. Name: "doesnotexist",
  125. },
  126. },
  127. },
  128. },
  129. },
  130. } {
  131. if err := doDiag(dt, i); err != nil {
  132. t.Error(err)
  133. }
  134. }
  135. })
  136. }
  137. func doDiag(dt diagTest, seq int) error {
  138. c, err := icmp.ListenPacket(dt.network, dt.address)
  139. if err != nil {
  140. return err
  141. }
  142. defer c.Close()
  143. dst, err := googleAddr(c, dt.protocol)
  144. if err != nil {
  145. return err
  146. }
  147. if dt.network != "udp6" && dt.protocol == iana.ProtocolIPv6ICMP {
  148. var f ipv6.ICMPFilter
  149. f.SetAll(true)
  150. f.Accept(ipv6.ICMPTypeDestinationUnreachable)
  151. f.Accept(ipv6.ICMPTypePacketTooBig)
  152. f.Accept(ipv6.ICMPTypeTimeExceeded)
  153. f.Accept(ipv6.ICMPTypeParameterProblem)
  154. f.Accept(ipv6.ICMPTypeEchoReply)
  155. f.Accept(ipv6.ICMPTypeExtendedEchoReply)
  156. if err := c.IPv6PacketConn().SetICMPFilter(&f); err != nil {
  157. return err
  158. }
  159. }
  160. switch m := dt.m.Body.(type) {
  161. case *icmp.Echo:
  162. m.Seq = 1 << uint(seq)
  163. case *icmp.ExtendedEchoRequest:
  164. m.Seq = 1 << uint(seq)
  165. }
  166. wb, err := dt.m.Marshal(nil)
  167. if err != nil {
  168. return err
  169. }
  170. if n, err := c.WriteTo(wb, dst); err != nil {
  171. return err
  172. } else if n != len(wb) {
  173. return fmt.Errorf("got %v; want %v", n, len(wb))
  174. }
  175. rb := make([]byte, 1500)
  176. if err := c.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil {
  177. return err
  178. }
  179. n, peer, err := c.ReadFrom(rb)
  180. if err != nil {
  181. return err
  182. }
  183. rm, err := icmp.ParseMessage(dt.protocol, rb[:n])
  184. if err != nil {
  185. return err
  186. }
  187. switch {
  188. case dt.m.Type == ipv4.ICMPTypeEcho && rm.Type == ipv4.ICMPTypeEchoReply:
  189. fallthrough
  190. case dt.m.Type == ipv6.ICMPTypeEchoRequest && rm.Type == ipv6.ICMPTypeEchoReply:
  191. fallthrough
  192. case dt.m.Type == ipv4.ICMPTypeExtendedEchoRequest && rm.Type == ipv4.ICMPTypeExtendedEchoReply:
  193. fallthrough
  194. case dt.m.Type == ipv6.ICMPTypeExtendedEchoRequest && rm.Type == ipv6.ICMPTypeExtendedEchoReply:
  195. return nil
  196. default:
  197. return fmt.Errorf("got %+v from %v; want echo reply or extended echo reply", rm, peer)
  198. }
  199. }
  200. func googleAddr(c *icmp.PacketConn, protocol int) (net.Addr, error) {
  201. host := "ipv4.google.com"
  202. if protocol == iana.ProtocolIPv6ICMP {
  203. host = "ipv6.google.com"
  204. }
  205. ips, err := net.LookupIP(host)
  206. if err != nil {
  207. return nil, err
  208. }
  209. netaddr := func(ip net.IP) (net.Addr, error) {
  210. switch c.LocalAddr().(type) {
  211. case *net.UDPAddr:
  212. return &net.UDPAddr{IP: ip}, nil
  213. case *net.IPAddr:
  214. return &net.IPAddr{IP: ip}, nil
  215. default:
  216. return nil, errors.New("neither UDPAddr nor IPAddr")
  217. }
  218. }
  219. if len(ips) > 0 {
  220. return netaddr(ips[0])
  221. }
  222. return nil, errors.New("no A or AAAA record")
  223. }
  224. func TestConcurrentNonPrivilegedListenPacket(t *testing.T) {
  225. if testing.Short() {
  226. t.Skip("avoid external network")
  227. }
  228. if m, ok := supportsNonPrivilegedICMP(); !ok {
  229. t.Skip(m)
  230. }
  231. network, address := "udp4", "127.0.0.1"
  232. if !nettest.SupportsIPv4() {
  233. network, address = "udp6", "::1"
  234. }
  235. const N = 1000
  236. var wg sync.WaitGroup
  237. wg.Add(N)
  238. for i := 0; i < N; i++ {
  239. go func() {
  240. defer wg.Done()
  241. c, err := icmp.ListenPacket(network, address)
  242. if err != nil {
  243. t.Error(err)
  244. return
  245. }
  246. c.Close()
  247. }()
  248. }
  249. wg.Wait()
  250. }
  251. var (
  252. nonPrivOnce sync.Once
  253. nonPrivMsg string
  254. nonPrivICMP bool
  255. )
  256. func supportsNonPrivilegedICMP() (string, bool) {
  257. nonPrivOnce.Do(func() {
  258. switch runtime.GOOS {
  259. case "darwin":
  260. nonPrivICMP = true
  261. case "linux":
  262. for _, t := range []struct{ network, address string }{
  263. {"udp4", "127.0.0.1"},
  264. {"udp6", "::1"},
  265. } {
  266. c, err := icmp.ListenPacket(t.network, t.address)
  267. if err != nil {
  268. nonPrivMsg = "you may need to adjust the net.ipv4.ping_group_range kernel state"
  269. return
  270. }
  271. c.Close()
  272. }
  273. nonPrivICMP = true
  274. default:
  275. nonPrivMsg = "not supported on " + runtime.GOOS
  276. }
  277. })
  278. return nonPrivMsg, nonPrivICMP
  279. }