syncthing/internal/beacon/broadcast.go

109 lines
2.4 KiB
Go
Raw Normal View History

2014-11-16 20:13:20 +00:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 19:43:32 +00:00
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.
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
import "net"
2014-03-28 10:04:48 +00:00
2014-08-17 09:53:26 +00:00
type Broadcast struct {
conn *net.UDPConn
2014-03-28 10:04:48 +00:00
port int
inbox chan []byte
outbox chan recv
}
2014-08-17 09:53:26 +00:00
func NewBroadcast(port int) (*Broadcast, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: port})
if err != nil {
return nil, err
}
2014-08-17 09:53:26 +00:00
b := &Broadcast{
conn: conn,
2014-03-28 10:04:48 +00:00
port: port,
inbox: make(chan []byte),
outbox: make(chan recv, 16),
2014-03-28 10:04:48 +00:00
}
go genericReader(b.conn, b.outbox)
go b.writer()
return b, nil
2014-03-28 10:04:48 +00:00
}
2014-08-17 09:53:26 +00:00
func (b *Broadcast) Send(data []byte) {
2014-03-28 10:04:48 +00:00
b.inbox <- data
}
2014-08-17 09:53:26 +00:00
func (b *Broadcast) Recv() ([]byte, net.Addr) {
2014-03-28 10:04:48 +00:00
recv := <-b.outbox
return recv.data, recv.src
}
2014-08-17 09:53:26 +00:00
func (b *Broadcast) writer() {
for bs := range b.inbox {
2014-03-28 10:04:48 +00:00
addrs, err := net.InterfaceAddrs()
2014-03-28 10:04:48 +00:00
if err != nil {
2014-08-17 09:53:26 +00:00
l.Warnln("Broadcast: interface addresses:", err)
continue
}
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})
}
2014-05-15 00:08:56 +00:00
if debug {
l.Debugln("addresses:", dsts)
}
for _, ip := range dsts {
dst := &net.UDPAddr{IP: ip, Port: b.port}
_, err := b.conn.WriteTo(bs, dst)
if err != nil {
2014-05-15 00:08:56 +00:00
if debug {
l.Debugln(err)
}
} else if debug {
2014-05-15 00:08:56 +00:00
l.Debugf("sent %d bytes to %s", len(bs), dst)
2014-04-30 13:13:54 +00:00
}
2014-03-28 10:04:48 +00:00
}
}
}
2014-03-28 10:04:48 +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
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 10:04:48 +00:00
}
}
return bc
2014-03-28 10:04:48 +00:00
}