2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2009-09-25 21:05:59 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // uint32_t
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <ctime>
|
2022-11-19 05:00:25 +00:00
|
|
|
#include <functional>
|
2023-06-30 04:51:55 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
2022-01-23 05:41:01 +00:00
|
|
|
#include <vector>
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <sys/socket.h> // socklen_t
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "libtransmission/transmission.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/interned-string.h"
|
|
|
|
#include "libtransmission/peer-mgr.h"
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
struct tr_address;
|
2022-11-19 05:00:25 +00:00
|
|
|
class tr_announcer_udp;
|
2023-07-08 15:24:03 +00:00
|
|
|
struct tr_session;
|
|
|
|
struct tr_torrent;
|
2022-01-23 05:41:01 +00:00
|
|
|
struct tr_torrent_announcer;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- Tracker Publish / Subscribe
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
/** @brief Notification object to tell listeners about announce or scrape occurrences */
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_tracker_event
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2022-11-19 05:00:25 +00:00
|
|
|
enum class Type
|
|
|
|
{
|
|
|
|
Error,
|
|
|
|
ErrorClear,
|
|
|
|
Counts,
|
|
|
|
Peers,
|
|
|
|
Warning,
|
|
|
|
};
|
|
|
|
|
|
|
|
// What type of event this is
|
|
|
|
Type type;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
// for Warning and Error events
|
2021-12-23 17:16:05 +00:00
|
|
|
std::string_view text;
|
|
|
|
tr_interned_string announce_url;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
// for Peers events
|
2022-01-23 05:41:01 +00:00
|
|
|
std::vector<tr_pex> pex;
|
2010-04-20 21:54:03 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
// for Peers and Counts events
|
2023-09-01 21:51:58 +00:00
|
|
|
std::optional<int64_t> leechers;
|
|
|
|
std::optional<int64_t> seeders;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2023-01-15 18:37:54 +00:00
|
|
|
using tr_tracker_callback = std::function<void(tr_torrent&, tr_tracker_event const*)>;
|
2010-06-19 14:33:10 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
class tr_announcer
|
|
|
|
{
|
|
|
|
public:
|
2023-11-13 03:43:43 +00:00
|
|
|
[[nodiscard]] static std::unique_ptr<tr_announcer> create(tr_session* session, tr_announcer_udp&);
|
2022-11-19 05:00:25 +00:00
|
|
|
virtual ~tr_announcer() = default;
|
|
|
|
|
2023-01-10 20:47:53 +00:00
|
|
|
virtual tr_torrent_announcer* addTorrent(tr_torrent*, tr_tracker_callback callback) = 0;
|
2022-11-19 05:00:25 +00:00
|
|
|
virtual void startTorrent(tr_torrent* tor) = 0;
|
|
|
|
virtual void stopTorrent(tr_torrent* tor) = 0;
|
|
|
|
virtual void resetTorrent(tr_torrent* tor) = 0;
|
|
|
|
virtual void removeTorrent(tr_torrent* tor) = 0;
|
2022-11-30 03:05:11 +00:00
|
|
|
virtual void startShutdown() = 0;
|
2023-11-13 03:43:43 +00:00
|
|
|
|
|
|
|
virtual void upkeep() = 0;
|
2022-11-19 05:00:25 +00:00
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
std::unique_ptr<tr_announcer> tr_announcerCreate(tr_session* session);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// --- For torrent customers
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerChangeMyPort(tr_torrent*);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_announcerCanManualAnnounce(tr_torrent const*);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerManualAnnounce(tr_torrent*);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerTorrentCompleted(tr_torrent*);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
TR_ANN_UP,
|
|
|
|
TR_ANN_DOWN,
|
|
|
|
TR_ANN_CORRUPT
|
|
|
|
};
|
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
void tr_announcerAddBytes(tr_torrent*, int type, uint32_t n_bytes);
|
2010-02-08 16:47:30 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t tr_announcerNextManualAnnounce(tr_torrent const*);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
tr_tracker_view tr_announcerTracker(tr_torrent const* torrent, size_t nth);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-12-10 02:03:26 +00:00
|
|
|
size_t tr_announcerTrackerCount(tr_torrent const* tor);
|
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- ANNOUNCE
|
2022-10-21 18:15:14 +00:00
|
|
|
|
|
|
|
enum tr_announce_event
|
|
|
|
{
|
|
|
|
/* Note: the ordering of this enum's values is important to
|
|
|
|
* announcer.c's tr_tier.announce_event_priority. If changing
|
|
|
|
* the enum, ensure announcer.c is compatible with the change. */
|
|
|
|
TR_ANNOUNCE_EVENT_NONE,
|
|
|
|
TR_ANNOUNCE_EVENT_STARTED,
|
|
|
|
TR_ANNOUNCE_EVENT_COMPLETED,
|
|
|
|
TR_ANNOUNCE_EVENT_STOPPED,
|
|
|
|
};
|
|
|
|
|
2022-11-18 02:23:54 +00:00
|
|
|
struct tr_announce_request;
|
|
|
|
struct tr_announce_response;
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2022-11-18 02:23:54 +00:00
|
|
|
struct tr_scrape_request;
|
|
|
|
struct tr_scrape_response;
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2023-01-07 14:27:54 +00:00
|
|
|
// --- UDP ANNOUNCER
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
using tr_scrape_response_func = std::function<void(tr_scrape_response const&)>;
|
|
|
|
using tr_announce_response_func = std::function<void(tr_announce_response const&)>;
|
|
|
|
|
2022-10-21 18:15:14 +00:00
|
|
|
class tr_announcer_udp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Mediator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~Mediator() noexcept = default;
|
|
|
|
virtual void sendto(void const* buf, size_t buflen, sockaddr const* addr, socklen_t addrlen) = 0;
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] virtual std::optional<tr_address> announce_ip() const = 0;
|
2022-10-21 18:15:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~tr_announcer_udp() noexcept = default;
|
|
|
|
|
|
|
|
[[nodiscard]] static std::unique_ptr<tr_announcer_udp> create(Mediator&);
|
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
virtual void announce(tr_announce_request const& request, tr_announce_response_func on_response) = 0;
|
2022-10-21 18:15:14 +00:00
|
|
|
|
2022-11-19 05:00:25 +00:00
|
|
|
virtual void scrape(tr_scrape_request const& request, tr_scrape_response_func on_response) = 0;
|
2022-10-21 18:15:14 +00:00
|
|
|
|
|
|
|
virtual void upkeep() = 0;
|
|
|
|
|
|
|
|
// @brief process an incoming udp message if it's a tracker response.
|
|
|
|
// @return true if msg was a tracker response; false otherwise
|
2023-05-06 04:11:05 +00:00
|
|
|
virtual bool handle_message(uint8_t const* msg, size_t msglen) = 0;
|
2023-11-13 03:43:43 +00:00
|
|
|
|
|
|
|
[[nodiscard]] virtual bool is_idle() const noexcept = 0;
|
2022-10-21 18:15:14 +00:00
|
|
|
};
|