]> Sergey Matveev's repositories - btrtrc.git/blob - peerconn.go
Fix panic logging unknown holepunch error code
[btrtrc.git] / peerconn.go
1 package torrent
2
3 import (
4         "bufio"
5         "bytes"
6         "context"
7         "errors"
8         "fmt"
9         "io"
10         "math/rand"
11         "net"
12         "net/netip"
13         "strconv"
14         "strings"
15         "sync/atomic"
16         "time"
17
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"
26
27         "github.com/anacrolix/torrent/bencode"
28         "github.com/anacrolix/torrent/internal/alloclim"
29         "github.com/anacrolix/torrent/metainfo"
30         "github.com/anacrolix/torrent/mse"
31         pp "github.com/anacrolix/torrent/peer_protocol"
32         utHolepunch "github.com/anacrolix/torrent/peer_protocol/ut-holepunch"
33 )
34
35 // Maintains the state of a BitTorrent-protocol based connection with a peer.
36 type PeerConn struct {
37         Peer
38
39         // A string that should identify the PeerConn's net.Conn endpoints. The net.Conn could
40         // be wrapping WebRTC, uTP, or TCP etc. Used in writing the conn status for peers.
41         connString string
42
43         // See BEP 3 etc.
44         PeerID             PeerID
45         PeerExtensionBytes pp.PeerExtensionBits
46         PeerListenPort     int
47
48         // The actual Conn, used for closing, and setting socket options. Do not use methods on this
49         // while holding any mutexes.
50         conn net.Conn
51         // The Reader and Writer for this Conn, with hooks installed for stats,
52         // limiting, deadlines etc.
53         w io.Writer
54         r io.Reader
55
56         messageWriter peerConnMsgWriter
57
58         PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber
59         PeerClientName   atomic.Value
60         uploadTimer      *time.Timer
61         pex              pexConnState
62
63         // The pieces the peer has claimed to have.
64         _peerPieces roaring.Bitmap
65         // The peer has everything. This can occur due to a special message, when
66         // we may not even know the number of pieces in the torrent yet.
67         peerSentHaveAll bool
68
69         peerRequestDataAllocLimiter alloclim.Limiter
70
71         outstandingHolepunchingRendezvous map[netip.AddrPort]struct{}
72 }
73
74 func (cn *PeerConn) pexStatus() string {
75         if !cn.bitExtensionEnabled(pp.ExtensionBitLtep) {
76                 return "extended protocol disabled"
77         }
78         if cn.PeerExtensionIDs == nil {
79                 return "pending extended handshake"
80         }
81         if !cn.supportsExtension(pp.ExtensionNamePex) {
82                 return "unsupported"
83         }
84         if true {
85                 return fmt.Sprintf(
86                         "%v conns, %v unsent events",
87                         len(cn.pex.remoteLiveConns),
88                         cn.pex.numPending(),
89                 )
90         } else {
91                 // This alternative branch prints out the remote live conn addresses.
92                 return fmt.Sprintf(
93                         "%v conns, %v unsent events",
94                         strings.Join(generics.SliceMap(
95                                 maps.Keys(cn.pex.remoteLiveConns),
96                                 func(from netip.AddrPort) string {
97                                         return from.String()
98                                 }), ","),
99                         cn.pex.numPending(),
100                 )
101         }
102 }
103
104 func (cn *PeerConn) peerImplStatusLines() []string {
105         return []string{
106                 cn.connString,
107                 fmt.Sprintf("peer id: %+q", cn.PeerID),
108                 fmt.Sprintf("extensions: %v", cn.PeerExtensionBytes),
109                 fmt.Sprintf("ltep extensions: %v", cn.PeerExtensionIDs),
110                 fmt.Sprintf("pex: %s", cn.pexStatus()),
111         }
112 }
113
114 // Returns true if the connection is over IPv6.
115 func (cn *PeerConn) ipv6() bool {
116         ip := cn.remoteIp()
117         if ip.To4() != nil {
118                 return false
119         }
120         return len(ip) == net.IPv6len
121 }
122
123 // Returns true the if the dialer/initiator has the higher client peer ID. See
124 // https://github.com/arvidn/libtorrent/blame/272828e1cc37b042dfbbafa539222d8533e99755/src/bt_peer_connection.cpp#L3536-L3557.
125 // As far as I can tell, Transmission just keeps the oldest connection.
126 func (cn *PeerConn) isPreferredDirection() bool {
127         // True if our client peer ID is higher than the remote's peer ID.
128         return bytes.Compare(cn.PeerID[:], cn.t.cl.peerID[:]) < 0 == cn.outgoing
129 }
130
131 // Returns whether the left connection should be preferred over the right one,
132 // considering only their networking properties. If ok is false, we can't
133 // decide.
134 func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) bool {
135         var ml multiless.Computation
136         ml = ml.Bool(r.isPreferredDirection(), l.isPreferredDirection())
137         ml = ml.Bool(l.utp(), r.utp())
138         ml = ml.Bool(r.ipv6(), l.ipv6())
139         return ml.Less()
140 }
141
142 func (cn *PeerConn) peerHasAllPieces() (all, known bool) {
143         if cn.peerSentHaveAll {
144                 return true, true
145         }
146         if !cn.t.haveInfo() {
147                 return false, false
148         }
149         return cn._peerPieces.GetCardinality() == uint64(cn.t.numPieces()), true
150 }
151
152 func (cn *PeerConn) onGotInfo(info *metainfo.Info) {
153         cn.setNumPieces(info.NumPieces())
154 }
155
156 // Correct the PeerPieces slice length. Return false if the existing slice is invalid, such as by
157 // receiving badly sized BITFIELD, or invalid HAVE messages.
158 func (cn *PeerConn) setNumPieces(num pieceIndex) {
159         cn._peerPieces.RemoveRange(bitmap.BitRange(num), bitmap.ToEnd)
160         cn.peerPiecesChanged()
161 }
162
163 func (cn *PeerConn) peerPieces() *roaring.Bitmap {
164         return &cn._peerPieces
165 }
166
167 func (cn *PeerConn) connectionFlags() (ret string) {
168         c := func(b byte) {
169                 ret += string([]byte{b})
170         }
171         if cn.cryptoMethod == mse.CryptoMethodRC4 {
172                 c('E')
173         } else if cn.headerEncrypted {
174                 c('e')
175         }
176         ret += string(cn.Discovery)
177         if cn.utp() {
178                 c('U')
179         }
180         return
181 }
182
183 func (cn *PeerConn) utp() bool {
184         return parseNetworkString(cn.Network).Udp
185 }
186
187 func (cn *PeerConn) onClose() {
188         if cn.pex.IsEnabled() {
189                 cn.pex.Close()
190         }
191         cn.tickleWriter()
192         if cn.conn != nil {
193                 go cn.conn.Close()
194         }
195         if cb := cn.callbacks.PeerConnClosed; cb != nil {
196                 cb(cn)
197         }
198 }
199
200 // Writes a message into the write buffer. Returns whether it's okay to keep writing. Writing is
201 // done asynchronously, so it may be that we're not able to honour backpressure from this method.
202 func (cn *PeerConn) write(msg pp.Message) bool {
203         torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
204         // We don't need to track bytes here because the connection's Writer has that behaviour injected
205         // (although there's some delay between us buffering the message, and the connection writer
206         // flushing it out.).
207         notFull := cn.messageWriter.write(msg)
208         // Last I checked only Piece messages affect stats, and we don't write those.
209         cn.wroteMsg(&msg)
210         cn.tickleWriter()
211         return notFull
212 }
213
214 func (cn *PeerConn) requestMetadataPiece(index int) {
215         eID := cn.PeerExtensionIDs[pp.ExtensionNameMetadata]
216         if eID == pp.ExtensionDeleteNumber {
217                 return
218         }
219         if index < len(cn.metadataRequests) && cn.metadataRequests[index] {
220                 return
221         }
222         cn.logger.WithDefaultLevel(log.Debug).Printf("requesting metadata piece %d", index)
223         cn.write(pp.MetadataExtensionRequestMsg(eID, index))
224         for index >= len(cn.metadataRequests) {
225                 cn.metadataRequests = append(cn.metadataRequests, false)
226         }
227         cn.metadataRequests[index] = true
228 }
229
230 func (cn *PeerConn) requestedMetadataPiece(index int) bool {
231         return index < len(cn.metadataRequests) && cn.metadataRequests[index]
232 }
233
234 func (cn *PeerConn) onPeerSentCancel(r Request) {
235         if _, ok := cn.peerRequests[r]; !ok {
236                 torrent.Add("unexpected cancels received", 1)
237                 return
238         }
239         if cn.fastEnabled() {
240                 cn.reject(r)
241         } else {
242                 delete(cn.peerRequests, r)
243         }
244 }
245
246 func (cn *PeerConn) choke(msg messageWriter) (more bool) {
247         if cn.choking {
248                 return true
249         }
250         cn.choking = true
251         more = msg(pp.Message{
252                 Type: pp.Choke,
253         })
254         if !cn.fastEnabled() {
255                 cn.deleteAllPeerRequests()
256         }
257         return
258 }
259
260 func (cn *PeerConn) deleteAllPeerRequests() {
261         for _, state := range cn.peerRequests {
262                 state.allocReservation.Drop()
263         }
264         cn.peerRequests = nil
265 }
266
267 func (cn *PeerConn) unchoke(msg func(pp.Message) bool) bool {
268         if !cn.choking {
269                 return true
270         }
271         cn.choking = false
272         return msg(pp.Message{
273                 Type: pp.Unchoke,
274         })
275 }
276
277 func (pc *PeerConn) writeInterested(interested bool) bool {
278         return pc.write(pp.Message{
279                 Type: func() pp.MessageType {
280                         if interested {
281                                 return pp.Interested
282                         } else {
283                                 return pp.NotInterested
284                         }
285                 }(),
286         })
287 }
288
289 func (me *PeerConn) _request(r Request) bool {
290         return me.write(pp.Message{
291                 Type:   pp.Request,
292                 Index:  r.Index,
293                 Begin:  r.Begin,
294                 Length: r.Length,
295         })
296 }
297
298 func (me *PeerConn) _cancel(r RequestIndex) bool {
299         me.write(makeCancelMessage(me.t.requestIndexToRequest(r)))
300         // Transmission does not send rejects for received cancels. See
301         // https://github.com/transmission/transmission/pull/2275.
302         return me.fastEnabled() && !me.remoteIsTransmission()
303 }
304
305 func (cn *PeerConn) fillWriteBuffer() {
306         if cn.messageWriter.writeBuffer.Len() > writeBufferLowWaterLen {
307                 // Fully committing to our max requests requires sufficient space (see
308                 // maxLocalToRemoteRequests). Flush what we have instead. We also prefer always to make
309                 // requests than to do PEX or upload, so we short-circuit before handling those. Any update
310                 // request reason will not be cleared, so we'll come right back here when there's space. We
311                 // can't do this in maybeUpdateActualRequestState because it's a method on Peer and has no
312                 // knowledge of write buffers.
313         }
314         cn.maybeUpdateActualRequestState()
315         if cn.pex.IsEnabled() {
316                 if flow := cn.pex.Share(cn.write); !flow {
317                         return
318                 }
319         }
320         cn.upload(cn.write)
321 }
322
323 func (cn *PeerConn) have(piece pieceIndex) {
324         if cn.sentHaves.Get(bitmap.BitIndex(piece)) {
325                 return
326         }
327         cn.write(pp.Message{
328                 Type:  pp.Have,
329                 Index: pp.Integer(piece),
330         })
331         cn.sentHaves.Add(bitmap.BitIndex(piece))
332 }
333
334 func (cn *PeerConn) postBitfield() {
335         if cn.sentHaves.Len() != 0 {
336                 panic("bitfield must be first have-related message sent")
337         }
338         if !cn.t.haveAnyPieces() {
339                 return
340         }
341         cn.write(pp.Message{
342                 Type:     pp.Bitfield,
343                 Bitfield: cn.t.bitfield(),
344         })
345         cn.sentHaves = bitmap.Bitmap{cn.t._completedPieces.Clone()}
346 }
347
348 func (cn *PeerConn) handleUpdateRequests() {
349         // The writer determines the request state as needed when it can write.
350         cn.tickleWriter()
351 }
352
353 func (cn *PeerConn) raisePeerMinPieces(newMin pieceIndex) {
354         if newMin > cn.peerMinPieces {
355                 cn.peerMinPieces = newMin
356         }
357 }
358
359 func (cn *PeerConn) peerSentHave(piece pieceIndex) error {
360         if cn.t.haveInfo() && piece >= cn.t.numPieces() || piece < 0 {
361                 return errors.New("invalid piece")
362         }
363         if cn.peerHasPiece(piece) {
364                 return nil
365         }
366         cn.raisePeerMinPieces(piece + 1)
367         if !cn.peerHasPiece(piece) {
368                 cn.t.incPieceAvailability(piece)
369         }
370         cn._peerPieces.Add(uint32(piece))
371         if cn.t.wantPieceIndex(piece) {
372                 cn.updateRequests("have")
373         }
374         cn.peerPiecesChanged()
375         return nil
376 }
377
378 func (cn *PeerConn) peerSentBitfield(bf []bool) error {
379         if len(bf)%8 != 0 {
380                 panic("expected bitfield length divisible by 8")
381         }
382         // We know that the last byte means that at most the last 7 bits are wasted.
383         cn.raisePeerMinPieces(pieceIndex(len(bf) - 7))
384         if cn.t.haveInfo() && len(bf) > int(cn.t.numPieces()) {
385                 // Ignore known excess pieces.
386                 bf = bf[:cn.t.numPieces()]
387         }
388         bm := boolSliceToBitmap(bf)
389         if cn.t.haveInfo() && pieceIndex(bm.GetCardinality()) == cn.t.numPieces() {
390                 cn.onPeerHasAllPieces()
391                 return nil
392         }
393         if !bm.IsEmpty() {
394                 cn.raisePeerMinPieces(pieceIndex(bm.Maximum()) + 1)
395         }
396         shouldUpdateRequests := false
397         if cn.peerSentHaveAll {
398                 if !cn.t.deleteConnWithAllPieces(&cn.Peer) {
399                         panic(cn)
400                 }
401                 cn.peerSentHaveAll = false
402                 if !cn._peerPieces.IsEmpty() {
403                         panic("if peer has all, we expect no individual peer pieces to be set")
404                 }
405         } else {
406                 bm.Xor(&cn._peerPieces)
407         }
408         cn.peerSentHaveAll = false
409         // bm is now 'on' for pieces that are changing
410         bm.Iterate(func(x uint32) bool {
411                 pi := pieceIndex(x)
412                 if cn._peerPieces.Contains(x) {
413                         // Then we must be losing this piece
414                         cn.t.decPieceAvailability(pi)
415                 } else {
416                         if !shouldUpdateRequests && cn.t.wantPieceIndex(pieceIndex(x)) {
417                                 shouldUpdateRequests = true
418                         }
419                         // We must be gaining this piece
420                         cn.t.incPieceAvailability(pieceIndex(x))
421                 }
422                 return true
423         })
424         // Apply the changes. If we had everything previously, this should be empty, so xor is the same
425         // as or.
426         cn._peerPieces.Xor(&bm)
427         if shouldUpdateRequests {
428                 cn.updateRequests("bitfield")
429         }
430         // We didn't guard this before, I see no reason to do it now.
431         cn.peerPiecesChanged()
432         return nil
433 }
434
435 func (cn *PeerConn) onPeerHasAllPieces() {
436         t := cn.t
437         if t.haveInfo() {
438                 cn._peerPieces.Iterate(func(x uint32) bool {
439                         t.decPieceAvailability(pieceIndex(x))
440                         return true
441                 })
442         }
443         t.addConnWithAllPieces(&cn.Peer)
444         cn.peerSentHaveAll = true
445         cn._peerPieces.Clear()
446         if !cn.t._pendingPieces.IsEmpty() {
447                 cn.updateRequests("Peer.onPeerHasAllPieces")
448         }
449         cn.peerPiecesChanged()
450 }
451
452 func (cn *PeerConn) onPeerSentHaveAll() error {
453         cn.onPeerHasAllPieces()
454         return nil
455 }
456
457 func (cn *PeerConn) peerSentHaveNone() error {
458         if !cn.peerSentHaveAll {
459                 cn.t.decPeerPieceAvailability(&cn.Peer)
460         }
461         cn._peerPieces.Clear()
462         cn.peerSentHaveAll = false
463         cn.peerPiecesChanged()
464         return nil
465 }
466
467 func (c *PeerConn) requestPendingMetadata() {
468         if c.t.haveInfo() {
469                 return
470         }
471         if c.PeerExtensionIDs[pp.ExtensionNameMetadata] == 0 {
472                 // Peer doesn't support this.
473                 return
474         }
475         // Request metadata pieces that we don't have in a random order.
476         var pending []int
477         for index := 0; index < c.t.metadataPieceCount(); index++ {
478                 if !c.t.haveMetadataPiece(index) && !c.requestedMetadataPiece(index) {
479                         pending = append(pending, index)
480                 }
481         }
482         rand.Shuffle(len(pending), func(i, j int) { pending[i], pending[j] = pending[j], pending[i] })
483         for _, i := range pending {
484                 c.requestMetadataPiece(i)
485         }
486 }
487
488 func (cn *PeerConn) wroteMsg(msg *pp.Message) {
489         torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
490         if msg.Type == pp.Extended {
491                 for name, id := range cn.PeerExtensionIDs {
492                         if id != msg.ExtendedID {
493                                 continue
494                         }
495                         torrent.Add(fmt.Sprintf("Extended messages written for protocol %q", name), 1)
496                 }
497         }
498         cn.allStats(func(cs *ConnStats) { cs.wroteMsg(msg) })
499 }
500
501 func (cn *PeerConn) wroteBytes(n int64) {
502         cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesWritten }))
503 }
504
505 func (c *PeerConn) fastEnabled() bool {
506         return c.PeerExtensionBytes.SupportsFast() && c.t.cl.config.Extensions.SupportsFast()
507 }
508
509 func (c *PeerConn) reject(r Request) {
510         if !c.fastEnabled() {
511                 panic("fast not enabled")
512         }
513         c.write(r.ToMsg(pp.Reject))
514         // It is possible to reject a request before it is added to peer requests due to being invalid.
515         if state, ok := c.peerRequests[r]; ok {
516                 state.allocReservation.Drop()
517                 delete(c.peerRequests, r)
518         }
519 }
520
521 func (c *PeerConn) maximumPeerRequestChunkLength() (_ Option[int]) {
522         uploadRateLimiter := c.t.cl.config.UploadRateLimiter
523         if uploadRateLimiter.Limit() == rate.Inf {
524                 return
525         }
526         return Some(uploadRateLimiter.Burst())
527 }
528
529 // startFetch is for testing purposes currently.
530 func (c *PeerConn) onReadRequest(r Request, startFetch bool) error {
531         requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1)
532         if _, ok := c.peerRequests[r]; ok {
533                 torrent.Add("duplicate requests received", 1)
534                 if c.fastEnabled() {
535                         return errors.New("received duplicate request with fast enabled")
536                 }
537                 return nil
538         }
539         if c.choking {
540                 torrent.Add("requests received while choking", 1)
541                 if c.fastEnabled() {
542                         torrent.Add("requests rejected while choking", 1)
543                         c.reject(r)
544                 }
545                 return nil
546         }
547         // TODO: What if they've already requested this?
548         if len(c.peerRequests) >= localClientReqq {
549                 torrent.Add("requests received while queue full", 1)
550                 if c.fastEnabled() {
551                         c.reject(r)
552                 }
553                 // BEP 6 says we may close here if we choose.
554                 return nil
555         }
556         if opt := c.maximumPeerRequestChunkLength(); opt.Ok && int(r.Length) > opt.Value {
557                 err := fmt.Errorf("peer requested chunk too long (%v)", r.Length)
558                 c.logger.Levelf(log.Warning, err.Error())
559                 if c.fastEnabled() {
560                         c.reject(r)
561                         return nil
562                 } else {
563                         return err
564                 }
565         }
566         if !c.t.havePiece(pieceIndex(r.Index)) {
567                 // TODO: Tell the peer we don't have the piece, and reject this request.
568                 requestsReceivedForMissingPieces.Add(1)
569                 return fmt.Errorf("peer requested piece we don't have: %v", r.Index.Int())
570         }
571         pieceLength := c.t.pieceLength(pieceIndex(r.Index))
572         // Check this after we know we have the piece, so that the piece length will be known.
573         if chunkOverflowsPiece(r.ChunkSpec, pieceLength) {
574                 torrent.Add("bad requests received", 1)
575                 return errors.New("chunk overflows piece")
576         }
577         if c.peerRequests == nil {
578                 c.peerRequests = make(map[Request]*peerRequestState, localClientReqq)
579         }
580         value := &peerRequestState{
581                 allocReservation: c.peerRequestDataAllocLimiter.Reserve(int64(r.Length)),
582         }
583         c.peerRequests[r] = value
584         if startFetch {
585                 // TODO: Limit peer request data read concurrency.
586                 go c.peerRequestDataReader(r, value)
587         }
588         return nil
589 }
590
591 func (c *PeerConn) peerRequestDataReader(r Request, prs *peerRequestState) {
592         b, err := c.readPeerRequestData(r, prs)
593         c.locker().Lock()
594         defer c.locker().Unlock()
595         if err != nil {
596                 c.peerRequestDataReadFailed(err, r)
597         } else {
598                 if b == nil {
599                         panic("data must be non-nil to trigger send")
600                 }
601                 torrent.Add("peer request data read successes", 1)
602                 prs.data = b
603                 // This might be required for the error case too (#752 and #753).
604                 c.tickleWriter()
605         }
606 }
607
608 // If this is maintained correctly, we might be able to support optional synchronous reading for
609 // chunk sending, the way it used to work.
610 func (c *PeerConn) peerRequestDataReadFailed(err error, r Request) {
611         torrent.Add("peer request data read failures", 1)
612         logLevel := log.Warning
613         if c.t.hasStorageCap() {
614                 // It's expected that pieces might drop. See
615                 // https://github.com/anacrolix/torrent/issues/702#issuecomment-1000953313.
616                 logLevel = log.Debug
617         }
618         c.logger.WithDefaultLevel(logLevel).Printf("error reading chunk for peer Request %v: %v", r, err)
619         if c.t.closed.IsSet() {
620                 return
621         }
622         i := pieceIndex(r.Index)
623         if c.t.pieceComplete(i) {
624                 // There used to be more code here that just duplicated the following break. Piece
625                 // completions are currently cached, so I'm not sure how helpful this update is, except to
626                 // pull any completion changes pushed to the storage backend in failed reads that got us
627                 // here.
628                 c.t.updatePieceCompletion(i)
629         }
630         // We've probably dropped a piece from storage, but there's no way to communicate this to the
631         // peer. If they ask for it again, we kick them allowing us to send them updated piece states if
632         // we reconnect. TODO: Instead, we could just try to update them with Bitfield or HaveNone and
633         // if they kick us for breaking protocol, on reconnect we will be compliant again (at least
634         // initially).
635         if c.fastEnabled() {
636                 c.reject(r)
637         } else {
638                 if c.choking {
639                         // If fast isn't enabled, I think we would have wiped all peer requests when we last
640                         // choked, and requests while we're choking would be ignored. It could be possible that
641                         // a peer request data read completed concurrently to it being deleted elsewhere.
642                         c.logger.WithDefaultLevel(log.Warning).Printf("already choking peer, requests might not be rejected correctly")
643                 }
644                 // Choking a non-fast peer should cause them to flush all their requests.
645                 c.choke(c.write)
646         }
647 }
648
649 func (c *PeerConn) readPeerRequestData(r Request, prs *peerRequestState) ([]byte, error) {
650         // Should we depend on Torrent closure here? I think it's okay to get cancelled from elsewhere,
651         // or fail to read and then cleanup.
652         ctx := context.Background()
653         err := prs.allocReservation.Wait(ctx)
654         if err != nil {
655                 if ctx.Err() == nil {
656                         // The error is from the reservation itself. Something is very broken, or we're not
657                         // guarding against excessively large requests.
658                         err = log.WithLevel(log.Critical, err)
659                 }
660                 err = fmt.Errorf("waiting for alloc limit reservation: %w", err)
661                 return nil, err
662         }
663         b := make([]byte, r.Length)
664         p := c.t.info.Piece(int(r.Index))
665         n, err := c.t.readAt(b, p.Offset()+int64(r.Begin))
666         if n == len(b) {
667                 if err == io.EOF {
668                         err = nil
669                 }
670         } else {
671                 if err == nil {
672                         panic("expected error")
673                 }
674         }
675         return b, err
676 }
677
678 func (c *PeerConn) logProtocolBehaviour(level log.Level, format string, arg ...interface{}) {
679         c.logger.WithContextText(fmt.Sprintf(
680                 "peer id %q, ext v %q", c.PeerID, c.PeerClientName.Load(),
681         )).SkipCallers(1).Levelf(level, format, arg...)
682 }
683
684 // Processes incoming BitTorrent wire-protocol messages. The client lock is held upon entry and
685 // exit. Returning will end the connection.
686 func (c *PeerConn) mainReadLoop() (err error) {
687         defer func() {
688                 if err != nil {
689                         torrent.Add("connection.mainReadLoop returned with error", 1)
690                 } else {
691                         torrent.Add("connection.mainReadLoop returned with no error", 1)
692                 }
693         }()
694         t := c.t
695         cl := t.cl
696
697         decoder := pp.Decoder{
698                 R:         bufio.NewReaderSize(c.r, 1<<17),
699                 MaxLength: 4 * pp.Integer(max(int64(t.chunkSize), defaultChunkSize)),
700                 Pool:      &t.chunkPool,
701         }
702         for {
703                 var msg pp.Message
704                 func() {
705                         cl.unlock()
706                         defer cl.lock()
707                         err = decoder.Decode(&msg)
708                 }()
709                 if cb := c.callbacks.ReadMessage; cb != nil && err == nil {
710                         cb(c, &msg)
711                 }
712                 if t.closed.IsSet() || c.closed.IsSet() {
713                         return nil
714                 }
715                 if err != nil {
716                         return err
717                 }
718                 c.lastMessageReceived = time.Now()
719                 if msg.Keepalive {
720                         receivedKeepalives.Add(1)
721                         continue
722                 }
723                 messageTypesReceived.Add(msg.Type.String(), 1)
724                 if msg.Type.FastExtension() && !c.fastEnabled() {
725                         runSafeExtraneous(func() { torrent.Add("fast messages received when extension is disabled", 1) })
726                         return fmt.Errorf("received fast extension message (type=%v) but extension is disabled", msg.Type)
727                 }
728                 switch msg.Type {
729                 case pp.Choke:
730                         if c.peerChoking {
731                                 break
732                         }
733                         if !c.fastEnabled() {
734                                 c.deleteAllRequests("choked by non-fast PeerConn")
735                         } else {
736                                 // We don't decrement pending requests here, let's wait for the peer to either
737                                 // reject or satisfy the outstanding requests. Additionally, some peers may unchoke
738                                 // us and resume where they left off, we don't want to have piled on to those chunks
739                                 // in the meanwhile. I think a peer's ability to abuse this should be limited: they
740                                 // could let us request a lot of stuff, then choke us and never reject, but they're
741                                 // only a single peer, our chunk balancing should smooth over this abuse.
742                         }
743                         c.peerChoking = true
744                         c.updateExpectingChunks()
745                 case pp.Unchoke:
746                         if !c.peerChoking {
747                                 // Some clients do this for some reason. Transmission doesn't error on this, so we
748                                 // won't for consistency.
749                                 c.logProtocolBehaviour(log.Debug, "received unchoke when already unchoked")
750                                 break
751                         }
752                         c.peerChoking = false
753                         preservedCount := 0
754                         c.requestState.Requests.Iterate(func(x RequestIndex) bool {
755                                 if !c.peerAllowedFast.Contains(c.t.pieceIndexOfRequestIndex(x)) {
756                                         preservedCount++
757                                 }
758                                 return true
759                         })
760                         if preservedCount != 0 {
761                                 // TODO: Yes this is a debug log but I'm not happy with the state of the logging lib
762                                 // right now.
763                                 c.logger.Levelf(log.Debug,
764                                         "%v requests were preserved while being choked (fast=%v)",
765                                         preservedCount,
766                                         c.fastEnabled())
767
768                                 torrent.Add("requestsPreservedThroughChoking", int64(preservedCount))
769                         }
770                         if !c.t._pendingPieces.IsEmpty() {
771                                 c.updateRequests("unchoked")
772                         }
773                         c.updateExpectingChunks()
774                 case pp.Interested:
775                         c.peerInterested = true
776                         c.tickleWriter()
777                 case pp.NotInterested:
778                         c.peerInterested = false
779                         // We don't clear their requests since it isn't clear in the spec.
780                         // We'll probably choke them for this, which will clear them if
781                         // appropriate, and is clearly specified.
782                 case pp.Have:
783                         err = c.peerSentHave(pieceIndex(msg.Index))
784                 case pp.Bitfield:
785                         err = c.peerSentBitfield(msg.Bitfield)
786                 case pp.Request:
787                         r := newRequestFromMessage(&msg)
788                         err = c.onReadRequest(r, true)
789                         if err != nil {
790                                 err = fmt.Errorf("on reading request %v: %w", r, err)
791                         }
792                 case pp.Piece:
793                         c.doChunkReadStats(int64(len(msg.Piece)))
794                         err = c.receiveChunk(&msg)
795                         if len(msg.Piece) == int(t.chunkSize) {
796                                 t.chunkPool.Put(&msg.Piece)
797                         }
798                         if err != nil {
799                                 err = fmt.Errorf("receiving chunk: %w", err)
800                         }
801                 case pp.Cancel:
802                         req := newRequestFromMessage(&msg)
803                         c.onPeerSentCancel(req)
804                 case pp.Port:
805                         ipa, ok := tryIpPortFromNetAddr(c.RemoteAddr)
806                         if !ok {
807                                 break
808                         }
809                         pingAddr := net.UDPAddr{
810                                 IP:   ipa.IP,
811                                 Port: ipa.Port,
812                         }
813                         if msg.Port != 0 {
814                                 pingAddr.Port = int(msg.Port)
815                         }
816                         cl.eachDhtServer(func(s DhtServer) {
817                                 go s.Ping(&pingAddr)
818                         })
819                 case pp.Suggest:
820                         torrent.Add("suggests received", 1)
821                         log.Fmsg("peer suggested piece %d", msg.Index).AddValues(c, msg.Index).LogLevel(log.Debug, c.t.logger)
822                         c.updateRequests("suggested")
823                 case pp.HaveAll:
824                         err = c.onPeerSentHaveAll()
825                 case pp.HaveNone:
826                         err = c.peerSentHaveNone()
827                 case pp.Reject:
828                         req := newRequestFromMessage(&msg)
829                         if !c.remoteRejectedRequest(c.t.requestIndexFromRequest(req)) {
830                                 c.logger.Printf("received invalid reject [request=%v, peer=%v]", req, c)
831                                 err = fmt.Errorf("received invalid reject [request=%v]", req)
832                         }
833                 case pp.AllowedFast:
834                         torrent.Add("allowed fasts received", 1)
835                         log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).LogLevel(log.Debug, c.t.logger)
836                         c.updateRequests("PeerConn.mainReadLoop allowed fast")
837                 case pp.Extended:
838                         err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload)
839                 default:
840                         err = fmt.Errorf("received unknown message type: %#v", msg.Type)
841                 }
842                 if err != nil {
843                         return err
844                 }
845         }
846 }
847
848 func (c *PeerConn) onReadExtendedMsg(id pp.ExtensionNumber, payload []byte) (err error) {
849         defer func() {
850                 // TODO: Should we still do this?
851                 if err != nil {
852                         // These clients use their own extension IDs for outgoing message
853                         // types, which is incorrect.
854                         if bytes.HasPrefix(c.PeerID[:], []byte("-SD0100-")) || strings.HasPrefix(string(c.PeerID[:]), "-XL0012-") {
855                                 err = nil
856                         }
857                 }
858         }()
859         t := c.t
860         cl := t.cl
861         switch id {
862         case pp.HandshakeExtendedID:
863                 var d pp.ExtendedHandshakeMessage
864                 if err := bencode.Unmarshal(payload, &d); err != nil {
865                         c.logger.Printf("error parsing extended handshake message %q: %s", payload, err)
866                         return fmt.Errorf("unmarshalling extended handshake payload: %w", err)
867                 }
868                 if cb := c.callbacks.ReadExtendedHandshake; cb != nil {
869                         cb(c, &d)
870                 }
871                 // c.logger.WithDefaultLevel(log.Debug).Printf("received extended handshake message:\n%s", spew.Sdump(d))
872                 if d.Reqq != 0 {
873                         c.PeerMaxRequests = d.Reqq
874                 }
875                 c.PeerClientName.Store(d.V)
876                 if c.PeerExtensionIDs == nil {
877                         c.PeerExtensionIDs = make(map[pp.ExtensionName]pp.ExtensionNumber, len(d.M))
878                 }
879                 c.PeerListenPort = d.Port
880                 c.PeerPrefersEncryption = d.Encryption
881                 for name, id := range d.M {
882                         if _, ok := c.PeerExtensionIDs[name]; !ok {
883                                 peersSupportingExtension.Add(
884                                         // expvar.Var.String must produce valid JSON. "ut_payme\xeet_address" was being
885                                         // entered here which caused problems later when unmarshalling.
886                                         strconv.Quote(string(name)),
887                                         1)
888                         }
889                         c.PeerExtensionIDs[name] = id
890                 }
891                 if d.MetadataSize != 0 {
892                         if err = t.setMetadataSize(d.MetadataSize); err != nil {
893                                 return fmt.Errorf("setting metadata size to %d: %w", d.MetadataSize, err)
894                         }
895                 }
896                 c.requestPendingMetadata()
897                 if !t.cl.config.DisablePEX {
898                         t.pex.Add(c) // we learnt enough now
899                         // This checks the extension is supported internally.
900                         c.pex.Init(c)
901                 }
902                 return nil
903         case metadataExtendedId:
904                 err := cl.gotMetadataExtensionMsg(payload, t, c)
905                 if err != nil {
906                         return fmt.Errorf("handling metadata extension message: %w", err)
907                 }
908                 return nil
909         case pexExtendedId:
910                 if !c.pex.IsEnabled() {
911                         return nil // or hang-up maybe?
912                 }
913                 err = c.pex.Recv(payload)
914                 if err != nil {
915                         err = fmt.Errorf("receiving pex message: %w", err)
916                 }
917                 return
918         case utHolepunchExtendedId:
919                 var msg utHolepunch.Msg
920                 err = msg.UnmarshalBinary(payload)
921                 if err != nil {
922                         err = fmt.Errorf("unmarshalling ut_holepunch message: %w", err)
923                         return
924                 }
925                 err = c.t.handleReceivedUtHolepunchMsg(msg, c)
926                 return
927         default:
928                 return fmt.Errorf("unexpected extended message ID: %v", id)
929         }
930 }
931
932 // Set both the Reader and Writer for the connection from a single ReadWriter.
933 func (cn *PeerConn) setRW(rw io.ReadWriter) {
934         cn.r = rw
935         cn.w = rw
936 }
937
938 // Returns the Reader and Writer as a combined ReadWriter.
939 func (cn *PeerConn) rw() io.ReadWriter {
940         return struct {
941                 io.Reader
942                 io.Writer
943         }{cn.r, cn.w}
944 }
945
946 func (c *PeerConn) uploadAllowed() bool {
947         if c.t.cl.config.NoUpload {
948                 return false
949         }
950         if c.t.dataUploadDisallowed {
951                 return false
952         }
953         if c.t.seeding() {
954                 return true
955         }
956         if !c.peerHasWantedPieces() {
957                 return false
958         }
959         // Don't upload more than 100 KiB more than we download.
960         if c._stats.BytesWrittenData.Int64() >= c._stats.BytesReadData.Int64()+100<<10 {
961                 return false
962         }
963         return true
964 }
965
966 func (c *PeerConn) setRetryUploadTimer(delay time.Duration) {
967         if c.uploadTimer == nil {
968                 c.uploadTimer = time.AfterFunc(delay, c.tickleWriter)
969         } else {
970                 c.uploadTimer.Reset(delay)
971         }
972 }
973
974 // Also handles choking and unchoking of the remote peer.
975 func (c *PeerConn) upload(msg func(pp.Message) bool) bool {
976         // Breaking or completing this loop means we don't want to upload to the
977         // peer anymore, and we choke them.
978 another:
979         for c.uploadAllowed() {
980                 // We want to upload to the peer.
981                 if !c.unchoke(msg) {
982                         return false
983                 }
984                 for r, state := range c.peerRequests {
985                         if state.data == nil {
986                                 continue
987                         }
988                         res := c.t.cl.config.UploadRateLimiter.ReserveN(time.Now(), int(r.Length))
989                         if !res.OK() {
990                                 panic(fmt.Sprintf("upload rate limiter burst size < %d", r.Length))
991                         }
992                         delay := res.Delay()
993                         if delay > 0 {
994                                 res.Cancel()
995                                 c.setRetryUploadTimer(delay)
996                                 // Hard to say what to return here.
997                                 return true
998                         }
999                         more := c.sendChunk(r, msg, state)
1000                         delete(c.peerRequests, r)
1001                         if !more {
1002                                 return false
1003                         }
1004                         goto another
1005                 }
1006                 return true
1007         }
1008         return c.choke(msg)
1009 }
1010
1011 func (cn *PeerConn) drop() {
1012         cn.t.dropConnection(cn)
1013 }
1014
1015 func (cn *PeerConn) ban() {
1016         cn.t.cl.banPeerIP(cn.remoteIp())
1017 }
1018
1019 // This is called when something has changed that should wake the writer, such as putting stuff into
1020 // the writeBuffer, or changing some state that the writer can act on.
1021 func (c *PeerConn) tickleWriter() {
1022         c.messageWriter.writeCond.Broadcast()
1023 }
1024
1025 func (c *PeerConn) sendChunk(r Request, msg func(pp.Message) bool, state *peerRequestState) (more bool) {
1026         c.lastChunkSent = time.Now()
1027         state.allocReservation.Release()
1028         return msg(pp.Message{
1029                 Type:  pp.Piece,
1030                 Index: r.Index,
1031                 Begin: r.Begin,
1032                 Piece: state.data,
1033         })
1034 }
1035
1036 func (c *PeerConn) setTorrent(t *Torrent) {
1037         if c.t != nil {
1038                 panic("connection already associated with a torrent")
1039         }
1040         c.t = t
1041         c.logger.WithDefaultLevel(log.Debug).Printf("set torrent=%v", t)
1042         t.reconcileHandshakeStats(c)
1043 }
1044
1045 func (c *PeerConn) pexPeerFlags() pp.PexPeerFlags {
1046         f := pp.PexPeerFlags(0)
1047         if c.PeerPrefersEncryption {
1048                 f |= pp.PexPrefersEncryption
1049         }
1050         if c.outgoing {
1051                 f |= pp.PexOutgoingConn
1052         }
1053         if c.utp() {
1054                 f |= pp.PexSupportsUtp
1055         }
1056         return f
1057 }
1058
1059 // This returns the address to use if we want to dial the peer again. It incorporates the peer's
1060 // advertised listen port.
1061 func (c *PeerConn) dialAddr() PeerRemoteAddr {
1062         if c.outgoing || c.PeerListenPort == 0 {
1063                 return c.RemoteAddr
1064         }
1065         addrPort, err := addrPortFromPeerRemoteAddr(c.RemoteAddr)
1066         if err != nil {
1067                 c.logger.Levelf(
1068                         log.Warning,
1069                         "error parsing %q for alternate dial port: %v",
1070                         c.RemoteAddr,
1071                         err,
1072                 )
1073                 return c.RemoteAddr
1074         }
1075         return netip.AddrPortFrom(addrPort.Addr(), uint16(c.PeerListenPort))
1076 }
1077
1078 func (c *PeerConn) pexEvent(t pexEventType) (_ pexEvent, err error) {
1079         f := c.pexPeerFlags()
1080         dialAddr := c.dialAddr()
1081         addr, err := addrPortFromPeerRemoteAddr(dialAddr)
1082         if err != nil || !addr.IsValid() {
1083                 err = fmt.Errorf("parsing dial addr %q: %w", dialAddr, err)
1084                 return
1085         }
1086         return pexEvent{t, addr, f, nil}, nil
1087 }
1088
1089 func (c *PeerConn) String() string {
1090         return fmt.Sprintf("%T %p [id=%q, exts=%v, v=%q]", c, c, c.PeerID, c.PeerExtensionBytes, c.PeerClientName.Load())
1091 }
1092
1093 // Returns the pieces the peer could have based on their claims. If we don't know how many pieces
1094 // are in the torrent, it could be a very large range the peer has sent HaveAll.
1095 func (cn *PeerConn) PeerPieces() *roaring.Bitmap {
1096         cn.locker().RLock()
1097         defer cn.locker().RUnlock()
1098         return cn.newPeerPieces()
1099 }
1100
1101 func (pc *PeerConn) remoteIsTransmission() bool {
1102         return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-'
1103 }
1104
1105 func (pc *PeerConn) remoteDialAddrPort() (netip.AddrPort, error) {
1106         dialAddr := pc.dialAddr()
1107         return addrPortFromPeerRemoteAddr(dialAddr)
1108 }
1109
1110 func (pc *PeerConn) bitExtensionEnabled(bit pp.ExtensionBit) bool {
1111         return pc.t.cl.config.Extensions.GetBit(bit) && pc.PeerExtensionBytes.GetBit(bit)
1112 }
1113
1114 func (cn *PeerConn) peerPiecesChanged() {
1115         cn.t.maybeDropMutuallyCompletePeer(cn)
1116 }
1117
1118 // Returns whether the connection could be useful to us. We're seeding and
1119 // they want data, we don't have metainfo and they can provide it, etc.
1120 func (c *PeerConn) useful() bool {
1121         t := c.t
1122         if c.closed.IsSet() {
1123                 return false
1124         }
1125         if !t.haveInfo() {
1126                 return c.supportsExtension("ut_metadata")
1127         }
1128         if t.seeding() && c.peerInterested {
1129                 return true
1130         }
1131         if c.peerHasWantedPieces() {
1132                 return true
1133         }
1134         return false
1135 }