7 "github.com/anacrolix/missinggo"
8 "github.com/anacrolix/torrent/metainfo"
9 pp "github.com/anacrolix/torrent/peer_protocol"
10 "golang.org/x/time/rate"
13 type chunkSpec struct {
14 Begin, Length pp.Integer
22 func (r request) ToMsg(mt pp.MessageType) pp.Message {
31 func newRequest(index, begin, length pp.Integer) request {
32 return request{index, chunkSpec{begin, length}}
35 func newRequestFromMessage(msg *pp.Message) request {
37 case pp.Request, pp.Cancel, pp.Reject:
38 return newRequest(msg.Index, msg.Begin, msg.Length)
40 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
46 // The size in bytes of a metadata extension piece.
47 func metadataPieceSize(totalSize int, piece int) int {
48 ret := totalSize - piece*(1<<14)
55 // Return the request that would include the given offset into the torrent data.
56 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
58 if offset < 0 || offset >= torrentLength {
61 r.Index = pp.Integer(offset / pieceSize)
62 r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
63 r.Length = pp.Integer(chunkSize)
64 pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
65 if r.Length > pieceLeft {
68 torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
69 if int64(r.Length) > torrentLeft {
70 r.Length = pp.Integer(torrentLeft)
76 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
77 off = int64(r.Index)*pieceSize + int64(r.Begin)
78 if off < 0 || off >= torrentLength {
79 panic("invalid request")
84 func validateInfo(info *metainfo.Info) error {
85 if len(info.Pieces)%20 != 0 {
86 return errors.New("pieces has invalid length")
88 if info.PieceLength == 0 {
89 if info.TotalLength() != 0 {
90 return errors.New("zero piece length")
93 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
94 return errors.New("piece count and file lengths are at odds")
100 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
101 ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
102 if ret.Begin+ret.Length > pieceLength {
103 ret.Length = pieceLength - ret.Begin
108 func connLessTrusted(l, r *connection) bool {
109 return l.netGoodPiecesDirtied() < r.netGoodPiecesDirtied()
112 // Convert a net.Addr to its compact IP representation. Either 4 or 16 bytes
113 // per "yourip" field of http://www.bittorrent.org/beps/bep_0010.html.
114 func addrCompactIP(addr net.Addr) (string, error) {
115 host, _, err := net.SplitHostPort(addr.String())
119 ip := net.ParseIP(host)
120 if v4 := ip.To4(); v4 != nil {
124 return string(v4), nil
126 return string(ip.To16()), nil
129 func connIsIpv6(nc interface {
133 rip := missinggo.AddrIP(ra)
134 return rip.To4() == nil && rip.To16() != nil
137 func clamp(min, value, max int64) int64 {
150 func max(as ...int64) int64 {
152 for _, a := range as[1:] {
160 func min(as ...int64) int64 {
162 for _, a := range as[1:] {
170 var unlimited = rate.NewLimiter(rate.Inf, 0)
172 type pieceIndex = int