]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Move a bunch of stuff into subpackages
[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         "github.com/anacrolix/torrent/types"
10         "github.com/anacrolix/torrent/types/infohash"
11         "golang.org/x/time/rate"
12
13         "github.com/anacrolix/torrent/metainfo"
14         pp "github.com/anacrolix/torrent/peer_protocol"
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 {
97                 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
98                         return errors.New("piece count and file lengths are at odds")
99                 }
100         }
101         return nil
102 }
103
104 func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec {
105         ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
106         if ret.Begin+ret.Length > pieceLength {
107                 ret.Length = pieceLength - ret.Begin
108         }
109         return ret
110 }
111
112 func connLessTrusted(l, r *Peer) bool {
113         return l.trust().Less(r.trust())
114 }
115
116 func connIsIpv6(nc interface {
117         LocalAddr() net.Addr
118 }) bool {
119         ra := nc.LocalAddr()
120         rip := addrIpOrNil(ra)
121         return rip.To4() == nil && rip.To16() != nil
122 }
123
124 func clamp(min, value, max int64) int64 {
125         if min > max {
126                 panic("harumph")
127         }
128         if value < min {
129                 value = min
130         }
131         if value > max {
132                 value = max
133         }
134         return value
135 }
136
137 func max(as ...int64) int64 {
138         ret := as[0]
139         for _, a := range as[1:] {
140                 if a > ret {
141                         ret = a
142                 }
143         }
144         return ret
145 }
146
147 func maxInt(as ...int) int {
148         ret := as[0]
149         for _, a := range as[1:] {
150                 if a > ret {
151                         ret = a
152                 }
153         }
154         return ret
155 }
156
157 func min(as ...int64) int64 {
158         ret := as[0]
159         for _, a := range as[1:] {
160                 if a < ret {
161                         ret = a
162                 }
163         }
164         return ret
165 }
166
167 func minInt(as ...int) int {
168         ret := as[0]
169         for _, a := range as[1:] {
170                 if a < ret {
171                         ret = a
172                 }
173         }
174         return ret
175 }
176
177 var unlimited = rate.NewLimiter(rate.Inf, 0)
178
179 type (
180         pieceIndex = int
181         // Deprecated: Use infohash.T directly to avoid unnecessary imports.
182         InfoHash = infohash.T
183         IpPort   = missinggo.IpPort
184 )
185
186 func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) {
187         for i, b := range slice {
188                 if b {
189                         rb.AddInt(i)
190                 }
191         }
192         return
193 }