2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-05-14 20:29:18 -07:00
|
|
|
package beacon
|
2014-03-28 03:04:48 -07:00
|
|
|
|
2015-06-10 01:07:51 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/thejerf/suture"
|
|
|
|
)
|
2014-03-28 03:04:48 -07:00
|
|
|
|
2014-08-17 02:53:26 -07:00
|
|
|
type Broadcast struct {
|
2015-06-10 01:07:51 -07:00
|
|
|
*suture.Supervisor
|
2014-03-28 03:04:48 -07:00
|
|
|
port int
|
|
|
|
inbox chan []byte
|
|
|
|
outbox chan recv
|
2015-09-20 06:30:25 -07:00
|
|
|
br *broadcastReader
|
|
|
|
bw *broadcastWriter
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
|
|
|
|
2015-06-10 01:07:51 -07:00
|
|
|
func NewBroadcast(port int) *Broadcast {
|
2014-08-17 02:53:26 -07:00
|
|
|
b := &Broadcast{
|
2015-06-10 01:07:51 -07: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-10 18:12:20 -07:00
|
|
|
// Only log restarts in debug mode.
|
|
|
|
Log: func(line string) {
|
|
|
|
if debug {
|
|
|
|
l.Debugln(line)
|
|
|
|
}
|
|
|
|
},
|
2015-06-10 01:07:51 -07:00
|
|
|
}),
|
2014-03-28 03:04:48 -07:00
|
|
|
port: port,
|
|
|
|
inbox: make(chan []byte),
|
2014-05-14 11:26:05 -07:00
|
|
|
outbox: make(chan recv, 16),
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
2014-05-14 11:26:05 -07:00
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
b.br = &broadcastReader{
|
2015-06-10 01:07:51 -07:00
|
|
|
port: port,
|
|
|
|
outbox: b.outbox,
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
|
|
|
b.Add(b.br)
|
|
|
|
b.bw = &broadcastWriter{
|
2015-06-10 01:07:51 -07:00
|
|
|
port: port,
|
|
|
|
inbox: b.inbox,
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
|
|
|
b.Add(b.bw)
|
2015-06-10 01:07:51 -07:00
|
|
|
|
|
|
|
return b
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
|
|
|
|
2014-08-17 02:53:26 -07:00
|
|
|
func (b *Broadcast) Send(data []byte) {
|
2014-03-28 03:04:48 -07:00
|
|
|
b.inbox <- data
|
|
|
|
}
|
|
|
|
|
2014-08-17 02:53:26 -07:00
|
|
|
func (b *Broadcast) Recv() ([]byte, net.Addr) {
|
2014-03-28 03:04:48 -07:00
|
|
|
recv := <-b.outbox
|
|
|
|
return recv.data, recv.src
|
|
|
|
}
|
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
func (b *Broadcast) Error() error {
|
|
|
|
if err := b.br.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.bw.Error()
|
|
|
|
}
|
|
|
|
|
2015-06-10 01:07:51 -07:00
|
|
|
type broadcastWriter struct {
|
2015-09-20 06:30:25 -07:00
|
|
|
port int
|
|
|
|
inbox chan []byte
|
|
|
|
conn *net.UDPConn
|
|
|
|
errorHolder
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *broadcastWriter) Serve() {
|
|
|
|
if debug {
|
|
|
|
l.Debugln(w, "starting")
|
|
|
|
defer l.Debugln(w, "stopping")
|
|
|
|
}
|
2014-03-28 03:04:48 -07:00
|
|
|
|
2015-06-10 01:07:51 -07:00
|
|
|
var err error
|
|
|
|
w.conn, err = net.ListenUDP("udp4", nil)
|
|
|
|
if err != nil {
|
2015-09-20 06:30:25 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(err)
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
w.setError(err)
|
2015-06-10 01:07:51 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer w.conn.Close()
|
|
|
|
|
|
|
|
for bs := range w.inbox {
|
2014-05-14 11:26:05 -07:00
|
|
|
addrs, err := net.InterfaceAddrs()
|
2014-03-28 03:04:48 -07:00
|
|
|
if err != nil {
|
2015-06-04 04:57:50 -07:00
|
|
|
if debug {
|
2015-09-20 06:30:25 -07:00
|
|
|
l.Debugln(err)
|
2015-06-04 04:57:50 -07:00
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
w.setError(err)
|
2014-05-14 11:26:05 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var dsts []net.IP
|
|
|
|
for _, addr := range addrs {
|
2014-09-30 08:23:31 -07:00
|
|
|
if iaddr, ok := addr.(*net.IPNet); ok && len(iaddr.IP) >= 4 && iaddr.IP.IsGlobalUnicast() && iaddr.IP.To4() != nil {
|
2014-05-14 11:26:05 -07: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})
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:08:56 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln("addresses:", dsts)
|
|
|
|
}
|
|
|
|
|
2015-09-22 07:04:48 -07:00
|
|
|
success := 0
|
2014-05-14 11:26:05 -07:00
|
|
|
for _, ip := range dsts {
|
2015-06-10 01:07:51 -07:00
|
|
|
dst := &net.UDPAddr{IP: ip, Port: w.port}
|
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
w.conn.SetWriteDeadline(time.Now().Add(time.Second))
|
2015-06-10 01:07:51 -07:00
|
|
|
_, err := w.conn.WriteTo(bs, dst)
|
2015-09-20 06:30:25 -07:00
|
|
|
w.conn.SetWriteDeadline(time.Time{})
|
2015-09-22 07:04:48 -07:00
|
|
|
|
2015-06-10 01:07:51 -07: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-09-20 06:30:25 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(err)
|
|
|
|
}
|
|
|
|
w.setError(err)
|
2015-06-10 01:07:51 -07:00
|
|
|
return
|
2015-09-22 07:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err, ok := err.(net.Error); ok && err.Temporary() {
|
2015-06-10 01:07:51 -07:00
|
|
|
// A transient error. Lets hope for better luck in the future.
|
2014-05-14 17:08:56 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(err)
|
|
|
|
}
|
2015-06-10 01:07:51 -07:00
|
|
|
continue
|
2015-09-22 07:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2015-06-10 01:07:51 -07:00
|
|
|
// Some other error that we don't expect. Bail and retry.
|
2015-09-20 06:30:25 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(err)
|
|
|
|
}
|
|
|
|
w.setError(err)
|
2015-06-10 01:07:51 -07:00
|
|
|
return
|
2015-09-22 07:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if debug {
|
2014-05-14 17:08:56 -07:00
|
|
|
l.Debugf("sent %d bytes to %s", len(bs), dst)
|
2014-04-30 06:13:54 -07:00
|
|
|
}
|
2015-09-22 07:04:48 -07:00
|
|
|
|
|
|
|
success++
|
|
|
|
}
|
|
|
|
|
|
|
|
if success > 0 {
|
|
|
|
w.setError(nil)
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 11:26:05 -07:00
|
|
|
}
|
2014-03-28 03:04:48 -07:00
|
|
|
|
2015-06-10 01:07:51 -07:00
|
|
|
func (w *broadcastWriter) Stop() {
|
|
|
|
w.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *broadcastWriter) String() string {
|
|
|
|
return fmt.Sprintf("broadcastWriter@%p", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
type broadcastReader struct {
|
|
|
|
port int
|
|
|
|
outbox chan recv
|
|
|
|
conn *net.UDPConn
|
2015-09-20 06:30:25 -07:00
|
|
|
errorHolder
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *broadcastReader) Serve() {
|
|
|
|
if debug {
|
|
|
|
l.Debugln(r, "starting")
|
|
|
|
defer l.Debugln(r, "stopping")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
r.conn, err = net.ListenUDP("udp4", &net.UDPAddr{Port: r.port})
|
|
|
|
if err != nil {
|
2015-09-20 06:30:25 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(err)
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
r.setError(err)
|
2015-06-10 01:07:51 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.conn.Close()
|
|
|
|
|
|
|
|
bs := make([]byte, 65536)
|
|
|
|
for {
|
|
|
|
n, addr, err := r.conn.ReadFrom(bs)
|
|
|
|
if err != nil {
|
2015-09-20 06:30:25 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(err)
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
r.setError(err)
|
2015-06-10 01:07:51 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
r.setError(nil)
|
2015-06-10 01:07:51 -07:00
|
|
|
|
|
|
|
if debug {
|
|
|
|
l.Debugf("recv %d bytes from %s", n, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := make([]byte, n)
|
|
|
|
copy(c, bs)
|
|
|
|
select {
|
|
|
|
case r.outbox <- recv{c, addr}:
|
|
|
|
default:
|
|
|
|
if debug {
|
|
|
|
l.Debugln("dropping message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *broadcastReader) Stop() {
|
|
|
|
r.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *broadcastReader) String() string {
|
|
|
|
return fmt.Sprintf("broadcastReader@%p", r)
|
|
|
|
}
|
|
|
|
|
2014-05-14 11:26:05 -07: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 03:04:48 -07:00
|
|
|
|
2014-05-14 11:26:05 -07:00
|
|
|
offset := len(bc.IP) - len(bc.Mask)
|
|
|
|
for i := range bc.IP {
|
2014-07-31 04:39:49 -07:00
|
|
|
if i-offset >= 0 {
|
2014-05-14 11:26:05 -07:00
|
|
|
bc.IP[i] = ip.IP[i] | ^ip.Mask[i-offset]
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
2014-05-14 11:26:05 -07:00
|
|
|
}
|
|
|
|
return bc
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|