]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
cmd/btrtrc client
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "errors"
5         "net"
6
7         "github.com/RoaringBitmap/roaring"
8         "github.com/anacrolix/missinggo/v2"
9         "golang.org/x/time/rate"
10
11         "github.com/anacrolix/torrent/metainfo"
12         pp "github.com/anacrolix/torrent/peer_protocol"
13         "github.com/anacrolix/torrent/types"
14         "github.com/anacrolix/torrent/types/infohash"
15 )
16
17 type (
18         Request       = types.Request
19         ChunkSpec     = types.ChunkSpec
20         PiecePriority = types.PiecePriority
21 )
22
23 const (
24         PiecePriorityNormal    = types.PiecePriorityNormal
25         PiecePriorityNone      = types.PiecePriorityNone
26         PiecePriorityNow       = types.PiecePriorityNow
27         PiecePriorityReadahead = types.PiecePriorityReadahead
28         PiecePriorityNext      = types.PiecePriorityNext
29         PiecePriorityHigh      = types.PiecePriorityHigh
30 )
31
32 func newRequest(index, begin, length pp.Integer) Request {
33         return Request{index, ChunkSpec{begin, length}}
34 }
35
36 func newRequestFromMessage(msg *pp.Message) Request {
37         switch msg.Type {
38         case pp.Request, pp.Cancel, pp.Reject:
39                 return newRequest(msg.Index, msg.Begin, msg.Length)
40         case pp.Piece:
41                 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
42         default:
43                 panic(msg.Type)
44         }
45 }
46
47 // The size in bytes of a metadata extension piece.
48 func metadataPieceSize(totalSize, piece int) int {
49         ret := totalSize - piece*(1<<14)
50         if ret > 1<<14 {
51                 ret = 1 << 14
52         }
53         return ret
54 }
55
56 // Return the request that would include the given offset into the torrent data.
57 func torrentOffsetRequest(
58         torrentLength, pieceSize, chunkSize, offset int64,
59 ) (
60         r Request, ok bool,
61 ) {
62         if offset < 0 || offset >= torrentLength {
63                 return
64         }
65         r.Index = pp.Integer(offset / pieceSize)
66         r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
67         r.Length = pp.Integer(chunkSize)
68         pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
69         if r.Length > pieceLeft {
70                 r.Length = pieceLeft
71         }
72         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
73         if int64(r.Length) > torrentLeft {
74                 r.Length = pp.Integer(torrentLeft)
75         }
76         ok = true
77         return
78 }
79
80 func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
81         off = int64(r.Index)*pieceSize + int64(r.Begin)
82         if off < 0 || off >= torrentLength {
83                 panic("invalid Request")
84         }
85         return
86 }
87
88 func validateInfo(info *metainfo.Info) error {
89         if len(info.Pieces)%20 != 0 {
90                 return errors.New("pieces has invalid length")
91         }
92         if info.PieceLength == 0 {
93                 if info.TotalLength() != 0 {
94                         return errors.New("zero piece length")
95                 }
96         } else if !info.HasV2() {
97                 // TotalLength returns different values for V1 and V2 depending on whether v1 pad files are
98                 // counted. Split the interface into several methods?
99                 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
100                         return errors.New("piece count and file lengths are at odds")
101                 }
102         }
103         return nil
104 }
105
106 func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec {
107         ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
108         if ret.Begin+ret.Length > pieceLength {
109                 ret.Length = pieceLength - ret.Begin
110         }
111         return ret
112 }
113
114 func connLessTrusted(l, r *Peer) bool {
115         return l.trust().Less(r.trust())
116 }
117
118 func connIsIpv6(nc interface {
119         LocalAddr() net.Addr
120 },
121 ) bool {
122         ra := nc.LocalAddr()
123         rip := addrIpOrNil(ra)
124         return rip.To4() == nil && rip.To16() != nil
125 }
126
127 func clamp(min, value, max int64) int64 {
128         if min > max {
129                 panic("harumph")
130         }
131         if value < min {
132                 value = min
133         }
134         if value > max {
135                 value = max
136         }
137         return value
138 }
139
140 func max(as ...int64) int64 {
141         ret := as[0]
142         for _, a := range as[1:] {
143                 if a > ret {
144                         ret = a
145                 }
146         }
147         return ret
148 }
149
150 func maxInt(as ...int) int {
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 minInt(as ...int) int {
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 (
173         pieceIndex = int
174         // Deprecated: Use infohash.T directly to avoid unnecessary imports.
175         InfoHash = infohash.T
176         IpPort   = missinggo.IpPort
177 )
178
179 func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) {
180         for i, b := range slice {
181                 if b {
182                         rb.AddInt(i)
183                 }
184         }
185         return
186 }