2014-11-20 21:24:11 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-11-20 21:24:11 +00:00
|
|
|
|
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-01-13 12:22:56 +00:00
|
|
|
"github.com/syncthing/protocol"
|
2014-11-20 21:24:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for _, proto := range []string{"udp", "udp4", "udp6"} {
|
|
|
|
Register(proto, func(uri *url.URL, pkt *Announce) (Client, error) {
|
|
|
|
c := &UDPClient{}
|
|
|
|
err := c.Start(uri, pkt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UDPClient struct {
|
|
|
|
url *url.URL
|
|
|
|
|
|
|
|
id protocol.DeviceID
|
|
|
|
|
|
|
|
stop chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
listenAddress *net.UDPAddr
|
|
|
|
|
|
|
|
globalBroadcastInterval time.Duration
|
|
|
|
errorRetryInterval time.Duration
|
|
|
|
|
|
|
|
status bool
|
|
|
|
mut sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *UDPClient) Start(uri *url.URL, pkt *Announce) error {
|
|
|
|
d.url = uri
|
|
|
|
d.id = protocol.DeviceIDFromBytes(pkt.This.ID)
|
|
|
|
d.stop = make(chan struct{})
|
|
|
|
|
|
|
|
params := uri.Query()
|
|
|
|
// The address must not have a port, as otherwise both announce and lookup
|
|
|
|
// sockets would try to bind to the same port.
|
|
|
|
addr, err := net.ResolveUDPAddr(d.url.Scheme, params.Get("listenaddress")+":0")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.listenAddress = addr
|
|
|
|
|
|
|
|
broadcastSeconds, err := strconv.ParseUint(params.Get("broadcast"), 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
d.globalBroadcastInterval = DefaultGlobalBroadcastInterval
|
|
|
|
} else {
|
|
|
|
d.globalBroadcastInterval = time.Duration(broadcastSeconds) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
retrySeconds, err := strconv.ParseUint(params.Get("retry"), 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
d.errorRetryInterval = DefaultErrorRetryInternval
|
|
|
|
} else {
|
|
|
|
d.errorRetryInterval = time.Duration(retrySeconds) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
d.wg.Add(1)
|
|
|
|
go d.broadcast(pkt.MustMarshalXDR())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *UDPClient) broadcast(pkt []byte) {
|
|
|
|
defer d.wg.Done()
|
|
|
|
|
|
|
|
conn, err := net.ListenUDP(d.url.Scheme, d.listenAddress)
|
|
|
|
for err != nil {
|
2015-03-11 19:56:05 +00:00
|
|
|
if debug {
|
|
|
|
l.Debugf("discover %s: broadcast listen: %v; trying again in %v", d.url, err, d.errorRetryInterval)
|
|
|
|
}
|
2014-11-20 21:24:11 +00:00
|
|
|
select {
|
|
|
|
case <-d.stop:
|
|
|
|
return
|
2014-12-03 07:34:15 +00:00
|
|
|
case <-time.After(d.errorRetryInterval):
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
conn, err = net.ListenUDP(d.url.Scheme, d.listenAddress)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
remote, err := net.ResolveUDPAddr(d.url.Scheme, d.url.Host)
|
|
|
|
for err != nil {
|
2015-03-11 19:56:05 +00:00
|
|
|
if debug {
|
2015-03-08 16:46:15 +00:00
|
|
|
l.Debugf("discover %s: broadcast resolve: %v; trying again in %v", d.url, err, d.errorRetryInterval)
|
|
|
|
}
|
2014-11-20 21:24:11 +00:00
|
|
|
select {
|
|
|
|
case <-d.stop:
|
|
|
|
return
|
2014-12-03 07:34:15 +00:00
|
|
|
case <-time.After(d.errorRetryInterval):
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
remote, err = net.ResolveUDPAddr(d.url.Scheme, d.url.Host)
|
|
|
|
}
|
|
|
|
|
2014-12-03 07:34:15 +00:00
|
|
|
timer := time.NewTimer(0)
|
2014-11-20 21:24:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-d.stop:
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-timer.C:
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: broadcast: Sending self announcement to %v", d.url, remote)
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.WriteTo(pkt, remote)
|
|
|
|
if err != nil {
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: broadcast: Failed to send self announcement: %s", d.url, err)
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
ok = false
|
|
|
|
} else {
|
|
|
|
// Verify that the announce server responds positively for our device ID
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
res := d.Lookup(d.id)
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: broadcast: Self-lookup returned: %v", d.url, res)
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
ok = len(res) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
d.mut.Lock()
|
|
|
|
d.status = ok
|
|
|
|
d.mut.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
timer.Reset(d.globalBroadcastInterval)
|
|
|
|
} else {
|
|
|
|
timer.Reset(d.errorRetryInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *UDPClient) Lookup(device protocol.DeviceID) []string {
|
|
|
|
extIP, err := net.ResolveUDPAddr(d.url.Scheme, d.url.Host)
|
|
|
|
if err != nil {
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: Lookup(%s): %s", d.url, device, err)
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.DialUDP(d.url.Scheme, d.listenAddress, extIP)
|
|
|
|
if err != nil {
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: Lookup(%s): %s", d.url, device, err)
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.SetDeadline(time.Now().Add(5 * time.Second))
|
|
|
|
if err != nil {
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: Lookup(%s): %s", d.url, device, err)
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := Query{QueryMagic, device[:]}.MustMarshalXDR()
|
|
|
|
_, err = conn.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: Lookup(%s): %s", d.url, device, err)
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = make([]byte, 2048)
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err, ok := err.(net.Error); ok && err.Timeout() {
|
|
|
|
// Expected if the server doesn't know about requested device ID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: Lookup(%s): %s", d.url, device, err)
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var pkt Announce
|
|
|
|
err = pkt.UnmarshalXDR(buf[:n])
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
if debug {
|
2014-12-18 23:12:12 +00:00
|
|
|
l.Debugf("discover %s: Lookup(%s): %s\n%s", d.url, device, err, hex.Dump(buf[:n]))
|
2014-11-20 21:24:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var addrs []string
|
|
|
|
for _, a := range pkt.This.Addresses {
|
|
|
|
deviceAddr := net.JoinHostPort(net.IP(a.IP).String(), strconv.Itoa(int(a.Port)))
|
|
|
|
addrs = append(addrs, deviceAddr)
|
|
|
|
}
|
2014-12-18 23:12:12 +00:00
|
|
|
if debug {
|
|
|
|
l.Debugf("discover %s: Lookup(%s) result: %v", d.url, device, addrs)
|
|
|
|
}
|
2014-11-20 21:24:11 +00:00
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *UDPClient) Stop() {
|
|
|
|
if d.stop != nil {
|
|
|
|
close(d.stop)
|
|
|
|
d.wg.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *UDPClient) StatusOK() bool {
|
|
|
|
d.mut.RLock()
|
|
|
|
defer d.mut.RUnlock()
|
|
|
|
return d.status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *UDPClient) Address() string {
|
|
|
|
return d.url.String()
|
|
|
|
}
|