2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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 http://mozilla.org/MPL/2.0/.
|
2014-08-17 06:01:48 -07:00
|
|
|
|
|
|
|
package beacon
|
|
|
|
|
2015-08-23 06:02:18 -07:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"golang.org/x/net/ipv6"
|
|
|
|
)
|
2014-08-17 06:01:48 -07:00
|
|
|
|
|
|
|
type Multicast struct {
|
2015-08-23 06:02:18 -07:00
|
|
|
conn *ipv6.PacketConn
|
2014-08-17 06:01:48 -07:00
|
|
|
addr *net.UDPAddr
|
|
|
|
inbox chan []byte
|
|
|
|
outbox chan recv
|
2015-08-23 06:02:18 -07:00
|
|
|
intfs []net.Interface
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
|
|
|
|
2015-08-23 06:02:18 -07:00
|
|
|
func NewMulticast(addr string) (*Multicast, error) {
|
2015-04-02 12:45:52 -07:00
|
|
|
gaddr, err := net.ResolveUDPAddr("udp6", addr)
|
2014-08-17 06:01:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-23 06:02:18 -07:00
|
|
|
|
2015-08-27 08:51:15 -07:00
|
|
|
conn, err := net.ListenPacket("udp6", addr)
|
2015-04-02 12:45:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-23 06:02:18 -07:00
|
|
|
|
|
|
|
intfs, err := net.Interfaces()
|
2014-08-17 06:01:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-23 06:02:18 -07:00
|
|
|
|
|
|
|
p := ipv6.NewPacketConn(conn)
|
|
|
|
joined := 0
|
|
|
|
for _, intf := range intfs {
|
|
|
|
err := p.JoinGroup(&intf, &net.UDPAddr{IP: gaddr.IP})
|
|
|
|
if debug {
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln("IPv6 join", intf.Name, "failed:", err)
|
|
|
|
} else {
|
|
|
|
l.Debugln("IPv6 join", intf.Name, "success")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
joined++
|
|
|
|
}
|
|
|
|
|
|
|
|
if joined == 0 {
|
|
|
|
return nil, errors.New("no multicast interfaces available")
|
|
|
|
}
|
|
|
|
|
2014-08-17 06:01:48 -07:00
|
|
|
b := &Multicast{
|
2015-08-23 06:02:18 -07:00
|
|
|
conn: p,
|
2014-08-17 06:01:48 -07:00
|
|
|
addr: gaddr,
|
|
|
|
inbox: make(chan []byte),
|
|
|
|
outbox: make(chan recv, 16),
|
2015-08-23 06:02:18 -07:00
|
|
|
intfs: intfs,
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
|
|
|
|
2015-08-23 06:02:18 -07:00
|
|
|
go genericReader(ipv6ReaderAdapter{b.conn}, b.outbox)
|
2014-08-17 06:01:48 -07:00
|
|
|
go b.writer()
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Multicast) Send(data []byte) {
|
|
|
|
b.inbox <- data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Multicast) Recv() ([]byte, net.Addr) {
|
|
|
|
recv := <-b.outbox
|
|
|
|
return recv.data, recv.src
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Multicast) writer() {
|
2015-08-23 06:02:18 -07:00
|
|
|
wcm := &ipv6.ControlMessage{
|
|
|
|
HopLimit: 1,
|
|
|
|
}
|
|
|
|
|
2014-08-17 06:01:48 -07:00
|
|
|
for bs := range b.inbox {
|
2015-08-23 06:02:18 -07:00
|
|
|
for _, intf := range b.intfs {
|
|
|
|
wcm.IfIndex = intf.Index
|
|
|
|
_, err := b.conn.WriteTo(bs, wcm, b.addr)
|
|
|
|
if err != nil && debug {
|
|
|
|
l.Debugln(err, "on write to", b.addr)
|
|
|
|
} else if debug {
|
|
|
|
l.Debugf("sent %d bytes to %v on %s", len(bs), b.addr, intf.Name)
|
|
|
|
}
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 06:02:18 -07:00
|
|
|
|
|
|
|
// This makes ReadFrom on an *ipv6.PacketConn behave like ReadFrom on a
|
|
|
|
// net.PacketConn.
|
|
|
|
type ipv6ReaderAdapter struct {
|
|
|
|
c *ipv6.PacketConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i ipv6ReaderAdapter) ReadFrom(bs []byte) (int, net.Addr, error) {
|
|
|
|
n, _, src, err := i.c.ReadFrom(bs)
|
|
|
|
return n, src, err
|
|
|
|
}
|