]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Move IpPort to missinggo
[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 pp.Integer, 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 func connIsIpv6(nc interface {
113         LocalAddr() net.Addr
114 }) bool {
115         ra := nc.LocalAddr()
116         rip := missinggo.AddrIP(ra)
117         return rip.To4() == nil && rip.To16() != nil
118 }
119
120 func clamp(min, value, max int64) int64 {
121         if min > max {
122                 panic("harumph")
123         }
124         if value < min {
125                 value = min
126         }
127         if value > max {
128                 value = max
129         }
130         return value
131 }
132
133 func max(as ...int64) int64 {
134         ret := as[0]
135         for _, a := range as[1:] {
136                 if a > ret {
137                         ret = a
138                 }
139         }
140         return ret
141 }
142
143 func min(as ...int64) int64 {
144         ret := as[0]
145         for _, a := range as[1:] {
146                 if a < ret {
147                         ret = a
148                 }
149         }
150         return ret
151 }
152
153 var unlimited = rate.NewLimiter(rate.Inf, 0)
154
155 type (
156         pieceIndex = int
157         InfoHash   = metainfo.Hash
158         IpPort     = missinggo.IpPort
159 )