peer_protocol/handshake.go | 30 +++++++++++++++++------------- diff --git a/peer_protocol/handshake.go b/peer_protocol/handshake.go index 5e6f123dd99379e8605cdf36419e8d2112e57832..ba81a0cf385974123f5734dd990c6780364009f9 100644 --- a/peer_protocol/handshake.go +++ b/peer_protocol/handshake.go @@ -2,12 +2,11 @@ package peer_protocol import ( "encoding/hex" + "errors" "fmt" "io" + "strconv" - "golang.org/x/xerrors" - - "github.com/anacrolix/missinggo/v2" "github.com/anacrolix/torrent/metainfo" ) @@ -34,8 +33,8 @@ type ( PeerExtensionBits [8]byte ) -func (me PeerExtensionBits) String() string { - return hex.EncodeToString(me[:]) +func (pex PeerExtensionBits) String() string { + return hex.EncodeToString(pex[:]) } func NewPeerExtensionBytes(bits ...ExtensionBit) (ret PeerExtensionBits) { @@ -98,7 +97,7 @@ } // Wait until writes complete before returning from handshake. err = <-writeDone if err != nil { - err = fmt.Errorf("error writing: %s", err) + err = fmt.Errorf("error writing: %w", err) } }() @@ -119,16 +118,21 @@ } var b [68]byte _, err = io.ReadFull(sock, b[:68]) if err != nil { - err = xerrors.Errorf("while reading: %w", err) - return + return res, fmt.Errorf("while reading: %w", err) } if string(b[:20]) != Protocol { - err = xerrors.Errorf("unexpected protocol string") - return + return res, errors.New("unexpected protocol string") + } + + copyExact := func(dst []byte, src []byte) { + if dstLen, srcLen := uint64(len(dst)), uint64(len(src)); dstLen != srcLen { + panic("dst len " + strconv.FormatUint(dstLen,10) + " != src len " + strconv.FormatUint(srcLen,10)) + } + copy(dst, src) } - missinggo.CopyExact(&res.PeerExtensionBits, b[20:28]) - missinggo.CopyExact(&res.Hash, b[28:48]) - missinggo.CopyExact(&res.PeerID, b[48:68]) + copyExact(res.PeerExtensionBits[:], b[20:28]) + copyExact(res.Hash[:], b[28:48]) + copyExact(res.PeerID[:], b[48:68]) // peerExtensions.Add(res.PeerExtensionBits.String(), 1) // TODO: Maybe we can just drop peers here if we're not interested. This