lib/protocol: Use DeviceID in protocol messages, with custom marshalling

This makes the device ID a real type that can be used in the protobuf
schema. That avoids the juggling back and forth from []byte in a bunch
of places and simplifies the code.

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3695
This commit is contained in:
Jakob Borg
2016-10-29 21:56:24 +00:00
committed by Audrius Butkevicius
parent 1cdfef4d6a
commit 0296c23685
13 changed files with 728 additions and 195 deletions

View File

@@ -7,7 +7,6 @@
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"flag"
@@ -45,7 +44,7 @@ func main() {
flag.Parse()
if fake {
log.Println("My ID:", protocol.DeviceIDFromBytes(myID))
log.Println("My ID:", myID)
}
runbeacon(beacon.NewMulticast(mc), fake)
@@ -75,17 +74,17 @@ func recv(bc beacon.Interface) {
var ann discover.Announce
ann.Unmarshal(data[4:])
if bytes.Equal(ann.ID, myID) {
if ann.ID == myID {
// This is one of our own fake packets, don't print it.
continue
}
// Print announcement details for the first packet from a given
// device ID and source address, or if -all was given.
key := string(ann.ID) + src.String()
key := ann.ID.String() + src.String()
if all || !seen[key] {
log.Printf("Announcement from %v\n", src)
log.Printf(" %v at %s\n", protocol.DeviceIDFromBytes(ann.ID), strings.Join(ann.Addresses, ", "))
log.Printf(" %v at %s\n", ann.ID, strings.Join(ann.Addresses, ", "))
seen[key] = true
}
}
@@ -106,9 +105,9 @@ func send(bc beacon.Interface) {
}
// returns a random but recognizable device ID
func randomDeviceID() []byte {
var id [32]byte
func randomDeviceID() protocol.DeviceID {
var id protocol.DeviceID
copy(id[:], randomPrefix)
rand.Read(id[len(randomPrefix):])
return id[:]
return id
}