2013-12-15 03:43:31 -07:00
|
|
|
/*
|
|
|
|
This is the local node discovery protocol. In principle we might be better
|
|
|
|
served by something more standardized, such as mDNS / DNS-SD. In practice, this
|
|
|
|
was much easier and quicker to get up and running.
|
|
|
|
|
|
|
|
The mode of operation is to periodically (currently once every 30 seconds)
|
2013-12-22 14:29:23 -07:00
|
|
|
broadcast an Announcement packet to UDP port 21025. The packet has the
|
|
|
|
following format:
|
2013-12-15 03:43:31 -07:00
|
|
|
|
|
|
|
0 1 2 3
|
|
|
|
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2013-12-22 14:29:23 -07:00
|
|
|
| Magic Number (0x20121025) |
|
2013-12-15 03:43:31 -07:00
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2013-12-22 14:29:23 -07:00
|
|
|
| Port Number | Reserved |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
| Length of NodeID |
|
2013-12-15 03:43:31 -07:00
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
/ /
|
|
|
|
\ NodeID (variable length) \
|
|
|
|
/ /
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2013-12-22 15:13:51 -07:00
|
|
|
| Length of IP |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
/ /
|
|
|
|
\ IP (variable length) \
|
|
|
|
/ /
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2013-12-15 03:43:31 -07:00
|
|
|
|
2013-12-22 14:29:23 -07:00
|
|
|
This is the XDR encoding of:
|
|
|
|
|
|
|
|
struct Announcement {
|
|
|
|
unsigned int Magic;
|
|
|
|
unsigned short Port;
|
|
|
|
string NodeID<>;
|
|
|
|
}
|
|
|
|
|
|
|
|
(Hence NodeID is padded to a multiple of 32 bits)
|
|
|
|
|
2013-12-22 15:13:51 -07:00
|
|
|
The sending node's address is not encoded in local announcement -- the Length
|
|
|
|
of IP field is set to zero and the address is taken to be the source address of
|
|
|
|
the announcement. In announcement packets sent by a discovery server in
|
|
|
|
response to a query, the IP is present and the length is either 4 (IPv4) or 16
|
|
|
|
(IPv6).
|
|
|
|
|
|
|
|
Every time such a packet is received, a local table that maps NodeID to Address
|
|
|
|
is updated. When the local node wants to connect to another node with the
|
|
|
|
address specification 'dynamic', this table is consulted.
|
2013-12-22 14:29:23 -07:00
|
|
|
|
|
|
|
For external discovery, an identical packet is sent every 30 minutes to the
|
|
|
|
external discovery server. The server keeps information for up to 60 minutes.
|
|
|
|
To query the server, and UDP packet with the format below is sent.
|
|
|
|
|
|
|
|
0 1 2 3
|
|
|
|
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
| Magic Number (0x19760309) |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
| Length of NodeID |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
/ /
|
|
|
|
\ NodeID (variable length) \
|
|
|
|
/ /
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
|
|
|
This is the XDR encoding of:
|
|
|
|
|
|
|
|
struct Announcement {
|
|
|
|
unsigned int Magic;
|
|
|
|
string NodeID<>;
|
|
|
|
}
|
|
|
|
|
|
|
|
(Hence NodeID is padded to a multiple of 32 bits)
|
|
|
|
|
|
|
|
It is answered with an announcement packet for the queried node ID if the
|
|
|
|
information is available. There is no answer for queries about unknown nodes. A
|
|
|
|
reasonable timeout is recommended instead. (This, combined with server side
|
|
|
|
rate limits for packets per source IP and queries per node ID, prevents the
|
|
|
|
server from being used as an amplifier in a DDoS attack.)
|
2013-12-15 03:43:31 -07:00
|
|
|
*/
|
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-12-18 10:29:15 -07:00
|
|
|
"log"
|
2013-12-15 03:43:31 -07:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2013-12-24 09:10:49 -07:00
|
|
|
|
|
|
|
"github.com/calmh/syncthing/buffers"
|
2013-12-15 03:43:31 -07:00
|
|
|
)
|
|
|
|
|
2013-12-22 14:29:23 -07:00
|
|
|
const (
|
|
|
|
AnnouncementPort = 21025
|
|
|
|
AnnouncementMagic = 0x20121025
|
|
|
|
QueryMagic = 0x19760309
|
|
|
|
)
|
|
|
|
|
2013-12-15 03:43:31 -07:00
|
|
|
type Discoverer struct {
|
2013-12-22 14:29:23 -07:00
|
|
|
MyID string
|
|
|
|
ListenPort int
|
|
|
|
BroadcastIntv time.Duration
|
|
|
|
ExtListenPort int
|
|
|
|
ExtBroadcastIntv time.Duration
|
2013-12-15 03:43:31 -07:00
|
|
|
|
|
|
|
conn *net.UDPConn
|
|
|
|
registry map[string]string
|
|
|
|
registryLock sync.RWMutex
|
2013-12-22 14:29:23 -07:00
|
|
|
extServer string
|
|
|
|
}
|
|
|
|
|
2013-12-22 15:13:51 -07:00
|
|
|
// We tolerate a certain amount of errors because we might be running on
|
2013-12-18 10:29:15 -07:00
|
|
|
// laptops that sleep and wake, have intermittent network connectivity, etc.
|
|
|
|
// When we hit this many errors in succession, we stop.
|
|
|
|
const maxErrors = 30
|
|
|
|
|
2013-12-22 14:29:23 -07:00
|
|
|
func NewDiscoverer(id string, port int, extPort int, extServer string) (*Discoverer, error) {
|
|
|
|
local4 := &net.UDPAddr{IP: net.IP{0, 0, 0, 0}, Port: AnnouncementPort}
|
2013-12-15 03:43:31 -07:00
|
|
|
conn, err := net.ListenUDP("udp4", local4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
disc := &Discoverer{
|
2013-12-22 14:29:23 -07:00
|
|
|
MyID: id,
|
|
|
|
ListenPort: port,
|
|
|
|
BroadcastIntv: 30 * time.Second,
|
|
|
|
ExtListenPort: extPort,
|
|
|
|
ExtBroadcastIntv: 1800 * time.Second,
|
|
|
|
|
|
|
|
conn: conn,
|
|
|
|
registry: make(map[string]string),
|
|
|
|
extServer: extServer,
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
go disc.recvAnnouncements()
|
|
|
|
|
2013-12-22 14:29:23 -07:00
|
|
|
if disc.ListenPort > 0 {
|
|
|
|
disc.sendAnnouncements()
|
|
|
|
}
|
|
|
|
if len(disc.extServer) > 0 && disc.ExtListenPort > 0 {
|
|
|
|
disc.sendExtAnnouncements()
|
|
|
|
}
|
|
|
|
|
2013-12-15 03:43:31 -07:00
|
|
|
return disc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discoverer) sendAnnouncements() {
|
2013-12-22 14:29:23 -07:00
|
|
|
remote4 := &net.UDPAddr{IP: net.IP{255, 255, 255, 255}, Port: AnnouncementPort}
|
2013-12-15 03:43:31 -07:00
|
|
|
|
2013-12-22 19:35:05 -07:00
|
|
|
buf := EncodePacket(Packet{AnnouncementMagic, uint16(d.ListenPort), d.MyID, nil})
|
2013-12-22 14:29:23 -07:00
|
|
|
go d.writeAnnouncements(buf, remote4, d.BroadcastIntv)
|
|
|
|
}
|
2013-12-15 03:43:31 -07:00
|
|
|
|
2013-12-22 14:29:23 -07:00
|
|
|
func (d *Discoverer) sendExtAnnouncements() {
|
2013-12-22 19:35:05 -07:00
|
|
|
extIP, err := net.ResolveUDPAddr("udp", d.extServer+":22025")
|
2013-12-22 14:29:23 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("discover/external: %v; no external announcements", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-22 19:35:05 -07:00
|
|
|
buf := EncodePacket(Packet{AnnouncementMagic, uint16(d.ExtListenPort), d.MyID, nil})
|
|
|
|
go d.writeAnnouncements(buf, extIP, d.ExtBroadcastIntv)
|
2013-12-22 14:29:23 -07:00
|
|
|
}
|
2013-12-15 03:43:31 -07:00
|
|
|
|
2013-12-22 14:29:23 -07:00
|
|
|
func (d *Discoverer) writeAnnouncements(buf []byte, remote *net.UDPAddr, intv time.Duration) {
|
2013-12-18 10:29:15 -07:00
|
|
|
var errCounter = 0
|
|
|
|
var err error
|
|
|
|
for errCounter < maxErrors {
|
2013-12-22 14:29:23 -07:00
|
|
|
_, _, err = d.conn.WriteMsgUDP(buf, nil, remote)
|
2013-12-15 03:43:31 -07:00
|
|
|
if err != nil {
|
2013-12-22 19:35:05 -07:00
|
|
|
log.Println("discover/write: warning:", err)
|
2013-12-18 10:29:15 -07:00
|
|
|
errCounter++
|
|
|
|
} else {
|
|
|
|
errCounter = 0
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
2013-12-22 14:29:23 -07:00
|
|
|
time.Sleep(intv)
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
2013-12-22 14:29:23 -07:00
|
|
|
log.Println("discover/write: %v: stopping due to too many errors:", remote, err)
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discoverer) recvAnnouncements() {
|
|
|
|
var buf = make([]byte, 1024)
|
2013-12-18 10:29:15 -07:00
|
|
|
var errCounter = 0
|
|
|
|
var err error
|
|
|
|
for errCounter < maxErrors {
|
2013-12-22 14:29:23 -07:00
|
|
|
n, addr, err := d.conn.ReadFromUDP(buf)
|
2013-12-15 03:43:31 -07:00
|
|
|
if err != nil {
|
2013-12-22 14:29:23 -07:00
|
|
|
errCounter++
|
2013-12-18 10:29:15 -07:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
2013-12-22 14:29:23 -07:00
|
|
|
|
2013-12-22 19:35:05 -07:00
|
|
|
pkt, err := DecodePacket(buf[:n])
|
|
|
|
if err != nil || pkt.Magic != AnnouncementMagic {
|
2013-12-22 14:29:23 -07:00
|
|
|
errCounter++
|
|
|
|
time.Sleep(time.Second)
|
2013-12-15 03:43:31 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-12-22 14:29:23 -07:00
|
|
|
errCounter = 0
|
|
|
|
|
2013-12-22 19:35:05 -07:00
|
|
|
if pkt.ID != d.MyID {
|
|
|
|
nodeAddr := fmt.Sprintf("%s:%d", addr.IP.String(), pkt.Port)
|
2013-12-15 03:43:31 -07:00
|
|
|
d.registryLock.Lock()
|
2013-12-22 19:35:05 -07:00
|
|
|
if d.registry[pkt.ID] != nodeAddr {
|
|
|
|
d.registry[pkt.ID] = nodeAddr
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
d.registryLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 10:29:15 -07:00
|
|
|
log.Println("discover/read: stopping due to too many errors:", err)
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
|
2013-12-22 15:13:51 -07:00
|
|
|
func (d *Discoverer) externalLookup(node string) (string, bool) {
|
2013-12-22 19:35:05 -07:00
|
|
|
extIP, err := net.ResolveUDPAddr("udp", d.extServer+":22025")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("discover/external: %v; no external lookup", err)
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.DialUDP("udp", nil, extIP)
|
2013-12-22 15:13:51 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("discover/external: %v; no external lookup", err)
|
|
|
|
return "", false
|
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 {
|
|
|
|
log.Printf("discover/external: %v; no external lookup", err)
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2013-12-24 09:10:49 -07:00
|
|
|
_, err = conn.Write(EncodePacket(Packet{QueryMagic, 0, node, nil}))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("discover/external: %v; no external lookup", err)
|
|
|
|
return "", false
|
|
|
|
}
|
2013-12-22 14:29:23 -07:00
|
|
|
|
2013-12-30 13:06:44 -07:00
|
|
|
var buf = buffers.Get(256)
|
2013-12-24 09:10:49 -07:00
|
|
|
defer buffers.Put(buf)
|
2013-12-22 14:29:23 -07:00
|
|
|
|
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 node ID
|
|
|
|
return "", false
|
|
|
|
}
|
2013-12-24 09:10:49 -07:00
|
|
|
log.Printf("discover/external/read: %v; no external lookup", err)
|
|
|
|
return "", false
|
|
|
|
}
|
2013-12-22 14:29:23 -07:00
|
|
|
|
2013-12-24 09:10:49 -07:00
|
|
|
pkt, err := DecodePacket(buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("discover/external/read: %v; no external lookup", err)
|
|
|
|
return "", false
|
|
|
|
}
|
2013-12-22 14:29:23 -07:00
|
|
|
|
2013-12-24 09:10:49 -07:00
|
|
|
if pkt.Magic != AnnouncementMagic {
|
|
|
|
log.Printf("discover/external/read: bad magic; no external lookup", err)
|
2013-12-22 15:13:51 -07:00
|
|
|
return "", false
|
2013-12-22 14:29:23 -07:00
|
|
|
}
|
2013-12-24 09:10:49 -07:00
|
|
|
|
|
|
|
return fmt.Sprintf("%s:%d", ipStr(pkt.IP), pkt.Port), true
|
2013-12-22 14:29:23 -07:00
|
|
|
}
|
|
|
|
|
2013-12-22 15:13:51 -07:00
|
|
|
func (d *Discoverer) Lookup(node string) (string, bool) {
|
|
|
|
d.registryLock.Lock()
|
|
|
|
addr, ok := d.registry[node]
|
|
|
|
d.registryLock.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return addr, true
|
|
|
|
} else if len(d.extServer) != 0 {
|
|
|
|
// We might want to cache this, but not permanently so it needs some intelligence
|
|
|
|
return d.externalLookup(node)
|
2013-12-22 14:29:23 -07:00
|
|
|
}
|
2013-12-22 15:13:51 -07:00
|
|
|
return "", false
|
2013-12-22 14:29:23 -07:00
|
|
|
}
|