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

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef DHTDHT_H
0007 #define DHTDHT_H
0008 
0009 #include "dhtbase.h"
0010 #include "key.h"
0011 #include "rpcmsg.h"
0012 #include <qmap.h>
0013 #include <qstring.h>
0014 #include <qtimer.h>
0015 #include <util/constants.h>
0016 #include <util/timer.h>
0017 
0018 namespace net
0019 {
0020 class AddressResolver;
0021 }
0022 
0023 namespace bt
0024 {
0025 class SHA1Hash;
0026 }
0027 
0028 namespace dht
0029 {
0030 class Node;
0031 class RPCServer;
0032 class Database;
0033 class TaskManager;
0034 class Task;
0035 class AnnounceTask;
0036 class NodeLookup;
0037 class KBucket;
0038 class ErrMsg;
0039 class PingReq;
0040 class FindNodeReq;
0041 class GetPeersReq;
0042 class AnnounceReq;
0043 
0044 /**
0045     @author Joris Guisson <joris.guisson@gmail.com>
0046 */
0047 class DHT : public DHTBase
0048 {
0049     Q_OBJECT
0050 public:
0051     DHT();
0052     ~DHT() override;
0053 
0054     void ping(const PingReq &r);
0055     void findNode(const FindNodeReq &r);
0056     void response(const RPCMsg &r);
0057     void getPeers(const GetPeersReq &r);
0058     void announce(const AnnounceReq &r);
0059     void error(const ErrMsg &r);
0060     void timeout(RPCMsg::Ptr r);
0061 
0062     /**
0063      * A Peer has received a PORT message, and uses this function to alert the DHT of it.
0064      * @param ip The IP of the peer
0065      * @param port The port in the PORT message
0066      */
0067     void portReceived(const QString &ip, bt::Uint16 port) override;
0068 
0069     /**
0070      * Do an announce on the DHT network
0071      * @param info_hash The info_hash
0072      * @param port The port
0073      * @return The task which handles this
0074      */
0075     AnnounceTask *announce(const bt::SHA1Hash &info_hash, bt::Uint16 port) override;
0076 
0077     /**
0078      * Refresh a bucket using a find node task.
0079      * @param id The id
0080      * @param bucket The bucket to refresh
0081      */
0082     NodeLookup *refreshBucket(const dht::Key &id, KBucket &bucket);
0083 
0084     /**
0085      * Do a NodeLookup.
0086      * @param id The id of the key to search
0087      */
0088     NodeLookup *findNode(const dht::Key &id);
0089 
0090     /// Do a findNode for our node id
0091     NodeLookup *findOwnNode();
0092 
0093     /// See if it is possible to start a task
0094     bool canStartTask() const;
0095 
0096     void start(const QString &table, const QString &key_file, bt::Uint16 port) override;
0097     void stop() override;
0098     void addDHTNode(const QString &host, bt::Uint16 hport) override;
0099 
0100     QMap<QString, int> getClosestGoodNodes(int maxNodes) override;
0101 
0102     /// Bootstrap from well-known nodes
0103     void bootstrap();
0104 
0105 private Q_SLOTS:
0106     void update() override;
0107     void onResolverResults(net::AddressResolver *ar);
0108     void ownNodeLookupFinished(Task *t);
0109     void expireDatabaseItems();
0110 
0111 private:
0112     Node *node;
0113     RPCServer *srv;
0114     Database *db;
0115     TaskManager *tman;
0116     QTimer expire_timer;
0117     QString table_file;
0118     QTimer update_timer;
0119     NodeLookup *our_node_lookup;
0120 };
0121 
0122 }
0123 
0124 #endif