15 "github.com/anacrolix/libtorgo/bencode"
16 "github.com/anacrolix/libtorgo/metainfo"
17 "github.com/bradfitz/iter"
19 "github.com/anacrolix/torrent/data"
20 pp "github.com/anacrolix/torrent/peer_protocol"
21 "github.com/anacrolix/torrent/tracker"
22 "github.com/anacrolix/torrent/util"
25 func (t *torrent) pieceNumPendingBytes(index int) (count pp.Integer) {
26 if t.pieceComplete(index) {
29 piece := t.Pieces[index]
30 if !piece.EverHashed {
31 return t.pieceLength(index)
33 pendingChunks := t.Pieces[index].PendingChunkSpecs
34 count = pp.Integer(len(pendingChunks)) * chunkSize
35 _lastChunkSpec := lastChunkSpec(t.pieceLength(index))
36 if _lastChunkSpec.Length != chunkSize {
37 if _, ok := pendingChunks[_lastChunkSpec]; ok {
38 count += _lastChunkSpec.Length - chunkSize
44 type peersKey struct {
49 // Data maintains per-piece persistent state.
50 type StatefulData interface {
52 // We believe the piece data will pass a hash check.
53 PieceCompleted(index int) error
54 // Returns true if the piece is complete.
55 PieceComplete(index int) bool
58 // Is not aware of Client. Maintains state of torrent for with-in a Client.
63 // Closed when no more network activity is desired. This includes
64 // announcing, and communicating with peers.
65 ceasingNetworking chan struct{}
69 // Total length of the torrent in bytes. Stored because it's not O(1) to
70 // get this from the info dict.
75 // The info dict. Nil if we don't have it.
77 // Active peer connections, running message stream loops.
79 // Set of addrs to which we're attempting to connect. Connections are
80 // half-open until all handshakes are completed.
81 HalfOpen map[string]struct{}
83 // Reserve of peers to connect to. A peer can be both here and in the
84 // active connections if were told about the peer after connecting with
85 // them. That encourages us to reconnect to peers that are well known.
86 Peers map[peersKey]Peer
89 // BEP 12 Multitracker Metadata Extension. The tracker.Client instances
90 // mirror their respective URLs from the announce-list metainfo key.
91 Trackers [][]tracker.Client
92 // Name used if the info name isn't available.
94 // The bencoded bytes of the info dict.
96 // Each element corresponds to the 16KiB metadata pieces. If true, we have
97 // received that piece.
100 // Closed when .Info is set.
101 gotMetainfo chan struct{}
102 GotMetainfo <-chan struct{}
104 pruneTimer *time.Timer
107 func (t *torrent) pieceComplete(piece int) bool {
108 // TODO: This is called when setting metadata, and before storage is
109 // assigned, which doesn't seem right.
110 return t.data != nil && t.data.PieceComplete(piece)
113 // A file-like handle to torrent data that implements SectionOpener. Opened
114 // sections will be reused so long as Reads and ReadAt's are contiguous.
124 func (h *handle) Close() error {
131 func (h *handle) ReadAt(b []byte, off int64) (n int, err error) {
132 return h.readAt(b, off)
135 func (h *handle) readAt(b []byte, off int64) (n int, err error) {
136 avail := h.t.prepareRead(off)
137 if int64(len(b)) > avail {
140 if int64(len(b)) > h.size-off {
143 if h.rcOff != off && h.rc != nil {
148 h.rc, err = h.so.OpenSection(off, h.size-off)
154 n, err = h.rc.Read(b)
159 func (h *handle) Read(b []byte) (n int, err error) {
160 n, err = h.readAt(b, h.curOff)
165 func (h *handle) Seek(off int64, whence int) (newOff int64, err error) {
172 h.curOff = h.size + off
174 err = errors.New("bad whence")
180 // Implements Handle on top of an io.SectionReader.
181 type sectionReaderHandle struct {
185 func (sectionReaderHandle) Close() error { return nil }
187 func (T Torrent) NewReadHandle() Handle {
188 if so, ok := T.data.(SectionOpener); ok {
195 return sectionReaderHandle{io.NewSectionReader(T, 0, T.Length())}
198 func (t *torrent) numConnsUnchoked() (num int) {
199 for _, c := range t.Conns {
207 // There's a connection to that address already.
208 func (t *torrent) addrActive(addr string) bool {
209 if _, ok := t.HalfOpen[addr]; ok {
212 for _, c := range t.Conns {
213 if c.remoteAddr().String() == addr {
220 func (t *torrent) worstConnsHeap() (wcs *worstConns) {
222 c: append([]*connection{}, t.Conns...),
229 func (t *torrent) ceaseNetworking() {
231 defer t.stateMu.Unlock()
233 case <-t.ceasingNetworking:
237 close(t.ceasingNetworking)
238 for _, c := range t.Conns {
244 func (t *torrent) addPeer(p Peer) {
245 t.Peers[peersKey{string(p.IP), p.Port}] = p
248 func (t *torrent) invalidateMetadata() {
254 func (t *torrent) saveMetadataPiece(index int, data []byte) {
258 if index >= len(t.metadataHave) {
259 log.Printf("%s: ignoring metadata piece %d", t, index)
262 copy(t.MetaData[(1<<14)*index:], data)
263 t.metadataHave[index] = true
266 func (t *torrent) metadataPieceCount() int {
267 return (len(t.MetaData) + (1 << 14) - 1) / (1 << 14)
270 func (t *torrent) haveMetadataPiece(piece int) bool {
272 return (1<<14)*piece < len(t.MetaData)
274 return piece < len(t.metadataHave) && t.metadataHave[piece]
278 func (t *torrent) metadataSizeKnown() bool {
279 return t.MetaData != nil
282 func (t *torrent) metadataSize() int {
283 return len(t.MetaData)
286 func infoPieceHashes(info *metainfo.Info) (ret []string) {
287 for i := 0; i < len(info.Pieces); i += 20 {
288 ret = append(ret, string(info.Pieces[i:i+20]))
293 // Called when metadata for a torrent becomes available.
294 func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte, eventLocker sync.Locker) (err error) {
297 for _, f := range t.Info.UpvertedFiles() {
300 t.MetaData = infoBytes
302 for _, hash := range infoPieceHashes(md) {
304 piece.Event.L = eventLocker
305 util.CopyExact(piece.Hash[:], hash)
306 t.Pieces = append(t.Pieces, piece)
308 for _, conn := range t.Conns {
309 t.initRequestOrdering(conn)
310 if err := conn.setNumPieces(t.numPieces()); err != nil {
311 log.Printf("closing connection: %s", err)
318 func (t *torrent) setStorage(td data.Data) (err error) {
319 if c, ok := t.data.(io.Closer); ok {
322 if sd, ok := td.(StatefulData); ok {
325 t.data = &statelessDataWrapper{td, make([]bool, t.Info.NumPieces())}
330 func (t *torrent) haveAllMetadataPieces() bool {
334 if t.metadataHave == nil {
337 for _, have := range t.metadataHave {
345 func (t *torrent) setMetadataSize(bytes int64) {
346 if t.MetaData != nil {
349 if bytes <= 0 || bytes > 10000000 { // 10MB, pulled from my ass.
352 t.MetaData = make([]byte, bytes)
353 t.metadataHave = make([]bool, (bytes+(1<<14)-1)/(1<<14))
356 // The current working name for the torrent. Either the name in the info dict,
357 // or a display name given such as by the dn value in a magnet link, or "".
358 func (t *torrent) Name() string {
362 if t.DisplayName != "" {
368 func (t *torrent) pieceStatusChar(index int) byte {
371 case t.pieceComplete(index):
373 case p.QueuedForHash:
379 case t.piecePartiallyDownloaded(index):
381 case piecePriorityNone:
382 return 'F' // Forgotten
388 case piecePriorityNone:
390 case piecePriorityNow:
392 case piecePriorityReadahead:
394 case piecePriorityNext:
402 func (t *torrent) metadataPieceSize(piece int) int {
403 return metadataPieceSize(len(t.MetaData), piece)
406 func (t *torrent) newMetadataExtensionMessage(c *connection, msgType int, piece int, data []byte) pp.Message {
412 d["total_size"] = len(t.MetaData)
414 p, err := bencode.Marshal(d)
420 ExtendedID: byte(c.PeerExtensionIDs["ut_metadata"]),
421 ExtendedPayload: append(p, data...),
425 type PieceStatusCharSequence struct {
426 Char byte // The state of this sequence of pieces.
427 Count int // How many consecutive pieces have this state.
430 // Returns the state of pieces of the torrent. They are grouped into runs of
431 // same state. The sum of the Counts of the sequences is the number of pieces
432 // in the torrent. See the function torrent.pieceStatusChar for the possible
434 func (t *torrent) PieceStatusCharSequences() []PieceStatusCharSequence {
436 defer t.stateMu.Unlock()
437 return t.pieceStatusCharSequences()
440 // Returns the length of sequences of identical piece status chars.
441 func (t *torrent) pieceStatusCharSequences() (ret []PieceStatusCharSequence) {
446 writeSequence := func() {
447 ret = append(ret, PieceStatusCharSequence{char, count})
449 if len(t.Pieces) != 0 {
450 char = t.pieceStatusChar(0)
452 for index := range t.Pieces {
453 char1 := t.pieceStatusChar(index)
468 func (t *torrent) writeStatus(w io.Writer) {
469 fmt.Fprintf(w, "Infohash: %x\n", t.InfoHash)
470 fmt.Fprintf(w, "Metadata length: %d\n", t.metadataSize())
471 fmt.Fprintf(w, "Metadata have: ")
472 for _, h := range t.metadataHave {
473 fmt.Fprintf(w, "%c", func() rune {
482 fmt.Fprintf(w, "Piece length: %s\n", func() string {
484 return fmt.Sprint(t.usualPieceSize())
490 fmt.Fprint(w, "Pieces: ")
491 for _, seq := range t.pieceStatusCharSequences() {
492 fmt.Fprintf(w, "%d%c ", seq.Count, seq.Char)
496 fmt.Fprintf(w, "Trackers: ")
497 for _, tier := range t.Trackers {
498 for _, tr := range tier {
499 fmt.Fprintf(w, "%q ", tr.String())
503 fmt.Fprintf(w, "Pending peers: %d\n", len(t.Peers))
504 fmt.Fprintf(w, "Half open: %d\n", len(t.HalfOpen))
505 fmt.Fprintf(w, "Active peers: %d\n", len(t.Conns))
506 sort.Sort(&worstConns{
510 for _, c := range t.Conns {
515 func (t *torrent) String() string {
518 s = fmt.Sprintf("%x", t.InfoHash)
523 func (t *torrent) haveInfo() bool {
527 // TODO: Include URIs that weren't converted to tracker clients.
528 func (t *torrent) announceList() (al [][]string) {
529 for _, tier := range t.Trackers {
531 for _, tr := range tier {
532 l = append(l, tr.URL())
539 // Returns a run-time generated MetaInfo that includes the info bytes and
540 // announce-list as currently known to the client.
541 func (t *torrent) MetaInfo() *metainfo.MetaInfo {
542 if t.MetaData == nil {
543 panic("info bytes not set")
545 return &metainfo.MetaInfo{
546 Info: metainfo.InfoEx{
550 CreationDate: time.Now().Unix(),
551 Comment: "dynamic metainfo from client",
552 CreatedBy: "go.torrent",
553 AnnounceList: t.announceList(),
557 func (t *torrent) bytesLeft() (left int64) {
561 for i := 0; i < t.numPieces(); i++ {
562 left += int64(t.pieceNumPendingBytes(i))
567 func (t *torrent) piecePartiallyDownloaded(index int) bool {
568 return t.pieceNumPendingBytes(index) != t.pieceLength(index)
571 func numChunksForPiece(chunkSize int, pieceSize int) int {
572 return (pieceSize + chunkSize - 1) / chunkSize
575 func (t *torrent) usualPieceSize() int {
576 return int(t.Info.PieceLength)
579 func (t *torrent) lastPieceSize() int {
580 return int(t.pieceLength(t.numPieces() - 1))
583 func (t *torrent) numPieces() int {
584 return t.Info.NumPieces()
587 func (t *torrent) numPiecesCompleted() (num int) {
588 for i := range iter.N(t.Info.NumPieces()) {
589 if t.pieceComplete(i) {
596 func (t *torrent) Length() int64 {
600 func (t *torrent) isClosed() bool {
609 func (t *torrent) close() (err error) {
615 if c, ok := t.data.(io.Closer); ok {
618 for _, conn := range t.Conns {
624 // Return the request that would include the given offset into the torrent data.
625 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
626 r request, ok bool) {
627 if offset < 0 || offset >= torrentLength {
630 r.Index = pp.Integer(offset / pieceSize)
631 r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
632 left := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
633 if chunkSize < left {
634 r.Length = pp.Integer(chunkSize)
636 r.Length = pp.Integer(left)
642 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
643 off = int64(r.Index)*pieceSize + int64(r.Begin)
644 if off < 0 || off >= torrentLength {
645 panic("invalid request")
650 func (t *torrent) requestOffset(r request) int64 {
651 return torrentRequestOffset(t.Length(), int64(t.usualPieceSize()), r)
654 // Return the request that would include the given offset into the torrent data.
655 func (t *torrent) offsetRequest(off int64) (req request, ok bool) {
656 return torrentOffsetRequest(t.Length(), t.Info.PieceLength, chunkSize, off)
659 func (t *torrent) writeChunk(piece int, begin int64, data []byte) (err error) {
660 _, err = t.data.WriteAt(data, int64(piece)*t.Info.PieceLength+begin)
664 func (t *torrent) bitfield() (bf []bool) {
665 for _, p := range t.Pieces {
666 bf = append(bf, p.EverHashed && len(p.PendingChunkSpecs) == 0)
671 func (t *torrent) pieceChunks(piece int) (css []chunkSpec) {
672 css = make([]chunkSpec, 0, (t.pieceLength(piece)+chunkSize-1)/chunkSize)
674 for left := t.pieceLength(piece); left != 0; left -= cs.Length {
676 if cs.Length > chunkSize {
677 cs.Length = chunkSize
679 css = append(css, cs)
680 cs.Begin += cs.Length
685 func (t *torrent) pendAllChunkSpecs(index int) {
686 piece := t.Pieces[index]
687 if piece.PendingChunkSpecs == nil {
688 piece.PendingChunkSpecs = make(
689 map[chunkSpec]struct{},
690 (t.pieceLength(index)+chunkSize-1)/chunkSize)
692 pcss := piece.PendingChunkSpecs
693 for _, cs := range t.pieceChunks(int(index)) {
694 pcss[cs] = struct{}{}
704 // Peer is known to support encryption.
705 SupportsEncryption bool
708 func (t *torrent) pieceLength(piece int) (len_ pp.Integer) {
709 if int(piece) == t.numPieces()-1 {
710 len_ = pp.Integer(t.Length() % t.Info.PieceLength)
713 len_ = pp.Integer(t.Info.PieceLength)
718 func (t *torrent) hashPiece(piece pp.Integer) (ps pieceSum) {
719 hash := pieceHash.New()
720 t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength)
721 util.CopyExact(ps[:], hash.Sum(nil))
725 func (t *torrent) haveAllPieces() bool {
729 for i := range t.Pieces {
730 if !t.pieceComplete(i) {
737 func (me *torrent) haveAnyPieces() bool {
738 for i := range me.Pieces {
739 if me.pieceComplete(i) {
746 func (t *torrent) havePiece(index int) bool {
747 return t.haveInfo() && t.pieceComplete(index)
750 func (t *torrent) haveChunk(r request) bool {
751 p := t.Pieces[r.Index]
755 _, ok := p.PendingChunkSpecs[r.chunkSpec]
759 func (t *torrent) wantChunk(r request) bool {
760 if !t.wantPiece(int(r.Index)) {
763 _, ok := t.Pieces[r.Index].PendingChunkSpecs[r.chunkSpec]
767 func (t *torrent) wantPiece(index int) bool {
772 // Put piece complete check last, since it's the slowest!
773 return p.Priority != piecePriorityNone && !p.QueuedForHash && !p.Hashing && !t.pieceComplete(index)
776 func (t *torrent) connHasWantedPieces(c *connection) bool {
777 return c.pieceRequestOrder != nil && c.pieceRequestOrder.First() != nil
780 func (t *torrent) extentPieces(off, _len int64) (pieces []int) {
781 for i := off / int64(t.usualPieceSize()); i*int64(t.usualPieceSize()) < off+_len; i++ {
782 pieces = append(pieces, int(i))