From b36ffe1c9d570051a79ddb025071a0806e933c7a Mon Sep 17 00:00:00 2001 From: David Wragg Date: Tue, 13 Oct 2015 15:20:42 +0100 Subject: [PATCH] Support peers that don't have short IDs --- router/connection.go | 19 +++++++++++++------ router/peer.go | 22 ++++++++++++---------- router/peers.go | 8 ++++++++ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/router/connection.go b/router/connection.go index 926d1392..9f047159 100644 --- a/router/connection.go +++ b/router/connection.go @@ -277,7 +277,7 @@ func (features features) Get(key string) string { } func (conn *LocalConnection) parseFeatures(features features) (*Peer, error) { - if err := features.MustHave([]string{"PeerNameFlavour", "Name", "NickName", "ShortID", "UID", "ConnID"}); err != nil { + if err := features.MustHave([]string{"PeerNameFlavour", "Name", "NickName", "UID", "ConnID"}); err != nil { return nil, err } @@ -293,10 +293,15 @@ func (conn *LocalConnection) parseFeatures(features features) (*Peer, error) { nickName := features.Get("NickName") - shortID, err := strconv.ParseUint(features.Get("ShortID"), 10, - PeerShortIDBits) - if err != nil { - return nil, err + var shortID uint64 + var hasShortID bool + if shortIDStr, present := features["ShortID"]; present { + hasShortID = true + shortID, err = strconv.ParseUint(shortIDStr, 10, + PeerShortIDBits) + if err != nil { + return nil, err + } } uid, err := ParsePeerUID(features.Get("UID")) @@ -310,7 +315,9 @@ func (conn *LocalConnection) parseFeatures(features features) (*Peer, error) { } conn.uid ^= remoteConnID - return NewPeer(name, nickName, uid, 0, PeerShortID(shortID)), nil + peer := NewPeer(name, nickName, uid, 0, PeerShortID(shortID)) + peer.HasShortID = hasShortID + return peer, nil } func (conn *LocalConnection) registerRemote(remote *Peer, acceptNewPeer bool) error { diff --git a/router/peer.go b/router/peer.go index 82ae2201..e73d1592 100644 --- a/router/peer.go +++ b/router/peer.go @@ -30,11 +30,12 @@ func randomPeerShortID() PeerShortID { } type PeerSummary struct { - NameByte []byte - NickName string - UID PeerUID - Version uint64 - ShortID PeerShortID + NameByte []byte + NickName string + UID PeerUID + Version uint64 + ShortID PeerShortID + HasShortID bool } type Peer struct { @@ -56,11 +57,12 @@ func NewPeerFromSummary(summary PeerSummary) *Peer { func NewPeer(name PeerName, nickName string, uid PeerUID, version uint64, shortID PeerShortID) *Peer { return NewPeerFromSummary(PeerSummary{ - NameByte: name.Bin(), - NickName: nickName, - UID: uid, - Version: version, - ShortID: shortID, + NameByte: name.Bin(), + NickName: nickName, + UID: uid, + Version: version, + ShortID: shortID, + HasShortID: true, }) } diff --git a/router/peers.go b/router/peers.go index 163f8c48..098a7825 100644 --- a/router/peers.go +++ b/router/peers.go @@ -114,6 +114,10 @@ func (peers *Peers) unlockAndNotify(pending *PeersPendingNotifications) { } func (peers *Peers) addByShortID(peer *Peer, pending *PeersPendingNotifications) { + if !peer.HasShortID { + return + } + entry, ok := peers.byShortID[peer.ShortID] if !ok { entry = ShortIDPeers{peer: peer} @@ -146,6 +150,10 @@ func (peers *Peers) addByShortID(peer *Peer, pending *PeersPendingNotifications) } func (peers *Peers) deleteByShortID(peer *Peer, pending *PeersPendingNotifications) { + if !peer.HasShortID { + return + } + entry := peers.byShortID[peer.ShortID] var otherIndex int