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