2014-04-03 20:16:59 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-11-21 14:04:07 +08:00
|
|
|
"bufio"
|
2015-06-29 17:37:52 +08:00
|
|
|
"bytes"
|
2023-02-14 09:46:23 +08:00
|
|
|
"context"
|
2021-05-11 15:14:02 +08:00
|
|
|
"errors"
|
2014-06-26 15:29:12 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-05-16 16:46:38 +08:00
|
|
|
"math/rand"
|
2014-04-03 20:16:59 +08:00
|
|
|
"net"
|
2016-02-09 21:45:47 +08:00
|
|
|
"strconv"
|
2016-09-11 12:32:56 +08:00
|
|
|
"strings"
|
2014-04-03 20:16:59 +08:00
|
|
|
"time"
|
|
|
|
|
2021-10-05 14:48:34 +08:00
|
|
|
"github.com/RoaringBitmap/roaring"
|
2022-01-21 19:28:15 +08:00
|
|
|
. "github.com/anacrolix/generics"
|
2018-02-03 09:06:52 +08:00
|
|
|
"github.com/anacrolix/log"
|
2020-01-10 12:09:21 +08:00
|
|
|
"github.com/anacrolix/missinggo/v2/bitmap"
|
2019-12-23 11:04:07 +08:00
|
|
|
"github.com/anacrolix/multiless"
|
2022-11-15 20:22:10 +08:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2023-03-01 09:33:25 +08:00
|
|
|
"github.com/anacrolix/torrent/internal/alloclim"
|
2021-05-20 16:51:08 +08:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2017-12-01 15:12:29 +08:00
|
|
|
"github.com/anacrolix/torrent/mse"
|
2015-03-20 13:37:44 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2014-07-16 15:06:18 +08:00
|
|
|
)
|
|
|
|
|
2020-09-29 14:21:54 +08:00
|
|
|
// Maintains the state of a BitTorrent-protocol based connection with a peer.
|
2020-05-30 08:14:20 +08:00
|
|
|
type PeerConn struct {
|
2021-01-20 10:10:32 +08:00
|
|
|
Peer
|
2020-05-30 08:14:20 +08:00
|
|
|
|
2020-09-29 14:21:54 +08:00
|
|
|
// A string that should identify the PeerConn's net.Conn endpoints. The net.Conn could
|
|
|
|
// be wrapping WebRTC, uTP, or TCP etc. Used in writing the conn status for peers.
|
|
|
|
connString string
|
|
|
|
|
|
|
|
// See BEP 3 etc.
|
|
|
|
PeerID PeerID
|
|
|
|
PeerExtensionBytes pp.PeerExtensionBits
|
|
|
|
|
2021-11-17 11:00:50 +08:00
|
|
|
// The actual Conn, used for closing, and setting socket options. Do not use methods on this
|
|
|
|
// while holding any mutexes.
|
2020-05-30 08:14:20 +08:00
|
|
|
conn net.Conn
|
|
|
|
// The Reader and Writer for this Conn, with hooks installed for stats,
|
|
|
|
// limiting, deadlines etc.
|
|
|
|
w io.Writer
|
|
|
|
r io.Reader
|
|
|
|
|
2021-05-20 16:55:23 +08:00
|
|
|
messageWriter peerConnMsgWriter
|
2020-10-13 10:33:22 +08:00
|
|
|
|
2021-05-20 16:51:08 +08:00
|
|
|
uploadTimer *time.Timer
|
|
|
|
pex pexConnState
|
2021-11-12 09:37:40 +08:00
|
|
|
|
|
|
|
// The pieces the peer has claimed to have.
|
|
|
|
_peerPieces roaring.Bitmap
|
|
|
|
// The peer has everything. This can occur due to a special message, when
|
|
|
|
// we may not even know the number of pieces in the torrent yet.
|
|
|
|
peerSentHaveAll bool
|
2023-02-14 09:46:23 +08:00
|
|
|
|
|
|
|
peerRequestDataAllocLimiter alloclim.Limiter
|
2016-05-07 16:56:44 +08:00
|
|
|
}
|
|
|
|
|
2022-12-31 08:27:47 +08:00
|
|
|
func (cn *PeerConn) peerImplStatusLines() []string {
|
|
|
|
return []string{fmt.Sprintf("%+-55q %s %s", cn.PeerID, cn.PeerExtensionBytes, cn.connString)}
|
2020-09-29 14:21:54 +08:00
|
|
|
}
|
|
|
|
|
2018-06-16 15:14:47 +08:00
|
|
|
// Returns true if the connection is over IPv6.
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) ipv6() bool {
|
2021-01-25 11:22:24 +08:00
|
|
|
ip := cn.remoteIp()
|
2018-06-16 15:14:47 +08:00
|
|
|
if ip.To4() != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return len(ip) == net.IPv6len
|
|
|
|
}
|
|
|
|
|
2020-02-20 13:47:37 +08:00
|
|
|
// Returns true the if the dialer/initiator has the lower client peer ID. TODO: Find the
|
2018-06-16 15:14:47 +08:00
|
|
|
// specification for this.
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) isPreferredDirection() bool {
|
2018-06-16 15:14:47 +08:00
|
|
|
return bytes.Compare(cn.t.cl.peerID[:], cn.PeerID[:]) < 0 == cn.outgoing
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns whether the left connection should be preferred over the right one,
|
|
|
|
// considering only their networking properties. If ok is false, we can't
|
|
|
|
// decide.
|
2022-01-12 12:01:33 +08:00
|
|
|
func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) bool {
|
2022-01-12 12:07:26 +08:00
|
|
|
var ml multiless.Computation
|
|
|
|
ml = ml.Bool(r.isPreferredDirection(), l.isPreferredDirection())
|
|
|
|
ml = ml.Bool(l.utp(), r.utp())
|
|
|
|
ml = ml.Bool(r.ipv6(), l.ipv6())
|
2022-01-12 12:01:33 +08:00
|
|
|
return ml.Less()
|
2018-06-16 15:14:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-18 05:58:56 +08:00
|
|
|
func (cn *PeerConn) peerHasAllPieces() (all, known bool) {
|
2018-01-29 15:18:08 +08:00
|
|
|
if cn.peerSentHaveAll {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
if !cn.t.haveInfo() {
|
|
|
|
return false, false
|
|
|
|
}
|
2021-11-17 11:01:11 +08:00
|
|
|
return cn._peerPieces.GetCardinality() == uint64(cn.t.numPieces()), true
|
2018-01-29 15:18:08 +08:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:18:25 +08:00
|
|
|
func (cn *PeerConn) onGotInfo(info *metainfo.Info) {
|
|
|
|
cn.setNumPieces(info.NumPieces())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correct the PeerPieces slice length. Return false if the existing slice is invalid, such as by
|
|
|
|
// receiving badly sized BITFIELD, or invalid HAVE messages.
|
|
|
|
func (cn *PeerConn) setNumPieces(num pieceIndex) {
|
2021-05-20 09:16:54 +08:00
|
|
|
cn._peerPieces.RemoveRange(bitmap.BitRange(num), bitmap.ToEnd)
|
2021-10-11 12:45:55 +08:00
|
|
|
cn.peerPiecesChanged()
|
2014-07-16 15:09:30 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 09:37:40 +08:00
|
|
|
func (cn *PeerConn) peerPieces() *roaring.Bitmap {
|
|
|
|
return &cn._peerPieces
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) connectionFlags() (ret string) {
|
2014-06-26 15:29:12 +08:00
|
|
|
c := func(b byte) {
|
2015-03-18 15:38:29 +08:00
|
|
|
ret += string([]byte{b})
|
2014-12-09 14:26:06 +08:00
|
|
|
}
|
2017-09-13 16:20:20 +08:00
|
|
|
if cn.cryptoMethod == mse.CryptoMethodRC4 {
|
2015-03-18 15:38:29 +08:00
|
|
|
c('E')
|
2017-09-13 16:20:20 +08:00
|
|
|
} else if cn.headerEncrypted {
|
|
|
|
c('e')
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
2016-11-26 21:05:19 +08:00
|
|
|
ret += string(cn.Discovery)
|
2018-04-12 09:41:07 +08:00
|
|
|
if cn.utp() {
|
2018-02-15 13:04:42 +08:00
|
|
|
c('U')
|
2014-11-17 03:29:31 +08:00
|
|
|
}
|
2015-08-02 02:06:22 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) utp() bool {
|
2021-01-25 12:43:28 +08:00
|
|
|
return parseNetworkString(cn.Network).Udp
|
2018-04-12 09:41:07 +08:00
|
|
|
}
|
|
|
|
|
2021-01-04 12:51:23 +08:00
|
|
|
func (cn *PeerConn) onClose() {
|
2020-04-09 00:03:29 +08:00
|
|
|
if cn.pex.IsEnabled() {
|
|
|
|
cn.pex.Close()
|
|
|
|
}
|
2018-02-03 12:44:03 +08:00
|
|
|
cn.tickleWriter()
|
2016-05-11 21:50:21 +08:00
|
|
|
if cn.conn != nil {
|
2021-11-17 11:00:50 +08:00
|
|
|
go cn.conn.Close()
|
2016-05-11 21:50:21 +08:00
|
|
|
}
|
2020-10-14 12:11:45 +08:00
|
|
|
if cb := cn.callbacks.PeerConnClosed; cb != nil {
|
2020-10-13 10:02:39 +08:00
|
|
|
cb(cn)
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2021-05-10 10:29:47 +08:00
|
|
|
// Writes a message into the write buffer. Returns whether it's okay to keep writing. Writing is
|
|
|
|
// done asynchronously, so it may be that we're not able to honour backpressure from this method.
|
|
|
|
func (cn *PeerConn) write(msg pp.Message) bool {
|
|
|
|
torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
|
2021-05-20 16:51:08 +08:00
|
|
|
// We don't need to track bytes here because the connection's Writer has that behaviour injected
|
|
|
|
// (although there's some delay between us buffering the message, and the connection writer
|
2020-11-13 12:50:08 +08:00
|
|
|
// flushing it out.).
|
2021-05-20 16:51:08 +08:00
|
|
|
notFull := cn.messageWriter.write(msg)
|
2021-05-10 10:29:47 +08:00
|
|
|
// Last I checked only Piece messages affect stats, and we don't write those.
|
2020-05-30 15:52:27 +08:00
|
|
|
cn.wroteMsg(&msg)
|
2021-05-09 12:14:11 +08:00
|
|
|
cn.tickleWriter()
|
2021-05-20 16:51:08 +08:00
|
|
|
return notFull
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) requestMetadataPiece(index int) {
|
2018-07-10 10:20:36 +08:00
|
|
|
eID := cn.PeerExtensionIDs[pp.ExtensionNameMetadata]
|
2021-06-18 13:04:07 +08:00
|
|
|
if eID == pp.ExtensionDeleteNumber {
|
2015-03-27 12:36:59 +08:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if index < len(cn.metadataRequests) && cn.metadataRequests[index] {
|
2015-03-27 12:36:59 +08:00
|
|
|
return
|
|
|
|
}
|
2020-10-12 14:12:34 +08:00
|
|
|
cn.logger.WithDefaultLevel(log.Debug).Printf("requesting metadata piece %d", index)
|
2021-06-18 13:04:07 +08:00
|
|
|
cn.write(pp.MetadataExtensionRequestMsg(eID, index))
|
2016-04-19 12:11:11 +08:00
|
|
|
for index >= len(cn.metadataRequests) {
|
|
|
|
cn.metadataRequests = append(cn.metadataRequests, false)
|
2015-03-27 12:36:59 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.metadataRequests[index] = true
|
2015-03-27 12:36:59 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) requestedMetadataPiece(index int) bool {
|
2016-04-19 12:11:11 +08:00
|
|
|
return index < len(cn.metadataRequests) && cn.metadataRequests[index]
|
2015-03-27 12:36:59 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 11:23:22 +08:00
|
|
|
func (cn *PeerConn) onPeerSentCancel(r Request) {
|
2020-02-21 08:07:50 +08:00
|
|
|
if _, ok := cn.peerRequests[r]; !ok {
|
2018-02-04 21:18:38 +08:00
|
|
|
torrent.Add("unexpected cancels received", 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cn.fastEnabled() {
|
|
|
|
cn.reject(r)
|
|
|
|
} else {
|
2020-02-21 08:07:50 +08:00
|
|
|
delete(cn.peerRequests, r)
|
2014-04-16 15:33:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) choke(msg messageWriter) (more bool) {
|
|
|
|
if cn.choking {
|
2017-09-18 10:09:08 +08:00
|
|
|
return true
|
2014-05-21 15:49:28 +08:00
|
|
|
}
|
2020-02-21 08:07:50 +08:00
|
|
|
cn.choking = true
|
2018-02-04 21:18:38 +08:00
|
|
|
more = msg(pp.Message{
|
2017-09-18 10:09:08 +08:00
|
|
|
Type: pp.Choke,
|
|
|
|
})
|
2021-12-27 19:19:04 +08:00
|
|
|
if !cn.fastEnabled() {
|
2023-02-14 09:46:23 +08:00
|
|
|
cn.deleteAllPeerRequests()
|
2018-02-04 21:18:38 +08:00
|
|
|
}
|
|
|
|
return
|
2014-05-21 15:49:28 +08:00
|
|
|
}
|
|
|
|
|
2023-02-14 09:46:23 +08:00
|
|
|
func (cn *PeerConn) deleteAllPeerRequests() {
|
|
|
|
for _, state := range cn.peerRequests {
|
|
|
|
state.allocReservation.Drop()
|
|
|
|
}
|
|
|
|
cn.peerRequests = nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) unchoke(msg func(pp.Message) bool) bool {
|
|
|
|
if !cn.choking {
|
2017-09-18 10:09:08 +08:00
|
|
|
return true
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
2020-02-21 08:07:50 +08:00
|
|
|
cn.choking = false
|
2017-09-18 10:09:08 +08:00
|
|
|
return msg(pp.Message{
|
2014-08-21 16:12:49 +08:00
|
|
|
Type: pp.Unchoke,
|
2014-04-03 20:16:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-02 15:41:59 +08:00
|
|
|
func (pc *PeerConn) writeInterested(interested bool) bool {
|
2020-05-30 13:18:28 +08:00
|
|
|
return pc.write(pp.Message{
|
2014-08-21 16:12:49 +08:00
|
|
|
Type: func() pp.MessageType {
|
2014-04-03 20:16:59 +08:00
|
|
|
if interested {
|
2014-08-21 16:12:49 +08:00
|
|
|
return pp.Interested
|
2014-04-03 20:16:59 +08:00
|
|
|
} else {
|
2014-08-21 16:12:49 +08:00
|
|
|
return pp.NotInterested
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
}(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-20 18:23:45 +08:00
|
|
|
func (me *PeerConn) _request(r Request) bool {
|
|
|
|
return me.write(pp.Message{
|
2017-11-06 11:53:00 +08:00
|
|
|
Type: pp.Request,
|
|
|
|
Index: r.Index,
|
|
|
|
Begin: r.Begin,
|
|
|
|
Length: r.Length,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:04:06 +08:00
|
|
|
func (me *PeerConn) _cancel(r RequestIndex) bool {
|
|
|
|
me.write(makeCancelMessage(me.t.requestIndexToRequest(r)))
|
2021-12-07 11:16:02 +08:00
|
|
|
// Transmission does not send rejects for received cancels. See
|
|
|
|
// https://github.com/transmission/transmission/pull/2275.
|
2021-12-11 21:04:06 +08:00
|
|
|
return me.fastEnabled() && !me.remoteIsTransmission()
|
2020-05-30 13:18:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *PeerConn) fillWriteBuffer() {
|
2021-12-24 05:55:57 +08:00
|
|
|
if cn.messageWriter.writeBuffer.Len() > writeBufferLowWaterLen {
|
|
|
|
// Fully committing to our max requests requires sufficient space (see
|
|
|
|
// maxLocalToRemoteRequests). Flush what we have instead. We also prefer always to make
|
|
|
|
// requests than to do PEX or upload, so we short-circuit before handling those. Any update
|
|
|
|
// request reason will not be cleared, so we'll come right back here when there's space. We
|
|
|
|
// can't do this in maybeUpdateActualRequestState because it's a method on Peer and has no
|
|
|
|
// knowledge of write buffers.
|
|
|
|
}
|
|
|
|
cn.maybeUpdateActualRequestState()
|
2020-04-09 00:03:29 +08:00
|
|
|
if cn.pex.IsEnabled() {
|
2020-05-30 13:18:28 +08:00
|
|
|
if flow := cn.pex.Share(cn.write); !flow {
|
2020-04-15 20:02:34 +08:00
|
|
|
return
|
|
|
|
}
|
2020-04-09 00:03:29 +08:00
|
|
|
}
|
2023-05-06 11:44:23 +08:00
|
|
|
// write负责将msg写入缓冲区
|
|
|
|
// upload负责send
|
2020-05-30 13:18:28 +08:00
|
|
|
cn.upload(cn.write)
|
2017-08-31 21:48:52 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) have(piece pieceIndex) {
|
2018-07-12 07:15:15 +08:00
|
|
|
if cn.sentHaves.Get(bitmap.BitIndex(piece)) {
|
2016-01-04 19:37:49 +08:00
|
|
|
return
|
|
|
|
}
|
2021-05-10 10:29:47 +08:00
|
|
|
cn.write(pp.Message{
|
2016-01-04 19:37:49 +08:00
|
|
|
Type: pp.Have,
|
|
|
|
Index: pp.Integer(piece),
|
|
|
|
})
|
2018-07-12 07:15:15 +08:00
|
|
|
cn.sentHaves.Add(bitmap.BitIndex(piece))
|
2016-01-04 19:37:49 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) postBitfield() {
|
2018-02-04 16:10:25 +08:00
|
|
|
if cn.sentHaves.Len() != 0 {
|
2016-01-04 19:37:49 +08:00
|
|
|
panic("bitfield must be first have-related message sent")
|
|
|
|
}
|
2018-02-04 16:10:25 +08:00
|
|
|
if !cn.t.haveAnyPieces() {
|
|
|
|
return
|
|
|
|
}
|
2021-05-10 10:29:47 +08:00
|
|
|
cn.write(pp.Message{
|
2016-01-04 19:37:49 +08:00
|
|
|
Type: pp.Bitfield,
|
2018-02-04 16:10:25 +08:00
|
|
|
Bitfield: cn.t.bitfield(),
|
2016-01-04 19:37:49 +08:00
|
|
|
})
|
2021-08-16 09:07:10 +08:00
|
|
|
cn.sentHaves = bitmap.Bitmap{cn.t._completedPieces.Clone()}
|
2016-01-04 19:37:49 +08:00
|
|
|
}
|
2016-01-24 12:21:17 +08:00
|
|
|
|
2021-10-21 07:28:57 +08:00
|
|
|
func (cn *PeerConn) handleUpdateRequests() {
|
|
|
|
// The writer determines the request state as needed when it can write.
|
2021-10-08 10:53:36 +08:00
|
|
|
cn.tickleWriter()
|
2017-08-31 21:48:52 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) raisePeerMinPieces(newMin pieceIndex) {
|
2016-04-19 12:11:11 +08:00
|
|
|
if newMin > cn.peerMinPieces {
|
|
|
|
cn.peerMinPieces = newMin
|
2016-03-22 09:07:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) peerSentHave(piece pieceIndex) error {
|
2018-01-09 14:26:01 +08:00
|
|
|
if cn.t.haveInfo() && piece >= cn.t.numPieces() || piece < 0 {
|
2016-03-22 09:07:03 +08:00
|
|
|
return errors.New("invalid piece")
|
|
|
|
}
|
2020-02-21 08:07:50 +08:00
|
|
|
if cn.peerHasPiece(piece) {
|
2016-03-22 09:07:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.raisePeerMinPieces(piece + 1)
|
2021-05-10 15:02:17 +08:00
|
|
|
if !cn.peerHasPiece(piece) {
|
|
|
|
cn.t.incPieceAvailability(piece)
|
|
|
|
}
|
2021-10-05 14:48:34 +08:00
|
|
|
cn._peerPieces.Add(uint32(piece))
|
2021-10-11 12:45:55 +08:00
|
|
|
if cn.t.wantPieceIndex(piece) {
|
|
|
|
cn.updateRequests("have")
|
|
|
|
}
|
|
|
|
cn.peerPiecesChanged()
|
2016-03-22 09:07:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) peerSentBitfield(bf []bool) error {
|
2016-03-22 09:07:03 +08:00
|
|
|
if len(bf)%8 != 0 {
|
|
|
|
panic("expected bitfield length divisible by 8")
|
|
|
|
}
|
2021-05-10 15:02:17 +08:00
|
|
|
// We know that the last byte means that at most the last 7 bits are wasted.
|
2018-07-12 07:15:15 +08:00
|
|
|
cn.raisePeerMinPieces(pieceIndex(len(bf) - 7))
|
|
|
|
if cn.t.haveInfo() && len(bf) > int(cn.t.numPieces()) {
|
2016-03-22 09:07:03 +08:00
|
|
|
// Ignore known excess pieces.
|
2016-04-19 12:11:11 +08:00
|
|
|
bf = bf[:cn.t.numPieces()]
|
2016-03-22 09:07:03 +08:00
|
|
|
}
|
2021-12-17 19:06:21 +08:00
|
|
|
bm := boolSliceToBitmap(bf)
|
|
|
|
if cn.t.haveInfo() && pieceIndex(bm.GetCardinality()) == cn.t.numPieces() {
|
|
|
|
cn.onPeerHasAllPieces()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !bm.IsEmpty() {
|
|
|
|
cn.raisePeerMinPieces(pieceIndex(bm.Maximum()) + 1)
|
|
|
|
}
|
|
|
|
shouldUpdateRequests := false
|
|
|
|
if cn.peerSentHaveAll {
|
|
|
|
if !cn.t.deleteConnWithAllPieces(&cn.Peer) {
|
|
|
|
panic(cn)
|
|
|
|
}
|
|
|
|
cn.peerSentHaveAll = false
|
|
|
|
if !cn._peerPieces.IsEmpty() {
|
|
|
|
panic("if peer has all, we expect no individual peer pieces to be set")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bm.Xor(&cn._peerPieces)
|
|
|
|
}
|
2021-05-10 15:02:17 +08:00
|
|
|
cn.peerSentHaveAll = false
|
2021-12-17 19:06:21 +08:00
|
|
|
// bm is now 'on' for pieces that are changing
|
|
|
|
bm.Iterate(func(x uint32) bool {
|
|
|
|
pi := pieceIndex(x)
|
|
|
|
if cn._peerPieces.Contains(x) {
|
|
|
|
// Then we must be losing this piece
|
|
|
|
cn.t.decPieceAvailability(pi)
|
2021-05-10 15:02:17 +08:00
|
|
|
} else {
|
2021-12-17 19:06:21 +08:00
|
|
|
if !shouldUpdateRequests && cn.t.wantPieceIndex(pieceIndex(x)) {
|
|
|
|
shouldUpdateRequests = true
|
2021-05-10 15:02:17 +08:00
|
|
|
}
|
2021-12-17 19:06:21 +08:00
|
|
|
// We must be gaining this piece
|
|
|
|
cn.t.incPieceAvailability(pieceIndex(x))
|
2016-03-21 11:17:15 +08:00
|
|
|
}
|
2021-12-17 19:06:21 +08:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
// Apply the changes. If we had everything previously, this should be empty, so xor is the same
|
|
|
|
// as or.
|
|
|
|
cn._peerPieces.Xor(&bm)
|
|
|
|
if shouldUpdateRequests {
|
|
|
|
cn.updateRequests("bitfield")
|
2016-03-21 11:17:15 +08:00
|
|
|
}
|
2021-12-17 19:06:21 +08:00
|
|
|
// We didn't guard this before, I see no reason to do it now.
|
2021-10-11 12:45:55 +08:00
|
|
|
cn.peerPiecesChanged()
|
2016-03-22 09:07:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:37:40 +08:00
|
|
|
func (cn *PeerConn) onPeerHasAllPieces() {
|
2021-05-10 15:02:17 +08:00
|
|
|
t := cn.t
|
|
|
|
if t.haveInfo() {
|
2021-12-17 19:06:21 +08:00
|
|
|
cn._peerPieces.Iterate(func(x uint32) bool {
|
|
|
|
t.decPieceAvailability(pieceIndex(x))
|
|
|
|
return true
|
|
|
|
})
|
2021-05-10 15:02:17 +08:00
|
|
|
}
|
2021-12-17 19:06:21 +08:00
|
|
|
t.addConnWithAllPieces(&cn.Peer)
|
2018-01-29 15:18:08 +08:00
|
|
|
cn.peerSentHaveAll = true
|
2020-01-10 12:09:21 +08:00
|
|
|
cn._peerPieces.Clear()
|
2021-10-13 12:16:53 +08:00
|
|
|
if !cn.t._pendingPieces.IsEmpty() {
|
|
|
|
cn.updateRequests("Peer.onPeerHasAllPieces")
|
2021-10-11 12:45:55 +08:00
|
|
|
}
|
|
|
|
cn.peerPiecesChanged()
|
2021-05-10 15:02:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *PeerConn) onPeerSentHaveAll() error {
|
|
|
|
cn.onPeerHasAllPieces()
|
2016-03-22 09:07:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) peerSentHaveNone() error {
|
2023-02-19 10:24:25 +08:00
|
|
|
if !cn.peerSentHaveAll {
|
2021-12-17 19:06:21 +08:00
|
|
|
cn.t.decPeerPieceAvailability(&cn.Peer)
|
|
|
|
}
|
2020-01-10 12:09:21 +08:00
|
|
|
cn._peerPieces.Clear()
|
2018-01-29 15:18:08 +08:00
|
|
|
cn.peerSentHaveAll = false
|
2021-10-11 12:45:55 +08:00
|
|
|
cn.peerPiecesChanged()
|
2016-03-21 11:17:15 +08:00
|
|
|
return nil
|
|
|
|
}
|
2016-05-16 16:46:38 +08:00
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) requestPendingMetadata() {
|
2016-05-16 16:46:38 +08:00
|
|
|
if c.t.haveInfo() {
|
|
|
|
return
|
|
|
|
}
|
2018-07-10 10:20:36 +08:00
|
|
|
if c.PeerExtensionIDs[pp.ExtensionNameMetadata] == 0 {
|
2016-05-16 16:46:38 +08:00
|
|
|
// Peer doesn't support this.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Request metadata pieces that we don't have in a random order.
|
|
|
|
var pending []int
|
|
|
|
for index := 0; index < c.t.metadataPieceCount(); index++ {
|
|
|
|
if !c.t.haveMetadataPiece(index) && !c.requestedMetadataPiece(index) {
|
|
|
|
pending = append(pending, index)
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 16:45:36 +08:00
|
|
|
rand.Shuffle(len(pending), func(i, j int) { pending[i], pending[j] = pending[j], pending[i] })
|
|
|
|
for _, i := range pending {
|
|
|
|
c.requestMetadataPiece(i)
|
2016-05-16 16:46:38 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-05 13:52:33 +08:00
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) wroteMsg(msg *pp.Message) {
|
2018-06-29 20:10:21 +08:00
|
|
|
torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
|
2020-04-15 16:09:15 +08:00
|
|
|
if msg.Type == pp.Extended {
|
|
|
|
for name, id := range cn.PeerExtensionIDs {
|
|
|
|
if id != msg.ExtendedID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
torrent.Add(fmt.Sprintf("Extended messages written for protocol %q", name), 1)
|
|
|
|
}
|
|
|
|
}
|
2018-06-10 07:18:52 +08:00
|
|
|
cn.allStats(func(cs *ConnStats) { cs.wroteMsg(msg) })
|
2016-07-05 13:52:33 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) wroteBytes(n int64) {
|
2018-06-12 18:21:53 +08:00
|
|
|
cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesWritten }))
|
2018-06-10 07:18:52 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) fastEnabled() bool {
|
2020-04-23 11:03:35 +08:00
|
|
|
return c.PeerExtensionBytes.SupportsFast() && c.t.cl.config.Extensions.SupportsFast()
|
2018-02-03 10:35:09 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 11:23:22 +08:00
|
|
|
func (c *PeerConn) reject(r Request) {
|
2018-02-03 10:35:09 +08:00
|
|
|
if !c.fastEnabled() {
|
2018-02-04 21:18:38 +08:00
|
|
|
panic("fast not enabled")
|
2018-02-03 10:35:09 +08:00
|
|
|
}
|
2021-05-10 10:29:47 +08:00
|
|
|
c.write(r.ToMsg(pp.Reject))
|
2023-02-14 09:46:23 +08:00
|
|
|
// It is possible to reject a request before it is added to peer requests due to being invalid.
|
|
|
|
if state, ok := c.peerRequests[r]; ok {
|
|
|
|
state.allocReservation.Drop()
|
|
|
|
delete(c.peerRequests, r)
|
|
|
|
}
|
2018-02-03 10:35:09 +08:00
|
|
|
}
|
|
|
|
|
2022-06-25 21:16:58 +08:00
|
|
|
func (c *PeerConn) maximumPeerRequestChunkLength() (_ Option[int]) {
|
|
|
|
uploadRateLimiter := c.t.cl.config.UploadRateLimiter
|
|
|
|
if uploadRateLimiter.Limit() == rate.Inf {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return Some(uploadRateLimiter.Burst())
|
|
|
|
}
|
|
|
|
|
|
|
|
// startFetch is for testing purposes currently.
|
|
|
|
func (c *PeerConn) onReadRequest(r Request, startFetch bool) error {
|
2018-02-02 16:19:14 +08:00
|
|
|
requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1)
|
2023-05-06 11:44:23 +08:00
|
|
|
// 来自某个peer的重复请求
|
2020-02-21 08:07:50 +08:00
|
|
|
if _, ok := c.peerRequests[r]; ok {
|
2018-02-04 21:18:38 +08:00
|
|
|
torrent.Add("duplicate requests received", 1)
|
2022-06-25 21:16:58 +08:00
|
|
|
if c.fastEnabled() {
|
|
|
|
return errors.New("received duplicate request with fast enabled")
|
|
|
|
}
|
2018-02-03 10:35:09 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-05-06 11:44:23 +08:00
|
|
|
// 当前peer choking, 无法返回chunk
|
2020-02-21 08:07:50 +08:00
|
|
|
if c.choking {
|
2018-02-04 21:18:38 +08:00
|
|
|
torrent.Add("requests received while choking", 1)
|
|
|
|
if c.fastEnabled() {
|
|
|
|
torrent.Add("requests rejected while choking", 1)
|
|
|
|
c.reject(r)
|
|
|
|
}
|
2018-02-02 16:19:14 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-20 06:34:20 +08:00
|
|
|
// TODO: What if they've already requested this?
|
|
|
|
if len(c.peerRequests) >= localClientReqq {
|
2018-02-04 21:18:38 +08:00
|
|
|
torrent.Add("requests received while queue full", 1)
|
|
|
|
if c.fastEnabled() {
|
|
|
|
c.reject(r)
|
|
|
|
}
|
|
|
|
// BEP 6 says we may close here if we choose.
|
2018-02-02 16:19:14 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-05-06 11:44:23 +08:00
|
|
|
// chunk size is usually 16 KiB
|
2022-06-25 21:16:58 +08:00
|
|
|
if opt := c.maximumPeerRequestChunkLength(); opt.Ok && int(r.Length) > opt.Value {
|
|
|
|
err := fmt.Errorf("peer requested chunk too long (%v)", r.Length)
|
|
|
|
c.logger.Levelf(log.Warning, err.Error())
|
|
|
|
if c.fastEnabled() {
|
|
|
|
c.reject(r)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-05-06 11:44:23 +08:00
|
|
|
// 当前peer没有该piece, 因此没有该chunk
|
2018-07-17 19:28:01 +08:00
|
|
|
if !c.t.havePiece(pieceIndex(r.Index)) {
|
2022-06-25 21:16:58 +08:00
|
|
|
// TODO: Tell the peer we don't have the piece, and reject this request.
|
2018-02-02 16:19:14 +08:00
|
|
|
requestsReceivedForMissingPieces.Add(1)
|
|
|
|
return fmt.Errorf("peer requested piece we don't have: %v", r.Index.Int())
|
|
|
|
}
|
2023-05-06 11:44:23 +08:00
|
|
|
// 检查chunk是否超过piece的范围
|
2023-02-13 20:27:15 +08:00
|
|
|
pieceLength := c.t.pieceLength(pieceIndex(r.Index))
|
2019-08-23 10:19:28 +08:00
|
|
|
// Check this after we know we have the piece, so that the piece length will be known.
|
2023-02-13 20:27:15 +08:00
|
|
|
if chunkOverflowsPiece(r.ChunkSpec, pieceLength) {
|
2019-08-23 10:19:28 +08:00
|
|
|
torrent.Add("bad requests received", 1)
|
2023-02-13 20:27:15 +08:00
|
|
|
return errors.New("chunk overflows piece")
|
2019-08-23 10:19:28 +08:00
|
|
|
}
|
2020-02-21 08:07:50 +08:00
|
|
|
if c.peerRequests == nil {
|
2021-05-20 06:34:20 +08:00
|
|
|
c.peerRequests = make(map[Request]*peerRequestState, localClientReqq)
|
2018-02-02 16:19:14 +08:00
|
|
|
}
|
2023-02-14 09:46:23 +08:00
|
|
|
value := &peerRequestState{
|
|
|
|
allocReservation: c.peerRequestDataAllocLimiter.Reserve(int64(r.Length)),
|
|
|
|
}
|
2020-11-13 12:50:08 +08:00
|
|
|
c.peerRequests[r] = value
|
2022-06-25 21:16:58 +08:00
|
|
|
if startFetch {
|
|
|
|
// TODO: Limit peer request data read concurrency.
|
|
|
|
go c.peerRequestDataReader(r, value)
|
|
|
|
}
|
2018-02-02 16:19:14 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-28 11:23:22 +08:00
|
|
|
func (c *PeerConn) peerRequestDataReader(r Request, prs *peerRequestState) {
|
2023-05-06 11:44:23 +08:00
|
|
|
b, err := c.readPeerRequestData(r, prs) // 读取数据(读取方式与该torrent的storage有关)
|
2020-11-13 12:50:08 +08:00
|
|
|
c.locker().Lock()
|
|
|
|
defer c.locker().Unlock()
|
|
|
|
if err != nil {
|
|
|
|
c.peerRequestDataReadFailed(err, r)
|
|
|
|
} else {
|
|
|
|
if b == nil {
|
|
|
|
panic("data must be non-nil to trigger send")
|
|
|
|
}
|
2021-12-27 19:06:52 +08:00
|
|
|
torrent.Add("peer request data read successes", 1)
|
2020-11-13 12:50:08 +08:00
|
|
|
prs.data = b
|
2022-06-14 08:07:09 +08:00
|
|
|
// This might be required for the error case too (#752 and #753).
|
2020-11-13 12:50:08 +08:00
|
|
|
c.tickleWriter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is maintained correctly, we might be able to support optional synchronous reading for
|
|
|
|
// chunk sending, the way it used to work.
|
2021-01-28 11:23:22 +08:00
|
|
|
func (c *PeerConn) peerRequestDataReadFailed(err error, r Request) {
|
2021-12-27 19:06:52 +08:00
|
|
|
torrent.Add("peer request data read failures", 1)
|
|
|
|
logLevel := log.Warning
|
|
|
|
if c.t.hasStorageCap() {
|
|
|
|
// It's expected that pieces might drop. See
|
|
|
|
// https://github.com/anacrolix/torrent/issues/702#issuecomment-1000953313.
|
|
|
|
logLevel = log.Debug
|
|
|
|
}
|
|
|
|
c.logger.WithDefaultLevel(logLevel).Printf("error reading chunk for peer Request %v: %v", r, err)
|
2021-10-27 12:14:11 +08:00
|
|
|
if c.t.closed.IsSet() {
|
|
|
|
return
|
|
|
|
}
|
2020-11-13 12:50:08 +08:00
|
|
|
i := pieceIndex(r.Index)
|
|
|
|
if c.t.pieceComplete(i) {
|
|
|
|
// There used to be more code here that just duplicated the following break. Piece
|
|
|
|
// completions are currently cached, so I'm not sure how helpful this update is, except to
|
|
|
|
// pull any completion changes pushed to the storage backend in failed reads that got us
|
|
|
|
// here.
|
|
|
|
c.t.updatePieceCompletion(i)
|
|
|
|
}
|
2021-12-27 19:19:04 +08:00
|
|
|
// We've probably dropped a piece from storage, but there's no way to communicate this to the
|
|
|
|
// peer. If they ask for it again, we kick them allowing us to send them updated piece states if
|
|
|
|
// we reconnect. TODO: Instead, we could just try to update them with Bitfield or HaveNone and
|
|
|
|
// if they kick us for breaking protocol, on reconnect we will be compliant again (at least
|
|
|
|
// initially).
|
|
|
|
if c.fastEnabled() {
|
|
|
|
c.reject(r)
|
|
|
|
} else {
|
|
|
|
if c.choking {
|
|
|
|
// If fast isn't enabled, I think we would have wiped all peer requests when we last
|
|
|
|
// choked, and requests while we're choking would be ignored. It could be possible that
|
|
|
|
// a peer request data read completed concurrently to it being deleted elsewhere.
|
|
|
|
c.logger.WithDefaultLevel(log.Warning).Printf("already choking peer, requests might not be rejected correctly")
|
|
|
|
}
|
|
|
|
// Choking a non-fast peer should cause them to flush all their requests.
|
|
|
|
c.choke(c.write)
|
2020-11-13 12:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 11:44:23 +08:00
|
|
|
// 利用 torrent.Piece.Storage().ReadAt() 读取数据到b
|
2023-02-14 09:46:23 +08:00
|
|
|
func (c *PeerConn) readPeerRequestData(r Request, prs *peerRequestState) ([]byte, error) {
|
|
|
|
// Should we depend on Torrent closure here? I think it's okay to get cancelled from elsewhere,
|
|
|
|
// or fail to read and then cleanup.
|
|
|
|
ctx := context.Background()
|
|
|
|
err := prs.allocReservation.Wait(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if ctx.Err() == nil {
|
|
|
|
// The error is from the reservation itself. Something is very broken, or we're not
|
|
|
|
// guarding against excessively large requests.
|
|
|
|
err = log.WithLevel(log.Critical, err)
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("waiting for alloc limit reservation: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-13 12:50:08 +08:00
|
|
|
b := make([]byte, r.Length)
|
|
|
|
p := c.t.info.Piece(int(r.Index))
|
|
|
|
n, err := c.t.readAt(b, p.Offset()+int64(r.Begin))
|
|
|
|
if n == len(b) {
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err == nil {
|
|
|
|
panic("expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 10:36:37 +08:00
|
|
|
func (c *PeerConn) logProtocolBehaviour(level log.Level, format string, arg ...interface{}) {
|
2022-01-23 06:37:11 +08:00
|
|
|
c.logger.WithContextText(fmt.Sprintf(
|
2021-12-06 16:02:55 +08:00
|
|
|
"peer id %q, ext v %q", c.PeerID, c.PeerClientName.Load(),
|
2022-01-23 06:37:11 +08:00
|
|
|
)).SkipCallers(1).Levelf(level, format, arg...)
|
2021-10-19 10:36:37 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 16:13:55 +08:00
|
|
|
// Processes incoming BitTorrent wire-protocol messages. The client lock is held upon entry and
|
|
|
|
// exit. Returning will end the connection.
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) mainReadLoop() (err error) {
|
2018-02-12 21:56:21 +08:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
torrent.Add("connection.mainReadLoop returned with error", 1)
|
|
|
|
} else {
|
|
|
|
torrent.Add("connection.mainReadLoop returned with no error", 1)
|
|
|
|
}
|
|
|
|
}()
|
2016-09-11 12:32:56 +08:00
|
|
|
t := c.t
|
|
|
|
cl := t.cl
|
2016-09-12 15:10:11 +08:00
|
|
|
|
2016-09-11 12:32:56 +08:00
|
|
|
decoder := pp.Decoder{
|
2017-09-21 17:29:56 +08:00
|
|
|
R: bufio.NewReaderSize(c.r, 1<<17),
|
2022-05-08 09:36:43 +08:00
|
|
|
MaxLength: 4 * pp.Integer(max(int64(t.chunkSize), defaultChunkSize)),
|
2021-09-09 20:27:16 +08:00
|
|
|
Pool: &t.chunkPool,
|
2016-09-11 12:32:56 +08:00
|
|
|
}
|
|
|
|
for {
|
2018-02-12 21:56:21 +08:00
|
|
|
var msg pp.Message
|
2017-08-31 14:25:49 +08:00
|
|
|
func() {
|
2018-07-25 11:41:50 +08:00
|
|
|
cl.unlock()
|
|
|
|
defer cl.lock()
|
2017-08-31 14:25:49 +08:00
|
|
|
err = decoder.Decode(&msg)
|
|
|
|
}()
|
2020-10-14 12:11:45 +08:00
|
|
|
if cb := c.callbacks.ReadMessage; cb != nil && err == nil {
|
2020-07-15 12:00:47 +08:00
|
|
|
cb(c, &msg)
|
|
|
|
}
|
2020-05-03 16:40:09 +08:00
|
|
|
if t.closed.IsSet() || c.closed.IsSet() {
|
2016-09-11 12:32:56 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.lastMessageReceived = time.Now()
|
|
|
|
if msg.Keepalive {
|
|
|
|
receivedKeepalives.Add(1)
|
|
|
|
continue
|
|
|
|
}
|
2018-02-05 12:30:35 +08:00
|
|
|
messageTypesReceived.Add(msg.Type.String(), 1)
|
2018-02-04 21:18:38 +08:00
|
|
|
if msg.Type.FastExtension() && !c.fastEnabled() {
|
2020-04-23 10:34:43 +08:00
|
|
|
runSafeExtraneous(func() { torrent.Add("fast messages received when extension is disabled", 1) })
|
2018-02-04 21:18:38 +08:00
|
|
|
return fmt.Errorf("received fast extension message (type=%v) but extension is disabled", msg.Type)
|
|
|
|
}
|
2016-09-11 12:32:56 +08:00
|
|
|
switch msg.Type {
|
|
|
|
case pp.Choke:
|
2021-10-12 08:58:46 +08:00
|
|
|
if c.peerChoking {
|
|
|
|
break
|
|
|
|
}
|
2020-04-23 10:36:19 +08:00
|
|
|
if !c.fastEnabled() {
|
2022-05-09 09:34:08 +08:00
|
|
|
c.deleteAllRequests("choked by non-fast PeerConn")
|
2021-10-11 15:21:24 +08:00
|
|
|
} else {
|
2021-10-25 13:36:58 +08:00
|
|
|
// We don't decrement pending requests here, let's wait for the peer to either
|
2021-12-11 21:04:06 +08:00
|
|
|
// reject or satisfy the outstanding requests. Additionally, some peers may unchoke
|
2021-10-25 13:36:58 +08:00
|
|
|
// us and resume where they left off, we don't want to have piled on to those chunks
|
2021-12-11 21:04:06 +08:00
|
|
|
// in the meanwhile. I think a peer's ability to abuse this should be limited: they
|
2021-10-25 13:36:58 +08:00
|
|
|
// could let us request a lot of stuff, then choke us and never reject, but they're
|
|
|
|
// only a single peer, our chunk balancing should smooth over this abuse.
|
2020-04-23 10:36:19 +08:00
|
|
|
}
|
2021-10-12 08:58:46 +08:00
|
|
|
c.peerChoking = true
|
2018-06-24 18:04:31 +08:00
|
|
|
c.updateExpectingChunks()
|
2016-09-11 12:32:56 +08:00
|
|
|
case pp.Unchoke:
|
2021-10-11 15:21:24 +08:00
|
|
|
if !c.peerChoking {
|
2021-10-19 10:36:37 +08:00
|
|
|
// Some clients do this for some reason. Transmission doesn't error on this, so we
|
|
|
|
// won't for consistency.
|
2021-10-20 08:22:35 +08:00
|
|
|
c.logProtocolBehaviour(log.Debug, "received unchoke when already unchoked")
|
2021-10-19 10:36:37 +08:00
|
|
|
break
|
2021-10-11 15:21:24 +08:00
|
|
|
}
|
2020-02-21 08:07:50 +08:00
|
|
|
c.peerChoking = false
|
2021-10-12 10:06:11 +08:00
|
|
|
preservedCount := 0
|
2022-05-09 09:34:08 +08:00
|
|
|
c.requestState.Requests.Iterate(func(x RequestIndex) bool {
|
|
|
|
if !c.peerAllowedFast.Contains(c.t.pieceIndexOfRequestIndex(x)) {
|
2021-10-12 10:06:11 +08:00
|
|
|
preservedCount++
|
2021-10-11 15:21:24 +08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2021-10-12 10:06:11 +08:00
|
|
|
if preservedCount != 0 {
|
|
|
|
// TODO: Yes this is a debug log but I'm not happy with the state of the logging lib
|
|
|
|
// right now.
|
2022-01-23 06:37:11 +08:00
|
|
|
c.logger.Levelf(log.Debug,
|
2021-10-18 16:06:33 +08:00
|
|
|
"%v requests were preserved while being choked (fast=%v)",
|
|
|
|
preservedCount,
|
|
|
|
c.fastEnabled())
|
2022-01-23 06:37:11 +08:00
|
|
|
|
2021-10-12 10:06:11 +08:00
|
|
|
torrent.Add("requestsPreservedThroughChoking", int64(preservedCount))
|
|
|
|
}
|
2021-11-29 09:12:30 +08:00
|
|
|
if !c.t._pendingPieces.IsEmpty() {
|
|
|
|
c.updateRequests("unchoked")
|
|
|
|
}
|
2018-06-24 18:04:31 +08:00
|
|
|
c.updateExpectingChunks()
|
2016-09-11 12:32:56 +08:00
|
|
|
case pp.Interested:
|
2020-02-21 08:07:50 +08:00
|
|
|
c.peerInterested = true
|
2017-09-18 10:09:08 +08:00
|
|
|
c.tickleWriter()
|
2016-09-11 12:32:56 +08:00
|
|
|
case pp.NotInterested:
|
2020-02-21 08:07:50 +08:00
|
|
|
c.peerInterested = false
|
2018-06-23 20:32:11 +08:00
|
|
|
// We don't clear their requests since it isn't clear in the spec.
|
|
|
|
// We'll probably choke them for this, which will clear them if
|
|
|
|
// appropriate, and is clearly specified.
|
2016-09-11 12:32:56 +08:00
|
|
|
case pp.Have:
|
2018-07-17 19:28:01 +08:00
|
|
|
err = c.peerSentHave(pieceIndex(msg.Index))
|
2019-10-01 16:13:16 +08:00
|
|
|
case pp.Bitfield:
|
|
|
|
err = c.peerSentBitfield(msg.Bitfield)
|
2023-05-06 11:44:23 +08:00
|
|
|
// peer对chunk的请求(需要发送chunk, load from memory)
|
2016-09-11 12:32:56 +08:00
|
|
|
case pp.Request:
|
2018-02-02 16:19:14 +08:00
|
|
|
r := newRequestFromMessage(&msg)
|
2022-06-25 21:16:58 +08:00
|
|
|
err = c.onReadRequest(r, true)
|
2023-02-13 20:27:15 +08:00
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("on reading request %v: %w", r, err)
|
|
|
|
}
|
2023-05-06 11:44:23 +08:00
|
|
|
// 收到peer的chunk(需要接收chunk, store into memory)
|
2016-09-11 12:32:56 +08:00
|
|
|
case pp.Piece:
|
2021-05-21 09:49:57 +08:00
|
|
|
c.doChunkReadStats(int64(len(msg.Piece)))
|
2018-06-24 18:35:46 +08:00
|
|
|
err = c.receiveChunk(&msg)
|
2016-09-12 15:10:11 +08:00
|
|
|
if len(msg.Piece) == int(t.chunkSize) {
|
2017-11-07 13:11:59 +08:00
|
|
|
t.chunkPool.Put(&msg.Piece)
|
2016-09-12 15:10:11 +08:00
|
|
|
}
|
2018-06-26 18:24:39 +08:00
|
|
|
if err != nil {
|
2021-09-30 10:01:56 +08:00
|
|
|
err = fmt.Errorf("receiving chunk: %w", err)
|
2018-06-26 18:24:39 +08:00
|
|
|
}
|
2019-10-01 16:13:16 +08:00
|
|
|
case pp.Cancel:
|
|
|
|
req := newRequestFromMessage(&msg)
|
|
|
|
c.onPeerSentCancel(req)
|
2016-09-11 12:32:56 +08:00
|
|
|
case pp.Port:
|
2020-07-15 14:15:38 +08:00
|
|
|
ipa, ok := tryIpPortFromNetAddr(c.RemoteAddr)
|
2020-02-20 13:47:37 +08:00
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2018-11-04 13:56:55 +08:00
|
|
|
pingAddr := net.UDPAddr{
|
2020-02-20 13:47:37 +08:00
|
|
|
IP: ipa.IP,
|
|
|
|
Port: ipa.Port,
|
2016-09-11 12:32:56 +08:00
|
|
|
}
|
|
|
|
if msg.Port != 0 {
|
|
|
|
pingAddr.Port = int(msg.Port)
|
|
|
|
}
|
2020-02-20 14:46:29 +08:00
|
|
|
cl.eachDhtServer(func(s DhtServer) {
|
|
|
|
go s.Ping(&pingAddr)
|
2018-04-12 09:41:07 +08:00
|
|
|
})
|
2019-10-01 16:13:16 +08:00
|
|
|
case pp.Suggest:
|
|
|
|
torrent.Add("suggests received", 1)
|
2022-01-23 06:37:11 +08:00
|
|
|
log.Fmsg("peer suggested piece %d", msg.Index).AddValues(c, msg.Index).LogLevel(log.Debug, c.t.logger)
|
2021-10-08 10:53:36 +08:00
|
|
|
c.updateRequests("suggested")
|
2019-10-01 16:13:16 +08:00
|
|
|
case pp.HaveAll:
|
|
|
|
err = c.onPeerSentHaveAll()
|
|
|
|
case pp.HaveNone:
|
|
|
|
err = c.peerSentHaveNone()
|
|
|
|
case pp.Reject:
|
2021-12-11 21:04:06 +08:00
|
|
|
req := newRequestFromMessage(&msg)
|
|
|
|
if !c.remoteRejectedRequest(c.t.requestIndexFromRequest(req)) {
|
2022-04-14 17:02:45 +08:00
|
|
|
c.logger.Printf("received invalid reject [request=%v, peer=%v]", req, c)
|
2021-12-11 21:04:06 +08:00
|
|
|
err = fmt.Errorf("received invalid reject [request=%v]", req)
|
|
|
|
}
|
2018-02-04 21:18:38 +08:00
|
|
|
case pp.AllowedFast:
|
|
|
|
torrent.Add("allowed fasts received", 1)
|
2022-01-23 06:37:11 +08:00
|
|
|
log.Fmsg("peer allowed fast: %d", msg.Index).AddValues(c).LogLevel(log.Debug, c.t.logger)
|
2021-10-11 19:04:53 +08:00
|
|
|
c.updateRequests("PeerConn.mainReadLoop allowed fast")
|
2019-10-01 16:13:16 +08:00
|
|
|
case pp.Extended:
|
|
|
|
err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload)
|
2016-09-11 12:32:56 +08:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("received unknown message type: %#v", msg.Type)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 13:30:51 +08:00
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) onReadExtendedMsg(id pp.ExtensionNumber, payload []byte) (err error) {
|
2018-02-03 11:06:10 +08:00
|
|
|
defer func() {
|
|
|
|
// TODO: Should we still do this?
|
|
|
|
if err != nil {
|
|
|
|
// These clients use their own extension IDs for outgoing message
|
|
|
|
// types, which is incorrect.
|
|
|
|
if bytes.HasPrefix(c.PeerID[:], []byte("-SD0100-")) || strings.HasPrefix(string(c.PeerID[:]), "-XL0012-") {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
t := c.t
|
|
|
|
cl := t.cl
|
|
|
|
switch id {
|
|
|
|
case pp.HandshakeExtendedID:
|
2018-07-10 10:20:36 +08:00
|
|
|
var d pp.ExtendedHandshakeMessage
|
|
|
|
if err := bencode.Unmarshal(payload, &d); err != nil {
|
2020-04-18 15:45:01 +08:00
|
|
|
c.logger.Printf("error parsing extended handshake message %q: %s", payload, err)
|
2021-05-11 15:14:02 +08:00
|
|
|
return fmt.Errorf("unmarshalling extended handshake payload: %w", err)
|
2018-02-03 11:06:10 +08:00
|
|
|
}
|
2020-10-14 12:11:45 +08:00
|
|
|
if cb := c.callbacks.ReadExtendedHandshake; cb != nil {
|
2020-07-15 14:16:09 +08:00
|
|
|
cb(c, &d)
|
|
|
|
}
|
2021-11-08 11:47:01 +08:00
|
|
|
// c.logger.WithDefaultLevel(log.Debug).Printf("received extended handshake message:\n%s", spew.Sdump(d))
|
2018-07-10 10:20:36 +08:00
|
|
|
if d.Reqq != 0 {
|
|
|
|
c.PeerMaxRequests = d.Reqq
|
2018-02-03 11:06:10 +08:00
|
|
|
}
|
2021-12-06 16:02:55 +08:00
|
|
|
c.PeerClientName.Store(d.V)
|
2018-07-10 10:20:36 +08:00
|
|
|
if c.PeerExtensionIDs == nil {
|
|
|
|
c.PeerExtensionIDs = make(map[pp.ExtensionName]pp.ExtensionNumber, len(d.M))
|
2018-02-03 11:06:10 +08:00
|
|
|
}
|
2019-12-11 19:45:04 +08:00
|
|
|
c.PeerListenPort = d.Port
|
|
|
|
c.PeerPrefersEncryption = d.Encryption
|
2018-07-10 10:20:36 +08:00
|
|
|
for name, id := range d.M {
|
|
|
|
if _, ok := c.PeerExtensionIDs[name]; !ok {
|
2021-11-19 10:02:42 +08:00
|
|
|
peersSupportingExtension.Add(
|
|
|
|
// expvar.Var.String must produce valid JSON. "ut_payme\xeet_address" was being
|
|
|
|
// entered here which caused problems later when unmarshalling.
|
|
|
|
strconv.Quote(string(name)),
|
|
|
|
1)
|
2018-02-03 11:06:10 +08:00
|
|
|
}
|
2018-07-10 10:20:36 +08:00
|
|
|
c.PeerExtensionIDs[name] = id
|
2018-02-03 11:06:10 +08:00
|
|
|
}
|
2018-07-10 10:20:36 +08:00
|
|
|
if d.MetadataSize != 0 {
|
|
|
|
if err = t.setMetadataSize(d.MetadataSize); err != nil {
|
2021-05-11 15:14:02 +08:00
|
|
|
return fmt.Errorf("setting metadata size to %d: %w", d.MetadataSize, err)
|
2018-02-03 11:06:10 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-01 16:46:10 +08:00
|
|
|
c.requestPendingMetadata()
|
2020-04-09 00:03:29 +08:00
|
|
|
if !t.cl.config.DisablePEX {
|
|
|
|
t.pex.Add(c) // we learnt enough now
|
|
|
|
c.pex.Init(c)
|
2019-12-11 19:45:04 +08:00
|
|
|
}
|
2018-02-03 11:06:10 +08:00
|
|
|
return nil
|
|
|
|
case metadataExtendedId:
|
|
|
|
err := cl.gotMetadataExtensionMsg(payload, t, c)
|
|
|
|
if err != nil {
|
2019-10-03 17:09:55 +08:00
|
|
|
return fmt.Errorf("handling metadata extension message: %w", err)
|
2018-02-03 11:06:10 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case pexExtendedId:
|
2020-04-09 00:03:29 +08:00
|
|
|
if !c.pex.IsEnabled() {
|
|
|
|
return nil // or hang-up maybe?
|
2018-02-03 11:06:10 +08:00
|
|
|
}
|
2020-04-09 00:03:29 +08:00
|
|
|
return c.pex.Recv(payload)
|
2018-02-03 11:06:10 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected extended message ID: %v", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 13:30:51 +08:00
|
|
|
// Set both the Reader and Writer for the connection from a single ReadWriter.
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) setRW(rw io.ReadWriter) {
|
2016-10-10 13:30:51 +08:00
|
|
|
cn.r = rw
|
|
|
|
cn.w = rw
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the Reader and Writer as a combined ReadWriter.
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) rw() io.ReadWriter {
|
2016-10-10 13:30:51 +08:00
|
|
|
return struct {
|
|
|
|
io.Reader
|
|
|
|
io.Writer
|
|
|
|
}{cn.r, cn.w}
|
|
|
|
}
|
2016-11-22 11:16:18 +08:00
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) uploadAllowed() bool {
|
2018-02-02 16:04:56 +08:00
|
|
|
if c.t.cl.config.NoUpload {
|
|
|
|
return false
|
2016-11-22 18:12:53 +08:00
|
|
|
}
|
2020-05-31 03:42:51 +08:00
|
|
|
if c.t.dataUploadDisallowed {
|
|
|
|
return false
|
|
|
|
}
|
2018-02-02 16:04:56 +08:00
|
|
|
if c.t.seeding() {
|
2017-09-18 10:09:08 +08:00
|
|
|
return true
|
2016-11-22 18:12:53 +08:00
|
|
|
}
|
2018-02-02 16:04:56 +08:00
|
|
|
if !c.peerHasWantedPieces() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Don't upload more than 100 KiB more than we download.
|
2020-01-10 12:09:21 +08:00
|
|
|
if c._stats.BytesWrittenData.Int64() >= c._stats.BytesReadData.Int64()+100<<10 {
|
2018-02-02 16:04:56 +08:00
|
|
|
return false
|
2016-11-22 18:12:53 +08:00
|
|
|
}
|
2018-02-02 16:04:56 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) setRetryUploadTimer(delay time.Duration) {
|
2018-02-02 16:04:56 +08:00
|
|
|
if c.uploadTimer == nil {
|
2021-05-20 16:51:08 +08:00
|
|
|
c.uploadTimer = time.AfterFunc(delay, c.tickleWriter)
|
2018-02-02 16:04:56 +08:00
|
|
|
} else {
|
|
|
|
c.uploadTimer.Reset(delay)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also handles choking and unchoking of the remote peer.
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) upload(msg func(pp.Message) bool) bool {
|
2016-11-22 18:12:53 +08:00
|
|
|
// Breaking or completing this loop means we don't want to upload to the
|
|
|
|
// peer anymore, and we choke them.
|
|
|
|
another:
|
2018-02-02 16:04:56 +08:00
|
|
|
for c.uploadAllowed() {
|
2016-11-22 18:12:53 +08:00
|
|
|
// We want to upload to the peer.
|
2020-02-21 08:07:50 +08:00
|
|
|
if !c.unchoke(msg) {
|
2017-09-18 10:09:08 +08:00
|
|
|
return false
|
|
|
|
}
|
2020-11-13 12:50:08 +08:00
|
|
|
for r, state := range c.peerRequests {
|
|
|
|
if state.data == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-06-16 14:30:04 +08:00
|
|
|
res := c.t.cl.config.UploadRateLimiter.ReserveN(time.Now(), int(r.Length))
|
2017-09-18 10:09:08 +08:00
|
|
|
if !res.OK() {
|
2017-11-04 13:58:38 +08:00
|
|
|
panic(fmt.Sprintf("upload rate limiter burst size < %d", r.Length))
|
2017-09-18 10:09:08 +08:00
|
|
|
}
|
2016-11-22 18:12:53 +08:00
|
|
|
delay := res.Delay()
|
|
|
|
if delay > 0 {
|
|
|
|
res.Cancel()
|
2018-02-02 16:04:56 +08:00
|
|
|
c.setRetryUploadTimer(delay)
|
2017-09-18 10:09:08 +08:00
|
|
|
// Hard to say what to return here.
|
|
|
|
return true
|
2016-11-22 18:12:53 +08:00
|
|
|
}
|
2020-11-13 12:50:08 +08:00
|
|
|
more := c.sendChunk(r, msg, state)
|
2020-02-21 08:07:50 +08:00
|
|
|
delete(c.peerRequests, r)
|
2017-09-18 10:09:08 +08:00
|
|
|
if !more {
|
|
|
|
return false
|
|
|
|
}
|
2023-05-06 11:44:23 +08:00
|
|
|
// 完成一次写入, 打印日志
|
2023-05-08 11:30:36 +08:00
|
|
|
if Debug {
|
|
|
|
log.Printf("write %d to %s", len(state.data), c.RemoteAddr)
|
|
|
|
}
|
2016-11-22 18:12:53 +08:00
|
|
|
goto another
|
|
|
|
}
|
2017-09-18 10:09:08 +08:00
|
|
|
return true
|
2016-11-22 18:12:53 +08:00
|
|
|
}
|
2020-02-21 08:07:50 +08:00
|
|
|
return c.choke(msg)
|
2016-11-22 18:12:53 +08:00
|
|
|
}
|
2016-11-23 08:52:41 +08:00
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (cn *PeerConn) drop() {
|
2016-11-23 08:52:41 +08:00
|
|
|
cn.t.dropConnection(cn)
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:33:34 +08:00
|
|
|
func (cn *PeerConn) ban() {
|
|
|
|
cn.t.cl.banPeerIP(cn.remoteIp())
|
|
|
|
}
|
|
|
|
|
2020-11-13 12:50:08 +08:00
|
|
|
// This is called when something has changed that should wake the writer, such as putting stuff into
|
|
|
|
// the writeBuffer, or changing some state that the writer can act on.
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) tickleWriter() {
|
2021-05-20 16:51:08 +08:00
|
|
|
c.messageWriter.writeCond.Broadcast()
|
2017-09-01 13:26:50 +08:00
|
|
|
}
|
2017-09-18 11:47:49 +08:00
|
|
|
|
2023-05-06 11:44:23 +08:00
|
|
|
// 函数式编程
|
|
|
|
// fillWriteBuffer => cn.upload(cn.write)
|
|
|
|
// msg即cn.write负责将数据写入缓冲区(套接字的缓冲区)
|
2021-01-28 11:23:22 +08:00
|
|
|
func (c *PeerConn) sendChunk(r Request, msg func(pp.Message) bool, state *peerRequestState) (more bool) {
|
2020-11-13 12:50:08 +08:00
|
|
|
c.lastChunkSent = time.Now()
|
2023-02-14 09:46:23 +08:00
|
|
|
state.allocReservation.Release()
|
2020-11-13 12:50:08 +08:00
|
|
|
return msg(pp.Message{
|
2018-02-02 10:26:04 +08:00
|
|
|
Type: pp.Piece,
|
|
|
|
Index: r.Index,
|
|
|
|
Begin: r.Begin,
|
2020-11-13 12:50:08 +08:00
|
|
|
Piece: state.data,
|
2018-02-02 10:26:04 +08:00
|
|
|
})
|
|
|
|
}
|
2018-02-02 16:07:20 +08:00
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) setTorrent(t *Torrent) {
|
2018-02-02 16:07:20 +08:00
|
|
|
if c.t != nil {
|
|
|
|
panic("connection already associated with a torrent")
|
|
|
|
}
|
|
|
|
c.t = t
|
2020-10-10 07:24:58 +08:00
|
|
|
c.logger.WithDefaultLevel(log.Debug).Printf("set torrent=%v", t)
|
2018-06-10 07:18:52 +08:00
|
|
|
t.reconcileHandshakeStats(c)
|
2018-02-02 16:07:20 +08:00
|
|
|
}
|
2018-07-10 07:50:39 +08:00
|
|
|
|
2019-12-11 19:45:04 +08:00
|
|
|
func (c *PeerConn) pexPeerFlags() pp.PexPeerFlags {
|
|
|
|
f := pp.PexPeerFlags(0)
|
|
|
|
if c.PeerPrefersEncryption {
|
|
|
|
f |= pp.PexPrefersEncryption
|
|
|
|
}
|
|
|
|
if c.outgoing {
|
|
|
|
f |= pp.PexOutgoingConn
|
|
|
|
}
|
2021-01-25 11:22:24 +08:00
|
|
|
if c.utp() {
|
2019-12-11 19:45:04 +08:00
|
|
|
f |= pp.PexSupportsUtp
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:58:55 +08:00
|
|
|
// This returns the address to use if we want to dial the peer again. It incorporates the peer's
|
|
|
|
// advertised listen port.
|
2021-01-25 11:22:24 +08:00
|
|
|
func (c *PeerConn) dialAddr() PeerRemoteAddr {
|
2020-04-09 00:03:29 +08:00
|
|
|
if !c.outgoing && c.PeerListenPort != 0 {
|
2020-07-15 14:15:38 +08:00
|
|
|
switch addr := c.RemoteAddr.(type) {
|
2020-04-09 00:03:29 +08:00
|
|
|
case *net.TCPAddr:
|
|
|
|
dialAddr := *addr
|
|
|
|
dialAddr.Port = c.PeerListenPort
|
|
|
|
return &dialAddr
|
|
|
|
case *net.UDPAddr:
|
|
|
|
dialAddr := *addr
|
|
|
|
dialAddr.Port = c.PeerListenPort
|
|
|
|
return &dialAddr
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 14:15:38 +08:00
|
|
|
return c.RemoteAddr
|
2020-04-09 00:03:29 +08:00
|
|
|
}
|
|
|
|
|
2020-04-01 04:14:43 +08:00
|
|
|
func (c *PeerConn) pexEvent(t pexEventType) pexEvent {
|
|
|
|
f := c.pexPeerFlags()
|
2020-04-09 00:03:29 +08:00
|
|
|
addr := c.dialAddr()
|
2021-12-07 02:46:25 +08:00
|
|
|
return pexEvent{t, addr, f, nil}
|
2020-04-01 04:14:43 +08:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:07:50 +08:00
|
|
|
func (c *PeerConn) String() string {
|
2021-12-06 16:02:55 +08:00
|
|
|
return fmt.Sprintf("%T %p [id=%q, exts=%v, v=%q]", c, c, c.PeerID, c.PeerExtensionBytes, c.PeerClientName.Load())
|
2019-10-01 19:16:18 +08:00
|
|
|
}
|
2019-12-18 10:52:00 +08:00
|
|
|
|
2021-05-20 12:01:31 +08:00
|
|
|
// Returns the pieces the peer could have based on their claims. If we don't know how many pieces
|
|
|
|
// are in the torrent, it could be a very large range the peer has sent HaveAll.
|
2021-10-05 14:48:34 +08:00
|
|
|
func (cn *PeerConn) PeerPieces() *roaring.Bitmap {
|
2020-02-21 08:51:24 +08:00
|
|
|
cn.locker().RLock()
|
|
|
|
defer cn.locker().RUnlock()
|
2021-05-10 15:02:17 +08:00
|
|
|
return cn.newPeerPieces()
|
2020-02-21 08:51:24 +08:00
|
|
|
}
|
|
|
|
|
2021-12-07 11:16:02 +08:00
|
|
|
func (pc *PeerConn) remoteIsTransmission() bool {
|
|
|
|
return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-'
|
|
|
|
}
|