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,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://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 (
|
2019-11-21 00:41:15 -07:00
|
|
|
"context"
|
2015-06-10 01:07:51 -07:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
2014-03-28 03:04:48 -07:00
|
|
|
|
2019-08-22 03:30:57 -07:00
|
|
|
func NewBroadcast(port int) Interface {
|
|
|
|
c := newCast("broadcastBeacon")
|
2019-11-21 00:41:15 -07:00
|
|
|
c.addReader(func(ctx context.Context) error {
|
|
|
|
return readBroadcasts(ctx, c.outbox, port)
|
2019-08-22 03:30:57 -07:00
|
|
|
})
|
2019-11-21 00:41:15 -07:00
|
|
|
c.addWriter(func(ctx context.Context) error {
|
|
|
|
return writeBroadcasts(ctx, c.inbox, port)
|
2019-08-22 03:30:57 -07:00
|
|
|
})
|
|
|
|
return c
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
|
|
|
|
2019-11-21 00:41:15 -07:00
|
|
|
func writeBroadcasts(ctx context.Context, inbox <-chan []byte, port int) error {
|
2015-10-20 06:14:05 -07:00
|
|
|
conn, err := net.ListenUDP("udp4", nil)
|
2015-06-10 01:07:51 -07:00
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
2019-11-21 00:41:15 -07:00
|
|
|
doneCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2019-07-19 10:40:40 -07:00
|
|
|
go func() {
|
2019-11-21 00:41:15 -07:00
|
|
|
<-doneCtx.Done()
|
2019-07-19 10:40:40 -07:00
|
|
|
conn.Close()
|
|
|
|
}()
|
2015-10-20 06:14:05 -07:00
|
|
|
|
2019-07-19 10:40:40 -07:00
|
|
|
for {
|
|
|
|
var bs []byte
|
|
|
|
select {
|
2019-08-22 03:30:57 -07:00
|
|
|
case bs = <-inbox:
|
2019-11-21 00:41:15 -07:00
|
|
|
case <-doneCtx.Done():
|
2020-11-17 05:19:04 -07:00
|
|
|
return doneCtx.Err()
|
2019-07-19 10:40:40 -07:00
|
|
|
}
|
2015-06-10 01:07:51 -07:00
|
|
|
|
2020-03-17 01:40:37 -07:00
|
|
|
intfs, err := net.Interfaces()
|
2014-03-28 03:04:48 -07:00
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-08-22 03:30:57 -07:00
|
|
|
return err
|
2014-05-14 11:26:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var dsts []net.IP
|
2020-03-17 01:40:37 -07:00
|
|
|
for _, intf := range intfs {
|
|
|
|
if intf.Flags&net.FlagBroadcast == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := intf.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if iaddr, ok := addr.(*net.IPNet); ok && len(iaddr.IP) >= 4 && iaddr.IP.IsGlobalUnicast() && iaddr.IP.To4() != nil {
|
|
|
|
baddr := bcast(iaddr)
|
|
|
|
dsts = append(dsts, baddr.IP)
|
|
|
|
}
|
2014-05-14 11:26:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dsts) == 0 {
|
|
|
|
// Fall back to the general IPv4 broadcast address
|
|
|
|
dsts = append(dsts, net.IP{0xff, 0xff, 0xff, 0xff})
|
|
|
|
}
|
|
|
|
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("addresses:", dsts)
|
2014-05-14 17:08:56 -07:00
|
|
|
|
2015-09-22 07:04:48 -07:00
|
|
|
success := 0
|
2014-05-14 11:26:05 -07:00
|
|
|
for _, ip := range dsts {
|
2019-08-22 03:30:57 -07:00
|
|
|
dst := &net.UDPAddr{IP: ip, Port: port}
|
2015-06-10 01:07:51 -07:00
|
|
|
|
2019-02-02 04:16:27 -07:00
|
|
|
conn.SetWriteDeadline(time.Now().Add(time.Second))
|
2019-08-22 03:30:57 -07:00
|
|
|
_, err = conn.WriteTo(bs, dst)
|
2019-02-02 04:16:27 -07:00
|
|
|
conn.SetWriteDeadline(time.Time{})
|
2015-09-22 07:04:48 -07:00
|
|
|
|
2019-08-22 03:30:57 -07:00
|
|
|
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
|
2015-06-10 01:07:51 -07:00
|
|
|
// Write timeouts should not happen. We treat it as a fatal
|
|
|
|
// error on the socket.
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2015-09-22 07:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-10-12 00:13:44 -07:00
|
|
|
// Some other error that we don't expect. Debug and continue.
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2017-10-12 00:13:44 -07:00
|
|
|
continue
|
2015-09-22 07:04:48 -07:00
|
|
|
}
|
|
|
|
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("sent %d bytes to %s", len(bs), dst)
|
2015-09-22 07:04:48 -07:00
|
|
|
success++
|
|
|
|
}
|
|
|
|
|
2019-08-22 03:30:57 -07:00
|
|
|
if success == 0 {
|
2021-09-07 15:11:16 -07:00
|
|
|
l.Debugln("couldn't send any broadcasts")
|
2019-08-22 03:30:57 -07:00
|
|
|
return err
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 11:26:05 -07:00
|
|
|
}
|
2014-03-28 03:04:48 -07:00
|
|
|
|
2019-11-21 00:41:15 -07:00
|
|
|
func readBroadcasts(ctx context.Context, outbox chan<- recv, port int) error {
|
2019-08-22 03:30:57 -07:00
|
|
|
conn, err := net.ListenUDP("udp4", &net.UDPAddr{Port: port})
|
2015-06-10 01:07:51 -07:00
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
2019-11-21 00:41:15 -07:00
|
|
|
|
|
|
|
doneCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2019-07-19 10:40:40 -07:00
|
|
|
go func() {
|
2019-11-21 00:41:15 -07:00
|
|
|
<-doneCtx.Done()
|
2019-07-19 10:40:40 -07:00
|
|
|
conn.Close()
|
|
|
|
}()
|
2015-06-10 01:07:51 -07:00
|
|
|
|
|
|
|
bs := make([]byte, 65536)
|
|
|
|
for {
|
2015-10-20 06:14:05 -07:00
|
|
|
n, addr, err := conn.ReadFrom(bs)
|
2015-06-10 01:07:51 -07:00
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
|
|
|
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("recv %d bytes from %s", n, addr)
|
2015-06-10 01:07:51 -07:00
|
|
|
|
|
|
|
c := make([]byte, n)
|
|
|
|
copy(c, bs)
|
|
|
|
select {
|
2019-08-22 03:30:57 -07:00
|
|
|
case outbox <- recv{c, addr}:
|
2019-11-21 00:41:15 -07:00
|
|
|
case <-doneCtx.Done():
|
2020-11-17 05:19:04 -07:00
|
|
|
return doneCtx.Err()
|
2015-06-10 01:07:51 -07:00
|
|
|
default:
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("dropping message")
|
2015-06-10 01:07:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|