File indexing completed on 2025-01-05 04:37:22
0001 /* 0002 SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #ifndef BTPEER_H 0007 #define BTPEER_H 0008 0009 #include "connectionlimit.h" 0010 #include "peerid.h" 0011 #include "peerprotocolextension.h" 0012 #include <QDateTime> 0013 #include <QObject> 0014 #include <interfaces/peerinterface.h> 0015 #include <ktorrent_export.h> 0016 #include <mse/encryptedpacketsocket.h> 0017 #include <util/ptrmap.h> 0018 #include <util/timer.h> 0019 0020 namespace net 0021 { 0022 class Address; 0023 } 0024 0025 namespace bt 0026 { 0027 class Peer; 0028 class Piece; 0029 class PacketReader; 0030 class PeerDownloader; 0031 class PeerUploader; 0032 class PeerManager; 0033 class BitSet; 0034 0035 /** 0036 * @author Joris Guisson 0037 * @brief Manages the connection with a peer 0038 * 0039 * This class manages a connection with a peer in the P2P network. 0040 * It provides functions for sending packets. Packets it receives 0041 * get relayed to the outside world using a bunch of signals. 0042 */ 0043 class KTORRENT_EXPORT Peer : public QObject, public PeerInterface 0044 { 0045 Q_OBJECT 0046 public: 0047 /** 0048 * Constructor, set the socket. 0049 * The socket is already opened. 0050 * @param sock The socket 0051 * @param peer_id The Peer's BitTorrent ID 0052 * @param num_chunks The number of chunks in the file 0053 * @param chunk_size Size of each chunk 0054 * @param support Which extensions the peer supports 0055 * @param local Whether or not it is a local peer 0056 * @param token ConnectionLimit token 0057 * @param pman The PeerManager 0058 */ 0059 Peer(mse::EncryptedPacketSocket::Ptr sock, 0060 const PeerID &peer_id, 0061 Uint32 num_chunks, 0062 Uint32 chunk_size, 0063 Uint32 support, 0064 bool local, 0065 ConnectionLimit::Token::Ptr token, 0066 PeerManager *pman); 0067 0068 ~Peer() override; 0069 0070 /// Get the peer's unique ID. 0071 Uint32 getID() const 0072 { 0073 return id; 0074 } 0075 0076 /// Get the IP address of the Peer. 0077 QString getIPAddresss() const; 0078 0079 /// Get the port of the Peer 0080 Uint16 getPort() const; 0081 0082 /// Get the address of the peer 0083 net::Address getAddress() const; 0084 0085 /// See if the peer is stalled. 0086 bool isStalled() const; 0087 0088 /// Are we being snubbed by the Peer 0089 bool isSnubbed() const; 0090 0091 /// Get the upload rate in bytes per sec 0092 Uint32 getUploadRate() const; 0093 0094 /// Get the download rate in bytes per sec 0095 Uint32 getDownloadRate() const; 0096 0097 /// Update the up- and down- speed and handle incoming packets 0098 void update(); 0099 0100 /// Pause the peer connection 0101 void pause(); 0102 0103 /// Unpause the peer connection 0104 void unpause(); 0105 0106 /// Get the PeerDownloader. 0107 PeerDownloader *getPeerDownloader() const 0108 { 0109 return downloader; 0110 } 0111 0112 /// Get the PeerUploader. 0113 PeerUploader *getPeerUploader() const 0114 { 0115 return uploader; 0116 } 0117 0118 /// Get the PeerManager 0119 PeerManager *getPeerManager() 0120 { 0121 return pman; 0122 } 0123 0124 /** 0125 * Send a chunk of data. 0126 * @param data The data 0127 * @param len The length 0128 * @param proto Indicates whether the packed is data or a protocol message 0129 * @return Number of bytes written 0130 */ 0131 Uint32 sendData(const Uint8 *data, Uint32 len); 0132 0133 /** 0134 * Reads data from the peer. 0135 * @param buf The buffer to store the data 0136 * @param len The maximum number of bytes to read 0137 * @return The number of bytes read 0138 */ 0139 Uint32 readData(Uint8 *buf, Uint32 len); 0140 0141 /// Get the number of bytes available to read. 0142 Uint32 bytesAvailable() const; 0143 0144 /** 0145 * Close the peers connection. 0146 */ 0147 void closeConnection(); 0148 0149 /** 0150 * Kill the Peer. 0151 */ 0152 void kill() override; 0153 0154 /// Get the time in milliseconds since the last time a piece was received. 0155 Uint32 getTimeSinceLastPiece() const; 0156 0157 /// Get the time the peer connection was established. 0158 const QTime &getConnectTime() const 0159 { 0160 return connect_time; 0161 } 0162 0163 /** 0164 * Get the percentual amount of data available from peer. 0165 */ 0166 float percentAvailable() const; 0167 0168 /// Set the ACA score 0169 void setACAScore(double s); 0170 0171 bt::Uint32 averageDownloadSpeed() const override; 0172 0173 /// Choke the peer 0174 void choke(); 0175 0176 /** 0177 * Emit the port packet signal. 0178 */ 0179 void emitPortPacket(); 0180 0181 /** 0182 * Emit the pex signal 0183 */ 0184 void emitPex(const QByteArray &data); 0185 0186 /// Disable or enable pex 0187 void setPexEnabled(bool on); 0188 0189 /** 0190 * Emit the metadataDownloaded signal 0191 */ 0192 void emitMetadataDownloaded(const QByteArray &data); 0193 0194 /// Send an extended protocol handshake 0195 void sendExtProtHandshake(Uint16 port, Uint32 metadata_size, bool partial_seed); 0196 0197 /** 0198 * Set the peer's group IDs for traffic 0199 * @param up_gid The upload gid 0200 * @param down_gid The download gid 0201 */ 0202 void setGroupIDs(Uint32 up_gid, Uint32 down_gid); 0203 0204 /// Enable or disable hostname resolving 0205 static void setResolveHostnames(bool on); 0206 0207 /// Check if the peer has wanted chunks 0208 bool hasWantedChunks(const BitSet &wanted_chunks) const; 0209 0210 /** 0211 * Send a choke packet. 0212 */ 0213 void sendChoke(); 0214 0215 /** 0216 * Send an unchoke packet. 0217 */ 0218 void sendUnchoke(); 0219 0220 /** 0221 * Sends an unchoke message but doesn't update the am_choked field so KT still thinks 0222 * it is choked (and will not upload to it), this is to punish snubbers. 0223 */ 0224 void sendEvilUnchoke(); 0225 0226 /** 0227 * Send an interested packet. 0228 */ 0229 void sendInterested(); 0230 0231 /** 0232 * Send a not interested packet. 0233 */ 0234 void sendNotInterested(); 0235 0236 /** 0237 * Send a request for data. 0238 * @param req The Request 0239 */ 0240 void sendRequest(const Request &r); 0241 0242 /** 0243 * Cancel a request. 0244 * @param req The Request 0245 */ 0246 void sendCancel(const Request &r); 0247 0248 /** 0249 * Send a reject for a request 0250 * @param req The Request 0251 */ 0252 void sendReject(const Request &r); 0253 0254 /** 0255 * Send a have packet. 0256 * @param index 0257 */ 0258 void sendHave(Uint32 index); 0259 0260 /** 0261 * Send an allowed fast packet 0262 * @param index 0263 */ 0264 void sendAllowedFast(Uint32 index); 0265 0266 /** 0267 * Send a chunk of data. 0268 * @param index Index of chunk 0269 * @param begin Offset into chunk 0270 * @param len Length of data 0271 * @param ch The Chunk 0272 * @return true If we satisfy the request, false otherwise 0273 */ 0274 bool sendChunk(Uint32 index, Uint32 begin, Uint32 len, Chunk *ch); 0275 0276 /** 0277 * Send a BitSet. The BitSet indicates which chunks we have. 0278 * @param bs The BitSet 0279 */ 0280 void sendBitSet(const BitSet &bs); 0281 0282 /** 0283 * Send a port message 0284 * @param port The port 0285 */ 0286 void sendPort(Uint16 port); 0287 0288 /// Send a have all message 0289 void sendHaveAll(); 0290 0291 /// Send a have none message 0292 void sendHaveNone(); 0293 0294 /** 0295 * Send a suggest piece packet 0296 * @param index Index of the chunk 0297 */ 0298 void sendSuggestPiece(Uint32 index); 0299 0300 /// Send an extended protocol message 0301 void sendExtProtMsg(Uint8 id, const QByteArray &data); 0302 0303 /** 0304 * Clear all pending piece uploads we are not in the progress of sending. 0305 */ 0306 void clearPendingPieceUploads(); 0307 0308 void chunkAllowed(Uint32 chunk) override; 0309 void handlePacket(const bt::Uint8 *packet, bt::Uint32 size) override; 0310 0311 typedef QSharedPointer<Peer> Ptr; 0312 typedef QWeakPointer<Peer> WPtr; 0313 0314 private Q_SLOTS: 0315 void resolved(const QString &hinfo); 0316 0317 private: 0318 void handleChoke(Uint32 len); 0319 void handleUnchoke(Uint32 len); 0320 void handleInterested(Uint32 len); 0321 void handleNotInterested(Uint32 len); 0322 void handleHave(const Uint8 *packet, Uint32 len); 0323 void handleHaveAll(Uint32 len); 0324 void handleHaveNone(Uint32 len); 0325 void handleBitField(const Uint8 *packet, Uint32 len); 0326 void handleRequest(const Uint8 *packet, Uint32 len); 0327 void handlePiece(const Uint8 *packet, Uint32 len); 0328 void handleCancel(const Uint8 *packet, Uint32 len); 0329 void handleReject(const Uint8 *packet, Uint32 len); 0330 void handlePort(const Uint8 *packet, Uint32 len); 0331 void handleExtendedPacket(const Uint8 *packet, Uint32 size); 0332 void handleExtendedHandshake(const Uint8 *packet, Uint32 size); 0333 0334 Q_SIGNALS: 0335 /** 0336 * Emitted when metadata has been downloaded from the Peer 0337 */ 0338 void metadataDownloaded(const QByteArray &data); 0339 0340 private: 0341 mse::EncryptedPacketSocket::Ptr sock; 0342 ConnectionLimit::Token::Ptr token; 0343 0344 Timer stalled_timer; 0345 0346 Uint32 id; 0347 0348 Timer snub_timer; 0349 PacketReader *preader; 0350 PeerDownloader *downloader; 0351 PeerUploader *uploader; 0352 0353 QTime connect_time; 0354 bool pex_allowed; 0355 PeerManager *pman; 0356 PtrMap<Uint32, PeerProtocolExtension> extensions; 0357 Uint32 ut_pex_id; 0358 0359 Uint64 bytes_downloaded_since_unchoke; 0360 0361 static bool resolve_hostname; 0362 0363 friend class PeerDownloader; 0364 }; 0365 } 0366 0367 #endif