File indexing completed on 2025-01-05 04:37:18
0001 /* 0002 SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #ifndef MSESTREAMSOCKET_H 0007 #define MSESTREAMSOCKET_H 0008 0009 #include <ktorrent_export.h> 0010 #include <net/packetsocket.h> 0011 #include <util/constants.h> 0012 0013 class QString; 0014 0015 using bt::Uint16; 0016 using bt::Uint32; 0017 using bt::Uint8; 0018 0019 namespace bt 0020 { 0021 class SHA1Hash; 0022 } 0023 0024 namespace mse 0025 { 0026 class RC4Encryptor; 0027 0028 /** 0029 * @author Joris Guisson <joris.guisson@gmail.com> 0030 * 0031 * Wrapper around a TCP socket which handles RC4 encryption. 0032 */ 0033 class KTORRENT_EXPORT EncryptedPacketSocket : public net::PacketSocket 0034 { 0035 public: 0036 EncryptedPacketSocket(int ip_version); 0037 EncryptedPacketSocket(int fd, int ip_version); 0038 EncryptedPacketSocket(net::SocketDevice *sock); 0039 ~EncryptedPacketSocket() override; 0040 0041 /** 0042 * Send a chunk of data. (Does not encrypt the data) 0043 * @param data The data 0044 * @param len The length 0045 * @return Number of bytes written 0046 */ 0047 Uint32 sendData(const Uint8 *data, Uint32 len); 0048 0049 /** 0050 * Reads data from the peer. 0051 * @param buf The buffer to store the data 0052 * @param len The maximum number of bytes to read 0053 * @return The number of bytes read 0054 */ 0055 Uint32 readData(Uint8 *buf, Uint32 len); 0056 0057 /// Get the number of bytes available to read. 0058 Uint32 bytesAvailable() const; 0059 0060 /// Are we using encryption 0061 bool encrypted() const 0062 { 0063 return enc != nullptr; 0064 } 0065 0066 /** 0067 * Initialize the RC4 encryption algorithm. 0068 * @param dkey 0069 * @param ekey 0070 */ 0071 void initCrypt(const bt::SHA1Hash &dkey, const bt::SHA1Hash &ekey); 0072 0073 /// Set the encryptor 0074 void setRC4Encryptor(RC4Encryptor *enc); 0075 0076 /// Disables encryption. All data will be sent over as plain text. 0077 void disableCrypt(); 0078 0079 /// Close the socket 0080 void close(); 0081 0082 /// Connect the socket to a remote host 0083 bool connectTo(const QString &ip, Uint16 port); 0084 0085 /// Connect the socket to a remote host 0086 bool connectTo(const net::Address &addr); 0087 0088 /// Get the IP address of the remote peer 0089 QString getRemoteIPAddress() const; 0090 0091 /// Get the port of the remote peer 0092 bt::Uint16 getRemotePort() const; 0093 0094 /// Get the full address 0095 net::Address getRemoteAddress() const; 0096 0097 /** 0098 * Reinsert data, this is needed when we read to much during the crypto handshake. 0099 * This data will be the first to read out. The data will be copied to a temporary buffer 0100 * which will be destroyed when the reinserted data has been read. 0101 */ 0102 void reinsert(const Uint8 *d, Uint32 size); 0103 0104 /// see if the socket is still OK 0105 bool ok() const; 0106 0107 /// Start monitoring of this socket by the monitor thread 0108 void startMonitoring(net::SocketReader *rdr); 0109 0110 /// Stop monitoring this socket 0111 void stopMonitoring(); 0112 0113 /// Is this socket connecting to a remote host 0114 bool connecting() const; 0115 0116 /// See if a connect was success full 0117 bool connectSuccesFull() const; 0118 0119 /** 0120 * Set the TOS byte for new sockets. 0121 * @param t TOS value 0122 */ 0123 static void setTOS(Uint8 t) 0124 { 0125 tos = t; 0126 } 0127 0128 /** 0129 * Set the remote address of the socket. Used by Socks to set the actual 0130 * address of the connection. 0131 * @param addr The address 0132 */ 0133 void setRemoteAddress(const net::Address &addr); 0134 0135 typedef QSharedPointer<EncryptedPacketSocket> Ptr; 0136 0137 private: 0138 void preProcess(bt::Packet::Ptr packet) override; 0139 void postProcess(Uint8 *data, Uint32 size) override; 0140 0141 private: 0142 RC4Encryptor *enc; 0143 Uint8 *reinserted_data; 0144 Uint32 reinserted_data_size; 0145 Uint32 reinserted_data_read; 0146 bool monitored; 0147 0148 static Uint8 tos; 0149 }; 0150 0151 } 0152 0153 #endif