2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2010-2022 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.
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#ifndef LIBTRANSMISSION_ANNOUNCER_MODULE
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only the libtransmission announcer module should #include this header.
|
2011-03-11 04:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-04 00:55:04 +00:00
|
|
|
#include <array>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // uint64_t
|
2022-07-27 21:53:39 +00:00
|
|
|
#include <optional>
|
2021-09-30 21:33:31 +00:00
|
|
|
#include <string>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <string_view>
|
2022-01-23 05:41:01 +00:00
|
|
|
#include <vector>
|
2021-09-30 21:33:31 +00:00
|
|
|
|
2021-11-04 00:55:04 +00:00
|
|
|
#include "transmission.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
#include "interned-string.h"
|
2022-04-21 15:58:13 +00:00
|
|
|
#include "net.h"
|
2022-01-25 04:25:55 +00:00
|
|
|
#include "peer-mgr.h" // tr_pex
|
2022-08-06 01:37:21 +00:00
|
|
|
|
|
|
|
struct tr_url_parsed_t;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
**** SCRAPE
|
|
|
|
***/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
/* pick a number small enough for common tracker software:
|
|
|
|
* - ocelot has no upper bound
|
|
|
|
* - opentracker has an upper bound of 64
|
|
|
|
* - udp protocol has an upper bound of 74
|
|
|
|
* - xbtt has no upper bound
|
|
|
|
*
|
|
|
|
* This is only an upper bound: if the tracker complains about
|
|
|
|
* length, announcer will incrementally lower the batch size.
|
|
|
|
*/
|
|
|
|
auto inline constexpr TR_MULTISCRAPE_MAX = 60;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_scrape_request
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the scrape URL */
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_interned_string scrape_url;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the name to use when deep logging is enabled */
|
|
|
|
char log_name[128];
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* info hashes of the torrents to scrape */
|
2021-11-04 00:55:04 +00:00
|
|
|
std::array<tr_sha1_digest_t, TR_MULTISCRAPE_MAX> info_hash;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many hashes to use in the info_hash field */
|
2022-01-23 05:41:01 +00:00
|
|
|
int info_hash_count = 0;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
struct tr_scrape_response_row
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the torrent's info_hash */
|
2021-11-04 00:55:04 +00:00
|
|
|
tr_sha1_digest_t info_hash;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many peers are seeding this torrent */
|
2022-01-23 05:41:01 +00:00
|
|
|
int seeders = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many peers are downloading this torrent */
|
2022-01-23 05:41:01 +00:00
|
|
|
int leechers = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many times this torrent has been downloaded */
|
2022-01-23 05:41:01 +00:00
|
|
|
int downloads = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the number of active downloaders in the swarm.
|
|
|
|
* this is a BEP 21 extension that some trackers won't support.
|
|
|
|
* http://www.bittorrent.org/beps/bep_0021.html#tracker-scrapes */
|
2022-01-23 05:41:01 +00:00
|
|
|
int downloaders = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
};
|
|
|
|
|
2021-09-30 21:33:31 +00:00
|
|
|
struct tr_scrape_response
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* whether or not we managed to connect to the tracker */
|
2022-01-23 05:41:01 +00:00
|
|
|
bool did_connect = false;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* whether or not the scrape timed out */
|
2022-01-23 05:41:01 +00:00
|
|
|
bool did_timeout = false;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many info hashes are in the 'rows' field */
|
|
|
|
int row_count;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the individual torrents' scrape results */
|
2022-01-23 05:41:01 +00:00
|
|
|
std::array<tr_scrape_response_row, TR_MULTISCRAPE_MAX> rows;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the raw scrape url */
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_interned_string scrape_url;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
/* human-readable error string on failure, or nullptr */
|
2021-09-30 21:33:31 +00:00
|
|
|
std::string errmsg;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* minimum interval (in seconds) allowed between scrapes.
|
|
|
|
* this is an unofficial extension that some trackers won't support. */
|
|
|
|
int min_request_interval;
|
2021-09-30 21:33:31 +00:00
|
|
|
};
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
using tr_scrape_response_func = void (*)(tr_scrape_response const* response, void* user_data);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_tracker_http_scrape(
|
2022-09-06 17:52:58 +00:00
|
|
|
tr_session const* session,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_scrape_request const* req,
|
|
|
|
tr_scrape_response_func response_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* user_data);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_tracker_udp_scrape(
|
|
|
|
tr_session* session,
|
|
|
|
tr_scrape_request const* req,
|
|
|
|
tr_scrape_response_func response_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* user_data);
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/***
|
|
|
|
**** ANNOUNCE
|
|
|
|
***/
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
enum tr_announce_event
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2021-09-10 00:28:22 +00:00
|
|
|
/* 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. */
|
2017-04-19 12:04:45 +00:00
|
|
|
TR_ANNOUNCE_EVENT_NONE,
|
|
|
|
TR_ANNOUNCE_EVENT_STARTED,
|
2021-09-10 00:28:22 +00:00
|
|
|
TR_ANNOUNCE_EVENT_COMPLETED,
|
|
|
|
TR_ANNOUNCE_EVENT_STOPPED,
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2022-07-31 20:58:14 +00:00
|
|
|
std::string_view tr_announce_event_get_string(tr_announce_event);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_announce_request
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_announce_event event = {};
|
|
|
|
bool partial_seed = false;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the port we listen for incoming peers on */
|
2022-04-21 15:58:13 +00:00
|
|
|
tr_port port;
|
2011-03-13 00:18:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* per-session key */
|
2022-01-23 05:41:01 +00:00
|
|
|
int key = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the number of peers we'd like to get back in the response */
|
2022-01-23 05:41:01 +00:00
|
|
|
int numwant = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the number of bytes we uploaded since the last 'started' event */
|
2022-01-23 05:41:01 +00:00
|
|
|
uint64_t up = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the number of good bytes we downloaded since the last 'started' event */
|
2022-01-23 05:41:01 +00:00
|
|
|
uint64_t down = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the number of bad bytes we downloaded since the last 'started' event */
|
2022-01-23 05:41:01 +00:00
|
|
|
uint64_t corrupt = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the total size of the torrent minus the number of bytes completed */
|
2022-01-23 05:41:01 +00:00
|
|
|
uint64_t leftUntilComplete = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the tracker's announce URL */
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_interned_string announce_url;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* key generated by and returned from an http tracker.
|
|
|
|
* see tr_announce_response.tracker_id_str */
|
2022-01-23 05:41:01 +00:00
|
|
|
std::string tracker_id;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the torrent's peer id.
|
|
|
|
* this changes when a torrent is stopped -> restarted. */
|
2021-10-22 02:40:55 +00:00
|
|
|
tr_peer_id_t peer_id;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the torrent's info_hash */
|
2021-11-04 00:55:04 +00:00
|
|
|
tr_sha1_digest_t info_hash;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the name to use when deep logging is enabled */
|
|
|
|
char log_name[128];
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_announce_response
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* the torrent's info hash */
|
2022-01-23 05:41:01 +00:00
|
|
|
tr_sha1_digest_t info_hash = {};
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* whether or not we managed to connect to the tracker */
|
2022-01-23 05:41:01 +00:00
|
|
|
bool did_connect = false;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* whether or not the scrape timed out */
|
2022-01-23 05:41:01 +00:00
|
|
|
bool did_timeout = false;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* preferred interval between announces.
|
|
|
|
* transmission treats this as the interval for periodic announces */
|
2022-01-23 05:41:01 +00:00
|
|
|
int interval = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* minimum interval between announces. (optional)
|
|
|
|
* transmission treats this as the min interval for manual announces */
|
2022-01-23 05:41:01 +00:00
|
|
|
int min_interval = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many peers are seeding this torrent */
|
2022-01-23 05:41:01 +00:00
|
|
|
int seeders = -1;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many peers are downloading this torrent */
|
2022-01-23 05:41:01 +00:00
|
|
|
int leechers = -1;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how many times this torrent has been downloaded */
|
2022-01-23 05:41:01 +00:00
|
|
|
int downloads = -1;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* IPv4 peers that we acquired from the tracker */
|
2022-01-23 05:41:01 +00:00
|
|
|
std::vector<tr_pex> pex;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* IPv6 peers that we acquired from the tracker */
|
2022-01-23 05:41:01 +00:00
|
|
|
std::vector<tr_pex> pex6;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
/* human-readable error string on failure, or nullptr */
|
2021-12-23 17:16:05 +00:00
|
|
|
std::string errmsg;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
/* human-readable warning string or nullptr */
|
2021-12-23 17:16:05 +00:00
|
|
|
std::string warning;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* key generated by and returned from an http tracker.
|
|
|
|
* if this is provided, subsequent http announces must include this. */
|
2022-01-23 05:41:01 +00:00
|
|
|
std::string tracker_id;
|
2022-02-24 13:59:58 +00:00
|
|
|
|
|
|
|
/* tracker extension that returns the client's public IP address.
|
|
|
|
* https://www.bittorrent.org/beps/bep_0024.html */
|
|
|
|
std::optional<tr_address> external_ip;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
using tr_announce_response_func = void (*)(tr_announce_response const* response, void* userdata);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_tracker_http_announce(
|
2022-09-06 17:52:58 +00:00
|
|
|
tr_session const* session,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_announce_request const* req,
|
|
|
|
tr_announce_response_func response_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* user_data);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void tr_tracker_udp_announce(
|
|
|
|
tr_session* session,
|
|
|
|
tr_announce_request const* req,
|
|
|
|
tr_announce_response_func response_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* user_data);
|
2011-03-13 06:38:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_tracker_udp_start_shutdown(tr_session* session);
|
2021-11-02 23:00:01 +00:00
|
|
|
|
2022-08-26 18:35:28 +00:00
|
|
|
void tr_announcerParseHttpAnnounceResponse(tr_announce_response& response, std::string_view benc, std::string_view log_name);
|
2022-01-25 04:25:55 +00:00
|
|
|
|
2022-08-26 18:35:28 +00:00
|
|
|
void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::string_view benc, std::string_view log_name);
|
2022-01-27 23:18:50 +00:00
|
|
|
|
2021-12-23 17:16:05 +00:00
|
|
|
tr_interned_string tr_announcerGetKey(tr_url_parsed_t const& parsed);
|