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. }),
  35. inbox: make(chan []byte),
  36. outbox: make(chan recv, 16),
  37. }
  38. m.mr = &multicastReader{
  39. addr: addr,
  40. outbox: m.outbox,
  41. stop: make(chan struct{}),
  42. }
  43. m.Add(m.mr)
  44. m.mw = &multicastWriter{
  45. addr: addr,
  46. inbox: m.inbox,
  47. stop: make(chan struct{}),
  48. }
  49. m.Add(m.mw)
  50. return m
  51. }
  52. func (m *Multicast) Send(data []byte) {
  53. m.inbox <- data
  54. }
  55. func (m *Multicast) Recv() ([]byte, net.Addr) {
  56. recv := <-m.outbox
  57. return recv.data, recv.src
  58. }
  59. func (m *Multicast) Error() error {
  60. if err := m.mr.Error(); err != nil {
  61. return err
  62. }
  63. return m.mw.Error()
  64. }
  65. type multicastWriter struct {
  66. addr string
  67. inbox <-chan []byte
  68. errorHolder
  69. stop chan struct{}
  70. }
  71. func (w *multicastWriter) Serve() {
  72. l.Debugln(w, "starting")
  73. defer l.Debugln(w, "stopping")
  74. gaddr, err := net.ResolveUDPAddr("udp6", w.addr)
  75. if err != nil {
  76. l.Debugln(err)
  77. w.setError(err)
  78. return
  79. }
  80. conn, err := net.ListenPacket("udp6", ":0")
  81. if err != nil {
  82. l.Debugln(err)
  83. w.setError(err)
  84. return
  85. }
  86. pconn := ipv6.NewPacketConn(conn)
  87. wcm := &ipv6.ControlMessage{
  88. HopLimit: 1,
  89. }
  90. for bs := range w.inbox {
  91. intfs, err := net.Interfaces()
  92. if err != nil {
  93. l.Debugln(err)
  94. w.setError(err)
  95. return
  96. }
  97. success := 0
  98. for _, intf := range intfs {
  99. wcm.IfIndex = intf.Index
  100. pconn.SetWriteDeadline(time.Now().Add(time.Second))
  101. _, err = pconn.WriteTo(bs, wcm, gaddr)
  102. pconn.SetWriteDeadline(time.Time{})
  103. if err != nil {
  104. l.Debugln(err, "on write to", gaddr, intf.Name)
  105. w.setError(err)
  106. continue
  107. }
  108. l.Debugf("sent %d bytes to %v on %s", len(bs), gaddr, intf.Name)
  109. success++
  110. }
  111. if success > 0 {
  112. w.setError(nil)
  113. } else {
  114. l.Debugln(err)
  115. w.setError(err)
  116. }
  117. }
  118. }
  119. func (w *multicastWriter) Stop() {
  120. close(w.stop)
  121. }
  122. func (w *multicastWriter) String() string {
  123. return fmt.Sprintf("multicastWriter@%p", w)
  124. }
  125. type multicastReader struct {
  126. addr string
  127. outbox chan<- recv
  128. errorHolder
  129. stop chan struct{}
  130. }
  131. func (r *multicastReader) Serve() {
  132. l.Debugln(r, "starting")
  133. defer l.Debugln(r, "stopping")
  134. gaddr, err := net.ResolveUDPAddr("udp6", r.addr)
  135. if err != nil {
  136. l.Debugln(err)
  137. r.setError(err)
  138. return
  139. }
  140. conn, err := net.ListenPacket("udp6", r.addr)
  141. if err != nil {
  142. l.Debugln(err)
  143. r.setError(err)
  144. return
  145. }
  146. intfs, err := net.Interfaces()
  147. if err != nil {
  148. l.Debugln(err)
  149. r.setError(err)
  150. return
  151. }
  152. pconn := ipv6.NewPacketConn(conn)
  153. joined := 0
  154. for _, intf := range intfs {
  155. err := pconn.JoinGroup(&intf, &net.UDPAddr{IP: gaddr.IP})
  156. if err != nil {
  157. l.Debugln("IPv6 join", intf.Name, "failed:", err)
  158. } else {
  159. l.Debugln("IPv6 join", intf.Name, "success")
  160. }
  161. joined++
  162. }
  163. if joined == 0 {
  164. l.Debugln("no multicast interfaces available")
  165. r.setError(errors.New("no multicast interfaces available"))
  166. return
  167. }
  168. bs := make([]byte, 65536)
  169. for {
  170. n, _, addr, err := pconn.ReadFrom(bs)
  171. if err != nil {
  172. l.Debugln(err)
  173. r.setError(err)
  174. continue
  175. }
  176. l.Debugf("recv %d bytes from %s", n, addr)
  177. c := make([]byte, n)
  178. copy(c, bs)
  179. select {
  180. case r.outbox <- recv{c, addr}:
  181. default:
  182. l.Debugln("dropping message")
  183. }
  184. }
  185. }
  186. func (r *multicastReader) Stop() {
  187. close(r.stop)
  188. }
  189. func (r *multicastReader) String() string {
  190. return fmt.Sprintf("multicastReader@%p", r)
  191. }