18 "github.com/RoaringBitmap/roaring"
19 "github.com/anacrolix/generics"
20 . "github.com/anacrolix/generics"
21 "github.com/anacrolix/log"
22 "github.com/anacrolix/missinggo/v2/bitmap"
23 "github.com/anacrolix/multiless"
24 "golang.org/x/exp/maps"
25 "golang.org/x/time/rate"
27 "github.com/anacrolix/torrent/bencode"
28 "github.com/anacrolix/torrent/internal/alloclim"
29 "github.com/anacrolix/torrent/merkle"
30 "github.com/anacrolix/torrent/metainfo"
31 "github.com/anacrolix/torrent/mse"
32 pp "github.com/anacrolix/torrent/peer_protocol"
33 utHolepunch "github.com/anacrolix/torrent/peer_protocol/ut-holepunch"
36 // Maintains the state of a BitTorrent-protocol based connection with a peer.
37 type PeerConn struct {
41 protocolLogger log.Logger
46 // A string that should identify the PeerConn's net.Conn endpoints. The net.Conn could
47 // be wrapping WebRTC, uTP, or TCP etc. Used in writing the conn status for peers.
52 PeerExtensionBytes pp.PeerExtensionBits
55 // The local extended protocols to advertise in the extended handshake, and to support receiving
56 // from the peer. This will point to the Client default when the PeerConnAdded callback is
57 // invoked. Do not modify this, point it to your own instance. Do not modify the destination
58 // after returning from the callback.
59 LocalLtepProtocolMap *LocalLtepProtocolMap
61 // The actual Conn, used for closing, and setting socket options. Do not use methods on this
62 // while holding any mutexes.
64 // The Reader and Writer for this Conn, with hooks installed for stats,
65 // limiting, deadlines etc.
69 messageWriter peerConnMsgWriter
71 // The peer's extension map, as sent in their extended handshake.
72 PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber
73 PeerClientName atomic.Value
74 uploadTimer *time.Timer
77 // The pieces the peer has claimed to have.
78 _peerPieces roaring.Bitmap
79 // The peer has everything. This can occur due to a special message, when
80 // we may not even know the number of pieces in the torrent yet.
83 peerRequestDataAllocLimiter alloclim.Limiter
85 outstandingHolepunchingRendezvous map[netip.AddrPort]struct{}
87 // Hash requests sent to the peer. If there's an issue we probably don't want to reissue these,
88 // because I haven't implemented it smart enough yet.
89 sentHashRequests map[hashRequest]struct{}
90 // Hash pieces received from the peer, mapped from pieces root to piece layer hashes. This way
91 // we can verify all the pieces for a file when they're all arrived before submitting them to
93 receivedHashPieces map[[32]byte][][32]byte
96 func (cn *PeerConn) pexStatus() string {
97 if !cn.bitExtensionEnabled(pp.ExtensionBitLtep) {
98 return "extended protocol disabled"
100 if cn.PeerExtensionIDs == nil {
101 return "pending extended handshake"
103 if !cn.supportsExtension(pp.ExtensionNamePex) {
108 "%v conns, %v unsent events",
109 len(cn.pex.remoteLiveConns),
113 // This alternative branch prints out the remote live conn addresses.
115 "%v conns, %v unsent events",
116 strings.Join(generics.SliceMap(
117 maps.Keys(cn.pex.remoteLiveConns),
118 func(from netip.AddrPort) string {
126 func (cn *PeerConn) peerImplStatusLines() []string {
129 fmt.Sprintf("peer id: %+q", cn.PeerID),
130 fmt.Sprintf("extensions: %v", cn.PeerExtensionBytes),
131 fmt.Sprintf("ltep extensions: %v", cn.PeerExtensionIDs),
132 fmt.Sprintf("pex: %s", cn.pexStatus()),
136 // Returns true if the connection is over IPv6.
137 func (cn *PeerConn) ipv6() bool {
142 return len(ip) == net.IPv6len
145 // Returns true the if the dialer/initiator has the higher client peer ID. See
146 // https://github.com/arvidn/libtorrent/blame/272828e1cc37b042dfbbafa539222d8533e99755/src/bt_peer_connection.cpp#L3536-L3557.
147 // As far as I can tell, Transmission just keeps the oldest connection.
148 func (cn *PeerConn) isPreferredDirection() bool {
149 // True if our client peer ID is higher than the remote's peer ID.
150 return bytes.Compare(cn.PeerID[:], cn.t.cl.peerID[:]) < 0 == cn.outgoing
153 // Returns whether the left connection should be preferred over the right one,
154 // considering only their networking properties. If ok is false, we can't
156 func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) bool {
157 var ml multiless.Computation
158 ml = ml.Bool(r.isPreferredDirection(), l.isPreferredDirection())
159 ml = ml.Bool(l.utp(), r.utp())
160 ml = ml.Bool(r.ipv6(), l.ipv6())
164 func (cn *PeerConn) peerHasAllPieces() (all, known bool) {
165 if cn.peerSentHaveAll {
168 if !cn.t.haveInfo() {
171 return cn._peerPieces.GetCardinality() == uint64(cn.t.numPieces()), true
174 func (cn *PeerConn) onGotInfo(info *metainfo.Info) {
175 cn.setNumPieces(info.NumPieces())
178 // Correct the PeerPieces slice length. Return false if the existing slice is invalid, such as by
179 // receiving badly sized BITFIELD, or invalid HAVE messages.
180 func (cn *PeerConn) setNumPieces(num pieceIndex) {
181 cn._peerPieces.RemoveRange(bitmap.BitRange(num), bitmap.ToEnd)
182 cn.peerPiecesChanged()
185 func (cn *PeerConn) peerPieces() *roaring.Bitmap {
186 return &cn._peerPieces
189 func (cn *PeerConn) connectionFlags() string {
190 var sb strings.Builder
191 add := func(s string) {
197 // From first relevant to last.
198 add(string(cn.Discovery))
202 if cn.cryptoMethod == mse.CryptoMethodRC4 {
204 } else if cn.headerEncrypted {
215 func (cn *PeerConn) utp() bool {
216 return parseNetworkString(cn.Network).Udp
219 func (cn *PeerConn) onClose() {
220 if cn.pex.IsEnabled() {
227 if cb := cn.callbacks.PeerConnClosed; cb != nil {
232 // Writes a message into the write buffer. Returns whether it's okay to keep writing. Writing is
233 // done asynchronously, so it may be that we're not able to honour backpressure from this method.
234 func (cn *PeerConn) write(msg pp.Message) bool {
235 torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
236 // We don't need to track bytes here because the connection's Writer has that behaviour injected
237 // (although there's some delay between us buffering the message, and the connection writer
238 // flushing it out.).
239 notFull := cn.messageWriter.write(msg)
240 // Last I checked only Piece messages affect stats, and we don't write those.
246 func (cn *PeerConn) requestMetadataPiece(index int) {
247 eID := cn.PeerExtensionIDs[pp.ExtensionNameMetadata]
248 if eID == pp.ExtensionDeleteNumber {
251 if index < len(cn.metadataRequests) && cn.metadataRequests[index] {
254 cn.protocolLogger.WithDefaultLevel(log.Debug).Printf("requesting metadata piece %d", index)
255 cn.write(pp.MetadataExtensionRequestMsg(eID, index))
256 for index >= len(cn.metadataRequests) {
257 cn.metadataRequests = append(cn.metadataRequests, false)
259 cn.metadataRequests[index] = true
262 func (cn *PeerConn) requestedMetadataPiece(index int) bool {
263 return index < len(cn.metadataRequests) && cn.metadataRequests[index]
266 func (cn *PeerConn) onPeerSentCancel(r Request) {
267 if _, ok := cn.peerRequests[r]; !ok {
268 torrent.Add("unexpected cancels received", 1)
271 if cn.fastEnabled() {
274 delete(cn.peerRequests, r)
278 func (cn *PeerConn) choke(msg messageWriter) (more bool) {
283 more = msg(pp.Message{
286 if !cn.fastEnabled() {
287 cn.deleteAllPeerRequests()
292 func (cn *PeerConn) deleteAllPeerRequests() {
293 for _, state := range cn.peerRequests {
294 state.allocReservation.Drop()
296 cn.peerRequests = nil
299 func (cn *PeerConn) unchoke(msg func(pp.Message) bool) bool {
304 return msg(pp.Message{
309 func (pc *PeerConn) writeInterested(interested bool) bool {
310 return pc.write(pp.Message{
311 Type: func() pp.MessageType {
315 return pp.NotInterested
321 func (me *PeerConn) _request(r Request) bool {
322 return me.write(pp.Message{
330 func (me *PeerConn) _cancel(r RequestIndex) bool {
331 me.write(makeCancelMessage(me.t.requestIndexToRequest(r)))
332 return me.remoteRejectsCancels()
335 // Whether we should expect a reject message after sending a cancel.
336 func (me *PeerConn) remoteRejectsCancels() bool {
337 if !me.fastEnabled() {
340 if me.remoteIsTransmission() {
341 // Transmission did not send rejects for received cancels. See
342 // https://github.com/transmission/transmission/pull/2275. Fixed in 4.0.0-beta.1 onward in
343 // https://github.com/transmission/transmission/commit/76719bf34c255da4fca991c2ad3fa4b65d2154b1.
344 // Peer ID prefix scheme described
345 // https://github.com/transmission/transmission/blob/7ec7607bbcf0fa99bd4b157b9b0f0c411d59f45d/CMakeLists.txt#L128-L149.
346 return me.PeerID[3] >= '4'
351 func (cn *PeerConn) fillWriteBuffer() {
352 if cn.messageWriter.writeBuffer.Len() > writeBufferLowWaterLen {
353 // Fully committing to our max requests requires sufficient space (see
354 // maxLocalToRemoteRequests). Flush what we have instead. We also prefer always to make
355 // requests than to do PEX or upload, so we short-circuit before handling those. Any update
356 // request reason will not be cleared, so we'll come right back here when there's space. We
357 // can't do this in maybeUpdateActualRequestState because it's a method on Peer and has no
358 // knowledge of write buffers.
361 cn.requestMissingHashes()
362 cn.maybeUpdateActualRequestState()
363 if cn.pex.IsEnabled() {
364 if flow := cn.pex.Share(cn.write); !flow {
371 func (cn *PeerConn) have(piece pieceIndex) {
372 if cn.sentHaves.Get(bitmap.BitIndex(piece)) {
377 Index: pp.Integer(piece),
379 cn.sentHaves.Add(bitmap.BitIndex(piece))
382 func (cn *PeerConn) postBitfield() {
383 if cn.sentHaves.Len() != 0 {
384 panic("bitfield must be first have-related message sent")
386 if !cn.t.haveAnyPieces() {
391 Bitfield: cn.t.bitfield(),
393 cn.sentHaves = bitmap.Bitmap{cn.t._completedPieces.Clone()}
396 func (cn *PeerConn) handleUpdateRequests() {
397 // The writer determines the request state as needed when it can write.
401 func (cn *PeerConn) raisePeerMinPieces(newMin pieceIndex) {
402 if newMin > cn.peerMinPieces {
403 cn.peerMinPieces = newMin
407 func (cn *PeerConn) peerSentHave(piece pieceIndex) error {
408 if cn.t.haveInfo() && piece >= cn.t.numPieces() || piece < 0 {
409 return errors.New("invalid piece")
411 if cn.peerHasPiece(piece) {
414 cn.raisePeerMinPieces(piece + 1)
415 if !cn.peerHasPiece(piece) {
416 cn.t.incPieceAvailability(piece)
418 cn._peerPieces.Add(uint32(piece))
419 if cn.t.wantPieceIndex(piece) {
420 cn.updateRequests("have")
422 cn.peerPiecesChanged()
426 func (cn *PeerConn) peerSentBitfield(bf []bool) error {
428 panic("expected bitfield length divisible by 8")
430 // We know that the last byte means that at most the last 7 bits are wasted.
431 cn.raisePeerMinPieces(pieceIndex(len(bf) - 7))
432 if cn.t.haveInfo() && len(bf) > int(cn.t.numPieces()) {
433 // Ignore known excess pieces.
434 bf = bf[:cn.t.numPieces()]
436 bm := boolSliceToBitmap(bf)
437 if cn.t.haveInfo() && pieceIndex(bm.GetCardinality()) == cn.t.numPieces() {
438 cn.onPeerHasAllPieces()
442 cn.raisePeerMinPieces(pieceIndex(bm.Maximum()) + 1)
444 shouldUpdateRequests := false
445 if cn.peerSentHaveAll {
446 if !cn.t.deleteConnWithAllPieces(&cn.Peer) {
449 cn.peerSentHaveAll = false
450 if !cn._peerPieces.IsEmpty() {
451 panic("if peer has all, we expect no individual peer pieces to be set")
454 bm.Xor(&cn._peerPieces)
456 cn.peerSentHaveAll = false
457 // bm is now 'on' for pieces that are changing
458 bm.Iterate(func(x uint32) bool {
460 if cn._peerPieces.Contains(x) {
461 // Then we must be losing this piece
462 cn.t.decPieceAvailability(pi)
464 if !shouldUpdateRequests && cn.t.wantPieceIndex(pieceIndex(x)) {
465 shouldUpdateRequests = true
467 // We must be gaining this piece
468 cn.t.incPieceAvailability(pieceIndex(x))
472 // Apply the changes. If we had everything previously, this should be empty, so xor is the same
474 cn._peerPieces.Xor(&bm)
475 if shouldUpdateRequests {
476 cn.updateRequests("bitfield")
478 // We didn't guard this before, I see no reason to do it now.
479 cn.peerPiecesChanged()
483 func (cn *PeerConn) onPeerHasAllPiecesNoTriggers() {
486 cn._peerPieces.Iterate(func(x uint32) bool {
487 t.decPieceAvailability(pieceIndex(x))
491 t.addConnWithAllPieces(&cn.Peer)
492 cn.peerSentHaveAll = true
493 cn._peerPieces.Clear()
496 func (cn *PeerConn) onPeerHasAllPieces() {
497 cn.onPeerHasAllPiecesNoTriggers()
498 cn.peerHasAllPiecesTriggers()
501 func (cn *PeerConn) peerHasAllPiecesTriggers() {
502 if !cn.t._pendingPieces.IsEmpty() {
503 cn.updateRequests("Peer.onPeerHasAllPieces")
505 cn.peerPiecesChanged()
508 func (cn *PeerConn) onPeerSentHaveAll() error {
509 cn.onPeerHasAllPieces()
513 func (cn *PeerConn) peerSentHaveNone() error {
514 if !cn.peerSentHaveAll {
515 cn.t.decPeerPieceAvailability(&cn.Peer)
517 cn._peerPieces.Clear()
518 cn.peerSentHaveAll = false
519 cn.peerPiecesChanged()
523 func (c *PeerConn) requestPendingMetadata() {
527 if c.PeerExtensionIDs[pp.ExtensionNameMetadata] == 0 {
528 // Peer doesn't support this.
531 // Request metadata pieces that we don't have in a random order.
533 for index := 0; index < c.t.metadataPieceCount(); index++ {
534 if !c.t.haveMetadataPiece(index) && !c.requestedMetadataPiece(index) {
535 pending = append(pending, index)
538 rand.Shuffle(len(pending), func(i, j int) { pending[i], pending[j] = pending[j], pending[i] })
539 for _, i := range pending {
540 c.requestMetadataPiece(i)
544 func (cn *PeerConn) wroteMsg(msg *pp.Message) {
545 torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
546 if msg.Type == pp.Extended {
547 for name, id := range cn.PeerExtensionIDs {
548 if id != msg.ExtendedID {
551 torrent.Add(fmt.Sprintf("Extended messages written for protocol %q", name), 1)
554 cn.allStats(func(cs *ConnStats) { cs.wroteMsg(msg) })
557 func (cn *PeerConn) wroteBytes(n int64) {
558 cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesWritten }))
561 func (c *PeerConn) fastEnabled() bool {
562 return c.PeerExtensionBytes.SupportsFast() && c.t.cl.config.Extensions.SupportsFast()
565 func (c *PeerConn) reject(r Request) {
566 if !c.fastEnabled() {
567 panic("fast not enabled")
569 c.write(r.ToMsg(pp.Reject))
570 // It is possible to reject a request before it is added to peer requests due to being invalid.
571 if state, ok := c.peerRequests[r]; ok {
572 state.allocReservation.Drop()
573 delete(c.peerRequests, r)
577 func (c *PeerConn) maximumPeerRequestChunkLength() (_ Option[int]) {
578 uploadRateLimiter := c.t.cl.config.UploadRateLimiter
579 if uploadRateLimiter.Limit() == rate.Inf {
582 return Some(uploadRateLimiter.Burst())
585 // startFetch is for testing purposes currently.
586 func (c *PeerConn) onReadRequest(r Request, startFetch bool) error {
587 requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1)
588 if _, ok := c.peerRequests[r]; ok {
589 torrent.Add("duplicate requests received", 1)
591 return errors.New("received duplicate request with fast enabled")
596 torrent.Add("requests received while choking", 1)
598 torrent.Add("requests rejected while choking", 1)
603 // TODO: What if they've already requested this?
604 if len(c.peerRequests) >= localClientReqq {
605 torrent.Add("requests received while queue full", 1)
609 // BEP 6 says we may close here if we choose.
612 if opt := c.maximumPeerRequestChunkLength(); opt.Ok && int(r.Length) > opt.Value {
613 err := fmt.Errorf("peer requested chunk too long (%v)", r.Length)
614 c.protocolLogger.Levelf(log.Warning, err.Error())
622 if !c.t.havePiece(pieceIndex(r.Index)) {
623 // TODO: Tell the peer we don't have the piece, and reject this request.
624 requestsReceivedForMissingPieces.Add(1)
625 return fmt.Errorf("peer requested piece we don't have: %v", r.Index.Int())
627 pieceLength := c.t.pieceLength(pieceIndex(r.Index))
628 // Check this after we know we have the piece, so that the piece length will be known.
629 if chunkOverflowsPiece(r.ChunkSpec, pieceLength) {
630 torrent.Add("bad requests received", 1)
631 return errors.New("chunk overflows piece")
633 if c.peerRequests == nil {
634 c.peerRequests = make(map[Request]*peerRequestState, localClientReqq)
636 value := &peerRequestState{
637 allocReservation: c.peerRequestDataAllocLimiter.Reserve(int64(r.Length)),
639 c.peerRequests[r] = value
641 // TODO: Limit peer request data read concurrency.
642 go c.peerRequestDataReader(r, value)
647 func (c *PeerConn) peerRequestDataReader(r Request, prs *peerRequestState) {
648 // Should we depend on Torrent closure here? I think it's okay to get cancelled from elsewhere,
649 // or fail to read and then cleanup. Also, we used to hang here if the reservation was never
650 // dropped, that was fixed.
651 ctx := context.Background()
652 err := prs.allocReservation.Wait(ctx)
654 c.logger.WithDefaultLevel(log.Debug).Levelf(log.ErrorLevel(err), "waiting for alloc limit reservation: %v", err)
657 b, err := c.readPeerRequestData(r)
659 defer c.locker().Unlock()
661 c.peerRequestDataReadFailed(err, r)
664 panic("data must be non-nil to trigger send")
666 torrent.Add("peer request data read successes", 1)
668 // This might be required for the error case too (#752 and #753).
673 // If this is maintained correctly, we might be able to support optional synchronous reading for
674 // chunk sending, the way it used to work.
675 func (c *PeerConn) peerRequestDataReadFailed(err error, r Request) {
676 torrent.Add("peer request data read failures", 1)
677 logLevel := log.Warning
678 if c.t.hasStorageCap() {
679 // It's expected that pieces might drop. See
680 // https://github.com/anacrolix/torrent/issues/702#issuecomment-1000953313.
683 c.logger.Levelf(logLevel, "error reading chunk for peer Request %v: %v", r, err)
684 if c.t.closed.IsSet() {
687 i := pieceIndex(r.Index)
688 if c.t.pieceComplete(i) {
689 // There used to be more code here that just duplicated the following break. Piece
690 // completions are currently cached, so I'm not sure how helpful this update is, except to
691 // pull any completion changes pushed to the storage backend in failed reads that got us
693 c.t.updatePieceCompletion(i)
695 // We've probably dropped a piece from storage, but there's no way to communicate this to the
696 // peer. If they ask for it again, we kick them allowing us to send them updated piece states if
697 // we reconnect. TODO: Instead, we could just try to update them with Bitfield or HaveNone and
698 // if they kick us for breaking protocol, on reconnect we will be compliant again (at least
704 // If fast isn't enabled, I think we would have wiped all peer requests when we last
705 // choked, and requests while we're choking would be ignored. It could be possible that
706 // a peer request data read completed concurrently to it being deleted elsewhere.
707 c.protocolLogger.WithDefaultLevel(log.Warning).Printf("already choking peer, requests might not be rejected correctly")
709 // Choking a non-fast peer should cause them to flush all their requests.
714 func (c *PeerConn) readPeerRequestData(r Request) ([]byte, error) {
715 b := make([]byte, r.Length)
716 p := c.t.info.Piece(int(r.Index))
717 n, err := c.t.readAt(b, p.Offset()+int64(r.Begin))
724 panic("expected error")
730 func (c *PeerConn) logProtocolBehaviour(level log.Level, format string, arg ...interface{}) {
731 c.protocolLogger.WithContextText(fmt.Sprintf(
732 "peer id %q, ext v %q", c.PeerID, c.PeerClientName.Load(),
733 )).SkipCallers(1).Levelf(level, format, arg...)
736 // Processes incoming BitTorrent wire-protocol messages. The client lock is held upon entry and
737 // exit. Returning will end the connection.
738 func (c *PeerConn) mainReadLoop() (err error) {
741 torrent.Add("connection.mainReadLoop returned with error", 1)
743 torrent.Add("connection.mainReadLoop returned with no error", 1)
749 decoder := pp.Decoder{
750 R: bufio.NewReaderSize(c.r, 1<<17),
751 MaxLength: 4 * pp.Integer(max(int64(t.chunkSize), defaultChunkSize)),
759 err = decoder.Decode(&msg)
761 err = fmt.Errorf("decoding message: %w", err)
764 // Do this before checking closed.
765 if cb := c.callbacks.ReadMessage; cb != nil && err == nil {
768 if t.closed.IsSet() || c.closed.IsSet() {
772 err = log.WithLevel(log.Info, err)
775 c.lastMessageReceived = time.Now()
777 receivedKeepalives.Add(1)
780 messageTypesReceived.Add(msg.Type.String(), 1)
781 if msg.Type.FastExtension() && !c.fastEnabled() {
782 runSafeExtraneous(func() { torrent.Add("fast messages received when extension is disabled", 1) })
783 return fmt.Errorf("received fast extension message (type=%v) but extension is disabled", msg.Type)
790 if !c.fastEnabled() {
791 c.deleteAllRequests("choked by non-fast PeerConn")
793 // We don't decrement pending requests here, let's wait for the peer to either
794 // reject or satisfy the outstanding requests. Additionally, some peers may unchoke
795 // us and resume where they left off, we don't want to have piled on to those chunks
796 // in the meanwhile. I think a peer's ability to abuse this should be limited: they
797 // could let us request a lot of stuff, then choke us and never reject, but they're
798 // only a single peer, our chunk balancing should smooth over this abuse.
801 c.updateExpectingChunks()
804 // Some clients do this for some reason. Transmission doesn't error on this, so we
805 // won't for consistency.
806 c.logProtocolBehaviour(log.Debug, "received unchoke when already unchoked")
809 c.peerChoking = false
811 c.requestState.Requests.Iterate(func(x RequestIndex) bool {
812 if !c.peerAllowedFast.Contains(c.t.pieceIndexOfRequestIndex(x)) {
817 if preservedCount != 0 {
818 // TODO: Yes this is a debug log but I'm not happy with the state of the logging lib
820 c.protocolLogger.Levelf(log.Debug,
821 "%v requests were preserved while being choked (fast=%v)",
825 torrent.Add("requestsPreservedThroughChoking", int64(preservedCount))
827 if !c.t._pendingPieces.IsEmpty() {
828 c.updateRequests("unchoked")
830 c.updateExpectingChunks()
832 c.peerInterested = true
834 case pp.NotInterested:
835 c.peerInterested = false
836 // We don't clear their requests since it isn't clear in the spec.
837 // We'll probably choke them for this, which will clear them if
838 // appropriate, and is clearly specified.
840 err = c.peerSentHave(pieceIndex(msg.Index))
842 err = c.peerSentBitfield(msg.Bitfield)
844 r := newRequestFromMessage(&msg)
845 err = c.onReadRequest(r, true)
847 err = fmt.Errorf("on reading request %v: %w", r, err)
850 c.doChunkReadStats(int64(len(msg.Piece)))
851 err = c.receiveChunk(&msg)
852 if len(msg.Piece) == int(t.chunkSize) {
853 t.chunkPool.Put(&msg.Piece)
856 err = fmt.Errorf("receiving chunk: %w", err)
859 req := newRequestFromMessage(&msg)
860 c.onPeerSentCancel(req)
862 ipa, ok := tryIpPortFromNetAddr(c.RemoteAddr)
866 pingAddr := net.UDPAddr{
871 pingAddr.Port = int(msg.Port)
873 cl.eachDhtServer(func(s DhtServer) {
877 torrent.Add("suggests received", 1)
878 log.Fmsg("peer suggested piece %d", msg.Index).AddValues(c, msg.Index).LogLevel(log.Debug, c.t.logger)
879 c.updateRequests("suggested")
881 err = c.onPeerSentHaveAll()
883 err = c.peerSentHaveNone()
885 req := newRequestFromMessage(&msg)
886 if !c.remoteRejectedRequest(c.t.requestIndexFromRequest(req)) {
887 err = fmt.Errorf("received invalid reject for request %v", req)
888 c.protocolLogger.Levelf(log.Debug, "%v", err)
891 torrent.Add("allowed fasts received", 1)
892 log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).LogLevel(log.Debug, c.t.logger)
893 c.updateRequests("PeerConn.mainReadLoop allowed fast")
895 err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload)
897 err = c.onReadHashes(&msg)
898 case pp.HashRequest, pp.HashReject:
899 c.protocolLogger.Levelf(log.Info, "received unimplemented BitTorrent v2 message: %v", msg.Type)
901 err = fmt.Errorf("received unknown message type: %#v", msg.Type)
909 func (c *PeerConn) onReadExtendedMsg(id pp.ExtensionNumber, payload []byte) (err error) {
911 // TODO: Should we still do this?
913 // These clients use their own extension IDs for outgoing message
914 // types, which is incorrect.
915 if bytes.HasPrefix(c.PeerID[:], []byte("-SD0100-")) || strings.HasPrefix(string(c.PeerID[:]), "-XL0012-") {
923 event := PeerConnReadExtensionMessageEvent{
928 for _, cb := range c.callbacks.PeerConnReadExtensionMessage {
932 if id == pp.HandshakeExtendedID {
933 var d pp.ExtendedHandshakeMessage
934 if err := bencode.Unmarshal(payload, &d); err != nil {
935 c.protocolLogger.Printf("error parsing extended handshake message %q: %s", payload, err)
936 return fmt.Errorf("unmarshalling extended handshake payload: %w", err)
938 // Trigger this callback after it's been processed. If you want to handle it yourself, you
939 // should hook PeerConnReadExtensionMessage.
940 if cb := c.callbacks.ReadExtendedHandshake; cb != nil {
944 c.PeerMaxRequests = d.Reqq
946 c.PeerClientName.Store(d.V)
947 if c.PeerExtensionIDs == nil {
948 c.PeerExtensionIDs = make(map[pp.ExtensionName]pp.ExtensionNumber, len(d.M))
950 c.PeerListenPort = d.Port
951 c.PeerPrefersEncryption = d.Encryption
952 for name, id := range d.M {
953 if _, ok := c.PeerExtensionIDs[name]; !ok {
954 peersSupportingExtension.Add(
955 // expvar.Var.String must produce valid JSON. "ut_payme\xeet_address" was being
956 // entered here which caused problems later when unmarshalling.
957 strconv.Quote(string(name)),
960 c.PeerExtensionIDs[name] = id
962 if d.MetadataSize != 0 {
963 if err = t.setMetadataSize(d.MetadataSize); err != nil {
964 return fmt.Errorf("setting metadata size to %d: %w", d.MetadataSize, err)
967 c.requestPendingMetadata()
968 if !t.cl.config.DisablePEX {
969 t.pex.Add(c) // we learnt enough now
970 // This checks the extension is supported internally.
975 extensionName, builtin, err := c.LocalLtepProtocolMap.LookupId(id)
980 // User should have taken care of this in PeerConnReadExtensionMessage callback.
983 switch extensionName {
984 case pp.ExtensionNameMetadata:
985 err := cl.gotMetadataExtensionMsg(payload, t, c)
987 return fmt.Errorf("handling metadata extension message: %w", err)
990 case pp.ExtensionNamePex:
991 if !c.pex.IsEnabled() {
992 return nil // or hang-up maybe?
994 err = c.pex.Recv(payload)
996 err = fmt.Errorf("receiving pex message: %w", err)
999 case utHolepunch.ExtensionName:
1000 var msg utHolepunch.Msg
1001 err = msg.UnmarshalBinary(payload)
1003 err = fmt.Errorf("unmarshalling ut_holepunch message: %w", err)
1006 err = c.t.handleReceivedUtHolepunchMsg(msg, c)
1009 panic(fmt.Sprintf("unhandled builtin extension protocol %q", extensionName))
1013 // Set both the Reader and Writer for the connection from a single ReadWriter.
1014 func (cn *PeerConn) setRW(rw io.ReadWriter) {
1019 // Returns the Reader and Writer as a combined ReadWriter.
1020 func (cn *PeerConn) rw() io.ReadWriter {
1027 func (c *PeerConn) uploadAllowed() bool {
1028 if c.t.cl.config.NoUpload {
1031 if c.t.dataUploadDisallowed {
1037 if !c.peerHasWantedPieces() {
1040 // Don't upload more than 100 KiB more than we download.
1041 if c._stats.BytesWrittenData.Int64() >= c._stats.BytesReadData.Int64()+100<<10 {
1047 func (c *PeerConn) setRetryUploadTimer(delay time.Duration) {
1048 if c.uploadTimer == nil {
1049 c.uploadTimer = time.AfterFunc(delay, c.tickleWriter)
1051 c.uploadTimer.Reset(delay)
1055 // Also handles choking and unchoking of the remote peer.
1056 func (c *PeerConn) upload(msg func(pp.Message) bool) bool {
1057 // Breaking or completing this loop means we don't want to upload to the peer anymore, and we
1060 for c.uploadAllowed() {
1061 // We want to upload to the peer.
1062 if !c.unchoke(msg) {
1065 for r, state := range c.peerRequests {
1066 if state.data == nil {
1069 res := c.t.cl.config.UploadRateLimiter.ReserveN(time.Now(), int(r.Length))
1071 panic(fmt.Sprintf("upload rate limiter burst size < %d", r.Length))
1073 delay := res.Delay()
1076 c.setRetryUploadTimer(delay)
1077 // Hard to say what to return here.
1080 more := c.sendChunk(r, msg, state)
1081 delete(c.peerRequests, r)
1092 func (cn *PeerConn) drop() {
1093 cn.t.dropConnection(cn)
1096 func (cn *PeerConn) ban() {
1097 cn.t.cl.banPeerIP(cn.remoteIp())
1100 // This is called when something has changed that should wake the writer, such as putting stuff into
1101 // the writeBuffer, or changing some state that the writer can act on.
1102 func (c *PeerConn) tickleWriter() {
1103 c.messageWriter.writeCond.Broadcast()
1106 func (c *PeerConn) sendChunk(r Request, msg func(pp.Message) bool, state *peerRequestState) (more bool) {
1107 c.lastChunkSent = time.Now()
1108 state.allocReservation.Release()
1109 return msg(pp.Message{
1117 func (c *Peer) setTorrent(t *Torrent) {
1119 panic("connection already associated with a torrent")
1122 c.logger.WithDefaultLevel(log.Debug).Printf("set torrent=%v", t)
1123 t.reconcileHandshakeStats(c)
1126 func (c *PeerConn) pexPeerFlags() pp.PexPeerFlags {
1127 f := pp.PexPeerFlags(0)
1128 if c.PeerPrefersEncryption {
1129 f |= pp.PexPrefersEncryption
1132 f |= pp.PexOutgoingConn
1135 f |= pp.PexSupportsUtp
1140 // This returns the address to use if we want to dial the peer again. It incorporates the peer's
1141 // advertised listen port.
1142 func (c *PeerConn) dialAddr() PeerRemoteAddr {
1143 if c.outgoing || c.PeerListenPort == 0 {
1146 addrPort, err := addrPortFromPeerRemoteAddr(c.RemoteAddr)
1150 "error parsing %q for alternate dial port: %v",
1156 return netip.AddrPortFrom(addrPort.Addr(), uint16(c.PeerListenPort))
1159 func (c *PeerConn) pexEvent(t pexEventType) (_ pexEvent, err error) {
1160 f := c.pexPeerFlags()
1161 dialAddr := c.dialAddr()
1162 addr, err := addrPortFromPeerRemoteAddr(dialAddr)
1163 if err != nil || !addr.IsValid() {
1164 err = fmt.Errorf("parsing dial addr %q: %w", dialAddr, err)
1167 return pexEvent{t, addr, f, nil}, nil
1170 func (pc *PeerConn) String() string {
1171 return fmt.Sprintf("%T %p [id=%+q, exts=%v, v=%q]", pc, pc, pc.PeerID, pc.PeerExtensionBytes, pc.PeerClientName.Load())
1174 // Returns the pieces the peer could have based on their claims. If we don't know how many pieces
1175 // are in the torrent, it could be a very large range if the peer has sent HaveAll.
1176 func (pc *PeerConn) PeerPieces() *roaring.Bitmap {
1178 defer pc.locker().RUnlock()
1179 return pc.newPeerPieces()
1182 func (pc *PeerConn) remoteIsTransmission() bool {
1183 return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-'
1186 func (pc *PeerConn) remoteDialAddrPort() (netip.AddrPort, error) {
1187 dialAddr := pc.dialAddr()
1188 return addrPortFromPeerRemoteAddr(dialAddr)
1191 func (pc *PeerConn) bitExtensionEnabled(bit pp.ExtensionBit) bool {
1192 return pc.t.cl.config.Extensions.GetBit(bit) && pc.PeerExtensionBytes.GetBit(bit)
1195 func (cn *PeerConn) peerPiecesChanged() {
1196 cn.t.maybeDropMutuallyCompletePeer(cn)
1199 // Returns whether the connection could be useful to us. We're seeding and
1200 // they want data, we don't have metainfo and they can provide it, etc.
1201 func (c *PeerConn) useful() bool {
1203 if c.closed.IsSet() {
1207 return c.supportsExtension("ut_metadata")
1209 if t.seeding() && c.peerInterested {
1212 if c.peerHasWantedPieces() {
1218 func makeBuiltinLtepProtocols(pex bool) LocalLtepProtocolMap {
1219 ps := []pp.ExtensionName{pp.ExtensionNameMetadata, utHolepunch.ExtensionName}
1221 ps = append(ps, pp.ExtensionNamePex)
1223 return LocalLtepProtocolMap{
1225 NumBuiltin: len(ps),
1229 func (c *PeerConn) addBuiltinLtepProtocols(pex bool) {
1230 c.LocalLtepProtocolMap = &c.t.cl.defaultLocalLtepProtocolMap
1233 func (pc *PeerConn) WriteExtendedMessage(extName pp.ExtensionName, payload []byte) error {
1235 defer pc.locker().Unlock()
1236 id := pc.PeerExtensionIDs[extName]
1238 return fmt.Errorf("peer does not support or has disabled extension %q", extName)
1240 pc.write(pp.Message{
1243 ExtendedPayload: payload,
1248 func (pc *PeerConn) shouldRequestHashes() bool {
1249 return pc.t.haveInfo() && pc.v2 && pc.t.info.HasV2()
1252 func (pc *PeerConn) requestMissingHashes() {
1253 if !pc.shouldRequestHashes() {
1257 baseLayer := pp.Integer(merkle.Log2RoundingUp(merkle.RoundUpToPowerOfTwo(
1258 uint((pc.t.usualPieceSize() + merkle.BlockSize - 1) / merkle.BlockSize)),
1260 nextFileBeginPiece := 0
1262 for _, file := range info.UpvertedFiles() {
1263 fileNumPieces := int((file.Length + info.PieceLength - 1) / info.PieceLength)
1264 curFileBeginPiece := nextFileBeginPiece
1265 nextFileBeginPiece += fileNumPieces
1266 haveAllHashes := true
1267 for i := range fileNumPieces {
1268 torrentPieceIndex := curFileBeginPiece + i
1269 if !pc.peerHasPiece(torrentPieceIndex) {
1272 if !pc.t.piece(torrentPieceIndex).hashV2.Ok {
1273 haveAllHashes = false
1279 // We would be requesting the leaves, the file must be short enough that we can just do with
1280 // the pieces root as the piece hash.
1281 if fileNumPieces <= 1 {
1284 piecesRoot := file.PiecesRoot.Unwrap()
1285 proofLayers := pp.Integer(0)
1286 for index := 0; index < fileNumPieces; index += 512 {
1287 // Minimizing to the number of pieces in a file conflicts with the BEP.
1288 length := merkle.RoundUpToPowerOfTwo(uint(min(512, fileNumPieces-index)))
1290 // This should have been filtered out by baseLayer and pieces root as piece hash
1295 pc.protocolLogger.Levelf(log.Warning, "requesting odd hashes length %d", length)
1298 Type: pp.HashRequest,
1299 PiecesRoot: piecesRoot,
1300 BaseLayer: baseLayer,
1301 Index: pp.Integer(index),
1302 Length: pp.Integer(length),
1303 ProofLayers: proofLayers,
1305 hr := hashRequestFromMessage(msg)
1306 if generics.MapContains(pc.sentHashRequests, hr) {
1310 generics.MakeMapIfNil(&pc.sentHashRequests)
1311 pc.sentHashRequests[hr] = struct{}{}
1316 func (pc *PeerConn) onReadHashes(msg *pp.Message) (err error) {
1317 file := pc.t.getFileByPiecesRoot(msg.PiecesRoot)
1318 filePieceHashes := pc.receivedHashPieces[msg.PiecesRoot]
1319 if filePieceHashes == nil {
1320 filePieceHashes = make([][32]byte, file.numPieces())
1321 generics.MakeMapIfNil(&pc.receivedHashPieces)
1322 pc.receivedHashPieces[msg.PiecesRoot] = filePieceHashes
1324 if msg.ProofLayers != 0 {
1325 // This isn't handled yet.
1326 panic(msg.ProofLayers)
1328 copy(filePieceHashes[msg.Index:], msg.Hashes)
1329 root := merkle.RootWithPadHash(
1331 metainfo.HashForPiecePad(int64(pc.t.usualPieceSize())))
1332 expectedPiecesRoot := file.piecesRoot.Unwrap()
1333 if root == expectedPiecesRoot {
1334 pc.protocolLogger.WithNames(v2HashesLogName).Levelf(
1336 "got piece hashes for file %v (num pieces %v)",
1337 file, file.numPieces())
1338 for filePieceIndex, peerHash := range filePieceHashes {
1339 torrentPieceIndex := file.BeginPieceIndex() + filePieceIndex
1340 pc.t.piece(torrentPieceIndex).setV2Hash(peerHash)
1343 pc.protocolLogger.WithNames(v2HashesLogName).Levelf(
1345 "peer file piece hashes root mismatch: %x != %x",
1346 root, expectedPiecesRoot)
1351 type hashRequest struct {
1353 baseLayer, index, length, proofLayers pp.Integer
1356 func (hr hashRequest) toMessage() pp.Message {
1358 Type: pp.HashRequest,
1359 PiecesRoot: hr.piecesRoot,
1360 BaseLayer: hr.baseLayer,
1363 ProofLayers: hr.proofLayers,
1367 func hashRequestFromMessage(m pp.Message) hashRequest {
1369 piecesRoot: m.PiecesRoot,
1370 baseLayer: m.BaseLayer,
1373 proofLayers: m.ProofLayers,