]> Sergey Matveev's repositories - btrtrc.git/blob - peerconn.go
Improve some internal piece priority naming
[btrtrc.git] / peerconn.go
1 package torrent
2
3 import (
4         "bufio"
5         "bytes"
6         "context"
7         "errors"
8         "fmt"
9         "io"
10         "math/rand"
11         "net"
12         "net/netip"
13         "strconv"
14         "strings"
15         "sync/atomic"
16         "time"
17
18         "github.com/RoaringBitmap/roaring"
19         "github.com/anacrolix/generics"
20         . "github.com/anacrolix/generics"
21         "github.com/anacrolix/log"
22         "github.com/anacrolix/missinggo/v2/bitmap"
23         "github.com/anacrolix/multiless"
24         "golang.org/x/exp/maps"
25         "golang.org/x/time/rate"
26
27         "github.com/anacrolix/torrent/bencode"
28         "github.com/anacrolix/torrent/internal/alloclim"
29         "github.com/anacrolix/torrent/merkle"
30         "github.com/anacrolix/torrent/metainfo"
31         "github.com/anacrolix/torrent/mse"
32         pp "github.com/anacrolix/torrent/peer_protocol"
33         utHolepunch "github.com/anacrolix/torrent/peer_protocol/ut-holepunch"
34 )
35
36 // Maintains the state of a BitTorrent-protocol based connection with a peer.
37 type PeerConn struct {
38         Peer
39
40         // Move to PeerConn?
41         protocolLogger log.Logger
42
43         // BEP 52
44         v2 bool
45
46         // A string that should identify the PeerConn's net.Conn endpoints. The net.Conn could
47         // be wrapping WebRTC, uTP, or TCP etc. Used in writing the conn status for peers.
48         connString string
49
50         // See BEP 3 etc.
51         PeerID             PeerID
52         PeerExtensionBytes pp.PeerExtensionBits
53         PeerListenPort     int
54
55         // The local extended protocols to advertise in the extended handshake, and to support receiving
56         // from the peer. This will point to the Client default when the PeerConnAdded callback is
57         // invoked. Do not modify this, point it to your own instance. Do not modify the destination
58         // after returning from the callback.
59         LocalLtepProtocolMap *LocalLtepProtocolMap
60
61         // The actual Conn, used for closing, and setting socket options. Do not use methods on this
62         // while holding any mutexes.
63         conn net.Conn
64         // The Reader and Writer for this Conn, with hooks installed for stats,
65         // limiting, deadlines etc.
66         w io.Writer
67         r io.Reader
68
69         messageWriter peerConnMsgWriter
70
71         // The peer's extension map, as sent in their extended handshake.
72         PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber
73         PeerClientName   atomic.Value
74         uploadTimer      *time.Timer
75         pex              pexConnState
76
77         // The pieces the peer has claimed to have.
78         _peerPieces roaring.Bitmap
79         // The peer has everything. This can occur due to a special message, when
80         // we may not even know the number of pieces in the torrent yet.
81         peerSentHaveAll bool
82
83         peerRequestDataAllocLimiter alloclim.Limiter
84
85         outstandingHolepunchingRendezvous map[netip.AddrPort]struct{}
86
87         // Hash requests sent to the peer. If there's an issue we probably don't want to reissue these,
88         // because I haven't implemented it smart enough yet.
89         sentHashRequests map[hashRequest]struct{}
90         // Hash pieces received from the peer, mapped from pieces root to piece layer hashes. This way
91         // we can verify all the pieces for a file when they're all arrived before submitting them to
92         // the torrent.
93         receivedHashPieces map[[32]byte][][32]byte
94 }
95
96 func (cn *PeerConn) pexStatus() string {
97         if !cn.bitExtensionEnabled(pp.ExtensionBitLtep) {
98                 return "extended protocol disabled"
99         }
100         if cn.PeerExtensionIDs == nil {
101                 return "pending extended handshake"
102         }
103         if !cn.supportsExtension(pp.ExtensionNamePex) {
104                 return "unsupported"
105         }
106         if true {
107                 return fmt.Sprintf(
108                         "%v conns, %v unsent events",
109                         len(cn.pex.remoteLiveConns),
110                         cn.pex.numPending(),
111                 )
112         } else {
113                 // This alternative branch prints out the remote live conn addresses.
114                 return fmt.Sprintf(
115                         "%v conns, %v unsent events",
116                         strings.Join(generics.SliceMap(
117                                 maps.Keys(cn.pex.remoteLiveConns),
118                                 func(from netip.AddrPort) string {
119                                         return from.String()
120                                 }), ","),
121                         cn.pex.numPending(),
122                 )
123         }
124 }
125
126 func (cn *PeerConn) peerImplStatusLines() []string {
127         return []string{
128                 cn.connString,
129                 fmt.Sprintf("peer id: %+q", cn.PeerID),
130                 fmt.Sprintf("extensions: %v", cn.PeerExtensionBytes),
131                 fmt.Sprintf("ltep extensions: %v", cn.PeerExtensionIDs),
132                 fmt.Sprintf("pex: %s", cn.pexStatus()),
133         }
134 }
135
136 // Returns true if the connection is over IPv6.
137 func (cn *PeerConn) ipv6() bool {
138         ip := cn.remoteIp()
139         if ip.To4() != nil {
140                 return false
141         }
142         return len(ip) == net.IPv6len
143 }
144
145 // Returns true the if the dialer/initiator has the higher client peer ID. See
146 // https://github.com/arvidn/libtorrent/blame/272828e1cc37b042dfbbafa539222d8533e99755/src/bt_peer_connection.cpp#L3536-L3557.
147 // As far as I can tell, Transmission just keeps the oldest connection.
148 func (cn *PeerConn) isPreferredDirection() bool {
149         // True if our client peer ID is higher than the remote's peer ID.
150         return bytes.Compare(cn.PeerID[:], cn.t.cl.peerID[:]) < 0 == cn.outgoing
151 }
152
153 // Returns whether the left connection should be preferred over the right one,
154 // considering only their networking properties. If ok is false, we can't
155 // decide.
156 func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) bool {
157         var ml multiless.Computation
158         ml = ml.Bool(r.isPreferredDirection(), l.isPreferredDirection())
159         ml = ml.Bool(l.utp(), r.utp())
160         ml = ml.Bool(r.ipv6(), l.ipv6())
161         return ml.Less()
162 }
163
164 func (cn *PeerConn) peerHasAllPieces() (all, known bool) {
165         if cn.peerSentHaveAll {
166                 return true, true
167         }
168         if !cn.t.haveInfo() {
169                 return false, false
170         }
171         return cn._peerPieces.GetCardinality() == uint64(cn.t.numPieces()), true
172 }
173
174 func (cn *PeerConn) onGotInfo(info *metainfo.Info) {
175         cn.setNumPieces(info.NumPieces())
176 }
177
178 // Correct the PeerPieces slice length. Return false if the existing slice is invalid, such as by
179 // receiving badly sized BITFIELD, or invalid HAVE messages.
180 func (cn *PeerConn) setNumPieces(num pieceIndex) {
181         cn._peerPieces.RemoveRange(bitmap.BitRange(num), bitmap.ToEnd)
182         cn.peerPiecesChanged()
183 }
184
185 func (cn *PeerConn) peerPieces() *roaring.Bitmap {
186         return &cn._peerPieces
187 }
188
189 func (cn *PeerConn) connectionFlags() string {
190         var sb strings.Builder
191         add := func(s string) {
192                 if sb.Len() > 0 {
193                         sb.WriteByte(',')
194                 }
195                 sb.WriteString(s)
196         }
197         // From first relevant to last.
198         add(string(cn.Discovery))
199         if cn.utp() {
200                 add("U")
201         }
202         if cn.cryptoMethod == mse.CryptoMethodRC4 {
203                 add("E")
204         } else if cn.headerEncrypted {
205                 add("e")
206         }
207         if cn.v2 {
208                 add("v2")
209         } else {
210                 add("v1")
211         }
212         return sb.String()
213 }
214
215 func (cn *PeerConn) utp() bool {
216         return parseNetworkString(cn.Network).Udp
217 }
218
219 func (cn *PeerConn) onClose() {
220         if cn.pex.IsEnabled() {
221                 cn.pex.Close()
222         }
223         cn.tickleWriter()
224         if cn.conn != nil {
225                 go cn.conn.Close()
226         }
227         if cb := cn.callbacks.PeerConnClosed; cb != nil {
228                 cb(cn)
229         }
230 }
231
232 // Writes a message into the write buffer. Returns whether it's okay to keep writing. Writing is
233 // done asynchronously, so it may be that we're not able to honour backpressure from this method.
234 func (cn *PeerConn) write(msg pp.Message) bool {
235         torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
236         // We don't need to track bytes here because the connection's Writer has that behaviour injected
237         // (although there's some delay between us buffering the message, and the connection writer
238         // flushing it out.).
239         notFull := cn.messageWriter.write(msg)
240         // Last I checked only Piece messages affect stats, and we don't write those.
241         cn.wroteMsg(&msg)
242         cn.tickleWriter()
243         return notFull
244 }
245
246 func (cn *PeerConn) requestMetadataPiece(index int) {
247         eID := cn.PeerExtensionIDs[pp.ExtensionNameMetadata]
248         if eID == pp.ExtensionDeleteNumber {
249                 return
250         }
251         if index < len(cn.metadataRequests) && cn.metadataRequests[index] {
252                 return
253         }
254         cn.protocolLogger.WithDefaultLevel(log.Debug).Printf("requesting metadata piece %d", index)
255         cn.write(pp.MetadataExtensionRequestMsg(eID, index))
256         for index >= len(cn.metadataRequests) {
257                 cn.metadataRequests = append(cn.metadataRequests, false)
258         }
259         cn.metadataRequests[index] = true
260 }
261
262 func (cn *PeerConn) requestedMetadataPiece(index int) bool {
263         return index < len(cn.metadataRequests) && cn.metadataRequests[index]
264 }
265
266 func (cn *PeerConn) onPeerSentCancel(r Request) {
267         if _, ok := cn.peerRequests[r]; !ok {
268                 torrent.Add("unexpected cancels received", 1)
269                 return
270         }
271         if cn.fastEnabled() {
272                 cn.reject(r)
273         } else {
274                 delete(cn.peerRequests, r)
275         }
276 }
277
278 func (cn *PeerConn) choke(msg messageWriter) (more bool) {
279         if cn.choking {
280                 return true
281         }
282         cn.choking = true
283         more = msg(pp.Message{
284                 Type: pp.Choke,
285         })
286         if !cn.fastEnabled() {
287                 cn.deleteAllPeerRequests()
288         }
289         return
290 }
291
292 func (cn *PeerConn) deleteAllPeerRequests() {
293         for _, state := range cn.peerRequests {
294                 state.allocReservation.Drop()
295         }
296         cn.peerRequests = nil
297 }
298
299 func (cn *PeerConn) unchoke(msg func(pp.Message) bool) bool {
300         if !cn.choking {
301                 return true
302         }
303         cn.choking = false
304         return msg(pp.Message{
305                 Type: pp.Unchoke,
306         })
307 }
308
309 func (pc *PeerConn) writeInterested(interested bool) bool {
310         return pc.write(pp.Message{
311                 Type: func() pp.MessageType {
312                         if interested {
313                                 return pp.Interested
314                         } else {
315                                 return pp.NotInterested
316                         }
317                 }(),
318         })
319 }
320
321 func (me *PeerConn) _request(r Request) bool {
322         return me.write(pp.Message{
323                 Type:   pp.Request,
324                 Index:  r.Index,
325                 Begin:  r.Begin,
326                 Length: r.Length,
327         })
328 }
329
330 func (me *PeerConn) _cancel(r RequestIndex) bool {
331         me.write(makeCancelMessage(me.t.requestIndexToRequest(r)))
332         return me.remoteRejectsCancels()
333 }
334
335 // Whether we should expect a reject message after sending a cancel.
336 func (me *PeerConn) remoteRejectsCancels() bool {
337         if !me.fastEnabled() {
338                 return false
339         }
340         if me.remoteIsTransmission() {
341                 // Transmission did not send rejects for received cancels. See
342                 // https://github.com/transmission/transmission/pull/2275. Fixed in 4.0.0-beta.1 onward in
343                 // https://github.com/transmission/transmission/commit/76719bf34c255da4fca991c2ad3fa4b65d2154b1.
344                 // Peer ID prefix scheme described
345                 // https://github.com/transmission/transmission/blob/7ec7607bbcf0fa99bd4b157b9b0f0c411d59f45d/CMakeLists.txt#L128-L149.
346                 return me.PeerID[3] >= '4'
347         }
348         return true
349 }
350
351 func (cn *PeerConn) fillWriteBuffer() {
352         if cn.messageWriter.writeBuffer.Len() > writeBufferLowWaterLen {
353                 // Fully committing to our max requests requires sufficient space (see
354                 // maxLocalToRemoteRequests). Flush what we have instead. We also prefer always to make
355                 // requests than to do PEX or upload, so we short-circuit before handling those. Any update
356                 // request reason will not be cleared, so we'll come right back here when there's space. We
357                 // can't do this in maybeUpdateActualRequestState because it's a method on Peer and has no
358                 // knowledge of write buffers.
359                 return
360         }
361         cn.requestMissingHashes()
362         cn.maybeUpdateActualRequestState()
363         if cn.pex.IsEnabled() {
364                 if flow := cn.pex.Share(cn.write); !flow {
365                         return
366                 }
367         }
368         cn.upload(cn.write)
369 }
370
371 func (cn *PeerConn) have(piece pieceIndex) {
372         if cn.sentHaves.Get(bitmap.BitIndex(piece)) {
373                 return
374         }
375         cn.write(pp.Message{
376                 Type:  pp.Have,
377                 Index: pp.Integer(piece),
378         })
379         cn.sentHaves.Add(bitmap.BitIndex(piece))
380 }
381
382 func (cn *PeerConn) postBitfield() {
383         if cn.sentHaves.Len() != 0 {
384                 panic("bitfield must be first have-related message sent")
385         }
386         if !cn.t.haveAnyPieces() {
387                 return
388         }
389         cn.write(pp.Message{
390                 Type:     pp.Bitfield,
391                 Bitfield: cn.t.bitfield(),
392         })
393         cn.sentHaves = bitmap.Bitmap{cn.t._completedPieces.Clone()}
394 }
395
396 func (cn *PeerConn) handleUpdateRequests() {
397         // The writer determines the request state as needed when it can write.
398         cn.tickleWriter()
399 }
400
401 func (cn *PeerConn) raisePeerMinPieces(newMin pieceIndex) {
402         if newMin > cn.peerMinPieces {
403                 cn.peerMinPieces = newMin
404         }
405 }
406
407 func (cn *PeerConn) peerSentHave(piece pieceIndex) error {
408         if cn.t.haveInfo() && piece >= cn.t.numPieces() || piece < 0 {
409                 return errors.New("invalid piece")
410         }
411         if cn.peerHasPiece(piece) {
412                 return nil
413         }
414         cn.raisePeerMinPieces(piece + 1)
415         if !cn.peerHasPiece(piece) {
416                 cn.t.incPieceAvailability(piece)
417         }
418         cn._peerPieces.Add(uint32(piece))
419         if cn.t.wantPieceIndex(piece) {
420                 cn.updateRequests("have")
421         }
422         cn.peerPiecesChanged()
423         return nil
424 }
425
426 func (cn *PeerConn) peerSentBitfield(bf []bool) error {
427         if len(bf)%8 != 0 {
428                 panic("expected bitfield length divisible by 8")
429         }
430         // We know that the last byte means that at most the last 7 bits are wasted.
431         cn.raisePeerMinPieces(pieceIndex(len(bf) - 7))
432         if cn.t.haveInfo() && len(bf) > int(cn.t.numPieces()) {
433                 // Ignore known excess pieces.
434                 bf = bf[:cn.t.numPieces()]
435         }
436         bm := boolSliceToBitmap(bf)
437         if cn.t.haveInfo() && pieceIndex(bm.GetCardinality()) == cn.t.numPieces() {
438                 cn.onPeerHasAllPieces()
439                 return nil
440         }
441         if !bm.IsEmpty() {
442                 cn.raisePeerMinPieces(pieceIndex(bm.Maximum()) + 1)
443         }
444         shouldUpdateRequests := false
445         if cn.peerSentHaveAll {
446                 if !cn.t.deleteConnWithAllPieces(&cn.Peer) {
447                         panic(cn)
448                 }
449                 cn.peerSentHaveAll = false
450                 if !cn._peerPieces.IsEmpty() {
451                         panic("if peer has all, we expect no individual peer pieces to be set")
452                 }
453         } else {
454                 bm.Xor(&cn._peerPieces)
455         }
456         cn.peerSentHaveAll = false
457         // bm is now 'on' for pieces that are changing
458         bm.Iterate(func(x uint32) bool {
459                 pi := pieceIndex(x)
460                 if cn._peerPieces.Contains(x) {
461                         // Then we must be losing this piece
462                         cn.t.decPieceAvailability(pi)
463                 } else {
464                         if !shouldUpdateRequests && cn.t.wantPieceIndex(pieceIndex(x)) {
465                                 shouldUpdateRequests = true
466                         }
467                         // We must be gaining this piece
468                         cn.t.incPieceAvailability(pieceIndex(x))
469                 }
470                 return true
471         })
472         // Apply the changes. If we had everything previously, this should be empty, so xor is the same
473         // as or.
474         cn._peerPieces.Xor(&bm)
475         if shouldUpdateRequests {
476                 cn.updateRequests("bitfield")
477         }
478         // We didn't guard this before, I see no reason to do it now.
479         cn.peerPiecesChanged()
480         return nil
481 }
482
483 func (cn *PeerConn) onPeerHasAllPiecesNoTriggers() {
484         t := cn.t
485         if t.haveInfo() {
486                 cn._peerPieces.Iterate(func(x uint32) bool {
487                         t.decPieceAvailability(pieceIndex(x))
488                         return true
489                 })
490         }
491         t.addConnWithAllPieces(&cn.Peer)
492         cn.peerSentHaveAll = true
493         cn._peerPieces.Clear()
494 }
495
496 func (cn *PeerConn) onPeerHasAllPieces() {
497         cn.onPeerHasAllPiecesNoTriggers()
498         cn.peerHasAllPiecesTriggers()
499 }
500
501 func (cn *PeerConn) peerHasAllPiecesTriggers() {
502         if !cn.t._pendingPieces.IsEmpty() {
503                 cn.updateRequests("Peer.onPeerHasAllPieces")
504         }
505         cn.peerPiecesChanged()
506 }
507
508 func (cn *PeerConn) onPeerSentHaveAll() error {
509         cn.onPeerHasAllPieces()
510         return nil
511 }
512
513 func (cn *PeerConn) peerSentHaveNone() error {
514         if !cn.peerSentHaveAll {
515                 cn.t.decPeerPieceAvailability(&cn.Peer)
516         }
517         cn._peerPieces.Clear()
518         cn.peerSentHaveAll = false
519         cn.peerPiecesChanged()
520         return nil
521 }
522
523 func (c *PeerConn) requestPendingMetadata() {
524         if c.t.haveInfo() {
525                 return
526         }
527         if c.PeerExtensionIDs[pp.ExtensionNameMetadata] == 0 {
528                 // Peer doesn't support this.
529                 return
530         }
531         // Request metadata pieces that we don't have in a random order.
532         var pending []int
533         for index := 0; index < c.t.metadataPieceCount(); index++ {
534                 if !c.t.haveMetadataPiece(index) && !c.requestedMetadataPiece(index) {
535                         pending = append(pending, index)
536                 }
537         }
538         rand.Shuffle(len(pending), func(i, j int) { pending[i], pending[j] = pending[j], pending[i] })
539         for _, i := range pending {
540                 c.requestMetadataPiece(i)
541         }
542 }
543
544 func (cn *PeerConn) wroteMsg(msg *pp.Message) {
545         torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
546         if msg.Type == pp.Extended {
547                 for name, id := range cn.PeerExtensionIDs {
548                         if id != msg.ExtendedID {
549                                 continue
550                         }
551                         torrent.Add(fmt.Sprintf("Extended messages written for protocol %q", name), 1)
552                 }
553         }
554         cn.allStats(func(cs *ConnStats) { cs.wroteMsg(msg) })
555 }
556
557 func (cn *PeerConn) wroteBytes(n int64) {
558         cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesWritten }))
559 }
560
561 func (c *PeerConn) fastEnabled() bool {
562         return c.PeerExtensionBytes.SupportsFast() && c.t.cl.config.Extensions.SupportsFast()
563 }
564
565 func (c *PeerConn) reject(r Request) {
566         if !c.fastEnabled() {
567                 panic("fast not enabled")
568         }
569         c.write(r.ToMsg(pp.Reject))
570         // It is possible to reject a request before it is added to peer requests due to being invalid.
571         if state, ok := c.peerRequests[r]; ok {
572                 state.allocReservation.Drop()
573                 delete(c.peerRequests, r)
574         }
575 }
576
577 func (c *PeerConn) maximumPeerRequestChunkLength() (_ Option[int]) {
578         uploadRateLimiter := c.t.cl.config.UploadRateLimiter
579         if uploadRateLimiter.Limit() == rate.Inf {
580                 return
581         }
582         return Some(uploadRateLimiter.Burst())
583 }
584
585 // startFetch is for testing purposes currently.
586 func (c *PeerConn) onReadRequest(r Request, startFetch bool) error {
587         requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1)
588         if _, ok := c.peerRequests[r]; ok {
589                 torrent.Add("duplicate requests received", 1)
590                 if c.fastEnabled() {
591                         return errors.New("received duplicate request with fast enabled")
592                 }
593                 return nil
594         }
595         if c.choking {
596                 torrent.Add("requests received while choking", 1)
597                 if c.fastEnabled() {
598                         torrent.Add("requests rejected while choking", 1)
599                         c.reject(r)
600                 }
601                 return nil
602         }
603         // TODO: What if they've already requested this?
604         if len(c.peerRequests) >= localClientReqq {
605                 torrent.Add("requests received while queue full", 1)
606                 if c.fastEnabled() {
607                         c.reject(r)
608                 }
609                 // BEP 6 says we may close here if we choose.
610                 return nil
611         }
612         if opt := c.maximumPeerRequestChunkLength(); opt.Ok && int(r.Length) > opt.Value {
613                 err := fmt.Errorf("peer requested chunk too long (%v)", r.Length)
614                 c.protocolLogger.Levelf(log.Warning, err.Error())
615                 if c.fastEnabled() {
616                         c.reject(r)
617                         return nil
618                 } else {
619                         return err
620                 }
621         }
622         if !c.t.havePiece(pieceIndex(r.Index)) {
623                 // TODO: Tell the peer we don't have the piece, and reject this request.
624                 requestsReceivedForMissingPieces.Add(1)
625                 return fmt.Errorf("peer requested piece we don't have: %v", r.Index.Int())
626         }
627         pieceLength := c.t.pieceLength(pieceIndex(r.Index))
628         // Check this after we know we have the piece, so that the piece length will be known.
629         if chunkOverflowsPiece(r.ChunkSpec, pieceLength) {
630                 torrent.Add("bad requests received", 1)
631                 return errors.New("chunk overflows piece")
632         }
633         if c.peerRequests == nil {
634                 c.peerRequests = make(map[Request]*peerRequestState, localClientReqq)
635         }
636         value := &peerRequestState{
637                 allocReservation: c.peerRequestDataAllocLimiter.Reserve(int64(r.Length)),
638         }
639         c.peerRequests[r] = value
640         if startFetch {
641                 // TODO: Limit peer request data read concurrency.
642                 go c.peerRequestDataReader(r, value)
643         }
644         return nil
645 }
646
647 func (c *PeerConn) peerRequestDataReader(r Request, prs *peerRequestState) {
648         // Should we depend on Torrent closure here? I think it's okay to get cancelled from elsewhere,
649         // or fail to read and then cleanup. Also, we used to hang here if the reservation was never
650         // dropped, that was fixed.
651         ctx := context.Background()
652         err := prs.allocReservation.Wait(ctx)
653         if err != nil {
654                 c.logger.WithDefaultLevel(log.Debug).Levelf(log.ErrorLevel(err), "waiting for alloc limit reservation: %v", err)
655                 return
656         }
657         b, err := c.readPeerRequestData(r)
658         c.locker().Lock()
659         defer c.locker().Unlock()
660         if err != nil {
661                 c.peerRequestDataReadFailed(err, r)
662         } else {
663                 if b == nil {
664                         panic("data must be non-nil to trigger send")
665                 }
666                 torrent.Add("peer request data read successes", 1)
667                 prs.data = b
668                 // This might be required for the error case too (#752 and #753).
669                 c.tickleWriter()
670         }
671 }
672
673 // If this is maintained correctly, we might be able to support optional synchronous reading for
674 // chunk sending, the way it used to work.
675 func (c *PeerConn) peerRequestDataReadFailed(err error, r Request) {
676         torrent.Add("peer request data read failures", 1)
677         logLevel := log.Warning
678         if c.t.hasStorageCap() {
679                 // It's expected that pieces might drop. See
680                 // https://github.com/anacrolix/torrent/issues/702#issuecomment-1000953313.
681                 logLevel = log.Debug
682         }
683         c.logger.Levelf(logLevel, "error reading chunk for peer Request %v: %v", r, err)
684         if c.t.closed.IsSet() {
685                 return
686         }
687         i := pieceIndex(r.Index)
688         if c.t.pieceComplete(i) {
689                 // There used to be more code here that just duplicated the following break. Piece
690                 // completions are currently cached, so I'm not sure how helpful this update is, except to
691                 // pull any completion changes pushed to the storage backend in failed reads that got us
692                 // here.
693                 c.t.updatePieceCompletion(i)
694         }
695         // We've probably dropped a piece from storage, but there's no way to communicate this to the
696         // peer. If they ask for it again, we kick them allowing us to send them updated piece states if
697         // we reconnect. TODO: Instead, we could just try to update them with Bitfield or HaveNone and
698         // if they kick us for breaking protocol, on reconnect we will be compliant again (at least
699         // initially).
700         if c.fastEnabled() {
701                 c.reject(r)
702         } else {
703                 if c.choking {
704                         // If fast isn't enabled, I think we would have wiped all peer requests when we last
705                         // choked, and requests while we're choking would be ignored. It could be possible that
706                         // a peer request data read completed concurrently to it being deleted elsewhere.
707                         c.protocolLogger.WithDefaultLevel(log.Warning).Printf("already choking peer, requests might not be rejected correctly")
708                 }
709                 // Choking a non-fast peer should cause them to flush all their requests.
710                 c.choke(c.write)
711         }
712 }
713
714 func (c *PeerConn) readPeerRequestData(r Request) ([]byte, error) {
715         b := make([]byte, r.Length)
716         p := c.t.info.Piece(int(r.Index))
717         n, err := c.t.readAt(b, p.Offset()+int64(r.Begin))
718         if n == len(b) {
719                 if err == io.EOF {
720                         err = nil
721                 }
722         } else {
723                 if err == nil {
724                         panic("expected error")
725                 }
726         }
727         return b, err
728 }
729
730 func (c *PeerConn) logProtocolBehaviour(level log.Level, format string, arg ...interface{}) {
731         c.protocolLogger.WithContextText(fmt.Sprintf(
732                 "peer id %q, ext v %q", c.PeerID, c.PeerClientName.Load(),
733         )).SkipCallers(1).Levelf(level, format, arg...)
734 }
735
736 // Processes incoming BitTorrent wire-protocol messages. The client lock is held upon entry and
737 // exit. Returning will end the connection.
738 func (c *PeerConn) mainReadLoop() (err error) {
739         defer func() {
740                 if err != nil {
741                         torrent.Add("connection.mainReadLoop returned with error", 1)
742                 } else {
743                         torrent.Add("connection.mainReadLoop returned with no error", 1)
744                 }
745         }()
746         t := c.t
747         cl := t.cl
748
749         decoder := pp.Decoder{
750                 R:         bufio.NewReaderSize(c.r, 1<<17),
751                 MaxLength: 4 * pp.Integer(max(int64(t.chunkSize), defaultChunkSize)),
752                 Pool:      &t.chunkPool,
753         }
754         for {
755                 var msg pp.Message
756                 func() {
757                         cl.unlock()
758                         defer cl.lock()
759                         err = decoder.Decode(&msg)
760                         if err != nil {
761                                 err = fmt.Errorf("decoding message: %w", err)
762                         }
763                 }()
764                 // Do this before checking closed.
765                 if cb := c.callbacks.ReadMessage; cb != nil && err == nil {
766                         cb(c, &msg)
767                 }
768                 if t.closed.IsSet() || c.closed.IsSet() {
769                         return nil
770                 }
771                 if err != nil {
772                         err = log.WithLevel(log.Info, err)
773                         return err
774                 }
775                 c.lastMessageReceived = time.Now()
776                 if msg.Keepalive {
777                         receivedKeepalives.Add(1)
778                         continue
779                 }
780                 messageTypesReceived.Add(msg.Type.String(), 1)
781                 if msg.Type.FastExtension() && !c.fastEnabled() {
782                         runSafeExtraneous(func() { torrent.Add("fast messages received when extension is disabled", 1) })
783                         return fmt.Errorf("received fast extension message (type=%v) but extension is disabled", msg.Type)
784                 }
785                 switch msg.Type {
786                 case pp.Choke:
787                         if c.peerChoking {
788                                 break
789                         }
790                         if !c.fastEnabled() {
791                                 c.deleteAllRequests("choked by non-fast PeerConn")
792                         } else {
793                                 // We don't decrement pending requests here, let's wait for the peer to either
794                                 // reject or satisfy the outstanding requests. Additionally, some peers may unchoke
795                                 // us and resume where they left off, we don't want to have piled on to those chunks
796                                 // in the meanwhile. I think a peer's ability to abuse this should be limited: they
797                                 // could let us request a lot of stuff, then choke us and never reject, but they're
798                                 // only a single peer, our chunk balancing should smooth over this abuse.
799                         }
800                         c.peerChoking = true
801                         c.updateExpectingChunks()
802                 case pp.Unchoke:
803                         if !c.peerChoking {
804                                 // Some clients do this for some reason. Transmission doesn't error on this, so we
805                                 // won't for consistency.
806                                 c.logProtocolBehaviour(log.Debug, "received unchoke when already unchoked")
807                                 break
808                         }
809                         c.peerChoking = false
810                         preservedCount := 0
811                         c.requestState.Requests.Iterate(func(x RequestIndex) bool {
812                                 if !c.peerAllowedFast.Contains(c.t.pieceIndexOfRequestIndex(x)) {
813                                         preservedCount++
814                                 }
815                                 return true
816                         })
817                         if preservedCount != 0 {
818                                 // TODO: Yes this is a debug log but I'm not happy with the state of the logging lib
819                                 // right now.
820                                 c.protocolLogger.Levelf(log.Debug,
821                                         "%v requests were preserved while being choked (fast=%v)",
822                                         preservedCount,
823                                         c.fastEnabled())
824
825                                 torrent.Add("requestsPreservedThroughChoking", int64(preservedCount))
826                         }
827                         if !c.t._pendingPieces.IsEmpty() {
828                                 c.updateRequests("unchoked")
829                         }
830                         c.updateExpectingChunks()
831                 case pp.Interested:
832                         c.peerInterested = true
833                         c.tickleWriter()
834                 case pp.NotInterested:
835                         c.peerInterested = false
836                         // We don't clear their requests since it isn't clear in the spec.
837                         // We'll probably choke them for this, which will clear them if
838                         // appropriate, and is clearly specified.
839                 case pp.Have:
840                         err = c.peerSentHave(pieceIndex(msg.Index))
841                 case pp.Bitfield:
842                         err = c.peerSentBitfield(msg.Bitfield)
843                 case pp.Request:
844                         r := newRequestFromMessage(&msg)
845                         err = c.onReadRequest(r, true)
846                         if err != nil {
847                                 err = fmt.Errorf("on reading request %v: %w", r, err)
848                         }
849                 case pp.Piece:
850                         c.doChunkReadStats(int64(len(msg.Piece)))
851                         err = c.receiveChunk(&msg)
852                         if len(msg.Piece) == int(t.chunkSize) {
853                                 t.chunkPool.Put(&msg.Piece)
854                         }
855                         if err != nil {
856                                 err = fmt.Errorf("receiving chunk: %w", err)
857                         }
858                 case pp.Cancel:
859                         req := newRequestFromMessage(&msg)
860                         c.onPeerSentCancel(req)
861                 case pp.Port:
862                         ipa, ok := tryIpPortFromNetAddr(c.RemoteAddr)
863                         if !ok {
864                                 break
865                         }
866                         pingAddr := net.UDPAddr{
867                                 IP:   ipa.IP,
868                                 Port: ipa.Port,
869                         }
870                         if msg.Port != 0 {
871                                 pingAddr.Port = int(msg.Port)
872                         }
873                         cl.eachDhtServer(func(s DhtServer) {
874                                 go s.Ping(&pingAddr)
875                         })
876                 case pp.Suggest:
877                         torrent.Add("suggests received", 1)
878                         log.Fmsg("peer suggested piece %d", msg.Index).AddValues(c, msg.Index).LogLevel(log.Debug, c.t.logger)
879                         c.updateRequests("suggested")
880                 case pp.HaveAll:
881                         err = c.onPeerSentHaveAll()
882                 case pp.HaveNone:
883                         err = c.peerSentHaveNone()
884                 case pp.Reject:
885                         req := newRequestFromMessage(&msg)
886                         if !c.remoteRejectedRequest(c.t.requestIndexFromRequest(req)) {
887                                 err = fmt.Errorf("received invalid reject for request %v", req)
888                                 c.protocolLogger.Levelf(log.Debug, "%v", err)
889                         }
890                 case pp.AllowedFast:
891                         torrent.Add("allowed fasts received", 1)
892                         log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).LogLevel(log.Debug, c.t.logger)
893                         c.updateRequests("PeerConn.mainReadLoop allowed fast")
894                 case pp.Extended:
895                         err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload)
896                 case pp.Hashes:
897                         err = c.onReadHashes(&msg)
898                 case pp.HashRequest, pp.HashReject:
899                         c.protocolLogger.Levelf(log.Info, "received unimplemented BitTorrent v2 message: %v", msg.Type)
900                 default:
901                         err = fmt.Errorf("received unknown message type: %#v", msg.Type)
902                 }
903                 if err != nil {
904                         return err
905                 }
906         }
907 }
908
909 func (c *PeerConn) onReadExtendedMsg(id pp.ExtensionNumber, payload []byte) (err error) {
910         defer func() {
911                 // TODO: Should we still do this?
912                 if err != nil {
913                         // These clients use their own extension IDs for outgoing message
914                         // types, which is incorrect.
915                         if bytes.HasPrefix(c.PeerID[:], []byte("-SD0100-")) || strings.HasPrefix(string(c.PeerID[:]), "-XL0012-") {
916                                 err = nil
917                         }
918                 }
919         }()
920         t := c.t
921         cl := t.cl
922         {
923                 event := PeerConnReadExtensionMessageEvent{
924                         PeerConn:        c,
925                         ExtensionNumber: id,
926                         Payload:         payload,
927                 }
928                 for _, cb := range c.callbacks.PeerConnReadExtensionMessage {
929                         cb(event)
930                 }
931         }
932         if id == pp.HandshakeExtendedID {
933                 var d pp.ExtendedHandshakeMessage
934                 if err := bencode.Unmarshal(payload, &d); err != nil {
935                         c.protocolLogger.Printf("error parsing extended handshake message %q: %s", payload, err)
936                         return fmt.Errorf("unmarshalling extended handshake payload: %w", err)
937                 }
938                 // Trigger this callback after it's been processed. If you want to handle it yourself, you
939                 // should hook PeerConnReadExtensionMessage.
940                 if cb := c.callbacks.ReadExtendedHandshake; cb != nil {
941                         cb(c, &d)
942                 }
943                 if d.Reqq != 0 {
944                         c.PeerMaxRequests = d.Reqq
945                 }
946                 c.PeerClientName.Store(d.V)
947                 if c.PeerExtensionIDs == nil {
948                         c.PeerExtensionIDs = make(map[pp.ExtensionName]pp.ExtensionNumber, len(d.M))
949                 }
950                 c.PeerListenPort = d.Port
951                 c.PeerPrefersEncryption = d.Encryption
952                 for name, id := range d.M {
953                         if _, ok := c.PeerExtensionIDs[name]; !ok {
954                                 peersSupportingExtension.Add(
955                                         // expvar.Var.String must produce valid JSON. "ut_payme\xeet_address" was being
956                                         // entered here which caused problems later when unmarshalling.
957                                         strconv.Quote(string(name)),
958                                         1)
959                         }
960                         c.PeerExtensionIDs[name] = id
961                 }
962                 if d.MetadataSize != 0 {
963                         if err = t.setMetadataSize(d.MetadataSize); err != nil {
964                                 return fmt.Errorf("setting metadata size to %d: %w", d.MetadataSize, err)
965                         }
966                 }
967                 c.requestPendingMetadata()
968                 if !t.cl.config.DisablePEX {
969                         t.pex.Add(c) // we learnt enough now
970                         // This checks the extension is supported internally.
971                         c.pex.Init(c)
972                 }
973                 return nil
974         }
975         extensionName, builtin, err := c.LocalLtepProtocolMap.LookupId(id)
976         if err != nil {
977                 return
978         }
979         if !builtin {
980                 // User should have taken care of this in PeerConnReadExtensionMessage callback.
981                 return nil
982         }
983         switch extensionName {
984         case pp.ExtensionNameMetadata:
985                 err := cl.gotMetadataExtensionMsg(payload, t, c)
986                 if err != nil {
987                         return fmt.Errorf("handling metadata extension message: %w", err)
988                 }
989                 return nil
990         case pp.ExtensionNamePex:
991                 if !c.pex.IsEnabled() {
992                         return nil // or hang-up maybe?
993                 }
994                 err = c.pex.Recv(payload)
995                 if err != nil {
996                         err = fmt.Errorf("receiving pex message: %w", err)
997                 }
998                 return
999         case utHolepunch.ExtensionName:
1000                 var msg utHolepunch.Msg
1001                 err = msg.UnmarshalBinary(payload)
1002                 if err != nil {
1003                         err = fmt.Errorf("unmarshalling ut_holepunch message: %w", err)
1004                         return
1005                 }
1006                 err = c.t.handleReceivedUtHolepunchMsg(msg, c)
1007                 return
1008         default:
1009                 panic(fmt.Sprintf("unhandled builtin extension protocol %q", extensionName))
1010         }
1011 }
1012
1013 // Set both the Reader and Writer for the connection from a single ReadWriter.
1014 func (cn *PeerConn) setRW(rw io.ReadWriter) {
1015         cn.r = rw
1016         cn.w = rw
1017 }
1018
1019 // Returns the Reader and Writer as a combined ReadWriter.
1020 func (cn *PeerConn) rw() io.ReadWriter {
1021         return struct {
1022                 io.Reader
1023                 io.Writer
1024         }{cn.r, cn.w}
1025 }
1026
1027 func (c *PeerConn) uploadAllowed() bool {
1028         if c.t.cl.config.NoUpload {
1029                 return false
1030         }
1031         if c.t.dataUploadDisallowed {
1032                 return false
1033         }
1034         if c.t.seeding() {
1035                 return true
1036         }
1037         if !c.peerHasWantedPieces() {
1038                 return false
1039         }
1040         // Don't upload more than 100 KiB more than we download.
1041         if c._stats.BytesWrittenData.Int64() >= c._stats.BytesReadData.Int64()+100<<10 {
1042                 return false
1043         }
1044         return true
1045 }
1046
1047 func (c *PeerConn) setRetryUploadTimer(delay time.Duration) {
1048         if c.uploadTimer == nil {
1049                 c.uploadTimer = time.AfterFunc(delay, c.tickleWriter)
1050         } else {
1051                 c.uploadTimer.Reset(delay)
1052         }
1053 }
1054
1055 // Also handles choking and unchoking of the remote peer.
1056 func (c *PeerConn) upload(msg func(pp.Message) bool) bool {
1057         // Breaking or completing this loop means we don't want to upload to the peer anymore, and we
1058         // choke them.
1059 another:
1060         for c.uploadAllowed() {
1061                 // We want to upload to the peer.
1062                 if !c.unchoke(msg) {
1063                         return false
1064                 }
1065                 for r, state := range c.peerRequests {
1066                         if state.data == nil {
1067                                 continue
1068                         }
1069                         res := c.t.cl.config.UploadRateLimiter.ReserveN(time.Now(), int(r.Length))
1070                         if !res.OK() {
1071                                 panic(fmt.Sprintf("upload rate limiter burst size < %d", r.Length))
1072                         }
1073                         delay := res.Delay()
1074                         if delay > 0 {
1075                                 res.Cancel()
1076                                 c.setRetryUploadTimer(delay)
1077                                 // Hard to say what to return here.
1078                                 return true
1079                         }
1080                         more := c.sendChunk(r, msg, state)
1081                         delete(c.peerRequests, r)
1082                         if !more {
1083                                 return false
1084                         }
1085                         goto another
1086                 }
1087                 return true
1088         }
1089         return c.choke(msg)
1090 }
1091
1092 func (cn *PeerConn) drop() {
1093         cn.t.dropConnection(cn)
1094 }
1095
1096 func (cn *PeerConn) ban() {
1097         cn.t.cl.banPeerIP(cn.remoteIp())
1098 }
1099
1100 // This is called when something has changed that should wake the writer, such as putting stuff into
1101 // the writeBuffer, or changing some state that the writer can act on.
1102 func (c *PeerConn) tickleWriter() {
1103         c.messageWriter.writeCond.Broadcast()
1104 }
1105
1106 func (c *PeerConn) sendChunk(r Request, msg func(pp.Message) bool, state *peerRequestState) (more bool) {
1107         c.lastChunkSent = time.Now()
1108         state.allocReservation.Release()
1109         return msg(pp.Message{
1110                 Type:  pp.Piece,
1111                 Index: r.Index,
1112                 Begin: r.Begin,
1113                 Piece: state.data,
1114         })
1115 }
1116
1117 func (c *Peer) setTorrent(t *Torrent) {
1118         if c.t != nil {
1119                 panic("connection already associated with a torrent")
1120         }
1121         c.t = t
1122         c.logger.WithDefaultLevel(log.Debug).Printf("set torrent=%v", t)
1123         t.reconcileHandshakeStats(c)
1124 }
1125
1126 func (c *PeerConn) pexPeerFlags() pp.PexPeerFlags {
1127         f := pp.PexPeerFlags(0)
1128         if c.PeerPrefersEncryption {
1129                 f |= pp.PexPrefersEncryption
1130         }
1131         if c.outgoing {
1132                 f |= pp.PexOutgoingConn
1133         }
1134         if c.utp() {
1135                 f |= pp.PexSupportsUtp
1136         }
1137         return f
1138 }
1139
1140 // This returns the address to use if we want to dial the peer again. It incorporates the peer's
1141 // advertised listen port.
1142 func (c *PeerConn) dialAddr() PeerRemoteAddr {
1143         if c.outgoing || c.PeerListenPort == 0 {
1144                 return c.RemoteAddr
1145         }
1146         addrPort, err := addrPortFromPeerRemoteAddr(c.RemoteAddr)
1147         if err != nil {
1148                 c.logger.Levelf(
1149                         log.Warning,
1150                         "error parsing %q for alternate dial port: %v",
1151                         c.RemoteAddr,
1152                         err,
1153                 )
1154                 return c.RemoteAddr
1155         }
1156         return netip.AddrPortFrom(addrPort.Addr(), uint16(c.PeerListenPort))
1157 }
1158
1159 func (c *PeerConn) pexEvent(t pexEventType) (_ pexEvent, err error) {
1160         f := c.pexPeerFlags()
1161         dialAddr := c.dialAddr()
1162         addr, err := addrPortFromPeerRemoteAddr(dialAddr)
1163         if err != nil || !addr.IsValid() {
1164                 err = fmt.Errorf("parsing dial addr %q: %w", dialAddr, err)
1165                 return
1166         }
1167         return pexEvent{t, addr, f, nil}, nil
1168 }
1169
1170 func (pc *PeerConn) String() string {
1171         return fmt.Sprintf("%T %p [id=%+q, exts=%v, v=%q]", pc, pc, pc.PeerID, pc.PeerExtensionBytes, pc.PeerClientName.Load())
1172 }
1173
1174 // Returns the pieces the peer could have based on their claims. If we don't know how many pieces
1175 // are in the torrent, it could be a very large range if the peer has sent HaveAll.
1176 func (pc *PeerConn) PeerPieces() *roaring.Bitmap {
1177         pc.locker().RLock()
1178         defer pc.locker().RUnlock()
1179         return pc.newPeerPieces()
1180 }
1181
1182 func (pc *PeerConn) remoteIsTransmission() bool {
1183         return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-'
1184 }
1185
1186 func (pc *PeerConn) remoteDialAddrPort() (netip.AddrPort, error) {
1187         dialAddr := pc.dialAddr()
1188         return addrPortFromPeerRemoteAddr(dialAddr)
1189 }
1190
1191 func (pc *PeerConn) bitExtensionEnabled(bit pp.ExtensionBit) bool {
1192         return pc.t.cl.config.Extensions.GetBit(bit) && pc.PeerExtensionBytes.GetBit(bit)
1193 }
1194
1195 func (cn *PeerConn) peerPiecesChanged() {
1196         cn.t.maybeDropMutuallyCompletePeer(cn)
1197 }
1198
1199 // Returns whether the connection could be useful to us. We're seeding and
1200 // they want data, we don't have metainfo and they can provide it, etc.
1201 func (c *PeerConn) useful() bool {
1202         t := c.t
1203         if c.closed.IsSet() {
1204                 return false
1205         }
1206         if !t.haveInfo() {
1207                 return c.supportsExtension("ut_metadata")
1208         }
1209         if t.seeding() && c.peerInterested {
1210                 return true
1211         }
1212         if c.peerHasWantedPieces() {
1213                 return true
1214         }
1215         return false
1216 }
1217
1218 func makeBuiltinLtepProtocols(pex bool) LocalLtepProtocolMap {
1219         ps := []pp.ExtensionName{pp.ExtensionNameMetadata, utHolepunch.ExtensionName}
1220         if pex {
1221                 ps = append(ps, pp.ExtensionNamePex)
1222         }
1223         return LocalLtepProtocolMap{
1224                 Index:      ps,
1225                 NumBuiltin: len(ps),
1226         }
1227 }
1228
1229 func (c *PeerConn) addBuiltinLtepProtocols(pex bool) {
1230         c.LocalLtepProtocolMap = &c.t.cl.defaultLocalLtepProtocolMap
1231 }
1232
1233 func (pc *PeerConn) WriteExtendedMessage(extName pp.ExtensionName, payload []byte) error {
1234         pc.locker().Lock()
1235         defer pc.locker().Unlock()
1236         id := pc.PeerExtensionIDs[extName]
1237         if id == 0 {
1238                 return fmt.Errorf("peer does not support or has disabled extension %q", extName)
1239         }
1240         pc.write(pp.Message{
1241                 Type:            pp.Extended,
1242                 ExtendedID:      id,
1243                 ExtendedPayload: payload,
1244         })
1245         return nil
1246 }
1247
1248 func (pc *PeerConn) shouldRequestHashes() bool {
1249         return pc.t.haveInfo() && pc.v2 && pc.t.info.HasV2()
1250 }
1251
1252 func (pc *PeerConn) requestMissingHashes() {
1253         if !pc.shouldRequestHashes() {
1254                 return
1255         }
1256         info := pc.t.info
1257         baseLayer := pp.Integer(merkle.Log2RoundingUp(merkle.RoundUpToPowerOfTwo(
1258                 uint((pc.t.usualPieceSize() + merkle.BlockSize - 1) / merkle.BlockSize)),
1259         ))
1260         nextFileBeginPiece := 0
1261 file:
1262         for _, file := range info.UpvertedFiles() {
1263                 fileNumPieces := int((file.Length + info.PieceLength - 1) / info.PieceLength)
1264                 curFileBeginPiece := nextFileBeginPiece
1265                 nextFileBeginPiece += fileNumPieces
1266                 haveAllHashes := true
1267                 for i := range fileNumPieces {
1268                         torrentPieceIndex := curFileBeginPiece + i
1269                         if !pc.peerHasPiece(torrentPieceIndex) {
1270                                 continue file
1271                         }
1272                         if !pc.t.piece(torrentPieceIndex).hashV2.Ok {
1273                                 haveAllHashes = false
1274                         }
1275                 }
1276                 if haveAllHashes {
1277                         continue
1278                 }
1279                 // We would be requesting the leaves, the file must be short enough that we can just do with
1280                 // the pieces root as the piece hash.
1281                 if fileNumPieces <= 1 {
1282                         continue
1283                 }
1284                 piecesRoot := file.PiecesRoot.Unwrap()
1285                 proofLayers := pp.Integer(0)
1286                 for index := 0; index < fileNumPieces; index += 512 {
1287                         // Minimizing to the number of pieces in a file conflicts with the BEP.
1288                         length := merkle.RoundUpToPowerOfTwo(uint(min(512, fileNumPieces-index)))
1289                         if length < 2 {
1290                                 // This should have been filtered out by baseLayer and pieces root as piece hash
1291                                 // checks.
1292                                 panic(length)
1293                         }
1294                         if length%2 != 0 {
1295                                 pc.protocolLogger.Levelf(log.Warning, "requesting odd hashes length %d", length)
1296                         }
1297                         msg := pp.Message{
1298                                 Type:        pp.HashRequest,
1299                                 PiecesRoot:  piecesRoot,
1300                                 BaseLayer:   baseLayer,
1301                                 Index:       pp.Integer(index),
1302                                 Length:      pp.Integer(length),
1303                                 ProofLayers: proofLayers,
1304                         }
1305                         hr := hashRequestFromMessage(msg)
1306                         if generics.MapContains(pc.sentHashRequests, hr) {
1307                                 continue
1308                         }
1309                         pc.write(msg)
1310                         generics.MakeMapIfNil(&pc.sentHashRequests)
1311                         pc.sentHashRequests[hr] = struct{}{}
1312                 }
1313         }
1314 }
1315
1316 func (pc *PeerConn) onReadHashes(msg *pp.Message) (err error) {
1317         file := pc.t.getFileByPiecesRoot(msg.PiecesRoot)
1318         filePieceHashes := pc.receivedHashPieces[msg.PiecesRoot]
1319         if filePieceHashes == nil {
1320                 filePieceHashes = make([][32]byte, file.numPieces())
1321                 generics.MakeMapIfNil(&pc.receivedHashPieces)
1322                 pc.receivedHashPieces[msg.PiecesRoot] = filePieceHashes
1323         }
1324         if msg.ProofLayers != 0 {
1325                 // This isn't handled yet.
1326                 panic(msg.ProofLayers)
1327         }
1328         copy(filePieceHashes[msg.Index:], msg.Hashes)
1329         root := merkle.RootWithPadHash(
1330                 filePieceHashes,
1331                 metainfo.HashForPiecePad(int64(pc.t.usualPieceSize())))
1332         expectedPiecesRoot := file.piecesRoot.Unwrap()
1333         if root == expectedPiecesRoot {
1334                 pc.protocolLogger.WithNames(v2HashesLogName).Levelf(
1335                         log.Info,
1336                         "got piece hashes for file %v (num pieces %v)",
1337                         file, file.numPieces())
1338                 for filePieceIndex, peerHash := range filePieceHashes {
1339                         torrentPieceIndex := file.BeginPieceIndex() + filePieceIndex
1340                         pc.t.piece(torrentPieceIndex).setV2Hash(peerHash)
1341                 }
1342         } else {
1343                 pc.protocolLogger.WithNames(v2HashesLogName).Levelf(
1344                         log.Debug,
1345                         "peer file piece hashes root mismatch: %x != %x",
1346                         root, expectedPiecesRoot)
1347         }
1348         return nil
1349 }
1350
1351 type hashRequest struct {
1352         piecesRoot                            [32]byte
1353         baseLayer, index, length, proofLayers pp.Integer
1354 }
1355
1356 func (hr hashRequest) toMessage() pp.Message {
1357         return pp.Message{
1358                 Type:        pp.HashRequest,
1359                 PiecesRoot:  hr.piecesRoot,
1360                 BaseLayer:   hr.baseLayer,
1361                 Index:       hr.index,
1362                 Length:      hr.length,
1363                 ProofLayers: hr.proofLayers,
1364         }
1365 }
1366
1367 func hashRequestFromMessage(m pp.Message) hashRequest {
1368         return hashRequest{
1369                 piecesRoot:  m.PiecesRoot,
1370                 baseLayer:   m.BaseLayer,
1371                 index:       m.Index,
1372                 length:      m.Length,
1373                 proofLayers: m.ProofLayers,
1374         }
1375 }