]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Check that chunks we request aren't being hashed or queued for hash
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "errors"
5         "net"
6
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"
11 )
12
13 type chunkSpec struct {
14         Begin, Length pp.Integer
15 }
16
17 type request struct {
18         Index pp.Integer
19         chunkSpec
20 }
21
22 func (r request) ToMsg(mt pp.MessageType) pp.Message {
23         return pp.Message{
24                 Type:   mt,
25                 Index:  r.Index,
26                 Begin:  r.Begin,
27                 Length: r.Length,
28         }
29 }
30
31 func newRequest(index, begin, length pp.Integer) request {
32         return request{index, chunkSpec{begin, length}}
33 }
34
35 func newRequestFromMessage(msg *pp.Message) request {
36         switch msg.Type {
37         case pp.Request, pp.Cancel, pp.Reject:
38                 return newRequest(msg.Index, msg.Begin, msg.Length)
39         case pp.Piece:
40                 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
41         default:
42                 panic(msg.Type)
43         }
44 }
45
46 // The size in bytes of a metadata extension piece.
47 func metadataPieceSize(totalSize int, piece int) int {
48         ret := totalSize - piece*(1<<14)
49         if ret > 1<<14 {
50                 ret = 1 << 14
51         }
52         return ret
53 }
54
55 // Return the request that would include the given offset into the torrent data.
56 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
57         r request, ok bool) {
58         if offset < 0 || offset >= torrentLength {
59                 return
60         }
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 {
66                 r.Length = pieceLeft
67         }
68         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
69         if int64(r.Length) > torrentLeft {
70                 r.Length = pp.Integer(torrentLeft)
71         }
72         ok = true
73         return
74 }
75
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")
80         }
81         return
82 }
83
84 func validateInfo(info *metainfo.Info) error {
85         if len(info.Pieces)%20 != 0 {
86                 return errors.New("pieces has invalid length")
87         }
88         if info.PieceLength == 0 {
89                 if info.TotalLength() != 0 {
90                         return errors.New("zero piece length")
91                 }
92         } else {
93                 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
94                         return errors.New("piece count and file lengths are at odds")
95                 }
96         }
97         return nil
98 }
99
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
104         }
105         return ret
106 }
107
108 func connLessTrusted(l, r *connection) bool {
109         return l.netGoodPiecesDirtied() < r.netGoodPiecesDirtied()
110 }
111
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())
116         if err != nil {
117                 return "", err
118         }
119         ip := net.ParseIP(host)
120         if v4 := ip.To4(); v4 != nil {
121                 if len(v4) != 4 {
122                         panic(v4)
123                 }
124                 return string(v4), nil
125         }
126         return string(ip.To16()), nil
127 }
128
129 func connIsIpv6(nc interface {
130         LocalAddr() net.Addr
131 }) bool {
132         ra := nc.LocalAddr()
133         rip := missinggo.AddrIP(ra)
134         return rip.To4() == nil && rip.To16() != nil
135 }
136
137 func clamp(min, value, max int64) int64 {
138         if min > max {
139                 panic("harumph")
140         }
141         if value < min {
142                 value = min
143         }
144         if value > max {
145                 value = max
146         }
147         return value
148 }
149
150 func max(as ...int64) int64 {
151         ret := as[0]
152         for _, a := range as[1:] {
153                 if a > ret {
154                         ret = a
155                 }
156         }
157         return ret
158 }
159
160 func min(as ...int64) int64 {
161         ret := as[0]
162         for _, a := range as[1:] {
163                 if a < ret {
164                         ret = a
165                 }
166         }
167         return ret
168 }
169
170 var unlimited = rate.NewLimiter(rate.Inf, 0)
171
172 type pieceIndex = int