Files
syncthing-arm/lib/beacon/broadcast.go
T

154 lines
3.0 KiB
Go
Raw Normal View History

2014-11-16 21:13:20 +01:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 21:43:32 +02:00
//
2015-03-07 21:36:35 +01: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 https://mozilla.org/MPL/2.0/.
2014-06-01 22:50:14 +02:00
2014-05-15 00:29:18 -03:00
package beacon
2014-03-28 11:04:48 +01:00
import (
"net"
"time"
)
2014-03-28 11:04:48 +01:00
2019-08-22 12:30:57 +02: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
}
2019-08-22 12:30:57 +02:00
func writeBroadcasts(inbox <-chan []byte, port int, stop chan struct{}) error {
2015-10-20 15:14:05 +02:00
conn, err := net.ListenUDP("udp4", nil)
if err != nil {
l.Debugln(err)
return err
}
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-stop:
case <-done:
}
conn.Close()
}()
2015-10-20 15:14:05 +02:00
for {
var bs []byte
select {
2019-08-22 12:30:57 +02:00
case bs = <-inbox:
case <-stop:
return nil
}
addrs, err := net.InterfaceAddrs()
2014-03-28 11:04:48 +01:00
if err != nil {
l.Debugln(err)
2019-08-22 12:30:57 +02:00
return err
}
var dsts []net.IP
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)
}
}
if len(dsts) == 0 {
// Fall back to the general IPv4 broadcast address
dsts = append(dsts, net.IP{0xff, 0xff, 0xff, 0xff})
}
l.Debugln("addresses:", dsts)
2014-05-14 21:08:56 -03:00
success := 0
for _, ip := range dsts {
2019-08-22 12:30:57 +02:00
dst := &net.UDPAddr{IP: ip, Port: port}
2019-02-02 12:16:27 +01:00
conn.SetWriteDeadline(time.Now().Add(time.Second))
2019-08-22 12:30:57 +02:00
_, err = conn.WriteTo(bs, dst)
2019-02-02 12:16:27 +01:00
conn.SetWriteDeadline(time.Time{})
2019-08-22 12:30:57 +02:00
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
// Write timeouts should not happen. We treat it as a fatal
// error on the socket.
l.Debugln(err)
return err
}
if err != nil {
// Some other error that we don't expect. Debug and continue.
l.Debugln(err)
continue
}
l.Debugf("sent %d bytes to %s", len(bs), dst)
success++
}
2019-08-22 12:30:57 +02:00
if success == 0 {
l.Debugln("couldn't send any braodcasts")
return err
2014-03-28 11:04:48 +01:00
}
}
}
2014-03-28 11:04:48 +01:00
2019-08-22 12:30:57 +02:00
func readBroadcasts(outbox chan<- recv, port int, stop chan struct{}) error {
conn, err := net.ListenUDP("udp4", &net.UDPAddr{Port: port})
if err != nil {
l.Debugln(err)
return err
}
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-stop:
case <-done:
}
conn.Close()
}()
bs := make([]byte, 65536)
for {
2015-10-20 15:14:05 +02:00
n, addr, err := conn.ReadFrom(bs)
if err != nil {
l.Debugln(err)
return err
}
l.Debugf("recv %d bytes from %s", n, addr)
c := make([]byte, n)
copy(c, bs)
select {
2019-08-22 12:30:57 +02:00
case outbox <- recv{c, addr}:
case <-stop:
return nil
default:
l.Debugln("dropping message")
}
}
}
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 11:04:48 +01:00
offset := len(bc.IP) - len(bc.Mask)
for i := range bc.IP {
if i-offset >= 0 {
bc.IP[i] = ip.IP[i] | ^ip.Mask[i-offset]
2014-03-28 11:04:48 +01:00
}
}
return bc
2014-03-28 11:04:48 +01:00
}