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