changes as per review

This commit is contained in:
Aarsh Shah 2020-06-03 14:31:16 +05:30 committed by Steven Allen
parent c4a1e220ac
commit 513e13d0d2
1 changed files with 26 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package identify
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"runtime/debug" "runtime/debug"
"sync" "sync"
"time" "time"
@ -44,8 +45,11 @@ const LibP2PVersion = "ipfs/0.1.0"
// Deprecated: Set this with the UserAgent option. // Deprecated: Set this with the UserAgent option.
var ClientVersion = "github.com/libp2p/go-libp2p" var ClientVersion = "github.com/libp2p/go-libp2p"
var legacyIDSize = 2 * 1024 // 2k Bytes var (
var signedIDSize = 8 * 1024 // 8K legacyIDSize = 2 * 1024 // 2k Bytes
signedIDSize = 8 * 1024 // 8K
maxMessages = 10
)
func init() { func init() {
bi, ok := debug.ReadBuildInfo() bi, ok := debug.ReadBuildInfo()
@ -407,28 +411,35 @@ func (ids *IDService) handleIdentifyResponse(s network.Stream) {
c := s.Conn() c := s.Conn()
r := ggio.NewDelimitedReader(s, signedIDSize) r := ggio.NewDelimitedReader(s, signedIDSize)
mes := pb.Identify{} mes := &pb.Identify{}
if err := r.ReadMsg(&mes); err != nil {
log.Warning("error reading identify message: ", err)
s.Reset()
return
}
if mes.More { if err := readAllIDMessages(r, mes); err != nil {
m := &pb.Identify{}
if err := r.ReadMsg(m); err != nil {
log.Warning("error reading identify message: ", err) log.Warning("error reading identify message: ", err)
s.Reset() s.Reset()
return return
} }
mes.SignedPeerRecord = m.SignedPeerRecord
}
defer func() { go helpers.FullClose(s) }() defer func() { go helpers.FullClose(s) }()
log.Debugf("%s received message from %s %s", s.Protocol(), c.RemotePeer(), c.RemoteMultiaddr()) log.Debugf("%s received message from %s %s", s.Protocol(), c.RemotePeer(), c.RemoteMultiaddr())
ids.consumeMessage(&mes, c) ids.consumeMessage(mes, c)
}
func readAllIDMessages(r ggio.Reader, finalMsg proto.Message) error {
mes := &pb.Identify{}
for i := 0; i < maxMessages; i++ {
switch err := r.ReadMsg(mes); err {
case io.EOF:
return nil
case nil:
proto.Merge(finalMsg, mes)
default:
return err
}
}
return fmt.Errorf("too many parts")
} }
func (ids *IDService) getSnapshot() *identifySnapshot { func (ids *IDService) getSnapshot() *identifySnapshot {