]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Remove errant log message
[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 )
11
12 type chunkSpec struct {
13         Begin, Length pp.Integer
14 }
15
16 type request struct {
17         Index pp.Integer
18         chunkSpec
19 }
20
21 func (r request) ToMsg(mt pp.MessageType) pp.Message {
22         return pp.Message{
23                 Type:   mt,
24                 Index:  r.Index,
25                 Begin:  r.Begin,
26                 Length: r.Length,
27         }
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 int, 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(torrentLength, pieceSize, chunkSize, offset int64) (
56         r request, ok bool) {
57         if offset < 0 || offset >= torrentLength {
58                 return
59         }
60         r.Index = pp.Integer(offset / pieceSize)
61         r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
62         r.Length = pp.Integer(chunkSize)
63         pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
64         if r.Length > pieceLeft {
65                 r.Length = pieceLeft
66         }
67         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
68         if int64(r.Length) > torrentLeft {
69                 r.Length = pp.Integer(torrentLeft)
70         }
71         ok = true
72         return
73 }
74
75 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
76         off = int64(r.Index)*pieceSize + int64(r.Begin)
77         if off < 0 || off >= torrentLength {
78                 panic("invalid request")
79         }
80         return
81 }
82
83 func validateInfo(info *metainfo.Info) error {
84         if len(info.Pieces)%20 != 0 {
85                 return errors.New("pieces has invalid length")
86         }
87         if info.PieceLength == 0 {
88                 if info.TotalLength() != 0 {
89                         return errors.New("zero piece length")
90                 }
91         } else {
92                 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
93                         return errors.New("piece count and file lengths are at odds")
94                 }
95         }
96         return nil
97 }
98
99 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
100         ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
101         if ret.Begin+ret.Length > pieceLength {
102                 ret.Length = pieceLength - ret.Begin
103         }
104         return ret
105 }
106
107 func connLessTrusted(l, r *connection) bool {
108         return l.netGoodPiecesDirtied() < r.netGoodPiecesDirtied()
109 }
110
111 // Convert a net.Addr to its compact IP representation. Either 4 or 16 bytes
112 // per "yourip" field of http://www.bittorrent.org/beps/bep_0010.html.
113 func addrCompactIP(addr net.Addr) (string, error) {
114         host, _, err := net.SplitHostPort(addr.String())
115         if err != nil {
116                 return "", err
117         }
118         ip := net.ParseIP(host)
119         if v4 := ip.To4(); v4 != nil {
120                 if len(v4) != 4 {
121                         panic(v4)
122                 }
123                 return string(v4), nil
124         }
125         return string(ip.To16()), nil
126 }
127
128 func connIsIpv6(nc net.Conn) bool {
129         ra := nc.RemoteAddr()
130         rip := missinggo.AddrIP(ra)
131         return rip.To4() == nil && rip.To16() != nil
132 }