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,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://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"
|
2015-09-20 06:30:25 -07:00
|
|
|
"time"
|
2015-08-23 06:02:18 -07:00
|
|
|
|
|
|
|
"golang.org/x/net/ipv6"
|
|
|
|
)
|
2014-08-17 06:01:48 -07:00
|
|
|
|
2019-08-22 03:30:57 -07:00
|
|
|
func NewMulticast(addr string) Interface {
|
|
|
|
c := newCast("multicastBeacon")
|
|
|
|
c.addReader(func(stop chan struct{}) error {
|
|
|
|
return readMulticasts(c.outbox, addr, stop)
|
|
|
|
})
|
|
|
|
c.addWriter(func(stop chan struct{}) error {
|
|
|
|
return writeMulticasts(c.inbox, addr, stop)
|
|
|
|
})
|
|
|
|
return c
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:30:57 -07:00
|
|
|
func writeMulticasts(inbox <-chan []byte, addr string, stop chan struct{}) error {
|
|
|
|
gaddr, err := net.ResolveUDPAddr("udp6", addr)
|
2015-09-20 06:30:25 -07:00
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.ListenPacket("udp6", ":0")
|
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
2019-07-19 10:40:40 -07:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}()
|
2015-09-20 06:30:25 -07:00
|
|
|
|
|
|
|
pconn := ipv6.NewPacketConn(conn)
|
|
|
|
|
|
|
|
wcm := &ipv6.ControlMessage{
|
|
|
|
HopLimit: 1,
|
|
|
|
}
|
|
|
|
|
2019-07-09 02:40:30 -07:00
|
|
|
for {
|
|
|
|
var bs []byte
|
|
|
|
select {
|
2019-08-22 03:30:57 -07:00
|
|
|
case bs = <-inbox:
|
2019-07-09 02:40:30 -07:00
|
|
|
case <-stop:
|
2019-07-19 10:40:40 -07:00
|
|
|
return nil
|
2019-07-09 02:40:30 -07:00
|
|
|
}
|
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
intfs, err := net.Interfaces()
|
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
|
|
|
|
2015-09-22 07:04:48 -07:00
|
|
|
success := 0
|
2015-09-20 06:30:25 -07:00
|
|
|
for _, intf := range intfs {
|
|
|
|
wcm.IfIndex = intf.Index
|
2019-02-02 04:16:27 -07:00
|
|
|
pconn.SetWriteDeadline(time.Now().Add(time.Second))
|
2015-09-20 06:30:25 -07:00
|
|
|
_, err = pconn.WriteTo(bs, wcm, gaddr)
|
2019-02-02 04:16:27 -07:00
|
|
|
pconn.SetWriteDeadline(time.Time{})
|
2015-09-22 07:04:48 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err, "on write to", gaddr, intf.Name)
|
2015-09-22 07:04:48 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("sent %d bytes to %v on %s", len(bs), gaddr, intf.Name)
|
2015-09-22 07:04:48 -07:00
|
|
|
|
|
|
|
success++
|
2019-07-09 02:40:30 -07:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stop:
|
2019-07-19 10:40:40 -07:00
|
|
|
return nil
|
2019-07-09 02:40:30 -07:00
|
|
|
default:
|
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:30:57 -07:00
|
|
|
if success == 0 {
|
|
|
|
return err
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 03:30:57 -07:00
|
|
|
func readMulticasts(outbox chan<- recv, addr string, stop chan struct{}) error {
|
|
|
|
gaddr, err := net.ResolveUDPAddr("udp6", addr)
|
2014-08-17 06:01:48 -07:00
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
2015-08-23 06:02:18 -07:00
|
|
|
|
2019-08-22 03:30:57 -07:00
|
|
|
conn, err := net.ListenPacket("udp6", addr)
|
2015-04-02 12:45:52 -07:00
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2015-04-02 12:45:52 -07:00
|
|
|
}
|
2019-07-19 10:40:40 -07:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}()
|
2015-08-23 06:02:18 -07:00
|
|
|
|
|
|
|
intfs, err := net.Interfaces()
|
2014-08-17 06:01:48 -07:00
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-07-19 10:40:40 -07:00
|
|
|
return err
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
2015-08-23 06:02:18 -07:00
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
pconn := ipv6.NewPacketConn(conn)
|
2015-08-23 06:02:18 -07:00
|
|
|
joined := 0
|
|
|
|
for _, intf := range intfs {
|
2015-09-20 06:30:25 -07:00
|
|
|
err := pconn.JoinGroup(&intf, &net.UDPAddr{IP: gaddr.IP})
|
2015-10-03 08:25:21 -07:00
|
|
|
if err != nil {
|
|
|
|
l.Debugln("IPv6 join", intf.Name, "failed:", err)
|
|
|
|
} else {
|
|
|
|
l.Debugln("IPv6 join", intf.Name, "success")
|
2015-08-23 06:02:18 -07:00
|
|
|
}
|
|
|
|
joined++
|
|
|
|
}
|
|
|
|
|
|
|
|
if joined == 0 {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("no multicast interfaces available")
|
2019-07-19 10:40:40 -07:00
|
|
|
return errors.New("no multicast interfaces available")
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
bs := make([]byte, 65536)
|
|
|
|
for {
|
2019-07-19 10:40:40 -07:00
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
2015-09-20 06:30:25 -07:00
|
|
|
n, _, addr, err := pconn.ReadFrom(bs)
|
|
|
|
if err != nil {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln(err)
|
2019-08-22 03:30:57 -07:00
|
|
|
return err
|
2015-09-20 06:30:25 -07:00
|
|
|
}
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugf("recv %d bytes from %s", n, addr)
|
2015-08-23 06:02:18 -07:00
|
|
|
|
2015-09-20 06:30:25 -07:00
|
|
|
c := make([]byte, n)
|
|
|
|
copy(c, bs)
|
|
|
|
select {
|
2019-08-22 03:30:57 -07:00
|
|
|
case outbox <- recv{c, addr}:
|
2015-09-20 06:30:25 -07:00
|
|
|
default:
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("dropping message")
|
2014-08-17 06:01:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|