File indexing completed on 2025-01-05 04:37:17

0001 /*
0002     SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef BT_SERVERINTERFACE_H
0008 #define BT_SERVERINTERFACE_H
0009 
0010 #include <QObject>
0011 #include <QStringList>
0012 #include <ktorrent_export.h>
0013 #include <mse/encryptedpacketsocket.h>
0014 #include <util/constants.h>
0015 
0016 namespace bt
0017 {
0018 class SHA1Hash;
0019 class PeerManager;
0020 
0021 /**
0022     Base class for all servers which accept connections.
0023 */
0024 class KTORRENT_EXPORT ServerInterface : public QObject
0025 {
0026     Q_OBJECT
0027 public:
0028     ServerInterface(QObject *parent = nullptr);
0029     ~ServerInterface() override;
0030 
0031     /**
0032      * Change the port.
0033      * @param port The new port
0034      */
0035     virtual bool changePort(Uint16 port) = 0;
0036 
0037     /// Set the port to use
0038     static void setPort(Uint16 p)
0039     {
0040         port = p;
0041     }
0042 
0043     /// Get the port in use
0044     static Uint16 getPort()
0045     {
0046         return port;
0047     }
0048 
0049     /**
0050      * Add a PeerManager.
0051      * @param pman The PeerManager
0052      */
0053     static void addPeerManager(PeerManager *pman);
0054 
0055     /**
0056      * Remove a PeerManager.
0057      * @param pman The PeerManager
0058      */
0059     static void removePeerManager(PeerManager *pman);
0060 
0061     /**
0062      * Find the PeerManager given the info_hash of it's torrent.
0063      * @param hash The info_hash
0064      * @return The PeerManager or 0 if one can't be found
0065      */
0066     static PeerManager *findPeerManager(const SHA1Hash &hash);
0067 
0068     /**
0069      * Find the info_hash based on the skey hash. The skey hash is a hash
0070      * of 'req2' followed by the info_hash. This function finds the info_hash
0071      * which matches the skey hash.
0072      * @param skey HASH('req2',info_hash)
0073      * @param info_hash which matches
0074      * @return true If one was found
0075      */
0076     static bool findInfoHash(const SHA1Hash &skey, SHA1Hash &info_hash);
0077 
0078     /**
0079      * Enable encryption.
0080      * @param allow_unencrypted Allow unencrypted connections (if encryption fails)
0081      */
0082     static void enableEncryption(bool allow_unencrypted);
0083 
0084     /**
0085      * Disable encrypted authentication.
0086      */
0087     static void disableEncryption();
0088 
0089     static bool isEncryptionEnabled()
0090     {
0091         return encryption;
0092     }
0093     static bool unencryptedConnectionsAllowed()
0094     {
0095         return allow_unencrypted;
0096     }
0097 
0098     /**
0099         Get a list of potential IP addresses to bind to
0100     */
0101     static QStringList bindAddresses();
0102 
0103     static void setUtpEnabled(bool on, bool only_use_utp);
0104     static bool isUtpEnabled()
0105     {
0106         return utp_enabled;
0107     }
0108     static bool onlyUseUtp()
0109     {
0110         return only_use_utp;
0111     }
0112     static void setPrimaryTransportProtocol(TransportProtocol proto);
0113     static TransportProtocol primaryTransportProtocol()
0114     {
0115         return primary_transport_protocol;
0116     }
0117 
0118 protected:
0119     void newConnection(mse::EncryptedPacketSocket::Ptr sock);
0120 
0121 protected:
0122     static Uint16 port;
0123     static QList<PeerManager *> peer_managers;
0124     static bool encryption;
0125     static bool allow_unencrypted;
0126     static bool utp_enabled;
0127     static bool only_use_utp;
0128     static TransportProtocol primary_transport_protocol;
0129 };
0130 
0131 }
0132 
0133 #endif // BT_SERVERINTERFACE_H