Refactor node ID handling, use check digits (fixes #269)

New node ID:s contain four Luhn check digits and are grouped
differently. Code uses NodeID type instead of string, so it's formatted
homogenously everywhere.
This commit is contained in:
Jakob Borg 2014-06-30 01:42:03 +02:00
parent aff41d0b08
commit 926b08c197
1 changed files with 20 additions and 3 deletions

View File

@ -17,6 +17,7 @@ import (
"time"
"github.com/calmh/syncthing/discover"
"github.com/calmh/syncthing/protocol"
"github.com/golang/groupcache/lru"
"github.com/juju/ratelimit"
)
@ -32,7 +33,7 @@ type address struct {
}
var (
nodes = make(map[string]node)
nodes = make(map[protocol.NodeID]node)
lock sync.Mutex
queries = 0
announces = 0
@ -182,8 +183,16 @@ func handleAnnounceV2(addr *net.UDPAddr, buf []byte) {
updated: time.Now(),
}
var id protocol.NodeID
if len(pkt.This.ID) == 32 {
// Raw node ID
copy(id[:], pkt.This.ID)
} else {
id.UnmarshalText(pkt.This.ID)
}
lock.Lock()
nodes[pkt.This.ID] = node
nodes[id] = node
lock.Unlock()
}
@ -199,8 +208,16 @@ func handleQueryV2(conn *net.UDPConn, addr *net.UDPAddr, buf []byte) {
log.Printf("<- %v %#v", addr, pkt)
}
var id protocol.NodeID
if len(pkt.NodeID) == 32 {
// Raw node ID
copy(id[:], pkt.NodeID)
} else {
id.UnmarshalText(pkt.NodeID)
}
lock.Lock()
node, ok := nodes[pkt.NodeID]
node, ok := nodes[id]
queries++
lock.Unlock()