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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
//go:generate go run ../../script/protofmt.go local.proto
|
|
|
|
//go:generate protoc --proto_path=../../../../../:../../../../gogo/protobuf/protobuf:. --gogofast_out=. local.proto
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
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,
|
|
|
|
localBcastTick: time.Tick(BroadcastInterval),
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *localClient) announcementPkt() Announce {
|
|
|
|
return Announce{
|
2016-07-04 11:16:48 +00:00
|
|
|
ID: c.myID[:],
|
|
|
|
Addresses: c.addrList.AllAddresses(),
|
|
|
|
InstanceID: rand.Int63(),
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *localClient) sendLocalAnnouncements() {
|
2016-07-04 10:40:29 +00:00
|
|
|
msg := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(msg, Magic)
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
var pkt = c.announcementPkt()
|
2016-07-04 10:40:29 +00:00
|
|
|
bs, _ := pkt.Marshal()
|
|
|
|
msg = append(msg, bs...)
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
c.beacon.Send(msg)
|
|
|
|
|
|
|
|
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-07-04 10:40:29 +00:00
|
|
|
l.Debugf("discover: Received local announcement from %s for %s", addr, protocol.DeviceIDFromBytes(pkt.ID))
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
var newDevice bool
|
2016-07-04 10:40:29 +00:00
|
|
|
if !bytes.Equal(pkt.ID, c.myID[:]) {
|
|
|
|
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
|
|
|
var id protocol.DeviceID
|
|
|
|
copy(id[:], device.ID)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
ce, existsAlready := c.Get(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.
|
|
|
|
|
2015-11-09 20:27:38 +00:00
|
|
|
l.Debugln("discover: Registering addresses for", 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Set(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{}{
|
|
|
|
"device": id.String(),
|
2015-11-09 20:27:38 +00:00
|
|
|
"addrs": validAddresses,
|
2015-09-20 13:30:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return isNewDevice
|
|
|
|
}
|