FedP2P/peerconn.go

1795 lines
50 KiB
Go
Raw Normal View History

package torrent
import (
"bufio"
2015-06-29 17:37:52 +08:00
"bytes"
"errors"
"fmt"
"io"
"math/rand"
"net"
"sort"
2016-02-09 21:45:47 +08:00
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/RoaringBitmap/roaring"
2022-01-21 19:28:15 +08:00
"github.com/anacrolix/chansync"
. "github.com/anacrolix/generics"
2018-02-03 09:06:52 +08:00
"github.com/anacrolix/log"
2017-01-14 17:39:48 +08:00
"github.com/anacrolix/missinggo/iter"
"github.com/anacrolix/missinggo/v2/bitmap"
"github.com/anacrolix/multiless"
"github.com/anacrolix/torrent/bencode"
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"
pp "github.com/anacrolix/torrent/peer_protocol"
request_strategy "github.com/anacrolix/torrent/request-strategy"
)
type PeerSource string
const (
PeerSourceTracker = "Tr"
PeerSourceIncoming = "I"
PeerSourceDhtGetPeers = "Hg" // Peers we found by searching a DHT.
PeerSourceDhtAnnouncePeer = "Ha" // Peers that were announced to us by a DHT.
PeerSourcePex = "X"
// The peer was given directly, such as through a magnet link.
PeerSourceDirect = "M"
)
type peerRequestState struct {
data []byte
}
type PeerRemoteAddr interface {
String() string
}
// Since we have to store all the requests in memory, we can't reasonably exceed what would be
// indexable with the memory space available.
type (
maxRequests = int
requestState = request_strategy.PeerRequestState
)
2021-01-20 10:10:32 +08:00
type Peer struct {
// First to ensure 64-bit alignment for atomics. See #262.
_stats ConnStats
t *Torrent
2020-05-30 13:18:28 +08:00
2020-06-02 15:41:59 +08:00
peerImpl
callbacks *Callbacks
2020-05-30 13:18:28 +08:00
outgoing bool
Network string
RemoteAddr PeerRemoteAddr
2022-01-21 19:28:15 +08:00
bannableAddr Option[bannableAddr]
// True if the connection is operating over MSE obfuscation.
headerEncrypted bool
2018-02-16 07:36:29 +08:00
cryptoMethod mse.CryptoMethod
Discovery PeerSource
trusted bool
2021-05-20 16:51:08 +08:00
closed chansync.SetOnce
// Set true after we've added our ConnStats generated during handshake to
// other ConnStat instances as determined when the *Torrent became known.
reconciledHandshakeStats bool
2014-08-28 07:32:49 +08:00
lastMessageReceived time.Time
completedHandshake time.Time
lastUsefulChunkReceived time.Time
2015-06-16 14:57:47 +08:00
lastChunkSent time.Time
2014-08-28 07:32:49 +08:00
// Stuff controlled by the local peer.
needRequestUpdate string
requestState requestState
updateRequestsTimer *time.Timer
lastRequestUpdate time.Time
peakRequests maxRequests
lastBecameInterested time.Time
priorInterest time.Duration
lastStartedExpectingToReceiveChunks time.Time
cumulativeExpectedToReceiveChunks time.Duration
_chunksReceivedWhileExpecting int64
choking bool
piecesReceivedSinceLastRequestUpdate maxRequests
maxPiecesReceivedBetweenRequestUpdates maxRequests
// Chunks that we might reasonably expect to receive from the peer. Due to latency, buffering,
// and implementation differences, we may receive chunks that are no longer in the set of
// requests actually want. This could use a roaring.BSI if the memory use becomes noticeable.
2021-09-19 13:16:37 +08:00
validReceiveChunks map[RequestIndex]int
// Indexed by metadata piece, set to true if posted and pending a
// response.
metadataRequests []bool
2018-02-04 16:10:25 +08:00
sentHaves bitmap.Bitmap
// Stuff controlled by the remote peer.
2020-04-01 04:14:43 +08:00
peerInterested bool
peerChoking bool
peerRequests map[Request]*peerRequestState
PeerPrefersEncryption bool // as indicated by 'e' field in extension handshake
PeerListenPort int
// The highest possible number of pieces the torrent could have based on
// communication with the peer. Generally only useful until we have the
// torrent info.
peerMinPieces pieceIndex
// Pieces we've accepted chunks for from the peer.
peerTouchedPieces map[pieceIndex]struct{}
peerAllowedFast roaring.Bitmap
2015-03-12 17:06:23 +08:00
PeerMaxRequests maxRequests // Maximum pending requests the peer allows.
PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber
PeerClientName atomic.Value
2020-05-30 08:14:20 +08:00
logger log.Logger
}
// 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
// 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
// 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
messageWriter peerConnMsgWriter
2021-05-20 16:51:08 +08:00
uploadTimer *time.Timer
pex pexConnState
// 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
}
func (cn *PeerConn) connStatusString() string {
2020-10-06 06:50:47 +08:00
return fmt.Sprintf("%+-55q %s %s", cn.PeerID, cn.PeerExtensionBytes, cn.connString)
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) updateExpectingChunks() {
if cn.expectingChunks() {
if cn.lastStartedExpectingToReceiveChunks.IsZero() {
cn.lastStartedExpectingToReceiveChunks = time.Now()
}
} else {
if !cn.lastStartedExpectingToReceiveChunks.IsZero() {
cn.cumulativeExpectedToReceiveChunks += time.Since(cn.lastStartedExpectingToReceiveChunks)
cn.lastStartedExpectingToReceiveChunks = time.Time{}
}
}
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) expectingChunks() bool {
if cn.requestState.Requests.IsEmpty() {
2021-05-09 22:53:32 +08:00
return false
}
if !cn.requestState.Interested {
2021-05-09 22:53:32 +08:00
return false
}
if !cn.peerChoking {
2021-09-19 13:16:37 +08:00
return true
2021-05-09 22:53:32 +08:00
}
haveAllowedFastRequests := false
cn.peerAllowedFast.Iterate(func(i uint32) bool {
haveAllowedFastRequests = roaringBitmapRangeCardinality(
&cn.requestState.Requests,
cn.t.pieceRequestIndexOffset(pieceIndex(i)),
cn.t.pieceRequestIndexOffset(pieceIndex(i+1)),
) == 0
return !haveAllowedFastRequests
})
return haveAllowedFastRequests
}
func (cn *Peer) remoteChokingPiece(piece pieceIndex) bool {
return cn.peerChoking && !cn.peerAllowedFast.Contains(bitmap.BitIndex(piece))
}
// Returns true if the connection is over IPv6.
func (cn *PeerConn) ipv6() bool {
ip := cn.remoteIp()
if ip.To4() != nil {
return false
}
return len(ip) == net.IPv6len
}
// Returns true the if the dialer/initiator has the lower client peer ID. TODO: Find the
// specification for this.
func (cn *PeerConn) isPreferredDirection() bool {
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.
func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) bool {
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())
return ml.Less()
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) cumInterest() time.Duration {
ret := cn.priorInterest
if cn.requestState.Interested {
ret += time.Since(cn.lastBecameInterested)
}
return ret
}
2021-12-18 05:58:56 +08:00
func (cn *PeerConn) peerHasAllPieces() (all, known bool) {
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
}
2021-10-21 07:28:57 +08:00
func (cn *Peer) locker() *lockWithDeferreds {
2018-07-25 11:41:50 +08:00
return cn.t.cl.locker()
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) supportsExtension(ext pp.ExtensionName) bool {
_, ok := cn.PeerExtensionIDs[ext]
return ok
}
// The best guess at number of pieces in the torrent for this peer.
2021-01-20 10:10:32 +08:00
func (cn *Peer) bestPeerNumPieces() pieceIndex {
if cn.t.haveInfo() {
return cn.t.numPieces()
}
return cn.peerMinPieces
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) completedString() string {
have := pieceIndex(cn.peerPieces().GetCardinality())
if all, _ := cn.peerHasAllPieces(); all {
have = cn.bestPeerNumPieces()
}
return fmt.Sprintf("%d/%d", have, cn.bestPeerNumPieces())
}
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)
cn.peerPiecesChanged()
}
func (cn *PeerConn) peerPieces() *roaring.Bitmap {
return &cn._peerPieces
}
func eventAgeString(t time.Time) string {
if t.IsZero() {
return "never"
}
2017-11-07 13:11:59 +08:00
return fmt.Sprintf("%.2fs ago", time.Since(t).Seconds())
}
func (cn *PeerConn) connectionFlags() (ret string) {
c := func(b byte) {
ret += string([]byte{b})
2014-12-09 14:26:06 +08:00
}
if cn.cryptoMethod == mse.CryptoMethodRC4 {
c('E')
} else if cn.headerEncrypted {
c('e')
}
ret += string(cn.Discovery)
if cn.utp() {
c('U')
}
return
}
func (cn *PeerConn) utp() bool {
return parseNetworkString(cn.Network).Udp
}
2018-06-09 20:08:24 +08:00
// Inspired by https://github.com/transmission/transmission/wiki/Peer-Status-Text.
2021-01-20 10:10:32 +08:00
func (cn *Peer) statusFlags() (ret string) {
c := func(b byte) {
ret += string([]byte{b})
}
if cn.requestState.Interested {
c('i')
}
if cn.choking {
c('c')
}
c('-')
ret += cn.connectionFlags()
c('-')
if cn.peerInterested {
c('i')
}
if cn.peerChoking {
c('c')
}
return
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) downloadRate() float64 {
2021-05-09 22:53:32 +08:00
num := cn._stats.BytesReadUsefulData.Int64()
if num == 0 {
return 0
}
return float64(num) / cn.totalExpectingTime().Seconds()
}
func (cn *Peer) numRequestsByPiece() (ret map[pieceIndex]int) {
ret = make(map[pieceIndex]int)
cn.requestState.Requests.Iterate(func(x uint32) bool {
2021-09-19 13:16:37 +08:00
ret[pieceIndex(x/cn.t.chunksPerRegularPiece())]++
return true
})
2021-05-09 22:53:32 +08:00
return
2015-06-29 17:37:52 +08:00
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) writeStatus(w io.Writer, t *Torrent) {
// \t isn't preserved in <pre> blocks?
2021-02-09 16:21:54 +08:00
if cn.closed.IsSet() {
fmt.Fprint(w, "CLOSED: ")
}
fmt.Fprintln(w, cn.connStatusString())
2021-05-09 22:53:32 +08:00
prio, err := cn.peerPriority()
prioStr := fmt.Sprintf("%08x", prio)
if err != nil {
prioStr += ": " + err.Error()
}
fmt.Fprintf(w, " bep40-prio: %v\n", prioStr)
fmt.Fprintf(w, " last msg: %s, connected: %s, last helpful: %s, itime: %s, etime: %s\n",
eventAgeString(cn.lastMessageReceived),
eventAgeString(cn.completedHandshake),
eventAgeString(cn.lastHelpful()),
cn.cumInterest(),
cn.totalExpectingTime(),
)
2015-05-15 06:39:53 +08:00
fmt.Fprintf(w,
" %s completed, %d pieces touched, good chunks: %v/%v:%v reqq: %d+%v/(%d/%d):%d/%d, flags: %s, dr: %.1f KiB/s\n",
cn.completedString(),
len(cn.peerTouchedPieces),
&cn._stats.ChunksReadUseful,
&cn._stats.ChunksRead,
&cn._stats.ChunksWritten,
cn.requestState.Requests.GetCardinality(),
cn.requestState.Cancelled.GetCardinality(),
cn.nominalMaxRequests(),
2021-05-09 22:53:32 +08:00
cn.PeerMaxRequests,
len(cn.peerRequests),
2021-05-09 22:53:32 +08:00
localClientReqq,
2015-05-15 06:39:53 +08:00
cn.statusFlags(),
cn.downloadRate()/(1<<10),
2015-05-15 06:39:53 +08:00
)
2021-05-09 22:53:32 +08:00
fmt.Fprintf(w, " requested pieces:")
type pieceNumRequestsType struct {
piece pieceIndex
numRequests int
}
var pieceNumRequests []pieceNumRequestsType
2021-05-09 22:53:32 +08:00
for piece, count := range cn.numRequestsByPiece() {
pieceNumRequests = append(pieceNumRequests, pieceNumRequestsType{piece, count})
}
sort.Slice(pieceNumRequests, func(i, j int) bool {
return pieceNumRequests[i].piece < pieceNumRequests[j].piece
})
for _, elem := range pieceNumRequests {
fmt.Fprintf(w, " %v(%v)", elem.piece, elem.numRequests)
2021-05-09 22:53:32 +08:00
}
fmt.Fprintf(w, "\n")
}
func (p *Peer) close() {
if !p.closed.Set() {
return
}
2021-10-21 07:28:57 +08:00
if p.updateRequestsTimer != nil {
p.updateRequestsTimer.Stop()
}
p.peerImpl.onClose()
if p.t != nil {
p.t.decPeerPieceAvailability(p)
}
for _, f := range p.callbacks.PeerClosed {
f(p)
}
2020-05-30 15:52:27 +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()
}
cn.tickleWriter()
if cn.conn != nil {
go cn.conn.Close()
}
if cb := cn.callbacks.PeerConnClosed; cb != nil {
2020-10-13 10:02:39 +08:00
cb(cn)
}
}
// Peer definitely has a piece, for purposes of requesting. So it's not sufficient that we think
// they do (known=true).
2021-01-20 10:10:32 +08:00
func (cn *Peer) peerHasPiece(piece pieceIndex) bool {
if all, known := cn.peerHasAllPieces(); all && known {
return true
}
return cn.peerPieces().ContainsInt(piece)
}
// 64KiB, but temporarily less to work around an issue with WebRTC. TODO: Update when
// https://github.com/pion/datachannel/issues/59 is fixed.
const (
writeBufferHighWaterLen = 1 << 15
writeBufferLowWaterLen = writeBufferHighWaterLen / 2
)
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
// 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)
cn.tickleWriter()
2021-05-20 16:51:08 +08:00
return notFull
}
func (cn *PeerConn) requestMetadataPiece(index int) {
eID := cn.PeerExtensionIDs[pp.ExtensionNameMetadata]
2021-06-18 13:04:07 +08:00
if eID == pp.ExtensionDeleteNumber {
return
}
if index < len(cn.metadataRequests) && cn.metadataRequests[index] {
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))
for index >= len(cn.metadataRequests) {
cn.metadataRequests = append(cn.metadataRequests, false)
}
cn.metadataRequests[index] = true
}
func (cn *PeerConn) requestedMetadataPiece(index int) bool {
return index < len(cn.metadataRequests) && cn.metadataRequests[index]
}
var (
interestedMsgLen = len(pp.Message{Type: pp.Interested}.MustMarshalBinary())
requestMsgLen = len(pp.Message{Type: pp.Request}.MustMarshalBinary())
// This is the maximum request count that could fit in the write buffer if it's at or below the
// low water mark when we run maybeUpdateActualRequestState.
maxLocalToRemoteRequests = (writeBufferHighWaterLen - writeBufferLowWaterLen - interestedMsgLen) / requestMsgLen
)
2016-02-08 13:09:12 +08:00
// The actual value to use as the maximum outbound requests.
func (cn *Peer) nominalMaxRequests() maxRequests {
return maxRequests(maxInt(1, minInt(cn.PeerMaxRequests, cn.peakRequests*2, maxLocalToRemoteRequests)))
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) totalExpectingTime() (ret time.Duration) {
ret = cn.cumulativeExpectedToReceiveChunks
if !cn.lastStartedExpectingToReceiveChunks.IsZero() {
ret += time.Since(cn.lastStartedExpectingToReceiveChunks)
}
return
}
func (cn *PeerConn) onPeerSentCancel(r Request) {
if _, ok := cn.peerRequests[r]; !ok {
torrent.Add("unexpected cancels received", 1)
return
}
if cn.fastEnabled() {
cn.reject(r)
} else {
delete(cn.peerRequests, r)
2014-04-16 15:33:33 +08:00
}
}
func (cn *PeerConn) choke(msg messageWriter) (more bool) {
if cn.choking {
return true
2014-05-21 15:49:28 +08:00
}
cn.choking = true
more = msg(pp.Message{
Type: pp.Choke,
})
if !cn.fastEnabled() {
cn.peerRequests = nil
}
return
2014-05-21 15:49:28 +08:00
}
func (cn *PeerConn) unchoke(msg func(pp.Message) bool) bool {
if !cn.choking {
return true
}
cn.choking = false
return msg(pp.Message{
Type: pp.Unchoke,
})
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) setInterested(interested bool) bool {
if cn.requestState.Interested == interested {
return true
}
cn.requestState.Interested = interested
if interested {
cn.lastBecameInterested = time.Now()
} else if !cn.lastBecameInterested.IsZero() {
cn.priorInterest += time.Since(cn.lastBecameInterested)
}
cn.updateExpectingChunks()
// log.Printf("%p: setting interest: %v", cn, interested)
2020-06-02 15:41:59 +08:00
return cn.writeInterested(interested)
2020-05-30 13:18:28 +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{
Type: func() pp.MessageType {
if interested {
return pp.Interested
} else {
return pp.NotInterested
}
}(),
})
}
// The function takes a message to be sent, and returns true if more messages
// are okay.
type messageWriter func(pp.Message) bool
// This function seems to only used by Peer.request. It's all logic checks, so maybe we can no-op it
// when we want to go fast.
2021-09-19 13:16:37 +08:00
func (cn *Peer) shouldRequest(r RequestIndex) error {
pi := pieceIndex(r / cn.t.chunksPerRegularPiece())
if cn.requestState.Cancelled.Contains(r) {
return errors.New("request is cancelled and waiting acknowledgement")
}
2021-09-19 13:16:37 +08:00
if !cn.peerHasPiece(pi) {
2021-05-09 21:38:38 +08:00
return errors.New("requesting piece peer doesn't have")
}
2020-05-30 13:18:28 +08:00
if !cn.t.peerIsActive(cn) {
panic("requesting but not in active conns")
}
if cn.closed.IsSet() {
panic("requesting when connection is closed")
}
2021-09-19 13:16:37 +08:00
if cn.t.hashingPiece(pi) {
panic("piece is being hashed")
}
2021-09-19 13:16:37 +08:00
if cn.t.pieceQueuedForHash(pi) {
panic("piece is queued for hash")
}
2021-09-19 13:16:37 +08:00
if cn.peerChoking && !cn.peerAllowedFast.Contains(bitmap.BitIndex(pi)) {
// This could occur if we made a request with the fast extension, and then got choked and
// haven't had the request rejected yet.
if !cn.requestState.Requests.Contains(r) {
panic("peer choking and piece not allowed fast")
}
}
return nil
}
2021-10-26 12:03:04 +08:00
func (cn *Peer) mustRequest(r RequestIndex) bool {
more, err := cn.request(r)
if err != nil {
panic(err)
}
return more
}
2021-09-19 13:16:37 +08:00
func (cn *Peer) request(r RequestIndex) (more bool, err error) {
if err := cn.shouldRequest(r); err != nil {
panic(err)
2021-05-09 21:38:38 +08:00
}
if cn.requestState.Requests.Contains(r) {
return true, nil
}
if maxRequests(cn.requestState.Requests.GetCardinality()) >= cn.nominalMaxRequests() {
return true, errors.New("too many outstanding requests")
2021-05-09 21:38:38 +08:00
}
cn.requestState.Requests.Add(r)
if cn.validReceiveChunks == nil {
2021-09-19 13:16:37 +08:00
cn.validReceiveChunks = make(map[RequestIndex]int)
}
cn.validReceiveChunks[r]++
2021-12-03 18:30:41 +08:00
cn.t.pendingRequests[r] = cn
2021-12-02 10:10:02 +08:00
cn.t.lastRequested[r] = time.Now()
cn.updateExpectingChunks()
2021-09-19 13:16:37 +08:00
ppReq := cn.t.requestIndexToRequest(r)
for _, f := range cn.callbacks.SentRequest {
2021-09-19 13:16:37 +08:00
f(PeerRequestEvent{cn, ppReq})
}
2021-09-19 13:16:37 +08:00
return cn.peerImpl._request(ppReq), nil
2020-05-30 13:18:28 +08:00
}
func (me *PeerConn) _request(r Request) bool {
return me.write(pp.Message{
Type: pp.Request,
Index: r.Index,
Begin: r.Begin,
Length: r.Length,
})
}
func (me *Peer) cancel(r RequestIndex) {
if !me.deleteRequest(r) {
panic("request not existing should have been guarded")
}
if me._cancel(r) {
if !me.requestState.Cancelled.CheckedAdd(r) {
panic("request already cancelled")
}
}
me.decPeakRequests()
if me.isLowOnRequests() {
me.updateRequests("Peer.cancel")
}
}
func (me *PeerConn) _cancel(r RequestIndex) bool {
me.write(makeCancelMessage(me.t.requestIndexToRequest(r)))
// Transmission does not send rejects for received cancels. See
// https://github.com/transmission/transmission/pull/2275.
return me.fastEnabled() && !me.remoteIsTransmission()
2020-05-30 13:18:28 +08:00
}
func (cn *PeerConn) fillWriteBuffer() {
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
}
2020-05-30 13:18:28 +08:00
cn.upload(cn.write)
}
func (cn *PeerConn) have(piece pieceIndex) {
if cn.sentHaves.Get(bitmap.BitIndex(piece)) {
return
}
2021-05-10 10:29:47 +08:00
cn.write(pp.Message{
Type: pp.Have,
Index: pp.Integer(piece),
})
cn.sentHaves.Add(bitmap.BitIndex(piece))
}
func (cn *PeerConn) postBitfield() {
2018-02-04 16:10:25 +08:00
if cn.sentHaves.Len() != 0 {
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{
Type: pp.Bitfield,
2018-02-04 16:10:25 +08:00
Bitfield: cn.t.bitfield(),
})
cn.sentHaves = bitmap.Bitmap{cn.t._completedPieces.Clone()}
}
2021-10-21 07:28:57 +08:00
// Sets a reason to update requests, and if there wasn't already one, handle it.
func (cn *Peer) updateRequests(reason string) {
if cn.needRequestUpdate != "" {
return
}
if reason != peerUpdateRequestsTimerReason && !cn.isLowOnRequests() {
return
}
cn.needRequestUpdate = reason
2021-10-21 07:28:57 +08:00
cn.handleUpdateRequests()
}
func (cn *PeerConn) handleUpdateRequests() {
// The writer determines the request state as needed when it can write.
cn.tickleWriter()
}
// Emits the indices in the Bitmaps bms in order, never repeating any index.
// skip is mutated during execution, and its initial values will never be
// emitted.
func iterBitmapsDistinct(skip *bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func {
return func(cb iter.Callback) {
for _, bm := range bms {
2020-01-10 07:27:05 +08:00
if !iter.All(
func(_i interface{}) bool {
i := _i.(int)
2021-05-20 09:16:54 +08:00
if skip.Contains(bitmap.BitIndex(i)) {
return true
}
2021-05-20 09:16:54 +08:00
skip.Add(bitmap.BitIndex(i))
2020-01-10 07:27:05 +08:00
return cb(i)
},
bm.Iter,
2020-01-10 07:27:05 +08:00
) {
return
}
}
}
}
func (cn *Peer) peerPiecesChanged() {
cn.t.maybeDropMutuallyCompletePeer(cn)
}
func (cn *PeerConn) raisePeerMinPieces(newMin pieceIndex) {
if newMin > cn.peerMinPieces {
cn.peerMinPieces = newMin
}
}
func (cn *PeerConn) peerSentHave(piece pieceIndex) error {
if cn.t.haveInfo() && piece >= cn.t.numPieces() || piece < 0 {
return errors.New("invalid piece")
}
if cn.peerHasPiece(piece) {
return nil
}
cn.raisePeerMinPieces(piece + 1)
if !cn.peerHasPiece(piece) {
cn.t.incPieceAvailability(piece)
}
cn._peerPieces.Add(uint32(piece))
if cn.t.wantPieceIndex(piece) {
cn.updateRequests("have")
}
cn.peerPiecesChanged()
return nil
}
func (cn *PeerConn) peerSentBitfield(bf []bool) error {
if len(bf)%8 != 0 {
panic("expected bitfield length divisible by 8")
}
// We know that the last byte means that at most the last 7 bits are wasted.
cn.raisePeerMinPieces(pieceIndex(len(bf) - 7))
if cn.t.haveInfo() && len(bf) > int(cn.t.numPieces()) {
// Ignore known excess pieces.
bf = bf[:cn.t.numPieces()]
}
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)
}
cn.peerSentHaveAll = false
// 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)
} else {
if !shouldUpdateRequests && cn.t.wantPieceIndex(pieceIndex(x)) {
shouldUpdateRequests = true
}
// We must be gaining this piece
cn.t.incPieceAvailability(pieceIndex(x))
2016-03-21 11:17:15 +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
}
// We didn't guard this before, I see no reason to do it now.
cn.peerPiecesChanged()
return nil
}
func (cn *PeerConn) onPeerHasAllPieces() {
t := cn.t
if t.haveInfo() {
cn._peerPieces.Iterate(func(x uint32) bool {
t.decPieceAvailability(pieceIndex(x))
return true
})
}
t.addConnWithAllPieces(&cn.Peer)
cn.peerSentHaveAll = true
cn._peerPieces.Clear()
if !cn.t._pendingPieces.IsEmpty() {
cn.updateRequests("Peer.onPeerHasAllPieces")
}
cn.peerPiecesChanged()
}
func (cn *PeerConn) onPeerSentHaveAll() error {
cn.onPeerHasAllPieces()
return nil
}
func (cn *PeerConn) peerSentHaveNone() error {
if cn.peerSentHaveAll {
cn.t.decPeerPieceAvailability(&cn.Peer)
}
cn._peerPieces.Clear()
cn.peerSentHaveAll = false
cn.peerPiecesChanged()
2016-03-21 11:17:15 +08:00
return nil
}
func (c *PeerConn) requestPendingMetadata() {
if c.t.haveInfo() {
return
}
if c.PeerExtensionIDs[pp.ExtensionNameMetadata] == 0 {
// 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-07-05 13:52:33 +08:00
func (cn *PeerConn) wroteMsg(msg *pp.Message) {
torrent.Add(fmt.Sprintf("messages written of type %s", msg.Type.String()), 1)
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)
}
}
cn.allStats(func(cs *ConnStats) { cs.wroteMsg(msg) })
2016-07-05 13:52:33 +08:00
}
// After handshake, we know what Torrent and Client stats to include for a
// connection.
2021-01-20 10:10:32 +08:00
func (cn *Peer) postHandshakeStats(f func(*ConnStats)) {
t := cn.t
2018-06-12 18:21:53 +08:00
f(&t.stats)
f(&t.cl.stats)
}
// All ConnStats that include this connection. Some objects are not known
// until the handshake is complete, after which it's expected to reconcile the
// differences.
2021-01-20 10:10:32 +08:00
func (cn *Peer) allStats(f func(*ConnStats)) {
f(&cn._stats)
if cn.reconciledHandshakeStats {
cn.postHandshakeStats(f)
2016-07-12 14:42:04 +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 }))
}
func (cn *Peer) readBytes(n int64) {
2018-06-12 18:21:53 +08:00
cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesRead }))
2016-07-05 13:52:33 +08:00
}
// Returns whether the connection could be useful to us. We're seeding and
// they want data, we don't have metainfo and they can provide it, etc.
2021-01-20 10:10:32 +08:00
func (c *Peer) useful() bool {
t := c.t
if c.closed.IsSet() {
return false
}
if !t.haveInfo() {
return c.supportsExtension("ut_metadata")
}
if t.seeding() && c.peerInterested {
return true
}
if c.peerHasWantedPieces() {
return true
}
return false
}
2016-07-05 22:42:16 +08:00
2021-01-20 10:10:32 +08:00
func (c *Peer) lastHelpful() (ret time.Time) {
ret = c.lastUsefulChunkReceived
if c.t.seeding() && c.lastChunkSent.After(ret) {
ret = c.lastChunkSent
2016-07-05 22:42:16 +08:00
}
return
2016-07-05 22:42:16 +08:00
}
func (c *PeerConn) fastEnabled() bool {
return c.PeerExtensionBytes.SupportsFast() && c.t.cl.config.Extensions.SupportsFast()
}
func (c *PeerConn) reject(r Request) {
if !c.fastEnabled() {
panic("fast not enabled")
}
2021-05-10 10:29:47 +08:00
c.write(r.ToMsg(pp.Reject))
delete(c.peerRequests, r)
}
func (c *PeerConn) onReadRequest(r Request) error {
2018-02-02 16:19:14 +08:00
requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1)
if _, ok := c.peerRequests[r]; ok {
torrent.Add("duplicate requests received", 1)
return nil
}
if c.choking {
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 {
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
}
if !c.t.havePiece(pieceIndex(r.Index)) {
2018-02-02 16:19:14 +08:00
// This isn't necessarily them screwing up. We can drop pieces
// from our storage, and can't communicate this to peers
// except by reconnecting.
requestsReceivedForMissingPieces.Add(1)
return fmt.Errorf("peer requested piece we don't have: %v", r.Index.Int())
}
// Check this after we know we have the piece, so that the piece length will be known.
if r.Begin+r.Length > c.t.pieceLength(pieceIndex(r.Index)) {
torrent.Add("bad requests received", 1)
return errors.New("bad Request")
}
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
}
value := &peerRequestState{}
c.peerRequests[r] = value
go c.peerRequestDataReader(r, value)
2018-02-02 16:19:14 +08:00
return nil
}
func (c *PeerConn) peerRequestDataReader(r Request, prs *peerRequestState) {
b, err := readPeerRequestData(r, c)
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)
prs.data = b
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.
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)
if c.t.closed.IsSet() {
return
}
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)
}
// 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)
}
}
func readPeerRequestData(r Request, c *PeerConn) ([]byte, error) {
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
}
func runSafeExtraneous(f func()) {
if true {
go f()
} else {
f()
}
}
func (c *PeerConn) logProtocolBehaviour(level log.Level, format string, arg ...interface{}) {
2022-01-23 06:37:11 +08:00
c.logger.WithContextText(fmt.Sprintf(
"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...)
}
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.
func (c *PeerConn) mainReadLoop() (err error) {
defer func() {
if err != nil {
torrent.Add("connection.mainReadLoop returned with error", 1)
} else {
torrent.Add("connection.mainReadLoop returned with no error", 1)
}
}()
t := c.t
cl := t.cl
2016-09-12 15:10:11 +08:00
decoder := pp.Decoder{
R: bufio.NewReaderSize(c.r, 1<<17),
MaxLength: 256 * 1024,
Pool: &t.chunkPool,
}
for {
var msg pp.Message
func() {
2018-07-25 11:41:50 +08:00
cl.unlock()
defer cl.lock()
err = decoder.Decode(&msg)
}()
if cb := c.callbacks.ReadMessage; cb != nil && err == nil {
2020-07-15 12:00:47 +08:00
cb(c, &msg)
}
if t.closed.IsSet() || c.closed.IsSet() {
return nil
}
if err != nil {
return err
}
c.lastMessageReceived = time.Now()
if msg.Keepalive {
receivedKeepalives.Add(1)
continue
}
messageTypesReceived.Add(msg.Type.String(), 1)
if msg.Type.FastExtension() && !c.fastEnabled() {
runSafeExtraneous(func() { torrent.Add("fast messages received when extension is disabled", 1) })
return fmt.Errorf("received fast extension message (type=%v) but extension is disabled", msg.Type)
}
switch msg.Type {
case pp.Choke:
if c.peerChoking {
break
}
if !c.fastEnabled() {
if !c.deleteAllRequests().IsEmpty() {
c.t.iterPeers(func(p *Peer) {
if p.isLowOnRequests() {
p.updateRequests("choked by non-fast PeerConn")
}
})
}
} else {
// We don't decrement pending requests here, let's wait for the peer to either
// reject or satisfy the outstanding requests. Additionally, some peers may unchoke
// us and resume where they left off, we don't want to have piled on to those chunks
// in the meanwhile. I think a peer's ability to abuse this should be limited: they
// 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.
}
c.peerChoking = true
c.updateExpectingChunks()
case pp.Unchoke:
if !c.peerChoking {
// 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")
break
}
c.peerChoking = false
preservedCount := 0
c.requestState.Requests.Iterate(func(x uint32) bool {
if !c.peerAllowedFast.Contains(x / c.t.chunksPerRegularPiece()) {
preservedCount++
}
return true
})
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
torrent.Add("requestsPreservedThroughChoking", int64(preservedCount))
}
if !c.t._pendingPieces.IsEmpty() {
c.updateRequests("unchoked")
}
c.updateExpectingChunks()
case pp.Interested:
c.peerInterested = true
c.tickleWriter()
case pp.NotInterested:
c.peerInterested = false
// 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.
case pp.Have:
err = c.peerSentHave(pieceIndex(msg.Index))
case pp.Bitfield:
err = c.peerSentBitfield(msg.Bitfield)
case pp.Request:
2018-02-02 16:19:14 +08:00
r := newRequestFromMessage(&msg)
err = c.onReadRequest(r)
case pp.Piece:
c.doChunkReadStats(int64(len(msg.Piece)))
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
}
case pp.Cancel:
req := newRequestFromMessage(&msg)
c.onPeerSentCancel(req)
case pp.Port:
2020-07-15 14:15:38 +08:00
ipa, ok := tryIpPortFromNetAddr(c.RemoteAddr)
if !ok {
break
}
pingAddr := net.UDPAddr{
IP: ipa.IP,
Port: ipa.Port,
}
if msg.Port != 0 {
pingAddr.Port = int(msg.Port)
}
cl.eachDhtServer(func(s DhtServer) {
go s.Ping(&pingAddr)
})
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)
c.updateRequests("suggested")
case pp.HaveAll:
err = c.onPeerSentHaveAll()
case pp.HaveNone:
err = c.peerSentHaveNone()
case pp.Reject:
req := newRequestFromMessage(&msg)
if !c.remoteRejectedRequest(c.t.requestIndexFromRequest(req)) {
log.Printf("received invalid reject [request=%v, peer=%v]", req, c)
err = fmt.Errorf("received invalid reject [request=%v]", req)
}
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)
c.updateRequests("PeerConn.mainReadLoop allowed fast")
case pp.Extended:
err = c.onReadExtendedMsg(msg.ExtendedID, msg.ExtendedPayload)
default:
err = fmt.Errorf("received unknown message type: %#v", msg.Type)
}
if err != nil {
return err
}
}
}
// Returns true if it was valid to reject the request.
func (c *Peer) remoteRejectedRequest(r RequestIndex) bool {
if c.deleteRequest(r) {
c.decPeakRequests()
} else if !c.requestState.Cancelled.CheckedRemove(r) {
return false
}
if c.isLowOnRequests() {
c.updateRequests("Peer.remoteRejectedRequest")
}
c.decExpectedChunkReceive(r)
return true
2020-06-02 14:41:49 +08:00
}
2021-09-19 13:16:37 +08:00
func (c *Peer) decExpectedChunkReceive(r RequestIndex) {
count := c.validReceiveChunks[r]
if count == 1 {
delete(c.validReceiveChunks, r)
} else if count > 1 {
c.validReceiveChunks[r] = count - 1
} else {
panic(r)
}
}
func (c *PeerConn) onReadExtendedMsg(id pp.ExtensionNumber, payload []byte) (err error) {
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:
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)
return fmt.Errorf("unmarshalling extended handshake payload: %w", err)
}
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))
if d.Reqq != 0 {
c.PeerMaxRequests = d.Reqq
}
c.PeerClientName.Store(d.V)
if c.PeerExtensionIDs == nil {
c.PeerExtensionIDs = make(map[pp.ExtensionName]pp.ExtensionNumber, len(d.M))
}
c.PeerListenPort = d.Port
c.PeerPrefersEncryption = d.Encryption
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)
}
c.PeerExtensionIDs[name] = id
}
if d.MetadataSize != 0 {
if err = t.setMetadataSize(d.MetadataSize); err != nil {
return fmt.Errorf("setting metadata size to %d: %w", d.MetadataSize, err)
}
}
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)
}
return nil
case metadataExtendedId:
err := cl.gotMetadataExtensionMsg(payload, t, c)
if err != nil {
return fmt.Errorf("handling metadata extension message: %w", err)
}
return nil
case pexExtendedId:
2020-04-09 00:03:29 +08:00
if !c.pex.IsEnabled() {
return nil // or hang-up maybe?
}
2020-04-09 00:03:29 +08:00
return c.pex.Recv(payload)
default:
return fmt.Errorf("unexpected extended message ID: %v", id)
}
}
// Set both the Reader and Writer for the connection from a single ReadWriter.
func (cn *PeerConn) setRW(rw io.ReadWriter) {
cn.r = rw
cn.w = rw
}
// Returns the Reader and Writer as a combined ReadWriter.
func (cn *PeerConn) rw() io.ReadWriter {
return struct {
io.Reader
io.Writer
}{cn.r, cn.w}
}
func (c *Peer) doChunkReadStats(size int64) {
c.allStats(func(cs *ConnStats) { cs.receivedChunk(size) })
}
// Handle a received chunk from a peer.
2021-01-20 10:10:32 +08:00
func (c *Peer) receiveChunk(msg *pp.Message) error {
chunksReceived.Add("total", 1)
2021-09-19 13:16:37 +08:00
ppReq := newRequestFromMessage(msg)
req := c.t.requestIndexFromRequest(ppReq)
2022-01-10 13:19:16 +08:00
t := c.t
if c.bannableAddr.Ok() {
t.smartBanCache.RecordBlock(c.bannableAddr.Value(), req, msg.Piece)
2022-01-10 13:19:16 +08:00
}
if c.peerChoking {
chunksReceived.Add("while choked", 1)
}
if c.validReceiveChunks[req] <= 0 {
chunksReceived.Add("unexpected", 1)
return errors.New("received unexpected chunk")
}
c.decExpectedChunkReceive(req)
if c.peerChoking && c.peerAllowedFast.Contains(bitmap.BitIndex(ppReq.Index)) {
chunksReceived.Add("due to allowed fast", 1)
}
2021-05-09 22:53:32 +08:00
// The request needs to be deleted immediately to prevent cancels occurring asynchronously when
// have actually already received the piece, while we have the Client unlocked to write the data
// out.
intended := false
{
if c.requestState.Requests.Contains(req) {
for _, f := range c.callbacks.ReceivedRequested {
f(PeerMessageEvent{c, msg})
}
}
// Request has been satisfied.
if c.deleteRequest(req) || c.requestState.Cancelled.CheckedRemove(req) {
intended = true
2021-05-09 22:53:32 +08:00
if !c.peerChoking {
c._chunksReceivedWhileExpecting++
}
if c.isLowOnRequests() {
c.updateRequests("Peer.receiveChunk deleted request")
}
} else {
chunksReceived.Add("unintended", 1)
}
}
cl := t.cl
// Do we actually want this chunk?
2021-09-19 13:16:37 +08:00
if t.haveChunk(ppReq) {
2021-11-08 11:47:01 +08:00
// panic(fmt.Sprintf("%+v", ppReq))
chunksReceived.Add("redundant", 1)
c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.ChunksReadWasted }))
return nil
}
2021-09-19 13:16:37 +08:00
piece := &t.pieces[ppReq.Index]
2018-06-12 18:21:53 +08:00
c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.ChunksReadUseful }))
c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulData }))
if intended {
c.piecesReceivedSinceLastRequestUpdate++
2021-05-09 22:53:32 +08:00
c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulIntendedData }))
}
2021-01-20 11:22:44 +08:00
for _, f := range c.t.cl.config.Callbacks.ReceivedUsefulData {
f(ReceivedUsefulDataEvent{c, msg})
}
c.lastUsefulChunkReceived = time.Now()
// Need to record that it hasn't been written yet, before we attempt to do
// anything with it.
piece.incrementPendingWrites()
2017-12-29 09:16:05 +08:00
// Record that we have the chunk, so we aren't trying to download it while
// waiting for it to be written to storage.
2021-09-19 13:16:37 +08:00
piece.unpendChunkIndex(chunkIndexFromChunkSpec(ppReq.ChunkSpec, t.chunkSize))
// Cancel pending requests for this chunk from *other* peers.
if p := t.pendingRequests[req]; p != nil {
if p == c {
panic("should not be pending request from conn that just received it")
}
2021-05-09 21:38:38 +08:00
p.cancel(req)
}
err := func() error {
2018-07-25 11:41:50 +08:00
cl.unlock()
defer cl.lock()
2019-06-13 10:18:08 +08:00
concurrentChunkWrites.Add(1)
defer concurrentChunkWrites.Add(-1)
2020-02-22 16:38:56 +08:00
// Write the chunk out. Note that the upper bound on chunk writing concurrency will be the
// number of connections. We write inline with receiving the chunk (with this lock dance),
// because we want to handle errors synchronously and I haven't thought of a nice way to
// defer any concurrency to the storage and have that notify the client of errors. TODO: Do
// that instead.
return t.writeChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
}()
piece.decrementPendingWrites()
if err != nil {
2020-10-10 07:24:58 +08:00
c.logger.WithDefaultLevel(log.Error).Printf("writing received chunk %v: %v", req, err)
t.pendRequest(req)
// Necessary to pass TestReceiveChunkStorageFailureSeederFastExtensionDisabled. I think a
// request update runs while we're writing the chunk that just failed. Then we never do a
// fresh update after pending the failed request.
c.updateRequests("Peer.receiveChunk error writing chunk")
t.onWriteChunkErr(err)
return nil
}
2021-09-19 13:16:37 +08:00
c.onDirtiedPiece(pieceIndex(ppReq.Index))
// We need to ensure the piece is only queued once, so only the last chunk writer gets this job.
2021-09-19 13:16:37 +08:00
if t.pieceAllDirty(pieceIndex(ppReq.Index)) && piece.pendingWrites == 0 {
t.queuePieceCheck(pieceIndex(ppReq.Index))
// We don't pend all chunks here anymore because we don't want code dependent on the dirty
// chunk status (such as the haveChunk call above) to have to check all the various other
// piece states like queued for hash, hashing etc. This does mean that we need to be sure
// that chunk pieces are pended at an appropriate time later however.
}
cl.event.Broadcast()
2020-01-23 10:51:14 +08:00
// We do this because we've written a chunk, and may change PieceState.Partial.
2021-09-19 13:16:37 +08:00
t.publishPieceChange(pieceIndex(ppReq.Index))
return nil
}
2021-01-20 10:10:32 +08:00
func (c *Peer) onDirtiedPiece(piece pieceIndex) {
2018-02-03 09:08:16 +08:00
if c.peerTouchedPieces == nil {
c.peerTouchedPieces = make(map[pieceIndex]struct{})
2018-02-03 09:08:16 +08:00
}
c.peerTouchedPieces[piece] = struct{}{}
ds := &c.t.pieces[piece].dirtiers
if *ds == nil {
2021-01-20 10:10:32 +08:00
*ds = make(map[*Peer]struct{})
2018-02-03 09:08:16 +08:00
}
(*ds)[c] = struct{}{}
}
func (c *PeerConn) uploadAllowed() bool {
if c.t.cl.config.NoUpload {
return false
}
if c.t.dataUploadDisallowed {
return false
}
if c.t.seeding() {
return true
}
if !c.peerHasWantedPieces() {
return false
}
// Don't upload more than 100 KiB more than we download.
if c._stats.BytesWrittenData.Int64() >= c._stats.BytesReadData.Int64()+100<<10 {
return false
}
return true
}
func (c *PeerConn) setRetryUploadTimer(delay time.Duration) {
if c.uploadTimer == nil {
2021-05-20 16:51:08 +08:00
c.uploadTimer = time.AfterFunc(delay, c.tickleWriter)
} else {
c.uploadTimer.Reset(delay)
}
}
// Also handles choking and unchoking of the remote peer.
func (c *PeerConn) upload(msg func(pp.Message) bool) bool {
// Breaking or completing this loop means we don't want to upload to the
// peer anymore, and we choke them.
another:
for c.uploadAllowed() {
// We want to upload to the peer.
if !c.unchoke(msg) {
return false
}
for r, state := range c.peerRequests {
if state.data == nil {
continue
}
res := c.t.cl.config.UploadRateLimiter.ReserveN(time.Now(), int(r.Length))
if !res.OK() {
panic(fmt.Sprintf("upload rate limiter burst size < %d", r.Length))
}
delay := res.Delay()
if delay > 0 {
res.Cancel()
c.setRetryUploadTimer(delay)
// Hard to say what to return here.
return true
}
more := c.sendChunk(r, msg, state)
delete(c.peerRequests, r)
if !more {
return false
}
goto another
}
return true
}
return c.choke(msg)
}
func (cn *PeerConn) drop() {
cn.t.dropConnection(cn)
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) netGoodPiecesDirtied() int64 {
return cn._stats.PiecesDirtiedGood.Int64() - cn._stats.PiecesDirtiedBad.Int64()
}
2021-01-20 10:10:32 +08:00
func (c *Peer) peerHasWantedPieces() bool {
if all, _ := c.peerHasAllPieces(); all {
return !c.t.haveAllPieces() && !c.t._pendingPieces.IsEmpty()
2021-10-11 12:46:40 +08:00
}
if !c.t.haveInfo() {
return !c.peerPieces().IsEmpty()
2021-10-11 12:46:40 +08:00
}
return c.peerPieces().Intersects(&c.t._pendingPieces)
}
// Returns true if an outstanding request is removed. Cancelled requests should be handled
// separately.
2021-09-19 13:16:37 +08:00
func (c *Peer) deleteRequest(r RequestIndex) bool {
if !c.requestState.Requests.CheckedRemove(r) {
return false
}
for _, f := range c.callbacks.DeletedRequest {
2021-09-19 13:16:37 +08:00
f(PeerRequestEvent{c, c.t.requestIndexToRequest(r)})
}
c.updateExpectingChunks()
if c.t.requestingPeer(r) != c {
panic("only one peer should have a given request at a time")
}
delete(c.t.pendingRequests, r)
delete(c.t.lastRequested, r)
// c.t.iterPeers(func(p *Peer) {
// if p.isLowOnRequests() {
// p.updateRequests("Peer.deleteRequest")
// }
// })
return true
}
2017-11-04 14:08:02 +08:00
func (c *Peer) deleteAllRequests() (deleted *roaring.Bitmap) {
deleted = c.requestState.Requests.Clone()
deleted.Iterate(func(x uint32) bool {
if !c.deleteRequest(x) {
panic("request should exist")
}
2021-09-19 13:16:37 +08:00
return true
})
c.assertNoRequests()
return
}
func (c *Peer) assertNoRequests() {
if !c.requestState.Requests.IsEmpty() {
panic(c.requestState.Requests.GetCardinality())
}
}
func (c *Peer) cancelAllRequests() (cancelled *roaring.Bitmap) {
cancelled = c.requestState.Requests.Clone()
cancelled.Iterate(func(x uint32) bool {
c.cancel(x)
return true
})
c.assertNoRequests()
return
}
// 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.
func (c *PeerConn) tickleWriter() {
2021-05-20 16:51:08 +08:00
c.messageWriter.writeCond.Broadcast()
}
func (c *PeerConn) sendChunk(r Request, msg func(pp.Message) bool, state *peerRequestState) (more bool) {
c.lastChunkSent = time.Now()
return msg(pp.Message{
2018-02-02 10:26:04 +08:00
Type: pp.Piece,
Index: r.Index,
Begin: r.Begin,
Piece: state.data,
2018-02-02 10:26:04 +08:00
})
}
func (c *PeerConn) setTorrent(t *Torrent) {
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)
t.reconcileHandshakeStats(c)
}
2021-01-20 10:10:32 +08:00
func (c *Peer) peerPriority() (peerPriority, error) {
return bep40Priority(c.remoteIpPort(), c.t.cl.publicAddr(c.remoteIp()))
}
2021-01-20 10:10:32 +08:00
func (c *Peer) remoteIp() net.IP {
host, _, _ := net.SplitHostPort(c.RemoteAddr.String())
return net.ParseIP(host)
}
2021-01-20 10:10:32 +08:00
func (c *Peer) remoteIpPort() IpPort {
2020-07-15 14:15:38 +08:00
ipa, _ := tryIpPortFromNetAddr(c.RemoteAddr)
return IpPort{ipa.IP, uint16(ipa.Port)}
}
2019-10-01 19:16:18 +08:00
func (c *PeerConn) pexPeerFlags() pp.PexPeerFlags {
f := pp.PexPeerFlags(0)
if c.PeerPrefersEncryption {
f |= pp.PexPrefersEncryption
}
if c.outgoing {
f |= pp.PexOutgoingConn
}
if c.utp() {
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.
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
}
func (c *PeerConn) String() string {
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
}
2021-01-20 10:10:32 +08:00
func (c *Peer) trust() connectionTrust {
return connectionTrust{c.trusted, c.netGoodPiecesDirtied()}
}
type connectionTrust struct {
Implicit bool
NetGoodPiecesDirted int64
}
func (l connectionTrust) Less(r connectionTrust) bool {
return multiless.New().Bool(l.Implicit, r.Implicit).Int64(l.NetGoodPiecesDirted, r.NetGoodPiecesDirted).Less()
}
// 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.
func (cn *PeerConn) PeerPieces() *roaring.Bitmap {
2020-02-21 08:51:24 +08:00
cn.locker().RLock()
defer cn.locker().RUnlock()
return cn.newPeerPieces()
2020-02-21 08:51:24 +08:00
}
// Returns a new Bitmap that includes bits for all pieces the peer could have based on their claims.
func (cn *Peer) newPeerPieces() *roaring.Bitmap {
// TODO: Can we use copy on write?
ret := cn.peerPieces().Clone()
if all, _ := cn.peerHasAllPieces(); all {
if cn.t.haveInfo() {
ret.AddRange(0, bitmap.BitRange(cn.t.numPieces()))
} else {
ret.AddRange(0, bitmap.ToEnd)
}
}
return ret
}
2021-01-20 10:10:32 +08:00
func (cn *Peer) stats() *ConnStats {
return &cn._stats
}
2021-01-28 13:31:13 +08:00
func (p *Peer) TryAsPeerConn() (*PeerConn, bool) {
pc, ok := p.peerImpl.(*PeerConn)
return pc, ok
}
func (p *Peer) uncancelledRequests() uint64 {
return p.requestState.Requests.GetCardinality()
}
func (pc *PeerConn) remoteIsTransmission() bool {
return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-'
}