15 "github.com/anacrolix/missinggo"
16 "github.com/anacrolix/missinggo/pubsub"
17 "github.com/bradfitz/iter"
19 "github.com/anacrolix/torrent/bencode"
20 "github.com/anacrolix/torrent/metainfo"
21 pp "github.com/anacrolix/torrent/peer_protocol"
22 "github.com/anacrolix/torrent/tracker"
25 func (t *torrent) chunkIndexSpec(chunkIndex, piece int) chunkSpec {
26 return chunkIndexSpec(chunkIndex, t.pieceLength(piece), t.chunkSize)
29 func (t *torrent) pieceNumPendingBytes(index int) (count pp.Integer) {
30 if t.pieceComplete(index) {
33 piece := &t.Pieces[index]
34 count = t.pieceLength(index)
35 if !piece.EverHashed {
38 for i, dirty := range piece.DirtyChunks {
40 count -= t.chunkIndexSpec(i, index).Length
46 type peersKey struct {
51 // Is not aware of Client. Maintains state of torrent for with-in a Client.
56 // Closed when no more network activity is desired. This includes
57 // announcing, and communicating with peers.
58 ceasingNetworking chan struct{}
62 // Values are the piece indices that changed.
63 pieceStateChanges *pubsub.PubSub
65 // Chunks that are wanted before all others. This is for
66 // responsive/streaming readers that want to unblock ASAP.
67 urgent map[request]struct{}
68 // Total length of the torrent in bytes. Stored because it's not O(1) to
69 // get this from the info dict.
74 // The info dict. Nil if we don't have it (yet).
76 // Active peer connections, running message stream loops.
78 // Set of addrs to which we're attempting to connect. Connections are
79 // half-open until all handshakes are completed.
80 HalfOpen map[string]struct{}
82 // Reserve of peers to connect to. A peer can be both here and in the
83 // active connections if were told about the peer after connecting with
84 // them. That encourages us to reconnect to peers that are well known.
85 Peers map[peersKey]Peer
88 // BEP 12 Multitracker Metadata Extension. The tracker.Client instances
89 // mirror their respective URLs from the announce-list metainfo key.
90 Trackers [][]tracker.Client
91 // Name used if the info name isn't available.
93 // The bencoded bytes of the info dict.
95 // Each element corresponds to the 16KiB metadata pieces. If true, we have
96 // received that piece.
99 // Closed when .Info is set.
100 gotMetainfo chan struct{}
102 connPiecePriorites sync.Pool
106 piecePrioritiesReused = expvar.NewInt("piecePrioritiesReused")
107 piecePrioritiesNew = expvar.NewInt("piecePrioritiesNew")
110 func (t *torrent) setDisplayName(dn string) {
114 func (t *torrent) newConnPiecePriorities() []int {
115 _ret := t.connPiecePriorites.Get()
117 piecePrioritiesReused.Add(1)
120 piecePrioritiesNew.Add(1)
121 return rand.Perm(t.numPieces())
124 func (t *torrent) pieceComplete(piece int) bool {
125 // TODO: This is called when setting metadata, and before storage is
126 // assigned, which doesn't seem right.
127 return t.data != nil && t.data.PieceComplete(piece)
130 func (t *torrent) numConnsUnchoked() (num int) {
131 for _, c := range t.Conns {
139 // There's a connection to that address already.
140 func (t *torrent) addrActive(addr string) bool {
141 if _, ok := t.HalfOpen[addr]; ok {
144 for _, c := range t.Conns {
145 if c.remoteAddr().String() == addr {
152 func (t *torrent) worstConns(cl *Client) (wcs *worstConns) {
154 c: make([]*connection, 0, len(t.Conns)),
158 for _, c := range t.Conns {
162 wcs.c = append(wcs.c, c)
168 func (t *torrent) ceaseNetworking() {
170 defer t.stateMu.Unlock()
172 case <-t.ceasingNetworking:
176 close(t.ceasingNetworking)
177 for _, c := range t.Conns {
182 func (t *torrent) addPeer(p Peer, cl *Client) {
184 if len(t.Peers) >= torrentPeersHighWater {
187 key := peersKey{string(p.IP), p.Port}
188 if _, ok := t.Peers[key]; ok {
192 peersAddedBySource.Add(string(p.Source), 1)
197 func (t *torrent) invalidateMetadata() {
203 func (t *torrent) saveMetadataPiece(index int, data []byte) {
207 if index >= len(t.metadataHave) {
208 log.Printf("%s: ignoring metadata piece %d", t, index)
211 copy(t.MetaData[(1<<14)*index:], data)
212 t.metadataHave[index] = true
215 func (t *torrent) metadataPieceCount() int {
216 return (len(t.MetaData) + (1 << 14) - 1) / (1 << 14)
219 func (t *torrent) haveMetadataPiece(piece int) bool {
221 return (1<<14)*piece < len(t.MetaData)
223 return piece < len(t.metadataHave) && t.metadataHave[piece]
227 func (t *torrent) metadataSizeKnown() bool {
228 return t.MetaData != nil
231 func (t *torrent) metadataSize() int {
232 return len(t.MetaData)
235 func infoPieceHashes(info *metainfo.Info) (ret []string) {
236 for i := 0; i < len(info.Pieces); i += 20 {
237 ret = append(ret, string(info.Pieces[i:i+20]))
242 // Called when metadata for a torrent becomes available.
243 func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte) (err error) {
244 err = validateInfo(md)
246 err = fmt.Errorf("bad info: %s", err)
251 for _, f := range t.Info.UpvertedFiles() {
254 t.MetaData = infoBytes
256 hashes := infoPieceHashes(md)
257 t.Pieces = make([]piece, len(hashes))
258 for i, hash := range hashes {
259 piece := &t.Pieces[i]
260 piece.noPendingWrites.L = &piece.pendingWritesMutex
261 missinggo.CopyExact(piece.Hash[:], hash)
263 for _, conn := range t.Conns {
264 t.initRequestOrdering(conn)
265 if err := conn.setNumPieces(t.numPieces()); err != nil {
266 log.Printf("closing connection: %s", err)
273 func (t *torrent) setStorage(td Data) (err error) {
281 func (t *torrent) haveAllMetadataPieces() bool {
285 if t.metadataHave == nil {
288 for _, have := range t.metadataHave {
296 func (t *torrent) setMetadataSize(bytes int64, cl *Client) {
298 // We already know the correct metadata size.
301 if bytes <= 0 || bytes > 10000000 { // 10MB, pulled from my ass.
302 log.Printf("received bad metadata size: %d", bytes)
305 if t.MetaData != nil && len(t.MetaData) == int(bytes) {
308 t.MetaData = make([]byte, bytes)
309 t.metadataHave = make([]bool, (bytes+(1<<14)-1)/(1<<14))
310 for _, c := range t.Conns {
311 cl.requestPendingMetadata(t, c)
316 // The current working name for the torrent. Either the name in the info dict,
317 // or a display name given such as by the dn value in a magnet link, or "".
318 func (t *torrent) Name() string {
325 func (t *torrent) pieceState(index int) (ret PieceState) {
326 p := &t.Pieces[index]
327 ret.Priority = p.Priority
328 if t.pieceComplete(index) {
331 if p.QueuedForHash || p.Hashing {
334 if !ret.Complete && t.piecePartiallyDownloaded(index) {
340 func (t *torrent) metadataPieceSize(piece int) int {
341 return metadataPieceSize(len(t.MetaData), piece)
344 func (t *torrent) newMetadataExtensionMessage(c *connection, msgType int, piece int, data []byte) pp.Message {
350 d["total_size"] = len(t.MetaData)
352 p, err := bencode.Marshal(d)
358 ExtendedID: byte(c.PeerExtensionIDs["ut_metadata"]),
359 ExtendedPayload: append(p, data...),
363 func (t *torrent) pieceStateRuns() (ret []PieceStateRun) {
364 rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
365 ret = append(ret, PieceStateRun{
366 PieceState: el.(PieceState),
370 for index := range t.Pieces {
371 rle.Append(t.pieceState(index), 1)
377 // Produces a small string representing a PieceStateRun.
378 func pieceStateRunStatusChars(psr PieceStateRun) (ret string) {
379 ret = fmt.Sprintf("%d", psr.Length)
380 ret += func() string {
381 switch psr.Priority {
382 case PiecePriorityNext:
384 case PiecePriorityNormal:
386 case PiecePriorityReadahead:
388 case PiecePriorityNow:
406 func (t *torrent) writeStatus(w io.Writer, cl *Client) {
407 fmt.Fprintf(w, "Infohash: %x\n", t.InfoHash)
408 fmt.Fprintf(w, "Metadata length: %d\n", t.metadataSize())
410 fmt.Fprintf(w, "Metadata have: ")
411 for _, h := range t.metadataHave {
412 fmt.Fprintf(w, "%c", func() rune {
422 fmt.Fprintf(w, "Piece length: %s\n", func() string {
424 return fmt.Sprint(t.usualPieceSize())
430 fmt.Fprintf(w, "Num Pieces: %d\n", t.numPieces())
431 fmt.Fprint(w, "Piece States:")
432 for _, psr := range t.pieceStateRuns() {
434 w.Write([]byte(pieceStateRunStatusChars(psr)))
438 fmt.Fprintf(w, "Urgent:")
439 for req := range t.urgent {
440 fmt.Fprintf(w, " %v", req)
443 fmt.Fprintf(w, "Trackers: ")
444 for _, tier := range t.Trackers {
445 for _, tr := range tier {
446 fmt.Fprintf(w, "%q ", tr.String())
450 fmt.Fprintf(w, "Pending peers: %d\n", len(t.Peers))
451 fmt.Fprintf(w, "Half open: %d\n", len(t.HalfOpen))
452 fmt.Fprintf(w, "Active peers: %d\n", len(t.Conns))
453 sort.Sort(&worstConns{
458 for i, c := range t.Conns {
459 fmt.Fprintf(w, "%2d. ", i+1)
464 func (t *torrent) String() string {
467 s = fmt.Sprintf("%x", t.InfoHash)
472 func (t *torrent) haveInfo() bool {
473 return t != nil && t.Info != nil
476 // TODO: Include URIs that weren't converted to tracker clients.
477 func (t *torrent) announceList() (al [][]string) {
478 for _, tier := range t.Trackers {
480 for _, tr := range tier {
481 l = append(l, tr.URL())
488 // Returns a run-time generated MetaInfo that includes the info bytes and
489 // announce-list as currently known to the client.
490 func (t *torrent) MetaInfo() *metainfo.MetaInfo {
491 if t.MetaData == nil {
492 panic("info bytes not set")
494 return &metainfo.MetaInfo{
495 Info: metainfo.InfoEx{
499 CreationDate: time.Now().Unix(),
500 Comment: "dynamic metainfo from client",
501 CreatedBy: "go.torrent",
502 AnnounceList: t.announceList(),
506 func (t *torrent) bytesLeft() (left int64) {
510 for i := 0; i < t.numPieces(); i++ {
511 left += int64(t.pieceNumPendingBytes(i))
516 func (t *torrent) piecePartiallyDownloaded(index int) bool {
517 pendingBytes := t.pieceNumPendingBytes(index)
518 return pendingBytes != 0 && pendingBytes != t.pieceLength(index)
521 func numChunksForPiece(chunkSize int, pieceSize int) int {
522 return (pieceSize + chunkSize - 1) / chunkSize
525 func (t *torrent) usualPieceSize() int {
526 return int(t.Info.PieceLength)
529 func (t *torrent) lastPieceSize() int {
530 return int(t.pieceLength(t.numPieces() - 1))
533 func (t *torrent) numPieces() int {
534 return t.Info.NumPieces()
537 func (t *torrent) numPiecesCompleted() (num int) {
538 for i := range iter.N(t.Info.NumPieces()) {
539 if t.pieceComplete(i) {
546 func (t *torrent) isClosed() bool {
555 func (t *torrent) close() (err error) {
561 if c, ok := t.data.(io.Closer); ok {
564 for _, conn := range t.Conns {
567 t.pieceStateChanges.Close()
571 func (t *torrent) requestOffset(r request) int64 {
572 return torrentRequestOffset(t.length, int64(t.usualPieceSize()), r)
575 // Return the request that would include the given offset into the torrent
576 // data. Returns !ok if there is no such request.
577 func (t *torrent) offsetRequest(off int64) (req request, ok bool) {
578 return torrentOffsetRequest(t.length, t.Info.PieceLength, int64(t.chunkSize), off)
581 func (t *torrent) writeChunk(piece int, begin int64, data []byte) (err error) {
582 n, err := t.data.WriteAt(data, int64(piece)*t.Info.PieceLength+begin)
583 if err == nil && n != len(data) {
584 err = io.ErrShortWrite
589 func (t *torrent) bitfield() (bf []bool) {
590 for i := range t.Pieces {
591 bf = append(bf, t.havePiece(i))
596 func (t *torrent) validOutgoingRequest(r request) bool {
597 if r.Index >= pp.Integer(t.Info.NumPieces()) {
600 if r.Begin%t.chunkSize != 0 {
603 if r.Length > t.chunkSize {
606 pieceLength := t.pieceLength(int(r.Index))
607 if r.Begin+r.Length > pieceLength {
610 return r.Length == t.chunkSize || r.Begin+r.Length == pieceLength
613 func (t *torrent) pieceChunks(piece int) (css []chunkSpec) {
614 css = make([]chunkSpec, 0, (t.pieceLength(piece)+t.chunkSize-1)/t.chunkSize)
616 for left := t.pieceLength(piece); left != 0; left -= cs.Length {
618 if cs.Length > t.chunkSize {
619 cs.Length = t.chunkSize
621 css = append(css, cs)
622 cs.Begin += cs.Length
627 func (t *torrent) pieceNumChunks(piece int) int {
628 return int((t.pieceLength(piece) + t.chunkSize - 1) / t.chunkSize)
631 func (t *torrent) pendAllChunkSpecs(pieceIndex int) {
632 t.Pieces[pieceIndex].DirtyChunks = nil
640 // Peer is known to support encryption.
641 SupportsEncryption bool
644 func (t *torrent) pieceLength(piece int) (len_ pp.Integer) {
645 if piece < 0 || piece > t.Info.NumPieces() {
648 if int(piece) == t.numPieces()-1 {
649 len_ = pp.Integer(t.length % t.Info.PieceLength)
652 len_ = pp.Integer(t.Info.PieceLength)
657 func (t *torrent) hashPiece(piece int) (ps pieceSum) {
658 hash := pieceHash.New()
659 p := &t.Pieces[piece]
660 p.pendingWritesMutex.Lock()
661 for p.pendingWrites != 0 {
662 p.noPendingWrites.Wait()
664 p.pendingWritesMutex.Unlock()
665 pl := t.Info.Piece(int(piece)).Length()
666 n, err := t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, pl)
668 if err != io.ErrUnexpectedEOF {
669 log.Printf("error hashing piece with %T: %s", t.data, err)
674 panic(fmt.Sprintf("%T: %d != %d", t.data, n, pl))
676 missinggo.CopyExact(ps[:], hash.Sum(nil))
680 func (t *torrent) haveAllPieces() bool {
684 for i := range t.Pieces {
685 if !t.pieceComplete(i) {
692 func (me *torrent) haveAnyPieces() bool {
693 for i := range me.Pieces {
694 if me.pieceComplete(i) {
701 func (t *torrent) havePiece(index int) bool {
702 return t.haveInfo() && t.pieceComplete(index)
705 func (t *torrent) haveChunk(r request) (ret bool) {
707 // log.Println("have chunk", r, ret)
712 if t.pieceComplete(int(r.Index)) {
715 p := &t.Pieces[r.Index]
716 return !p.pendingChunk(r.chunkSpec, t.chunkSize)
719 func chunkIndex(cs chunkSpec, chunkSize pp.Integer) int {
720 return int(cs.Begin / chunkSize)
723 // TODO: This should probably be called wantPiece.
724 func (t *torrent) wantChunk(r request) bool {
725 if !t.wantPiece(int(r.Index)) {
728 if t.Pieces[r.Index].pendingChunk(r.chunkSpec, t.chunkSize) {
735 func (t *torrent) urgentChunkInPiece(piece int) bool {
736 p := pp.Integer(piece)
737 for req := range t.urgent {
745 // TODO: This should be called wantPieceIndex.
746 func (t *torrent) wantPiece(index int) bool {
750 p := &t.Pieces[index]
757 if p.Priority == PiecePriorityNone {
758 if !t.urgentChunkInPiece(index) {
762 // Put piece complete check last, since it's the slowest as it can involve
763 // calling out into external data stores.
764 return !t.pieceComplete(index)
767 func (t *torrent) connHasWantedPieces(c *connection) bool {
768 return c.pieceRequestOrder != nil && !c.pieceRequestOrder.Empty()
771 func (t *torrent) extentPieces(off, _len int64) (pieces []int) {
772 for i := off / int64(t.usualPieceSize()); i*int64(t.usualPieceSize()) < off+_len; i++ {
773 pieces = append(pieces, int(i))
778 func (t *torrent) worstBadConn(cl *Client) *connection {
779 wcs := t.worstConns(cl)
782 c := heap.Pop(wcs).(*connection)
783 if c.UnwantedChunksReceived >= 6 && c.UnwantedChunksReceived > c.UsefulChunksReceived {
786 if wcs.Len() >= (socketsPerTorrent+1)/2 {
787 // Give connections 1 minute to prove themselves.
788 if time.Since(c.completedHandshake) > time.Minute {
796 func (t *torrent) publishPieceChange(piece int) {
797 cur := t.pieceState(piece)
798 p := &t.Pieces[piece]
799 if cur != p.PublicPieceState {
800 t.pieceStateChanges.Publish(piece)
802 p.PublicPieceState = cur
805 func (t *torrent) pieceNumPendingChunks(piece int) int {
806 return t.pieceNumChunks(piece) - t.Pieces[piece].numDirtyChunks()
809 func (t *torrent) pieceAllDirty(piece int) bool {
810 p := &t.Pieces[piece]
811 if len(p.DirtyChunks) != t.pieceNumChunks(piece) {
814 for _, dirty := range p.DirtyChunks {