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(torrentLength, pieceSize, chunkSize, offset int64) (
57 if offset < 0 || offset >= torrentLength {
60 r.Index = pp.Integer(offset / pieceSize)
61 r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
62 r.Length = pp.Integer(chunkSize)
63 pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
64 if r.Length > pieceLeft {
67 torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
68 if int64(r.Length) > torrentLeft {
69 r.Length = pp.Integer(torrentLeft)
75 func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
76 off = int64(r.Index)*pieceSize + int64(r.Begin)
77 if off < 0 || off >= torrentLength {
78 panic("invalid Request")
83 func validateInfo(info *metainfo.Info) error {
84 if len(info.Pieces)%20 != 0 {
85 return errors.New("pieces has invalid length")
87 if info.PieceLength == 0 {
88 if info.TotalLength() != 0 {
89 return errors.New("zero piece length")
92 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
93 return errors.New("piece count and file lengths are at odds")
99 func chunkIndexSpec(index pp.Integer, pieceLength, chunkSize pp.Integer) ChunkSpec {
100 ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
101 if ret.Begin+ret.Length > pieceLength {
102 ret.Length = pieceLength - ret.Begin
107 func connLessTrusted(l, r *Peer) bool {
108 return l.trust().Less(r.trust())
111 func connIsIpv6(nc interface {
115 rip := addrIpOrNil(ra)
116 return rip.To4() == nil && rip.To16() != nil
119 func clamp(min, value, max int64) int64 {
132 func max(as ...int64) int64 {
134 for _, a := range as[1:] {
142 func min(as ...int64) int64 {
144 for _, a := range as[1:] {
152 func minInt(as ...int) int {
154 for _, a := range as[1:] {
162 var unlimited = rate.NewLimiter(rate.Inf, 0)
166 InfoHash = metainfo.Hash
167 IpPort = missinggo.IpPort