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