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 (
|
|
|
|
"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")
|
|
|
|
c.addReader(func(stop chan struct{}) error {
|
|
|
|
return readBroadcasts(c.outbox, port, stop)
|
|
|
|
})
|
|
|
|
c.addWriter(func(stop chan struct{}) error {
|
|
|
|
return writeBroadcasts(c.inbox, port, stop)
|
|
|
|
})
|
|
|
|
return c
|
2015-06-10 08:07:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 10:30:57 +00:00
|
|
|
func writeBroadcasts(inbox <-chan []byte, port int, stop chan struct{}) 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-07-19 17:40:40 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
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-07-19 17:40:40 +00:00
|
|
|
case <-stop:
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-10 08:07:51 +00:00
|
|
|
|
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)
|
2019-08-22 10:30:57 +00:00
|
|
|
return err
|
2014-05-14 18:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
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 {
|
|
|
|
l.Debugln("couldn't send any braodcasts")
|
|
|
|
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-08-22 10:30:57 +00:00
|
|
|
func readBroadcasts(outbox chan<- recv, port int, stop chan struct{}) error {
|
|
|
|
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-07-19 17:40:40 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
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-07-19 17:40:40 +00:00
|
|
|
case <-stop:
|
|
|
|
return nil
|
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 {
|
|
|
|
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
|
|
|
}
|