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/perf"
28 "github.com/anacrolix/missinggo/slices"
29 "github.com/anacrolix/missinggo/v2"
30 "github.com/anacrolix/missinggo/v2/bitmap"
31 "github.com/anacrolix/missinggo/v2/pubsub"
32 "github.com/anacrolix/multiless"
33 "github.com/anacrolix/sync"
34 "github.com/pion/datachannel"
35 "golang.org/x/exp/maps"
37 "github.com/anacrolix/torrent/bencode"
38 "github.com/anacrolix/torrent/common"
39 "github.com/anacrolix/torrent/internal/check"
40 "github.com/anacrolix/torrent/internal/nestedmaps"
41 "github.com/anacrolix/torrent/metainfo"
42 pp "github.com/anacrolix/torrent/peer_protocol"
43 utHolepunch "github.com/anacrolix/torrent/peer_protocol/ut-holepunch"
44 request_strategy "github.com/anacrolix/torrent/request-strategy"
45 "github.com/anacrolix/torrent/segments"
46 "github.com/anacrolix/torrent/storage"
47 "github.com/anacrolix/torrent/tracker"
48 typedRoaring "github.com/anacrolix/torrent/typed-roaring"
49 "github.com/anacrolix/torrent/webseed"
50 "github.com/anacrolix/torrent/webtorrent"
53 // Maintains state of torrent within a Client. Many methods should not be called before the info is
54 // available, see .Info and .GotInfo.
56 // Torrent-level aggregate statistics. First in struct to ensure 64-bit
57 // alignment. See #262.
62 networkingEnabled chansync.Flag
63 dataDownloadDisallowed chansync.Flag
64 dataUploadDisallowed bool
65 userOnWriteChunkErr func(error)
67 closed chansync.SetOnce
69 infoHash metainfo.Hash
72 // The order pieces are requested if there's no stronger reason like availability or priority.
73 pieceRequestOrder []int
74 // Values are the piece indices that changed.
75 pieceStateChanges pubsub.PubSub[PieceStateChange]
76 // The size of chunks to request from peers over the wire. This is
77 // normally 16KiB by convention these days.
80 // Total length of the torrent in bytes. Stored because it's not O(1) to
81 // get this from the info dict.
84 // The storage to open when the info dict becomes available.
85 storageOpener *storage.Client
86 // Storage for torrent data.
87 storage *storage.Torrent
88 // Read-locked for using storage, and write-locked for Closing.
89 storageLock sync.RWMutex
91 // TODO: Only announce stuff is used?
92 metainfo metainfo.MetaInfo
94 // The info dict. nil if we don't have it (yet).
96 fileIndex segments.Index
99 _chunksPerRegularPiece chunkIndexType
101 webSeeds map[string]*Peer
102 // Active peer connections, running message stream loops. TODO: Make this
103 // open (not-closed) connections only.
104 conns map[*PeerConn]struct{}
105 maxEstablishedConns int
106 // Set of addrs to which we're attempting to connect. Connections are
107 // half-open until all handshakes are completed.
108 halfOpen map[string]map[outgoingConnAttemptKey]*PeerInfo
109 // The final ess is not silent here as it's in the plural.
110 utHolepunchRendezvous map[netip.AddrPort]*utHolepunchRendezvous
112 // Reserve of peers to connect to. A peer can be both here and in the
113 // active connections if were told about the peer after connecting with
114 // them. That encourages us to reconnect to peers that are well known in
116 peers prioritizedPeers
117 // Whether we want to know more peers.
118 wantPeersEvent missinggo.Event
119 // An announcer for each tracker URL.
120 trackerAnnouncers map[string]torrentTrackerAnnouncer
121 // How many times we've initiated a DHT announce. TODO: Move into stats.
124 // Name used if the info name isn't available. Should be cleared when the
125 // Info does become available.
129 // The bencoded bytes of the info dict. This is actively manipulated if
130 // the info bytes aren't initially available, and we try to fetch them
133 // Each element corresponds to the 16KiB metadata pieces. If true, we have
134 // received that piece.
135 metadataCompletedChunks []bool
136 metadataChanged sync.Cond
138 // Closed when .Info is obtained.
139 gotMetainfoC chan struct{}
141 readers map[*reader]struct{}
142 _readerNowPieces bitmap.Bitmap
143 _readerReadaheadPieces bitmap.Bitmap
145 // A cache of pieces we need to get. Calculated from various piece and
146 // file priorities and completion states elsewhere.
147 _pendingPieces roaring.Bitmap
148 // A cache of completed piece indices.
149 _completedPieces roaring.Bitmap
150 // Pieces that need to be hashed.
151 piecesQueuedForHash bitmap.Bitmap
152 activePieceHashes int
153 initialPieceCheckDisabled bool
155 connsWithAllPieces map[*Peer]struct{}
157 requestState map[RequestIndex]requestState
158 // Chunks we've written to since the corresponding piece was last checked.
159 dirtyChunks typedRoaring.Bitmap[RequestIndex]
163 // Is On when all pieces are complete.
164 Complete chansync.Flag
166 // Torrent sources in use keyed by the source string.
167 activeSources sync.Map
168 sourcesLogger log.Logger
170 smartBanCache smartBanCache
172 // Large allocations reused between request state updates.
173 requestPieceStates []request_strategy.PieceRequestOrderState
174 requestIndexes []RequestIndex
177 type outgoingConnAttemptKey = *PeerInfo
179 func (t *Torrent) length() int64 {
180 return t._length.Value
183 func (t *Torrent) selectivePieceAvailabilityFromPeers(i pieceIndex) (count int) {
184 // This could be done with roaring.BitSliceIndexing.
185 t.iterPeers(func(peer *Peer) {
186 if _, ok := t.connsWithAllPieces[peer]; ok {
189 if peer.peerHasPiece(i) {
196 func (t *Torrent) decPieceAvailability(i pieceIndex) {
201 if p.relativeAvailability <= 0 {
202 panic(p.relativeAvailability)
204 p.relativeAvailability--
205 t.updatePieceRequestOrder(i)
208 func (t *Torrent) incPieceAvailability(i pieceIndex) {
209 // If we don't the info, this should be reconciled when we do.
212 p.relativeAvailability++
213 t.updatePieceRequestOrder(i)
217 func (t *Torrent) readerNowPieces() bitmap.Bitmap {
218 return t._readerNowPieces
221 func (t *Torrent) readerReadaheadPieces() bitmap.Bitmap {
222 return t._readerReadaheadPieces
225 func (t *Torrent) ignorePieceForRequests(i pieceIndex) bool {
226 return !t.wantPieceIndex(i)
229 // Returns a channel that is closed when the Torrent is closed.
230 func (t *Torrent) Closed() events.Done {
231 return t.closed.Done()
234 // KnownSwarm returns the known subset of the peers in the Torrent's swarm, including active,
235 // pending, and half-open peers.
236 func (t *Torrent) KnownSwarm() (ks []PeerInfo) {
237 // Add pending peers to the list
238 t.peers.Each(func(peer PeerInfo) {
239 ks = append(ks, peer)
242 // Add half-open peers to the list
243 for _, attempts := range t.halfOpen {
244 for _, peer := range attempts {
245 ks = append(ks, *peer)
249 // Add active peers to the list
250 for conn := range t.conns {
251 ks = append(ks, PeerInfo{
253 Addr: conn.RemoteAddr,
254 Source: conn.Discovery,
255 // > If the connection is encrypted, that's certainly enough to set SupportsEncryption.
256 // > But if we're not connected to them with an encrypted connection, I couldn't say
257 // > what's appropriate. We can carry forward the SupportsEncryption value as we
258 // > received it from trackers/DHT/PEX, or just use the encryption state for the
259 // > connection. It's probably easiest to do the latter for now.
260 // https://github.com/anacrolix/torrent/pull/188
261 SupportsEncryption: conn.headerEncrypted,
268 func (t *Torrent) setChunkSize(size pp.Integer) {
270 t.chunkPool = sync.Pool{
271 New: func() interface{} {
272 b := make([]byte, size)
278 func (t *Torrent) pieceComplete(piece pieceIndex) bool {
279 return t._completedPieces.Contains(bitmap.BitIndex(piece))
282 func (t *Torrent) pieceCompleteUncached(piece pieceIndex) storage.Completion {
283 if t.storage == nil {
284 return storage.Completion{Complete: false, Ok: true}
286 return t.pieces[piece].Storage().Completion()
289 // There's a connection to that address already.
290 func (t *Torrent) addrActive(addr string) bool {
291 if _, ok := t.halfOpen[addr]; ok {
294 for c := range t.conns {
296 if ra.String() == addr {
303 func (t *Torrent) appendUnclosedConns(ret []*PeerConn) []*PeerConn {
304 return t.appendConns(ret, func(conn *PeerConn) bool {
305 return !conn.closed.IsSet()
309 func (t *Torrent) appendConns(ret []*PeerConn, f func(*PeerConn) bool) []*PeerConn {
310 for c := range t.conns {
318 func (t *Torrent) addPeer(p PeerInfo) (added bool) {
320 torrent.Add(fmt.Sprintf("peers added by source %q", p.Source), 1)
321 if t.closed.IsSet() {
324 if ipAddr, ok := tryIpPortFromNetAddr(p.Addr); ok {
325 if cl.badPeerIPPort(ipAddr.IP, ipAddr.Port) {
326 torrent.Add("peers not added because of bad addr", 1)
327 // cl.logger.Printf("peers not added because of bad addr: %v", p)
331 if replaced, ok := t.peers.AddReturningReplacedPeer(p); ok {
332 torrent.Add("peers replaced", 1)
333 if !replaced.equal(p) {
334 t.logger.WithDefaultLevel(log.Debug).Printf("added %v replacing %v", p, replaced)
341 for t.peers.Len() > cl.config.TorrentPeersHighWater {
342 _, ok := t.peers.DeleteMin()
344 torrent.Add("excess reserve peers discarded", 1)
350 func (t *Torrent) invalidateMetadata() {
351 for i := 0; i < len(t.metadataCompletedChunks); i++ {
352 t.metadataCompletedChunks[i] = false
355 t.gotMetainfoC = make(chan struct{})
360 func (t *Torrent) saveMetadataPiece(index int, data []byte) {
364 if index >= len(t.metadataCompletedChunks) {
365 t.logger.Printf("%s: ignoring metadata piece %d", t, index)
368 copy(t.metadataBytes[(1<<14)*index:], data)
369 t.metadataCompletedChunks[index] = true
372 func (t *Torrent) metadataPieceCount() int {
373 return (len(t.metadataBytes) + (1 << 14) - 1) / (1 << 14)
376 func (t *Torrent) haveMetadataPiece(piece int) bool {
378 return (1<<14)*piece < len(t.metadataBytes)
380 return piece < len(t.metadataCompletedChunks) && t.metadataCompletedChunks[piece]
384 func (t *Torrent) metadataSize() int {
385 return len(t.metadataBytes)
388 func infoPieceHashes(info *metainfo.Info) (ret [][]byte) {
389 for i := 0; i < len(info.Pieces); i += sha1.Size {
390 ret = append(ret, info.Pieces[i:i+sha1.Size])
395 func (t *Torrent) makePieces() {
396 hashes := infoPieceHashes(t.info)
397 t.pieces = make([]Piece, len(hashes))
398 for i, hash := range hashes {
399 piece := &t.pieces[i]
401 piece.index = pieceIndex(i)
402 piece.noPendingWrites.L = &piece.pendingWritesMutex
403 piece.hash = (*metainfo.Hash)(unsafe.Pointer(&hash[0]))
405 beginFile := pieceFirstFileIndex(piece.torrentBeginOffset(), files)
406 endFile := pieceEndFileIndex(piece.torrentEndOffset(), files)
407 piece.files = files[beginFile:endFile]
411 // Returns the index of the first file containing the piece. files must be
412 // ordered by offset.
413 func pieceFirstFileIndex(pieceOffset int64, files []*File) int {
414 for i, f := range files {
415 if f.offset+f.length > pieceOffset {
422 // Returns the index after the last file containing the piece. files must be
423 // ordered by offset.
424 func pieceEndFileIndex(pieceEndOffset int64, files []*File) int {
425 for i, f := range files {
426 if f.offset+f.length >= pieceEndOffset {
433 func (t *Torrent) cacheLength() {
435 for _, f := range t.info.UpvertedFiles() {
441 // TODO: This shouldn't fail for storage reasons. Instead we should handle storage failure
443 func (t *Torrent) setInfo(info *metainfo.Info) error {
444 if err := validateInfo(info); err != nil {
445 return fmt.Errorf("bad info: %s", err)
447 if t.storageOpener != nil {
449 t.storage, err = t.storageOpener.OpenTorrent(info, t.infoHash)
451 return fmt.Errorf("error opening torrent storage: %s", err)
457 t._chunksPerRegularPiece = chunkIndexType((pp.Integer(t.usualPieceSize()) + t.chunkSize - 1) / t.chunkSize)
459 t.fileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
460 t.displayName = "" // Save a few bytes lol.
467 func (t *Torrent) pieceRequestOrderKey(i int) request_strategy.PieceRequestOrderKey {
468 return request_strategy.PieceRequestOrderKey{
469 InfoHash: t.infoHash,
474 // This seems to be all the follow-up tasks after info is set, that can't fail.
475 func (t *Torrent) onSetInfo() {
476 t.pieceRequestOrder = rand.Perm(t.numPieces())
477 t.initPieceRequestOrder()
478 MakeSliceWithLength(&t.requestPieceStates, t.numPieces())
479 for i := range t.pieces {
481 // Need to add relativeAvailability before updating piece completion, as that may result in conns
483 if p.relativeAvailability != 0 {
484 panic(p.relativeAvailability)
486 p.relativeAvailability = t.selectivePieceAvailabilityFromPeers(i)
487 t.addRequestOrderPiece(i)
488 t.updatePieceCompletion(i)
489 if !t.initialPieceCheckDisabled && !p.storageCompletionOk {
490 // t.logger.Printf("piece %s completion unknown, queueing check", p)
494 t.cl.event.Broadcast()
495 close(t.gotMetainfoC)
496 t.updateWantPeersEvent()
497 t.requestState = make(map[RequestIndex]requestState)
498 t.tryCreateMorePieceHashers()
499 t.iterPeers(func(p *Peer) {
501 p.updateRequests("onSetInfo")
505 // Called when metadata for a torrent becomes available.
506 func (t *Torrent) setInfoBytesLocked(b []byte) error {
507 if metainfo.HashBytes(b) != t.infoHash {
508 return errors.New("info bytes have wrong hash")
510 var info metainfo.Info
511 if err := bencode.Unmarshal(b, &info); err != nil {
512 return fmt.Errorf("error unmarshalling info bytes: %s", err)
515 t.metadataCompletedChunks = nil
519 if err := t.setInfo(&info); err != nil {
526 func (t *Torrent) haveAllMetadataPieces() bool {
530 if t.metadataCompletedChunks == nil {
533 for _, have := range t.metadataCompletedChunks {
541 // TODO: Propagate errors to disconnect peer.
542 func (t *Torrent) setMetadataSize(size int) (err error) {
544 // We already know the correct metadata size.
547 if uint32(size) > maxMetadataSize {
548 return log.WithLevel(log.Warning, errors.New("bad size"))
550 if len(t.metadataBytes) == size {
553 t.metadataBytes = make([]byte, size)
554 t.metadataCompletedChunks = make([]bool, (size+(1<<14)-1)/(1<<14))
555 t.metadataChanged.Broadcast()
556 for c := range t.conns {
557 c.requestPendingMetadata()
562 // The current working name for the torrent. Either the name in the info dict,
563 // or a display name given such as by the dn value in a magnet link, or "".
564 func (t *Torrent) name() string {
566 defer t.nameMu.RUnlock()
568 return t.info.BestName()
570 if t.displayName != "" {
573 return "infohash:" + t.infoHash.HexString()
576 func (t *Torrent) pieceState(index pieceIndex) (ret PieceState) {
577 p := &t.pieces[index]
578 ret.Priority = t.piecePriority(index)
579 ret.Completion = p.completion()
580 ret.QueuedForHash = p.queuedForHash()
581 ret.Hashing = p.hashing
582 ret.Checking = ret.QueuedForHash || ret.Hashing
583 ret.Marking = p.marking
584 if !ret.Complete && t.piecePartiallyDownloaded(index) {
590 func (t *Torrent) metadataPieceSize(piece int) int {
591 return metadataPieceSize(len(t.metadataBytes), piece)
594 func (t *Torrent) newMetadataExtensionMessage(c *PeerConn, msgType pp.ExtendedMetadataRequestMsgType, piece int, data []byte) pp.Message {
597 ExtendedID: c.PeerExtensionIDs[pp.ExtensionNameMetadata],
598 ExtendedPayload: append(bencode.MustMarshal(pp.ExtendedMetadataRequestMsg{
600 TotalSize: len(t.metadataBytes),
606 type pieceAvailabilityRun struct {
611 func (me pieceAvailabilityRun) String() string {
612 return fmt.Sprintf("%v(%v)", me.Count, me.Availability)
615 func (t *Torrent) pieceAvailabilityRuns() (ret []pieceAvailabilityRun) {
616 rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
617 ret = append(ret, pieceAvailabilityRun{Availability: el.(int), Count: int(count)})
619 for i := range t.pieces {
620 rle.Append(t.pieces[i].availability(), 1)
626 func (t *Torrent) pieceAvailabilityFrequencies() (freqs []int) {
627 freqs = make([]int, t.numActivePeers()+1)
628 for i := range t.pieces {
629 freqs[t.piece(i).availability()]++
634 func (t *Torrent) pieceStateRuns() (ret PieceStateRuns) {
635 rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
636 ret = append(ret, PieceStateRun{
637 PieceState: el.(PieceState),
641 for index := range t.pieces {
642 rle.Append(t.pieceState(pieceIndex(index)), 1)
648 // Produces a small string representing a PieceStateRun.
649 func (psr PieceStateRun) String() (ret string) {
650 ret = fmt.Sprintf("%d", psr.Length)
651 ret += func() string {
652 switch psr.Priority {
653 case PiecePriorityNext:
655 case PiecePriorityNormal:
657 case PiecePriorityReadahead:
659 case PiecePriorityNow:
661 case PiecePriorityHigh:
670 if psr.QueuedForHash {
688 func (t *Torrent) writeStatus(w io.Writer) {
689 fmt.Fprintf(w, "Infohash: %s\n", t.infoHash.HexString())
690 fmt.Fprintf(w, "Metadata length: %d\n", t.metadataSize())
692 fmt.Fprintf(w, "Metadata have: ")
693 for _, h := range t.metadataCompletedChunks {
694 fmt.Fprintf(w, "%c", func() rune {
704 fmt.Fprintf(w, "Piece length: %s\n",
707 return fmt.Sprintf("%v (%v chunks)",
709 float64(t.usualPieceSize())/float64(t.chunkSize))
716 fmt.Fprintf(w, "Num Pieces: %d (%d completed)\n", t.numPieces(), t.numPiecesCompleted())
717 fmt.Fprintf(w, "Piece States: %s\n", t.pieceStateRuns())
718 // Generates a huge, unhelpful listing when piece availability is very scattered. Prefer
719 // availability frequencies instead.
721 fmt.Fprintf(w, "Piece availability: %v\n", strings.Join(func() (ret []string) {
722 for _, run := range t.pieceAvailabilityRuns() {
723 ret = append(ret, run.String())
728 fmt.Fprintf(w, "Piece availability frequency: %v\n", strings.Join(
729 func() (ret []string) {
730 for avail, freq := range t.pieceAvailabilityFrequencies() {
734 ret = append(ret, fmt.Sprintf("%v: %v", avail, freq))
740 fmt.Fprintf(w, "Reader Pieces:")
741 t.forReaderOffsetPieces(func(begin, end pieceIndex) (again bool) {
742 fmt.Fprintf(w, " %d:%d", begin, end)
747 fmt.Fprintf(w, "Enabled trackers:\n")
749 tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
750 fmt.Fprintf(tw, " URL\tExtra\n")
751 for _, ta := range slices.Sort(slices.FromMapElems(t.trackerAnnouncers), func(l, r torrentTrackerAnnouncer) bool {
754 var luns, runs url.URL = *lu, *ru
757 var ml missinggo.MultiLess
758 ml.StrictNext(luns.String() == runs.String(), luns.String() < runs.String())
759 ml.StrictNext(lu.String() == ru.String(), lu.String() < ru.String())
761 }).([]torrentTrackerAnnouncer) {
762 fmt.Fprintf(tw, " %q\t%v\n", ta.URL(), ta.statusLine())
767 fmt.Fprintf(w, "DHT Announces: %d\n", t.numDHTAnnounces)
769 dumpStats(w, t.statsLocked())
771 fmt.Fprintf(w, "webseeds:\n")
772 t.writePeerStatuses(w, maps.Values(t.webSeeds))
774 peerConns := maps.Keys(t.conns)
775 // Peers without priorities first, then those with. I'm undecided about how to order peers
776 // without priorities.
777 sort.Slice(peerConns, func(li, ri int) bool {
780 ml := multiless.New()
781 lpp := g.ResultFromTuple(l.peerPriority()).ToOption()
782 rpp := g.ResultFromTuple(r.peerPriority()).ToOption()
783 ml = ml.Bool(lpp.Ok, rpp.Ok)
784 ml = ml.Uint32(rpp.Value, lpp.Value)
788 fmt.Fprintf(w, "peer conns:\n")
789 t.writePeerStatuses(w, g.SliceMap(peerConns, func(pc *PeerConn) *Peer {
794 func (t *Torrent) writePeerStatuses(w io.Writer, peers []*Peer) {
796 for _, c := range peers {
800 w.Write(bytes.TrimRight(
801 bytes.ReplaceAll(buf.Bytes(), []byte("\n"), []byte("\n ")),
806 func (t *Torrent) haveInfo() bool {
810 // Returns a run-time generated MetaInfo that includes the info bytes and
811 // announce-list as currently known to the client.
812 func (t *Torrent) newMetaInfo() metainfo.MetaInfo {
813 return metainfo.MetaInfo{
814 CreationDate: time.Now().Unix(),
815 Comment: "dynamic metainfo from client",
816 CreatedBy: "go.torrent",
817 AnnounceList: t.metainfo.UpvertedAnnounceList().Clone(),
818 InfoBytes: func() []byte {
820 return t.metadataBytes
825 UrlList: func() []string {
826 ret := make([]string, 0, len(t.webSeeds))
827 for url := range t.webSeeds {
828 ret = append(ret, url)
836 func (t *Torrent) BytesMissing() (n int64) {
838 n = t.bytesMissingLocked()
843 func (t *Torrent) bytesMissingLocked() int64 {
847 func iterFlipped(b *roaring.Bitmap, end uint64, cb func(uint32) bool) {
848 roaring.Flip(b, 0, end).Iterate(cb)
851 func (t *Torrent) bytesLeft() (left int64) {
852 iterFlipped(&t._completedPieces, uint64(t.numPieces()), func(x uint32) bool {
853 p := t.piece(pieceIndex(x))
854 left += int64(p.length() - p.numDirtyBytes())
860 // Bytes left to give in tracker announces.
861 func (t *Torrent) bytesLeftAnnounce() int64 {
869 func (t *Torrent) piecePartiallyDownloaded(piece pieceIndex) bool {
870 if t.pieceComplete(piece) {
873 if t.pieceAllDirty(piece) {
876 return t.pieces[piece].hasDirtyChunks()
879 func (t *Torrent) usualPieceSize() int {
880 return int(t.info.PieceLength)
883 func (t *Torrent) numPieces() pieceIndex {
884 return t.info.NumPieces()
887 func (t *Torrent) numPiecesCompleted() (num pieceIndex) {
888 return pieceIndex(t._completedPieces.GetCardinality())
891 func (t *Torrent) close(wg *sync.WaitGroup) (err error) {
893 err = errors.New("already closed")
896 for _, f := range t.onClose {
899 if t.storage != nil {
904 defer t.storageLock.Unlock()
905 if f := t.storage.Close; f != nil {
908 t.logger.WithDefaultLevel(log.Warning).Printf("error closing storage: %v", err1)
913 t.iterPeers(func(p *Peer) {
916 if t.storage != nil {
917 t.deletePieceRequestOrder()
919 t.assertAllPiecesRelativeAvailabilityZero()
921 t.cl.event.Broadcast()
922 t.pieceStateChanges.Close()
923 t.updateWantPeersEvent()
927 func (t *Torrent) assertAllPiecesRelativeAvailabilityZero() {
928 for i := range t.pieces {
930 if p.relativeAvailability != 0 {
931 panic(fmt.Sprintf("piece %v has relative availability %v", i, p.relativeAvailability))
936 func (t *Torrent) requestOffset(r Request) int64 {
937 return torrentRequestOffset(t.length(), int64(t.usualPieceSize()), r)
940 // Return the request that would include the given offset into the torrent data. Returns !ok if
941 // there is no such request.
942 func (t *Torrent) offsetRequest(off int64) (req Request, ok bool) {
943 return torrentOffsetRequest(t.length(), t.info.PieceLength, int64(t.chunkSize), off)
946 func (t *Torrent) writeChunk(piece int, begin int64, data []byte) (err error) {
947 defer perf.ScopeTimerErr(&err)()
948 n, err := t.pieces[piece].Storage().WriteAt(data, begin)
949 if err == nil && n != len(data) {
950 err = io.ErrShortWrite
955 func (t *Torrent) bitfield() (bf []bool) {
956 bf = make([]bool, t.numPieces())
957 t._completedPieces.Iterate(func(piece uint32) (again bool) {
964 func (t *Torrent) pieceNumChunks(piece pieceIndex) chunkIndexType {
965 return chunkIndexType((t.pieceLength(piece) + t.chunkSize - 1) / t.chunkSize)
968 func (t *Torrent) chunksPerRegularPiece() chunkIndexType {
969 return t._chunksPerRegularPiece
972 func (t *Torrent) numChunks() RequestIndex {
973 if t.numPieces() == 0 {
976 return RequestIndex(t.numPieces()-1)*t.chunksPerRegularPiece() + t.pieceNumChunks(t.numPieces()-1)
979 func (t *Torrent) pendAllChunkSpecs(pieceIndex pieceIndex) {
980 t.dirtyChunks.RemoveRange(
981 uint64(t.pieceRequestIndexOffset(pieceIndex)),
982 uint64(t.pieceRequestIndexOffset(pieceIndex+1)))
985 func (t *Torrent) pieceLength(piece pieceIndex) pp.Integer {
986 if t.info.PieceLength == 0 {
987 // There will be no variance amongst pieces. Only pain.
990 if piece == t.numPieces()-1 {
991 ret := pp.Integer(t.length() % t.info.PieceLength)
996 return pp.Integer(t.info.PieceLength)
999 func (t *Torrent) smartBanBlockCheckingWriter(piece pieceIndex) *blockCheckingWriter {
1000 return &blockCheckingWriter{
1001 cache: &t.smartBanCache,
1002 requestIndex: t.pieceRequestIndexOffset(piece),
1003 chunkSize: t.chunkSize.Int(),
1007 func (t *Torrent) hashPiece(piece pieceIndex) (
1009 // These are peers that sent us blocks that differ from what we hash here.
1010 differingPeers map[bannableAddr]struct{},
1014 p.waitNoPendingWrites()
1015 storagePiece := t.pieces[piece].Storage()
1017 // Does the backend want to do its own hashing?
1018 if i, ok := storagePiece.PieceImpl.(storage.SelfHashing); ok {
1019 var sum metainfo.Hash
1020 // log.Printf("A piece decided to self-hash: %d", piece)
1021 sum, err = i.SelfHash()
1022 missinggo.CopyExact(&ret, sum)
1026 hash := pieceHash.New()
1027 const logPieceContents = false
1028 smartBanWriter := t.smartBanBlockCheckingWriter(piece)
1029 writers := []io.Writer{hash, smartBanWriter}
1030 var examineBuf bytes.Buffer
1031 if logPieceContents {
1032 writers = append(writers, &examineBuf)
1034 _, err = storagePiece.WriteTo(io.MultiWriter(writers...))
1035 if logPieceContents {
1036 t.logger.WithDefaultLevel(log.Debug).Printf("hashed %q with copy err %v", examineBuf.Bytes(), err)
1038 smartBanWriter.Flush()
1039 differingPeers = smartBanWriter.badPeers
1040 missinggo.CopyExact(&ret, hash.Sum(nil))
1044 func (t *Torrent) haveAnyPieces() bool {
1045 return !t._completedPieces.IsEmpty()
1048 func (t *Torrent) haveAllPieces() bool {
1052 return t._completedPieces.GetCardinality() == bitmap.BitRange(t.numPieces())
1055 func (t *Torrent) havePiece(index pieceIndex) bool {
1056 return t.haveInfo() && t.pieceComplete(index)
1059 func (t *Torrent) maybeDropMutuallyCompletePeer(
1060 // I'm not sure about taking peer here, not all peer implementations actually drop. Maybe that's
1064 if !t.cl.config.DropMutuallyCompletePeers {
1067 if !t.haveAllPieces() {
1070 if all, known := p.peerHasAllPieces(); !(known && all) {
1076 t.logger.WithDefaultLevel(log.Debug).Printf("dropping %v, which is mutually complete", p)
1080 func (t *Torrent) haveChunk(r Request) (ret bool) {
1082 // log.Println("have chunk", r, ret)
1087 if t.pieceComplete(pieceIndex(r.Index)) {
1090 p := &t.pieces[r.Index]
1091 return !p.pendingChunk(r.ChunkSpec, t.chunkSize)
1094 func chunkIndexFromChunkSpec(cs ChunkSpec, chunkSize pp.Integer) chunkIndexType {
1095 return chunkIndexType(cs.Begin / chunkSize)
1098 func (t *Torrent) wantPieceIndex(index pieceIndex) bool {
1099 return t._pendingPieces.Contains(uint32(index))
1102 // A pool of []*PeerConn, to reduce allocations in functions that need to index or sort Torrent
1103 // conns (which is a map).
1104 var peerConnSlices sync.Pool
1106 func getPeerConnSlice(cap int) []*PeerConn {
1107 getInterface := peerConnSlices.Get()
1108 if getInterface == nil {
1109 return make([]*PeerConn, 0, cap)
1111 return getInterface.([]*PeerConn)[:0]
1115 // Calls the given function with a slice of unclosed conns. It uses a pool to reduce allocations as
1116 // this is a frequent occurrence.
1117 func (t *Torrent) withUnclosedConns(f func([]*PeerConn)) {
1118 sl := t.appendUnclosedConns(getPeerConnSlice(len(t.conns)))
1120 peerConnSlices.Put(sl)
1123 func (t *Torrent) worstBadConnFromSlice(opts worseConnLensOpts, sl []*PeerConn) *PeerConn {
1124 wcs := worseConnSlice{conns: sl}
1127 for wcs.Len() != 0 {
1128 c := heap.Pop(&wcs).(*PeerConn)
1129 if opts.incomingIsBad && !c.outgoing {
1132 if opts.outgoingIsBad && c.outgoing {
1135 if c._stats.ChunksReadWasted.Int64() >= 6 && c._stats.ChunksReadWasted.Int64() > c._stats.ChunksReadUseful.Int64() {
1138 // If the connection is in the worst half of the established
1139 // connection quota and is older than a minute.
1140 if wcs.Len() >= (t.maxEstablishedConns+1)/2 {
1141 // Give connections 1 minute to prove themselves.
1142 if time.Since(c.completedHandshake) > time.Minute {
1150 // The worst connection is one that hasn't been sent, or sent anything useful for the longest. A bad
1151 // connection is one that usually sends us unwanted pieces, or has been in the worse half of the
1152 // established connections for more than a minute. This is O(n log n). If there was a way to not
1153 // consider the position of a conn relative to the total number, it could be reduced to O(n).
1154 func (t *Torrent) worstBadConn(opts worseConnLensOpts) (ret *PeerConn) {
1155 t.withUnclosedConns(func(ucs []*PeerConn) {
1156 ret = t.worstBadConnFromSlice(opts, ucs)
1161 type PieceStateChange struct {
1166 func (t *Torrent) publishPieceChange(piece pieceIndex) {
1167 t.cl._mu.Defer(func() {
1168 cur := t.pieceState(piece)
1169 p := &t.pieces[piece]
1170 if cur != p.publicPieceState {
1171 p.publicPieceState = cur
1172 t.pieceStateChanges.Publish(PieceStateChange{
1180 func (t *Torrent) pieceNumPendingChunks(piece pieceIndex) pp.Integer {
1181 if t.pieceComplete(piece) {
1184 return pp.Integer(t.pieceNumChunks(piece) - t.pieces[piece].numDirtyChunks())
1187 func (t *Torrent) pieceAllDirty(piece pieceIndex) bool {
1188 return t.pieces[piece].allChunksDirty()
1191 func (t *Torrent) readersChanged() {
1192 t.updateReaderPieces()
1193 t.updateAllPiecePriorities("Torrent.readersChanged")
1196 func (t *Torrent) updateReaderPieces() {
1197 t._readerNowPieces, t._readerReadaheadPieces = t.readerPiecePriorities()
1200 func (t *Torrent) readerPosChanged(from, to pieceRange) {
1204 t.updateReaderPieces()
1205 // Order the ranges, high and low.
1207 if l.begin > h.begin {
1210 if l.end < h.begin {
1211 // Two distinct ranges.
1212 t.updatePiecePriorities(l.begin, l.end, "Torrent.readerPosChanged")
1213 t.updatePiecePriorities(h.begin, h.end, "Torrent.readerPosChanged")
1220 t.updatePiecePriorities(l.begin, end, "Torrent.readerPosChanged")
1224 func (t *Torrent) maybeNewConns() {
1225 // Tickle the accept routine.
1226 t.cl.event.Broadcast()
1230 func (t *Torrent) piecePriorityChanged(piece pieceIndex, reason string) {
1231 if t._pendingPieces.Contains(uint32(piece)) {
1232 t.iterPeers(func(c *Peer) {
1233 // if c.requestState.Interested {
1236 if !c.isLowOnRequests() {
1239 if !c.peerHasPiece(piece) {
1242 if c.requestState.Interested && c.peerChoking && !c.peerAllowedFast.Contains(piece) {
1245 c.updateRequests(reason)
1249 t.publishPieceChange(piece)
1252 func (t *Torrent) updatePiecePriority(piece pieceIndex, reason string) {
1253 if !t.closed.IsSet() {
1254 // It would be possible to filter on pure-priority changes here to avoid churning the piece
1256 t.updatePieceRequestOrder(piece)
1258 p := &t.pieces[piece]
1259 newPrio := p.uncachedPriority()
1260 // t.logger.Printf("torrent %p: piece %d: uncached priority: %v", t, piece, newPrio)
1261 if newPrio == PiecePriorityNone {
1262 if !t._pendingPieces.CheckedRemove(uint32(piece)) {
1266 if !t._pendingPieces.CheckedAdd(uint32(piece)) {
1270 t.piecePriorityChanged(piece, reason)
1273 func (t *Torrent) updateAllPiecePriorities(reason string) {
1274 t.updatePiecePriorities(0, t.numPieces(), reason)
1277 // Update all piece priorities in one hit. This function should have the same
1278 // output as updatePiecePriority, but across all pieces.
1279 func (t *Torrent) updatePiecePriorities(begin, end pieceIndex, reason string) {
1280 for i := begin; i < end; i++ {
1281 t.updatePiecePriority(i, reason)
1285 // Returns the range of pieces [begin, end) that contains the extent of bytes.
1286 func (t *Torrent) byteRegionPieces(off, size int64) (begin, end pieceIndex) {
1287 if off >= t.length() {
1297 begin = pieceIndex(off / t.info.PieceLength)
1298 end = pieceIndex((off + size + t.info.PieceLength - 1) / t.info.PieceLength)
1299 if end > pieceIndex(t.info.NumPieces()) {
1300 end = pieceIndex(t.info.NumPieces())
1305 // Returns true if all iterations complete without breaking. Returns the read regions for all
1306 // readers. The reader regions should not be merged as some callers depend on this method to
1307 // enumerate readers.
1308 func (t *Torrent) forReaderOffsetPieces(f func(begin, end pieceIndex) (more bool)) (all bool) {
1309 for r := range t.readers {
1311 if p.begin >= p.end {
1314 if !f(p.begin, p.end) {
1321 func (t *Torrent) piecePriority(piece pieceIndex) piecePriority {
1322 return t.piece(piece).uncachedPriority()
1325 func (t *Torrent) pendRequest(req RequestIndex) {
1326 t.piece(t.pieceIndexOfRequestIndex(req)).pendChunkIndex(req % t.chunksPerRegularPiece())
1329 func (t *Torrent) pieceCompletionChanged(piece pieceIndex, reason string) {
1330 t.cl.event.Broadcast()
1331 if t.pieceComplete(piece) {
1332 t.onPieceCompleted(piece)
1334 t.onIncompletePiece(piece)
1336 t.updatePiecePriority(piece, reason)
1339 func (t *Torrent) numReceivedConns() (ret int) {
1340 for c := range t.conns {
1341 if c.Discovery == PeerSourceIncoming {
1348 func (t *Torrent) numOutgoingConns() (ret int) {
1349 for c := range t.conns {
1357 func (t *Torrent) maxHalfOpen() int {
1358 // Note that if we somehow exceed the maximum established conns, we want
1359 // the negative value to have an effect.
1360 establishedHeadroom := int64(t.maxEstablishedConns - len(t.conns))
1361 extraIncoming := int64(t.numReceivedConns() - t.maxEstablishedConns/2)
1362 // We want to allow some experimentation with new peers, and to try to
1363 // upset an oversupply of received connections.
1365 max(5, extraIncoming)+establishedHeadroom,
1366 int64(t.cl.config.HalfOpenConnsPerTorrent),
1370 func (t *Torrent) openNewConns() (initiated int) {
1371 defer t.updateWantPeersEvent()
1372 for t.peers.Len() != 0 {
1373 if !t.wantOutgoingConns() {
1376 if len(t.halfOpen) >= t.maxHalfOpen() {
1379 if len(t.cl.dialers) == 0 {
1382 if t.cl.numHalfOpen >= t.cl.config.TotalHalfOpenConns {
1385 p := t.peers.PopMax()
1386 t.initiateConn(p, false, false, false, false)
1392 func (t *Torrent) updatePieceCompletion(piece pieceIndex) bool {
1394 uncached := t.pieceCompleteUncached(piece)
1395 cached := p.completion()
1396 changed := cached != uncached
1397 complete := uncached.Complete
1398 p.storageCompletionOk = uncached.Ok
1401 t._completedPieces.Add(x)
1404 t._completedPieces.Remove(x)
1406 p.t.updatePieceRequestOrder(piece)
1408 if complete && len(p.dirtiers) != 0 {
1409 t.logger.Printf("marked piece %v complete but still has dirtiers", piece)
1412 log.Fstr("piece %d completion changed: %+v -> %+v", piece, cached, uncached).LogLevel(log.Debug, t.logger)
1413 t.pieceCompletionChanged(piece, "Torrent.updatePieceCompletion")
1418 // Non-blocking read. Client lock is not required.
1419 func (t *Torrent) readAt(b []byte, off int64) (n int, err error) {
1421 p := &t.pieces[off/t.info.PieceLength]
1422 p.waitNoPendingWrites()
1424 n1, err = p.Storage().ReadAt(b, off-p.Info().Offset())
1435 // Returns an error if the metadata was completed, but couldn't be set for some reason. Blame it on
1436 // the last peer to contribute. TODO: Actually we shouldn't blame peers for failure to open storage
1437 // etc. Also we should probably cached metadata pieces per-Peer, to isolate failure appropriately.
1438 func (t *Torrent) maybeCompleteMetadata() error {
1443 if !t.haveAllMetadataPieces() {
1444 // Don't have enough metadata pieces.
1447 err := t.setInfoBytesLocked(t.metadataBytes)
1449 t.invalidateMetadata()
1450 return fmt.Errorf("error setting info bytes: %s", err)
1452 if t.cl.config.Debug {
1453 t.logger.Printf("%s: got metadata from peers", t)
1458 func (t *Torrent) readerPiecePriorities() (now, readahead bitmap.Bitmap) {
1459 t.forReaderOffsetPieces(func(begin, end pieceIndex) bool {
1461 now.Add(bitmap.BitIndex(begin))
1462 readahead.AddRange(bitmap.BitRange(begin)+1, bitmap.BitRange(end))
1469 func (t *Torrent) needData() bool {
1470 if t.closed.IsSet() {
1476 return !t._pendingPieces.IsEmpty()
1479 func appendMissingStrings(old, new []string) (ret []string) {
1482 for _, n := range new {
1483 for _, o := range old {
1488 ret = append(ret, n)
1493 func appendMissingTrackerTiers(existing [][]string, minNumTiers int) (ret [][]string) {
1495 for minNumTiers > len(ret) {
1496 ret = append(ret, nil)
1501 func (t *Torrent) addTrackers(announceList [][]string) {
1502 fullAnnounceList := &t.metainfo.AnnounceList
1503 t.metainfo.AnnounceList = appendMissingTrackerTiers(*fullAnnounceList, len(announceList))
1504 for tierIndex, trackerURLs := range announceList {
1505 (*fullAnnounceList)[tierIndex] = appendMissingStrings((*fullAnnounceList)[tierIndex], trackerURLs)
1507 t.startMissingTrackerScrapers()
1508 t.updateWantPeersEvent()
1511 // Don't call this before the info is available.
1512 func (t *Torrent) bytesCompleted() int64 {
1516 return t.length() - t.bytesLeft()
1519 func (t *Torrent) SetInfoBytes(b []byte) (err error) {
1522 return t.setInfoBytesLocked(b)
1525 // Returns true if connection is removed from torrent.Conns.
1526 func (t *Torrent) deletePeerConn(c *PeerConn) (ret bool) {
1527 if !c.closed.IsSet() {
1528 panic("connection is not closed")
1529 // There are behaviours prevented by the closed state that will fail
1530 // if the connection has been deleted.
1534 // Avoid adding a drop event more than once. Probably we should track whether we've generated
1535 // the drop event against the PexConnState instead.
1537 if !t.cl.config.DisablePEX {
1541 torrent.Add("deleted connections", 1)
1542 c.deleteAllRequests("Torrent.deletePeerConn")
1543 t.assertPendingRequests()
1544 if t.numActivePeers() == 0 && len(t.connsWithAllPieces) != 0 {
1545 panic(t.connsWithAllPieces)
1550 func (t *Torrent) decPeerPieceAvailability(p *Peer) {
1551 if t.deleteConnWithAllPieces(p) {
1557 p.peerPieces().Iterate(func(i uint32) bool {
1558 p.t.decPieceAvailability(pieceIndex(i))
1563 func (t *Torrent) assertPendingRequests() {
1567 // var actual pendingRequests
1568 // if t.haveInfo() {
1569 // actual.m = make([]int, t.numChunks())
1571 // t.iterPeers(func(p *Peer) {
1572 // p.requestState.Requests.Iterate(func(x uint32) bool {
1577 // diff := cmp.Diff(actual.m, t.pendingRequests.m)
1583 func (t *Torrent) dropConnection(c *PeerConn) {
1584 t.cl.event.Broadcast()
1586 if t.deletePeerConn(c) {
1591 // Peers as in contact information for dialing out.
1592 func (t *Torrent) wantPeers() bool {
1593 if t.closed.IsSet() {
1596 if t.peers.Len() > t.cl.config.TorrentPeersLowWater {
1599 return t.wantOutgoingConns()
1602 func (t *Torrent) updateWantPeersEvent() {
1604 t.wantPeersEvent.Set()
1606 t.wantPeersEvent.Clear()
1610 // Returns whether the client should make effort to seed the torrent.
1611 func (t *Torrent) seeding() bool {
1613 if t.closed.IsSet() {
1616 if t.dataUploadDisallowed {
1619 if cl.config.NoUpload {
1622 if !cl.config.Seed {
1625 if cl.config.DisableAggressiveUpload && t.needData() {
1631 func (t *Torrent) onWebRtcConn(
1632 c datachannel.ReadWriteCloser,
1633 dcc webtorrent.DataChannelContext,
1636 netConn := webrtcNetConn{
1638 DataChannelContext: dcc,
1640 peerRemoteAddr := netConn.RemoteAddr()
1641 //t.logger.Levelf(log.Critical, "onWebRtcConn remote addr: %v", peerRemoteAddr)
1642 if t.cl.badPeerAddr(peerRemoteAddr) {
1645 localAddrIpPort := missinggo.IpPortFromNetAddr(netConn.LocalAddr())
1646 pc, err := t.cl.initiateProtocolHandshakes(
1647 context.Background(),
1652 outgoing: dcc.LocalOffered,
1653 remoteAddr: peerRemoteAddr,
1654 localPublicAddr: localAddrIpPort,
1655 network: webrtcNetwork,
1656 connString: fmt.Sprintf("webrtc offer_id %x: %v", dcc.OfferId, regularNetConnPeerConnConnString(netConn)),
1660 t.logger.WithDefaultLevel(log.Error).Printf("error in handshaking webrtc connection: %v", err)
1663 if dcc.LocalOffered {
1664 pc.Discovery = PeerSourceTracker
1666 pc.Discovery = PeerSourceIncoming
1668 pc.conn.SetWriteDeadline(time.Time{})
1671 err = t.cl.runHandshookConn(pc, t)
1673 t.logger.WithDefaultLevel(log.Debug).Printf("error running handshook webrtc conn: %v", err)
1677 func (t *Torrent) logRunHandshookConn(pc *PeerConn, logAll bool, level log.Level) {
1678 err := t.cl.runHandshookConn(pc, t)
1679 if err != nil || logAll {
1680 t.logger.WithDefaultLevel(level).Levelf(log.ErrorLevel(err), "error running handshook conn: %v", err)
1684 func (t *Torrent) runHandshookConnLoggingErr(pc *PeerConn) {
1685 t.logRunHandshookConn(pc, false, log.Debug)
1688 func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer {
1689 wtc, release := t.cl.websocketTrackers.Get(u.String(), t.infoHash)
1690 // This needs to run before the Torrent is dropped from the Client, to prevent a new webtorrent.TrackerClient for
1691 // the same info hash before the old one is cleaned up.
1692 t.onClose = append(t.onClose, release)
1693 wst := websocketTrackerStatus{u, wtc}
1695 err := wtc.Announce(tracker.Started, t.infoHash)
1697 t.logger.WithDefaultLevel(log.Warning).Printf(
1698 "error in initial announce to %q: %v",
1706 func (t *Torrent) startScrapingTracker(_url string) {
1710 u, err := url.Parse(_url)
1712 // URLs with a leading '*' appear to be a uTorrent convention to
1713 // disable trackers.
1715 log.Str("error parsing tracker url").AddValues("url", _url).Log(t.logger)
1719 if u.Scheme == "udp" {
1721 t.startScrapingTracker(u.String())
1723 t.startScrapingTracker(u.String())
1726 if _, ok := t.trackerAnnouncers[_url]; ok {
1729 sl := func() torrentTrackerAnnouncer {
1732 if t.cl.config.DisableWebtorrent {
1735 return t.startWebsocketAnnouncer(*u)
1737 if t.cl.config.DisableIPv4Peers || t.cl.config.DisableIPv4 {
1741 if t.cl.config.DisableIPv6 {
1745 newAnnouncer := &trackerScraper{
1748 lookupTrackerIp: t.cl.config.LookupTrackerIp,
1750 go newAnnouncer.Run()
1756 if t.trackerAnnouncers == nil {
1757 t.trackerAnnouncers = make(map[string]torrentTrackerAnnouncer)
1759 t.trackerAnnouncers[_url] = sl
1762 // Adds and starts tracker scrapers for tracker URLs that aren't already
1764 func (t *Torrent) startMissingTrackerScrapers() {
1765 if t.cl.config.DisableTrackers {
1768 t.startScrapingTracker(t.metainfo.Announce)
1769 for _, tier := range t.metainfo.AnnounceList {
1770 for _, url := range tier {
1771 t.startScrapingTracker(url)
1776 // Returns an AnnounceRequest with fields filled out to defaults and current
1778 func (t *Torrent) announceRequest(event tracker.AnnounceEvent) tracker.AnnounceRequest {
1779 // Note that IPAddress is not set. It's set for UDP inside the tracker code, since it's
1780 // dependent on the network in use.
1781 return tracker.AnnounceRequest{
1783 NumWant: func() int32 {
1784 if t.wantPeers() && len(t.cl.dialers) > 0 {
1785 return 200 // Win has UDP packet limit. See: https://github.com/anacrolix/torrent/issues/764
1790 Port: uint16(t.cl.incomingPeerPort()),
1791 PeerId: t.cl.peerID,
1792 InfoHash: t.infoHash,
1793 Key: t.cl.announceKey(),
1795 // The following are vaguely described in BEP 3.
1797 Left: t.bytesLeftAnnounce(),
1798 Uploaded: t.stats.BytesWrittenData.Int64(),
1799 // There's no mention of wasted or unwanted download in the BEP.
1800 Downloaded: t.stats.BytesReadUsefulData.Int64(),
1804 // Adds peers revealed in an announce until the announce ends, or we have
1806 func (t *Torrent) consumeDhtAnnouncePeers(pvs <-chan dht.PeersValues) {
1808 for v := range pvs {
1811 for _, cp := range v.Peers {
1813 // Can't do anything with this.
1816 if t.addPeer(PeerInfo{
1817 Addr: ipPortAddr{cp.IP, cp.Port},
1818 Source: PeerSourceDhtGetPeers,
1825 // log.Printf("added %v peers from dht for %v", added, t.InfoHash().HexString())
1830 // Announce using the provided DHT server. Peers are consumed automatically. done is closed when the
1831 // announce ends. stop will force the announce to end.
1832 func (t *Torrent) AnnounceToDht(s DhtServer) (done <-chan struct{}, stop func(), err error) {
1833 ps, err := s.Announce(t.infoHash, t.cl.incomingPeerPort(), true)
1837 _done := make(chan struct{})
1841 t.consumeDhtAnnouncePeers(ps.Peers())
1847 func (t *Torrent) timeboxedAnnounceToDht(s DhtServer) error {
1848 _, stop, err := t.AnnounceToDht(s)
1853 case <-t.closed.Done():
1854 case <-time.After(5 * time.Minute):
1860 func (t *Torrent) dhtAnnouncer(s DhtServer) {
1866 if t.closed.IsSet() {
1869 // We're also announcing ourselves as a listener, so we don't just want peer addresses.
1870 // TODO: We can include the announce_peer step depending on whether we can receive
1871 // inbound connections. We should probably only announce once every 15 mins too.
1872 if !t.wantAnyConns() {
1875 // TODO: Determine if there's a listener on the port we're announcing.
1876 if len(cl.dialers) == 0 && len(cl.listeners) == 0 {
1887 err := t.timeboxedAnnounceToDht(s)
1889 t.logger.WithDefaultLevel(log.Warning).Printf("error announcing %q to DHT: %s", t, err)
1895 func (t *Torrent) addPeers(peers []PeerInfo) (added int) {
1896 for _, p := range peers {
1904 // The returned TorrentStats may require alignment in memory. See
1905 // https://github.com/anacrolix/torrent/issues/383.
1906 func (t *Torrent) Stats() TorrentStats {
1908 defer t.cl.rUnlock()
1909 return t.statsLocked()
1912 func (t *Torrent) statsLocked() (ret TorrentStats) {
1913 ret.ActivePeers = len(t.conns)
1914 ret.HalfOpenPeers = len(t.halfOpen)
1915 ret.PendingPeers = t.peers.Len()
1916 ret.TotalPeers = t.numTotalPeers()
1917 ret.ConnectedSeeders = 0
1918 for c := range t.conns {
1919 if all, ok := c.peerHasAllPieces(); all && ok {
1920 ret.ConnectedSeeders++
1923 ret.ConnStats = t.stats.Copy()
1924 ret.PiecesComplete = t.numPiecesCompleted()
1928 // The total number of peers in the torrent.
1929 func (t *Torrent) numTotalPeers() int {
1930 peers := make(map[string]struct{})
1931 for conn := range t.conns {
1932 ra := conn.conn.RemoteAddr()
1934 // It's been closed and doesn't support RemoteAddr.
1937 peers[ra.String()] = struct{}{}
1939 for addr := range t.halfOpen {
1940 peers[addr] = struct{}{}
1942 t.peers.Each(func(peer PeerInfo) {
1943 peers[peer.Addr.String()] = struct{}{}
1948 // Reconcile bytes transferred before connection was associated with a
1950 func (t *Torrent) reconcileHandshakeStats(c *PeerConn) {
1951 if c._stats != (ConnStats{
1952 // Handshakes should only increment these fields:
1953 BytesWritten: c._stats.BytesWritten,
1954 BytesRead: c._stats.BytesRead,
1958 c.postHandshakeStats(func(cs *ConnStats) {
1959 cs.BytesRead.Add(c._stats.BytesRead.Int64())
1960 cs.BytesWritten.Add(c._stats.BytesWritten.Int64())
1962 c.reconciledHandshakeStats = true
1965 // Returns true if the connection is added.
1966 func (t *Torrent) addPeerConn(c *PeerConn) (err error) {
1969 torrent.Add("added connections", 1)
1972 if t.closed.IsSet() {
1973 return errors.New("torrent closed")
1975 for c0 := range t.conns {
1976 if c.PeerID != c0.PeerID {
1979 if !t.cl.config.DropDuplicatePeerIds {
1982 if c.hasPreferredNetworkOver(c0) {
1984 t.deletePeerConn(c0)
1986 return errors.New("existing connection preferred")
1989 if len(t.conns) >= t.maxEstablishedConns {
1990 numOutgoing := t.numOutgoingConns()
1991 numIncoming := len(t.conns) - numOutgoing
1992 c := t.worstBadConn(worseConnLensOpts{
1993 // We've already established that we have too many connections at this point, so we just
1994 // need to match what kind we have too many of vs. what we're trying to add now.
1995 incomingIsBad: (numIncoming-numOutgoing > 1) && c.outgoing,
1996 outgoingIsBad: (numOutgoing-numIncoming > 1) && !c.outgoing,
1999 return errors.New("don't want conn")
2004 if len(t.conns) >= t.maxEstablishedConns {
2007 t.conns[c] = struct{}{}
2008 t.cl.event.Broadcast()
2009 // We'll never receive the "p" extended handshake parameter.
2010 if !t.cl.config.DisablePEX && !c.PeerExtensionBytes.SupportsExtended() {
2016 func (t *Torrent) newConnsAllowed() bool {
2017 if !t.networkingEnabled.Bool() {
2020 if t.closed.IsSet() {
2023 if !t.needData() && (!t.seeding() || !t.haveAnyPieces()) {
2029 func (t *Torrent) wantAnyConns() bool {
2030 if !t.networkingEnabled.Bool() {
2033 if t.closed.IsSet() {
2036 if !t.needData() && (!t.seeding() || !t.haveAnyPieces()) {
2039 return len(t.conns) < t.maxEstablishedConns
2042 func (t *Torrent) wantOutgoingConns() bool {
2043 if !t.newConnsAllowed() {
2046 if len(t.conns) < t.maxEstablishedConns {
2049 numIncomingConns := len(t.conns) - t.numOutgoingConns()
2050 return t.worstBadConn(worseConnLensOpts{
2051 incomingIsBad: numIncomingConns-t.numOutgoingConns() > 1,
2052 outgoingIsBad: false,
2056 func (t *Torrent) wantIncomingConns() bool {
2057 if !t.newConnsAllowed() {
2060 if len(t.conns) < t.maxEstablishedConns {
2063 numIncomingConns := len(t.conns) - t.numOutgoingConns()
2064 return t.worstBadConn(worseConnLensOpts{
2065 incomingIsBad: false,
2066 outgoingIsBad: t.numOutgoingConns()-numIncomingConns > 1,
2070 func (t *Torrent) SetMaxEstablishedConns(max int) (oldMax int) {
2073 oldMax = t.maxEstablishedConns
2074 t.maxEstablishedConns = max
2075 wcs := worseConnSlice{
2076 conns: t.appendConns(nil, func(*PeerConn) bool {
2080 wcs.initKeys(worseConnLensOpts{})
2082 for len(t.conns) > t.maxEstablishedConns && wcs.Len() > 0 {
2083 t.dropConnection(heap.Pop(&wcs).(*PeerConn))
2089 func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) {
2090 t.logger.LazyLog(log.Debug, func() log.Msg {
2091 return log.Fstr("hashed piece %d (passed=%t)", piece, passed)
2095 t.cl.event.Broadcast()
2096 if t.closed.IsSet() {
2100 // Don't score the first time a piece is hashed, it could be an initial check.
2101 if p.storageCompletionOk {
2103 pieceHashedCorrect.Add(1)
2106 "piece %d failed hash: %d connections contributed", piece, len(p.dirtiers),
2107 ).AddValues(t, p).LogLevel(
2109 log.Debug, t.logger)
2111 pieceHashedNotCorrect.Add(1)
2116 t.publishPieceChange(piece)
2119 t.publishPieceChange(piece)
2123 if len(p.dirtiers) != 0 {
2124 // Don't increment stats above connection-level for every involved connection.
2125 t.allStats((*ConnStats).incrementPiecesDirtiedGood)
2127 for c := range p.dirtiers {
2128 c._stats.incrementPiecesDirtiedGood()
2130 t.clearPieceTouchers(piece)
2131 hasDirty := p.hasDirtyChunks()
2134 p.Flush() // You can be synchronous here!
2136 err := p.Storage().MarkComplete()
2138 t.logger.Printf("%T: error marking piece complete %d: %s", t.storage, piece, err)
2142 if t.closed.IsSet() {
2145 t.pendAllChunkSpecs(piece)
2147 if len(p.dirtiers) != 0 && p.allChunksDirty() && hashIoErr == nil {
2148 // Peers contributed to all the data for this piece hash failure, and the failure was
2149 // not due to errors in the storage (such as data being dropped in a cache).
2151 // Increment Torrent and above stats, and then specific connections.
2152 t.allStats((*ConnStats).incrementPiecesDirtiedBad)
2153 for c := range p.dirtiers {
2154 // Y u do dis peer?!
2155 c.stats().incrementPiecesDirtiedBad()
2158 bannableTouchers := make([]*Peer, 0, len(p.dirtiers))
2159 for c := range p.dirtiers {
2161 bannableTouchers = append(bannableTouchers, c)
2164 t.clearPieceTouchers(piece)
2165 slices.Sort(bannableTouchers, connLessTrusted)
2167 if t.cl.config.Debug {
2169 "bannable conns by trust for piece %d: %v",
2171 func() (ret []connectionTrust) {
2172 for _, c := range bannableTouchers {
2173 ret = append(ret, c.trust())
2180 if len(bannableTouchers) >= 1 {
2181 c := bannableTouchers[0]
2182 if len(bannableTouchers) != 1 {
2183 t.logger.Levelf(log.Warning, "would have banned %v for touching piece %v after failed piece check", c.remoteIp(), piece)
2185 // Turns out it's still useful to ban peers like this because if there's only a
2186 // single peer for a piece, and we never progress that piece to completion, we
2187 // will never smart-ban them. Discovered in
2188 // https://github.com/anacrolix/torrent/issues/715.
2189 t.logger.Levelf(log.Warning, "banning %v for being sole dirtier of piece %v after failed piece check", c, piece)
2194 t.onIncompletePiece(piece)
2195 p.Storage().MarkNotComplete()
2197 t.updatePieceCompletion(piece)
2200 func (t *Torrent) cancelRequestsForPiece(piece pieceIndex) {
2201 start := t.pieceRequestIndexOffset(piece)
2202 end := start + t.pieceNumChunks(piece)
2203 for ri := start; ri < end; ri++ {
2208 func (t *Torrent) onPieceCompleted(piece pieceIndex) {
2209 t.pendAllChunkSpecs(piece)
2210 t.cancelRequestsForPiece(piece)
2211 t.piece(piece).readerCond.Broadcast()
2212 for conn := range t.conns {
2214 t.maybeDropMutuallyCompletePeer(&conn.Peer)
2218 // Called when a piece is found to be not complete.
2219 func (t *Torrent) onIncompletePiece(piece pieceIndex) {
2220 if t.pieceAllDirty(piece) {
2221 t.pendAllChunkSpecs(piece)
2223 if !t.wantPieceIndex(piece) {
2224 // t.logger.Printf("piece %d incomplete and unwanted", piece)
2227 // We could drop any connections that we told we have a piece that we
2228 // don't here. But there's a test failure, and it seems clients don't care
2229 // if you request pieces that you already claim to have. Pruning bad
2230 // connections might just remove any connections that aren't treating us
2231 // favourably anyway.
2233 // for c := range t.conns {
2234 // if c.sentHave(piece) {
2238 t.iterPeers(func(conn *Peer) {
2239 if conn.peerHasPiece(piece) {
2240 conn.updateRequests("piece incomplete")
2245 func (t *Torrent) tryCreateMorePieceHashers() {
2246 for !t.closed.IsSet() && t.activePieceHashes < 2 && t.tryCreatePieceHasher() {
2250 func (t *Torrent) tryCreatePieceHasher() bool {
2251 if t.storage == nil {
2254 pi, ok := t.getPieceToHash()
2259 t.piecesQueuedForHash.Remove(bitmap.BitIndex(pi))
2261 t.publishPieceChange(pi)
2262 t.updatePiecePriority(pi, "Torrent.tryCreatePieceHasher")
2263 t.storageLock.RLock()
2264 t.activePieceHashes++
2265 go t.pieceHasher(pi)
2269 func (t *Torrent) getPieceToHash() (ret pieceIndex, ok bool) {
2270 t.piecesQueuedForHash.IterTyped(func(i pieceIndex) bool {
2271 if t.piece(i).hashing {
2281 func (t *Torrent) dropBannedPeers() {
2282 t.iterPeers(func(p *Peer) {
2283 remoteIp := p.remoteIp()
2284 if remoteIp == nil {
2285 if p.bannableAddr.Ok {
2286 t.logger.WithDefaultLevel(log.Debug).Printf("can't get remote ip for peer %v", p)
2290 netipAddr := netip.MustParseAddr(remoteIp.String())
2291 if Some(netipAddr) != p.bannableAddr {
2292 t.logger.WithDefaultLevel(log.Debug).Printf(
2293 "peer remote ip does not match its bannable addr [peer=%v, remote ip=%v, bannable addr=%v]",
2294 p, remoteIp, p.bannableAddr)
2296 if _, ok := t.cl.badPeerIPs[netipAddr]; ok {
2297 // Should this be a close?
2299 t.logger.WithDefaultLevel(log.Debug).Printf("dropped %v for banned remote IP %v", p, netipAddr)
2304 func (t *Torrent) pieceHasher(index pieceIndex) {
2306 sum, failedPeers, copyErr := t.hashPiece(index)
2307 correct := sum == *p.hash
2311 log.Fmsg("piece %v (%s) hash failure copy error: %v", p, p.hash.HexString(), copyErr).Log(t.logger)
2313 t.storageLock.RUnlock()
2317 for peer := range failedPeers {
2318 t.cl.banPeerIP(peer.AsSlice())
2319 t.logger.WithDefaultLevel(log.Debug).Printf("smart banned %v for piece %v", peer, index)
2322 for ri := t.pieceRequestIndexOffset(index); ri < t.pieceRequestIndexOffset(index+1); ri++ {
2323 t.smartBanCache.ForgetBlock(ri)
2327 t.pieceHashed(index, correct, copyErr)
2328 t.updatePiecePriority(index, "Torrent.pieceHasher")
2329 t.activePieceHashes--
2330 t.tryCreateMorePieceHashers()
2333 // Return the connections that touched a piece, and clear the entries while doing it.
2334 func (t *Torrent) clearPieceTouchers(pi pieceIndex) {
2336 for c := range p.dirtiers {
2337 delete(c.peerTouchedPieces, pi)
2338 delete(p.dirtiers, c)
2342 func (t *Torrent) peersAsSlice() (ret []*Peer) {
2343 t.iterPeers(func(p *Peer) {
2344 ret = append(ret, p)
2349 func (t *Torrent) queuePieceCheck(pieceIndex pieceIndex) {
2350 piece := t.piece(pieceIndex)
2351 if piece.queuedForHash() {
2354 t.piecesQueuedForHash.Add(bitmap.BitIndex(pieceIndex))
2355 t.publishPieceChange(pieceIndex)
2356 t.updatePiecePriority(pieceIndex, "Torrent.queuePieceCheck")
2357 t.tryCreateMorePieceHashers()
2360 // Forces all the pieces to be re-hashed. See also Piece.VerifyData. This should not be called
2361 // before the Info is available.
2362 func (t *Torrent) VerifyData() {
2363 for i := pieceIndex(0); i < t.NumPieces(); i++ {
2364 t.Piece(i).VerifyData()
2368 func (t *Torrent) connectingToPeerAddr(addrStr string) bool {
2369 return len(t.halfOpen[addrStr]) != 0
2372 func (t *Torrent) hasPeerConnForAddr(x PeerRemoteAddr) bool {
2373 addrStr := x.String()
2374 for c := range t.conns {
2376 if ra.String() == addrStr {
2383 func (t *Torrent) getHalfOpenPath(
2385 attemptKey outgoingConnAttemptKey,
2386 ) nestedmaps.Path[*PeerInfo] {
2387 return nestedmaps.Next(nestedmaps.Next(nestedmaps.Begin(&t.halfOpen), addrStr), attemptKey)
2390 func (t *Torrent) addHalfOpen(addrStr string, attemptKey *PeerInfo) {
2391 path := t.getHalfOpenPath(addrStr, attemptKey)
2393 panic("should be unique")
2395 path.Set(attemptKey)
2399 // Start the process of connecting to the given peer for the given torrent if appropriate. I'm not
2400 // sure all the PeerInfo fields are being used.
2401 func (t *Torrent) initiateConn(
2403 requireRendezvous bool,
2404 skipHolepunchRendezvous bool,
2406 receivedHolepunchConnect bool,
2408 if peer.Id == t.cl.peerID {
2411 if t.cl.badPeerAddr(peer.Addr) && !peer.Trusted {
2415 addrStr := addr.String()
2417 if t.connectingToPeerAddr(addrStr) {
2421 if t.hasPeerConnForAddr(addr) {
2425 t.addHalfOpen(addrStr, attemptKey)
2426 go t.cl.outgoingConnection(
2430 requireRendezvous: requireRendezvous,
2431 skipHolepunchRendezvous: skipHolepunchRendezvous,
2432 receivedHolepunchConnect: receivedHolepunchConnect,
2440 // Adds a trusted, pending peer for each of the given Client's addresses. Typically used in tests to
2441 // quickly make one Client visible to the Torrent of another Client.
2442 func (t *Torrent) AddClientPeer(cl *Client) int {
2443 return t.AddPeers(func() (ps []PeerInfo) {
2444 for _, la := range cl.ListenAddrs() {
2445 ps = append(ps, PeerInfo{
2454 // All stats that include this Torrent. Useful when we want to increment ConnStats but not for every
2456 func (t *Torrent) allStats(f func(*ConnStats)) {
2461 func (t *Torrent) hashingPiece(i pieceIndex) bool {
2462 return t.pieces[i].hashing
2465 func (t *Torrent) pieceQueuedForHash(i pieceIndex) bool {
2466 return t.piecesQueuedForHash.Get(bitmap.BitIndex(i))
2469 func (t *Torrent) dialTimeout() time.Duration {
2470 return reducedDialTimeout(t.cl.config.MinDialTimeout, t.cl.config.NominalDialTimeout, t.cl.config.HalfOpenConnsPerTorrent, t.peers.Len())
2473 func (t *Torrent) piece(i int) *Piece {
2477 func (t *Torrent) onWriteChunkErr(err error) {
2478 if t.userOnWriteChunkErr != nil {
2479 go t.userOnWriteChunkErr(err)
2482 t.logger.WithDefaultLevel(log.Critical).Printf("default chunk write error handler: disabling data download")
2483 t.disallowDataDownloadLocked()
2486 func (t *Torrent) DisallowDataDownload() {
2487 t.disallowDataDownloadLocked()
2490 func (t *Torrent) disallowDataDownloadLocked() {
2491 t.dataDownloadDisallowed.Set()
2494 func (t *Torrent) AllowDataDownload() {
2495 t.dataDownloadDisallowed.Clear()
2498 // Enables uploading data, if it was disabled.
2499 func (t *Torrent) AllowDataUpload() {
2502 t.dataUploadDisallowed = false
2503 for c := range t.conns {
2504 c.updateRequests("allow data upload")
2508 // Disables uploading data, if it was enabled.
2509 func (t *Torrent) DisallowDataUpload() {
2512 t.dataUploadDisallowed = true
2513 for c := range t.conns {
2514 // TODO: This doesn't look right. Shouldn't we tickle writers to choke peers or something instead?
2515 c.updateRequests("disallow data upload")
2519 // Sets a handler that is called if there's an error writing a chunk to local storage. By default,
2520 // or if nil, a critical message is logged, and data download is disabled.
2521 func (t *Torrent) SetOnWriteChunkError(f func(error)) {
2524 t.userOnWriteChunkErr = f
2527 func (t *Torrent) iterPeers(f func(p *Peer)) {
2528 for pc := range t.conns {
2531 for _, ws := range t.webSeeds {
2536 func (t *Torrent) callbacks() *Callbacks {
2537 return &t.cl.config.Callbacks
2540 type AddWebSeedsOpt func(*webseed.Client)
2542 // Sets the WebSeed trailing path escaper for a webseed.Client.
2543 func WebSeedPathEscaper(custom webseed.PathEscaper) AddWebSeedsOpt {
2544 return func(c *webseed.Client) {
2545 c.PathEscaper = custom
2549 func (t *Torrent) AddWebSeeds(urls []string, opts ...AddWebSeedsOpt) {
2552 for _, u := range urls {
2553 t.addWebSeed(u, opts...)
2557 func (t *Torrent) addWebSeed(url string, opts ...AddWebSeedsOpt) {
2558 if t.cl.config.DisableWebseeds {
2561 if _, ok := t.webSeeds[url]; ok {
2564 // I don't think Go http supports pipelining requests. However, we can have more ready to go
2565 // right away. This value should be some multiple of the number of connections to a host. I
2566 // would expect that double maxRequests plus a bit would be appropriate. This value is based on
2567 // downloading Sintel (08ada5a7a6183aae1e09d831df6748d566095a10) from
2568 // "https://webtorrent.io/torrents/".
2569 const maxRequests = 16
2575 reconciledHandshakeStats: true,
2576 // This should affect how often we have to recompute requests for this peer. Note that
2577 // because we can request more than 1 thing at a time over HTTP, we will hit the low
2578 // requests mark more often, so recomputation is probably sooner than with regular peer
2579 // conns. ~4x maxRequests would be about right.
2580 PeerMaxRequests: 128,
2581 // TODO: Set ban prefix?
2582 RemoteAddr: remoteAddrFromUrl(url),
2583 callbacks: t.callbacks(),
2585 client: webseed.Client{
2586 HttpClient: t.cl.httpClient,
2588 ResponseBodyWrapper: func(r io.Reader) io.Reader {
2589 return &rateLimitedReader{
2590 l: t.cl.config.DownloadRateLimiter,
2595 activeRequests: make(map[Request]webseed.Request, maxRequests),
2597 ws.peer.initRequestState()
2598 for _, opt := range opts {
2601 ws.peer.initUpdateRequestsTimer()
2602 ws.requesterCond.L = t.cl.locker()
2603 for i := 0; i < maxRequests; i += 1 {
2606 for _, f := range t.callbacks().NewPeer {
2609 ws.peer.logger = t.logger.WithContextValue(&ws)
2610 ws.peer.peerImpl = &ws
2612 ws.onGotInfo(t.info)
2614 t.webSeeds[url] = &ws.peer
2617 func (t *Torrent) peerIsActive(p *Peer) (active bool) {
2618 t.iterPeers(func(p1 *Peer) {
2626 func (t *Torrent) requestIndexToRequest(ri RequestIndex) Request {
2627 index := t.pieceIndexOfRequestIndex(ri)
2630 t.piece(index).chunkIndexSpec(ri % t.chunksPerRegularPiece()),
2634 func (t *Torrent) requestIndexFromRequest(r Request) RequestIndex {
2635 return t.pieceRequestIndexOffset(pieceIndex(r.Index)) + RequestIndex(r.Begin/t.chunkSize)
2638 func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex {
2639 return RequestIndex(piece) * t.chunksPerRegularPiece()
2642 func (t *Torrent) updateComplete() {
2643 t.Complete.SetBool(t.haveAllPieces())
2646 func (t *Torrent) cancelRequest(r RequestIndex) *Peer {
2647 p := t.requestingPeer(r)
2651 // TODO: This is a check that an old invariant holds. It can be removed after some testing.
2652 //delete(t.pendingRequests, r)
2653 if _, ok := t.requestState[r]; ok {
2654 panic("expected request state to be gone")
2659 func (t *Torrent) requestingPeer(r RequestIndex) *Peer {
2660 return t.requestState[r].peer
2663 func (t *Torrent) addConnWithAllPieces(p *Peer) {
2664 if t.connsWithAllPieces == nil {
2665 t.connsWithAllPieces = make(map[*Peer]struct{}, t.maxEstablishedConns)
2667 t.connsWithAllPieces[p] = struct{}{}
2670 func (t *Torrent) deleteConnWithAllPieces(p *Peer) bool {
2671 _, ok := t.connsWithAllPieces[p]
2672 delete(t.connsWithAllPieces, p)
2676 func (t *Torrent) numActivePeers() int {
2677 return len(t.conns) + len(t.webSeeds)
2680 func (t *Torrent) hasStorageCap() bool {
2681 f := t.storage.Capacity
2689 func (t *Torrent) pieceIndexOfRequestIndex(ri RequestIndex) pieceIndex {
2690 return pieceIndex(ri / t.chunksPerRegularPiece())
2693 func (t *Torrent) iterUndirtiedRequestIndexesInPiece(
2694 reuseIter *typedRoaring.Iterator[RequestIndex],
2696 f func(RequestIndex),
2698 reuseIter.Initialize(&t.dirtyChunks)
2699 pieceRequestIndexOffset := t.pieceRequestIndexOffset(piece)
2700 iterBitmapUnsetInRange(
2702 pieceRequestIndexOffset, pieceRequestIndexOffset+t.pieceNumChunks(piece),
2707 type requestState struct {
2712 // Returns an error if a received chunk is out of bounds in someway.
2713 func (t *Torrent) checkValidReceiveChunk(r Request) error {
2715 return errors.New("torrent missing info")
2717 if int(r.Index) >= t.numPieces() {
2718 return fmt.Errorf("chunk index %v, torrent num pieces %v", r.Index, t.numPieces())
2720 pieceLength := t.pieceLength(pieceIndex(r.Index))
2721 if r.Begin >= pieceLength {
2722 return fmt.Errorf("chunk begins beyond end of piece (%v >= %v)", r.Begin, pieceLength)
2724 // We could check chunk lengths here, but chunk request size is not changed often, and tricky
2725 // for peers to manipulate as they need to send potentially large buffers to begin with. There
2726 // should be considerable checks elsewhere for this case due to the network overhead. We should
2727 // catch most of the overflow manipulation stuff by checking index and begin above.
2731 func (t *Torrent) peerConnsWithDialAddrPort(target netip.AddrPort) (ret []*PeerConn) {
2732 for pc := range t.conns {
2733 dialAddr, err := pc.remoteDialAddrPort()
2737 if dialAddr != target {
2740 ret = append(ret, pc)
2745 func makeUtHolepunchMsgForPeerConn(
2746 recipient *PeerConn,
2747 msgType utHolepunch.MsgType,
2748 addrPort netip.AddrPort,
2749 errCode utHolepunch.ErrCode,
2751 utHolepunchMsg := utHolepunch.Msg{
2756 extendedPayload, err := utHolepunchMsg.MarshalBinary()
2762 ExtendedID: MapMustGet(recipient.PeerExtensionIDs, utHolepunch.ExtensionName),
2763 ExtendedPayload: extendedPayload,
2767 func sendUtHolepunchMsg(
2769 msgType utHolepunch.MsgType,
2770 addrPort netip.AddrPort,
2771 errCode utHolepunch.ErrCode,
2773 pc.write(makeUtHolepunchMsgForPeerConn(pc, msgType, addrPort, errCode))
2776 func (t *Torrent) handleReceivedUtHolepunchMsg(msg utHolepunch.Msg, sender *PeerConn) error {
2777 switch msg.MsgType {
2778 case utHolepunch.Rendezvous:
2779 t.logger.Printf("got holepunch rendezvous request for %v from %p", msg.AddrPort, sender)
2780 sendMsg := sendUtHolepunchMsg
2781 senderAddrPort, err := sender.remoteDialAddrPort()
2783 sender.logger.Levelf(
2785 "error getting ut_holepunch rendezvous sender's dial address: %v",
2788 // There's no better error code. The sender's address itself is invalid. I don't see
2789 // this error message being appropriate anywhere else anyway.
2790 sendMsg(sender, utHolepunch.Error, msg.AddrPort, utHolepunch.NoSuchPeer)
2792 targets := t.peerConnsWithDialAddrPort(msg.AddrPort)
2793 if len(targets) == 0 {
2794 sendMsg(sender, utHolepunch.Error, msg.AddrPort, utHolepunch.NotConnected)
2797 for _, pc := range targets {
2798 if !pc.supportsExtension(utHolepunch.ExtensionName) {
2799 sendMsg(sender, utHolepunch.Error, msg.AddrPort, utHolepunch.NoSupport)
2802 sendMsg(sender, utHolepunch.Connect, msg.AddrPort, 0)
2803 sendMsg(pc, utHolepunch.Connect, senderAddrPort, 0)
2806 case utHolepunch.Connect:
2807 t.logger.Printf("got holepunch connect from %v for %v", sender, msg.AddrPort)
2808 rz, ok := t.utHolepunchRendezvous[msg.AddrPort]
2810 delete(rz.relays, sender)
2812 rz.relayCond.Broadcast()
2814 // If the rendezvous was removed because we timed out or already got a connect signal,
2815 // it doesn't hurt to try again.
2816 t.initiateConn(PeerInfo{
2818 Source: PeerSourceUtHolepunch,
2819 }, false, true, true, true)
2822 case utHolepunch.Error:
2823 rz, ok := t.utHolepunchRendezvous[msg.AddrPort]
2825 delete(rz.relays, sender)
2826 rz.relayCond.Broadcast()
2828 t.logger.Printf("received ut_holepunch error message from %v: %v", sender, msg.ErrCode)
2831 return fmt.Errorf("unhandled msg type %v", msg.MsgType)
2835 func (t *Torrent) startHolepunchRendezvous(addrPort netip.AddrPort) (rz *utHolepunchRendezvous, err error) {
2836 if MapContains(t.utHolepunchRendezvous, addrPort) {
2837 err = errors.New("rendezvous already exists")
2841 for pc := range t.conns {
2842 if !pc.supportsExtension(utHolepunch.ExtensionName) {
2845 if pc.supportsExtension(pp.ExtensionNamePex) {
2846 if !g.MapContains(pc.pex.remoteLiveConns, addrPort) {
2850 sendUtHolepunchMsg(pc, utHolepunch.Rendezvous, addrPort, 0)
2851 MakeMapIfNilAndSet(&rz.relays, pc, struct{}{})
2853 if len(rz.relays) == 0 {
2854 err = fmt.Errorf("no eligible relays")
2857 if !MakeMapIfNilAndSet(&t.utHolepunchRendezvous, addrPort, rz) {
2858 panic("expected to fail earlier if rendezvous already exists")
2863 func (t *Torrent) numHalfOpenAttempts() (num int) {
2864 for _, attempts := range t.halfOpen {
2865 num += len(attempts)