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-06-01 20:50:14 +00:00
|
|
|
|
2014-05-15 03:29:18 +00:00
|
|
|
package beacon
|
2014-03-28 10:04:48 +00:00
|
|
|
|
2015-06-10 08:07:51 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2015-10-20 13:14:05 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2018-09-08 09:56:56 +00:00
|
|
|
"github.com/thejerf/suture"
|
2015-06-10 08:07:51 +00:00
|
|
|
)
|
2014-03-28 10:04:48 +00:00
|
|
|
|
2014-08-17 09:53:26 +00:00
|
|
|
type Broadcast struct {
|
2015-06-10 08:07:51 +00:00
|
|
|
*suture.Supervisor
|
2014-03-28 10:04:48 +00:00
|
|
|
port int
|
|
|
|
inbox chan []byte
|
|
|
|
outbox chan recv
|
2015-09-20 13:30:25 +00:00
|
|
|
br *broadcastReader
|
|
|
|
bw *broadcastWriter
|
2014-03-28 10:04:48 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 08:07:51 +00:00
|
|
|
func NewBroadcast(port int) *Broadcast {
|
2014-08-17 09:53:26 +00:00
|
|
|
b := &Broadcast{
|
2015-06-10 08:07:51 +00:00
|
|
|
Supervisor: suture.New("broadcastBeacon", 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,
|
2015-07-11 01:12:20 +00:00
|
|
|
// Only log restarts in debug mode.
|
|
|
|
Log: func(line string) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(line)
|
2015-07-11 01:12:20 +00:00
|
|
|
},
|
2018-09-08 09:56:56 +00:00
|
|
|
PassThroughPanics: true,
|
2015-06-10 08:07:51 +00:00
|
|
|
}),
|
2014-03-28 10:04:48 +00:00
|
|
|
port: port,
|
|
|
|
inbox: make(chan []byte),
|
2014-05-14 18:26:05 +00:00
|
|
|
outbox: make(chan recv, 16),
|
2014-03-28 10:04:48 +00:00
|
|
|
}
|
2014-05-14 18:26:05 +00:00
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
b.br = &broadcastReader{
|
2015-10-20 13:14:05 +00:00
|
|
|
port: port,
|
|
|
|
outbox: b.outbox,
|
|
|
|
connMut: sync.NewMutex(),
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
b.Add(b.br)
|
|
|
|
b.bw = &broadcastWriter{
|
2015-10-20 13:14:05 +00:00
|
|
|
port: port,
|
|
|
|
inbox: b.inbox,
|
|
|
|
connMut: sync.NewMutex(),
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
b.Add(b.bw)
|
2015-06-10 08:07:51 +00:00
|
|
|
|
|
|
|
return b
|
2014-03-28 10:04:48 +00:00
|
|
|
}
|
|
|
|
|
2014-08-17 09:53:26 +00:00
|
|
|
func (b *Broadcast) Send(data []byte) {
|
2014-03-28 10:04:48 +00:00
|
|
|
b.inbox <- data
|
|
|
|
}
|
|
|
|
|
2014-08-17 09:53:26 +00:00
|
|
|
func (b *Broadcast) Recv() ([]byte, net.Addr) {
|
2014-03-28 10:04:48 +00:00
|
|
|
recv := <-b.outbox
|
|
|
|
return recv.data, recv.src
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
func (b *Broadcast) Error() error {
|
|
|
|
if err := b.br.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.bw.Error()
|
|
|
|
}
|
|
|
|
|
2015-06-10 08:07:51 +00:00
|
|
|
type broadcastWriter struct {
|
2015-10-20 13:14:05 +00:00
|
|
|
port int
|
|
|
|
inbox chan []byte
|
|
|
|
conn *net.UDPConn
|
|
|
|
connMut sync.Mutex
|
2015-09-20 13:30:25 +00:00
|
|
|
errorHolder
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *broadcastWriter) Serve() {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(w, "starting")
|
|
|
|
defer l.Debugln(w, "stopping")
|
2014-03-28 10:04:48 +00:00
|
|
|
|
2015-10-20 13:14:05 +00:00
|
|
|
conn, err := net.ListenUDP("udp4", nil)
|
2015-06-10 08:07:51 +00:00
|
|
|
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)
|
2015-06-10 08:07:51 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-20 13:14:05 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
w.connMut.Lock()
|
|
|
|
w.conn = conn
|
|
|
|
w.connMut.Unlock()
|
2015-06-10 08:07:51 +00:00
|
|
|
|
|
|
|
for bs := range w.inbox {
|
2014-05-14 18:26:05 +00:00
|
|
|
addrs, err := net.InterfaceAddrs()
|
2014-03-28 10:04: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
|
|
|
w.setError(err)
|
2014-05-14 18:26:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var dsts []net.IP
|
|
|
|
for _, addr := range addrs {
|
2014-09-30 15:23:31 +00:00
|
|
|
if iaddr, ok := addr.(*net.IPNet); ok && len(iaddr.IP) >= 4 && iaddr.IP.IsGlobalUnicast() && iaddr.IP.To4() != nil {
|
2014-05-14 18:26:05 +00:00
|
|
|
baddr := bcast(iaddr)
|
|
|
|
dsts = append(dsts, baddr.IP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dsts) == 0 {
|
|
|
|
// Fall back to the general IPv4 broadcast address
|
|
|
|
dsts = append(dsts, net.IP{0xff, 0xff, 0xff, 0xff})
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("addresses:", dsts)
|
2014-05-15 00:08:56 +00:00
|
|
|
|
2015-09-22 14:04:48 +00:00
|
|
|
success := 0
|
2014-05-14 18:26:05 +00:00
|
|
|
for _, ip := range dsts {
|
2015-06-10 08:07:51 +00:00
|
|
|
dst := &net.UDPAddr{IP: ip, Port: w.port}
|
|
|
|
|
2015-10-20 13:14:05 +00:00
|
|
|
conn.SetWriteDeadline(time.Now().Add(time.Second))
|
|
|
|
_, err := conn.WriteTo(bs, dst)
|
|
|
|
conn.SetWriteDeadline(time.Time{})
|
2015-09-22 14:04:48 +00:00
|
|
|
|
2015-06-10 08:07:51 +00:00
|
|
|
if err, ok := err.(net.Error); ok && err.Timeout() {
|
|
|
|
// Write timeouts should not happen. We treat it as a fatal
|
|
|
|
// error on the socket.
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
w.setError(err)
|
2015-06-10 08:07:51 +00:00
|
|
|
return
|
2015-09-22 14:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-10-12 07:13:44 +00:00
|
|
|
// Some other error that we don't expect. Debug and continue.
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2015-09-20 13:30:25 +00:00
|
|
|
w.setError(err)
|
2017-10-12 07:13:44 +00:00
|
|
|
continue
|
2015-09-22 14:04:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("sent %d bytes to %s", len(bs), dst)
|
2015-09-22 14:04:48 +00:00
|
|
|
success++
|
|
|
|
}
|
|
|
|
|
|
|
|
if success > 0 {
|
|
|
|
w.setError(nil)
|
2014-03-28 10:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 18:26:05 +00:00
|
|
|
}
|
2014-03-28 10:04:48 +00:00
|
|
|
|
2015-06-10 08:07:51 +00:00
|
|
|
func (w *broadcastWriter) Stop() {
|
2015-10-20 13:14:05 +00:00
|
|
|
w.connMut.Lock()
|
|
|
|
if w.conn != nil {
|
|
|
|
w.conn.Close()
|
|
|
|
}
|
|
|
|
w.connMut.Unlock()
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *broadcastWriter) String() string {
|
|
|
|
return fmt.Sprintf("broadcastWriter@%p", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
type broadcastReader struct {
|
2015-10-20 13:14:05 +00:00
|
|
|
port int
|
|
|
|
outbox chan recv
|
|
|
|
conn *net.UDPConn
|
|
|
|
connMut sync.Mutex
|
2015-09-20 13:30:25 +00:00
|
|
|
errorHolder
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *broadcastReader) Serve() {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(r, "starting")
|
|
|
|
defer l.Debugln(r, "stopping")
|
2015-06-10 08:07:51 +00:00
|
|
|
|
2015-10-20 13:14:05 +00:00
|
|
|
conn, err := net.ListenUDP("udp4", &net.UDPAddr{Port: r.port})
|
2015-06-10 08:07:51 +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)
|
2015-06-10 08:07:51 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-20 13:14:05 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
r.connMut.Lock()
|
|
|
|
r.conn = conn
|
|
|
|
r.connMut.Unlock()
|
2015-06-10 08:07:51 +00:00
|
|
|
|
|
|
|
bs := make([]byte, 65536)
|
|
|
|
for {
|
2015-10-20 13:14:05 +00:00
|
|
|
n, addr, err := conn.ReadFrom(bs)
|
2015-06-10 08:07:51 +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)
|
2015-06-10 08:07:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
r.setError(nil)
|
2015-06-10 08:07:51 +00:00
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("recv %d bytes from %s", n, addr)
|
2015-06-10 08:07:51 +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")
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *broadcastReader) Stop() {
|
2015-10-20 13:14:05 +00:00
|
|
|
r.connMut.Lock()
|
|
|
|
if r.conn != nil {
|
|
|
|
r.conn.Close()
|
|
|
|
}
|
|
|
|
r.connMut.Unlock()
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *broadcastReader) String() string {
|
|
|
|
return fmt.Sprintf("broadcastReader@%p", r)
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:26:05 +00:00
|
|
|
func bcast(ip *net.IPNet) *net.IPNet {
|
|
|
|
var bc = &net.IPNet{}
|
|
|
|
bc.IP = make([]byte, len(ip.IP))
|
|
|
|
copy(bc.IP, ip.IP)
|
|
|
|
bc.Mask = ip.Mask
|
2014-03-28 10:04:48 +00:00
|
|
|
|
2014-05-14 18:26:05 +00:00
|
|
|
offset := len(bc.IP) - len(bc.Mask)
|
|
|
|
for i := range bc.IP {
|
2014-07-31 11:39:49 +00:00
|
|
|
if i-offset >= 0 {
|
2014-05-14 18:26:05 +00:00
|
|
|
bc.IP[i] = ip.IP[i] | ^ip.Mask[i-offset]
|
2014-03-28 10:04:48 +00:00
|
|
|
}
|
2014-05-14 18:26:05 +00:00
|
|
|
}
|
|
|
|
return bc
|
2014-03-28 10:04:48 +00:00
|
|
|
}
|