multicast.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package beacon
  7. import (
  8. "errors"
  9. "fmt"
  10. "net"
  11. "time"
  12. "github.com/thejerf/suture"
  13. "golang.org/x/net/ipv6"
  14. )
  15. type Multicast struct {
  16. *suture.Supervisor
  17. inbox chan []byte
  18. outbox chan recv
  19. mr *multicastReader
  20. mw *multicastWriter
  21. }
  22. func NewMulticast(addr string) *Multicast {
  23. m := &Multicast{
  24. Supervisor: suture.New("multicastBeacon", suture.Spec{
  25. // Don't retry too frenetically: an error to open a socket or
  26. // whatever is usually something that is either permanent or takes
  27. // a while to get solved...
  28. FailureThreshold: 2,
  29. FailureBackoff: 60 * time.Second,
  30. // Only log restarts in debug mode.
  31. Log: func(line string) {
  32. l.Debugln(line)
  33. },
  34. PassThroughPanics: true,
  35. }),
  36. inbox: make(chan []byte),
  37. outbox: make(chan recv, 16),
  38. }
  39. m.mr = &multicastReader{
  40. addr: addr,
  41. outbox: m.outbox,
  42. stop: make(chan struct{}),
  43. }
  44. m.Add(m.mr)
  45. m.mw = &multicastWriter{
  46. addr: addr,
  47. inbox: m.inbox,
  48. stop: make(chan struct{}),
  49. }
  50. m.Add(m.mw)
  51. return m
  52. }
  53. func (m *Multicast) Send(data []byte) {
  54. m.inbox <- data
  55. }
  56. func (m *Multicast) Recv() ([]byte, net.Addr) {
  57. recv := <-m.outbox
  58. return recv.data, recv.src
  59. }
  60. func (m *Multicast) Error() error {
  61. if err := m.mr.Error(); err != nil {
  62. return err
  63. }
  64. return m.mw.Error()
  65. }
  66. type multicastWriter struct {
  67. addr string
  68. inbox <-chan []byte
  69. errorHolder
  70. stop chan struct{}
  71. }
  72. func (w *multicastWriter) Serve() {
  73. l.Debugln(w, "starting")
  74. defer l.Debugln(w, "stopping")
  75. gaddr, err := net.ResolveUDPAddr("udp6", w.addr)
  76. if err != nil {
  77. l.Debugln(err)
  78. w.setError(err)
  79. return
  80. }
  81. conn, err := net.ListenPacket("udp6", ":0")
  82. if err != nil {
  83. l.Debugln(err)
  84. w.setError(err)
  85. return
  86. }
  87. pconn := ipv6.NewPacketConn(conn)
  88. wcm := &ipv6.ControlMessage{
  89. HopLimit: 1,
  90. }
  91. for bs := range w.inbox {
  92. intfs, err := net.Interfaces()
  93. if err != nil {
  94. l.Debugln(err)
  95. w.setError(err)
  96. return
  97. }
  98. success := 0
  99. for _, intf := range intfs {
  100. wcm.IfIndex = intf.Index
  101. pconn.SetWriteDeadline(time.Now().Add(time.Second))
  102. _, err = pconn.WriteTo(bs, wcm, gaddr)
  103. pconn.SetWriteDeadline(time.Time{})
  104. if err != nil {
  105. l.Debugln(err, "on write to", gaddr, intf.Name)
  106. w.setError(err)
  107. continue
  108. }
  109. l.Debugf("sent %d bytes to %v on %s", len(bs), gaddr, intf.Name)
  110. success++
  111. }
  112. if success > 0 {
  113. w.setError(nil)
  114. } else {
  115. l.Debugln(err)
  116. w.setError(err)
  117. }
  118. }
  119. }
  120. func (w *multicastWriter) Stop() {
  121. close(w.stop)
  122. }
  123. func (w *multicastWriter) String() string {
  124. return fmt.Sprintf("multicastWriter@%p", w)
  125. }
  126. type multicastReader struct {
  127. addr string
  128. outbox chan<- recv
  129. errorHolder
  130. stop chan struct{}
  131. }
  132. func (r *multicastReader) Serve() {
  133. l.Debugln(r, "starting")
  134. defer l.Debugln(r, "stopping")
  135. gaddr, err := net.ResolveUDPAddr("udp6", r.addr)
  136. if err != nil {
  137. l.Debugln(err)
  138. r.setError(err)
  139. return
  140. }
  141. conn, err := net.ListenPacket("udp6", r.addr)
  142. if err != nil {
  143. l.Debugln(err)
  144. r.setError(err)
  145. return
  146. }
  147. intfs, err := net.Interfaces()
  148. if err != nil {
  149. l.Debugln(err)
  150. r.setError(err)
  151. return
  152. }
  153. pconn := ipv6.NewPacketConn(conn)
  154. joined := 0
  155. for _, intf := range intfs {
  156. err := pconn.JoinGroup(&intf, &net.UDPAddr{IP: gaddr.IP})
  157. if err != nil {
  158. l.Debugln("IPv6 join", intf.Name, "failed:", err)
  159. } else {
  160. l.Debugln("IPv6 join", intf.Name, "success")
  161. }
  162. joined++
  163. }
  164. if joined == 0 {
  165. l.Debugln("no multicast interfaces available")
  166. r.setError(errors.New("no multicast interfaces available"))
  167. return
  168. }
  169. bs := make([]byte, 65536)
  170. for {
  171. n, _, addr, err := pconn.ReadFrom(bs)
  172. if err != nil {
  173. l.Debugln(err)
  174. r.setError(err)
  175. continue
  176. }
  177. l.Debugf("recv %d bytes from %s", n, addr)
  178. c := make([]byte, n)
  179. copy(c, bs)
  180. select {
  181. case r.outbox <- recv{c, addr}:
  182. default:
  183. l.Debugln("dropping message")
  184. }
  185. }
  186. }
  187. func (r *multicastReader) Stop() {
  188. close(r.stop)
  189. }
  190. func (r *multicastReader) String() string {
  191. return fmt.Sprintf("multicastReader@%p", r)
  192. }