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 (
|
2019-11-21 07:41:15 +00:00
|
|
|
"context"
|
2015-06-10 08:07:51 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
2014-03-28 10:04:48 +00:00
|
|
|
|
2019-08-22 10:30:57 +00:00
|
|
|
func NewBroadcast(port int) Interface {
|
|
|
|
c := newCast("broadcastBeacon")
|
2019-11-21 07:41:15 +00:00
|
|
|
c.addReader(func(ctx context.Context) error {
|
|
|
|
return readBroadcasts(ctx, c.outbox, port)
|
2019-08-22 10:30:57 +00:00
|
|
|
})
|
2019-11-21 07:41:15 +00:00
|
|
|
c.addWriter(func(ctx context.Context) error {
|
|
|
|
return writeBroadcasts(ctx, c.inbox, port)
|
2019-08-22 10:30:57 +00:00
|
|
|
})
|
|
|
|
return c
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func writeBroadcasts(ctx context.Context, inbox <-chan []byte, port int) error {
|
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)
|
2019-07-19 17:40:40 +00:00
|
|
|
return err
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
2019-11-21 07:41:15 +00:00
|
|
|
doneCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2019-07-19 17:40:40 +00:00
|
|
|
go func() {
|
2019-11-21 07:41:15 +00:00
|
|
|
<-doneCtx.Done()
|
2019-07-19 17:40:40 +00:00
|
|
|
conn.Close()
|
|
|
|
}()
|
2015-10-20 13:14:05 +00:00
|
|
|
|
2019-07-19 17:40:40 +00:00
|
|
|
for {
|
|
|
|
var bs []byte
|
|
|
|
select {
|
2019-08-22 10:30:57 +00:00
|
|
|
case bs = <-inbox:
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-doneCtx.Done():
|
2020-11-17 12:19:04 +00:00
|
|
|
return doneCtx.Err()
|
2019-07-19 17:40:40 +00:00
|
|
|
}
|
2015-06-10 08:07:51 +00:00
|
|
|
|
2020-03-17 08:40:37 +00:00
|
|
|
intfs, err := net.Interfaces()
|
2014-03-28 10:04:48 +00:00
|
|
|
if err != nil {
|
2023-09-12 12:28:17 +00:00
|
|
|
l.Debugln("Failed to list interfaces:", err)
|
|
|
|
// net.Interfaces() is broken on Android. see https://github.com/golang/go/issues/40569
|
|
|
|
// Use the general broadcast address 255.255.255.255 instead.
|
2014-05-14 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var dsts []net.IP
|
2020-03-17 08:40:37 +00:00
|
|
|
for _, intf := range intfs {
|
2023-08-22 09:27:43 +00:00
|
|
|
if intf.Flags&net.FlagRunning == 0 || intf.Flags&net.FlagBroadcast == 0 {
|
2020-03-17 08:40:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := intf.Addrs()
|
|
|
|
if err != nil {
|
2023-09-12 12:28:17 +00:00
|
|
|
l.Debugln("Failed to list interface addresses:", err)
|
|
|
|
// Interface discovery might work while retrieving the addresses doesn't. So log the error and carry on.
|
|
|
|
continue
|
2020-03-17 08:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 18:26:05 +00: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 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 {
|
2019-08-22 10:30:57 +00:00
|
|
|
dst := &net.UDPAddr{IP: ip, Port: port}
|
2015-06-10 08:07:51 +00:00
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
conn.SetWriteDeadline(time.Now().Add(time.Second))
|
2019-08-22 10:30:57 +00:00
|
|
|
_, err = conn.WriteTo(bs, dst)
|
2019-02-02 11:16:27 +00:00
|
|
|
conn.SetWriteDeadline(time.Time{})
|
2015-09-22 14:04:48 +00:00
|
|
|
|
2019-08-22 10:30:57 +00:00
|
|
|
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
|
2015-06-10 08:07:51 +00:00
|
|
|
// 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)
|
2019-07-19 17:40:40 +00:00
|
|
|
return err
|
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)
|
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++
|
|
|
|
}
|
|
|
|
|
2019-08-22 10:30:57 +00:00
|
|
|
if success == 0 {
|
2021-09-07 22:11:16 +00:00
|
|
|
l.Debugln("couldn't send any broadcasts")
|
2019-08-22 10:30:57 +00:00
|
|
|
return err
|
2014-03-28 10:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 18:26:05 +00:00
|
|
|
}
|
2014-03-28 10:04:48 +00:00
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
func readBroadcasts(ctx context.Context, outbox chan<- recv, port int) error {
|
2019-08-22 10:30:57 +00:00
|
|
|
conn, err := net.ListenUDP("udp4", &net.UDPAddr{Port: port})
|
2015-06-10 08:07:51 +00:00
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(err)
|
2019-07-19 17:40:40 +00:00
|
|
|
return err
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
2019-11-21 07:41:15 +00:00
|
|
|
|
|
|
|
doneCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2019-07-19 17:40:40 +00:00
|
|
|
go func() {
|
2019-11-21 07:41:15 +00:00
|
|
|
<-doneCtx.Done()
|
2019-07-19 17:40:40 +00:00
|
|
|
conn.Close()
|
|
|
|
}()
|
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)
|
2019-07-19 17:40:40 +00:00
|
|
|
return err
|
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 {
|
2019-08-22 10:30:57 +00:00
|
|
|
case outbox <- recv{c, addr}:
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-doneCtx.Done():
|
2020-11-17 12:19:04 +00:00
|
|
|
return doneCtx.Err()
|
2015-06-10 08:07:51 +00:00
|
|
|
default:
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("dropping message")
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:26:05 +00:00
|
|
|
func bcast(ip *net.IPNet) *net.IPNet {
|
2024-11-19 10:32:56 +00:00
|
|
|
bc := &net.IPNet{}
|
2014-05-14 18:26:05 +00:00
|
|
|
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
|
|
|
}
|