]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
gofumpt -extra
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "errors"
5         "net"
6
7         "github.com/anacrolix/missinggo/v2"
8         "github.com/anacrolix/torrent/types"
9         "golang.org/x/time/rate"
10
11         "github.com/anacrolix/torrent/metainfo"
12         pp "github.com/anacrolix/torrent/peer_protocol"
13 )
14
15 type (
16         Request       = types.Request
17         ChunkSpec     = types.ChunkSpec
18         piecePriority = types.PiecePriority
19 )
20
21 const (
22         PiecePriorityNormal    = types.PiecePriorityNormal
23         PiecePriorityNone      = types.PiecePriorityNone
24         PiecePriorityNow       = types.PiecePriorityNow
25         PiecePriorityReadahead = types.PiecePriorityReadahead
26         PiecePriorityNext      = types.PiecePriorityNext
27         PiecePriorityHigh      = types.PiecePriorityHigh
28 )
29
30 func newRequest(index, begin, length pp.Integer) Request {
31         return Request{index, ChunkSpec{begin, length}}
32 }
33
34 func newRequestFromMessage(msg *pp.Message) Request {
35         switch msg.Type {
36         case pp.Request, pp.Cancel, pp.Reject:
37                 return newRequest(msg.Index, msg.Begin, msg.Length)
38         case pp.Piece:
39                 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
40         default:
41                 panic(msg.Type)
42         }
43 }
44
45 // The size in bytes of a metadata extension piece.
46 func metadataPieceSize(totalSize, piece int) int {
47         ret := totalSize - piece*(1<<14)
48         if ret > 1<<14 {
49                 ret = 1 << 14
50         }
51         return ret
52 }
53
54 // Return the request that would include the given offset into the torrent data.
55 func torrentOffsetRequest(
56         torrentLength, pieceSize, chunkSize, offset int64,
57 ) (
58         r Request, ok bool,
59 ) {
60         if offset < 0 || offset >= torrentLength {
61                 return
62         }
63         r.Index = pp.Integer(offset / pieceSize)
64         r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
65         r.Length = pp.Integer(chunkSize)
66         pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
67         if r.Length > pieceLeft {
68                 r.Length = pieceLeft
69         }
70         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
71         if int64(r.Length) > torrentLeft {
72                 r.Length = pp.Integer(torrentLeft)
73         }
74         ok = true
75         return
76 }
77
78 func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
79         off = int64(r.Index)*pieceSize + int64(r.Begin)
80         if off < 0 || off >= torrentLength {
81                 panic("invalid Request")
82         }
83         return
84 }
85
86 func validateInfo(info *metainfo.Info) error {
87         if len(info.Pieces)%20 != 0 {
88                 return errors.New("pieces has invalid length")
89         }
90         if info.PieceLength == 0 {
91                 if info.TotalLength() != 0 {
92                         return errors.New("zero piece length")
93                 }
94         } else {
95                 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
96                         return errors.New("piece count and file lengths are at odds")
97                 }
98         }
99         return nil
100 }
101
102 func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec {
103         ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
104         if ret.Begin+ret.Length > pieceLength {
105                 ret.Length = pieceLength - ret.Begin
106         }
107         return ret
108 }
109
110 func connLessTrusted(l, r *Peer) bool {
111         return l.trust().Less(r.trust())
112 }
113
114 func connIsIpv6(nc interface {
115         LocalAddr() net.Addr
116 }) bool {
117         ra := nc.LocalAddr()
118         rip := addrIpOrNil(ra)
119         return rip.To4() == nil && rip.To16() != nil
120 }
121
122 func clamp(min, value, max int64) int64 {
123         if min > max {
124                 panic("harumph")
125         }
126         if value < min {
127                 value = min
128         }
129         if value > max {
130                 value = max
131         }
132         return value
133 }
134
135 func max(as ...int64) int64 {
136         ret := as[0]
137         for _, a := range as[1:] {
138                 if a > ret {
139                         ret = a
140                 }
141         }
142         return ret
143 }
144
145 func min(as ...int64) int64 {
146         ret := as[0]
147         for _, a := range as[1:] {
148                 if a < ret {
149                         ret = a
150                 }
151         }
152         return ret
153 }
154
155 func minInt(as ...int) int {
156         ret := as[0]
157         for _, a := range as[1:] {
158                 if a < ret {
159                         ret = a
160                 }
161         }
162         return ret
163 }
164
165 var unlimited = rate.NewLimiter(rate.Inf, 0)
166
167 type (
168         pieceIndex = int
169         InfoHash   = metainfo.Hash
170         IpPort     = missinggo.IpPort
171 )