From be7b5965511cb024c020568a23efa8c94786bb28 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Sun, 17 Oct 2021 19:42:19 +0200 Subject: [PATCH] Rename field sequence in MessageRaw due to name collision in PacketRaw --- Commands.go | 8 ++++---- Message Encoding.go | 2 +- Message Sequence.go | 23 ++++++++++++----------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Commands.go b/Commands.go index 5328354..477a843 100644 --- a/Commands.go +++ b/Commands.go @@ -139,7 +139,7 @@ func (peer *PeerInfo) peer2Record(allowLocal, allowIPv4, allowIPv6 bool) (result // cmdResponse handles the response to the announcement func (peer *PeerInfo) cmdResponse(msg *MessageResponse, connection *Connection) { // The sequence data is used to correlate this response with the announcement. - if msg.sequence == nil || msg.sequence.data == nil { + if msg.SequenceInfo == nil || msg.SequenceInfo.Data == nil { // If there is no sequence data but there were results returned, it means we received unsolicited response data. It will be rejected. if len(msg.HashesNotFound) > 0 || len(msg.Hash2Peers) > 0 || len(msg.FilesEmbed) > 0 { Filters.LogError("cmdResponse", "unsolicited response data received from %s\n", connection.Address.String()) @@ -149,7 +149,7 @@ func (peer *PeerInfo) cmdResponse(msg *MessageResponse, connection *Connection) } // bootstrap FIND_SELF? - if _, ok := msg.sequence.data.(*bootstrapFindSelf); ok { + if _, ok := msg.SequenceInfo.Data.(*bootstrapFindSelf); ok { for _, hash2Peer := range msg.Hash2Peers { // Make sure no garbage is returned. The key must be self and only Closest is expected. if !bytes.Equal(hash2Peer.ID.Hash, nodeID) || len(hash2Peer.Closest) == 0 { @@ -164,10 +164,10 @@ func (peer *PeerInfo) cmdResponse(msg *MessageResponse, connection *Connection) } // Response to an information request? - if _, ok := msg.sequence.data.(*dht.InformationRequest); ok { + if _, ok := msg.SequenceInfo.Data.(*dht.InformationRequest); ok { // Future: Once multiple information requests are pooled (multiplexed) into one or multiple Announcement sequences (messages), the responses need to be de-pooled. // A simple multiplex structure linked via the sequence containing a map (hash 2 IR) could simplify this. - info := msg.sequence.data.(*dht.InformationRequest) + info := msg.SequenceInfo.Data.(*dht.InformationRequest) if len(msg.HashesNotFound) > 0 { info.Done() diff --git a/Message Encoding.go b/Message Encoding.go index 73cd43c..1686186 100644 --- a/Message Encoding.go +++ b/Message Encoding.go @@ -49,7 +49,7 @@ const ( type MessageRaw struct { protocol.PacketRaw SenderPublicKey *btcec.PublicKey // Sender Public Key, ECDSA (secp256k1) 257-bit - sequence *sequenceExpiry // Sequence + SequenceInfo *SequenceExpiry // Sequence } // MessageAnnouncement is the decoded announcement message. diff --git a/Message Sequence.go b/Message Sequence.go index a871bcd..97a2822 100644 --- a/Message Sequence.go +++ b/Message Sequence.go @@ -27,19 +27,20 @@ import ( // sequences stores all sequence numbers that are valid at the moment. The value represents the time the sequence number was used. // Key = Peer ID + Sequence Number -var sequences map[string]*sequenceExpiry +var sequences map[string]*SequenceExpiry var sequencesMutex sync.Mutex -type sequenceExpiry struct { +// SequenceExpiry contains the decoded sequence information of a message. +type SequenceExpiry struct { sequence uint32 // Sequence number created time.Time // When the sequence was created. expires time.Time // When the sequence expires. This can be extended on the fly! counter int // How many replies used the sequence. Multiple Response messages may be returned for a single Announcement one. - data interface{} // Optional high-level data associated with the sequence + Data interface{} // Optional high-level data associated with the sequence } func initMessageSequence() { - sequences = make(map[string]*sequenceExpiry) + sequences = make(map[string]*SequenceExpiry) // auto-delete worker to remove expired sequences go func() { @@ -60,12 +61,12 @@ func initMessageSequence() { // msgNewSequence returns a new sequence and registers it. messageSequence must point to the variable holding the continuous next sequence number. // Use only for Announcement and Ping messages. -func msgNewSequence(publicKey *btcec.PublicKey, messageSequence *uint32, data interface{}) (info *sequenceExpiry) { - info = &sequenceExpiry{ +func msgNewSequence(publicKey *btcec.PublicKey, messageSequence *uint32, data interface{}) (info *SequenceExpiry) { + info = &SequenceExpiry{ sequence: atomic.AddUint32(messageSequence, 1), created: time.Now(), expires: time.Now().Add(time.Duration(ReplyTimeout) * time.Second), - data: data, + Data: data, } // Add the sequence to the list. Sequences are unique enough that collisions are unlikely and negligible. @@ -78,12 +79,12 @@ func msgNewSequence(publicKey *btcec.PublicKey, messageSequence *uint32, data in } // msgArbitrarySequence returns an arbitrary sequence to be used for uncontacted peers -func msgArbitrarySequence(publicKey *btcec.PublicKey, data interface{}) (info *sequenceExpiry) { - info = &sequenceExpiry{ +func msgArbitrarySequence(publicKey *btcec.PublicKey, data interface{}) (info *SequenceExpiry) { + info = &SequenceExpiry{ sequence: rand.Uint32(), created: time.Now(), expires: time.Now().Add(time.Duration(ReplyTimeout) * time.Second), - data: data, + Data: data, } // Add the sequence to the list. Sequences are unique enough that collisions are unlikely and negligible. @@ -129,7 +130,7 @@ func msgValidateSequence(raw *MessageRaw, invalidate bool) (valid bool, rtt time sequence.expires = time.Now().Add(time.Duration(ReplyTimeout) * time.Second / 2) } - raw.sequence = sequence + raw.SequenceInfo = sequence return sequence.expires.After(time.Now()), rtt }