2014-06-01 22:50:14 +02:00
|
|
|
// Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-05-15 05:29:18 +02:00
|
|
|
package beacon
|
2014-03-28 11:04:48 +01:00
|
|
|
|
2014-05-14 20:26:05 +02:00
|
|
|
import "net"
|
2014-03-28 11:04:48 +01:00
|
|
|
|
|
|
|
type recv struct {
|
|
|
|
data []byte
|
|
|
|
src net.Addr
|
|
|
|
}
|
|
|
|
|
2014-05-11 23:20:14 +02:00
|
|
|
type dst struct {
|
|
|
|
intf string
|
|
|
|
conn *net.UDPConn
|
|
|
|
}
|
|
|
|
|
2014-03-28 11:04:48 +01:00
|
|
|
type Beacon struct {
|
2014-05-14 20:26:05 +02:00
|
|
|
conn *net.UDPConn
|
2014-03-28 11:04:48 +01:00
|
|
|
port int
|
2014-05-11 23:20:14 +02:00
|
|
|
conns []dst
|
2014-03-28 11:04:48 +01:00
|
|
|
inbox chan []byte
|
|
|
|
outbox chan recv
|
|
|
|
}
|
|
|
|
|
2014-05-15 05:29:18 +02:00
|
|
|
func New(port int) (*Beacon, error) {
|
2014-05-14 20:26:05 +02:00
|
|
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: port})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-28 11:04:48 +01:00
|
|
|
b := &Beacon{
|
2014-05-14 20:26:05 +02:00
|
|
|
conn: conn,
|
2014-03-28 11:04:48 +01:00
|
|
|
port: port,
|
|
|
|
inbox: make(chan []byte),
|
2014-05-14 20:26:05 +02:00
|
|
|
outbox: make(chan recv, 16),
|
2014-03-28 11:04:48 +01:00
|
|
|
}
|
2014-05-14 20:26:05 +02:00
|
|
|
|
|
|
|
go b.reader()
|
|
|
|
go b.writer()
|
|
|
|
|
|
|
|
return b, nil
|
2014-03-28 11:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Beacon) Send(data []byte) {
|
|
|
|
b.inbox <- data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Beacon) Recv() ([]byte, net.Addr) {
|
|
|
|
recv := <-b.outbox
|
|
|
|
return recv.data, recv.src
|
|
|
|
}
|
|
|
|
|
2014-05-14 20:26:05 +02:00
|
|
|
func (b *Beacon) reader() {
|
2014-06-18 23:57:22 +02:00
|
|
|
bs := make([]byte, 65536)
|
2014-05-14 20:26:05 +02:00
|
|
|
for {
|
|
|
|
n, addr, err := b.conn.ReadFrom(bs)
|
|
|
|
if err != nil {
|
2014-05-15 02:08:56 +02:00
|
|
|
l.Warnln("Beacon read:", err)
|
2014-05-14 20:26:05 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if debug {
|
2014-05-15 02:08:56 +02:00
|
|
|
l.Debugf("recv %d bytes from %s", n, addr)
|
2014-05-14 20:26:05 +02:00
|
|
|
}
|
2014-06-18 23:57:22 +02:00
|
|
|
|
|
|
|
c := make([]byte, n)
|
|
|
|
copy(c, bs)
|
2014-05-14 20:26:05 +02:00
|
|
|
select {
|
2014-06-18 23:57:22 +02:00
|
|
|
case b.outbox <- recv{c, addr}:
|
2014-05-14 20:26:05 +02:00
|
|
|
default:
|
|
|
|
if debug {
|
2014-05-15 02:08:56 +02:00
|
|
|
l.Debugln("dropping message")
|
2014-05-14 20:26:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-30 15:13:54 +02:00
|
|
|
}
|
2014-05-14 20:26:05 +02:00
|
|
|
}
|
2014-03-28 11:04:48 +01:00
|
|
|
|
2014-05-14 20:26:05 +02:00
|
|
|
func (b *Beacon) writer() {
|
|
|
|
for bs := range b.inbox {
|
2014-03-28 11:04:48 +01:00
|
|
|
|
2014-05-14 20:26:05 +02:00
|
|
|
addrs, err := net.InterfaceAddrs()
|
2014-03-28 11:04:48 +01:00
|
|
|
if err != nil {
|
2014-05-15 02:08:56 +02:00
|
|
|
l.Warnln("Beacon: interface addresses:", err)
|
2014-05-14 20:26:05 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var dsts []net.IP
|
|
|
|
for _, addr := range addrs {
|
2014-06-14 11:14:37 +02:00
|
|
|
if iaddr, ok := addr.(*net.IPNet); ok && iaddr.IP.IsGlobalUnicast() && iaddr.IP.To4() != nil {
|
2014-05-14 20:26:05 +02: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-15 02:08:56 +02:00
|
|
|
if debug {
|
|
|
|
l.Debugln("addresses:", dsts)
|
|
|
|
}
|
|
|
|
|
2014-05-14 20:26:05 +02:00
|
|
|
for _, ip := range dsts {
|
|
|
|
dst := &net.UDPAddr{IP: ip, Port: b.port}
|
|
|
|
|
|
|
|
_, err := b.conn.WriteTo(bs, dst)
|
|
|
|
if err != nil {
|
2014-05-15 02:08:56 +02:00
|
|
|
if debug {
|
|
|
|
l.Debugln(err)
|
|
|
|
}
|
2014-05-28 07:03:47 +02:00
|
|
|
} else if debug {
|
2014-05-15 02:08:56 +02:00
|
|
|
l.Debugf("sent %d bytes to %s", len(bs), dst)
|
2014-04-30 15:13:54 +02:00
|
|
|
}
|
2014-03-28 11:04:48 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-14 20:26:05 +02:00
|
|
|
}
|
2014-03-28 11:04:48 +01:00
|
|
|
|
2014-05-14 20:26:05 +02: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 11:04:48 +01:00
|
|
|
|
2014-05-14 20:26:05 +02: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
|
|
|
}
|
2014-05-14 20:26:05 +02:00
|
|
|
}
|
|
|
|
return bc
|
2014-03-28 11:04:48 +01:00
|
|
|
}
|