2014-07-12 15:45:33 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
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
|
|
|
|
2014-05-14 11:26:05 -07:00
|
|
|
import "net"
|
2014-03-28 03:04:48 -07:00
|
|
|
|
2014-08-17 02:53:26 -07:00
|
|
|
type Broadcast struct {
|
2014-05-14 11:26:05 -07:00
|
|
|
conn *net.UDPConn
|
2014-03-28 03:04:48 -07:00
|
|
|
port int
|
|
|
|
inbox chan []byte
|
|
|
|
outbox chan recv
|
|
|
|
}
|
|
|
|
|
2014-08-17 02:53:26 -07:00
|
|
|
func NewBroadcast(port int) (*Broadcast, error) {
|
2014-05-14 11:26:05 -07:00
|
|
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: port})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-17 02:53:26 -07:00
|
|
|
b := &Broadcast{
|
2014-05-14 11:26:05 -07:00
|
|
|
conn: conn,
|
2014-03-28 03:04:48 -07:00
|
|
|
port: port,
|
|
|
|
inbox: make(chan []byte),
|
2014-05-14 11:26:05 -07:00
|
|
|
outbox: make(chan recv, 16),
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
2014-05-14 11:26:05 -07:00
|
|
|
|
2014-08-17 06:01:48 -07:00
|
|
|
go genericReader(b.conn, b.outbox)
|
2014-05-14 11:26:05 -07:00
|
|
|
go b.writer()
|
|
|
|
|
|
|
|
return b, nil
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
|
|
|
|
2014-08-17 02:53:26 -07:00
|
|
|
func (b *Broadcast) Send(data []byte) {
|
2014-03-28 03:04:48 -07:00
|
|
|
b.inbox <- data
|
|
|
|
}
|
|
|
|
|
2014-08-17 02:53:26 -07:00
|
|
|
func (b *Broadcast) Recv() ([]byte, net.Addr) {
|
2014-03-28 03:04:48 -07:00
|
|
|
recv := <-b.outbox
|
|
|
|
return recv.data, recv.src
|
|
|
|
}
|
|
|
|
|
2014-08-17 02:53:26 -07:00
|
|
|
func (b *Broadcast) writer() {
|
2014-05-14 11:26:05 -07:00
|
|
|
for bs := range b.inbox {
|
2014-03-28 03:04:48 -07:00
|
|
|
|
2014-05-14 11:26:05 -07:00
|
|
|
addrs, err := net.InterfaceAddrs()
|
2014-03-28 03:04:48 -07:00
|
|
|
if err != nil {
|
2014-08-17 02:53:26 -07:00
|
|
|
l.Warnln("Broadcast: interface addresses:", err)
|
2014-05-14 11:26:05 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var dsts []net.IP
|
|
|
|
for _, addr := range addrs {
|
2014-06-14 02:14:37 -07:00
|
|
|
if iaddr, ok := addr.(*net.IPNet); ok && iaddr.IP.IsGlobalUnicast() && iaddr.IP.To4() != nil {
|
2014-05-14 11:26:05 -07: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})
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:08:56 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln("addresses:", dsts)
|
|
|
|
}
|
|
|
|
|
2014-05-14 11:26:05 -07:00
|
|
|
for _, ip := range dsts {
|
|
|
|
dst := &net.UDPAddr{IP: ip, Port: b.port}
|
|
|
|
|
|
|
|
_, err := b.conn.WriteTo(bs, dst)
|
|
|
|
if err != nil {
|
2014-05-14 17:08:56 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(err)
|
|
|
|
}
|
2014-05-27 22:03:47 -07:00
|
|
|
} else if debug {
|
2014-05-14 17:08:56 -07:00
|
|
|
l.Debugf("sent %d bytes to %s", len(bs), dst)
|
2014-04-30 06:13:54 -07:00
|
|
|
}
|
2014-03-28 03:04:48 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 11:26:05 -07:00
|
|
|
}
|
2014-03-28 03:04:48 -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
|
|
|
}
|