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

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef BTUDPTRACKER_H
0007 #define BTUDPTRACKER_H
0008 
0009 #include "tracker.h"
0010 #include <QByteArray>
0011 #include <QTimer>
0012 #include <QUrl>
0013 #include <net/address.h>
0014 
0015 namespace net
0016 {
0017 class AddressResolver;
0018 }
0019 
0020 namespace bt
0021 {
0022 class UDPTrackerSocket;
0023 
0024 /**
0025  * @author Joris Guisson
0026  * @brief Communicates with an UDP tracker
0027  *
0028  * This class is able to communicate with an UDP tracker.
0029  * This is an implementation of the protocol described in
0030  * http://xbtt.sourceforge.net/udp_tracker_protocol.html
0031  */
0032 class UDPTracker : public Tracker
0033 {
0034     Q_OBJECT
0035 public:
0036     UDPTracker(const QUrl &url, TrackerDataSource *tds, const PeerID &id, int tier);
0037     ~UDPTracker() override;
0038 
0039     void start() override;
0040     void stop(WaitJob *wjob = nullptr) override;
0041     void completed() override;
0042     Uint32 failureCount() const override
0043     {
0044         return failures;
0045     }
0046     void scrape() override;
0047 
0048 private Q_SLOTS:
0049     void onConnTimeout();
0050     void connectReceived(Int32 tid, Int64 connection_id);
0051     void announceReceived(Int32 tid, const Uint8 *buf, Uint32 size);
0052     void scrapeReceived(Int32 tid, const Uint8 *buf, Uint32 size);
0053     void onError(Int32 tid, const QString &error_string);
0054     void onResolverResults(net::AddressResolver *ar);
0055     void manualUpdate() override;
0056 
0057 private:
0058     void sendConnect();
0059     void sendAnnounce();
0060     void sendScrape();
0061     bool doRequest();
0062 
0063     enum Event {
0064         NONE = 0,
0065         COMPLETED = 1,
0066         STARTED = 2,
0067         STOPPED = 3,
0068     };
0069 
0070     enum Todo {
0071         NOTHING = 0,
0072         SCRAPE_REQUEST = 1,
0073         ANNOUNCE_REQUEST = 2,
0074     };
0075 
0076 private:
0077     net::Address address;
0078     Int64 connection_id;
0079     Int32 transaction_id;
0080     Int32 scrape_transaction_id;
0081 
0082     Uint32 data_read;
0083     int failures;
0084     bool resolved;
0085     Uint32 todo;
0086     Event event;
0087     QTimer conn_timer;
0088 
0089     static UDPTrackerSocket *socket;
0090     static Uint32 num_instances;
0091 };
0092 
0093 }
0094 
0095 #endif