]> Sergey Matveev's repositories - btrtrc.git/blob - torrent.go
Cache sqlite storage capacity for a short while
[btrtrc.git] / torrent.go
1 package torrent
2
3 import (
4         "bytes"
5         "container/heap"
6         "context"
7         "crypto/sha1"
8         "errors"
9         "fmt"
10         "io"
11         "math/rand"
12         "net/netip"
13         "net/url"
14         "sort"
15         "strings"
16         "text/tabwriter"
17         "time"
18         "unsafe"
19
20         "github.com/RoaringBitmap/roaring"
21         "github.com/anacrolix/chansync"
22         "github.com/anacrolix/chansync/events"
23         "github.com/anacrolix/dht/v2"
24         . "github.com/anacrolix/generics"
25         g "github.com/anacrolix/generics"
26         "github.com/anacrolix/log"
27         "github.com/anacrolix/missinggo/slices"
28         "github.com/anacrolix/missinggo/v2"
29         "github.com/anacrolix/missinggo/v2/bitmap"
30         "github.com/anacrolix/missinggo/v2/pubsub"
31         "github.com/anacrolix/multiless"
32         "github.com/anacrolix/sync"
33         "github.com/pion/datachannel"
34         "golang.org/x/exp/maps"
35
36         "github.com/anacrolix/torrent/bencode"
37         "github.com/anacrolix/torrent/common"
38         "github.com/anacrolix/torrent/internal/check"
39         "github.com/anacrolix/torrent/internal/nestedmaps"
40         "github.com/anacrolix/torrent/metainfo"
41         pp "github.com/anacrolix/torrent/peer_protocol"
42         utHolepunch "github.com/anacrolix/torrent/peer_protocol/ut-holepunch"
43         request_strategy "github.com/anacrolix/torrent/request-strategy"
44         "github.com/anacrolix/torrent/segments"
45         "github.com/anacrolix/torrent/storage"
46         "github.com/anacrolix/torrent/tracker"
47         typedRoaring "github.com/anacrolix/torrent/typed-roaring"
48         "github.com/anacrolix/torrent/webseed"
49         "github.com/anacrolix/torrent/webtorrent"
50 )
51
52 // Maintains state of torrent within a Client. Many methods should not be called before the info is
53 // available, see .Info and .GotInfo.
54 type Torrent struct {
55         // Torrent-level aggregate statistics. First in struct to ensure 64-bit
56         // alignment. See #262.
57         stats  ConnStats
58         cl     *Client
59         logger log.Logger
60
61         networkingEnabled      chansync.Flag
62         dataDownloadDisallowed chansync.Flag
63         dataUploadDisallowed   bool
64         userOnWriteChunkErr    func(error)
65
66         closed   chansync.SetOnce
67         onClose  []func()
68         infoHash metainfo.Hash
69         pieces   []Piece
70
71         // The order pieces are requested if there's no stronger reason like availability or priority.
72         pieceRequestOrder []int
73         // Values are the piece indices that changed.
74         pieceStateChanges pubsub.PubSub[PieceStateChange]
75         // The size of chunks to request from peers over the wire. This is
76         // normally 16KiB by convention these days.
77         chunkSize pp.Integer
78         chunkPool sync.Pool
79         // Total length of the torrent in bytes. Stored because it's not O(1) to
80         // get this from the info dict.
81         _length Option[int64]
82
83         // The storage to open when the info dict becomes available.
84         storageOpener *storage.Client
85         // Storage for torrent data.
86         storage *storage.Torrent
87         // Read-locked for using storage, and write-locked for Closing.
88         storageLock sync.RWMutex
89
90         // TODO: Only announce stuff is used?
91         metainfo metainfo.MetaInfo
92
93         // The info dict. nil if we don't have it (yet).
94         info      *metainfo.Info
95         fileIndex segments.Index
96         files     *[]*File
97
98         _chunksPerRegularPiece chunkIndexType
99
100         webSeeds map[string]*Peer
101         // Active peer connections, running message stream loops. TODO: Make this
102         // open (not-closed) connections only.
103         conns               map[*PeerConn]struct{}
104         maxEstablishedConns int
105         // Set of addrs to which we're attempting to connect. Connections are
106         // half-open until all handshakes are completed.
107         halfOpen map[string]map[outgoingConnAttemptKey]*PeerInfo
108
109         // Reserve of peers to connect to. A peer can be both here and in the
110         // active connections if were told about the peer after connecting with
111         // them. That encourages us to reconnect to peers that are well known in
112         // the swarm.
113         peers prioritizedPeers
114         // Whether we want to know more peers.
115         wantPeersEvent missinggo.Event
116         // An announcer for each tracker URL.
117         trackerAnnouncers map[string]torrentTrackerAnnouncer
118         // How many times we've initiated a DHT announce. TODO: Move into stats.
119         numDHTAnnounces int
120
121         // Name used if the info name isn't available. Should be cleared when the
122         // Info does become available.
123         nameMu      sync.RWMutex
124         displayName string
125
126         // The bencoded bytes of the info dict. This is actively manipulated if
127         // the info bytes aren't initially available, and we try to fetch them
128         // from peers.
129         metadataBytes []byte
130         // Each element corresponds to the 16KiB metadata pieces. If true, we have
131         // received that piece.
132         metadataCompletedChunks []bool
133         metadataChanged         sync.Cond
134
135         // Closed when .Info is obtained.
136         gotMetainfoC chan struct{}
137
138         readers                map[*reader]struct{}
139         _readerNowPieces       bitmap.Bitmap
140         _readerReadaheadPieces bitmap.Bitmap
141
142         // A cache of pieces we need to get. Calculated from various piece and
143         // file priorities and completion states elsewhere.
144         _pendingPieces roaring.Bitmap
145         // A cache of completed piece indices.
146         _completedPieces roaring.Bitmap
147         // Pieces that need to be hashed.
148         piecesQueuedForHash       bitmap.Bitmap
149         activePieceHashes         int
150         initialPieceCheckDisabled bool
151
152         connsWithAllPieces map[*Peer]struct{}
153
154         requestState map[RequestIndex]requestState
155         // Chunks we've written to since the corresponding piece was last checked.
156         dirtyChunks typedRoaring.Bitmap[RequestIndex]
157
158         pex pexState
159
160         // Is On when all pieces are complete.
161         Complete chansync.Flag
162
163         // Torrent sources in use keyed by the source string.
164         activeSources sync.Map
165         sourcesLogger log.Logger
166
167         smartBanCache smartBanCache
168
169         // Large allocations reused between request state updates.
170         requestPieceStates []request_strategy.PieceRequestOrderState
171         requestIndexes     []RequestIndex
172
173         disableTriggers bool
174 }
175
176 type outgoingConnAttemptKey = *PeerInfo
177
178 func (t *Torrent) length() int64 {
179         return t._length.Value
180 }
181
182 func (t *Torrent) selectivePieceAvailabilityFromPeers(i pieceIndex) (count int) {
183         // This could be done with roaring.BitSliceIndexing.
184         t.iterPeers(func(peer *Peer) {
185                 if _, ok := t.connsWithAllPieces[peer]; ok {
186                         return
187                 }
188                 if peer.peerHasPiece(i) {
189                         count++
190                 }
191         })
192         return
193 }
194
195 func (t *Torrent) decPieceAvailability(i pieceIndex) {
196         if !t.haveInfo() {
197                 return
198         }
199         p := t.piece(i)
200         if p.relativeAvailability <= 0 {
201                 panic(p.relativeAvailability)
202         }
203         p.relativeAvailability--
204         t.updatePieceRequestOrderPiece(i)
205 }
206
207 func (t *Torrent) incPieceAvailability(i pieceIndex) {
208         // If we don't the info, this should be reconciled when we do.
209         if t.haveInfo() {
210                 p := t.piece(i)
211                 p.relativeAvailability++
212                 t.updatePieceRequestOrderPiece(i)
213         }
214 }
215
216 func (t *Torrent) readerNowPieces() bitmap.Bitmap {
217         return t._readerNowPieces
218 }
219
220 func (t *Torrent) readerReadaheadPieces() bitmap.Bitmap {
221         return t._readerReadaheadPieces
222 }
223
224 func (t *Torrent) ignorePieceForRequests(i pieceIndex) bool {
225         return !t.wantPieceIndex(i)
226 }
227
228 // Returns a channel that is closed when the Torrent is closed.
229 func (t *Torrent) Closed() events.Done {
230         return t.closed.Done()
231 }
232
233 // KnownSwarm returns the known subset of the peers in the Torrent's swarm, including active,
234 // pending, and half-open peers.
235 func (t *Torrent) KnownSwarm() (ks []PeerInfo) {
236         // Add pending peers to the list
237         t.peers.Each(func(peer PeerInfo) {
238                 ks = append(ks, peer)
239         })
240
241         // Add half-open peers to the list
242         for _, attempts := range t.halfOpen {
243                 for _, peer := range attempts {
244                         ks = append(ks, *peer)
245                 }
246         }
247
248         // Add active peers to the list
249         t.cl.rLock()
250         defer t.cl.rUnlock()
251         for conn := range t.conns {
252                 ks = append(ks, PeerInfo{
253                         Id:     conn.PeerID,
254                         Addr:   conn.RemoteAddr,
255                         Source: conn.Discovery,
256                         // > If the connection is encrypted, that's certainly enough to set SupportsEncryption.
257                         // > But if we're not connected to them with an encrypted connection, I couldn't say
258                         // > what's appropriate. We can carry forward the SupportsEncryption value as we
259                         // > received it from trackers/DHT/PEX, or just use the encryption state for the
260                         // > connection. It's probably easiest to do the latter for now.
261                         // https://github.com/anacrolix/torrent/pull/188
262                         SupportsEncryption: conn.headerEncrypted,
263                 })
264         }
265
266         return
267 }
268
269 func (t *Torrent) setChunkSize(size pp.Integer) {
270         t.chunkSize = size
271         t.chunkPool = sync.Pool{
272                 New: func() interface{} {
273                         b := make([]byte, size)
274                         return &b
275                 },
276         }
277 }
278
279 func (t *Torrent) pieceComplete(piece pieceIndex) bool {
280         return t._completedPieces.Contains(bitmap.BitIndex(piece))
281 }
282
283 func (t *Torrent) pieceCompleteUncached(piece pieceIndex) storage.Completion {
284         if t.storage == nil {
285                 return storage.Completion{Complete: false, Ok: true}
286         }
287         return t.pieces[piece].Storage().Completion()
288 }
289
290 // There's a connection to that address already.
291 func (t *Torrent) addrActive(addr string) bool {
292         if _, ok := t.halfOpen[addr]; ok {
293                 return true
294         }
295         for c := range t.conns {
296                 ra := c.RemoteAddr
297                 if ra.String() == addr {
298                         return true
299                 }
300         }
301         return false
302 }
303
304 func (t *Torrent) appendUnclosedConns(ret []*PeerConn) []*PeerConn {
305         return t.appendConns(ret, func(conn *PeerConn) bool {
306                 return !conn.closed.IsSet()
307         })
308 }
309
310 func (t *Torrent) appendConns(ret []*PeerConn, f func(*PeerConn) bool) []*PeerConn {
311         for c := range t.conns {
312                 if f(c) {
313                         ret = append(ret, c)
314                 }
315         }
316         return ret
317 }
318
319 func (t *Torrent) addPeer(p PeerInfo) (added bool) {
320         cl := t.cl
321         torrent.Add(fmt.Sprintf("peers added by source %q", p.Source), 1)
322         if t.closed.IsSet() {
323                 return false
324         }
325         if ipAddr, ok := tryIpPortFromNetAddr(p.Addr); ok {
326                 if cl.badPeerIPPort(ipAddr.IP, ipAddr.Port) {
327                         torrent.Add("peers not added because of bad addr", 1)
328                         // cl.logger.Printf("peers not added because of bad addr: %v", p)
329                         return false
330                 }
331         }
332         if replaced, ok := t.peers.AddReturningReplacedPeer(p); ok {
333                 torrent.Add("peers replaced", 1)
334                 if !replaced.equal(p) {
335                         t.logger.WithDefaultLevel(log.Debug).Printf("added %v replacing %v", p, replaced)
336                         added = true
337                 }
338         } else {
339                 added = true
340         }
341         t.openNewConns()
342         for t.peers.Len() > cl.config.TorrentPeersHighWater {
343                 _, ok := t.peers.DeleteMin()
344                 if ok {
345                         torrent.Add("excess reserve peers discarded", 1)
346                 }
347         }
348         return
349 }
350
351 func (t *Torrent) invalidateMetadata() {
352         for i := 0; i < len(t.metadataCompletedChunks); i++ {
353                 t.metadataCompletedChunks[i] = false
354         }
355         t.nameMu.Lock()
356         t.gotMetainfoC = make(chan struct{})
357         t.info = nil
358         t.nameMu.Unlock()
359 }
360
361 func (t *Torrent) saveMetadataPiece(index int, data []byte) {
362         if t.haveInfo() {
363                 return
364         }
365         if index >= len(t.metadataCompletedChunks) {
366                 t.logger.Printf("%s: ignoring metadata piece %d", t, index)
367                 return
368         }
369         copy(t.metadataBytes[(1<<14)*index:], data)
370         t.metadataCompletedChunks[index] = true
371 }
372
373 func (t *Torrent) metadataPieceCount() int {
374         return (len(t.metadataBytes) + (1 << 14) - 1) / (1 << 14)
375 }
376
377 func (t *Torrent) haveMetadataPiece(piece int) bool {
378         if t.haveInfo() {
379                 return (1<<14)*piece < len(t.metadataBytes)
380         } else {
381                 return piece < len(t.metadataCompletedChunks) && t.metadataCompletedChunks[piece]
382         }
383 }
384
385 func (t *Torrent) metadataSize() int {
386         return len(t.metadataBytes)
387 }
388
389 func infoPieceHashes(info *metainfo.Info) (ret [][]byte) {
390         for i := 0; i < len(info.Pieces); i += sha1.Size {
391                 ret = append(ret, info.Pieces[i:i+sha1.Size])
392         }
393         return
394 }
395
396 func (t *Torrent) makePieces() {
397         hashes := infoPieceHashes(t.info)
398         t.pieces = make([]Piece, len(hashes))
399         for i, hash := range hashes {
400                 piece := &t.pieces[i]
401                 piece.t = t
402                 piece.index = pieceIndex(i)
403                 piece.noPendingWrites.L = &piece.pendingWritesMutex
404                 piece.hash = (*metainfo.Hash)(unsafe.Pointer(&hash[0]))
405                 files := *t.files
406                 beginFile := pieceFirstFileIndex(piece.torrentBeginOffset(), files)
407                 endFile := pieceEndFileIndex(piece.torrentEndOffset(), files)
408                 piece.files = files[beginFile:endFile]
409         }
410 }
411
412 // Returns the index of the first file containing the piece. files must be
413 // ordered by offset.
414 func pieceFirstFileIndex(pieceOffset int64, files []*File) int {
415         for i, f := range files {
416                 if f.offset+f.length > pieceOffset {
417                         return i
418                 }
419         }
420         return 0
421 }
422
423 // Returns the index after the last file containing the piece. files must be
424 // ordered by offset.
425 func pieceEndFileIndex(pieceEndOffset int64, files []*File) int {
426         for i, f := range files {
427                 if f.offset+f.length >= pieceEndOffset {
428                         return i + 1
429                 }
430         }
431         return 0
432 }
433
434 func (t *Torrent) cacheLength() {
435         var l int64
436         for _, f := range t.info.UpvertedFiles() {
437                 l += f.Length
438         }
439         t._length = Some(l)
440 }
441
442 // TODO: This shouldn't fail for storage reasons. Instead we should handle storage failure
443 // separately.
444 func (t *Torrent) setInfo(info *metainfo.Info) error {
445         if err := validateInfo(info); err != nil {
446                 return fmt.Errorf("bad info: %s", err)
447         }
448         if t.storageOpener != nil {
449                 var err error
450                 t.storage, err = t.storageOpener.OpenTorrent(info, t.infoHash)
451                 if err != nil {
452                         return fmt.Errorf("error opening torrent storage: %s", err)
453                 }
454         }
455         t.nameMu.Lock()
456         t.info = info
457         t.nameMu.Unlock()
458         t._chunksPerRegularPiece = chunkIndexType((pp.Integer(t.usualPieceSize()) + t.chunkSize - 1) / t.chunkSize)
459         t.updateComplete()
460         t.fileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
461         t.displayName = "" // Save a few bytes lol.
462         t.initFiles()
463         t.cacheLength()
464         t.makePieces()
465         return nil
466 }
467
468 func (t *Torrent) pieceRequestOrderKey(i int) request_strategy.PieceRequestOrderKey {
469         return request_strategy.PieceRequestOrderKey{
470                 InfoHash: t.infoHash,
471                 Index:    i,
472         }
473 }
474
475 // This seems to be all the follow-up tasks after info is set, that can't fail.
476 func (t *Torrent) onSetInfo() {
477         t.pieceRequestOrder = rand.Perm(t.numPieces())
478         t.initPieceRequestOrder()
479         MakeSliceWithLength(&t.requestPieceStates, t.numPieces())
480         for i := range t.pieces {
481                 p := &t.pieces[i]
482                 // Need to add relativeAvailability before updating piece completion, as that may result in conns
483                 // being dropped.
484                 if p.relativeAvailability != 0 {
485                         panic(p.relativeAvailability)
486                 }
487                 p.relativeAvailability = t.selectivePieceAvailabilityFromPeers(i)
488                 t.addRequestOrderPiece(i)
489                 t.updatePieceCompletion(i)
490                 if !t.initialPieceCheckDisabled && !p.storageCompletionOk {
491                         // t.logger.Printf("piece %s completion unknown, queueing check", p)
492                         t.queuePieceCheck(i)
493                 }
494         }
495         t.cl.event.Broadcast()
496         close(t.gotMetainfoC)
497         t.updateWantPeersEvent()
498         t.requestState = make(map[RequestIndex]requestState)
499         t.tryCreateMorePieceHashers()
500         t.iterPeers(func(p *Peer) {
501                 p.onGotInfo(t.info)
502                 p.updateRequests("onSetInfo")
503         })
504 }
505
506 // Called when metadata for a torrent becomes available.
507 func (t *Torrent) setInfoBytesLocked(b []byte) error {
508         if metainfo.HashBytes(b) != t.infoHash {
509                 return errors.New("info bytes have wrong hash")
510         }
511         var info metainfo.Info
512         if err := bencode.Unmarshal(b, &info); err != nil {
513                 return fmt.Errorf("error unmarshalling info bytes: %s", err)
514         }
515         t.metadataBytes = b
516         t.metadataCompletedChunks = nil
517         if t.info != nil {
518                 return nil
519         }
520         if err := t.setInfo(&info); err != nil {
521                 return err
522         }
523         t.onSetInfo()
524         return nil
525 }
526
527 func (t *Torrent) haveAllMetadataPieces() bool {
528         if t.haveInfo() {
529                 return true
530         }
531         if t.metadataCompletedChunks == nil {
532                 return false
533         }
534         for _, have := range t.metadataCompletedChunks {
535                 if !have {
536                         return false
537                 }
538         }
539         return true
540 }
541
542 // TODO: Propagate errors to disconnect peer.
543 func (t *Torrent) setMetadataSize(size int) (err error) {
544         if t.haveInfo() {
545                 // We already know the correct metadata size.
546                 return
547         }
548         if uint32(size) > maxMetadataSize {
549                 return log.WithLevel(log.Warning, errors.New("bad size"))
550         }
551         if len(t.metadataBytes) == size {
552                 return
553         }
554         t.metadataBytes = make([]byte, size)
555         t.metadataCompletedChunks = make([]bool, (size+(1<<14)-1)/(1<<14))
556         t.metadataChanged.Broadcast()
557         for c := range t.conns {
558                 c.requestPendingMetadata()
559         }
560         return
561 }
562
563 // The current working name for the torrent. Either the name in the info dict,
564 // or a display name given such as by the dn value in a magnet link, or "".
565 func (t *Torrent) name() string {
566         t.nameMu.RLock()
567         defer t.nameMu.RUnlock()
568         if t.haveInfo() {
569                 return t.info.BestName()
570         }
571         if t.displayName != "" {
572                 return t.displayName
573         }
574         return "infohash:" + t.infoHash.HexString()
575 }
576
577 func (t *Torrent) pieceState(index pieceIndex) (ret PieceState) {
578         p := &t.pieces[index]
579         ret.Priority = t.piecePriority(index)
580         ret.Completion = p.completion()
581         ret.QueuedForHash = p.queuedForHash()
582         ret.Hashing = p.hashing
583         ret.Checking = ret.QueuedForHash || ret.Hashing
584         ret.Marking = p.marking
585         if !ret.Complete && t.piecePartiallyDownloaded(index) {
586                 ret.Partial = true
587         }
588         return
589 }
590
591 func (t *Torrent) metadataPieceSize(piece int) int {
592         return metadataPieceSize(len(t.metadataBytes), piece)
593 }
594
595 func (t *Torrent) newMetadataExtensionMessage(c *PeerConn, msgType pp.ExtendedMetadataRequestMsgType, piece int, data []byte) pp.Message {
596         return pp.Message{
597                 Type:       pp.Extended,
598                 ExtendedID: c.PeerExtensionIDs[pp.ExtensionNameMetadata],
599                 ExtendedPayload: append(bencode.MustMarshal(pp.ExtendedMetadataRequestMsg{
600                         Piece:     piece,
601                         TotalSize: len(t.metadataBytes),
602                         Type:      msgType,
603                 }), data...),
604         }
605 }
606
607 type pieceAvailabilityRun struct {
608         Count        pieceIndex
609         Availability int
610 }
611
612 func (me pieceAvailabilityRun) String() string {
613         return fmt.Sprintf("%v(%v)", me.Count, me.Availability)
614 }
615
616 func (t *Torrent) pieceAvailabilityRuns() (ret []pieceAvailabilityRun) {
617         rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
618                 ret = append(ret, pieceAvailabilityRun{Availability: el.(int), Count: int(count)})
619         })
620         for i := range t.pieces {
621                 rle.Append(t.pieces[i].availability(), 1)
622         }
623         rle.Flush()
624         return
625 }
626
627 func (t *Torrent) pieceAvailabilityFrequencies() (freqs []int) {
628         freqs = make([]int, t.numActivePeers()+1)
629         for i := range t.pieces {
630                 freqs[t.piece(i).availability()]++
631         }
632         return
633 }
634
635 func (t *Torrent) pieceStateRuns() (ret PieceStateRuns) {
636         rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
637                 ret = append(ret, PieceStateRun{
638                         PieceState: el.(PieceState),
639                         Length:     int(count),
640                 })
641         })
642         for index := range t.pieces {
643                 rle.Append(t.pieceState(pieceIndex(index)), 1)
644         }
645         rle.Flush()
646         return
647 }
648
649 // Produces a small string representing a PieceStateRun.
650 func (psr PieceStateRun) String() (ret string) {
651         ret = fmt.Sprintf("%d", psr.Length)
652         ret += func() string {
653                 switch psr.Priority {
654                 case PiecePriorityNext:
655                         return "N"
656                 case PiecePriorityNormal:
657                         return "."
658                 case PiecePriorityReadahead:
659                         return "R"
660                 case PiecePriorityNow:
661                         return "!"
662                 case PiecePriorityHigh:
663                         return "H"
664                 default:
665                         return ""
666                 }
667         }()
668         if psr.Hashing {
669                 ret += "H"
670         }
671         if psr.QueuedForHash {
672                 ret += "Q"
673         }
674         if psr.Marking {
675                 ret += "M"
676         }
677         if psr.Partial {
678                 ret += "P"
679         }
680         if psr.Complete {
681                 ret += "C"
682         }
683         if !psr.Ok {
684                 ret += "?"
685         }
686         return
687 }
688
689 func (t *Torrent) writeStatus(w io.Writer) {
690         fmt.Fprintf(w, "Infohash: %s\n", t.infoHash.HexString())
691         fmt.Fprintf(w, "Metadata length: %d\n", t.metadataSize())
692         if !t.haveInfo() {
693                 fmt.Fprintf(w, "Metadata have: ")
694                 for _, h := range t.metadataCompletedChunks {
695                         fmt.Fprintf(w, "%c", func() rune {
696                                 if h {
697                                         return 'H'
698                                 } else {
699                                         return '.'
700                                 }
701                         }())
702                 }
703                 fmt.Fprintln(w)
704         }
705         fmt.Fprintf(w, "Piece length: %s\n",
706                 func() string {
707                         if t.haveInfo() {
708                                 return fmt.Sprintf("%v (%v chunks)",
709                                         t.usualPieceSize(),
710                                         float64(t.usualPieceSize())/float64(t.chunkSize))
711                         } else {
712                                 return "no info"
713                         }
714                 }(),
715         )
716         if t.info != nil {
717                 fmt.Fprintf(w, "Num Pieces: %d (%d completed)\n", t.numPieces(), t.numPiecesCompleted())
718                 fmt.Fprintf(w, "Piece States: %s\n", t.pieceStateRuns())
719                 // Generates a huge, unhelpful listing when piece availability is very scattered. Prefer
720                 // availability frequencies instead.
721                 if false {
722                         fmt.Fprintf(w, "Piece availability: %v\n", strings.Join(func() (ret []string) {
723                                 for _, run := range t.pieceAvailabilityRuns() {
724                                         ret = append(ret, run.String())
725                                 }
726                                 return
727                         }(), " "))
728                 }
729                 fmt.Fprintf(w, "Piece availability frequency: %v\n", strings.Join(
730                         func() (ret []string) {
731                                 for avail, freq := range t.pieceAvailabilityFrequencies() {
732                                         if freq == 0 {
733                                                 continue
734                                         }
735                                         ret = append(ret, fmt.Sprintf("%v: %v", avail, freq))
736                                 }
737                                 return
738                         }(),
739                         ", "))
740         }
741         fmt.Fprintf(w, "Reader Pieces:")
742         t.forReaderOffsetPieces(func(begin, end pieceIndex) (again bool) {
743                 fmt.Fprintf(w, " %d:%d", begin, end)
744                 return true
745         })
746         fmt.Fprintln(w)
747
748         fmt.Fprintf(w, "Enabled trackers:\n")
749         func() {
750                 tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
751                 fmt.Fprintf(tw, "    URL\tExtra\n")
752                 for _, ta := range slices.Sort(slices.FromMapElems(t.trackerAnnouncers), func(l, r torrentTrackerAnnouncer) bool {
753                         lu := l.URL()
754                         ru := r.URL()
755                         var luns, runs url.URL = *lu, *ru
756                         luns.Scheme = ""
757                         runs.Scheme = ""
758                         var ml missinggo.MultiLess
759                         ml.StrictNext(luns.String() == runs.String(), luns.String() < runs.String())
760                         ml.StrictNext(lu.String() == ru.String(), lu.String() < ru.String())
761                         return ml.Less()
762                 }).([]torrentTrackerAnnouncer) {
763                         fmt.Fprintf(tw, "    %q\t%v\n", ta.URL(), ta.statusLine())
764                 }
765                 tw.Flush()
766         }()
767
768         fmt.Fprintf(w, "DHT Announces: %d\n", t.numDHTAnnounces)
769
770         dumpStats(w, t.statsLocked())
771
772         fmt.Fprintf(w, "webseeds:\n")
773         t.writePeerStatuses(w, maps.Values(t.webSeeds))
774
775         peerConns := maps.Keys(t.conns)
776         // Peers without priorities first, then those with. I'm undecided about how to order peers
777         // without priorities.
778         sort.Slice(peerConns, func(li, ri int) bool {
779                 l := peerConns[li]
780                 r := peerConns[ri]
781                 ml := multiless.New()
782                 lpp := g.ResultFromTuple(l.peerPriority()).ToOption()
783                 rpp := g.ResultFromTuple(r.peerPriority()).ToOption()
784                 ml = ml.Bool(lpp.Ok, rpp.Ok)
785                 ml = ml.Uint32(rpp.Value, lpp.Value)
786                 return ml.Less()
787         })
788
789         fmt.Fprintf(w, "%v peer conns:\n", len(peerConns))
790         t.writePeerStatuses(w, g.SliceMap(peerConns, func(pc *PeerConn) *Peer {
791                 return &pc.Peer
792         }))
793 }
794
795 func (t *Torrent) writePeerStatuses(w io.Writer, peers []*Peer) {
796         var buf bytes.Buffer
797         for _, c := range peers {
798                 fmt.Fprintf(w, "- ")
799                 buf.Reset()
800                 c.writeStatus(&buf)
801                 w.Write(bytes.TrimRight(
802                         bytes.ReplaceAll(buf.Bytes(), []byte("\n"), []byte("\n  ")),
803                         " "))
804         }
805 }
806
807 func (t *Torrent) haveInfo() bool {
808         return t.info != nil
809 }
810
811 // Returns a run-time generated MetaInfo that includes the info bytes and
812 // announce-list as currently known to the client.
813 func (t *Torrent) newMetaInfo() metainfo.MetaInfo {
814         return metainfo.MetaInfo{
815                 CreationDate: time.Now().Unix(),
816                 Comment:      "dynamic metainfo from client",
817                 CreatedBy:    "go.torrent",
818                 AnnounceList: t.metainfo.UpvertedAnnounceList().Clone(),
819                 InfoBytes: func() []byte {
820                         if t.haveInfo() {
821                                 return t.metadataBytes
822                         } else {
823                                 return nil
824                         }
825                 }(),
826                 UrlList: func() []string {
827                         ret := make([]string, 0, len(t.webSeeds))
828                         for url := range t.webSeeds {
829                                 ret = append(ret, url)
830                         }
831                         return ret
832                 }(),
833         }
834 }
835
836 // Returns a count of bytes that are not complete in storage, and not pending being written to
837 // storage. This value is from the perspective of the download manager, and may not agree with the
838 // actual state in storage. If you want read data synchronously you should use a Reader. See
839 // https://github.com/anacrolix/torrent/issues/828.
840 func (t *Torrent) BytesMissing() (n int64) {
841         t.cl.rLock()
842         n = t.bytesMissingLocked()
843         t.cl.rUnlock()
844         return
845 }
846
847 func (t *Torrent) bytesMissingLocked() int64 {
848         return t.bytesLeft()
849 }
850
851 func iterFlipped(b *roaring.Bitmap, end uint64, cb func(uint32) bool) {
852         roaring.Flip(b, 0, end).Iterate(cb)
853 }
854
855 func (t *Torrent) bytesLeft() (left int64) {
856         iterFlipped(&t._completedPieces, uint64(t.numPieces()), func(x uint32) bool {
857                 p := t.piece(pieceIndex(x))
858                 left += int64(p.length() - p.numDirtyBytes())
859                 return true
860         })
861         return
862 }
863
864 // Bytes left to give in tracker announces.
865 func (t *Torrent) bytesLeftAnnounce() int64 {
866         if t.haveInfo() {
867                 return t.bytesLeft()
868         } else {
869                 return -1
870         }
871 }
872
873 func (t *Torrent) piecePartiallyDownloaded(piece pieceIndex) bool {
874         if t.pieceComplete(piece) {
875                 return false
876         }
877         if t.pieceAllDirty(piece) {
878                 return false
879         }
880         return t.pieces[piece].hasDirtyChunks()
881 }
882
883 func (t *Torrent) usualPieceSize() int {
884         return int(t.info.PieceLength)
885 }
886
887 func (t *Torrent) numPieces() pieceIndex {
888         return t.info.NumPieces()
889 }
890
891 func (t *Torrent) numPiecesCompleted() (num pieceIndex) {
892         return pieceIndex(t._completedPieces.GetCardinality())
893 }
894
895 func (t *Torrent) close(wg *sync.WaitGroup) (err error) {
896         if !t.closed.Set() {
897                 err = errors.New("already closed")
898                 return
899         }
900         for _, f := range t.onClose {
901                 f()
902         }
903         if t.storage != nil {
904                 wg.Add(1)
905                 go func() {
906                         defer wg.Done()
907                         t.storageLock.Lock()
908                         defer t.storageLock.Unlock()
909                         if f := t.storage.Close; f != nil {
910                                 err1 := f()
911                                 if err1 != nil {
912                                         t.logger.WithDefaultLevel(log.Warning).Printf("error closing storage: %v", err1)
913                                 }
914                         }
915                 }()
916         }
917         t.iterPeers(func(p *Peer) {
918                 p.close()
919         })
920         if t.storage != nil {
921                 t.deletePieceRequestOrder()
922         }
923         t.assertAllPiecesRelativeAvailabilityZero()
924         t.pex.Reset()
925         t.cl.event.Broadcast()
926         t.pieceStateChanges.Close()
927         t.updateWantPeersEvent()
928         return
929 }
930
931 func (t *Torrent) assertAllPiecesRelativeAvailabilityZero() {
932         for i := range t.pieces {
933                 p := t.piece(i)
934                 if p.relativeAvailability != 0 {
935                         panic(fmt.Sprintf("piece %v has relative availability %v", i, p.relativeAvailability))
936                 }
937         }
938 }
939
940 func (t *Torrent) requestOffset(r Request) int64 {
941         return torrentRequestOffset(t.length(), int64(t.usualPieceSize()), r)
942 }
943
944 // Return the request that would include the given offset into the torrent data. Returns !ok if
945 // there is no such request.
946 func (t *Torrent) offsetRequest(off int64) (req Request, ok bool) {
947         return torrentOffsetRequest(t.length(), t.info.PieceLength, int64(t.chunkSize), off)
948 }
949
950 func (t *Torrent) writeChunk(piece int, begin int64, data []byte) (err error) {
951         //defer perf.ScopeTimerErr(&err)()
952         n, err := t.pieces[piece].Storage().WriteAt(data, begin)
953         if err == nil && n != len(data) {
954                 err = io.ErrShortWrite
955         }
956         return err
957 }
958
959 func (t *Torrent) bitfield() (bf []bool) {
960         bf = make([]bool, t.numPieces())
961         t._completedPieces.Iterate(func(piece uint32) (again bool) {
962                 bf[piece] = true
963                 return true
964         })
965         return
966 }
967
968 func (t *Torrent) pieceNumChunks(piece pieceIndex) chunkIndexType {
969         return chunkIndexType((t.pieceLength(piece) + t.chunkSize - 1) / t.chunkSize)
970 }
971
972 func (t *Torrent) chunksPerRegularPiece() chunkIndexType {
973         return t._chunksPerRegularPiece
974 }
975
976 func (t *Torrent) numChunks() RequestIndex {
977         if t.numPieces() == 0 {
978                 return 0
979         }
980         return RequestIndex(t.numPieces()-1)*t.chunksPerRegularPiece() + t.pieceNumChunks(t.numPieces()-1)
981 }
982
983 func (t *Torrent) pendAllChunkSpecs(pieceIndex pieceIndex) {
984         t.dirtyChunks.RemoveRange(
985                 uint64(t.pieceRequestIndexOffset(pieceIndex)),
986                 uint64(t.pieceRequestIndexOffset(pieceIndex+1)))
987 }
988
989 func (t *Torrent) pieceLength(piece pieceIndex) pp.Integer {
990         if t.info.PieceLength == 0 {
991                 // There will be no variance amongst pieces. Only pain.
992                 return 0
993         }
994         if piece == t.numPieces()-1 {
995                 ret := pp.Integer(t.length() % t.info.PieceLength)
996                 if ret != 0 {
997                         return ret
998                 }
999         }
1000         return pp.Integer(t.info.PieceLength)
1001 }
1002
1003 func (t *Torrent) smartBanBlockCheckingWriter(piece pieceIndex) *blockCheckingWriter {
1004         return &blockCheckingWriter{
1005                 cache:        &t.smartBanCache,
1006                 requestIndex: t.pieceRequestIndexOffset(piece),
1007                 chunkSize:    t.chunkSize.Int(),
1008         }
1009 }
1010
1011 func (t *Torrent) hashPiece(piece pieceIndex) (
1012         ret metainfo.Hash,
1013         // These are peers that sent us blocks that differ from what we hash here.
1014         differingPeers map[bannableAddr]struct{},
1015         err error,
1016 ) {
1017         p := t.piece(piece)
1018         p.waitNoPendingWrites()
1019         storagePiece := t.pieces[piece].Storage()
1020
1021         // Does the backend want to do its own hashing?
1022         if i, ok := storagePiece.PieceImpl.(storage.SelfHashing); ok {
1023                 var sum metainfo.Hash
1024                 // log.Printf("A piece decided to self-hash: %d", piece)
1025                 sum, err = i.SelfHash()
1026                 missinggo.CopyExact(&ret, sum)
1027                 return
1028         }
1029
1030         hash := pieceHash.New()
1031         const logPieceContents = false
1032         smartBanWriter := t.smartBanBlockCheckingWriter(piece)
1033         writers := []io.Writer{hash, smartBanWriter}
1034         var examineBuf bytes.Buffer
1035         if logPieceContents {
1036                 writers = append(writers, &examineBuf)
1037         }
1038         _, err = storagePiece.WriteTo(io.MultiWriter(writers...))
1039         if logPieceContents {
1040                 t.logger.WithDefaultLevel(log.Debug).Printf("hashed %q with copy err %v", examineBuf.Bytes(), err)
1041         }
1042         smartBanWriter.Flush()
1043         differingPeers = smartBanWriter.badPeers
1044         missinggo.CopyExact(&ret, hash.Sum(nil))
1045         return
1046 }
1047
1048 func (t *Torrent) haveAnyPieces() bool {
1049         return !t._completedPieces.IsEmpty()
1050 }
1051
1052 func (t *Torrent) haveAllPieces() bool {
1053         if !t.haveInfo() {
1054                 return false
1055         }
1056         return t._completedPieces.GetCardinality() == bitmap.BitRange(t.numPieces())
1057 }
1058
1059 func (t *Torrent) havePiece(index pieceIndex) bool {
1060         return t.haveInfo() && t.pieceComplete(index)
1061 }
1062
1063 func (t *Torrent) maybeDropMutuallyCompletePeer(
1064         // I'm not sure about taking peer here, not all peer implementations actually drop. Maybe that's
1065         // okay?
1066         p *PeerConn,
1067 ) {
1068         if !t.cl.config.DropMutuallyCompletePeers {
1069                 return
1070         }
1071         if !t.haveAllPieces() {
1072                 return
1073         }
1074         if all, known := p.peerHasAllPieces(); !(known && all) {
1075                 return
1076         }
1077         if p.useful() {
1078                 return
1079         }
1080         p.logger.Levelf(log.Debug, "is mutually complete; dropping")
1081         p.drop()
1082 }
1083
1084 func (t *Torrent) haveChunk(r Request) (ret bool) {
1085         // defer func() {
1086         //      log.Println("have chunk", r, ret)
1087         // }()
1088         if !t.haveInfo() {
1089                 return false
1090         }
1091         if t.pieceComplete(pieceIndex(r.Index)) {
1092                 return true
1093         }
1094         p := &t.pieces[r.Index]
1095         return !p.pendingChunk(r.ChunkSpec, t.chunkSize)
1096 }
1097
1098 func chunkIndexFromChunkSpec(cs ChunkSpec, chunkSize pp.Integer) chunkIndexType {
1099         return chunkIndexType(cs.Begin / chunkSize)
1100 }
1101
1102 func (t *Torrent) wantPieceIndex(index pieceIndex) bool {
1103         return !t._pendingPieces.IsEmpty() && t._pendingPieces.Contains(uint32(index))
1104 }
1105
1106 // A pool of []*PeerConn, to reduce allocations in functions that need to index or sort Torrent
1107 // conns (which is a map).
1108 var peerConnSlices sync.Pool
1109
1110 func getPeerConnSlice(cap int) []*PeerConn {
1111         getInterface := peerConnSlices.Get()
1112         if getInterface == nil {
1113                 return make([]*PeerConn, 0, cap)
1114         } else {
1115                 return getInterface.([]*PeerConn)[:0]
1116         }
1117 }
1118
1119 // Calls the given function with a slice of unclosed conns. It uses a pool to reduce allocations as
1120 // this is a frequent occurrence.
1121 func (t *Torrent) withUnclosedConns(f func([]*PeerConn)) {
1122         sl := t.appendUnclosedConns(getPeerConnSlice(len(t.conns)))
1123         f(sl)
1124         peerConnSlices.Put(sl)
1125 }
1126
1127 func (t *Torrent) worstBadConnFromSlice(opts worseConnLensOpts, sl []*PeerConn) *PeerConn {
1128         wcs := worseConnSlice{conns: sl}
1129         wcs.initKeys(opts)
1130         heap.Init(&wcs)
1131         for wcs.Len() != 0 {
1132                 c := heap.Pop(&wcs).(*PeerConn)
1133                 if opts.incomingIsBad && !c.outgoing {
1134                         return c
1135                 }
1136                 if opts.outgoingIsBad && c.outgoing {
1137                         return c
1138                 }
1139                 if c._stats.ChunksReadWasted.Int64() >= 6 && c._stats.ChunksReadWasted.Int64() > c._stats.ChunksReadUseful.Int64() {
1140                         return c
1141                 }
1142                 // If the connection is in the worst half of the established
1143                 // connection quota and is older than a minute.
1144                 if wcs.Len() >= (t.maxEstablishedConns+1)/2 {
1145                         // Give connections 1 minute to prove themselves.
1146                         if time.Since(c.completedHandshake) > time.Minute {
1147                                 return c
1148                         }
1149                 }
1150         }
1151         return nil
1152 }
1153
1154 // The worst connection is one that hasn't been sent, or sent anything useful for the longest. A bad
1155 // connection is one that usually sends us unwanted pieces, or has been in the worse half of the
1156 // established connections for more than a minute. This is O(n log n). If there was a way to not
1157 // consider the position of a conn relative to the total number, it could be reduced to O(n).
1158 func (t *Torrent) worstBadConn(opts worseConnLensOpts) (ret *PeerConn) {
1159         t.withUnclosedConns(func(ucs []*PeerConn) {
1160                 ret = t.worstBadConnFromSlice(opts, ucs)
1161         })
1162         return
1163 }
1164
1165 type PieceStateChange struct {
1166         Index int
1167         PieceState
1168 }
1169
1170 func (t *Torrent) publishPieceStateChange(piece pieceIndex) {
1171         t.cl._mu.Defer(func() {
1172                 cur := t.pieceState(piece)
1173                 p := &t.pieces[piece]
1174                 if cur != p.publicPieceState {
1175                         p.publicPieceState = cur
1176                         t.pieceStateChanges.Publish(PieceStateChange{
1177                                 int(piece),
1178                                 cur,
1179                         })
1180                 }
1181         })
1182 }
1183
1184 func (t *Torrent) pieceNumPendingChunks(piece pieceIndex) pp.Integer {
1185         if t.pieceComplete(piece) {
1186                 return 0
1187         }
1188         return pp.Integer(t.pieceNumChunks(piece) - t.pieces[piece].numDirtyChunks())
1189 }
1190
1191 func (t *Torrent) pieceAllDirty(piece pieceIndex) bool {
1192         return t.pieces[piece].allChunksDirty()
1193 }
1194
1195 func (t *Torrent) readersChanged() {
1196         t.updateReaderPieces()
1197         t.updateAllPiecePriorities("Torrent.readersChanged")
1198 }
1199
1200 func (t *Torrent) updateReaderPieces() {
1201         t._readerNowPieces, t._readerReadaheadPieces = t.readerPiecePriorities()
1202 }
1203
1204 func (t *Torrent) readerPosChanged(from, to pieceRange) {
1205         if from == to {
1206                 return
1207         }
1208         t.updateReaderPieces()
1209         // Order the ranges, high and low.
1210         l, h := from, to
1211         if l.begin > h.begin {
1212                 l, h = h, l
1213         }
1214         if l.end < h.begin {
1215                 // Two distinct ranges.
1216                 t.updatePiecePriorities(l.begin, l.end, "Torrent.readerPosChanged")
1217                 t.updatePiecePriorities(h.begin, h.end, "Torrent.readerPosChanged")
1218         } else {
1219                 // Ranges overlap.
1220                 end := l.end
1221                 if h.end > end {
1222                         end = h.end
1223                 }
1224                 t.updatePiecePriorities(l.begin, end, "Torrent.readerPosChanged")
1225         }
1226 }
1227
1228 func (t *Torrent) maybeNewConns() {
1229         // Tickle the accept routine.
1230         t.cl.event.Broadcast()
1231         t.openNewConns()
1232 }
1233
1234 func (t *Torrent) onPiecePendingTriggers(piece pieceIndex, reason string) {
1235         if t._pendingPieces.Contains(uint32(piece)) {
1236                 t.iterPeers(func(c *Peer) {
1237                         // if c.requestState.Interested {
1238                         //      return
1239                         // }
1240                         if !c.isLowOnRequests() {
1241                                 return
1242                         }
1243                         if !c.peerHasPiece(piece) {
1244                                 return
1245                         }
1246                         if c.requestState.Interested && c.peerChoking && !c.peerAllowedFast.Contains(piece) {
1247                                 return
1248                         }
1249                         c.updateRequests(reason)
1250                 })
1251         }
1252         t.maybeNewConns()
1253         t.publishPieceStateChange(piece)
1254 }
1255
1256 func (t *Torrent) updatePiecePriorityNoTriggers(piece pieceIndex) (pendingChanged bool) {
1257         if !t.closed.IsSet() {
1258                 // It would be possible to filter on pure-priority changes here to avoid churning the piece
1259                 // request order.
1260                 t.updatePieceRequestOrderPiece(piece)
1261         }
1262         p := &t.pieces[piece]
1263         newPrio := p.uncachedPriority()
1264         // t.logger.Printf("torrent %p: piece %d: uncached priority: %v", t, piece, newPrio)
1265         if newPrio == PiecePriorityNone {
1266                 return t._pendingPieces.CheckedRemove(uint32(piece))
1267         } else {
1268                 return t._pendingPieces.CheckedAdd(uint32(piece))
1269         }
1270 }
1271
1272 func (t *Torrent) updatePiecePriority(piece pieceIndex, reason string) {
1273         if t.updatePiecePriorityNoTriggers(piece) && !t.disableTriggers {
1274                 t.onPiecePendingTriggers(piece, reason)
1275         }
1276         t.updatePieceRequestOrderPiece(piece)
1277 }
1278
1279 func (t *Torrent) updateAllPiecePriorities(reason string) {
1280         t.updatePiecePriorities(0, t.numPieces(), reason)
1281 }
1282
1283 // Update all piece priorities in one hit. This function should have the same
1284 // output as updatePiecePriority, but across all pieces.
1285 func (t *Torrent) updatePiecePriorities(begin, end pieceIndex, reason string) {
1286         for i := begin; i < end; i++ {
1287                 t.updatePiecePriority(i, reason)
1288         }
1289 }
1290
1291 // Returns the range of pieces [begin, end) that contains the extent of bytes.
1292 func (t *Torrent) byteRegionPieces(off, size int64) (begin, end pieceIndex) {
1293         if off >= t.length() {
1294                 return
1295         }
1296         if off < 0 {
1297                 size += off
1298                 off = 0
1299         }
1300         if size <= 0 {
1301                 return
1302         }
1303         begin = pieceIndex(off / t.info.PieceLength)
1304         end = pieceIndex((off + size + t.info.PieceLength - 1) / t.info.PieceLength)
1305         if end > pieceIndex(t.info.NumPieces()) {
1306                 end = pieceIndex(t.info.NumPieces())
1307         }
1308         return
1309 }
1310
1311 // Returns true if all iterations complete without breaking. Returns the read regions for all
1312 // readers. The reader regions should not be merged as some callers depend on this method to
1313 // enumerate readers.
1314 func (t *Torrent) forReaderOffsetPieces(f func(begin, end pieceIndex) (more bool)) (all bool) {
1315         for r := range t.readers {
1316                 p := r.pieces
1317                 if p.begin >= p.end {
1318                         continue
1319                 }
1320                 if !f(p.begin, p.end) {
1321                         return false
1322                 }
1323         }
1324         return true
1325 }
1326
1327 func (t *Torrent) piecePriority(piece pieceIndex) piecePriority {
1328         return t.piece(piece).uncachedPriority()
1329 }
1330
1331 func (t *Torrent) pendRequest(req RequestIndex) {
1332         t.piece(t.pieceIndexOfRequestIndex(req)).pendChunkIndex(req % t.chunksPerRegularPiece())
1333 }
1334
1335 func (t *Torrent) pieceCompletionChanged(piece pieceIndex, reason string) {
1336         t.cl.event.Broadcast()
1337         if t.pieceComplete(piece) {
1338                 t.onPieceCompleted(piece)
1339         } else {
1340                 t.onIncompletePiece(piece)
1341         }
1342         t.updatePiecePriority(piece, reason)
1343 }
1344
1345 func (t *Torrent) numReceivedConns() (ret int) {
1346         for c := range t.conns {
1347                 if c.Discovery == PeerSourceIncoming {
1348                         ret++
1349                 }
1350         }
1351         return
1352 }
1353
1354 func (t *Torrent) numOutgoingConns() (ret int) {
1355         for c := range t.conns {
1356                 if c.outgoing {
1357                         ret++
1358                 }
1359         }
1360         return
1361 }
1362
1363 func (t *Torrent) maxHalfOpen() int {
1364         // Note that if we somehow exceed the maximum established conns, we want
1365         // the negative value to have an effect.
1366         establishedHeadroom := int64(t.maxEstablishedConns - len(t.conns))
1367         extraIncoming := int64(t.numReceivedConns() - t.maxEstablishedConns/2)
1368         // We want to allow some experimentation with new peers, and to try to
1369         // upset an oversupply of received connections.
1370         return int(min(
1371                 max(5, extraIncoming)+establishedHeadroom,
1372                 int64(t.cl.config.HalfOpenConnsPerTorrent),
1373         ))
1374 }
1375
1376 func (t *Torrent) openNewConns() (initiated int) {
1377         defer t.updateWantPeersEvent()
1378         for t.peers.Len() != 0 {
1379                 if !t.wantOutgoingConns() {
1380                         return
1381                 }
1382                 if len(t.halfOpen) >= t.maxHalfOpen() {
1383                         return
1384                 }
1385                 if len(t.cl.dialers) == 0 {
1386                         return
1387                 }
1388                 if t.cl.numHalfOpen >= t.cl.config.TotalHalfOpenConns {
1389                         return
1390                 }
1391                 p := t.peers.PopMax()
1392                 opts := outgoingConnOpts{
1393                         peerInfo:                 p,
1394                         t:                        t,
1395                         requireRendezvous:        false,
1396                         skipHolepunchRendezvous:  false,
1397                         receivedHolepunchConnect: false,
1398                         HeaderObfuscationPolicy:  t.cl.config.HeaderObfuscationPolicy,
1399                 }
1400                 initiateConn(opts, false)
1401                 initiated++
1402         }
1403         return
1404 }
1405
1406 func (t *Torrent) updatePieceCompletion(piece pieceIndex) bool {
1407         p := t.piece(piece)
1408         uncached := t.pieceCompleteUncached(piece)
1409         cached := p.completion()
1410         changed := cached != uncached
1411         complete := uncached.Complete
1412         p.storageCompletionOk = uncached.Ok
1413         x := uint32(piece)
1414         if complete {
1415                 t._completedPieces.Add(x)
1416                 t.openNewConns()
1417         } else {
1418                 t._completedPieces.Remove(x)
1419         }
1420         p.t.updatePieceRequestOrderPiece(piece)
1421         t.updateComplete()
1422         if complete && len(p.dirtiers) != 0 {
1423                 t.logger.Printf("marked piece %v complete but still has dirtiers", piece)
1424         }
1425         if changed {
1426                 //slog.Debug(
1427                 //      "piece completion changed",
1428                 //      slog.Int("piece", piece),
1429                 //      slog.Any("from", cached),
1430                 //      slog.Any("to", uncached))
1431                 t.pieceCompletionChanged(piece, "Torrent.updatePieceCompletion")
1432         }
1433         return changed
1434 }
1435
1436 // Non-blocking read. Client lock is not required.
1437 func (t *Torrent) readAt(b []byte, off int64) (n int, err error) {
1438         for len(b) != 0 {
1439                 p := &t.pieces[off/t.info.PieceLength]
1440                 p.waitNoPendingWrites()
1441                 var n1 int
1442                 n1, err = p.Storage().ReadAt(b, off-p.Info().Offset())
1443                 if n1 == 0 {
1444                         break
1445                 }
1446                 off += int64(n1)
1447                 n += n1
1448                 b = b[n1:]
1449         }
1450         return
1451 }
1452
1453 // Returns an error if the metadata was completed, but couldn't be set for some reason. Blame it on
1454 // the last peer to contribute. TODO: Actually we shouldn't blame peers for failure to open storage
1455 // etc. Also we should probably cached metadata pieces per-Peer, to isolate failure appropriately.
1456 func (t *Torrent) maybeCompleteMetadata() error {
1457         if t.haveInfo() {
1458                 // Nothing to do.
1459                 return nil
1460         }
1461         if !t.haveAllMetadataPieces() {
1462                 // Don't have enough metadata pieces.
1463                 return nil
1464         }
1465         err := t.setInfoBytesLocked(t.metadataBytes)
1466         if err != nil {
1467                 t.invalidateMetadata()
1468                 return fmt.Errorf("error setting info bytes: %s", err)
1469         }
1470         if t.cl.config.Debug {
1471                 t.logger.Printf("%s: got metadata from peers", t)
1472         }
1473         return nil
1474 }
1475
1476 func (t *Torrent) readerPiecePriorities() (now, readahead bitmap.Bitmap) {
1477         t.forReaderOffsetPieces(func(begin, end pieceIndex) bool {
1478                 if end > begin {
1479                         now.Add(bitmap.BitIndex(begin))
1480                         readahead.AddRange(bitmap.BitRange(begin)+1, bitmap.BitRange(end))
1481                 }
1482                 return true
1483         })
1484         return
1485 }
1486
1487 func (t *Torrent) needData() bool {
1488         if t.closed.IsSet() {
1489                 return false
1490         }
1491         if !t.haveInfo() {
1492                 return true
1493         }
1494         return !t._pendingPieces.IsEmpty()
1495 }
1496
1497 func appendMissingStrings(old, new []string) (ret []string) {
1498         ret = old
1499 new:
1500         for _, n := range new {
1501                 for _, o := range old {
1502                         if o == n {
1503                                 continue new
1504                         }
1505                 }
1506                 ret = append(ret, n)
1507         }
1508         return
1509 }
1510
1511 func appendMissingTrackerTiers(existing [][]string, minNumTiers int) (ret [][]string) {
1512         ret = existing
1513         for minNumTiers > len(ret) {
1514                 ret = append(ret, nil)
1515         }
1516         return
1517 }
1518
1519 func (t *Torrent) addTrackers(announceList [][]string) {
1520         fullAnnounceList := &t.metainfo.AnnounceList
1521         t.metainfo.AnnounceList = appendMissingTrackerTiers(*fullAnnounceList, len(announceList))
1522         for tierIndex, trackerURLs := range announceList {
1523                 (*fullAnnounceList)[tierIndex] = appendMissingStrings((*fullAnnounceList)[tierIndex], trackerURLs)
1524         }
1525         t.startMissingTrackerScrapers()
1526         t.updateWantPeersEvent()
1527 }
1528
1529 // Don't call this before the info is available.
1530 func (t *Torrent) bytesCompleted() int64 {
1531         if !t.haveInfo() {
1532                 return 0
1533         }
1534         return t.length() - t.bytesLeft()
1535 }
1536
1537 func (t *Torrent) SetInfoBytes(b []byte) (err error) {
1538         t.cl.lock()
1539         defer t.cl.unlock()
1540         return t.setInfoBytesLocked(b)
1541 }
1542
1543 // Returns true if connection is removed from torrent.Conns.
1544 func (t *Torrent) deletePeerConn(c *PeerConn) (ret bool) {
1545         if !c.closed.IsSet() {
1546                 panic("connection is not closed")
1547                 // There are behaviours prevented by the closed state that will fail
1548                 // if the connection has been deleted.
1549         }
1550         _, ret = t.conns[c]
1551         delete(t.conns, c)
1552         // Avoid adding a drop event more than once. Probably we should track whether we've generated
1553         // the drop event against the PexConnState instead.
1554         if ret {
1555                 if !t.cl.config.DisablePEX {
1556                         t.pex.Drop(c)
1557                 }
1558         }
1559         torrent.Add("deleted connections", 1)
1560         c.deleteAllRequests("Torrent.deletePeerConn")
1561         t.assertPendingRequests()
1562         if t.numActivePeers() == 0 && len(t.connsWithAllPieces) != 0 {
1563                 panic(t.connsWithAllPieces)
1564         }
1565         return
1566 }
1567
1568 func (t *Torrent) decPeerPieceAvailability(p *Peer) {
1569         if t.deleteConnWithAllPieces(p) {
1570                 return
1571         }
1572         if !t.haveInfo() {
1573                 return
1574         }
1575         p.peerPieces().Iterate(func(i uint32) bool {
1576                 p.t.decPieceAvailability(pieceIndex(i))
1577                 return true
1578         })
1579 }
1580
1581 func (t *Torrent) assertPendingRequests() {
1582         if !check.Enabled {
1583                 return
1584         }
1585         // var actual pendingRequests
1586         // if t.haveInfo() {
1587         //      actual.m = make([]int, t.numChunks())
1588         // }
1589         // t.iterPeers(func(p *Peer) {
1590         //      p.requestState.Requests.Iterate(func(x uint32) bool {
1591         //              actual.Inc(x)
1592         //              return true
1593         //      })
1594         // })
1595         // diff := cmp.Diff(actual.m, t.pendingRequests.m)
1596         // if diff != "" {
1597         //      panic(diff)
1598         // }
1599 }
1600
1601 func (t *Torrent) dropConnection(c *PeerConn) {
1602         t.cl.event.Broadcast()
1603         c.close()
1604         if t.deletePeerConn(c) {
1605                 t.openNewConns()
1606         }
1607 }
1608
1609 // Peers as in contact information for dialing out.
1610 func (t *Torrent) wantPeers() bool {
1611         if t.closed.IsSet() {
1612                 return false
1613         }
1614         if t.peers.Len() > t.cl.config.TorrentPeersLowWater {
1615                 return false
1616         }
1617         return t.wantOutgoingConns()
1618 }
1619
1620 func (t *Torrent) updateWantPeersEvent() {
1621         if t.wantPeers() {
1622                 t.wantPeersEvent.Set()
1623         } else {
1624                 t.wantPeersEvent.Clear()
1625         }
1626 }
1627
1628 // Returns whether the client should make effort to seed the torrent.
1629 func (t *Torrent) seeding() bool {
1630         cl := t.cl
1631         if t.closed.IsSet() {
1632                 return false
1633         }
1634         if t.dataUploadDisallowed {
1635                 return false
1636         }
1637         if cl.config.NoUpload {
1638                 return false
1639         }
1640         if !cl.config.Seed {
1641                 return false
1642         }
1643         if cl.config.DisableAggressiveUpload && t.needData() {
1644                 return false
1645         }
1646         return true
1647 }
1648
1649 func (t *Torrent) onWebRtcConn(
1650         c datachannel.ReadWriteCloser,
1651         dcc webtorrent.DataChannelContext,
1652 ) {
1653         defer c.Close()
1654         netConn := webrtcNetConn{
1655                 ReadWriteCloser:    c,
1656                 DataChannelContext: dcc,
1657         }
1658         peerRemoteAddr := netConn.RemoteAddr()
1659         //t.logger.Levelf(log.Critical, "onWebRtcConn remote addr: %v", peerRemoteAddr)
1660         if t.cl.badPeerAddr(peerRemoteAddr) {
1661                 return
1662         }
1663         localAddrIpPort := missinggo.IpPortFromNetAddr(netConn.LocalAddr())
1664         pc, err := t.cl.initiateProtocolHandshakes(
1665                 context.Background(),
1666                 netConn,
1667                 t,
1668                 false,
1669                 newConnectionOpts{
1670                         outgoing:        dcc.LocalOffered,
1671                         remoteAddr:      peerRemoteAddr,
1672                         localPublicAddr: localAddrIpPort,
1673                         network:         webrtcNetwork,
1674                         connString:      fmt.Sprintf("webrtc offer_id %x: %v", dcc.OfferId, regularNetConnPeerConnConnString(netConn)),
1675                 },
1676         )
1677         if err != nil {
1678                 t.logger.WithDefaultLevel(log.Error).Printf("error in handshaking webrtc connection: %v", err)
1679                 return
1680         }
1681         if dcc.LocalOffered {
1682                 pc.Discovery = PeerSourceTracker
1683         } else {
1684                 pc.Discovery = PeerSourceIncoming
1685         }
1686         pc.conn.SetWriteDeadline(time.Time{})
1687         t.cl.lock()
1688         defer t.cl.unlock()
1689         err = t.runHandshookConn(pc)
1690         if err != nil {
1691                 t.logger.WithDefaultLevel(log.Debug).Printf("error running handshook webrtc conn: %v", err)
1692         }
1693 }
1694
1695 func (t *Torrent) logRunHandshookConn(pc *PeerConn, logAll bool, level log.Level) {
1696         err := t.runHandshookConn(pc)
1697         if err != nil || logAll {
1698                 t.logger.WithDefaultLevel(level).Levelf(log.ErrorLevel(err), "error running handshook conn: %v", err)
1699         }
1700 }
1701
1702 func (t *Torrent) runHandshookConnLoggingErr(pc *PeerConn) {
1703         t.logRunHandshookConn(pc, false, log.Debug)
1704 }
1705
1706 func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer {
1707         wtc, release := t.cl.websocketTrackers.Get(u.String(), t.infoHash)
1708         // This needs to run before the Torrent is dropped from the Client, to prevent a new webtorrent.TrackerClient for
1709         // the same info hash before the old one is cleaned up.
1710         t.onClose = append(t.onClose, release)
1711         wst := websocketTrackerStatus{u, wtc}
1712         go func() {
1713                 err := wtc.Announce(tracker.Started, t.infoHash)
1714                 if err != nil {
1715                         t.logger.WithDefaultLevel(log.Warning).Printf(
1716                                 "error in initial announce to %q: %v",
1717                                 u.String(), err,
1718                         )
1719                 }
1720         }()
1721         return wst
1722 }
1723
1724 func (t *Torrent) startScrapingTracker(_url string) {
1725         if _url == "" {
1726                 return
1727         }
1728         u, err := url.Parse(_url)
1729         if err != nil {
1730                 // URLs with a leading '*' appear to be a uTorrent convention to disable trackers.
1731                 if _url[0] != '*' {
1732                         t.logger.Levelf(log.Warning, "error parsing tracker url: %v", err)
1733                 }
1734                 return
1735         }
1736         if u.Scheme == "udp" {
1737                 u.Scheme = "udp4"
1738                 t.startScrapingTracker(u.String())
1739                 u.Scheme = "udp6"
1740                 t.startScrapingTracker(u.String())
1741                 return
1742         }
1743         if _, ok := t.trackerAnnouncers[_url]; ok {
1744                 return
1745         }
1746         sl := func() torrentTrackerAnnouncer {
1747                 switch u.Scheme {
1748                 case "ws", "wss":
1749                         if t.cl.config.DisableWebtorrent {
1750                                 return nil
1751                         }
1752                         return t.startWebsocketAnnouncer(*u)
1753                 case "udp4":
1754                         if t.cl.config.DisableIPv4Peers || t.cl.config.DisableIPv4 {
1755                                 return nil
1756                         }
1757                 case "udp6":
1758                         if t.cl.config.DisableIPv6 {
1759                                 return nil
1760                         }
1761                 }
1762                 newAnnouncer := &trackerScraper{
1763                         u:               *u,
1764                         t:               t,
1765                         lookupTrackerIp: t.cl.config.LookupTrackerIp,
1766                 }
1767                 go newAnnouncer.Run()
1768                 return newAnnouncer
1769         }()
1770         if sl == nil {
1771                 return
1772         }
1773         if t.trackerAnnouncers == nil {
1774                 t.trackerAnnouncers = make(map[string]torrentTrackerAnnouncer)
1775         }
1776         t.trackerAnnouncers[_url] = sl
1777 }
1778
1779 // Adds and starts tracker scrapers for tracker URLs that aren't already
1780 // running.
1781 func (t *Torrent) startMissingTrackerScrapers() {
1782         if t.cl.config.DisableTrackers {
1783                 return
1784         }
1785         t.startScrapingTracker(t.metainfo.Announce)
1786         for _, tier := range t.metainfo.AnnounceList {
1787                 for _, url := range tier {
1788                         t.startScrapingTracker(url)
1789                 }
1790         }
1791 }
1792
1793 // Returns an AnnounceRequest with fields filled out to defaults and current
1794 // values.
1795 func (t *Torrent) announceRequest(event tracker.AnnounceEvent) tracker.AnnounceRequest {
1796         // Note that IPAddress is not set. It's set for UDP inside the tracker code, since it's
1797         // dependent on the network in use.
1798         return tracker.AnnounceRequest{
1799                 Event: event,
1800                 NumWant: func() int32 {
1801                         if t.wantPeers() && len(t.cl.dialers) > 0 {
1802                                 return 200 // Win has UDP packet limit. See: https://github.com/anacrolix/torrent/issues/764
1803                         } else {
1804                                 return 0
1805                         }
1806                 }(),
1807                 Port:     uint16(t.cl.incomingPeerPort()),
1808                 PeerId:   t.cl.peerID,
1809                 InfoHash: t.infoHash,
1810                 Key:      t.cl.announceKey(),
1811
1812                 // The following are vaguely described in BEP 3.
1813
1814                 Left:     t.bytesLeftAnnounce(),
1815                 Uploaded: t.stats.BytesWrittenData.Int64(),
1816                 // There's no mention of wasted or unwanted download in the BEP.
1817                 Downloaded: t.stats.BytesReadUsefulData.Int64(),
1818         }
1819 }
1820
1821 // Adds peers revealed in an announce until the announce ends, or we have
1822 // enough peers.
1823 func (t *Torrent) consumeDhtAnnouncePeers(pvs <-chan dht.PeersValues) {
1824         cl := t.cl
1825         for v := range pvs {
1826                 cl.lock()
1827                 added := 0
1828                 for _, cp := range v.Peers {
1829                         if cp.Port == 0 {
1830                                 // Can't do anything with this.
1831                                 continue
1832                         }
1833                         if t.addPeer(PeerInfo{
1834                                 Addr:   ipPortAddr{cp.IP, cp.Port},
1835                                 Source: PeerSourceDhtGetPeers,
1836                         }) {
1837                                 added++
1838                         }
1839                 }
1840                 cl.unlock()
1841                 // if added != 0 {
1842                 //      log.Printf("added %v peers from dht for %v", added, t.InfoHash().HexString())
1843                 // }
1844         }
1845 }
1846
1847 // Announce using the provided DHT server. Peers are consumed automatically. done is closed when the
1848 // announce ends. stop will force the announce to end.
1849 func (t *Torrent) AnnounceToDht(s DhtServer) (done <-chan struct{}, stop func(), err error) {
1850         ps, err := s.Announce(t.infoHash, t.cl.incomingPeerPort(), true)
1851         if err != nil {
1852                 return
1853         }
1854         _done := make(chan struct{})
1855         done = _done
1856         stop = ps.Close
1857         go func() {
1858                 t.consumeDhtAnnouncePeers(ps.Peers())
1859                 close(_done)
1860         }()
1861         return
1862 }
1863
1864 func (t *Torrent) timeboxedAnnounceToDht(s DhtServer) error {
1865         _, stop, err := t.AnnounceToDht(s)
1866         if err != nil {
1867                 return err
1868         }
1869         select {
1870         case <-t.closed.Done():
1871         case <-time.After(5 * time.Minute):
1872         }
1873         stop()
1874         return nil
1875 }
1876
1877 func (t *Torrent) dhtAnnouncer(s DhtServer) {
1878         cl := t.cl
1879         cl.lock()
1880         defer cl.unlock()
1881         for {
1882                 for {
1883                         if t.closed.IsSet() {
1884                                 return
1885                         }
1886                         // We're also announcing ourselves as a listener, so we don't just want peer addresses.
1887                         // TODO: We can include the announce_peer step depending on whether we can receive
1888                         // inbound connections. We should probably only announce once every 15 mins too.
1889                         if !t.wantAnyConns() {
1890                                 goto wait
1891                         }
1892                         // TODO: Determine if there's a listener on the port we're announcing.
1893                         if len(cl.dialers) == 0 && len(cl.listeners) == 0 {
1894                                 goto wait
1895                         }
1896                         break
1897                 wait:
1898                         cl.event.Wait()
1899                 }
1900                 func() {
1901                         t.numDHTAnnounces++
1902                         cl.unlock()
1903                         defer cl.lock()
1904                         err := t.timeboxedAnnounceToDht(s)
1905                         if err != nil {
1906                                 t.logger.WithDefaultLevel(log.Warning).Printf("error announcing %q to DHT: %s", t, err)
1907                         }
1908                 }()
1909         }
1910 }
1911
1912 func (t *Torrent) addPeers(peers []PeerInfo) (added int) {
1913         for _, p := range peers {
1914                 if t.addPeer(p) {
1915                         added++
1916                 }
1917         }
1918         return
1919 }
1920
1921 // The returned TorrentStats may require alignment in memory. See
1922 // https://github.com/anacrolix/torrent/issues/383.
1923 func (t *Torrent) Stats() TorrentStats {
1924         t.cl.rLock()
1925         defer t.cl.rUnlock()
1926         return t.statsLocked()
1927 }
1928
1929 func (t *Torrent) statsLocked() (ret TorrentStats) {
1930         ret.ActivePeers = len(t.conns)
1931         ret.HalfOpenPeers = len(t.halfOpen)
1932         ret.PendingPeers = t.peers.Len()
1933         ret.TotalPeers = t.numTotalPeers()
1934         ret.ConnectedSeeders = 0
1935         for c := range t.conns {
1936                 if all, ok := c.peerHasAllPieces(); all && ok {
1937                         ret.ConnectedSeeders++
1938                 }
1939         }
1940         ret.ConnStats = t.stats.Copy()
1941         ret.PiecesComplete = t.numPiecesCompleted()
1942         return
1943 }
1944
1945 // The total number of peers in the torrent.
1946 func (t *Torrent) numTotalPeers() int {
1947         peers := make(map[string]struct{})
1948         for conn := range t.conns {
1949                 ra := conn.conn.RemoteAddr()
1950                 if ra == nil {
1951                         // It's been closed and doesn't support RemoteAddr.
1952                         continue
1953                 }
1954                 peers[ra.String()] = struct{}{}
1955         }
1956         for addr := range t.halfOpen {
1957                 peers[addr] = struct{}{}
1958         }
1959         t.peers.Each(func(peer PeerInfo) {
1960                 peers[peer.Addr.String()] = struct{}{}
1961         })
1962         return len(peers)
1963 }
1964
1965 // Reconcile bytes transferred before connection was associated with a
1966 // torrent.
1967 func (t *Torrent) reconcileHandshakeStats(c *Peer) {
1968         if c._stats != (ConnStats{
1969                 // Handshakes should only increment these fields:
1970                 BytesWritten: c._stats.BytesWritten,
1971                 BytesRead:    c._stats.BytesRead,
1972         }) {
1973                 panic("bad stats")
1974         }
1975         c.postHandshakeStats(func(cs *ConnStats) {
1976                 cs.BytesRead.Add(c._stats.BytesRead.Int64())
1977                 cs.BytesWritten.Add(c._stats.BytesWritten.Int64())
1978         })
1979         c.reconciledHandshakeStats = true
1980 }
1981
1982 // Returns true if the connection is added.
1983 func (t *Torrent) addPeerConn(c *PeerConn) (err error) {
1984         defer func() {
1985                 if err == nil {
1986                         torrent.Add("added connections", 1)
1987                 }
1988         }()
1989         if t.closed.IsSet() {
1990                 return errors.New("torrent closed")
1991         }
1992         for c0 := range t.conns {
1993                 if c.PeerID != c0.PeerID {
1994                         continue
1995                 }
1996                 if !t.cl.config.DropDuplicatePeerIds {
1997                         continue
1998                 }
1999                 if c.hasPreferredNetworkOver(c0) {
2000                         c0.close()
2001                         t.deletePeerConn(c0)
2002                 } else {
2003                         return errors.New("existing connection preferred")
2004                 }
2005         }
2006         if len(t.conns) >= t.maxEstablishedConns {
2007                 numOutgoing := t.numOutgoingConns()
2008                 numIncoming := len(t.conns) - numOutgoing
2009                 c := t.worstBadConn(worseConnLensOpts{
2010                         // We've already established that we have too many connections at this point, so we just
2011                         // need to match what kind we have too many of vs. what we're trying to add now.
2012                         incomingIsBad: (numIncoming-numOutgoing > 1) && c.outgoing,
2013                         outgoingIsBad: (numOutgoing-numIncoming > 1) && !c.outgoing,
2014                 })
2015                 if c == nil {
2016                         return errors.New("don't want conn")
2017                 }
2018                 c.close()
2019                 t.deletePeerConn(c)
2020         }
2021         if len(t.conns) >= t.maxEstablishedConns {
2022                 panic(len(t.conns))
2023         }
2024         t.conns[c] = struct{}{}
2025         t.cl.event.Broadcast()
2026         // We'll never receive the "p" extended handshake parameter.
2027         if !t.cl.config.DisablePEX && !c.PeerExtensionBytes.SupportsExtended() {
2028                 t.pex.Add(c)
2029         }
2030         return nil
2031 }
2032
2033 func (t *Torrent) newConnsAllowed() bool {
2034         if !t.networkingEnabled.Bool() {
2035                 return false
2036         }
2037         if t.closed.IsSet() {
2038                 return false
2039         }
2040         if !t.needData() && (!t.seeding() || !t.haveAnyPieces()) {
2041                 return false
2042         }
2043         return true
2044 }
2045
2046 func (t *Torrent) wantAnyConns() bool {
2047         if !t.networkingEnabled.Bool() {
2048                 return false
2049         }
2050         if t.closed.IsSet() {
2051                 return false
2052         }
2053         if !t.needData() && (!t.seeding() || !t.haveAnyPieces()) {
2054                 return false
2055         }
2056         return len(t.conns) < t.maxEstablishedConns
2057 }
2058
2059 func (t *Torrent) wantOutgoingConns() bool {
2060         if !t.newConnsAllowed() {
2061                 return false
2062         }
2063         if len(t.conns) < t.maxEstablishedConns {
2064                 return true
2065         }
2066         numIncomingConns := len(t.conns) - t.numOutgoingConns()
2067         return t.worstBadConn(worseConnLensOpts{
2068                 incomingIsBad: numIncomingConns-t.numOutgoingConns() > 1,
2069                 outgoingIsBad: false,
2070         }) != nil
2071 }
2072
2073 func (t *Torrent) wantIncomingConns() bool {
2074         if !t.newConnsAllowed() {
2075                 return false
2076         }
2077         if len(t.conns) < t.maxEstablishedConns {
2078                 return true
2079         }
2080         numIncomingConns := len(t.conns) - t.numOutgoingConns()
2081         return t.worstBadConn(worseConnLensOpts{
2082                 incomingIsBad: false,
2083                 outgoingIsBad: t.numOutgoingConns()-numIncomingConns > 1,
2084         }) != nil
2085 }
2086
2087 func (t *Torrent) SetMaxEstablishedConns(max int) (oldMax int) {
2088         t.cl.lock()
2089         defer t.cl.unlock()
2090         oldMax = t.maxEstablishedConns
2091         t.maxEstablishedConns = max
2092         wcs := worseConnSlice{
2093                 conns: t.appendConns(nil, func(*PeerConn) bool {
2094                         return true
2095                 }),
2096         }
2097         wcs.initKeys(worseConnLensOpts{})
2098         heap.Init(&wcs)
2099         for len(t.conns) > t.maxEstablishedConns && wcs.Len() > 0 {
2100                 t.dropConnection(heap.Pop(&wcs).(*PeerConn))
2101         }
2102         t.openNewConns()
2103         return oldMax
2104 }
2105
2106 func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) {
2107         t.logger.LazyLog(log.Debug, func() log.Msg {
2108                 return log.Fstr("hashed piece %d (passed=%t)", piece, passed)
2109         })
2110         p := t.piece(piece)
2111         p.numVerifies++
2112         t.cl.event.Broadcast()
2113         if t.closed.IsSet() {
2114                 return
2115         }
2116
2117         // Don't score the first time a piece is hashed, it could be an initial check.
2118         if p.storageCompletionOk {
2119                 if passed {
2120                         pieceHashedCorrect.Add(1)
2121                 } else {
2122                         log.Fmsg(
2123                                 "piece %d failed hash: %d connections contributed", piece, len(p.dirtiers),
2124                         ).AddValues(t, p).LogLevel(
2125
2126                                 log.Debug, t.logger)
2127
2128                         pieceHashedNotCorrect.Add(1)
2129                 }
2130         }
2131
2132         p.marking = true
2133         t.publishPieceStateChange(piece)
2134         defer func() {
2135                 p.marking = false
2136                 t.publishPieceStateChange(piece)
2137         }()
2138
2139         if passed {
2140                 if len(p.dirtiers) != 0 {
2141                         // Don't increment stats above connection-level for every involved connection.
2142                         t.allStats((*ConnStats).incrementPiecesDirtiedGood)
2143                 }
2144                 for c := range p.dirtiers {
2145                         c._stats.incrementPiecesDirtiedGood()
2146                 }
2147                 t.clearPieceTouchers(piece)
2148                 hasDirty := p.hasDirtyChunks()
2149                 t.cl.unlock()
2150                 if hasDirty {
2151                         p.Flush() // You can be synchronous here!
2152                 }
2153                 err := p.Storage().MarkComplete()
2154                 if err != nil {
2155                         t.logger.Levelf(log.Warning, "%T: error marking piece complete %d: %s", t.storage, piece, err)
2156                 }
2157                 t.cl.lock()
2158
2159                 if t.closed.IsSet() {
2160                         return
2161                 }
2162                 t.pendAllChunkSpecs(piece)
2163         } else {
2164                 if len(p.dirtiers) != 0 && p.allChunksDirty() && hashIoErr == nil {
2165                         // Peers contributed to all the data for this piece hash failure, and the failure was
2166                         // not due to errors in the storage (such as data being dropped in a cache).
2167
2168                         // Increment Torrent and above stats, and then specific connections.
2169                         t.allStats((*ConnStats).incrementPiecesDirtiedBad)
2170                         for c := range p.dirtiers {
2171                                 // Y u do dis peer?!
2172                                 c.stats().incrementPiecesDirtiedBad()
2173                         }
2174
2175                         bannableTouchers := make([]*Peer, 0, len(p.dirtiers))
2176                         for c := range p.dirtiers {
2177                                 if !c.trusted {
2178                                         bannableTouchers = append(bannableTouchers, c)
2179                                 }
2180                         }
2181                         t.clearPieceTouchers(piece)
2182                         slices.Sort(bannableTouchers, connLessTrusted)
2183
2184                         if t.cl.config.Debug {
2185                                 t.logger.Printf(
2186                                         "bannable conns by trust for piece %d: %v",
2187                                         piece,
2188                                         func() (ret []connectionTrust) {
2189                                                 for _, c := range bannableTouchers {
2190                                                         ret = append(ret, c.trust())
2191                                                 }
2192                                                 return
2193                                         }(),
2194                                 )
2195                         }
2196
2197                         if len(bannableTouchers) >= 1 {
2198                                 c := bannableTouchers[0]
2199                                 if len(bannableTouchers) != 1 {
2200                                         t.logger.Levelf(log.Debug, "would have banned %v for touching piece %v after failed piece check", c.remoteIp(), piece)
2201                                 } else {
2202                                         // Turns out it's still useful to ban peers like this because if there's only a
2203                                         // single peer for a piece, and we never progress that piece to completion, we
2204                                         // will never smart-ban them. Discovered in
2205                                         // https://github.com/anacrolix/torrent/issues/715.
2206                                         t.logger.Levelf(log.Warning, "banning %v for being sole dirtier of piece %v after failed piece check", c, piece)
2207                                         c.ban()
2208                                 }
2209                         }
2210                 }
2211                 t.onIncompletePiece(piece)
2212                 p.Storage().MarkNotComplete()
2213         }
2214         t.updatePieceCompletion(piece)
2215 }
2216
2217 func (t *Torrent) cancelRequestsForPiece(piece pieceIndex) {
2218         start := t.pieceRequestIndexOffset(piece)
2219         end := start + t.pieceNumChunks(piece)
2220         for ri := start; ri < end; ri++ {
2221                 t.cancelRequest(ri)
2222         }
2223 }
2224
2225 func (t *Torrent) onPieceCompleted(piece pieceIndex) {
2226         t.pendAllChunkSpecs(piece)
2227         t.cancelRequestsForPiece(piece)
2228         t.piece(piece).readerCond.Broadcast()
2229         for conn := range t.conns {
2230                 conn.have(piece)
2231                 t.maybeDropMutuallyCompletePeer(conn)
2232         }
2233 }
2234
2235 // Called when a piece is found to be not complete.
2236 func (t *Torrent) onIncompletePiece(piece pieceIndex) {
2237         if t.pieceAllDirty(piece) {
2238                 t.pendAllChunkSpecs(piece)
2239         }
2240         if !t.wantPieceIndex(piece) {
2241                 // t.logger.Printf("piece %d incomplete and unwanted", piece)
2242                 return
2243         }
2244         // We could drop any connections that we told we have a piece that we
2245         // don't here. But there's a test failure, and it seems clients don't care
2246         // if you request pieces that you already claim to have. Pruning bad
2247         // connections might just remove any connections that aren't treating us
2248         // favourably anyway.
2249
2250         // for c := range t.conns {
2251         //      if c.sentHave(piece) {
2252         //              c.drop()
2253         //      }
2254         // }
2255         t.iterPeers(func(conn *Peer) {
2256                 if conn.peerHasPiece(piece) {
2257                         conn.updateRequests("piece incomplete")
2258                 }
2259         })
2260 }
2261
2262 func (t *Torrent) tryCreateMorePieceHashers() {
2263         for !t.closed.IsSet() && t.activePieceHashes < t.cl.config.PieceHashersPerTorrent && t.tryCreatePieceHasher() {
2264         }
2265 }
2266
2267 func (t *Torrent) tryCreatePieceHasher() bool {
2268         if t.storage == nil {
2269                 return false
2270         }
2271         pi, ok := t.getPieceToHash()
2272         if !ok {
2273                 return false
2274         }
2275         p := t.piece(pi)
2276         t.piecesQueuedForHash.Remove(bitmap.BitIndex(pi))
2277         p.hashing = true
2278         t.publishPieceStateChange(pi)
2279         t.updatePiecePriority(pi, "Torrent.tryCreatePieceHasher")
2280         t.storageLock.RLock()
2281         t.activePieceHashes++
2282         go t.pieceHasher(pi)
2283         return true
2284 }
2285
2286 func (t *Torrent) getPieceToHash() (ret pieceIndex, ok bool) {
2287         t.piecesQueuedForHash.IterTyped(func(i pieceIndex) bool {
2288                 if t.piece(i).hashing {
2289                         return true
2290                 }
2291                 ret = i
2292                 ok = true
2293                 return false
2294         })
2295         return
2296 }
2297
2298 func (t *Torrent) dropBannedPeers() {
2299         t.iterPeers(func(p *Peer) {
2300                 remoteIp := p.remoteIp()
2301                 if remoteIp == nil {
2302                         if p.bannableAddr.Ok {
2303                                 t.logger.WithDefaultLevel(log.Debug).Printf("can't get remote ip for peer %v", p)
2304                         }
2305                         return
2306                 }
2307                 netipAddr := netip.MustParseAddr(remoteIp.String())
2308                 if Some(netipAddr) != p.bannableAddr {
2309                         t.logger.WithDefaultLevel(log.Debug).Printf(
2310                                 "peer remote ip does not match its bannable addr [peer=%v, remote ip=%v, bannable addr=%v]",
2311                                 p, remoteIp, p.bannableAddr)
2312                 }
2313                 if _, ok := t.cl.badPeerIPs[netipAddr]; ok {
2314                         // Should this be a close?
2315                         p.drop()
2316                         t.logger.WithDefaultLevel(log.Debug).Printf("dropped %v for banned remote IP %v", p, netipAddr)
2317                 }
2318         })
2319 }
2320
2321 func (t *Torrent) pieceHasher(index pieceIndex) {
2322         p := t.piece(index)
2323         sum, failedPeers, copyErr := t.hashPiece(index)
2324         correct := sum == *p.hash
2325         switch copyErr {
2326         case nil, io.EOF:
2327         default:
2328                 log.Fmsg("piece %v (%s) hash failure copy error: %v", p, p.hash.HexString(), copyErr).Log(t.logger)
2329         }
2330         t.storageLock.RUnlock()
2331         t.cl.lock()
2332         defer t.cl.unlock()
2333         if correct {
2334                 for peer := range failedPeers {
2335                         t.cl.banPeerIP(peer.AsSlice())
2336                         t.logger.WithDefaultLevel(log.Debug).Printf("smart banned %v for piece %v", peer, index)
2337                 }
2338                 t.dropBannedPeers()
2339                 for ri := t.pieceRequestIndexOffset(index); ri < t.pieceRequestIndexOffset(index+1); ri++ {
2340                         t.smartBanCache.ForgetBlock(ri)
2341                 }
2342         }
2343         p.hashing = false
2344         t.pieceHashed(index, correct, copyErr)
2345         t.updatePiecePriority(index, "Torrent.pieceHasher")
2346         t.activePieceHashes--
2347         t.tryCreateMorePieceHashers()
2348 }
2349
2350 // Return the connections that touched a piece, and clear the entries while doing it.
2351 func (t *Torrent) clearPieceTouchers(pi pieceIndex) {
2352         p := t.piece(pi)
2353         for c := range p.dirtiers {
2354                 delete(c.peerTouchedPieces, pi)
2355                 delete(p.dirtiers, c)
2356         }
2357 }
2358
2359 func (t *Torrent) peersAsSlice() (ret []*Peer) {
2360         t.iterPeers(func(p *Peer) {
2361                 ret = append(ret, p)
2362         })
2363         return
2364 }
2365
2366 func (t *Torrent) queuePieceCheck(pieceIndex pieceIndex) {
2367         piece := t.piece(pieceIndex)
2368         if piece.queuedForHash() {
2369                 return
2370         }
2371         t.piecesQueuedForHash.Add(bitmap.BitIndex(pieceIndex))
2372         t.publishPieceStateChange(pieceIndex)
2373         t.updatePiecePriority(pieceIndex, "Torrent.queuePieceCheck")
2374         t.tryCreateMorePieceHashers()
2375 }
2376
2377 // Forces all the pieces to be re-hashed. See also Piece.VerifyData. This should not be called
2378 // before the Info is available.
2379 func (t *Torrent) VerifyData() {
2380         for i := pieceIndex(0); i < t.NumPieces(); i++ {
2381                 t.Piece(i).VerifyData()
2382         }
2383 }
2384
2385 func (t *Torrent) connectingToPeerAddr(addrStr string) bool {
2386         return len(t.halfOpen[addrStr]) != 0
2387 }
2388
2389 func (t *Torrent) hasPeerConnForAddr(x PeerRemoteAddr) bool {
2390         addrStr := x.String()
2391         for c := range t.conns {
2392                 ra := c.RemoteAddr
2393                 if ra.String() == addrStr {
2394                         return true
2395                 }
2396         }
2397         return false
2398 }
2399
2400 func (t *Torrent) getHalfOpenPath(
2401         addrStr string,
2402         attemptKey outgoingConnAttemptKey,
2403 ) nestedmaps.Path[*PeerInfo] {
2404         return nestedmaps.Next(nestedmaps.Next(nestedmaps.Begin(&t.halfOpen), addrStr), attemptKey)
2405 }
2406
2407 func (t *Torrent) addHalfOpen(addrStr string, attemptKey *PeerInfo) {
2408         path := t.getHalfOpenPath(addrStr, attemptKey)
2409         if path.Exists() {
2410                 panic("should be unique")
2411         }
2412         path.Set(attemptKey)
2413         t.cl.numHalfOpen++
2414 }
2415
2416 // Start the process of connecting to the given peer for the given torrent if appropriate. I'm not
2417 // sure all the PeerInfo fields are being used.
2418 func initiateConn(
2419         opts outgoingConnOpts,
2420         ignoreLimits bool,
2421 ) {
2422         t := opts.t
2423         peer := opts.peerInfo
2424         if peer.Id == t.cl.peerID {
2425                 return
2426         }
2427         if t.cl.badPeerAddr(peer.Addr) && !peer.Trusted {
2428                 return
2429         }
2430         addr := peer.Addr
2431         addrStr := addr.String()
2432         if !ignoreLimits {
2433                 if t.connectingToPeerAddr(addrStr) {
2434                         return
2435                 }
2436         }
2437         if t.hasPeerConnForAddr(addr) {
2438                 return
2439         }
2440         attemptKey := &peer
2441         t.addHalfOpen(addrStr, attemptKey)
2442         go t.cl.outgoingConnection(
2443                 opts,
2444                 attemptKey,
2445         )
2446 }
2447
2448 // Adds a trusted, pending peer for each of the given Client's addresses. Typically used in tests to
2449 // quickly make one Client visible to the Torrent of another Client.
2450 func (t *Torrent) AddClientPeer(cl *Client) int {
2451         return t.AddPeers(func() (ps []PeerInfo) {
2452                 for _, la := range cl.ListenAddrs() {
2453                         ps = append(ps, PeerInfo{
2454                                 Addr:    la,
2455                                 Trusted: true,
2456                         })
2457                 }
2458                 return
2459         }())
2460 }
2461
2462 // All stats that include this Torrent. Useful when we want to increment ConnStats but not for every
2463 // connection.
2464 func (t *Torrent) allStats(f func(*ConnStats)) {
2465         f(&t.stats)
2466         f(&t.cl.connStats)
2467 }
2468
2469 func (t *Torrent) hashingPiece(i pieceIndex) bool {
2470         return t.pieces[i].hashing
2471 }
2472
2473 func (t *Torrent) pieceQueuedForHash(i pieceIndex) bool {
2474         return t.piecesQueuedForHash.Get(bitmap.BitIndex(i))
2475 }
2476
2477 func (t *Torrent) dialTimeout() time.Duration {
2478         return reducedDialTimeout(t.cl.config.MinDialTimeout, t.cl.config.NominalDialTimeout, t.cl.config.HalfOpenConnsPerTorrent, t.peers.Len())
2479 }
2480
2481 func (t *Torrent) piece(i int) *Piece {
2482         return &t.pieces[i]
2483 }
2484
2485 func (t *Torrent) onWriteChunkErr(err error) {
2486         if t.userOnWriteChunkErr != nil {
2487                 go t.userOnWriteChunkErr(err)
2488                 return
2489         }
2490         t.logger.WithDefaultLevel(log.Critical).Printf("default chunk write error handler: disabling data download")
2491         t.disallowDataDownloadLocked()
2492 }
2493
2494 func (t *Torrent) DisallowDataDownload() {
2495         t.cl.lock()
2496         defer t.cl.unlock()
2497         t.disallowDataDownloadLocked()
2498 }
2499
2500 func (t *Torrent) disallowDataDownloadLocked() {
2501         t.dataDownloadDisallowed.Set()
2502         t.iterPeers(func(p *Peer) {
2503                 // Could check if peer request state is empty/not interested?
2504                 p.updateRequests("disallow data download")
2505                 p.cancelAllRequests()
2506         })
2507 }
2508
2509 func (t *Torrent) AllowDataDownload() {
2510         t.cl.lock()
2511         defer t.cl.unlock()
2512         t.dataDownloadDisallowed.Clear()
2513         t.iterPeers(func(p *Peer) {
2514                 p.updateRequests("allow data download")
2515         })
2516 }
2517
2518 // Enables uploading data, if it was disabled.
2519 func (t *Torrent) AllowDataUpload() {
2520         t.cl.lock()
2521         defer t.cl.unlock()
2522         t.dataUploadDisallowed = false
2523         t.iterPeers(func(p *Peer) {
2524                 p.updateRequests("allow data upload")
2525         })
2526 }
2527
2528 // Disables uploading data, if it was enabled.
2529 func (t *Torrent) DisallowDataUpload() {
2530         t.cl.lock()
2531         defer t.cl.unlock()
2532         t.dataUploadDisallowed = true
2533         for c := range t.conns {
2534                 // TODO: This doesn't look right. Shouldn't we tickle writers to choke peers or something instead?
2535                 c.updateRequests("disallow data upload")
2536         }
2537 }
2538
2539 // Sets a handler that is called if there's an error writing a chunk to local storage. By default,
2540 // or if nil, a critical message is logged, and data download is disabled.
2541 func (t *Torrent) SetOnWriteChunkError(f func(error)) {
2542         t.cl.lock()
2543         defer t.cl.unlock()
2544         t.userOnWriteChunkErr = f
2545 }
2546
2547 func (t *Torrent) iterPeers(f func(p *Peer)) {
2548         for pc := range t.conns {
2549                 f(&pc.Peer)
2550         }
2551         for _, ws := range t.webSeeds {
2552                 f(ws)
2553         }
2554 }
2555
2556 func (t *Torrent) callbacks() *Callbacks {
2557         return &t.cl.config.Callbacks
2558 }
2559
2560 type AddWebSeedsOpt func(*webseed.Client)
2561
2562 // Sets the WebSeed trailing path escaper for a webseed.Client.
2563 func WebSeedPathEscaper(custom webseed.PathEscaper) AddWebSeedsOpt {
2564         return func(c *webseed.Client) {
2565                 c.PathEscaper = custom
2566         }
2567 }
2568
2569 func (t *Torrent) AddWebSeeds(urls []string, opts ...AddWebSeedsOpt) {
2570         t.cl.lock()
2571         defer t.cl.unlock()
2572         for _, u := range urls {
2573                 t.addWebSeed(u, opts...)
2574         }
2575 }
2576
2577 func (t *Torrent) addWebSeed(url string, opts ...AddWebSeedsOpt) {
2578         if t.cl.config.DisableWebseeds {
2579                 return
2580         }
2581         if _, ok := t.webSeeds[url]; ok {
2582                 return
2583         }
2584         // I don't think Go http supports pipelining requests. However, we can have more ready to go
2585         // right away. This value should be some multiple of the number of connections to a host. I
2586         // would expect that double maxRequests plus a bit would be appropriate. This value is based on
2587         // downloading Sintel (08ada5a7a6183aae1e09d831df6748d566095a10) from
2588         // "https://webtorrent.io/torrents/".
2589         const maxRequests = 16
2590         ws := webseedPeer{
2591                 peer: Peer{
2592                         t:                        t,
2593                         outgoing:                 true,
2594                         Network:                  "http",
2595                         reconciledHandshakeStats: true,
2596                         // This should affect how often we have to recompute requests for this peer. Note that
2597                         // because we can request more than 1 thing at a time over HTTP, we will hit the low
2598                         // requests mark more often, so recomputation is probably sooner than with regular peer
2599                         // conns. ~4x maxRequests would be about right.
2600                         PeerMaxRequests: 128,
2601                         // TODO: Set ban prefix?
2602                         RemoteAddr: remoteAddrFromUrl(url),
2603                         callbacks:  t.callbacks(),
2604                 },
2605                 client: webseed.Client{
2606                         HttpClient: t.cl.httpClient,
2607                         Url:        url,
2608                         ResponseBodyWrapper: func(r io.Reader) io.Reader {
2609                                 return &rateLimitedReader{
2610                                         l: t.cl.config.DownloadRateLimiter,
2611                                         r: r,
2612                                 }
2613                         },
2614                 },
2615                 activeRequests: make(map[Request]webseed.Request, maxRequests),
2616         }
2617         ws.peer.initRequestState()
2618         for _, opt := range opts {
2619                 opt(&ws.client)
2620         }
2621         ws.peer.initUpdateRequestsTimer()
2622         ws.requesterCond.L = t.cl.locker()
2623         for i := 0; i < maxRequests; i += 1 {
2624                 go ws.requester(i)
2625         }
2626         for _, f := range t.callbacks().NewPeer {
2627                 f(&ws.peer)
2628         }
2629         ws.peer.logger = t.logger.WithContextValue(&ws)
2630         ws.peer.peerImpl = &ws
2631         if t.haveInfo() {
2632                 ws.onGotInfo(t.info)
2633         }
2634         t.webSeeds[url] = &ws.peer
2635 }
2636
2637 func (t *Torrent) peerIsActive(p *Peer) (active bool) {
2638         t.iterPeers(func(p1 *Peer) {
2639                 if p1 == p {
2640                         active = true
2641                 }
2642         })
2643         return
2644 }
2645
2646 func (t *Torrent) requestIndexToRequest(ri RequestIndex) Request {
2647         index := t.pieceIndexOfRequestIndex(ri)
2648         return Request{
2649                 pp.Integer(index),
2650                 t.piece(index).chunkIndexSpec(ri % t.chunksPerRegularPiece()),
2651         }
2652 }
2653
2654 func (t *Torrent) requestIndexFromRequest(r Request) RequestIndex {
2655         return t.pieceRequestIndexOffset(pieceIndex(r.Index)) + RequestIndex(r.Begin/t.chunkSize)
2656 }
2657
2658 func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex {
2659         return RequestIndex(piece) * t.chunksPerRegularPiece()
2660 }
2661
2662 func (t *Torrent) updateComplete() {
2663         t.Complete.SetBool(t.haveAllPieces())
2664 }
2665
2666 func (t *Torrent) cancelRequest(r RequestIndex) *Peer {
2667         p := t.requestingPeer(r)
2668         if p != nil {
2669                 p.cancel(r)
2670         }
2671         // TODO: This is a check that an old invariant holds. It can be removed after some testing.
2672         //delete(t.pendingRequests, r)
2673         if _, ok := t.requestState[r]; ok {
2674                 panic("expected request state to be gone")
2675         }
2676         return p
2677 }
2678
2679 func (t *Torrent) requestingPeer(r RequestIndex) *Peer {
2680         return t.requestState[r].peer
2681 }
2682
2683 func (t *Torrent) addConnWithAllPieces(p *Peer) {
2684         if t.connsWithAllPieces == nil {
2685                 t.connsWithAllPieces = make(map[*Peer]struct{}, t.maxEstablishedConns)
2686         }
2687         t.connsWithAllPieces[p] = struct{}{}
2688 }
2689
2690 func (t *Torrent) deleteConnWithAllPieces(p *Peer) bool {
2691         _, ok := t.connsWithAllPieces[p]
2692         delete(t.connsWithAllPieces, p)
2693         return ok
2694 }
2695
2696 func (t *Torrent) numActivePeers() int {
2697         return len(t.conns) + len(t.webSeeds)
2698 }
2699
2700 func (t *Torrent) hasStorageCap() bool {
2701         f := t.storage.Capacity
2702         if f == nil {
2703                 return false
2704         }
2705         _, ok := (*f)()
2706         return ok
2707 }
2708
2709 func (t *Torrent) pieceIndexOfRequestIndex(ri RequestIndex) pieceIndex {
2710         return pieceIndex(ri / t.chunksPerRegularPiece())
2711 }
2712
2713 func (t *Torrent) iterUndirtiedRequestIndexesInPiece(
2714         reuseIter *typedRoaring.Iterator[RequestIndex],
2715         piece pieceIndex,
2716         f func(RequestIndex),
2717 ) {
2718         reuseIter.Initialize(&t.dirtyChunks)
2719         pieceRequestIndexOffset := t.pieceRequestIndexOffset(piece)
2720         iterBitmapUnsetInRange(
2721                 reuseIter,
2722                 pieceRequestIndexOffset, pieceRequestIndexOffset+t.pieceNumChunks(piece),
2723                 f,
2724         )
2725 }
2726
2727 type requestState struct {
2728         peer *Peer
2729         when time.Time
2730 }
2731
2732 // Returns an error if a received chunk is out of bounds in someway.
2733 func (t *Torrent) checkValidReceiveChunk(r Request) error {
2734         if !t.haveInfo() {
2735                 return errors.New("torrent missing info")
2736         }
2737         if int(r.Index) >= t.numPieces() {
2738                 return fmt.Errorf("chunk index %v, torrent num pieces %v", r.Index, t.numPieces())
2739         }
2740         pieceLength := t.pieceLength(pieceIndex(r.Index))
2741         if r.Begin >= pieceLength {
2742                 return fmt.Errorf("chunk begins beyond end of piece (%v >= %v)", r.Begin, pieceLength)
2743         }
2744         // We could check chunk lengths here, but chunk request size is not changed often, and tricky
2745         // for peers to manipulate as they need to send potentially large buffers to begin with. There
2746         // should be considerable checks elsewhere for this case due to the network overhead. We should
2747         // catch most of the overflow manipulation stuff by checking index and begin above.
2748         return nil
2749 }
2750
2751 func (t *Torrent) peerConnsWithDialAddrPort(target netip.AddrPort) (ret []*PeerConn) {
2752         for pc := range t.conns {
2753                 dialAddr, err := pc.remoteDialAddrPort()
2754                 if err != nil {
2755                         continue
2756                 }
2757                 if dialAddr != target {
2758                         continue
2759                 }
2760                 ret = append(ret, pc)
2761         }
2762         return
2763 }
2764
2765 func wrapUtHolepunchMsgForPeerConn(
2766         recipient *PeerConn,
2767         msg utHolepunch.Msg,
2768 ) pp.Message {
2769         extendedPayload, err := msg.MarshalBinary()
2770         if err != nil {
2771                 panic(err)
2772         }
2773         return pp.Message{
2774                 Type:            pp.Extended,
2775                 ExtendedID:      MapMustGet(recipient.PeerExtensionIDs, utHolepunch.ExtensionName),
2776                 ExtendedPayload: extendedPayload,
2777         }
2778 }
2779
2780 func sendUtHolepunchMsg(
2781         pc *PeerConn,
2782         msgType utHolepunch.MsgType,
2783         addrPort netip.AddrPort,
2784         errCode utHolepunch.ErrCode,
2785 ) {
2786         holepunchMsg := utHolepunch.Msg{
2787                 MsgType:  msgType,
2788                 AddrPort: addrPort,
2789                 ErrCode:  errCode,
2790         }
2791         incHolepunchMessagesSent(holepunchMsg)
2792         ppMsg := wrapUtHolepunchMsgForPeerConn(pc, holepunchMsg)
2793         pc.write(ppMsg)
2794 }
2795
2796 func incHolepunchMessages(msg utHolepunch.Msg, verb string) {
2797         torrent.Add(
2798                 fmt.Sprintf(
2799                         "holepunch %v %v messages %v",
2800                         msg.MsgType,
2801                         addrPortProtocolStr(msg.AddrPort),
2802                         verb,
2803                 ),
2804                 1,
2805         )
2806 }
2807
2808 func incHolepunchMessagesReceived(msg utHolepunch.Msg) {
2809         incHolepunchMessages(msg, "received")
2810 }
2811
2812 func incHolepunchMessagesSent(msg utHolepunch.Msg) {
2813         incHolepunchMessages(msg, "sent")
2814 }
2815
2816 func (t *Torrent) handleReceivedUtHolepunchMsg(msg utHolepunch.Msg, sender *PeerConn) error {
2817         incHolepunchMessagesReceived(msg)
2818         switch msg.MsgType {
2819         case utHolepunch.Rendezvous:
2820                 t.logger.Printf("got holepunch rendezvous request for %v from %p", msg.AddrPort, sender)
2821                 sendMsg := sendUtHolepunchMsg
2822                 senderAddrPort, err := sender.remoteDialAddrPort()
2823                 if err != nil {
2824                         sender.logger.Levelf(
2825                                 log.Warning,
2826                                 "error getting ut_holepunch rendezvous sender's dial address: %v",
2827                                 err,
2828                         )
2829                         // There's no better error code. The sender's address itself is invalid. I don't see
2830                         // this error message being appropriate anywhere else anyway.
2831                         sendMsg(sender, utHolepunch.Error, msg.AddrPort, utHolepunch.NoSuchPeer)
2832                 }
2833                 targets := t.peerConnsWithDialAddrPort(msg.AddrPort)
2834                 if len(targets) == 0 {
2835                         sendMsg(sender, utHolepunch.Error, msg.AddrPort, utHolepunch.NotConnected)
2836                         return nil
2837                 }
2838                 for _, pc := range targets {
2839                         if !pc.supportsExtension(utHolepunch.ExtensionName) {
2840                                 sendMsg(sender, utHolepunch.Error, msg.AddrPort, utHolepunch.NoSupport)
2841                                 continue
2842                         }
2843                         sendMsg(sender, utHolepunch.Connect, msg.AddrPort, 0)
2844                         sendMsg(pc, utHolepunch.Connect, senderAddrPort, 0)
2845                 }
2846                 return nil
2847         case utHolepunch.Connect:
2848                 holepunchAddr := msg.AddrPort
2849                 t.logger.Printf("got holepunch connect request for %v from %p", holepunchAddr, sender)
2850                 if g.MapContains(t.cl.undialableWithoutHolepunch, holepunchAddr) {
2851                         setAdd(&t.cl.undialableWithoutHolepunchDialedAfterHolepunchConnect, holepunchAddr)
2852                         if g.MapContains(t.cl.accepted, holepunchAddr) {
2853                                 setAdd(&t.cl.probablyOnlyConnectedDueToHolepunch, holepunchAddr)
2854                         }
2855                 }
2856                 opts := outgoingConnOpts{
2857                         peerInfo: PeerInfo{
2858                                 Addr:         msg.AddrPort,
2859                                 Source:       PeerSourceUtHolepunch,
2860                                 PexPeerFlags: sender.pex.remoteLiveConns[msg.AddrPort].UnwrapOrZeroValue(),
2861                         },
2862                         t: t,
2863                         // Don't attempt to start our own rendezvous if we fail to connect.
2864                         skipHolepunchRendezvous:  true,
2865                         receivedHolepunchConnect: true,
2866                         // Assume that the other end initiated the rendezvous, and will use our preferred
2867                         // encryption. So we will act normally.
2868                         HeaderObfuscationPolicy: t.cl.config.HeaderObfuscationPolicy,
2869                 }
2870                 initiateConn(opts, true)
2871                 return nil
2872         case utHolepunch.Error:
2873                 torrent.Add("holepunch error messages received", 1)
2874                 t.logger.Levelf(log.Debug, "received ut_holepunch error message from %v: %v", sender, msg.ErrCode)
2875                 return nil
2876         default:
2877                 return fmt.Errorf("unhandled msg type %v", msg.MsgType)
2878         }
2879 }
2880
2881 func addrPortProtocolStr(addrPort netip.AddrPort) string {
2882         addr := addrPort.Addr()
2883         switch {
2884         case addr.Is4():
2885                 return "ipv4"
2886         case addr.Is6():
2887                 return "ipv6"
2888         default:
2889                 panic(addrPort)
2890         }
2891 }
2892
2893 func (t *Torrent) trySendHolepunchRendezvous(addrPort netip.AddrPort) error {
2894         rzsSent := 0
2895         for pc := range t.conns {
2896                 if !pc.supportsExtension(utHolepunch.ExtensionName) {
2897                         continue
2898                 }
2899                 if pc.supportsExtension(pp.ExtensionNamePex) {
2900                         if !g.MapContains(pc.pex.remoteLiveConns, addrPort) {
2901                                 continue
2902                         }
2903                 }
2904                 t.logger.Levelf(log.Debug, "sent ut_holepunch rendezvous message to %v for %v", pc, addrPort)
2905                 sendUtHolepunchMsg(pc, utHolepunch.Rendezvous, addrPort, 0)
2906                 rzsSent++
2907         }
2908         if rzsSent == 0 {
2909                 return errors.New("no eligible relays")
2910         }
2911         return nil
2912 }
2913
2914 func (t *Torrent) numHalfOpenAttempts() (num int) {
2915         for _, attempts := range t.halfOpen {
2916                 num += len(attempts)
2917         }
2918         return
2919 }
2920
2921 func (t *Torrent) getDialTimeoutUnlocked() time.Duration {
2922         cl := t.cl
2923         cl.rLock()
2924         defer cl.rUnlock()
2925         return t.dialTimeout()
2926 }