multicast_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. var packetConnReadWriteMulticastUDPTests = []struct {
  18. addr string
  19. grp, src *net.UDPAddr
  20. }{
  21. {"224.0.0.0:0", &net.UDPAddr{IP: net.IPv4(224, 0, 0, 254)}, nil}, // see RFC 4727
  22. {"232.0.1.0:0", &net.UDPAddr{IP: net.IPv4(232, 0, 1, 254)}, &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}}, // see RFC 5771
  23. }
  24. func TestPacketConnReadWriteMulticastUDP(t *testing.T) {
  25. switch runtime.GOOS {
  26. case "fuchsia", "hurd", "illumos", "js", "nacl", "plan9", "solaris", "windows":
  27. t.Skipf("not supported on %s", runtime.GOOS)
  28. }
  29. ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  30. if err != nil {
  31. t.Skipf("not available on %s", runtime.GOOS)
  32. }
  33. for _, tt := range packetConnReadWriteMulticastUDPTests {
  34. c, err := net.ListenPacket("udp4", tt.addr)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. defer c.Close()
  39. grp := *tt.grp
  40. grp.Port = c.LocalAddr().(*net.UDPAddr).Port
  41. p := ipv4.NewPacketConn(c)
  42. defer p.Close()
  43. if tt.src == nil {
  44. if err := p.JoinGroup(ifi, &grp); err != nil {
  45. t.Fatal(err)
  46. }
  47. defer p.LeaveGroup(ifi, &grp)
  48. } else {
  49. if err := p.JoinSourceSpecificGroup(ifi, &grp, tt.src); err != nil {
  50. switch runtime.GOOS {
  51. case "freebsd", "linux":
  52. default: // platforms that don't support IGMPv2/3 fail here
  53. t.Logf("not supported on %s", runtime.GOOS)
  54. continue
  55. }
  56. t.Fatal(err)
  57. }
  58. defer p.LeaveSourceSpecificGroup(ifi, &grp, tt.src)
  59. }
  60. if err := p.SetMulticastInterface(ifi); err != nil {
  61. t.Fatal(err)
  62. }
  63. if _, err := p.MulticastInterface(); err != nil {
  64. t.Fatal(err)
  65. }
  66. if err := p.SetMulticastLoopback(true); err != nil {
  67. t.Fatal(err)
  68. }
  69. if _, err := p.MulticastLoopback(); err != nil {
  70. t.Fatal(err)
  71. }
  72. cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
  73. wb := []byte("HELLO-R-U-THERE")
  74. for i, toggle := range []bool{true, false, true} {
  75. if err := p.SetControlMessage(cf, toggle); err != nil {
  76. if protocolNotSupported(err) {
  77. t.Logf("not supported on %s", runtime.GOOS)
  78. continue
  79. }
  80. t.Fatal(err)
  81. }
  82. if err := p.SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
  83. t.Fatal(err)
  84. }
  85. p.SetMulticastTTL(i + 1)
  86. if n, err := p.WriteTo(wb, nil, &grp); err != nil {
  87. t.Fatal(err)
  88. } else if n != len(wb) {
  89. t.Fatalf("got %v; want %v", n, len(wb))
  90. }
  91. rb := make([]byte, 128)
  92. if n, _, _, err := p.ReadFrom(rb); err != nil {
  93. t.Fatal(err)
  94. } else if !bytes.Equal(rb[:n], wb) {
  95. t.Fatalf("got %v; want %v", rb[:n], wb)
  96. }
  97. }
  98. }
  99. }
  100. var packetConnReadWriteMulticastICMPTests = []struct {
  101. grp, src *net.IPAddr
  102. }{
  103. {&net.IPAddr{IP: net.IPv4(224, 0, 0, 254)}, nil}, // see RFC 4727
  104. {&net.IPAddr{IP: net.IPv4(232, 0, 1, 254)}, &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}}, // see RFC 5771
  105. }
  106. func TestPacketConnReadWriteMulticastICMP(t *testing.T) {
  107. switch runtime.GOOS {
  108. case "fuchsia", "hurd", "illumos", "js", "nacl", "plan9", "solaris", "windows":
  109. t.Skipf("not supported on %s", runtime.GOOS)
  110. }
  111. if !nettest.SupportsRawSocket() {
  112. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  113. }
  114. ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  115. if err != nil {
  116. t.Skipf("not available on %s", runtime.GOOS)
  117. }
  118. for _, tt := range packetConnReadWriteMulticastICMPTests {
  119. c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. defer c.Close()
  124. p := ipv4.NewPacketConn(c)
  125. defer p.Close()
  126. if tt.src == nil {
  127. if err := p.JoinGroup(ifi, tt.grp); err != nil {
  128. t.Fatal(err)
  129. }
  130. defer p.LeaveGroup(ifi, tt.grp)
  131. } else {
  132. if err := p.JoinSourceSpecificGroup(ifi, tt.grp, tt.src); err != nil {
  133. switch runtime.GOOS {
  134. case "freebsd", "linux":
  135. default: // platforms that don't support IGMPv2/3 fail here
  136. t.Logf("not supported on %s", runtime.GOOS)
  137. continue
  138. }
  139. t.Fatal(err)
  140. }
  141. defer p.LeaveSourceSpecificGroup(ifi, tt.grp, tt.src)
  142. }
  143. if err := p.SetMulticastInterface(ifi); err != nil {
  144. t.Fatal(err)
  145. }
  146. if _, err := p.MulticastInterface(); err != nil {
  147. t.Fatal(err)
  148. }
  149. if err := p.SetMulticastLoopback(true); err != nil {
  150. t.Fatal(err)
  151. }
  152. if _, err := p.MulticastLoopback(); err != nil {
  153. t.Fatal(err)
  154. }
  155. cf := ipv4.FlagDst | ipv4.FlagInterface
  156. if runtime.GOOS != "illumos" && runtime.GOOS != "solaris" {
  157. // Illumos and Solaris never allow modification of ICMP properties.
  158. cf |= ipv4.FlagTTL
  159. }
  160. for i, toggle := range []bool{true, false, true} {
  161. wb, err := (&icmp.Message{
  162. Type: ipv4.ICMPTypeEcho, Code: 0,
  163. Body: &icmp.Echo{
  164. ID: os.Getpid() & 0xffff, Seq: i + 1,
  165. Data: []byte("HELLO-R-U-THERE"),
  166. },
  167. }).Marshal(nil)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. if err := p.SetControlMessage(cf, toggle); err != nil {
  172. if protocolNotSupported(err) {
  173. t.Logf("not supported on %s", runtime.GOOS)
  174. continue
  175. }
  176. t.Fatal(err)
  177. }
  178. if err := p.SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
  179. t.Fatal(err)
  180. }
  181. p.SetMulticastTTL(i + 1)
  182. if n, err := p.WriteTo(wb, nil, tt.grp); err != nil {
  183. t.Fatal(err)
  184. } else if n != len(wb) {
  185. t.Fatalf("got %v; want %v", n, len(wb))
  186. }
  187. rb := make([]byte, 128)
  188. if n, _, _, err := p.ReadFrom(rb); err != nil {
  189. t.Fatal(err)
  190. } else {
  191. m, err := icmp.ParseMessage(iana.ProtocolICMP, rb[:n])
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. switch {
  196. case m.Type == ipv4.ICMPTypeEchoReply && m.Code == 0: // net.inet.icmp.bmcastecho=1
  197. case m.Type == ipv4.ICMPTypeEcho && m.Code == 0: // net.inet.icmp.bmcastecho=0
  198. default:
  199. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv4.ICMPTypeEchoReply, 0)
  200. }
  201. }
  202. }
  203. }
  204. }
  205. var rawConnReadWriteMulticastICMPTests = []struct {
  206. grp, src *net.IPAddr
  207. }{
  208. {&net.IPAddr{IP: net.IPv4(224, 0, 0, 254)}, nil}, // see RFC 4727
  209. {&net.IPAddr{IP: net.IPv4(232, 0, 1, 254)}, &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}}, // see RFC 5771
  210. }
  211. func TestRawConnReadWriteMulticastICMP(t *testing.T) {
  212. switch runtime.GOOS {
  213. case "fuchsia", "hurd", "illumos", "js", "nacl", "plan9", "solaris", "windows":
  214. t.Skipf("not supported on %s", runtime.GOOS)
  215. }
  216. if testing.Short() {
  217. t.Skip("to avoid external network")
  218. }
  219. if !nettest.SupportsRawSocket() {
  220. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  221. }
  222. ifi, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  223. if err != nil {
  224. t.Skipf("not available on %s", runtime.GOOS)
  225. }
  226. for _, tt := range rawConnReadWriteMulticastICMPTests {
  227. c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. defer c.Close()
  232. r, err := ipv4.NewRawConn(c)
  233. if err != nil {
  234. t.Fatal(err)
  235. }
  236. defer r.Close()
  237. if tt.src == nil {
  238. if err := r.JoinGroup(ifi, tt.grp); err != nil {
  239. t.Fatal(err)
  240. }
  241. defer r.LeaveGroup(ifi, tt.grp)
  242. } else {
  243. if err := r.JoinSourceSpecificGroup(ifi, tt.grp, tt.src); err != nil {
  244. switch runtime.GOOS {
  245. case "freebsd", "linux":
  246. default: // platforms that don't support IGMPv2/3 fail here
  247. t.Logf("not supported on %s", runtime.GOOS)
  248. continue
  249. }
  250. t.Fatal(err)
  251. }
  252. defer r.LeaveSourceSpecificGroup(ifi, tt.grp, tt.src)
  253. }
  254. if err := r.SetMulticastInterface(ifi); err != nil {
  255. t.Fatal(err)
  256. }
  257. if _, err := r.MulticastInterface(); err != nil {
  258. t.Fatal(err)
  259. }
  260. if err := r.SetMulticastLoopback(true); err != nil {
  261. t.Fatal(err)
  262. }
  263. if _, err := r.MulticastLoopback(); err != nil {
  264. t.Fatal(err)
  265. }
  266. cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
  267. for i, toggle := range []bool{true, false, true} {
  268. wb, err := (&icmp.Message{
  269. Type: ipv4.ICMPTypeEcho, Code: 0,
  270. Body: &icmp.Echo{
  271. ID: os.Getpid() & 0xffff, Seq: i + 1,
  272. Data: []byte("HELLO-R-U-THERE"),
  273. },
  274. }).Marshal(nil)
  275. if err != nil {
  276. t.Fatal(err)
  277. }
  278. wh := &ipv4.Header{
  279. Version: ipv4.Version,
  280. Len: ipv4.HeaderLen,
  281. TOS: i + 1,
  282. TotalLen: ipv4.HeaderLen + len(wb),
  283. Protocol: 1,
  284. Dst: tt.grp.IP,
  285. }
  286. if err := r.SetControlMessage(cf, toggle); err != nil {
  287. if protocolNotSupported(err) {
  288. t.Logf("not supported on %s", runtime.GOOS)
  289. continue
  290. }
  291. t.Fatal(err)
  292. }
  293. if err := r.SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
  294. t.Fatal(err)
  295. }
  296. r.SetMulticastTTL(i + 1)
  297. if err := r.WriteTo(wh, wb, nil); err != nil {
  298. t.Fatal(err)
  299. }
  300. rb := make([]byte, ipv4.HeaderLen+128)
  301. if rh, b, _, err := r.ReadFrom(rb); err != nil {
  302. t.Fatal(err)
  303. } else {
  304. m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
  305. if err != nil {
  306. t.Fatal(err)
  307. }
  308. switch {
  309. case (rh.Dst.IsLoopback() || rh.Dst.IsLinkLocalUnicast() || rh.Dst.IsGlobalUnicast()) && m.Type == ipv4.ICMPTypeEchoReply && m.Code == 0: // net.inet.icmp.bmcastecho=1
  310. case rh.Dst.IsMulticast() && m.Type == ipv4.ICMPTypeEcho && m.Code == 0: // net.inet.icmp.bmcastecho=0
  311. default:
  312. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv4.ICMPTypeEchoReply, 0)
  313. }
  314. }
  315. }
  316. }
  317. }