9 "github.com/anacrolix/torrent/metainfo"
10 pp "github.com/anacrolix/torrent/peer_protocol"
14 pieceHash = crypto.SHA1
15 maxRequests = 250 // Maximum pending requests we allow peers to send us.
16 chunkSize = 0x4000 // 16KiB
17 bep20 = "-GT0000-" // Peer ID client identifier prefix
18 nominalDialTimeout = time.Second * 30
19 minDialTimeout = 5 * time.Second
27 func (ih *InfoHash) AsString() string {
31 func (ih *InfoHash) HexString() string {
32 return fmt.Sprintf("%x", ih[:])
35 func lastChunkSpec(pieceLength pp.Integer) (cs chunkSpec) {
36 cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
37 cs.Length = pieceLength - cs.Begin
41 type chunkSpec struct {
42 Begin, Length pp.Integer
50 func newRequest(index, begin, length pp.Integer) request {
51 return request{index, chunkSpec{begin, length}}
55 // Requested data not yet available.
56 errDataNotReady = errors.New("data not ready")
59 // The size in bytes of a metadata extension piece.
60 func metadataPieceSize(totalSize int, piece int) int {
61 ret := totalSize - piece*(1<<14)
68 type superer interface {
72 // Returns ok if there's a parent, and it's not nil.
73 func super(child interface{}) (parent interface{}, ok bool) {
74 s, ok := child.(superer)
83 // Return the request that would include the given offset into the torrent data.
84 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
86 if offset < 0 || offset >= torrentLength {
89 r.Index = pp.Integer(offset / pieceSize)
90 r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
91 r.Length = pp.Integer(chunkSize)
92 pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
93 if r.Length > pieceLeft {
96 torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
97 if int64(r.Length) > torrentLeft {
98 r.Length = pp.Integer(torrentLeft)
104 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
105 off = int64(r.Index)*pieceSize + int64(r.Begin)
106 if off < 0 || off >= torrentLength {
107 panic("invalid request")
112 func validateInfo(info *metainfo.Info) error {
113 if len(info.Pieces)%20 != 0 {
114 return errors.New("pieces has invalid length")
116 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
117 return errors.New("piece count and file lengths are at odds")