multicast_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. 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 "nacl", "plan9", "solaris", "windows":
  27. t.Skipf("not supported on %s", runtime.GOOS)
  28. }
  29. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  30. if ifi == 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 nettest.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 "nacl", "plan9", "solaris", "windows":
  109. t.Skipf("not supported on %s", runtime.GOOS)
  110. }
  111. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  112. t.Skip(m)
  113. }
  114. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  115. if ifi == 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 != "solaris" {
  157. // Solaris never allows to modify 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 nettest.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. if testing.Short() {
  213. t.Skip("to avoid external network")
  214. }
  215. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  216. t.Skip(m)
  217. }
  218. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagMulticast|net.FlagLoopback)
  219. if ifi == nil {
  220. t.Skipf("not available on %s", runtime.GOOS)
  221. }
  222. for _, tt := range rawConnReadWriteMulticastICMPTests {
  223. c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. defer c.Close()
  228. r, err := ipv4.NewRawConn(c)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. defer r.Close()
  233. if tt.src == nil {
  234. if err := r.JoinGroup(ifi, tt.grp); err != nil {
  235. t.Fatal(err)
  236. }
  237. defer r.LeaveGroup(ifi, tt.grp)
  238. } else {
  239. if err := r.JoinSourceSpecificGroup(ifi, tt.grp, tt.src); err != nil {
  240. switch runtime.GOOS {
  241. case "freebsd", "linux":
  242. default: // platforms that don't support IGMPv2/3 fail here
  243. t.Logf("not supported on %s", runtime.GOOS)
  244. continue
  245. }
  246. t.Fatal(err)
  247. }
  248. defer r.LeaveSourceSpecificGroup(ifi, tt.grp, tt.src)
  249. }
  250. if err := r.SetMulticastInterface(ifi); err != nil {
  251. t.Fatal(err)
  252. }
  253. if _, err := r.MulticastInterface(); err != nil {
  254. t.Fatal(err)
  255. }
  256. if err := r.SetMulticastLoopback(true); err != nil {
  257. t.Fatal(err)
  258. }
  259. if _, err := r.MulticastLoopback(); err != nil {
  260. t.Fatal(err)
  261. }
  262. cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
  263. for i, toggle := range []bool{true, false, true} {
  264. wb, err := (&icmp.Message{
  265. Type: ipv4.ICMPTypeEcho, Code: 0,
  266. Body: &icmp.Echo{
  267. ID: os.Getpid() & 0xffff, Seq: i + 1,
  268. Data: []byte("HELLO-R-U-THERE"),
  269. },
  270. }).Marshal(nil)
  271. if err != nil {
  272. t.Fatal(err)
  273. }
  274. wh := &ipv4.Header{
  275. Version: ipv4.Version,
  276. Len: ipv4.HeaderLen,
  277. TOS: i + 1,
  278. TotalLen: ipv4.HeaderLen + len(wb),
  279. Protocol: 1,
  280. Dst: tt.grp.IP,
  281. }
  282. if err := r.SetControlMessage(cf, toggle); err != nil {
  283. if nettest.ProtocolNotSupported(err) {
  284. t.Logf("not supported on %s", runtime.GOOS)
  285. continue
  286. }
  287. t.Fatal(err)
  288. }
  289. if err := r.SetDeadline(time.Now().Add(200 * time.Millisecond)); err != nil {
  290. t.Fatal(err)
  291. }
  292. r.SetMulticastTTL(i + 1)
  293. if err := r.WriteTo(wh, wb, nil); err != nil {
  294. t.Fatal(err)
  295. }
  296. rb := make([]byte, ipv4.HeaderLen+128)
  297. if rh, b, _, err := r.ReadFrom(rb); err != nil {
  298. t.Fatal(err)
  299. } else {
  300. m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
  301. if err != nil {
  302. t.Fatal(err)
  303. }
  304. switch {
  305. case (rh.Dst.IsLoopback() || rh.Dst.IsLinkLocalUnicast() || rh.Dst.IsGlobalUnicast()) && m.Type == ipv4.ICMPTypeEchoReply && m.Code == 0: // net.inet.icmp.bmcastecho=1
  306. case rh.Dst.IsMulticast() && m.Type == ipv4.ICMPTypeEcho && m.Code == 0: // net.inet.icmp.bmcastecho=0
  307. default:
  308. t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv4.ICMPTypeEchoReply, 0)
  309. }
  310. }
  311. }
  312. }
  313. }