]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Merge branch 'master' of github.com:anacrolix/torrent
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "crypto"
5         "errors"
6         "fmt"
7         "time"
8
9         "github.com/anacrolix/torrent/metainfo"
10         pp "github.com/anacrolix/torrent/peer_protocol"
11 )
12
13 const (
14         pieceHash          = crypto.SHA1
15         maxRequests        = 250        // Maximum pending requests we allow peers to send us.
16         chunkSize          = 0x4000     // 16KiB
17         bep20              = "-GT0000-" // Peer ID client identifier prefix
18         nominalDialTimeout = time.Second * 30
19         minDialTimeout     = 5 * time.Second
20 )
21
22 type (
23         InfoHash [20]byte
24         pieceSum [20]byte
25 )
26
27 func (ih *InfoHash) AsString() string {
28         return string(ih[:])
29 }
30
31 func (ih *InfoHash) HexString() string {
32         return fmt.Sprintf("%x", ih[:])
33 }
34
35 func lastChunkSpec(pieceLength pp.Integer) (cs chunkSpec) {
36         cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
37         cs.Length = pieceLength - cs.Begin
38         return
39 }
40
41 type chunkSpec struct {
42         Begin, Length pp.Integer
43 }
44
45 type request struct {
46         Index pp.Integer
47         chunkSpec
48 }
49
50 func newRequest(index, begin, length pp.Integer) request {
51         return request{index, chunkSpec{begin, length}}
52 }
53
54 var (
55         // Requested data not yet available.
56         errDataNotReady = errors.New("data not ready")
57 )
58
59 // The size in bytes of a metadata extension piece.
60 func metadataPieceSize(totalSize int, piece int) int {
61         ret := totalSize - piece*(1<<14)
62         if ret > 1<<14 {
63                 ret = 1 << 14
64         }
65         return ret
66 }
67
68 type superer interface {
69         Super() interface{}
70 }
71
72 // Returns ok if there's a parent, and it's not nil.
73 func super(child interface{}) (parent interface{}, ok bool) {
74         s, ok := child.(superer)
75         if !ok {
76                 return
77         }
78         parent = s.Super()
79         ok = parent != nil
80         return
81 }
82
83 // Return the request that would include the given offset into the torrent data.
84 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
85         r request, ok bool) {
86         if offset < 0 || offset >= torrentLength {
87                 return
88         }
89         r.Index = pp.Integer(offset / pieceSize)
90         r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
91         r.Length = pp.Integer(chunkSize)
92         pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
93         if r.Length > pieceLeft {
94                 r.Length = pieceLeft
95         }
96         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
97         if int64(r.Length) > torrentLeft {
98                 r.Length = pp.Integer(torrentLeft)
99         }
100         ok = true
101         return
102 }
103
104 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
105         off = int64(r.Index)*pieceSize + int64(r.Begin)
106         if off < 0 || off >= torrentLength {
107                 panic("invalid request")
108         }
109         return
110 }
111
112 func validateInfo(info *metainfo.Info) error {
113         if len(info.Pieces)%20 != 0 {
114                 return errors.New("pieces has invalid length")
115         }
116         if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
117                 return errors.New("piece count and file lengths are at odds")
118         }
119         return nil
120 }