2015-09-20 13:30:25 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// 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/.
|
2015-09-20 13:30:25 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
//go:generate go run ../../script/protofmt.go local.proto
|
2017-01-03 00:16:21 +00:00
|
|
|
//go:generate protoc -I ../../vendor/ -I ../../vendor/github.com/gogo/protobuf/protobuf -I . --gogofast_out=. local.proto
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
2016-07-04 10:40:29 +00:00
|
|
|
"encoding/binary"
|
2015-09-20 13:30:25 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/beacon"
|
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2016-07-04 11:16:48 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2015-09-20 13:30:25 +00:00
|
|
|
"github.com/thejerf/suture"
|
|
|
|
)
|
|
|
|
|
|
|
|
type localClient struct {
|
|
|
|
*suture.Supervisor
|
2016-05-04 19:38:12 +00:00
|
|
|
myID protocol.DeviceID
|
|
|
|
addrList AddressLister
|
|
|
|
name string
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
beacon beacon.Interface
|
|
|
|
localBcastStart time.Time
|
|
|
|
localBcastTick <-chan time.Time
|
|
|
|
forcedBcastTick chan time.Time
|
|
|
|
|
|
|
|
*cache
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
BroadcastInterval = 30 * time.Second
|
|
|
|
CacheLifeTime = 3 * BroadcastInterval
|
2016-07-04 10:40:29 +00:00
|
|
|
Magic = uint32(0x2EA7D90B) // same as in BEP
|
|
|
|
v13Magic = uint32(0x7D79BC40) // previous version
|
2015-09-20 13:30:25 +00:00
|
|
|
)
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
func NewLocal(id protocol.DeviceID, addr string, addrList AddressLister) (FinderService, error) {
|
2015-09-20 13:30:25 +00:00
|
|
|
c := &localClient{
|
|
|
|
Supervisor: suture.NewSimple("local"),
|
|
|
|
myID: id,
|
|
|
|
addrList: addrList,
|
2016-12-18 18:57:41 +00:00
|
|
|
localBcastTick: time.NewTicker(BroadcastInterval).C,
|
2015-09-20 13:30:25 +00:00
|
|
|
forcedBcastTick: make(chan time.Time),
|
|
|
|
localBcastStart: time.Now(),
|
|
|
|
cache: newCache(),
|
|
|
|
}
|
|
|
|
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(host) == 0 {
|
|
|
|
// A broadcast client
|
|
|
|
c.name = "IPv4 local"
|
|
|
|
bcPort, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.startLocalIPv4Broadcasts(bcPort)
|
|
|
|
} else {
|
|
|
|
// A multicast client
|
|
|
|
c.name = "IPv6 local"
|
|
|
|
c.startLocalIPv6Multicasts(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
go c.sendLocalAnnouncements()
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *localClient) startLocalIPv4Broadcasts(localPort int) {
|
|
|
|
c.beacon = beacon.NewBroadcast(localPort)
|
|
|
|
c.Add(c.beacon)
|
|
|
|
go c.recvAnnouncements(c.beacon)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *localClient) startLocalIPv6Multicasts(localMCAddr string) {
|
|
|
|
c.beacon = beacon.NewMulticast(localMCAddr)
|
|
|
|
c.Add(c.beacon)
|
|
|
|
go c.recvAnnouncements(c.beacon)
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
// Lookup returns a list of addresses the device is available at.
|
|
|
|
func (c *localClient) Lookup(device protocol.DeviceID) (addresses []string, err error) {
|
2015-09-20 13:30:25 +00:00
|
|
|
if cache, ok := c.Get(device); ok {
|
|
|
|
if time.Since(cache.when) < CacheLifeTime {
|
2016-05-04 19:38:12 +00:00
|
|
|
addresses = cache.Addresses
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *localClient) String() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *localClient) Error() error {
|
|
|
|
return c.beacon.Error()
|
|
|
|
}
|
|
|
|
|
2017-11-17 09:12:35 +00:00
|
|
|
// announcementPkt appends the local discovery packet to send to msg. Returns
|
|
|
|
// true if the packet should be sent, false if there is nothing useful to
|
|
|
|
// send.
|
|
|
|
func (c *localClient) announcementPkt(instanceID int64, msg []byte) ([]byte, bool) {
|
|
|
|
addrs := c.addrList.AllAddresses()
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
// Nothing to announce
|
|
|
|
return msg, false
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 09:12:35 +00:00
|
|
|
if cap(msg) >= 4 {
|
|
|
|
msg = msg[:4]
|
|
|
|
} else {
|
|
|
|
msg = make([]byte, 4)
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
binary.BigEndian.PutUint32(msg, Magic)
|
|
|
|
|
2017-11-17 09:12:35 +00:00
|
|
|
pkt := Announce{
|
|
|
|
ID: c.myID,
|
|
|
|
Addresses: addrs,
|
|
|
|
InstanceID: instanceID,
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
bs, _ := pkt.Marshal()
|
|
|
|
msg = append(msg, bs...)
|
2015-09-20 13:30:25 +00:00
|
|
|
|
2017-11-17 09:12:35 +00:00
|
|
|
return msg, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *localClient) sendLocalAnnouncements() {
|
|
|
|
var msg []byte
|
|
|
|
var ok bool
|
|
|
|
instanceID := rand.Int63()
|
2015-09-20 13:30:25 +00:00
|
|
|
for {
|
2017-11-17 09:12:35 +00:00
|
|
|
if msg, ok = c.announcementPkt(instanceID, msg[:0]); ok {
|
|
|
|
c.beacon.Send(msg)
|
|
|
|
}
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.localBcastTick:
|
|
|
|
case <-c.forcedBcastTick:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *localClient) recvAnnouncements(b beacon.Interface) {
|
2016-07-04 10:40:29 +00:00
|
|
|
warnedAbout := make(map[string]bool)
|
2015-09-20 13:30:25 +00:00
|
|
|
for {
|
|
|
|
buf, addr := b.Recv()
|
2016-07-04 10:40:29 +00:00
|
|
|
if len(buf) < 4 {
|
|
|
|
l.Debugf("discover: short packet from %s")
|
|
|
|
continue
|
|
|
|
}
|
2015-09-20 13:30:25 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
magic := binary.BigEndian.Uint32(buf)
|
|
|
|
switch magic {
|
|
|
|
case Magic:
|
|
|
|
// All good
|
|
|
|
|
|
|
|
case v13Magic:
|
|
|
|
// Old version
|
|
|
|
if !warnedAbout[addr.String()] {
|
|
|
|
l.Warnf("Incompatible (v0.13) local discovery packet from %v - upgrade that device to connect", addr)
|
|
|
|
warnedAbout[addr.String()] = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
|
|
|
|
default:
|
|
|
|
l.Debugf("discover: Incorrect magic %x from %s", magic, addr)
|
2015-09-20 13:30:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
var pkt Announce
|
|
|
|
err := pkt.Unmarshal(buf[4:])
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
l.Debugf("discover: Failed to unmarshal local announcement from %s:\n%s", addr, hex.Dump(buf))
|
2016-05-04 19:38:12 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-10-29 21:56:24 +00:00
|
|
|
l.Debugf("discover: Received local announcement from %s for %s", addr, pkt.ID)
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
var newDevice bool
|
2016-10-29 21:56:24 +00:00
|
|
|
if pkt.ID != c.myID {
|
2016-07-04 10:40:29 +00:00
|
|
|
newDevice = c.registerDevice(addr, pkt)
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if newDevice {
|
2016-04-01 07:24:04 +00:00
|
|
|
// Force a transmit to announce ourselves, if we are ready to do
|
|
|
|
// so right away.
|
2015-09-20 13:30:25 +00:00
|
|
|
select {
|
|
|
|
case c.forcedBcastTick <- time.Now():
|
2016-04-01 07:24:04 +00:00
|
|
|
default:
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func (c *localClient) registerDevice(src net.Addr, device Announce) bool {
|
2015-09-20 13:30:25 +00:00
|
|
|
// Remember whether we already had a valid cache entry for this device.
|
2016-07-04 11:16:48 +00:00
|
|
|
// If the instance ID has changed the remote device has restarted since
|
|
|
|
// we last heard from it, so we should treat it as a new device.
|
2015-09-20 13:30:25 +00:00
|
|
|
|
2016-10-29 21:56:24 +00:00
|
|
|
ce, existsAlready := c.Get(device.ID)
|
2016-07-04 11:16:48 +00:00
|
|
|
isNewDevice := !existsAlready || time.Since(ce.when) > CacheLifeTime || ce.instanceID != device.InstanceID
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
// Any empty or unspecified addresses should be set to the source address
|
|
|
|
// of the announcement. We also skip any addresses we can't parse.
|
|
|
|
|
2016-10-29 21:56:24 +00:00
|
|
|
l.Debugln("discover: Registering addresses for", device.ID)
|
2015-09-20 13:30:25 +00:00
|
|
|
var validAddresses []string
|
|
|
|
for _, addr := range device.Addresses {
|
2016-07-04 10:40:29 +00:00
|
|
|
u, err := url.Parse(addr)
|
2015-09-20 13:30:25 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpAddr, err := net.ResolveTCPAddr("tcp", u.Host)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tcpAddr.IP) == 0 || tcpAddr.IP.IsUnspecified() {
|
2016-07-03 20:43:26 +00:00
|
|
|
srcAddr, err := net.ResolveTCPAddr("tcp", src.String())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not use IPv6 source address if requested scheme is tcp4
|
|
|
|
if u.Scheme == "tcp4" && srcAddr.IP.To4() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not use IPv4 source address if requested scheme is tcp6
|
|
|
|
if u.Scheme == "tcp6" && srcAddr.IP.To4() != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
host, _, err := net.SplitHostPort(src.String())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-09-27 19:44:08 +00:00
|
|
|
u.Host = net.JoinHostPort(host, strconv.Itoa(tcpAddr.Port))
|
2015-11-10 09:16:55 +00:00
|
|
|
l.Debugf("discover: Reconstructed URL is %#v", u)
|
2015-09-20 13:30:25 +00:00
|
|
|
validAddresses = append(validAddresses, u.String())
|
2016-07-04 10:40:29 +00:00
|
|
|
l.Debugf("discover: Replaced address %v in %s to get %s", tcpAddr.IP, addr, u.String())
|
2015-09-20 13:30:25 +00:00
|
|
|
} else {
|
2016-07-04 10:40:29 +00:00
|
|
|
validAddresses = append(validAddresses, addr)
|
|
|
|
l.Debugf("discover: Accepted address %s verbatim", addr)
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 21:56:24 +00:00
|
|
|
c.Set(device.ID, CacheEntry{
|
2016-07-04 11:16:48 +00:00
|
|
|
Addresses: validAddresses,
|
|
|
|
when: time.Now(),
|
|
|
|
found: true,
|
|
|
|
instanceID: device.InstanceID,
|
2015-09-20 13:30:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if isNewDevice {
|
|
|
|
events.Default.Log(events.DeviceDiscovered, map[string]interface{}{
|
2016-10-29 21:56:24 +00:00
|
|
|
"device": device.ID.String(),
|
2015-11-09 20:27:38 +00:00
|
|
|
"addrs": validAddresses,
|
2015-09-20 13:30:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return isNewDevice
|
|
|
|
}
|