8 "github.com/anacrolix/torrent/metainfo"
9 pp "github.com/anacrolix/torrent/peer_protocol"
13 pieceHash = crypto.SHA1
14 maxRequests = 250 // Maximum pending requests we allow peers to send us.
15 defaultChunkSize = 0x4000 // 16KiB
16 // Peer ID client identifier prefix. We'll update this occasionally to
17 // reflect changes to client behaviour that other clients may depend on.
18 // Also see `extendedHandshakeClientVersion`.
20 nominalDialTimeout = time.Second * 30
21 minDialTimeout = 5 * time.Second
28 func lastChunkSpec(pieceLength, chunkSize pp.Integer) (cs chunkSpec) {
29 cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
30 cs.Length = pieceLength - cs.Begin
34 type chunkSpec struct {
35 Begin, Length pp.Integer
43 func newRequest(index, begin, length pp.Integer) request {
44 return request{index, chunkSpec{begin, length}}
48 // Requested data not yet available.
49 errDataNotReady = errors.New("data not ready")
52 // The size in bytes of a metadata extension piece.
53 func metadataPieceSize(totalSize int, piece int) int {
54 ret := totalSize - piece*(1<<14)
61 type superer interface {
65 // Returns ok if there's a parent, and it's not nil.
66 func super(child interface{}) (parent interface{}, ok bool) {
67 s, ok := child.(superer)
76 // Return the request that would include the given offset into the torrent data.
77 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
79 if offset < 0 || offset >= torrentLength {
82 r.Index = pp.Integer(offset / pieceSize)
83 r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
84 r.Length = pp.Integer(chunkSize)
85 pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
86 if r.Length > pieceLeft {
89 torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
90 if int64(r.Length) > torrentLeft {
91 r.Length = pp.Integer(torrentLeft)
97 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
98 off = int64(r.Index)*pieceSize + int64(r.Begin)
99 if off < 0 || off >= torrentLength {
100 panic("invalid request")
105 func validateInfo(info *metainfo.Info) error {
106 if len(info.Pieces)%20 != 0 {
107 return errors.New("pieces has invalid length")
109 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
110 return errors.New("piece count and file lengths are at odds")
115 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
116 ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
117 if ret.Begin+ret.Length > pieceLength {
118 ret.Length = pieceLength - ret.Begin