]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Drop support for go 1.20
[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 {
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 },
119 ) bool {
120         ra := nc.LocalAddr()
121         rip := addrIpOrNil(ra)
122         return rip.To4() == nil && rip.To16() != nil
123 }
124
125 func clamp(min, value, max int64) int64 {
126         if min > max {
127                 panic("harumph")
128         }
129         if value < min {
130                 value = min
131         }
132         if value > max {
133                 value = max
134         }
135         return value
136 }
137
138 func max(as ...int64) int64 {
139         ret := as[0]
140         for _, a := range as[1:] {
141                 if a > ret {
142                         ret = a
143                 }
144         }
145         return ret
146 }
147
148 func maxInt(as ...int) int {
149         ret := as[0]
150         for _, a := range as[1:] {
151                 if a > ret {
152                         ret = a
153                 }
154         }
155         return ret
156 }
157
158 func min(as ...int64) int64 {
159         ret := as[0]
160         for _, a := range as[1:] {
161                 if a < ret {
162                         ret = a
163                 }
164         }
165         return ret
166 }
167
168 func minInt(as ...int) int {
169         ret := as[0]
170         for _, a := range as[1:] {
171                 if a < ret {
172                         ret = a
173                 }
174         }
175         return ret
176 }
177
178 var unlimited = rate.NewLimiter(rate.Inf, 0)
179
180 type (
181         pieceIndex = int
182         // Deprecated: Use infohash.T directly to avoid unnecessary imports.
183         InfoHash = infohash.T
184         IpPort   = missinggo.IpPort
185 )
186
187 func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) {
188         for i, b := range slice {
189                 if b {
190                         rb.AddInt(i)
191                 }
192         }
193         return
194 }