syncthing/internal/discover/discover.go

502 lines
11 KiB
Go
Raw Normal View History

2014-07-12 15:45:33 -07:00
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
// All rights reserved. Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2014-06-01 13:50:14 -07:00
2013-12-15 03:43:31 -07:00
package discover
import (
"bytes"
2014-02-20 09:40:15 -07:00
"encoding/hex"
"errors"
2014-05-11 15:55:43 -07:00
"io"
2013-12-15 03:43:31 -07:00
"net"
"strconv"
2013-12-15 03:43:31 -07:00
"sync"
"time"
2013-12-24 09:10:49 -07:00
2014-09-22 12:42:11 -07:00
"github.com/syncthing/syncthing/internal/beacon"
"github.com/syncthing/syncthing/internal/events"
"github.com/syncthing/syncthing/internal/protocol"
2013-12-15 03:43:31 -07:00
)
type Discoverer struct {
myID protocol.DeviceID
listenAddrs []string
localBcastIntv time.Duration
globalBcastIntv time.Duration
errorRetryIntv time.Duration
cacheLifetime time.Duration
broadcastBeacon beacon.Interface
multicastBeacon beacon.Interface
registry map[protocol.DeviceID][]cacheEntry
registryLock sync.RWMutex
extServer string
2014-04-18 04:20:42 -07:00
extPort uint16
localBcastTick <-chan time.Time
stopGlobal chan struct{}
globalWG sync.WaitGroup
forcedBcastTick chan time.Time
extAnnounceOK bool
extAnnounceOKmut sync.Mutex
2013-12-22 14:29:23 -07:00
}
type cacheEntry struct {
addr string
seen time.Time
}
2014-02-20 09:40:15 -07:00
var (
2014-02-24 05:29:30 -07:00
ErrIncorrectMagic = errors.New("incorrect magic number")
2014-02-20 09:40:15 -07:00
)
func NewDiscoverer(id protocol.DeviceID, addresses []string) *Discoverer {
return &Discoverer{
myID: id,
listenAddrs: addresses,
localBcastIntv: 30 * time.Second,
globalBcastIntv: 1800 * time.Second,
errorRetryIntv: 60 * time.Second,
cacheLifetime: 5 * time.Minute,
registry: make(map[protocol.DeviceID][]cacheEntry),
}
}
2013-12-15 03:43:31 -07:00
func (d *Discoverer) StartLocal(localPort int, localMCAddr string) {
if localPort > 0 {
bb, err := beacon.NewBroadcast(localPort)
if err != nil {
if debug {
l.Debugln(err)
}
l.Infoln("Local discovery over IPv4 unavailable")
} else {
d.broadcastBeacon = bb
go d.recvAnnouncements(bb)
}
}
if len(localMCAddr) > 0 {
mb, err := beacon.NewMulticast(localMCAddr)
if err != nil {
if debug {
l.Debugln(err)
}
l.Infoln("Local discovery over IPv6 unavailable")
} else {
d.multicastBeacon = mb
go d.recvAnnouncements(mb)
}
}
if d.broadcastBeacon == nil && d.multicastBeacon == nil {
l.Warnln("Local discovery unavailable")
} else {
d.localBcastTick = time.Tick(d.localBcastIntv)
d.forcedBcastTick = make(chan time.Time)
go d.sendLocalAnnouncements()
}
}
2014-04-18 04:20:42 -07:00
func (d *Discoverer) StartGlobal(server string, extPort uint16) {
// Wait for any previous announcer to stop before starting a new one.
d.globalWG.Wait()
d.extServer = server
2014-04-18 04:20:42 -07:00
d.extPort = extPort
d.stopGlobal = make(chan struct{})
d.globalWG.Add(1)
go d.sendExternalAnnouncements()
}
func (d *Discoverer) StopGlobal() {
if d.stopGlobal != nil {
close(d.stopGlobal)
d.globalWG.Wait()
}
}
func (d *Discoverer) ExtAnnounceOK() bool {
d.extAnnounceOKmut.Lock()
defer d.extAnnounceOKmut.Unlock()
return d.extAnnounceOK
}
func (d *Discoverer) Lookup(device protocol.DeviceID) []string {
2014-05-01 23:53:19 -07:00
d.registryLock.Lock()
cached := d.filterCached(d.registry[device])
2014-05-01 23:53:19 -07:00
d.registryLock.Unlock()
if len(cached) > 0 {
addrs := make([]string, len(cached))
for i := range cached {
addrs[i] = cached[i].addr
}
return addrs
2014-05-01 23:53:19 -07:00
} else if len(d.extServer) != 0 {
addrs := d.externalLookup(device)
cached = make([]cacheEntry, len(addrs))
for i := range addrs {
cached[i] = cacheEntry{
addr: addrs[i],
seen: time.Now(),
}
}
d.registryLock.Lock()
d.registry[device] = cached
d.registryLock.Unlock()
2014-05-01 23:53:19 -07:00
}
return nil
}
func (d *Discoverer) Hint(device string, addrs []string) {
resAddrs := resolveAddrs(addrs)
var id protocol.DeviceID
id.UnmarshalText([]byte(device))
d.registerDevice(nil, Device{
Addresses: resAddrs,
ID: id[:],
})
}
func (d *Discoverer) All() map[protocol.DeviceID][]cacheEntry {
d.registryLock.RLock()
devices := make(map[protocol.DeviceID][]cacheEntry, len(d.registry))
for device, addrs := range d.registry {
addrsCopy := make([]cacheEntry, len(addrs))
copy(addrsCopy, addrs)
devices[device] = addrsCopy
}
d.registryLock.RUnlock()
return devices
}
func (d *Discoverer) announcementPkt() []byte {
var addrs []Address
for _, astr := range d.listenAddrs {
addr, err := net.ResolveTCPAddr("tcp", astr)
if err != nil {
2014-05-14 17:08:56 -07:00
l.Warnln("%v: not announcing %s", err, astr)
continue
} else if debug {
2014-05-14 17:08:56 -07:00
l.Debugf("discover: announcing %s: %#v", astr, addr)
}
if len(addr.IP) == 0 || addr.IP.IsUnspecified() {
addrs = append(addrs, Address{Port: uint16(addr.Port)})
} else if bs := addr.IP.To4(); bs != nil {
addrs = append(addrs, Address{IP: bs, Port: uint16(addr.Port)})
} else if bs := addr.IP.To16(); bs != nil {
addrs = append(addrs, Address{IP: bs, Port: uint16(addr.Port)})
}
}
var pkt = Announce{
Magic: AnnouncementMagic,
This: Device{d.myID[:], addrs},
}
return pkt.MarshalXDR()
}
func (d *Discoverer) sendLocalAnnouncements() {
2014-05-11 15:55:43 -07:00
var addrs = resolveAddrs(d.listenAddrs)
var pkt = Announce{
Magic: AnnouncementMagic,
This: Device{d.myID[:], addrs},
2014-05-11 15:55:43 -07:00
}
msg := pkt.MarshalXDR()
2014-03-28 03:04:48 -07:00
for {
if d.multicastBeacon != nil {
d.multicastBeacon.Send(msg)
}
if d.broadcastBeacon != nil {
d.broadcastBeacon.Send(msg)
}
select {
case <-d.localBcastTick:
case <-d.forcedBcastTick:
}
}
2013-12-22 14:29:23 -07:00
}
2013-12-15 03:43:31 -07:00
func (d *Discoverer) sendExternalAnnouncements() {
defer d.globalWG.Done()
remote, err := net.ResolveUDPAddr("udp", d.extServer)
for err != nil {
l.Warnf("Global discovery: %v; trying again in %v", err, d.errorRetryIntv)
time.Sleep(d.errorRetryIntv)
remote, err = net.ResolveUDPAddr("udp", d.extServer)
2013-12-22 14:29:23 -07:00
}
conn, err := net.ListenUDP("udp", nil)
for err != nil {
l.Warnf("Global discovery: %v; trying again in %v", err, d.errorRetryIntv)
time.Sleep(d.errorRetryIntv)
conn, err = net.ListenUDP("udp", nil)
}
2014-04-18 04:20:42 -07:00
var buf []byte
if d.extPort != 0 {
var pkt = Announce{
Magic: AnnouncementMagic,
This: Device{d.myID[:], []Address{{Port: d.extPort}}},
2014-04-18 04:20:42 -07:00
}
buf = pkt.MarshalXDR()
} else {
buf = d.announcementPkt()
}
var bcastTick = time.Tick(d.globalBcastIntv)
var errTick <-chan time.Time
sendOneAnnouncement := func() {
var ok bool
2014-03-09 00:58:03 -07:00
if debug {
2014-05-14 17:08:56 -07:00
l.Debugf("discover: send announcement -> %v\n%s", remote, hex.Dump(buf))
}
_, err := conn.WriteTo(buf, remote)
2013-12-15 03:43:31 -07:00
if err != nil {
2014-05-14 17:08:56 -07:00
if debug {
l.Debugln("discover: warning:", err)
}
ok = false
2013-12-18 10:29:15 -07:00
} else {
// Verify that the announce server responds positively for our device ID
2014-04-16 06:06:54 -07:00
time.Sleep(1 * time.Second)
res := d.externalLookup(d.myID)
if debug {
2014-05-14 17:08:56 -07:00
l.Debugln("discover: external lookup check:", res)
}
ok = len(res) > 0
}
d.extAnnounceOKmut.Lock()
d.extAnnounceOK = ok
d.extAnnounceOKmut.Unlock()
if ok {
errTick = nil
} else if errTick != nil {
errTick = time.Tick(d.errorRetryIntv)
}
}
2014-08-12 15:29:29 -07:00
// Announce once, immediately
sendOneAnnouncement()
loop:
for {
select {
case <-d.stopGlobal:
break loop
case <-errTick:
sendOneAnnouncement()
case <-bcastTick:
sendOneAnnouncement()
2014-04-16 06:06:54 -07:00
}
2013-12-15 03:43:31 -07:00
}
if debug {
l.Debugln("discover: stopping global")
}
2013-12-15 03:43:31 -07:00
}
func (d *Discoverer) recvAnnouncements(b beacon.Interface) {
2014-03-28 03:04:48 -07:00
for {
buf, addr := b.Recv()
2013-12-22 14:29:23 -07:00
2014-03-09 00:58:03 -07:00
if debug {
l.Debugf("discover: read announcement from %s:\n%s", addr, hex.Dump(buf))
2014-02-20 09:40:15 -07:00
}
var pkt Announce
2014-03-28 03:04:48 -07:00
err := pkt.UnmarshalXDR(buf)
2014-05-11 15:55:43 -07:00
if err != nil && err != io.EOF {
2013-12-15 03:43:31 -07:00
continue
}
var newDevice bool
if bytes.Compare(pkt.This.ID, d.myID[:]) != 0 {
newDevice = d.registerDevice(addr, pkt.This)
2014-05-11 15:55:43 -07:00
}
if newDevice {
2014-05-11 15:55:43 -07:00
select {
case d.forcedBcastTick <- time.Now():
}
2013-12-15 03:43:31 -07:00
}
}
}
func (d *Discoverer) registerDevice(addr net.Addr, device Device) bool {
var id protocol.DeviceID
copy(id[:], device.ID)
d.registryLock.RLock()
current := d.filterCached(d.registry[id])
d.registryLock.RUnlock()
orig := current
for _, a := range device.Addresses {
var deviceAddr string
2014-05-11 15:55:43 -07:00
if len(a.IP) > 0 {
deviceAddr = net.JoinHostPort(net.IP(a.IP).String(), strconv.Itoa(int(a.Port)))
2014-05-11 15:55:43 -07:00
} else if addr != nil {
ua := addr.(*net.UDPAddr)
ua.Port = int(a.Port)
deviceAddr = ua.String()
2014-05-11 15:55:43 -07:00
}
for i := range current {
if current[i].addr == deviceAddr {
current[i].seen = time.Now()
goto done
}
2014-05-11 15:55:43 -07:00
}
current = append(current, cacheEntry{
addr: deviceAddr,
seen: time.Now(),
})
done:
2014-05-11 15:55:43 -07:00
}
2014-05-11 15:55:43 -07:00
if debug {
l.Debugf("discover: register: %v -> %v", id, current)
2014-05-11 15:55:43 -07:00
}
2014-05-11 15:55:43 -07:00
d.registryLock.Lock()
d.registry[id] = current
2014-05-11 15:55:43 -07:00
d.registryLock.Unlock()
2014-07-17 04:38:36 -07:00
if len(current) > len(orig) {
addrs := make([]string, len(current))
for i := range current {
addrs[i] = current[i].addr
}
events.Default.Log(events.DeviceDiscovered, map[string]interface{}{
"device": id.String(),
2014-07-17 04:38:36 -07:00
"addrs": addrs,
})
}
return len(current) > len(orig)
2014-05-11 15:55:43 -07:00
}
func (d *Discoverer) externalLookup(device protocol.DeviceID) []string {
2014-01-26 06:28:41 -07:00
extIP, err := net.ResolveUDPAddr("udp", d.extServer)
2013-12-22 19:35:05 -07:00
if err != nil {
2014-05-14 17:08:56 -07:00
if debug {
l.Debugf("discover: %v; no external lookup", err)
}
2014-02-20 09:40:15 -07:00
return nil
2013-12-22 19:35:05 -07:00
}
conn, err := net.DialUDP("udp", nil, extIP)
2013-12-22 15:13:51 -07:00
if err != nil {
2014-05-14 17:08:56 -07:00
if debug {
l.Debugf("discover: %v; no external lookup", err)
}
2014-02-20 09:40:15 -07:00
return nil
2013-12-22 14:29:23 -07:00
}
2013-12-24 09:10:49 -07:00
defer conn.Close()
2013-12-22 14:29:23 -07:00
2013-12-24 09:10:49 -07:00
err = conn.SetDeadline(time.Now().Add(5 * time.Second))
2013-12-22 19:35:05 -07:00
if err != nil {
2014-05-14 17:08:56 -07:00
if debug {
l.Debugf("discover: %v; no external lookup", err)
}
2014-02-20 09:40:15 -07:00
return nil
2013-12-22 19:35:05 -07:00
}
buf := Query{QueryMagic, device[:]}.MarshalXDR()
2014-02-20 09:40:15 -07:00
_, err = conn.Write(buf)
2013-12-24 09:10:49 -07:00
if err != nil {
2014-05-14 17:08:56 -07:00
if debug {
l.Debugf("discover: %v; no external lookup", err)
}
2014-02-20 09:40:15 -07:00
return nil
2013-12-24 09:10:49 -07:00
}
2013-12-22 14:29:23 -07:00
2014-06-18 14:57:22 -07:00
buf = make([]byte, 2048)
2013-12-24 09:10:49 -07:00
n, err := conn.Read(buf)
if err != nil {
2013-12-24 09:15:21 -07:00
if err, ok := err.(net.Error); ok && err.Timeout() {
// Expected if the server doesn't know about requested device ID
2014-02-20 09:40:15 -07:00
return nil
2013-12-24 09:15:21 -07:00
}
2014-05-14 17:08:56 -07:00
if debug {
l.Debugf("discover: %v; no external lookup", err)
}
2014-02-20 09:40:15 -07:00
return nil
2013-12-24 09:10:49 -07:00
}
2013-12-22 14:29:23 -07:00
2014-03-09 00:58:03 -07:00
if debug {
2014-05-14 17:08:56 -07:00
l.Debugf("discover: read external:\n%s", hex.Dump(buf[:n]))
2014-02-20 09:40:15 -07:00
}
var pkt Announce
2014-02-20 09:40:15 -07:00
err = pkt.UnmarshalXDR(buf[:n])
2014-05-11 15:55:43 -07:00
if err != nil && err != io.EOF {
2014-05-14 17:08:56 -07:00
if debug {
l.Debugln("discover:", err)
}
2014-02-20 09:40:15 -07:00
return nil
2013-12-24 09:10:49 -07:00
}
2013-12-22 14:29:23 -07:00
2014-02-20 09:40:15 -07:00
var addrs []string
2014-05-11 15:55:43 -07:00
for _, a := range pkt.This.Addresses {
deviceAddr := net.JoinHostPort(net.IP(a.IP).String(), strconv.Itoa(int(a.Port)))
addrs = append(addrs, deviceAddr)
2014-02-20 09:40:15 -07:00
}
return addrs
2013-12-22 14:29:23 -07:00
}
2014-05-11 15:55:43 -07:00
func (d *Discoverer) filterCached(c []cacheEntry) []cacheEntry {
for i := 0; i < len(c); {
if ago := time.Since(c[i].seen); ago > d.cacheLifetime {
if debug {
l.Debugf("removing cached address %s: seen %v ago", c[i].addr, ago)
}
c[i] = c[len(c)-1]
c = c[:len(c)-1]
} else {
i++
}
}
return c
}
2014-05-11 15:55:43 -07:00
func addrToAddr(addr *net.TCPAddr) Address {
if len(addr.IP) == 0 || addr.IP.IsUnspecified() {
return Address{Port: uint16(addr.Port)}
} else if bs := addr.IP.To4(); bs != nil {
return Address{IP: bs, Port: uint16(addr.Port)}
} else if bs := addr.IP.To16(); bs != nil {
return Address{IP: bs, Port: uint16(addr.Port)}
}
return Address{}
}
func resolveAddrs(addrs []string) []Address {
var raddrs []Address
for _, addrStr := range addrs {
addrRes, err := net.ResolveTCPAddr("tcp", addrStr)
if err != nil {
continue
}
addr := addrToAddr(addrRes)
if len(addr.IP) > 0 {
raddrs = append(raddrs, addr)
2014-05-16 07:28:52 -07:00
} else {
raddrs = append(raddrs, Address{Port: addr.Port})
2014-05-11 15:55:43 -07:00
}
}
return raddrs
}