2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-08-17 13:01:48 +00:00
|
|
|
|
|
|
|
package beacon
|
|
|
|
|
2015-08-23 13:02:18 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2015-09-20 13:30:25 +00:00
|
|
|
"fmt"
|
2015-08-23 13:02:18 +00:00
|
|
|
"net"
|
2015-09-20 13:30:25 +00:00
|
|
|
"time"
|
2015-08-23 13:02:18 +00:00
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
"github.com/thejerf/suture"
|
2015-08-23 13:02:18 +00:00
|
|
|
"golang.org/x/net/ipv6"
|
|
|
|
)
|
2014-08-17 13:01:48 +00:00
|
|
|
|
|
|
|
type Multicast struct {
|
2015-09-20 13:30:25 +00:00
|
|
|
*suture.Supervisor
|
2014-08-17 13:01:48 +00:00
|
|
|
inbox chan []byte
|
|
|
|
outbox chan recv
|
2015-09-20 13:30:25 +00:00
|
|
|
mr *multicastReader
|
|
|
|
mw *multicastWriter
|
2014-08-17 13:01:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
func NewMulticast(addr string) *Multicast {
|
|
|
|
m := &Multicast{
|
|
|
|
Supervisor: suture.New("multicastBeacon", suture.Spec{
|
|
|
|
// Don't retry too frenetically: an error to open a socket or
|
|
|
|
// whatever is usually something that is either permanent or takes
|
|
|
|
// a while to get solved...
|
|
|
|
FailureThreshold: 2,
|
|
|
|
FailureBackoff: 60 * time.Second,
|
|
|
|
// Only log restarts in debug mode.
|
|
|
|
Log: func(line string) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(line)
|
2015-09-20 13:30:25 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
inbox: make(chan []byte),
|
|
|
|
outbox: make(chan recv, 16),
|
|
|
|
}
|
|
|
|
|
|
|
|
m.mr = &multicastReader{
|
|
|
|
addr: addr,
|
|
|
|
outbox: m.outbox,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
|
|
|
m.Add(m.mr)
|
|
|
|
|
|
|
|
m.mw = &multicastWriter{
|
|
|
|
addr: addr,
|
|
|
|
inbox: m.inbox,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}
|
|
|
|
m.Add(m.mw)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multicast) Send(data []byte) {
|
|
|
|
m.inbox <- data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multicast) Recv() ([]byte, net.Addr) {
|
|
|
|
recv := <-m.outbox
|
|
|
|
return recv.data, recv.src
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Multicast) Error() error {
|
|
|
|
if err := m.mr.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.mw.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
type multicastWriter struct {
|
|
|
|
addr string
|
|
|
|
inbox <-chan []byte
|
|
|
|
errorHolder
|
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *multicastWriter) Serve() {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(w, "starting")
|
|
|
|
defer l.Debugln(w, "stopping")
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
gaddr, err := net.ResolveUDPAddr("udp6", w.addr)
|
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
w.setError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.ListenPacket("udp6", ":0")
|
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
w.setError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pconn := ipv6.NewPacketConn(conn)
|
|
|
|
|
|
|
|
wcm := &ipv6.ControlMessage{
|
|
|
|
HopLimit: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
for bs := range w.inbox {
|
|
|
|
intfs, err := net.Interfaces()
|
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
w.setError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-22 14:04:48 +00:00
|
|
|
success := 0
|
2015-09-20 13:30:25 +00:00
|
|
|
for _, intf := range intfs {
|
|
|
|
wcm.IfIndex = intf.Index
|
|
|
|
pconn.SetWriteDeadline(time.Now().Add(time.Second))
|
|
|
|
_, err = pconn.WriteTo(bs, wcm, gaddr)
|
|
|
|
pconn.SetWriteDeadline(time.Time{})
|
2015-09-22 14:04:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err, "on write to", gaddr, intf.Name)
|
2015-09-22 14:04:48 +00:00
|
|
|
w.setError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("sent %d bytes to %v on %s", len(bs), gaddr, intf.Name)
|
2015-09-22 14:04:48 +00:00
|
|
|
|
|
|
|
success++
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if success > 0 {
|
|
|
|
w.setError(nil)
|
|
|
|
} else {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
w.setError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *multicastWriter) Stop() {
|
|
|
|
close(w.stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *multicastWriter) String() string {
|
|
|
|
return fmt.Sprintf("multicastWriter@%p", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
type multicastReader struct {
|
|
|
|
addr string
|
|
|
|
outbox chan<- recv
|
|
|
|
errorHolder
|
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *multicastReader) Serve() {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(r, "starting")
|
|
|
|
defer l.Debugln(r, "stopping")
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
gaddr, err := net.ResolveUDPAddr("udp6", r.addr)
|
2014-08-17 13:01:48 +00:00
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
r.setError(err)
|
|
|
|
return
|
2014-08-17 13:01:48 +00:00
|
|
|
}
|
2015-08-23 13:02:18 +00:00
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
conn, err := net.ListenPacket("udp6", r.addr)
|
2015-04-02 19:45:52 +00:00
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
r.setError(err)
|
|
|
|
return
|
2015-04-02 19:45:52 +00:00
|
|
|
}
|
2015-08-23 13:02:18 +00:00
|
|
|
|
|
|
|
intfs, err := net.Interfaces()
|
2014-08-17 13:01:48 +00:00
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
r.setError(err)
|
|
|
|
return
|
2014-08-17 13:01:48 +00:00
|
|
|
}
|
2015-08-23 13:02:18 +00:00
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
pconn := ipv6.NewPacketConn(conn)
|
2015-08-23 13:02:18 +00:00
|
|
|
joined := 0
|
|
|
|
for _, intf := range intfs {
|
2015-09-20 13:30:25 +00:00
|
|
|
err := pconn.JoinGroup(&intf, &net.UDPAddr{IP: gaddr.IP})
|
2015-10-03 15:25:21 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Debugln("IPv6 join", intf.Name, "failed:", err)
|
|
|
|
} else {
|
|
|
|
l.Debugln("IPv6 join", intf.Name, "success")
|
2015-08-23 13:02:18 +00:00
|
|
|
}
|
|
|
|
joined++
|
|
|
|
}
|
|
|
|
|
|
|
|
if joined == 0 {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("no multicast interfaces available")
|
2015-09-20 13:30:25 +00:00
|
|
|
r.setError(errors.New("no multicast interfaces available"))
|
|
|
|
return
|
2014-08-17 13:01:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
bs := make([]byte, 65536)
|
|
|
|
for {
|
|
|
|
n, _, addr, err := pconn.ReadFrom(bs)
|
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
r.setError(err)
|
|
|
|
continue
|
|
|
|
}
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("recv %d bytes from %s", n, addr)
|
2015-08-23 13:02:18 +00:00
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
c := make([]byte, n)
|
|
|
|
copy(c, bs)
|
|
|
|
select {
|
|
|
|
case r.outbox <- recv{c, addr}:
|
|
|
|
default:
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("dropping message")
|
2014-08-17 13:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 13:02:18 +00:00
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
func (r *multicastReader) Stop() {
|
|
|
|
close(r.stop)
|
2015-08-23 13:02:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
func (r *multicastReader) String() string {
|
|
|
|
return fmt.Sprintf("multicastReader@%p", r)
|
2015-08-23 13:02:18 +00:00
|
|
|
}
|