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