7 "github.com/anacrolix/missinggo/v2"
8 "github.com/anacrolix/torrent/types"
9 "golang.org/x/time/rate"
11 "github.com/anacrolix/torrent/metainfo"
12 pp "github.com/anacrolix/torrent/peer_protocol"
16 Request = types.Request
17 ChunkSpec = types.ChunkSpec
18 piecePriority = types.PiecePriority
22 PiecePriorityNormal = types.PiecePriorityNormal
23 PiecePriorityNone = types.PiecePriorityNone
24 PiecePriorityNow = types.PiecePriorityNow
25 PiecePriorityReadahead = types.PiecePriorityReadahead
26 PiecePriorityNext = types.PiecePriorityNext
27 PiecePriorityHigh = types.PiecePriorityHigh
30 func newRequest(index, begin, length pp.Integer) Request {
31 return Request{index, ChunkSpec{begin, length}}
34 func newRequestFromMessage(msg *pp.Message) Request {
36 case pp.Request, pp.Cancel, pp.Reject:
37 return newRequest(msg.Index, msg.Begin, msg.Length)
39 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
45 // The size in bytes of a metadata extension piece.
46 func metadataPieceSize(totalSize int, piece int) int {
47 ret := totalSize - piece*(1<<14)
54 // Return the request that would include the given offset into the torrent data.
55 func torrentOffsetRequest(
56 torrentLength, pieceSize, chunkSize, offset int64,
60 if offset < 0 || offset >= torrentLength {
63 r.Index = pp.Integer(offset / pieceSize)
64 r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
65 r.Length = pp.Integer(chunkSize)
66 pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
67 if r.Length > pieceLeft {
70 torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
71 if int64(r.Length) > torrentLeft {
72 r.Length = pp.Integer(torrentLeft)
78 func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
79 off = int64(r.Index)*pieceSize + int64(r.Begin)
80 if off < 0 || off >= torrentLength {
81 panic("invalid Request")
86 func validateInfo(info *metainfo.Info) error {
87 if len(info.Pieces)%20 != 0 {
88 return errors.New("pieces has invalid length")
90 if info.PieceLength == 0 {
91 if info.TotalLength() != 0 {
92 return errors.New("zero piece length")
95 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
96 return errors.New("piece count and file lengths are at odds")
102 func chunkIndexSpec(index pp.Integer, pieceLength, chunkSize pp.Integer) ChunkSpec {
103 ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
104 if ret.Begin+ret.Length > pieceLength {
105 ret.Length = pieceLength - ret.Begin
110 func connLessTrusted(l, r *Peer) bool {
111 return l.trust().Less(r.trust())
114 func connIsIpv6(nc interface {
118 rip := addrIpOrNil(ra)
119 return rip.To4() == nil && rip.To16() != nil
122 func clamp(min, value, max int64) int64 {
135 func max(as ...int64) int64 {
137 for _, a := range as[1:] {
145 func min(as ...int64) int64 {
147 for _, a := range as[1:] {
155 func minInt(as ...int) int {
157 for _, a := range as[1:] {
165 var unlimited = rate.NewLimiter(rate.Inf, 0)
169 InfoHash = metainfo.Hash
170 IpPort = missinggo.IpPort