20 "github.com/RoaringBitmap/roaring"
21 "github.com/anacrolix/chansync"
22 "github.com/anacrolix/dht/v2"
23 "github.com/anacrolix/log"
24 "github.com/anacrolix/missinggo/perf"
25 "github.com/anacrolix/missinggo/pubsub"
26 "github.com/anacrolix/missinggo/slices"
27 "github.com/anacrolix/missinggo/v2"
28 "github.com/anacrolix/missinggo/v2/bitmap"
29 "github.com/anacrolix/missinggo/v2/prioritybitmap"
30 "github.com/anacrolix/multiless"
31 "github.com/anacrolix/sync"
32 "github.com/davecgh/go-spew/spew"
33 "github.com/pion/datachannel"
35 "github.com/anacrolix/torrent/bencode"
36 "github.com/anacrolix/torrent/common"
37 "github.com/anacrolix/torrent/metainfo"
38 pp "github.com/anacrolix/torrent/peer_protocol"
39 "github.com/anacrolix/torrent/segments"
40 "github.com/anacrolix/torrent/storage"
41 "github.com/anacrolix/torrent/tracker"
42 "github.com/anacrolix/torrent/webseed"
43 "github.com/anacrolix/torrent/webtorrent"
46 // Maintains state of torrent within a Client. Many methods should not be called before the info is
47 // available, see .Info and .GotInfo.
49 // Torrent-level aggregate statistics. First in struct to ensure 64-bit
50 // alignment. See #262.
55 networkingEnabled chansync.Flag
56 dataDownloadDisallowed chansync.Flag
57 dataUploadDisallowed bool
58 userOnWriteChunkErr func(error)
60 closed chansync.SetOnce
61 infoHash metainfo.Hash
63 // Values are the piece indices that changed.
64 pieceStateChanges *pubsub.PubSub
65 // The size of chunks to request from peers over the wire. This is
66 // normally 16KiB by convention these days.
69 // Total length of the torrent in bytes. Stored because it's not O(1) to
70 // get this from the info dict.
73 // The storage to open when the info dict becomes available.
74 storageOpener *storage.Client
75 // Storage for torrent data.
76 storage *storage.Torrent
77 // Read-locked for using storage, and write-locked for Closing.
78 storageLock sync.RWMutex
80 // TODO: Only announce stuff is used?
81 metainfo metainfo.MetaInfo
83 // The info dict. nil if we don't have it (yet).
85 fileIndex segments.Index
88 webSeeds map[string]*Peer
90 // Active peer connections, running message stream loops. TODO: Make this
91 // open (not-closed) connections only.
92 conns map[*PeerConn]struct{}
93 maxEstablishedConns int
94 // Set of addrs to which we're attempting to connect. Connections are
95 // half-open until all handshakes are completed.
96 halfOpen map[string]PeerInfo
98 // Reserve of peers to connect to. A peer can be both here and in the
99 // active connections if were told about the peer after connecting with
100 // them. That encourages us to reconnect to peers that are well known in
102 peers prioritizedPeers
103 // Whether we want to know to know more peers.
104 wantPeersEvent missinggo.Event
105 // An announcer for each tracker URL.
106 trackerAnnouncers map[string]torrentTrackerAnnouncer
107 // How many times we've initiated a DHT announce. TODO: Move into stats.
110 // Name used if the info name isn't available. Should be cleared when the
111 // Info does become available.
115 // The bencoded bytes of the info dict. This is actively manipulated if
116 // the info bytes aren't initially available, and we try to fetch them
119 // Each element corresponds to the 16KiB metadata pieces. If true, we have
120 // received that piece.
121 metadataCompletedChunks []bool
122 metadataChanged sync.Cond
124 // Closed when .Info is obtained.
125 gotMetainfoC chan struct{}
127 readers map[*reader]struct{}
128 _readerNowPieces bitmap.Bitmap
129 _readerReadaheadPieces bitmap.Bitmap
131 // A cache of pieces we need to get. Calculated from various piece and
132 // file priorities and completion states elsewhere.
133 _pendingPieces prioritybitmap.PriorityBitmap
134 // A cache of completed piece indices.
135 _completedPieces roaring.Bitmap
136 // Pieces that need to be hashed.
137 piecesQueuedForHash bitmap.Bitmap
138 activePieceHashes int
140 // A pool of piece priorities []int for assignment to new connections.
141 // These "inclinations" are used to give connections preference for
143 connPieceInclinationPool sync.Pool
145 // Count of each request across active connections.
146 pendingRequests map[RequestIndex]int
147 // Chunks we've written to since the corresponding piece was last checked.
148 dirtyChunks roaring.Bitmap
153 func (t *Torrent) pieceAvailabilityFromPeers(i pieceIndex) (count int) {
154 t.iterPeers(func(peer *Peer) {
155 if peer.peerHasPiece(i) {
162 func (t *Torrent) decPieceAvailability(i pieceIndex) {
167 if p.availability <= 0 {
168 panic(p.availability)
173 func (t *Torrent) incPieceAvailability(i pieceIndex) {
174 // If we don't the info, this should be reconciled when we do.
181 func (t *Torrent) readerNowPieces() bitmap.Bitmap {
182 return t._readerNowPieces
185 func (t *Torrent) readerReadaheadPieces() bitmap.Bitmap {
186 return t._readerReadaheadPieces
189 func (t *Torrent) ignorePieceForRequests(i pieceIndex) bool {
190 return !t.wantPieceIndex(i)
193 func (t *Torrent) pendingPieces() *prioritybitmap.PriorityBitmap {
194 return &t._pendingPieces
197 // Returns a channel that is closed when the Torrent is closed.
198 func (t *Torrent) Closed() chansync.Done {
199 return t.closed.Done()
202 // KnownSwarm returns the known subset of the peers in the Torrent's swarm, including active,
203 // pending, and half-open peers.
204 func (t *Torrent) KnownSwarm() (ks []PeerInfo) {
205 // Add pending peers to the list
206 t.peers.Each(func(peer PeerInfo) {
207 ks = append(ks, peer)
210 // Add half-open peers to the list
211 for _, peer := range t.halfOpen {
212 ks = append(ks, peer)
215 // Add active peers to the list
216 for conn := range t.conns {
218 ks = append(ks, PeerInfo{
220 Addr: conn.RemoteAddr,
221 Source: conn.Discovery,
222 // > If the connection is encrypted, that's certainly enough to set SupportsEncryption.
223 // > But if we're not connected to them with an encrypted connection, I couldn't say
224 // > what's appropriate. We can carry forward the SupportsEncryption value as we
225 // > received it from trackers/DHT/PEX, or just use the encryption state for the
226 // > connection. It's probably easiest to do the latter for now.
227 // https://github.com/anacrolix/torrent/pull/188
228 SupportsEncryption: conn.headerEncrypted,
235 func (t *Torrent) setChunkSize(size pp.Integer) {
237 t.chunkPool = sync.Pool{
238 New: func() interface{} {
239 b := make([]byte, size)
245 func (t *Torrent) pieceComplete(piece pieceIndex) bool {
246 return t._completedPieces.Contains(bitmap.BitIndex(piece))
249 func (t *Torrent) pieceCompleteUncached(piece pieceIndex) storage.Completion {
250 return t.pieces[piece].Storage().Completion()
253 // There's a connection to that address already.
254 func (t *Torrent) addrActive(addr string) bool {
255 if _, ok := t.halfOpen[addr]; ok {
258 for c := range t.conns {
260 if ra.String() == addr {
267 func (t *Torrent) appendUnclosedConns(ret []*PeerConn) []*PeerConn {
268 for c := range t.conns {
269 if !c.closed.IsSet() {
276 func (t *Torrent) addPeer(p PeerInfo) (added bool) {
278 torrent.Add(fmt.Sprintf("peers added by source %q", p.Source), 1)
279 if t.closed.IsSet() {
282 if ipAddr, ok := tryIpPortFromNetAddr(p.Addr); ok {
283 if cl.badPeerIPPort(ipAddr.IP, ipAddr.Port) {
284 torrent.Add("peers not added because of bad addr", 1)
285 // cl.logger.Printf("peers not added because of bad addr: %v", p)
289 if replaced, ok := t.peers.AddReturningReplacedPeer(p); ok {
290 torrent.Add("peers replaced", 1)
291 if !replaced.equal(p) {
292 t.logger.WithDefaultLevel(log.Debug).Printf("added %v replacing %v", p, replaced)
299 for t.peers.Len() > cl.config.TorrentPeersHighWater {
300 _, ok := t.peers.DeleteMin()
302 torrent.Add("excess reserve peers discarded", 1)
308 func (t *Torrent) invalidateMetadata() {
309 for i := 0; i < len(t.metadataCompletedChunks); i++ {
310 t.metadataCompletedChunks[i] = false
313 t.gotMetainfoC = make(chan struct{})
318 func (t *Torrent) saveMetadataPiece(index int, data []byte) {
322 if index >= len(t.metadataCompletedChunks) {
323 t.logger.Printf("%s: ignoring metadata piece %d", t, index)
326 copy(t.metadataBytes[(1<<14)*index:], data)
327 t.metadataCompletedChunks[index] = true
330 func (t *Torrent) metadataPieceCount() int {
331 return (len(t.metadataBytes) + (1 << 14) - 1) / (1 << 14)
334 func (t *Torrent) haveMetadataPiece(piece int) bool {
336 return (1<<14)*piece < len(t.metadataBytes)
338 return piece < len(t.metadataCompletedChunks) && t.metadataCompletedChunks[piece]
342 func (t *Torrent) metadataSize() int {
343 return len(t.metadataBytes)
346 func infoPieceHashes(info *metainfo.Info) (ret [][]byte) {
347 for i := 0; i < len(info.Pieces); i += sha1.Size {
348 ret = append(ret, info.Pieces[i:i+sha1.Size])
353 func (t *Torrent) makePieces() {
354 hashes := infoPieceHashes(t.info)
355 t.pieces = make([]Piece, len(hashes))
356 for i, hash := range hashes {
357 piece := &t.pieces[i]
359 piece.index = pieceIndex(i)
360 piece.noPendingWrites.L = &piece.pendingWritesMutex
361 piece.hash = (*metainfo.Hash)(unsafe.Pointer(&hash[0]))
363 beginFile := pieceFirstFileIndex(piece.torrentBeginOffset(), files)
364 endFile := pieceEndFileIndex(piece.torrentEndOffset(), files)
365 piece.files = files[beginFile:endFile]
369 // Returns the index of the first file containing the piece. files must be
370 // ordered by offset.
371 func pieceFirstFileIndex(pieceOffset int64, files []*File) int {
372 for i, f := range files {
373 if f.offset+f.length > pieceOffset {
380 // Returns the index after the last file containing the piece. files must be
381 // ordered by offset.
382 func pieceEndFileIndex(pieceEndOffset int64, files []*File) int {
383 for i, f := range files {
384 if f.offset+f.length >= pieceEndOffset {
391 func (t *Torrent) cacheLength() {
393 for _, f := range t.info.UpvertedFiles() {
399 // TODO: This shouldn't fail for storage reasons. Instead we should handle storage failure
401 func (t *Torrent) setInfo(info *metainfo.Info) error {
402 if err := validateInfo(info); err != nil {
403 return fmt.Errorf("bad info: %s", err)
405 if t.storageOpener != nil {
407 t.storage, err = t.storageOpener.OpenTorrent(info, t.infoHash)
409 return fmt.Errorf("error opening torrent storage: %s", err)
415 t.fileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
416 t.displayName = "" // Save a few bytes lol.
423 // This seems to be all the follow-up tasks after info is set, that can't fail.
424 func (t *Torrent) onSetInfo() {
425 t.iterPeers(func(p *Peer) {
428 for i := range t.pieces {
430 // Need to add availability before updating piece completion, as that may result in conns
432 if p.availability != 0 {
433 panic(p.availability)
435 p.availability = int64(t.pieceAvailabilityFromPeers(i))
436 t.updatePieceCompletion(pieceIndex(i))
437 if !p.storageCompletionOk {
438 // t.logger.Printf("piece %s completion unknown, queueing check", p)
439 t.queuePieceCheck(pieceIndex(i))
442 t.cl.event.Broadcast()
443 close(t.gotMetainfoC)
444 t.updateWantPeersEvent()
445 t.pendingRequests = make(map[RequestIndex]int)
446 t.tryCreateMorePieceHashers()
449 // Called when metadata for a torrent becomes available.
450 func (t *Torrent) setInfoBytesLocked(b []byte) error {
451 if metainfo.HashBytes(b) != t.infoHash {
452 return errors.New("info bytes have wrong hash")
454 var info metainfo.Info
455 if err := bencode.Unmarshal(b, &info); err != nil {
456 return fmt.Errorf("error unmarshalling info bytes: %s", err)
459 t.metadataCompletedChunks = nil
463 if err := t.setInfo(&info); err != nil {
470 func (t *Torrent) haveAllMetadataPieces() bool {
474 if t.metadataCompletedChunks == nil {
477 for _, have := range t.metadataCompletedChunks {
485 // TODO: Propagate errors to disconnect peer.
486 func (t *Torrent) setMetadataSize(size int) (err error) {
488 // We already know the correct metadata size.
491 if uint32(size) > maxMetadataSize {
492 return errors.New("bad size")
494 if len(t.metadataBytes) == size {
497 t.metadataBytes = make([]byte, size)
498 t.metadataCompletedChunks = make([]bool, (size+(1<<14)-1)/(1<<14))
499 t.metadataChanged.Broadcast()
500 for c := range t.conns {
501 c.requestPendingMetadata()
506 // The current working name for the torrent. Either the name in the info dict,
507 // or a display name given such as by the dn value in a magnet link, or "".
508 func (t *Torrent) name() string {
510 defer t.nameMu.RUnlock()
514 if t.displayName != "" {
517 return "infohash:" + t.infoHash.HexString()
520 func (t *Torrent) pieceState(index pieceIndex) (ret PieceState) {
521 p := &t.pieces[index]
522 ret.Priority = t.piecePriority(index)
523 ret.Completion = p.completion()
524 ret.QueuedForHash = p.queuedForHash()
525 ret.Hashing = p.hashing
526 ret.Checking = ret.QueuedForHash || ret.Hashing
527 ret.Marking = p.marking
528 if !ret.Complete && t.piecePartiallyDownloaded(index) {
534 func (t *Torrent) metadataPieceSize(piece int) int {
535 return metadataPieceSize(len(t.metadataBytes), piece)
538 func (t *Torrent) newMetadataExtensionMessage(c *PeerConn, msgType pp.ExtendedMetadataRequestMsgType, piece int, data []byte) pp.Message {
541 ExtendedID: c.PeerExtensionIDs[pp.ExtensionNameMetadata],
542 ExtendedPayload: append(bencode.MustMarshal(pp.ExtendedMetadataRequestMsg{
544 TotalSize: len(t.metadataBytes),
550 type pieceAvailabilityRun struct {
555 func (me pieceAvailabilityRun) String() string {
556 return fmt.Sprintf("%v(%v)", me.count, me.availability)
559 func (t *Torrent) pieceAvailabilityRuns() (ret []pieceAvailabilityRun) {
560 rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
561 ret = append(ret, pieceAvailabilityRun{availability: el.(int64), count: int(count)})
563 for i := range t.pieces {
564 rle.Append(t.pieces[i].availability, 1)
570 func (t *Torrent) pieceStateRuns() (ret PieceStateRuns) {
571 rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
572 ret = append(ret, PieceStateRun{
573 PieceState: el.(PieceState),
577 for index := range t.pieces {
578 rle.Append(t.pieceState(pieceIndex(index)), 1)
584 // Produces a small string representing a PieceStateRun.
585 func (psr PieceStateRun) String() (ret string) {
586 ret = fmt.Sprintf("%d", psr.Length)
587 ret += func() string {
588 switch psr.Priority {
589 case PiecePriorityNext:
591 case PiecePriorityNormal:
593 case PiecePriorityReadahead:
595 case PiecePriorityNow:
597 case PiecePriorityHigh:
606 if psr.QueuedForHash {
624 func (t *Torrent) writeStatus(w io.Writer) {
625 fmt.Fprintf(w, "Infohash: %s\n", t.infoHash.HexString())
626 fmt.Fprintf(w, "Metadata length: %d\n", t.metadataSize())
628 fmt.Fprintf(w, "Metadata have: ")
629 for _, h := range t.metadataCompletedChunks {
630 fmt.Fprintf(w, "%c", func() rune {
640 fmt.Fprintf(w, "Piece length: %s\n",
643 return fmt.Sprintf("%v (%v chunks)",
645 float64(t.usualPieceSize())/float64(t.chunkSize))
652 fmt.Fprintf(w, "Num Pieces: %d (%d completed)\n", t.numPieces(), t.numPiecesCompleted())
653 fmt.Fprintf(w, "Piece States: %s\n", t.pieceStateRuns())
654 fmt.Fprintf(w, "Piece availability: %v\n", strings.Join(func() (ret []string) {
655 for _, run := range t.pieceAvailabilityRuns() {
656 ret = append(ret, run.String())
661 fmt.Fprintf(w, "Reader Pieces:")
662 t.forReaderOffsetPieces(func(begin, end pieceIndex) (again bool) {
663 fmt.Fprintf(w, " %d:%d", begin, end)
668 fmt.Fprintf(w, "Enabled trackers:\n")
670 tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
671 fmt.Fprintf(tw, " URL\tExtra\n")
672 for _, ta := range slices.Sort(slices.FromMapElems(t.trackerAnnouncers), func(l, r torrentTrackerAnnouncer) bool {
675 var luns, runs url.URL = *lu, *ru
678 var ml missinggo.MultiLess
679 ml.StrictNext(luns.String() == runs.String(), luns.String() < runs.String())
680 ml.StrictNext(lu.String() == ru.String(), lu.String() < ru.String())
682 }).([]torrentTrackerAnnouncer) {
683 fmt.Fprintf(tw, " %q\t%v\n", ta.URL(), ta.statusLine())
688 fmt.Fprintf(w, "DHT Announces: %d\n", t.numDHTAnnounces)
690 spew.NewDefaultConfig()
691 spew.Fdump(w, t.statsLocked())
693 peers := t.peersAsSlice()
694 sort.Slice(peers, func(_i, _j int) bool {
697 if less, ok := multiless.New().EagerSameLess(
698 i.downloadRate() == j.downloadRate(), i.downloadRate() < j.downloadRate(),
702 return worseConn(i, j)
704 for i, c := range peers {
705 fmt.Fprintf(w, "%2d. ", i+1)
710 func (t *Torrent) haveInfo() bool {
714 // Returns a run-time generated MetaInfo that includes the info bytes and
715 // announce-list as currently known to the client.
716 func (t *Torrent) newMetaInfo() metainfo.MetaInfo {
717 return metainfo.MetaInfo{
718 CreationDate: time.Now().Unix(),
719 Comment: "dynamic metainfo from client",
720 CreatedBy: "go.torrent",
721 AnnounceList: t.metainfo.UpvertedAnnounceList().Clone(),
722 InfoBytes: func() []byte {
724 return t.metadataBytes
729 UrlList: func() []string {
730 ret := make([]string, 0, len(t.webSeeds))
731 for url := range t.webSeeds {
732 ret = append(ret, url)
740 func (t *Torrent) BytesMissing() (n int64) {
742 n = t.bytesMissingLocked()
747 func (t *Torrent) bytesMissingLocked() int64 {
751 func iterFlipped(b *roaring.Bitmap, end uint64, cb func(uint32) bool) {
752 roaring.Flip(b, 0, end).Iterate(cb)
755 func (t *Torrent) bytesLeft() (left int64) {
756 iterFlipped(&t._completedPieces, uint64(t.numPieces()), func(x uint32) bool {
757 p := t.piece(pieceIndex(x))
758 left += int64(p.length() - p.numDirtyBytes())
764 // Bytes left to give in tracker announces.
765 func (t *Torrent) bytesLeftAnnounce() int64 {
773 func (t *Torrent) piecePartiallyDownloaded(piece pieceIndex) bool {
774 if t.pieceComplete(piece) {
777 if t.pieceAllDirty(piece) {
780 return t.pieces[piece].hasDirtyChunks()
783 func (t *Torrent) usualPieceSize() int {
784 return int(t.info.PieceLength)
787 func (t *Torrent) numPieces() pieceIndex {
788 return pieceIndex(t.info.NumPieces())
791 func (t *Torrent) numPiecesCompleted() (num pieceIndex) {
792 return pieceIndex(t._completedPieces.GetCardinality())
795 func (t *Torrent) close(wg *sync.WaitGroup) (err error) {
797 if t.storage != nil {
802 defer t.storageLock.Unlock()
803 if f := t.storage.Close; f != nil {
806 t.logger.WithDefaultLevel(log.Warning).Printf("error closing storage: %v", err1)
811 t.iterPeers(func(p *Peer) {
815 t.cl.event.Broadcast()
816 t.pieceStateChanges.Close()
817 t.updateWantPeersEvent()
821 func (t *Torrent) requestOffset(r Request) int64 {
822 return torrentRequestOffset(*t.length, int64(t.usualPieceSize()), r)
825 // Return the request that would include the given offset into the torrent data. Returns !ok if
826 // there is no such request.
827 func (t *Torrent) offsetRequest(off int64) (req Request, ok bool) {
828 return torrentOffsetRequest(*t.length, t.info.PieceLength, int64(t.chunkSize), off)
831 func (t *Torrent) writeChunk(piece int, begin int64, data []byte) (err error) {
832 defer perf.ScopeTimerErr(&err)()
833 n, err := t.pieces[piece].Storage().WriteAt(data, begin)
834 if err == nil && n != len(data) {
835 err = io.ErrShortWrite
840 func (t *Torrent) bitfield() (bf []bool) {
841 bf = make([]bool, t.numPieces())
842 t._completedPieces.Iterate(func(piece uint32) (again bool) {
849 func (t *Torrent) pieceNumChunks(piece pieceIndex) chunkIndexType {
850 return chunkIndexType((t.pieceLength(piece) + t.chunkSize - 1) / t.chunkSize)
853 func (t *Torrent) chunksPerRegularPiece() uint32 {
854 return uint32((pp.Integer(t.usualPieceSize()) + t.chunkSize - 1) / t.chunkSize)
857 func (t *Torrent) pendAllChunkSpecs(pieceIndex pieceIndex) {
858 t.dirtyChunks.RemoveRange(
859 uint64(t.pieceRequestIndexOffset(pieceIndex)),
860 uint64(t.pieceRequestIndexOffset(pieceIndex+1)))
863 func (t *Torrent) pieceLength(piece pieceIndex) pp.Integer {
864 if t.info.PieceLength == 0 {
865 // There will be no variance amongst pieces. Only pain.
868 if piece == t.numPieces()-1 {
869 ret := pp.Integer(*t.length % t.info.PieceLength)
874 return pp.Integer(t.info.PieceLength)
877 func (t *Torrent) hashPiece(piece pieceIndex) (ret metainfo.Hash, err error) {
879 p.waitNoPendingWrites()
880 storagePiece := t.pieces[piece].Storage()
882 //Does the backend want to do its own hashing?
883 if i, ok := storagePiece.PieceImpl.(storage.SelfHashing); ok {
884 var sum metainfo.Hash
885 //log.Printf("A piece decided to self-hash: %d", piece)
886 sum, err = i.SelfHash()
887 missinggo.CopyExact(&ret, sum)
891 hash := pieceHash.New()
892 const logPieceContents = false
893 if logPieceContents {
894 var examineBuf bytes.Buffer
895 _, err = storagePiece.WriteTo(io.MultiWriter(hash, &examineBuf))
896 log.Printf("hashed %q with copy err %v", examineBuf.Bytes(), err)
898 _, err = storagePiece.WriteTo(hash)
900 missinggo.CopyExact(&ret, hash.Sum(nil))
904 func (t *Torrent) haveAnyPieces() bool {
905 return t._completedPieces.GetCardinality() != 0
908 func (t *Torrent) haveAllPieces() bool {
912 return t._completedPieces.GetCardinality() == bitmap.BitRange(t.numPieces())
915 func (t *Torrent) havePiece(index pieceIndex) bool {
916 return t.haveInfo() && t.pieceComplete(index)
919 func (t *Torrent) maybeDropMutuallyCompletePeer(
920 // I'm not sure about taking peer here, not all peer implementations actually drop. Maybe that's okay?
923 if !t.cl.config.DropMutuallyCompletePeers {
926 if !t.haveAllPieces() {
929 if all, known := p.peerHasAllPieces(); !(known && all) {
935 t.logger.WithDefaultLevel(log.Debug).Printf("dropping %v, which is mutually complete", p)
939 func (t *Torrent) haveChunk(r Request) (ret bool) {
941 // log.Println("have chunk", r, ret)
946 if t.pieceComplete(pieceIndex(r.Index)) {
949 p := &t.pieces[r.Index]
950 return !p.pendingChunk(r.ChunkSpec, t.chunkSize)
953 func chunkIndexFromChunkSpec(cs ChunkSpec, chunkSize pp.Integer) chunkIndexType {
954 return chunkIndexType(cs.Begin / chunkSize)
957 func (t *Torrent) wantPieceIndex(index pieceIndex) bool {
958 // TODO: Are these overly conservative, should we be guarding this here?
963 if index < 0 || index >= t.numPieces() {
967 p := &t.pieces[index]
968 if p.queuedForHash() {
974 if t.pieceComplete(index) {
977 if t._pendingPieces.Contains(int(index)) {
980 // t.logger.Printf("piece %d not pending", index)
981 return !t.forReaderOffsetPieces(func(begin, end pieceIndex) bool {
982 return index < begin || index >= end
986 // A pool of []*PeerConn, to reduce allocations in functions that need to index or sort Torrent
987 // conns (which is a map).
988 var peerConnSlices sync.Pool
990 // The worst connection is one that hasn't been sent, or sent anything useful for the longest. A bad
991 // connection is one that usually sends us unwanted pieces, or has been in the worse half of the
992 // established connections for more than a minute. This is O(n log n). If there was a way to not
993 // consider the position of a conn relative to the total number, it could be reduced to O(n).
994 func (t *Torrent) worstBadConn() (ret *PeerConn) {
996 getInterface := peerConnSlices.Get()
997 if getInterface == nil {
998 sl = make([]*PeerConn, 0, len(t.conns))
1000 sl = getInterface.([]*PeerConn)[:0]
1002 sl = t.appendUnclosedConns(sl)
1003 defer peerConnSlices.Put(sl)
1004 wcs := worseConnSlice{sl}
1006 for wcs.Len() != 0 {
1007 c := heap.Pop(&wcs).(*PeerConn)
1008 if c._stats.ChunksReadWasted.Int64() >= 6 && c._stats.ChunksReadWasted.Int64() > c._stats.ChunksReadUseful.Int64() {
1011 // If the connection is in the worst half of the established
1012 // connection quota and is older than a minute.
1013 if wcs.Len() >= (t.maxEstablishedConns+1)/2 {
1014 // Give connections 1 minute to prove themselves.
1015 if time.Since(c.completedHandshake) > time.Minute {
1023 type PieceStateChange struct {
1028 func (t *Torrent) publishPieceChange(piece pieceIndex) {
1029 t.cl._mu.Defer(func() {
1030 cur := t.pieceState(piece)
1031 p := &t.pieces[piece]
1032 if cur != p.publicPieceState {
1033 p.publicPieceState = cur
1034 t.pieceStateChanges.Publish(PieceStateChange{
1042 func (t *Torrent) pieceNumPendingChunks(piece pieceIndex) pp.Integer {
1043 if t.pieceComplete(piece) {
1046 return pp.Integer(t.pieceNumChunks(piece) - t.pieces[piece].numDirtyChunks())
1049 func (t *Torrent) pieceAllDirty(piece pieceIndex) bool {
1050 return t.pieces[piece].allChunksDirty()
1053 func (t *Torrent) readersChanged() {
1054 t.updateReaderPieces()
1055 t.updateAllPiecePriorities()
1058 func (t *Torrent) updateReaderPieces() {
1059 t._readerNowPieces, t._readerReadaheadPieces = t.readerPiecePriorities()
1062 func (t *Torrent) readerPosChanged(from, to pieceRange) {
1066 t.updateReaderPieces()
1067 // Order the ranges, high and low.
1069 if l.begin > h.begin {
1072 if l.end < h.begin {
1073 // Two distinct ranges.
1074 t.updatePiecePriorities(l.begin, l.end)
1075 t.updatePiecePriorities(h.begin, h.end)
1082 t.updatePiecePriorities(l.begin, end)
1086 func (t *Torrent) maybeNewConns() {
1087 // Tickle the accept routine.
1088 t.cl.event.Broadcast()
1092 func (t *Torrent) piecePriorityChanged(piece pieceIndex) {
1093 // t.logger.Printf("piece %d priority changed", piece)
1094 t.iterPeers(func(c *Peer) {
1095 if c.updatePiecePriority(piece) {
1096 // log.Print("conn piece priority changed")
1101 t.publishPieceChange(piece)
1104 func (t *Torrent) updatePiecePriority(piece pieceIndex) {
1105 p := &t.pieces[piece]
1106 newPrio := p.uncachedPriority()
1107 // t.logger.Printf("torrent %p: piece %d: uncached priority: %v", t, piece, newPrio)
1108 if newPrio == PiecePriorityNone {
1109 if !t._pendingPieces.Remove(int(piece)) {
1113 if !t._pendingPieces.Set(int(piece), newPrio.BitmapPriority()) {
1117 t.piecePriorityChanged(piece)
1120 func (t *Torrent) updateAllPiecePriorities() {
1121 t.updatePiecePriorities(0, t.numPieces())
1124 // Update all piece priorities in one hit. This function should have the same
1125 // output as updatePiecePriority, but across all pieces.
1126 func (t *Torrent) updatePiecePriorities(begin, end pieceIndex) {
1127 for i := begin; i < end; i++ {
1128 t.updatePiecePriority(i)
1132 // Returns the range of pieces [begin, end) that contains the extent of bytes.
1133 func (t *Torrent) byteRegionPieces(off, size int64) (begin, end pieceIndex) {
1134 if off >= *t.length {
1144 begin = pieceIndex(off / t.info.PieceLength)
1145 end = pieceIndex((off + size + t.info.PieceLength - 1) / t.info.PieceLength)
1146 if end > pieceIndex(t.info.NumPieces()) {
1147 end = pieceIndex(t.info.NumPieces())
1152 // Returns true if all iterations complete without breaking. Returns the read
1153 // regions for all readers. The reader regions should not be merged as some
1154 // callers depend on this method to enumerate readers.
1155 func (t *Torrent) forReaderOffsetPieces(f func(begin, end pieceIndex) (more bool)) (all bool) {
1156 for r := range t.readers {
1158 if p.begin >= p.end {
1161 if !f(p.begin, p.end) {
1168 func (t *Torrent) piecePriority(piece pieceIndex) piecePriority {
1169 prio, ok := t._pendingPieces.GetPriority(piece)
1171 return PiecePriorityNone
1176 ret := piecePriority(-prio)
1177 if ret == PiecePriorityNone {
1183 func (t *Torrent) pendRequest(req RequestIndex) {
1184 t.piece(int(req / t.chunksPerRegularPiece())).pendChunkIndex(req % t.chunksPerRegularPiece())
1187 func (t *Torrent) pieceCompletionChanged(piece pieceIndex) {
1188 t.cl.event.Broadcast()
1189 if t.pieceComplete(piece) {
1190 t.onPieceCompleted(piece)
1192 t.onIncompletePiece(piece)
1194 t.updatePiecePriority(piece)
1197 func (t *Torrent) numReceivedConns() (ret int) {
1198 for c := range t.conns {
1199 if c.Discovery == PeerSourceIncoming {
1206 func (t *Torrent) maxHalfOpen() int {
1207 // Note that if we somehow exceed the maximum established conns, we want
1208 // the negative value to have an effect.
1209 establishedHeadroom := int64(t.maxEstablishedConns - len(t.conns))
1210 extraIncoming := int64(t.numReceivedConns() - t.maxEstablishedConns/2)
1211 // We want to allow some experimentation with new peers, and to try to
1212 // upset an oversupply of received connections.
1213 return int(min(max(5, extraIncoming)+establishedHeadroom, int64(t.cl.config.HalfOpenConnsPerTorrent)))
1216 func (t *Torrent) openNewConns() (initiated int) {
1217 defer t.updateWantPeersEvent()
1218 for t.peers.Len() != 0 {
1222 if len(t.halfOpen) >= t.maxHalfOpen() {
1225 if len(t.cl.dialers) == 0 {
1228 if t.cl.numHalfOpen >= t.cl.config.TotalHalfOpenConns {
1231 p := t.peers.PopMax()
1238 func (t *Torrent) getConnPieceInclination() []int {
1239 _ret := t.connPieceInclinationPool.Get()
1241 pieceInclinationsNew.Add(1)
1242 return rand.Perm(int(t.numPieces()))
1244 pieceInclinationsReused.Add(1)
1245 return *_ret.(*[]int)
1248 func (t *Torrent) putPieceInclination(pi []int) {
1249 t.connPieceInclinationPool.Put(&pi)
1250 pieceInclinationsPut.Add(1)
1253 func (t *Torrent) updatePieceCompletion(piece pieceIndex) bool {
1255 uncached := t.pieceCompleteUncached(piece)
1256 cached := p.completion()
1257 changed := cached != uncached
1258 complete := uncached.Complete
1259 p.storageCompletionOk = uncached.Ok
1262 t._completedPieces.Add(x)
1264 t._completedPieces.Remove(x)
1266 if complete && len(p.dirtiers) != 0 {
1267 t.logger.Printf("marked piece %v complete but still has dirtiers", piece)
1270 log.Fstr("piece %d completion changed: %+v -> %+v", piece, cached, uncached).SetLevel(log.Debug).Log(t.logger)
1271 t.pieceCompletionChanged(piece)
1276 // Non-blocking read. Client lock is not required.
1277 func (t *Torrent) readAt(b []byte, off int64) (n int, err error) {
1279 p := &t.pieces[off/t.info.PieceLength]
1280 p.waitNoPendingWrites()
1282 n1, err = p.Storage().ReadAt(b, off-p.Info().Offset())
1293 // Returns an error if the metadata was completed, but couldn't be set for some reason. Blame it on
1294 // the last peer to contribute. TODO: Actually we shouldn't blame peers for failure to open storage
1295 // etc. Also we should probably cached metadata pieces per-Peer, to isolate failure appropriately.
1296 func (t *Torrent) maybeCompleteMetadata() error {
1301 if !t.haveAllMetadataPieces() {
1302 // Don't have enough metadata pieces.
1305 err := t.setInfoBytesLocked(t.metadataBytes)
1307 t.invalidateMetadata()
1308 return fmt.Errorf("error setting info bytes: %s", err)
1310 if t.cl.config.Debug {
1311 t.logger.Printf("%s: got metadata from peers", t)
1316 func (t *Torrent) readerPiecePriorities() (now, readahead bitmap.Bitmap) {
1317 t.forReaderOffsetPieces(func(begin, end pieceIndex) bool {
1319 now.Add(bitmap.BitIndex(begin))
1320 readahead.AddRange(bitmap.BitRange(begin)+1, bitmap.BitRange(end))
1327 func (t *Torrent) needData() bool {
1328 if t.closed.IsSet() {
1334 return t._pendingPieces.Len() != 0
1337 func appendMissingStrings(old, new []string) (ret []string) {
1340 for _, n := range new {
1341 for _, o := range old {
1346 ret = append(ret, n)
1351 func appendMissingTrackerTiers(existing [][]string, minNumTiers int) (ret [][]string) {
1353 for minNumTiers > len(ret) {
1354 ret = append(ret, nil)
1359 func (t *Torrent) addTrackers(announceList [][]string) {
1360 fullAnnounceList := &t.metainfo.AnnounceList
1361 t.metainfo.AnnounceList = appendMissingTrackerTiers(*fullAnnounceList, len(announceList))
1362 for tierIndex, trackerURLs := range announceList {
1363 (*fullAnnounceList)[tierIndex] = appendMissingStrings((*fullAnnounceList)[tierIndex], trackerURLs)
1365 t.startMissingTrackerScrapers()
1366 t.updateWantPeersEvent()
1369 // Don't call this before the info is available.
1370 func (t *Torrent) bytesCompleted() int64 {
1374 return *t.length - t.bytesLeft()
1377 func (t *Torrent) SetInfoBytes(b []byte) (err error) {
1380 return t.setInfoBytesLocked(b)
1383 // Returns true if connection is removed from torrent.Conns.
1384 func (t *Torrent) deletePeerConn(c *PeerConn) (ret bool) {
1385 if !c.closed.IsSet() {
1386 panic("connection is not closed")
1387 // There are behaviours prevented by the closed state that will fail
1388 // if the connection has been deleted.
1392 // Avoid adding a drop event more than once. Probably we should track whether we've generated
1393 // the drop event against the PexConnState instead.
1395 if !t.cl.config.DisablePEX {
1399 torrent.Add("deleted connections", 1)
1400 c.deleteAllRequests()
1401 if t.numActivePeers() == 0 {
1402 t.assertNoPendingRequests()
1407 func (t *Torrent) decPeerPieceAvailability(p *Peer) {
1411 p.newPeerPieces().Iterate(func(i uint32) bool {
1412 p.t.decPieceAvailability(pieceIndex(i))
1417 func (t *Torrent) numActivePeers() (num int) {
1418 t.iterPeers(func(*Peer) {
1424 func (t *Torrent) assertNoPendingRequests() {
1425 if len(t.pendingRequests) != 0 {
1426 panic(t.pendingRequests)
1428 //if len(t.lastRequested) != 0 {
1429 // panic(t.lastRequested)
1433 func (t *Torrent) dropConnection(c *PeerConn) {
1434 t.cl.event.Broadcast()
1436 if t.deletePeerConn(c) {
1441 func (t *Torrent) wantPeers() bool {
1442 if t.closed.IsSet() {
1445 if t.peers.Len() > t.cl.config.TorrentPeersLowWater {
1448 return t.needData() || t.seeding()
1451 func (t *Torrent) updateWantPeersEvent() {
1453 t.wantPeersEvent.Set()
1455 t.wantPeersEvent.Clear()
1459 // Returns whether the client should make effort to seed the torrent.
1460 func (t *Torrent) seeding() bool {
1462 if t.closed.IsSet() {
1465 if t.dataUploadDisallowed {
1468 if cl.config.NoUpload {
1471 if !cl.config.Seed {
1474 if cl.config.DisableAggressiveUpload && t.needData() {
1480 func (t *Torrent) onWebRtcConn(
1481 c datachannel.ReadWriteCloser,
1482 dcc webtorrent.DataChannelContext,
1485 pc, err := t.cl.initiateProtocolHandshakes(
1486 context.Background(),
1487 webrtcNetConn{c, dcc},
1491 webrtcNetAddr{dcc.Remote},
1493 fmt.Sprintf("webrtc offer_id %x", dcc.OfferId),
1496 t.logger.WithDefaultLevel(log.Error).Printf("error in handshaking webrtc connection: %v", err)
1499 if dcc.LocalOffered {
1500 pc.Discovery = PeerSourceTracker
1502 pc.Discovery = PeerSourceIncoming
1506 err = t.cl.runHandshookConn(pc, t)
1508 t.logger.WithDefaultLevel(log.Critical).Printf("error running handshook webrtc conn: %v", err)
1512 func (t *Torrent) logRunHandshookConn(pc *PeerConn, logAll bool, level log.Level) {
1513 err := t.cl.runHandshookConn(pc, t)
1514 if err != nil || logAll {
1515 t.logger.WithDefaultLevel(level).Printf("error running handshook conn: %v", err)
1519 func (t *Torrent) runHandshookConnLoggingErr(pc *PeerConn) {
1520 t.logRunHandshookConn(pc, false, log.Debug)
1523 func (t *Torrent) startWebsocketAnnouncer(u url.URL) torrentTrackerAnnouncer {
1524 wtc, release := t.cl.websocketTrackers.Get(u.String())
1529 wst := websocketTrackerStatus{u, wtc}
1531 err := wtc.Announce(tracker.Started, t.infoHash)
1533 t.logger.WithDefaultLevel(log.Warning).Printf(
1534 "error in initial announce to %q: %v",
1543 func (t *Torrent) startScrapingTracker(_url string) {
1547 u, err := url.Parse(_url)
1549 // URLs with a leading '*' appear to be a uTorrent convention to
1550 // disable trackers.
1552 log.Str("error parsing tracker url").AddValues("url", _url).Log(t.logger)
1556 if u.Scheme == "udp" {
1558 t.startScrapingTracker(u.String())
1560 t.startScrapingTracker(u.String())
1563 if _, ok := t.trackerAnnouncers[_url]; ok {
1566 sl := func() torrentTrackerAnnouncer {
1569 if t.cl.config.DisableWebtorrent {
1572 return t.startWebsocketAnnouncer(*u)
1574 if t.cl.config.DisableIPv4Peers || t.cl.config.DisableIPv4 {
1578 if t.cl.config.DisableIPv6 {
1582 newAnnouncer := &trackerScraper{
1586 go newAnnouncer.Run()
1592 if t.trackerAnnouncers == nil {
1593 t.trackerAnnouncers = make(map[string]torrentTrackerAnnouncer)
1595 t.trackerAnnouncers[_url] = sl
1598 // Adds and starts tracker scrapers for tracker URLs that aren't already
1600 func (t *Torrent) startMissingTrackerScrapers() {
1601 if t.cl.config.DisableTrackers {
1604 t.startScrapingTracker(t.metainfo.Announce)
1605 for _, tier := range t.metainfo.AnnounceList {
1606 for _, url := range tier {
1607 t.startScrapingTracker(url)
1612 // Returns an AnnounceRequest with fields filled out to defaults and current
1614 func (t *Torrent) announceRequest(event tracker.AnnounceEvent) tracker.AnnounceRequest {
1615 // Note that IPAddress is not set. It's set for UDP inside the tracker code, since it's
1616 // dependent on the network in use.
1617 return tracker.AnnounceRequest{
1619 NumWant: func() int32 {
1620 if t.wantPeers() && len(t.cl.dialers) > 0 {
1626 Port: uint16(t.cl.incomingPeerPort()),
1627 PeerId: t.cl.peerID,
1628 InfoHash: t.infoHash,
1629 Key: t.cl.announceKey(),
1631 // The following are vaguely described in BEP 3.
1633 Left: t.bytesLeftAnnounce(),
1634 Uploaded: t.stats.BytesWrittenData.Int64(),
1635 // There's no mention of wasted or unwanted download in the BEP.
1636 Downloaded: t.stats.BytesReadUsefulData.Int64(),
1640 // Adds peers revealed in an announce until the announce ends, or we have
1642 func (t *Torrent) consumeDhtAnnouncePeers(pvs <-chan dht.PeersValues) {
1644 for v := range pvs {
1647 for _, cp := range v.Peers {
1649 // Can't do anything with this.
1652 if t.addPeer(PeerInfo{
1653 Addr: ipPortAddr{cp.IP, cp.Port},
1654 Source: PeerSourceDhtGetPeers,
1661 // log.Printf("added %v peers from dht for %v", added, t.InfoHash().HexString())
1666 // Announce using the provided DHT server. Peers are consumed automatically. done is closed when the
1667 // announce ends. stop will force the announce to end.
1668 func (t *Torrent) AnnounceToDht(s DhtServer) (done <-chan struct{}, stop func(), err error) {
1669 ps, err := s.Announce(t.infoHash, t.cl.incomingPeerPort(), true)
1673 _done := make(chan struct{})
1677 t.consumeDhtAnnouncePeers(ps.Peers())
1683 func (t *Torrent) timeboxedAnnounceToDht(s DhtServer) error {
1684 _, stop, err := t.AnnounceToDht(s)
1689 case <-t.closed.Done():
1690 case <-time.After(5 * time.Minute):
1696 func (t *Torrent) dhtAnnouncer(s DhtServer) {
1702 if t.closed.IsSet() {
1708 // TODO: Determine if there's a listener on the port we're announcing.
1709 if len(cl.dialers) == 0 && len(cl.listeners) == 0 {
1720 err := t.timeboxedAnnounceToDht(s)
1722 t.logger.WithDefaultLevel(log.Warning).Printf("error announcing %q to DHT: %s", t, err)
1728 func (t *Torrent) addPeers(peers []PeerInfo) (added int) {
1729 for _, p := range peers {
1737 // The returned TorrentStats may require alignment in memory. See
1738 // https://github.com/anacrolix/torrent/issues/383.
1739 func (t *Torrent) Stats() TorrentStats {
1741 defer t.cl.rUnlock()
1742 return t.statsLocked()
1745 func (t *Torrent) statsLocked() (ret TorrentStats) {
1746 ret.ActivePeers = len(t.conns)
1747 ret.HalfOpenPeers = len(t.halfOpen)
1748 ret.PendingPeers = t.peers.Len()
1749 ret.TotalPeers = t.numTotalPeers()
1750 ret.ConnectedSeeders = 0
1751 for c := range t.conns {
1752 if all, ok := c.peerHasAllPieces(); all && ok {
1753 ret.ConnectedSeeders++
1756 ret.ConnStats = t.stats.Copy()
1760 // The total number of peers in the torrent.
1761 func (t *Torrent) numTotalPeers() int {
1762 peers := make(map[string]struct{})
1763 for conn := range t.conns {
1764 ra := conn.conn.RemoteAddr()
1766 // It's been closed and doesn't support RemoteAddr.
1769 peers[ra.String()] = struct{}{}
1771 for addr := range t.halfOpen {
1772 peers[addr] = struct{}{}
1774 t.peers.Each(func(peer PeerInfo) {
1775 peers[peer.Addr.String()] = struct{}{}
1780 // Reconcile bytes transferred before connection was associated with a
1782 func (t *Torrent) reconcileHandshakeStats(c *PeerConn) {
1783 if c._stats != (ConnStats{
1784 // Handshakes should only increment these fields:
1785 BytesWritten: c._stats.BytesWritten,
1786 BytesRead: c._stats.BytesRead,
1790 c.postHandshakeStats(func(cs *ConnStats) {
1791 cs.BytesRead.Add(c._stats.BytesRead.Int64())
1792 cs.BytesWritten.Add(c._stats.BytesWritten.Int64())
1794 c.reconciledHandshakeStats = true
1797 // Returns true if the connection is added.
1798 func (t *Torrent) addPeerConn(c *PeerConn) (err error) {
1801 torrent.Add("added connections", 1)
1804 if t.closed.IsSet() {
1805 return errors.New("torrent closed")
1807 for c0 := range t.conns {
1808 if c.PeerID != c0.PeerID {
1811 if !t.cl.config.DropDuplicatePeerIds {
1814 if left, ok := c.hasPreferredNetworkOver(c0); ok && left {
1816 t.deletePeerConn(c0)
1818 return errors.New("existing connection preferred")
1821 if len(t.conns) >= t.maxEstablishedConns {
1822 c := t.worstBadConn()
1824 return errors.New("don't want conns")
1829 if len(t.conns) >= t.maxEstablishedConns {
1832 t.conns[c] = struct{}{}
1833 if !t.cl.config.DisablePEX && !c.PeerExtensionBytes.SupportsExtended() {
1834 t.pex.Add(c) // as no further extended handshake expected
1839 func (t *Torrent) wantConns() bool {
1840 if !t.networkingEnabled.Bool() {
1843 if t.closed.IsSet() {
1846 if !t.seeding() && !t.needData() {
1849 if len(t.conns) < t.maxEstablishedConns {
1852 return t.worstBadConn() != nil
1855 func (t *Torrent) SetMaxEstablishedConns(max int) (oldMax int) {
1858 oldMax = t.maxEstablishedConns
1859 t.maxEstablishedConns = max
1860 wcs := slices.HeapInterface(slices.FromMapKeys(t.conns), func(l, r *PeerConn) bool {
1861 return worseConn(&l.Peer, &r.Peer)
1863 for len(t.conns) > t.maxEstablishedConns && wcs.Len() > 0 {
1864 t.dropConnection(wcs.Pop().(*PeerConn))
1870 func (t *Torrent) pieceHashed(piece pieceIndex, passed bool, hashIoErr error) {
1871 t.logger.Log(log.Fstr("hashed piece %d (passed=%t)", piece, passed).SetLevel(log.Debug))
1874 t.cl.event.Broadcast()
1875 if t.closed.IsSet() {
1879 // Don't score the first time a piece is hashed, it could be an initial check.
1880 if p.storageCompletionOk {
1882 pieceHashedCorrect.Add(1)
1885 "piece %d failed hash: %d connections contributed", piece, len(p.dirtiers),
1886 ).AddValues(t, p).SetLevel(log.Debug).Log(t.logger)
1887 pieceHashedNotCorrect.Add(1)
1892 t.publishPieceChange(piece)
1895 t.publishPieceChange(piece)
1899 if len(p.dirtiers) != 0 {
1900 // Don't increment stats above connection-level for every involved connection.
1901 t.allStats((*ConnStats).incrementPiecesDirtiedGood)
1903 for c := range p.dirtiers {
1904 c._stats.incrementPiecesDirtiedGood()
1906 t.clearPieceTouchers(piece)
1908 err := p.Storage().MarkComplete()
1910 t.logger.Printf("%T: error marking piece complete %d: %s", t.storage, piece, err)
1914 if t.closed.IsSet() {
1917 t.pendAllChunkSpecs(piece)
1919 if len(p.dirtiers) != 0 && p.allChunksDirty() && hashIoErr == nil {
1920 // Peers contributed to all the data for this piece hash failure, and the failure was
1921 // not due to errors in the storage (such as data being dropped in a cache).
1923 // Increment Torrent and above stats, and then specific connections.
1924 t.allStats((*ConnStats).incrementPiecesDirtiedBad)
1925 for c := range p.dirtiers {
1926 // Y u do dis peer?!
1927 c.stats().incrementPiecesDirtiedBad()
1930 bannableTouchers := make([]*Peer, 0, len(p.dirtiers))
1931 for c := range p.dirtiers {
1933 bannableTouchers = append(bannableTouchers, c)
1936 t.clearPieceTouchers(piece)
1937 slices.Sort(bannableTouchers, connLessTrusted)
1939 if t.cl.config.Debug {
1941 "bannable conns by trust for piece %d: %v",
1943 func() (ret []connectionTrust) {
1944 for _, c := range bannableTouchers {
1945 ret = append(ret, c.trust())
1952 if len(bannableTouchers) >= 1 {
1953 c := bannableTouchers[0]
1954 t.cl.banPeerIP(c.remoteIp())
1958 t.onIncompletePiece(piece)
1959 p.Storage().MarkNotComplete()
1961 t.updatePieceCompletion(piece)
1964 func (t *Torrent) cancelRequestsForPiece(piece pieceIndex) {
1965 // TODO: Make faster
1966 for cn := range t.conns {
1971 func (t *Torrent) onPieceCompleted(piece pieceIndex) {
1972 t.pendAllChunkSpecs(piece)
1973 t.cancelRequestsForPiece(piece)
1974 t.piece(piece).readerCond.Broadcast()
1975 for conn := range t.conns {
1977 t.maybeDropMutuallyCompletePeer(&conn.Peer)
1981 // Called when a piece is found to be not complete.
1982 func (t *Torrent) onIncompletePiece(piece pieceIndex) {
1983 if t.pieceAllDirty(piece) {
1984 t.pendAllChunkSpecs(piece)
1986 if !t.wantPieceIndex(piece) {
1987 // t.logger.Printf("piece %d incomplete and unwanted", piece)
1990 // We could drop any connections that we told we have a piece that we
1991 // don't here. But there's a test failure, and it seems clients don't care
1992 // if you request pieces that you already claim to have. Pruning bad
1993 // connections might just remove any connections that aren't treating us
1994 // favourably anyway.
1996 // for c := range t.conns {
1997 // if c.sentHave(piece) {
2001 t.iterPeers(func(conn *Peer) {
2002 if conn.peerHasPiece(piece) {
2003 conn.updateRequests()
2008 func (t *Torrent) tryCreateMorePieceHashers() {
2009 for !t.closed.IsSet() && t.activePieceHashes < 2 && t.tryCreatePieceHasher() {
2013 func (t *Torrent) tryCreatePieceHasher() bool {
2014 if t.storage == nil {
2017 pi, ok := t.getPieceToHash()
2022 t.piecesQueuedForHash.Remove(bitmap.BitIndex(pi))
2024 t.publishPieceChange(pi)
2025 t.updatePiecePriority(pi)
2026 t.storageLock.RLock()
2027 t.activePieceHashes++
2028 go t.pieceHasher(pi)
2032 func (t *Torrent) getPieceToHash() (ret pieceIndex, ok bool) {
2033 t.piecesQueuedForHash.IterTyped(func(i pieceIndex) bool {
2034 if t.piece(i).hashing {
2044 func (t *Torrent) pieceHasher(index pieceIndex) {
2046 sum, copyErr := t.hashPiece(index)
2047 correct := sum == *p.hash
2051 log.Fmsg("piece %v (%s) hash failure copy error: %v", p, p.hash.HexString(), copyErr).Log(t.logger)
2053 t.storageLock.RUnlock()
2057 t.updatePiecePriority(index)
2058 t.pieceHashed(index, correct, copyErr)
2059 t.publishPieceChange(index)
2060 t.activePieceHashes--
2061 t.tryCreateMorePieceHashers()
2064 // Return the connections that touched a piece, and clear the entries while doing it.
2065 func (t *Torrent) clearPieceTouchers(pi pieceIndex) {
2067 for c := range p.dirtiers {
2068 delete(c.peerTouchedPieces, pi)
2069 delete(p.dirtiers, c)
2073 func (t *Torrent) peersAsSlice() (ret []*Peer) {
2074 t.iterPeers(func(p *Peer) {
2075 ret = append(ret, p)
2080 func (t *Torrent) queuePieceCheck(pieceIndex pieceIndex) {
2081 piece := t.piece(pieceIndex)
2082 if piece.queuedForHash() {
2085 t.piecesQueuedForHash.Add(bitmap.BitIndex(pieceIndex))
2086 t.publishPieceChange(pieceIndex)
2087 t.updatePiecePriority(pieceIndex)
2088 t.tryCreateMorePieceHashers()
2091 // Forces all the pieces to be re-hashed. See also Piece.VerifyData. This should not be called
2092 // before the Info is available.
2093 func (t *Torrent) VerifyData() {
2094 for i := pieceIndex(0); i < t.NumPieces(); i++ {
2095 t.Piece(i).VerifyData()
2099 // Start the process of connecting to the given peer for the given torrent if appropriate.
2100 func (t *Torrent) initiateConn(peer PeerInfo) {
2101 if peer.Id == t.cl.peerID {
2104 if t.cl.badPeerAddr(peer.Addr) && !peer.Trusted {
2108 if t.addrActive(addr.String()) {
2112 t.halfOpen[addr.String()] = peer
2113 go t.cl.outgoingConnection(t, addr, peer.Source, peer.Trusted)
2116 // Adds a trusted, pending peer for each of the given Client's addresses. Typically used in tests to
2117 // quickly make one Client visible to the Torrent of another Client.
2118 func (t *Torrent) AddClientPeer(cl *Client) int {
2119 return t.AddPeers(func() (ps []PeerInfo) {
2120 for _, la := range cl.ListenAddrs() {
2121 ps = append(ps, PeerInfo{
2130 // All stats that include this Torrent. Useful when we want to increment ConnStats but not for every
2132 func (t *Torrent) allStats(f func(*ConnStats)) {
2137 func (t *Torrent) hashingPiece(i pieceIndex) bool {
2138 return t.pieces[i].hashing
2141 func (t *Torrent) pieceQueuedForHash(i pieceIndex) bool {
2142 return t.piecesQueuedForHash.Get(bitmap.BitIndex(i))
2145 func (t *Torrent) dialTimeout() time.Duration {
2146 return reducedDialTimeout(t.cl.config.MinDialTimeout, t.cl.config.NominalDialTimeout, t.cl.config.HalfOpenConnsPerTorrent, t.peers.Len())
2149 func (t *Torrent) piece(i int) *Piece {
2153 func (t *Torrent) onWriteChunkErr(err error) {
2154 if t.userOnWriteChunkErr != nil {
2155 go t.userOnWriteChunkErr(err)
2158 t.logger.WithDefaultLevel(log.Critical).Printf("default chunk write error handler: disabling data download")
2159 t.disallowDataDownloadLocked()
2162 func (t *Torrent) DisallowDataDownload() {
2163 t.disallowDataDownloadLocked()
2166 func (t *Torrent) disallowDataDownloadLocked() {
2167 t.dataDownloadDisallowed.Set()
2170 func (t *Torrent) AllowDataDownload() {
2171 t.dataDownloadDisallowed.Clear()
2174 // Enables uploading data, if it was disabled.
2175 func (t *Torrent) AllowDataUpload() {
2178 t.dataUploadDisallowed = false
2179 for c := range t.conns {
2184 // Disables uploading data, if it was enabled.
2185 func (t *Torrent) DisallowDataUpload() {
2188 t.dataUploadDisallowed = true
2189 for c := range t.conns {
2194 // Sets a handler that is called if there's an error writing a chunk to local storage. By default,
2195 // or if nil, a critical message is logged, and data download is disabled.
2196 func (t *Torrent) SetOnWriteChunkError(f func(error)) {
2199 t.userOnWriteChunkErr = f
2202 func (t *Torrent) iterPeers(f func(p *Peer)) {
2203 for pc := range t.conns {
2206 for _, ws := range t.webSeeds {
2211 func (t *Torrent) callbacks() *Callbacks {
2212 return &t.cl.config.Callbacks
2215 var WebseedHttpClient = &http.Client{
2216 Transport: &http.Transport{
2217 MaxConnsPerHost: 10,
2221 func (t *Torrent) addWebSeed(url string) {
2222 if t.cl.config.DisableWebseeds {
2225 if _, ok := t.webSeeds[url]; ok {
2228 const maxRequests = 10
2234 reconciledHandshakeStats: true,
2235 // TODO: Raise this limit, and instead limit concurrent fetches.
2236 PeerMaxRequests: 32,
2237 RemoteAddr: remoteAddrFromUrl(url),
2238 callbacks: t.callbacks(),
2240 client: webseed.Client{
2241 // Consider a MaxConnsPerHost in the transport for this, possibly in a global Client.
2242 HttpClient: WebseedHttpClient,
2245 activeRequests: make(map[Request]webseed.Request, maxRequests),
2247 ws.requesterCond.L = t.cl.locker()
2248 for i := 0; i < maxRequests; i += 1 {
2251 for _, f := range t.callbacks().NewPeer {
2254 ws.peer.logger = t.logger.WithContextValue(&ws)
2255 ws.peer.peerImpl = &ws
2257 ws.onGotInfo(t.info)
2259 t.webSeeds[url] = &ws.peer
2260 ws.peer.onPeerHasAllPieces()
2263 func (t *Torrent) peerIsActive(p *Peer) (active bool) {
2264 t.iterPeers(func(p1 *Peer) {
2272 func (t *Torrent) requestIndexToRequest(ri RequestIndex) Request {
2273 index := ri / t.chunksPerRegularPiece()
2276 t.piece(int(index)).chunkIndexSpec(ri % t.chunksPerRegularPiece()),
2280 func (t *Torrent) requestIndexFromRequest(r Request) RequestIndex {
2281 return t.pieceRequestIndexOffset(pieceIndex(r.Index)) + uint32(r.Begin/t.chunkSize)
2284 func (t *Torrent) numChunks() RequestIndex {
2285 return RequestIndex((t.Length() + int64(t.chunkSize) - 1) / int64(t.chunkSize))
2288 func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex {
2289 return RequestIndex(piece) * t.chunksPerRegularPiece()