misc.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package torrent
  2. import (
  3. "errors"
  4. "net"
  5. "github.com/RoaringBitmap/roaring"
  6. "github.com/anacrolix/missinggo/v2"
  7. "golang.org/x/time/rate"
  8. "github.com/anacrolix/torrent/metainfo"
  9. pp "github.com/anacrolix/torrent/peer_protocol"
  10. "github.com/anacrolix/torrent/types"
  11. "github.com/anacrolix/torrent/types/infohash"
  12. )
  13. type (
  14. Request = types.Request
  15. ChunkSpec = types.ChunkSpec
  16. PiecePriority = types.PiecePriority
  17. )
  18. const (
  19. PiecePriorityNormal = types.PiecePriorityNormal
  20. PiecePriorityNone = types.PiecePriorityNone
  21. PiecePriorityNow = types.PiecePriorityNow
  22. PiecePriorityReadahead = types.PiecePriorityReadahead
  23. PiecePriorityNext = types.PiecePriorityNext
  24. PiecePriorityHigh = types.PiecePriorityHigh
  25. )
  26. func newRequest(index, begin, length pp.Integer) Request {
  27. return Request{index, ChunkSpec{begin, length}}
  28. }
  29. func newRequestFromMessage(msg *pp.Message) Request {
  30. switch msg.Type {
  31. case pp.Request, pp.Cancel, pp.Reject:
  32. return newRequest(msg.Index, msg.Begin, msg.Length)
  33. case pp.Piece:
  34. return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
  35. default:
  36. panic(msg.Type)
  37. }
  38. }
  39. // The size in bytes of a metadata extension piece.
  40. func metadataPieceSize(totalSize, piece int) int {
  41. ret := totalSize - piece*(1<<14)
  42. if ret > 1<<14 {
  43. ret = 1 << 14
  44. }
  45. return ret
  46. }
  47. // Return the request that would include the given offset into the torrent data.
  48. func torrentOffsetRequest(
  49. torrentLength, pieceSize, chunkSize, offset int64,
  50. ) (
  51. r Request, ok bool,
  52. ) {
  53. if offset < 0 || offset >= torrentLength {
  54. return
  55. }
  56. r.Index = pp.Integer(offset / pieceSize)
  57. r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
  58. r.Length = pp.Integer(chunkSize)
  59. pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
  60. if r.Length > pieceLeft {
  61. r.Length = pieceLeft
  62. }
  63. torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
  64. if int64(r.Length) > torrentLeft {
  65. r.Length = pp.Integer(torrentLeft)
  66. }
  67. ok = true
  68. return
  69. }
  70. func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
  71. off = int64(r.Index)*pieceSize + int64(r.Begin)
  72. if off < 0 || off >= torrentLength {
  73. panic("invalid Request")
  74. }
  75. return
  76. }
  77. func validateInfo(info *metainfo.Info) error {
  78. if len(info.Pieces)%20 != 0 {
  79. return errors.New("pieces has invalid length")
  80. }
  81. if info.PieceLength == 0 {
  82. if info.TotalLength() != 0 {
  83. return errors.New("zero piece length")
  84. }
  85. } else if !info.HasV2() {
  86. // TotalLength returns different values for V1 and V2 depending on whether v1 pad files are
  87. // counted. Split the interface into several methods?
  88. if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
  89. return errors.New("piece count and file lengths are at odds")
  90. }
  91. }
  92. return nil
  93. }
  94. func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec {
  95. ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
  96. if ret.Begin+ret.Length > pieceLength {
  97. ret.Length = pieceLength - ret.Begin
  98. }
  99. return ret
  100. }
  101. func connLessTrusted(l, r *Peer) bool {
  102. return l.trust().Less(r.trust())
  103. }
  104. func connIsIpv6(nc interface {
  105. LocalAddr() net.Addr
  106. },
  107. ) bool {
  108. ra := nc.LocalAddr()
  109. rip := addrIpOrNil(ra)
  110. return rip.To4() == nil && rip.To16() != nil
  111. }
  112. func clamp(min, value, max int64) int64 {
  113. if min > max {
  114. panic("harumph")
  115. }
  116. if value < min {
  117. value = min
  118. }
  119. if value > max {
  120. value = max
  121. }
  122. return value
  123. }
  124. func max(as ...int64) int64 {
  125. ret := as[0]
  126. for _, a := range as[1:] {
  127. if a > ret {
  128. ret = a
  129. }
  130. }
  131. return ret
  132. }
  133. func maxInt(as ...int) int {
  134. ret := as[0]
  135. for _, a := range as[1:] {
  136. if a > ret {
  137. ret = a
  138. }
  139. }
  140. return ret
  141. }
  142. func minInt(as ...int) int {
  143. ret := as[0]
  144. for _, a := range as[1:] {
  145. if a < ret {
  146. ret = a
  147. }
  148. }
  149. return ret
  150. }
  151. var unlimited = rate.NewLimiter(rate.Inf, 0)
  152. type (
  153. pieceIndex = int
  154. // Deprecated: Use infohash.T directly to avoid unnecessary imports.
  155. InfoHash = infohash.T
  156. IpPort = missinggo.IpPort
  157. )
  158. func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) {
  159. for i, b := range slice {
  160. if b {
  161. rb.AddInt(i)
  162. }
  163. }
  164. return
  165. }