]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
request can be made from Reject and Cancel messages too
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "errors"
5         "net"
6
7         "github.com/anacrolix/torrent/metainfo"
8         pp "github.com/anacrolix/torrent/peer_protocol"
9 )
10
11 type chunkSpec struct {
12         Begin, Length pp.Integer
13 }
14
15 type request struct {
16         Index pp.Integer
17         chunkSpec
18 }
19
20 func newRequest(index, begin, length pp.Integer) request {
21         return request{index, chunkSpec{begin, length}}
22 }
23
24 func newRequestFromMessage(msg *pp.Message) request {
25         switch msg.Type {
26         case pp.Request, pp.Cancel, pp.Reject:
27                 return newRequest(msg.Index, msg.Begin, msg.Length)
28         case pp.Piece:
29                 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
30         default:
31                 panic(msg.Type)
32         }
33 }
34
35 // The size in bytes of a metadata extension piece.
36 func metadataPieceSize(totalSize int, piece int) int {
37         ret := totalSize - piece*(1<<14)
38         if ret > 1<<14 {
39                 ret = 1 << 14
40         }
41         return ret
42 }
43
44 // Return the request that would include the given offset into the torrent data.
45 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
46         r request, ok bool) {
47         if offset < 0 || offset >= torrentLength {
48                 return
49         }
50         r.Index = pp.Integer(offset / pieceSize)
51         r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
52         r.Length = pp.Integer(chunkSize)
53         pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
54         if r.Length > pieceLeft {
55                 r.Length = pieceLeft
56         }
57         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
58         if int64(r.Length) > torrentLeft {
59                 r.Length = pp.Integer(torrentLeft)
60         }
61         ok = true
62         return
63 }
64
65 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
66         off = int64(r.Index)*pieceSize + int64(r.Begin)
67         if off < 0 || off >= torrentLength {
68                 panic("invalid request")
69         }
70         return
71 }
72
73 func validateInfo(info *metainfo.Info) error {
74         if len(info.Pieces)%20 != 0 {
75                 return errors.New("pieces has invalid length")
76         }
77         if info.PieceLength == 0 {
78                 if info.TotalLength() != 0 {
79                         return errors.New("zero piece length")
80                 }
81         } else {
82                 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
83                         return errors.New("piece count and file lengths are at odds")
84                 }
85         }
86         return nil
87 }
88
89 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
90         ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
91         if ret.Begin+ret.Length > pieceLength {
92                 ret.Length = pieceLength - ret.Begin
93         }
94         return ret
95 }
96
97 func connLessTrusted(l, r *connection) bool {
98         return l.netGoodPiecesDirtied() < r.netGoodPiecesDirtied()
99 }
100
101 // Convert a net.Addr to its compact IP representation. Either 4 or 16 bytes
102 // per "yourip" field of http://www.bittorrent.org/beps/bep_0010.html.
103 func addrCompactIP(addr net.Addr) (string, error) {
104         host, _, err := net.SplitHostPort(addr.String())
105         if err != nil {
106                 return "", err
107         }
108         ip := net.ParseIP(host)
109         if v4 := ip.To4(); v4 != nil {
110                 if len(v4) != 4 {
111                         panic(v4)
112                 }
113                 return string(v4), nil
114         }
115         return string(ip.To16()), nil
116 }