2009-09-25 21:05:59 +00:00
|
|
|
/*
|
2014-01-18 20:56:57 +00:00
|
|
|
* This file Copyright (C) 2010-2014 Mnemosyne LLC
|
2009-09-25 21:05:59 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-09-25 21:05:59 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2021-10-14 15:36:49 +00:00
|
|
|
#include <array>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <climits> /* INT_MAX */
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib> /* qsort() */
|
|
|
|
#include <cstring> /* strcmp(), memcpy(), strncmp() */
|
2021-09-29 23:42:50 +00:00
|
|
|
#include <set>
|
2021-10-14 15:36:49 +00:00
|
|
|
#include <string_view>
|
2021-11-02 23:00:01 +00:00
|
|
|
#include <unordered_map>
|
2021-09-30 18:18:46 +00:00
|
|
|
#include <vector>
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2011-03-11 04:19:01 +00:00
|
|
|
#include <event2/event.h> /* evtimer */
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#define LIBTRANSMISSION_ANNOUNCER_MODULE
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "announcer.h"
|
2011-03-11 04:19:01 +00:00
|
|
|
#include "announcer-common.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "crypto-utils.h" /* tr_rand_int(), tr_rand_int_weak() */
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "peer-mgr.h" /* tr_peerMgrCompactToPex() */
|
2009-09-25 21:05:59 +00:00
|
|
|
#include "session.h"
|
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2009-09-25 21:05:59 +00:00
|
|
|
#include "utils.h"
|
2021-11-09 03:30:03 +00:00
|
|
|
#include "web-utils.h"
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
struct tr_tier;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void tier_build_log_name(struct tr_tier const* tier, char* buf, size_t buflen);
|
2010-02-14 03:40:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(tier, ...) \
|
2017-04-19 12:04:45 +00:00
|
|
|
do \
|
2013-01-25 23:34:20 +00:00
|
|
|
{ \
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_logGetDeepEnabled()) \
|
2013-01-25 23:34:20 +00:00
|
|
|
{ \
|
2017-04-19 12:04:45 +00:00
|
|
|
char name[128]; \
|
2017-05-22 20:12:57 +00:00
|
|
|
tier_build_log_name(tier, name, TR_N_ELEMENTS(name)); \
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddDeep(__FILE__, __LINE__, name, __VA_ARGS__); \
|
2013-01-25 23:34:20 +00:00
|
|
|
} \
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (0)
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
/* unless the tracker says otherwise, rescrape this frequently */
|
|
|
|
static auto constexpr DefaultScrapeIntervalSec = int{ 60 * 30 };
|
|
|
|
/* unless the tracker says otherwise, this is the announce interval */
|
|
|
|
static auto constexpr DefaultAnnounceIntervalSec = int{ 60 * 10 };
|
|
|
|
/* unless the tracker says otherwise, this is the announce min_interval */
|
|
|
|
static auto constexpr DefaultAnnounceMinIntervalSec = int{ 60 * 2 };
|
|
|
|
/* the value of the 'numwant' argument passed in tracker requests. */
|
|
|
|
static auto constexpr Numwant = int{ 80 };
|
|
|
|
|
|
|
|
/* how often to announce & scrape */
|
|
|
|
static auto constexpr UpkeepIntervalMsec = int{ 500 };
|
|
|
|
static auto constexpr MaxAnnouncesPerUpkeep = int{ 20 };
|
|
|
|
static auto constexpr MaxScrapesPerUpkeep = int{ 20 };
|
|
|
|
|
|
|
|
/* this is how often to call the UDP tracker upkeep */
|
|
|
|
static auto constexpr TauUpkeepIntervalSecs = int{ 5 };
|
|
|
|
|
|
|
|
/* how many infohashes to remove when we get a scrape-too-long error */
|
|
|
|
static auto constexpr TrMultiscrapeStep = int{ 5 };
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_announce_event_get_string(tr_announce_event e)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (e)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_ANNOUNCE_EVENT_COMPLETED:
|
|
|
|
return "completed";
|
|
|
|
|
|
|
|
case TR_ANNOUNCE_EVENT_STARTED:
|
|
|
|
return "started";
|
|
|
|
|
|
|
|
case TR_ANNOUNCE_EVENT_STOPPED:
|
|
|
|
return "stopped";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
namespace
|
2011-02-05 18:46:10 +00:00
|
|
|
{
|
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
struct StopsCompare
|
|
|
|
{
|
2021-11-04 00:55:04 +00:00
|
|
|
int compare(tr_announce_request const* a, tr_announce_request const* b) const // <=>
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
// primary key: volume of data transferred
|
2021-11-04 00:55:04 +00:00
|
|
|
auto const ax = a->up + a->down;
|
|
|
|
auto const bx = b->up + b->down;
|
|
|
|
if (ax < bx)
|
2021-09-29 23:42:50 +00:00
|
|
|
{
|
2021-11-04 00:55:04 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (ax > bx)
|
|
|
|
{
|
|
|
|
return 1;
|
2021-09-29 23:42:50 +00:00
|
|
|
}
|
2011-02-05 18:46:10 +00:00
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
// secondary key: the torrent's info_hash
|
2021-11-04 00:55:04 +00:00
|
|
|
if (a->info_hash < b->info_hash)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a->info_hash > b->info_hash)
|
2021-09-29 23:42:50 +00:00
|
|
|
{
|
2021-11-04 00:55:04 +00:00
|
|
|
return 1;
|
2021-09-29 23:42:50 +00:00
|
|
|
}
|
2011-06-19 18:26:04 +00:00
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
// tertiary key: the tracker's announce url
|
2021-11-04 00:55:04 +00:00
|
|
|
if (a->announce_url < b->announce_url)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a->announce_url > b->announce_url)
|
2021-11-02 23:00:01 +00:00
|
|
|
{
|
2021-11-04 00:55:04 +00:00
|
|
|
return 1;
|
2021-11-02 23:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-06-19 18:26:04 +00:00
|
|
|
|
2021-11-04 00:55:04 +00:00
|
|
|
bool operator()(tr_announce_request const* a, tr_announce_request const* b) const // less than
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
return compare(a, b) < 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-29 23:42:50 +00:00
|
|
|
};
|
2011-06-19 18:26:04 +00:00
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
} // namespace
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2019-02-16 20:19:38 +00:00
|
|
|
struct tr_scrape_info
|
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
tr_quark const scrape_url;
|
2019-02-16 20:19:38 +00:00
|
|
|
|
|
|
|
int multiscrape_max;
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
tr_scrape_info(tr_quark scrape_url_in, int const multiscrape_max_in)
|
|
|
|
: scrape_url{ scrape_url_in }
|
2021-09-30 18:18:46 +00:00
|
|
|
, multiscrape_max{ multiscrape_max_in }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2019-02-16 20:19:38 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
/**
|
|
|
|
* "global" (per-tr_session) fields
|
|
|
|
*/
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_announcer
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
std::set<tr_announce_request*, StopsCompare> stops;
|
2021-11-02 23:00:01 +00:00
|
|
|
std::unordered_map<tr_quark, tr_scrape_info> scrape_info;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session;
|
|
|
|
struct event* upkeepTimer;
|
2011-03-10 11:46:22 +00:00
|
|
|
int key;
|
2011-03-13 06:38:54 +00:00
|
|
|
time_t tauUpkeepAt;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
static tr_scrape_info* tr_announcerGetScrapeInfo(tr_announcer* announcer, tr_quark url)
|
2019-02-16 20:19:38 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
if (url == TR_KEY_NONE)
|
2019-02-16 20:19:38 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
return nullptr;
|
2019-02-16 20:19:38 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
auto& scrapes = announcer->scrape_info;
|
|
|
|
auto const it = scrapes.try_emplace(url, url, TR_MULTISCRAPE_MAX);
|
|
|
|
return &it.first->second;
|
2019-02-16 20:19:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void onUpkeepTimer(evutil_socket_t fd, short what, void* vannouncer);
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerInit(tr_session* session)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isSession(session));
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
auto* a = new tr_announcer{};
|
2017-04-19 12:04:45 +00:00
|
|
|
a->key = tr_rand_int(INT_MAX);
|
2009-09-25 21:05:59 +00:00
|
|
|
a->session = session;
|
2017-04-19 12:04:45 +00:00
|
|
|
a->upkeepTimer = evtimer_new(session->event_base, onUpkeepTimer, a);
|
2021-10-21 00:39:05 +00:00
|
|
|
tr_timerAddMsec(a->upkeepTimer, UpkeepIntervalMsec);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
session->announcer = a;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void flushCloseMessages(tr_announcer* announcer);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerClose(tr_session* session)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announcer* announcer = session->announcer;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
flushCloseMessages(announcer);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker_udp_start_shutdown(session);
|
2011-03-17 18:51:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
event_free(announcer->upkeepTimer);
|
2021-09-15 00:18:09 +00:00
|
|
|
announcer->upkeepTimer = nullptr;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
session->announcer = nullptr;
|
2021-09-29 23:42:50 +00:00
|
|
|
delete announcer;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* a row in tr_tier's list of trackers */
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_tracker
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
tr_quark key;
|
|
|
|
tr_quark announce_url;
|
2019-02-16 20:19:38 +00:00
|
|
|
struct tr_scrape_info* scrape_info;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tracker_id_str;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
int seederCount;
|
|
|
|
int leecherCount;
|
|
|
|
int downloadCount;
|
|
|
|
int downloaderCount;
|
2010-02-13 21:58:38 +00:00
|
|
|
|
2011-03-14 16:52:47 +00:00
|
|
|
int consecutiveFailures;
|
2011-01-20 22:51:05 +00:00
|
|
|
|
2010-01-13 22:46:22 +00:00
|
|
|
uint32_t id;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
// format: `${host}:${port}`
|
2021-11-09 03:30:03 +00:00
|
|
|
tr_quark tr_announcerGetKey(tr_url_parsed_t const& parsed)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
std::string buf;
|
|
|
|
tr_buildBuf(buf, parsed.host, ":"sv, parsed.portstr);
|
|
|
|
return tr_quark_new(buf);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 20:19:38 +00:00
|
|
|
static void trackerConstruct(tr_announcer* announcer, tr_tracker* tracker, tr_tracker_info const* inf)
|
2011-03-31 20:26:43 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(tracker, 0, sizeof(tr_tracker));
|
2021-11-02 23:00:01 +00:00
|
|
|
tracker->key = tr_announcerGetKey(inf->announce);
|
2021-11-10 00:13:47 +00:00
|
|
|
tracker->announce_url = tr_quark_new(tr_strvStrip(inf->announce));
|
2021-11-02 23:00:01 +00:00
|
|
|
tracker->scrape_info = inf->scrape == nullptr ? nullptr : tr_announcerGetScrapeInfo(announcer, tr_quark_new(inf->scrape));
|
2011-03-31 20:26:43 +00:00
|
|
|
tracker->id = inf->id;
|
2009-09-28 15:16:23 +00:00
|
|
|
tracker->seederCount = -1;
|
|
|
|
tracker->leecherCount = -1;
|
|
|
|
tracker->downloadCount = -1;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void trackerDestruct(tr_tracker* tracker)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tracker->tracker_id_str);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct tr_torrent_tiers;
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief A group of trackers in a single tier, as per the multitracker spec */
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_tier
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2010-02-08 16:47:30 +00:00
|
|
|
/* number of up/down/corrupt bytes since the last time we sent an
|
|
|
|
* "event=stopped" message that was acknowledged by the tracker */
|
2010-10-24 21:35:23 +00:00
|
|
|
uint64_t byteCounts[3];
|
2010-02-08 16:47:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker* trackers;
|
2011-03-31 20:26:43 +00:00
|
|
|
int tracker_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker* currentTracker;
|
2010-02-13 21:58:38 +00:00
|
|
|
int currentTrackerIndex;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-13 21:58:38 +00:00
|
|
|
time_t scrapeAt;
|
2009-09-25 21:05:59 +00:00
|
|
|
time_t lastScrapeStartTime;
|
2010-02-13 21:58:38 +00:00
|
|
|
time_t lastScrapeTime;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool lastScrapeSucceeded;
|
|
|
|
bool lastScrapeTimedOut;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-13 21:58:38 +00:00
|
|
|
time_t announceAt;
|
2009-09-25 21:05:59 +00:00
|
|
|
time_t manualAnnounceAllowedAt;
|
2010-02-13 21:58:38 +00:00
|
|
|
time_t lastAnnounceStartTime;
|
|
|
|
time_t lastAnnounceTime;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool lastAnnounceSucceeded;
|
|
|
|
bool lastAnnounceTimedOut;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announce_event* announce_events;
|
2021-09-10 00:28:22 +00:00
|
|
|
int announce_event_priority;
|
2011-03-11 04:19:01 +00:00
|
|
|
int announce_event_count;
|
|
|
|
int announce_event_alloc;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* unique lookup key */
|
|
|
|
int key;
|
|
|
|
|
|
|
|
int scrapeIntervalSec;
|
|
|
|
int announceIntervalSec;
|
|
|
|
int announceMinIntervalSec;
|
|
|
|
|
|
|
|
int lastAnnouncePeerCount;
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool isRunning;
|
|
|
|
bool isAnnouncing;
|
|
|
|
bool isScraping;
|
|
|
|
bool wasCopied;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
char lastAnnounceStr[128];
|
|
|
|
char lastScrapeStr[128];
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static time_t get_next_scrape_time(tr_session const* session, tr_tier const* tier, int interval)
|
2011-04-02 07:44:19 +00:00
|
|
|
{
|
2011-07-23 17:14:12 +00:00
|
|
|
/* Maybe don't scrape paused torrents */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tier->isRunning && !session->scrapePausedTorrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 14:22:28 +00:00
|
|
|
|
2011-04-02 07:44:19 +00:00
|
|
|
/* Add the interval, and then increment to the nearest 10th second.
|
2011-04-07 06:54:03 +00:00
|
|
|
* The latter step is to increase the odds of several torrents coming
|
2011-04-02 07:44:19 +00:00
|
|
|
* due at the same time to improve multiscrape. */
|
2021-10-14 14:22:28 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
time_t ret = now + interval;
|
|
|
|
while (ret % 10 != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
++ret;
|
2011-07-23 17:14:12 +00:00
|
|
|
}
|
|
|
|
|
2011-04-10 03:57:37 +00:00
|
|
|
return ret;
|
2011-04-02 07:44:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tierConstruct(tr_tier* tier, tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
static int nextKey = 1;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(tier, 0, sizeof(tr_tier));
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
tier->key = nextKey++;
|
|
|
|
tier->currentTrackerIndex = -1;
|
2021-10-21 00:39:05 +00:00
|
|
|
tier->scrapeIntervalSec = DefaultScrapeIntervalSec;
|
|
|
|
tier->announceIntervalSec = DefaultAnnounceIntervalSec;
|
|
|
|
tier->announceMinIntervalSec = DefaultAnnounceMinIntervalSec;
|
2021-09-10 00:28:22 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time(tor->session, tier, 0);
|
2011-03-31 20:26:43 +00:00
|
|
|
tier->tor = tor;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tierDestruct(tr_tier* tier)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tier->announce_events);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void tier_build_log_name(tr_tier const* tier, char* buf, size_t buflen)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
auto const* const name = tier != nullptr && tier->tor != nullptr ? tr_torrentName(tier->tor) : "?";
|
|
|
|
auto const key_sv = tier != nullptr && tier->currentTracker != nullptr ?
|
|
|
|
tr_quark_get_string_view(tier->currentTracker->key) :
|
|
|
|
"?"sv;
|
|
|
|
tr_snprintf(buf, buflen, "[%s---%" TR_PRIsv "]", name, TR_PRIsv_ARG(key_sv));
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tierIncrementTracker(tr_tier* tier)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2009-12-02 15:16:29 +00:00
|
|
|
/* move our index to the next tracker in the tier */
|
2021-09-15 00:18:09 +00:00
|
|
|
int const i = tier->currentTracker == nullptr ? 0 : (tier->currentTrackerIndex + 1) % tier->tracker_count;
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->currentTrackerIndex = i;
|
2011-03-31 20:26:43 +00:00
|
|
|
tier->currentTracker = &tier->trackers[i];
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* reset some of the tier's fields */
|
2021-10-21 00:39:05 +00:00
|
|
|
tier->scrapeIntervalSec = DefaultScrapeIntervalSec;
|
|
|
|
tier->announceIntervalSec = DefaultAnnounceIntervalSec;
|
|
|
|
tier->announceMinIntervalSec = DefaultAnnounceMinIntervalSec;
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->isAnnouncing = false;
|
|
|
|
tier->isScraping = false;
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->lastAnnounceStartTime = 0;
|
|
|
|
tier->lastScrapeStartTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Opaque, per-torrent data structure for tracker announce information
|
|
|
|
*
|
2009-09-25 21:05:59 +00:00
|
|
|
* this opaque data structure can be found in tr_torrent.tiers
|
|
|
|
*/
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_torrent_tiers
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tier* tiers;
|
2011-03-31 20:26:43 +00:00
|
|
|
int tier_count;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker* trackers;
|
2011-03-31 20:26:43 +00:00
|
|
|
int tracker_count;
|
|
|
|
|
2013-09-08 17:29:25 +00:00
|
|
|
tr_tracker_callback callback;
|
2017-04-19 12:04:45 +00:00
|
|
|
void* callbackData;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_torrent_tiers* tiersNew(void)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_new0(tr_torrent_tiers, 1);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tiersDestruct(tr_torrent_tiers* tt)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tt->tracker_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
trackerDestruct(&tt->trackers[i]);
|
|
|
|
}
|
2011-03-31 20:26:43 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tt->trackers);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tierDestruct(&tt->tiers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(tt->tiers);
|
2011-03-31 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tiersFree(tr_torrent_tiers* tt)
|
2011-03-31 20:26:43 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tiersDestruct(tt);
|
|
|
|
tr_free(tt);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 00:55:04 +00:00
|
|
|
static tr_tier* getTier(tr_announcer* announcer, tr_sha1_digest_t const& info_hash, int tierId)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_tier* tier = nullptr;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (announcer != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = announcer->session;
|
|
|
|
tr_torrent* tor = tr_torrentFindFromHash(session, info_hash);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tor != nullptr && tor->tiers != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent_tiers* tt = tor->tiers;
|
2011-03-31 20:26:43 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
for (int i = 0; tier == nullptr && i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tt->tiers[i].key == tierId)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
tier = &tt->tiers[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** PUBLISH
|
|
|
|
***/
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void publishMessage(tr_tier* tier, char const* msg, TrackerEventType type)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier != nullptr && tier->tor != nullptr && tier->tor->tiers != nullptr && tier->tor->tiers->callback != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent_tiers* tiers = tier->tor->tiers;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto event = tr_tracker_event{};
|
2009-09-25 21:05:59 +00:00
|
|
|
event.messageType = type;
|
|
|
|
event.text = msg;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier->currentTracker != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
event.announce_url = tier->currentTracker->announce_url;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-19 14:33:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(*tiers->callback)(tier->tor, &event, tiers->callbackData);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void publishErrorClear(tr_tier* tier)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
publishMessage(tier, nullptr, TR_TRACKER_ERROR_CLEAR);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void publishWarning(tr_tier* tier, char const* msg)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
publishMessage(tier, msg, TR_TRACKER_WARNING);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void publishError(tr_tier* tier, char const* msg)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
publishMessage(tier, msg, TR_TRACKER_ERROR);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 20:25:30 +00:00
|
|
|
static void publishPeerCounts(tr_tier* tier, int seeders, int leechers)
|
2010-05-18 15:14:54 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier->tor->tiers->callback != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto e = tr_tracker_event{};
|
2021-09-09 20:25:30 +00:00
|
|
|
e.messageType = TR_TRACKER_COUNTS;
|
|
|
|
e.seeders = seeders;
|
|
|
|
e.leechers = leechers;
|
|
|
|
dbgmsg(tier, "peer counts: %d seeders, %d leechers.", seeders, leechers);
|
2011-03-11 04:57:53 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
(*tier->tor->tiers->callback)(tier->tor, &e, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-18 15:14:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 20:25:30 +00:00
|
|
|
static void publishPeersPex(tr_tier* tier, int seeders, int leechers, tr_pex const* pex, int n)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier->tor->tiers->callback != nullptr)
|
2011-03-14 02:40:39 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto e = tr_tracker_event{};
|
2011-03-14 02:40:39 +00:00
|
|
|
e.messageType = TR_TRACKER_PEERS;
|
2021-09-09 20:25:30 +00:00
|
|
|
e.seeders = seeders;
|
|
|
|
e.leechers = leechers;
|
2011-03-14 02:40:39 +00:00
|
|
|
e.pex = pex;
|
|
|
|
e.pexCount = n;
|
2021-09-09 20:25:30 +00:00
|
|
|
dbgmsg(tier, "tracker knows of %d seeders and %d leechers and gave a list of %d peers.", seeders, leechers, n);
|
2011-03-11 04:57:53 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
(*tier->tor->tiers->callback)(tier->tor, &e, nullptr);
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
struct AnnTrackerInfo
|
2011-03-15 03:21:25 +00:00
|
|
|
{
|
2021-11-18 00:17:09 +00:00
|
|
|
AnnTrackerInfo(tr_tracker_info const& info_in, tr_url_parsed_t const& url_in)
|
2021-11-02 23:00:01 +00:00
|
|
|
: info{ info_in }
|
|
|
|
, url{ url_in }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-03-15 03:21:25 +00:00
|
|
|
tr_tracker_info info;
|
2021-11-09 03:30:03 +00:00
|
|
|
tr_url_parsed_t url;
|
2011-03-15 03:21:25 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
/* primary key: tier
|
|
|
|
* secondary key: udp comes before http */
|
|
|
|
int compare(AnnTrackerInfo const& that) const // <=>
|
|
|
|
{
|
|
|
|
if (this->info.tier != that.info.tier)
|
|
|
|
{
|
|
|
|
return this->info.tier - that.info.tier;
|
|
|
|
}
|
2011-03-15 03:21:25 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
return -this->url.scheme.compare(that.url.scheme);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
bool operator<(AnnTrackerInfo const& that) const // less than
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
return this->compare(that) < 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-11-02 23:00:01 +00:00
|
|
|
};
|
2011-03-15 03:21:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Massages the incoming list of trackers into something we can use.
|
|
|
|
*/
|
2020-11-05 22:46:21 +00:00
|
|
|
static tr_tracker_info* filter_trackers(tr_tracker_info const* input, int input_count, int* setme_count)
|
2011-03-15 03:21:25 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
auto tmp = std::vector<AnnTrackerInfo>{};
|
|
|
|
tmp.reserve(input_count);
|
2011-03-15 03:21:25 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
// build a list of valid trackers
|
|
|
|
for (auto const *walk = input, *const end = walk + input_count; walk != end; ++walk)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
auto const parsed = tr_urlParseTracker(walk->announce);
|
|
|
|
if (!parsed)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
// weed out implicit-vs-explicit port duplicates e.g.
|
|
|
|
// "http://tracker/announce" + "http://tracker:80/announce"
|
|
|
|
auto const is_same = [&parsed](auto const& item)
|
|
|
|
{
|
|
|
|
return item.url.scheme == parsed->scheme && item.url.host == parsed->host && item.url.port == parsed->port &&
|
|
|
|
item.url.path == parsed->path;
|
|
|
|
};
|
|
|
|
if (std::any_of(std::begin(tmp), std::end(tmp), is_same))
|
|
|
|
{
|
|
|
|
continue;
|
2011-03-15 03:21:25 +00:00
|
|
|
}
|
2021-11-02 23:00:01 +00:00
|
|
|
|
|
|
|
tmp.emplace_back(*walk, *parsed);
|
2011-03-15 03:21:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
// if two announce URLs differ only by scheme, put them in the same tier.
|
|
|
|
// (note: this can leave gaps in the `tier' values, but since the calling
|
|
|
|
// function doesn't care, there's no point in removing the gaps...)
|
|
|
|
for (size_t i = 0, n = std::size(tmp); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
for (size_t j = i + 1; j < n; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
auto const& a = tmp[i];
|
|
|
|
auto& b = tmp[j];
|
|
|
|
|
|
|
|
if ((a.info.tier != b.info.tier) && (a.url.port == b.url.port) && (a.url.host == b.url.host) &&
|
|
|
|
(a.url.path == b.url.path))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
b.info.tier = a.info.tier;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-15 03:21:25 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
// sort them, for two reasons:
|
|
|
|
// 1. unjumble the tiers from the previous step
|
|
|
|
// 2. move the UDP trackers to the front of each tier
|
|
|
|
std::sort(std::begin(tmp), std::end(tmp));
|
2011-03-15 03:21:25 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
// build the output
|
|
|
|
auto const n = std::size(tmp);
|
2011-03-15 03:21:25 +00:00
|
|
|
*setme_count = n;
|
2021-11-02 23:00:01 +00:00
|
|
|
auto* const ret = tr_new0(tr_tracker_info, n);
|
|
|
|
std::transform(std::begin(tmp), std::end(tmp), ret, [](auto const& item) { return item.info; });
|
2011-03-15 03:21:25 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void addTorrentToTier(tr_torrent_tiers* tt, tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
auto n = int{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker_info* infos = filter_trackers(tor->info.trackers, tor->info.trackerCount, &n);
|
2009-10-02 02:03:30 +00:00
|
|
|
|
2011-04-30 00:08:24 +00:00
|
|
|
/* build the array of trackers */
|
2017-04-19 12:04:45 +00:00
|
|
|
tt->trackers = tr_new0(tr_tracker, n);
|
2011-03-31 20:26:43 +00:00
|
|
|
tt->tracker_count = n;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-02-16 20:19:38 +00:00
|
|
|
trackerConstruct(tor->session->announcer, &tt->trackers[i], &infos[i]);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* count how many tiers there are */
|
2021-10-14 14:22:28 +00:00
|
|
|
auto tier_count = int{};
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (i == 0 || infos[i].tier != infos[i - 1].tier)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
++tier_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* build the array of tiers */
|
2021-10-14 14:22:28 +00:00
|
|
|
tr_tier* tier = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
tt->tiers = tr_new0(tr_tier, tier_count);
|
2011-03-31 20:26:43 +00:00
|
|
|
tt->tier_count = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (i != 0 && infos[i].tier == infos[i - 1].tier)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
++tier->tracker_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
tier = &tt->tiers[tt->tier_count++];
|
2017-04-19 12:04:45 +00:00
|
|
|
tierConstruct(tier, tor);
|
2011-03-31 20:26:43 +00:00
|
|
|
tier->trackers = &tt->trackers[i];
|
|
|
|
tier->tracker_count = 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
tierIncrementTracker(tier);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* cleanup */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(infos);
|
2009-10-02 04:54:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent_tiers* tr_announcerAddTorrent(tr_torrent* tor, tr_tracker_callback callback, void* callbackData)
|
2009-10-02 04:54:02 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2009-10-02 04:54:02 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
tr_torrent_tiers* tiers = tiersNew();
|
2010-06-19 14:33:10 +00:00
|
|
|
tiers->callback = callback;
|
|
|
|
tiers->callbackData = callbackData;
|
2009-10-02 04:54:02 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
addTorrentToTier(tiers, tor);
|
2009-10-02 04:54:02 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
return tiers;
|
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-10-02 04:54:02 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool tierCanManualAnnounce(tr_tier const* tier)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tier->manualAnnounceAllowedAt <= tr_time();
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_announcerCanManualAnnounce(tr_torrent const* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(tor->tiers != nullptr);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
struct tr_torrent_tiers const* tt = nullptr;
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->isRunning)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
tt = tor->tiers;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-22 19:56:01 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* return true if any tier can manual announce */
|
2021-09-15 00:18:09 +00:00
|
|
|
for (int i = 0; tt != nullptr && i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (tierCanManualAnnounce(&tt->tiers[i]))
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
return false;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t tr_announcerNextManualAnnounce(tr_torrent const* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
time_t ret = ~(time_t)0;
|
2020-11-09 03:31:02 +00:00
|
|
|
struct tr_torrent_tiers const* tt = tor->tiers;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* find the earliest manual announce time from all peers */
|
2021-09-15 00:18:09 +00:00
|
|
|
for (int i = 0; tt != nullptr && i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tt->tiers[i].isRunning)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
ret = std::min(ret, tt->tiers[i].manualAnnounceAllowedAt);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void dbgmsg_tier_announce_queue(tr_tier const* tier)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_logGetDeepEnabled())
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
char name[128];
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* buf = evbuffer_new();
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tier_build_log_name(tier, name, sizeof(name));
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tier->announce_event_count; ++i)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_announce_event const e = tier->announce_events[i];
|
|
|
|
char const* str = tr_announce_event_get_string(e);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(buf, "[%d:%s]", i, str);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2011-04-27 20:41:47 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
char* const message = evbuffer_free_to_str(buf, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddDeep(__FILE__, __LINE__, name, "announce queue is %s", message);
|
|
|
|
tr_free(message);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
// higher priorities go to the front of the announce queue
|
|
|
|
static void tier_update_announce_priority(tr_tier* tier)
|
|
|
|
{
|
|
|
|
int priority = -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < tier->announce_event_count; ++i)
|
|
|
|
{
|
2021-09-19 20:41:35 +00:00
|
|
|
priority = std::max(priority, int{ tier->announce_events[i] });
|
2021-09-10 00:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tier->announce_event_priority = priority;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tier_announce_remove_trailing(tr_tier* tier, tr_announce_event e)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
while (tier->announce_event_count > 0 && tier->announce_events[tier->announce_event_count - 1] == e)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
--tier->announce_event_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-10 00:28:22 +00:00
|
|
|
|
|
|
|
tier_update_announce_priority(tier);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tier_announce_event_push(tr_tier* tier, tr_announce_event e, time_t announceAt)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(tier != nullptr);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg_tier_announce_queue(tier);
|
|
|
|
dbgmsg(tier, "queued \"%s\"", tr_announce_event_get_string(e));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier->announce_event_count > 0)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
/* special case #1: if we're adding a "stopped" event,
|
|
|
|
* dump everything leading up to it except "completed" */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (e == TR_ANNOUNCE_EVENT_STOPPED)
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
bool has_completed = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_announce_event const c = TR_ANNOUNCE_EVENT_COMPLETED;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; !has_completed && i < tier->announce_event_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
has_completed = c == tier->announce_events[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->announce_event_count = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (has_completed)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->announce_events[tier->announce_event_count++] = c;
|
2021-09-10 00:28:22 +00:00
|
|
|
tier_update_announce_priority(tier);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* special case #2: dump all empty strings leading up to this event */
|
2017-04-19 12:04:45 +00:00
|
|
|
tier_announce_remove_trailing(tier, TR_ANNOUNCE_EVENT_NONE);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* special case #3: no consecutive duplicates */
|
2017-04-19 12:04:45 +00:00
|
|
|
tier_announce_remove_trailing(tier, e);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make room in the array for another event */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tier->announce_event_alloc <= tier->announce_event_count)
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->announce_event_alloc += 4;
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->announce_events = tr_renew(tr_announce_event, tier->announce_events, tier->announce_event_alloc);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add it */
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->announceAt = announceAt;
|
2021-09-10 00:28:22 +00:00
|
|
|
tier->announce_events[tier->announce_event_count++] = e;
|
|
|
|
tier_update_announce_priority(tier);
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg_tier_announce_queue(tier);
|
|
|
|
dbgmsg(tier, "announcing in %d seconds", (int)difftime(announceAt, tr_time()));
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_announce_event tier_announce_event_pull(tr_tier* tier)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_announce_event const e = tier->announce_events[0];
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2019-03-17 05:00:15 +00:00
|
|
|
tr_removeElementFromArray(tier->announce_events, 0, sizeof(tr_announce_event), tier->announce_event_count);
|
|
|
|
--tier->announce_event_count;
|
2021-09-10 00:28:22 +00:00
|
|
|
tier_update_announce_priority(tier);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2011-03-11 05:48:57 +00:00
|
|
|
return e;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void torrentAddAnnounce(tr_torrent* tor, tr_announce_event e, time_t announceAt)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_torrent_tiers* tt = tor->tiers;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* walk through each tier and tell them to announce */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tier_announce_event_push(&tt->tiers[i], e, announceAt);
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerTorrentStarted(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_STARTED, tr_time());
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void tr_announcerManualAnnounce(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_NONE, tr_time());
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void tr_announcerTorrentStopped(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_STOPPED, tr_time());
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void tr_announcerTorrentCompleted(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
torrentAddAnnounce(tor, TR_ANNOUNCE_EVENT_COMPLETED, tr_time());
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
void tr_announcerChangeMyPort(tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announcerTorrentStarted(tor);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2010-02-08 16:47:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerAddBytes(tr_torrent* tor, int type, uint32_t byteCount)
|
2010-02-08 16:47:30 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(tor));
|
|
|
|
TR_ASSERT(type == TR_ANN_UP || type == TR_ANN_DOWN || type == TR_ANN_CORRUPT);
|
2010-02-08 16:47:30 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct tr_torrent_tiers* tt = tor->tiers;
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tt->tiers[i].byteCounts[type] += byteCount;
|
|
|
|
}
|
2010-02-08 16:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static tr_announce_request* announce_request_new(
|
|
|
|
tr_announcer const* announcer,
|
|
|
|
tr_torrent* tor,
|
|
|
|
tr_tier const* tier,
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announce_event event)
|
|
|
|
{
|
|
|
|
tr_announce_request* req = tr_new0(tr_announce_request, 1);
|
|
|
|
req->port = tr_sessionGetPublicPeerPort(announcer->session);
|
2021-11-02 23:00:01 +00:00
|
|
|
req->announce_url = tier->currentTracker->announce_url;
|
2017-04-19 12:04:45 +00:00
|
|
|
req->tracker_id_str = tr_strdup(tier->currentTracker->tracker_id_str);
|
2021-11-04 00:55:04 +00:00
|
|
|
req->info_hash = tr_torrentInfoHash(tor);
|
2021-10-22 02:40:55 +00:00
|
|
|
req->peer_id = tr_torrentGetPeerId(tor);
|
2011-03-11 04:19:01 +00:00
|
|
|
req->up = tier->byteCounts[TR_ANN_UP];
|
|
|
|
req->down = tier->byteCounts[TR_ANN_DOWN];
|
|
|
|
req->corrupt = tier->byteCounts[TR_ANN_CORRUPT];
|
2021-11-25 18:26:51 +00:00
|
|
|
req->leftUntilComplete = tr_torrentHasMetadata(tor) ? tor->info.totalSize - tor->hasTotal() : INT64_MAX;
|
2011-03-11 04:19:01 +00:00
|
|
|
req->event = event;
|
2021-10-21 00:39:05 +00:00
|
|
|
req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : Numwant;
|
2011-03-11 04:19:01 +00:00
|
|
|
req->key = announcer->key;
|
2017-04-19 12:04:45 +00:00
|
|
|
req->partial_seed = tr_torrentGetCompleteness(tor) == TR_PARTIAL_SEED;
|
|
|
|
tier_build_log_name(tier, req->log_name, sizeof(req->log_name));
|
2011-03-11 04:19:01 +00:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void announce_request_free(tr_announce_request* req);
|
2011-06-19 18:26:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerRemoveTorrent(tr_announcer* announcer, tr_torrent* tor)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
struct tr_torrent_tiers const* tt = tor->tiers;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tt != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tt->tier_count; ++i)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_tier const* tier = &tt->tiers[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier->isRunning)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_announce_event const e = TR_ANNOUNCE_EVENT_STOPPED;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announce_request* req = announce_request_new(announcer, tor, tier, e);
|
2011-06-19 18:26:04 +00:00
|
|
|
|
2021-09-29 23:42:50 +00:00
|
|
|
if (announcer->stops.count(req))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
announce_request_free(req);
|
|
|
|
}
|
2011-06-19 18:26:04 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
announcer->stops.insert(req);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tiersFree(tor->tiers);
|
2021-09-15 00:18:09 +00:00
|
|
|
tor->tiers = nullptr;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int getRetryInterval(tr_tracker const* t)
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (t->consecutiveFailures)
|
2013-07-16 00:13:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return 20;
|
|
|
|
|
|
|
|
case 2:
|
2017-04-30 16:30:03 +00:00
|
|
|
return tr_rand_int_weak(60) + 60 * 5;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case 3:
|
2017-04-30 16:30:03 +00:00
|
|
|
return tr_rand_int_weak(60) + 60 * 15;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case 4:
|
2017-04-30 16:30:03 +00:00
|
|
|
return tr_rand_int_weak(60) + 60 * 30;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case 5:
|
2017-04-30 16:30:03 +00:00
|
|
|
return tr_rand_int_weak(60) + 60 * 60;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
2017-04-30 16:30:03 +00:00
|
|
|
return tr_rand_int_weak(60) + 60 * 120;
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-05 18:46:10 +00:00
|
|
|
struct announce_data
|
|
|
|
{
|
|
|
|
int tierId;
|
|
|
|
time_t timeSent;
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_announce_event event;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session;
|
2011-02-05 18:46:10 +00:00
|
|
|
|
|
|
|
/** If the request succeeds, the value for tier's "isRunning" flag */
|
2011-03-22 15:19:54 +00:00
|
|
|
bool isRunningOnSuccess;
|
2011-02-05 18:46:10 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_announce_error(tr_tier* tier, char const* err, tr_announce_event e)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2011-03-14 16:52:47 +00:00
|
|
|
/* increment the error count */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier->currentTracker != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-14 16:52:47 +00:00
|
|
|
++tier->currentTracker->consecutiveFailures;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-14 16:52:47 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/* set the error message */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(tier->lastAnnounceStr, err, sizeof(tier->lastAnnounceStr));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* switch to the next tracker */
|
2017-04-19 12:04:45 +00:00
|
|
|
tierIncrementTracker(tier);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* schedule a reannounce */
|
2021-10-14 14:22:28 +00:00
|
|
|
int const interval = getRetryInterval(tier->currentTracker);
|
2021-11-02 23:00:01 +00:00
|
|
|
auto const* const key_cstr = tr_quark_get_string(tier->currentTracker->key);
|
|
|
|
dbgmsg(tier, "Tracker '%s' announce error: %s (Retrying in %d seconds)", key_cstr, err, interval);
|
|
|
|
tr_logAddTorInfo(tier->tor, "Tracker '%s' announce error: %s (Retrying in %d seconds)", key_cstr, err, interval);
|
2017-04-19 12:04:45 +00:00
|
|
|
tier_announce_event_push(tier, e, tr_time() + interval);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_announce_done(tr_announce_response const* response, void* vdata)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct announce_data*>(vdata);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announcer* announcer = data->session->announcer;
|
|
|
|
tr_tier* tier = getTier(announcer, response->info_hash, data->tierId);
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
tr_announce_event const event = data->event;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
dbgmsg(
|
|
|
|
tier,
|
2017-04-19 12:04:45 +00:00
|
|
|
"Got announce response: "
|
|
|
|
"connected:%d "
|
|
|
|
"timeout:%d "
|
|
|
|
"seeders:%d "
|
|
|
|
"leechers:%d "
|
|
|
|
"downloads:%d "
|
|
|
|
"interval:%d "
|
|
|
|
"min_interval:%d "
|
|
|
|
"tracker_id_str:%s "
|
|
|
|
"pex:%zu "
|
|
|
|
"pex6:%zu "
|
|
|
|
"err:%s "
|
|
|
|
"warn:%s",
|
|
|
|
(int)response->did_connect,
|
|
|
|
(int)response->did_timeout,
|
|
|
|
response->seeders,
|
|
|
|
response->leechers,
|
|
|
|
response->downloads,
|
|
|
|
response->interval,
|
|
|
|
response->min_interval,
|
2021-09-15 00:18:09 +00:00
|
|
|
response->tracker_id_str != nullptr ? response->tracker_id_str : "none",
|
2017-04-19 12:04:45 +00:00
|
|
|
response->pex_count,
|
|
|
|
response->pex6_count,
|
2021-09-15 00:18:09 +00:00
|
|
|
response->errmsg != nullptr ? response->errmsg : "none",
|
|
|
|
response->warning != nullptr ? response->warning : "none");
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-01 00:28:04 +00:00
|
|
|
tier->lastAnnounceTime = now;
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->lastAnnounceTimedOut = response->did_timeout;
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastAnnounceSucceeded = false;
|
|
|
|
tier->isAnnouncing = false;
|
2011-01-29 19:04:02 +00:00
|
|
|
tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
|
2010-02-01 00:28:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!response->did_connect)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
on_announce_error(tier, _("Could not connect to tracker"), event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (response->did_timeout)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
on_announce_error(tier, _("Tracker did not respond"), event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2021-09-15 00:18:09 +00:00
|
|
|
else if (response->errmsg != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-06-25 18:47:35 +00:00
|
|
|
/* If the torrent's only tracker returned an error, publish it.
|
|
|
|
Don't bother publishing if there are other trackers -- it's
|
|
|
|
all too common for people to load up dozens of dead trackers
|
|
|
|
in a torrent's metainfo... */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier->tor->info.trackerCount < 2)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
publishError(tier, response->errmsg);
|
|
|
|
}
|
2011-06-25 18:47:35 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
on_announce_error(tier, response->errmsg, event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
auto const isStopped = event == TR_ANNOUNCE_EVENT_STOPPED;
|
|
|
|
auto leechers = int{};
|
|
|
|
auto scrape_fields = int{};
|
|
|
|
auto seeders = int{};
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
publishErrorClear(tier);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
tr_tracker* const tracker = tier->currentTracker;
|
|
|
|
if (tracker != nullptr)
|
2011-03-31 03:37:50 +00:00
|
|
|
{
|
2011-03-14 16:52:47 +00:00
|
|
|
tracker->consecutiveFailures = 0;
|
2011-07-31 00:58:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response->seeders >= 0)
|
2011-07-31 00:58:05 +00:00
|
|
|
{
|
2011-10-14 00:27:14 +00:00
|
|
|
tracker->seederCount = seeders = response->seeders;
|
2011-10-09 00:18:18 +00:00
|
|
|
++scrape_fields;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response->leechers >= 0)
|
2011-10-09 00:18:18 +00:00
|
|
|
{
|
2011-10-14 00:27:14 +00:00
|
|
|
tracker->leecherCount = leechers = response->leechers;
|
2011-10-09 00:18:18 +00:00
|
|
|
++scrape_fields;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response->downloads >= 0)
|
2011-10-09 00:18:18 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
tracker->downloadCount = response->downloads;
|
2011-10-09 00:18:18 +00:00
|
|
|
++scrape_fields;
|
2011-07-31 00:58:05 +00:00
|
|
|
}
|
2011-03-31 03:37:50 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
if (response->tracker_id_str != nullptr)
|
2011-03-31 03:37:50 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tracker->tracker_id_str);
|
2021-10-14 14:22:28 +00:00
|
|
|
tracker->tracker_id_str = tr_strdup(response->tracker_id_str);
|
2011-03-31 03:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
char const* const str = response->warning;
|
|
|
|
if (str != nullptr)
|
2011-01-29 19:04:02 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(tier->lastAnnounceStr, str, sizeof(tier->lastAnnounceStr));
|
|
|
|
dbgmsg(tier, "tracker gave \"%s\"", str);
|
|
|
|
publishWarning(tier, str);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2011-10-09 00:21:36 +00:00
|
|
|
else
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(tier->lastAnnounceStr, _("Success"), sizeof(tier->lastAnnounceStr));
|
2011-10-09 00:21:36 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
if (response->min_interval != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
tier->announceMinIntervalSec = response->min_interval;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-10-14 14:22:28 +00:00
|
|
|
if (response->interval != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 14:22:28 +00:00
|
|
|
tier->announceIntervalSec = response->interval;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response->pex_count > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
publishPeersPex(tier, seeders, leechers, response->pex, response->pex_count);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response->pex6_count > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
publishPeersPex(tier, seeders, leechers, response->pex6, response->pex6_count);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-09-09 20:25:30 +00:00
|
|
|
publishPeerCounts(tier, seeders, leechers);
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->isRunning = data->isRunningOnSuccess;
|
2011-07-31 00:58:05 +00:00
|
|
|
|
2011-10-09 00:18:18 +00:00
|
|
|
/* if the tracker included scrape fields in its announce response,
|
|
|
|
then a separate scrape isn't needed */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (scrape_fields >= 3 || (scrape_fields >= 1 && tracker->scrape_info == nullptr))
|
2011-07-31 00:58:05 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddTorDbg(
|
|
|
|
tier->tor,
|
|
|
|
"Announce response contained scrape info; "
|
|
|
|
"rescheduling next scrape to %d seconds from now.",
|
|
|
|
tier->scrapeIntervalSec);
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time(announcer->session, tier, tier->scrapeIntervalSec);
|
2011-07-31 00:58:05 +00:00
|
|
|
tier->lastScrapeTime = now;
|
|
|
|
tier->lastScrapeSucceeded = true;
|
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (tier->lastScrapeTime + tier->scrapeIntervalSec <= now)
|
2011-07-31 00:58:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time(announcer->session, tier, 0);
|
2011-07-31 00:58:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastAnnounceSucceeded = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->lastAnnouncePeerCount = response->pex_count + response->pex6_count;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (isStopped)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-01-29 19:04:02 +00:00
|
|
|
/* now that we've successfully stopped the torrent,
|
|
|
|
* we can reset the up/down/corrupt count we've kept
|
|
|
|
* for this tracker */
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->byteCounts[TR_ANN_UP] = 0;
|
|
|
|
tier->byteCounts[TR_ANN_DOWN] = 0;
|
|
|
|
tier->byteCounts[TR_ANN_CORRUPT] = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (!isStopped && tier->announce_event_count == 0)
|
2010-02-10 02:59:15 +00:00
|
|
|
{
|
2011-01-29 19:04:02 +00:00
|
|
|
/* the queue is empty, so enqueue a perodic update */
|
2021-10-14 14:22:28 +00:00
|
|
|
int const i = tier->announceIntervalSec;
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tier, "Sending periodic reannounce in %d seconds", i);
|
|
|
|
tier_announce_event_push(tier, TR_ANNOUNCE_EVENT_NONE, now + i);
|
2011-01-29 19:04:02 +00:00
|
|
|
}
|
2010-02-10 02:59:15 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(data);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void announce_request_free(tr_announce_request* req)
|
2011-06-19 18:26:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(req->tracker_id_str);
|
|
|
|
tr_free(req);
|
2011-06-19 18:26:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static void announce_request_delegate(
|
|
|
|
tr_announcer* announcer,
|
|
|
|
tr_announce_request* request,
|
|
|
|
tr_announce_response_func callback,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* callback_data)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = announcer->session;
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2013-01-30 19:59:55 +00:00
|
|
|
#if 0
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
fprintf(stderr, "ANNOUNCE: event %s isPartialSeed %d port %d key %d numwant %d up %" PRIu64 " down %" PRIu64
|
|
|
|
" corrupt %" PRIu64 " left %" PRIu64 " url [%s] tracker_id_str [%s] peer_id [%20.20s]\n",
|
|
|
|
tr_announce_event_get_string(request->event), (int)request->partial_seed, (int)request->port, request->key,
|
|
|
|
request->numwant, request->up, request->down, request->corrupt, request->leftUntilComplete, request->url,
|
|
|
|
request->tracker_id_str, request->peer_id);
|
|
|
|
|
2013-01-30 19:59:55 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-01 23:11:57 +00:00
|
|
|
if (auto const announce_sv = tr_quark_get_string_view(request->announce_url);
|
|
|
|
tr_strvStartsWith(announce_sv, "http://"sv) || tr_strvStartsWith(announce_sv, "https://"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tracker_http_announce(session, request, callback, callback_data);
|
|
|
|
}
|
2021-11-10 02:42:18 +00:00
|
|
|
else if (tr_strvStartsWith(announce_sv, "udp://"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tracker_udp_announce(session, request, callback, callback_data);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
tr_logAddError("Unsupported url: %" TR_PRIsv, TR_PRIsv_ARG(announce_sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
announce_request_free(request);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tierAnnounce(tr_announcer* announcer, tr_tier* tier)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(!tier->isAnnouncing);
|
|
|
|
TR_ASSERT(tier->announce_event_count > 0);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
|
|
|
|
tr_torrent* tor = tier->tor;
|
|
|
|
tr_announce_event announce_event = tier_announce_event_pull(tier);
|
|
|
|
tr_announce_request* req = announce_request_new(announcer, tor, tier, announce_event);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct announce_data* data = tr_new0(struct announce_data, 1);
|
2011-03-26 12:06:04 +00:00
|
|
|
data->session = announcer->session;
|
2011-03-11 05:48:57 +00:00
|
|
|
data->tierId = tier->key;
|
|
|
|
data->isRunningOnSuccess = tor->isRunning;
|
|
|
|
data->timeSent = now;
|
|
|
|
data->event = announce_event;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->isAnnouncing = true;
|
2011-03-11 05:48:57 +00:00
|
|
|
tier->lastAnnounceStartTime = now;
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
announce_request_delegate(announcer, req, on_announce_done, data);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
/***
|
|
|
|
****
|
2011-03-11 04:19:01 +00:00
|
|
|
**** SCRAPE
|
|
|
|
****
|
2011-02-05 16:45:06 +00:00
|
|
|
***/
|
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
static constexpr bool multiscrape_too_big(std::string_view errmsg)
|
2019-02-16 20:19:38 +00:00
|
|
|
{
|
|
|
|
/* Found a tracker that returns some bespoke string for this case?
|
|
|
|
Add your patch here and open a PR */
|
2021-10-14 15:36:49 +00:00
|
|
|
auto constexpr TooLongErrors = std::array<std::string_view, 3>{
|
2019-02-16 20:19:38 +00:00
|
|
|
"Bad Request",
|
|
|
|
"GET string too long",
|
2021-08-15 09:41:48 +00:00
|
|
|
"Request-URI Too Long",
|
2019-02-16 20:19:38 +00:00
|
|
|
};
|
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
for (auto const& tle : TooLongErrors)
|
2019-02-16 20:19:38 +00:00
|
|
|
{
|
2021-11-10 02:42:18 +00:00
|
|
|
if (tr_strvContains(errmsg, tle))
|
2019-02-16 20:19:38 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
static void on_scrape_error(tr_session const* session, tr_tier* tier, char const* errmsg)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2011-03-14 16:52:47 +00:00
|
|
|
/* increment the error count */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier->currentTracker != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-14 16:52:47 +00:00
|
|
|
++tier->currentTracker->consecutiveFailures;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-14 16:52:47 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/* set the error message */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(tier->lastScrapeStr, errmsg, sizeof(tier->lastScrapeStr));
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/* switch to the next tracker */
|
2017-04-19 12:04:45 +00:00
|
|
|
tierIncrementTracker(tier);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/* schedule a rescrape */
|
2021-10-14 14:22:28 +00:00
|
|
|
int const interval = getRetryInterval(tier->currentTracker);
|
2021-11-02 23:00:01 +00:00
|
|
|
auto const* const key_cstr = tr_quark_get_string(tier->currentTracker->key);
|
|
|
|
dbgmsg(tier, "Tracker '%s' scrape error: %s (Retrying in %zu seconds)", key_cstr, errmsg, (size_t)interval);
|
|
|
|
tr_logAddTorInfo(tier->tor, "Tracker '%s' error: %s (Retrying in %zu seconds)", key_cstr, errmsg, (size_t)interval);
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastScrapeSucceeded = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time(session, tier, interval);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
static tr_tier* find_tier(tr_torrent* tor, tr_quark scrape_url)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_torrent_tiers* tt = tor->tiers;
|
2011-03-11 15:44:24 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
for (int i = 0; tt != nullptr && i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_tracker const* const tracker = tt->tiers[i].currentTracker;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
if (tracker != nullptr && tracker->scrape_info != nullptr && tracker->scrape_info->scrape_url == scrape_url)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
return &tt->tiers[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
static void checkMultiscrapeMax(tr_announcer* announcer, tr_scrape_response const* response)
|
|
|
|
{
|
|
|
|
if (!multiscrape_too_big(response->errmsg))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const& url = response->scrape_url;
|
|
|
|
struct tr_scrape_info* const scrape_info = tr_announcerGetScrapeInfo(announcer, url);
|
|
|
|
if (scrape_info == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lower the max only if it hasn't already lowered for a similar
|
|
|
|
// error. So if N parallel multiscrapes all have the same `max`
|
|
|
|
// and error out, lower the value once for that batch, not N times.
|
|
|
|
int& multiscrape_max = scrape_info->multiscrape_max;
|
|
|
|
if (multiscrape_max < response->row_count)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int const n = std::max(1, int{ multiscrape_max - TrMultiscrapeStep });
|
|
|
|
if (multiscrape_max != n)
|
|
|
|
{
|
|
|
|
// don't log the full URL, since that might have a personal announce id
|
|
|
|
// (note: we know 'parsed' will be successful since this url has a scrape_info)
|
|
|
|
auto const parsed = *tr_urlParse(tr_quark_get_string_view(url));
|
|
|
|
auto clean_url = std::string{};
|
|
|
|
tr_buildBuf(clean_url, parsed.scheme, "://"sv, parsed.host, ":"sv, parsed.portstr);
|
|
|
|
tr_logAddNamedInfo(clean_url.c_str(), "Reducing multiscrape max to %d", n);
|
|
|
|
multiscrape_max = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_scrape_done(tr_scrape_response const* response, void* vsession)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announcer* announcer = session->announcer;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < response->row_count; ++i)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct tr_scrape_response_row const* row = &response->rows[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor = tr_torrentFindFromHash(session, row->info_hash);
|
2009-11-10 17:03:23 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tor != nullptr)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
tr_tier* tier = find_tier(tor, response->scrape_url);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tier != nullptr)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
auto const scrape_url_sv = tr_quark_get_string_view(response->scrape_url);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
dbgmsg(
|
|
|
|
tier,
|
2021-11-02 23:00:01 +00:00
|
|
|
"scraped url:%" TR_PRIsv
|
|
|
|
" -- "
|
2017-04-19 12:04:45 +00:00
|
|
|
"did_connect:%d "
|
|
|
|
"did_timeout:%d "
|
|
|
|
"seeders:%d "
|
|
|
|
"leechers:%d "
|
|
|
|
"downloads:%d "
|
|
|
|
"downloaders:%d "
|
|
|
|
"min_request_interval:%d "
|
|
|
|
"err:%s ",
|
2021-11-02 23:00:01 +00:00
|
|
|
TR_PRIsv_ARG(scrape_url_sv),
|
2017-04-19 12:04:45 +00:00
|
|
|
(int)response->did_connect,
|
|
|
|
(int)response->did_timeout,
|
|
|
|
row->seeders,
|
|
|
|
row->leechers,
|
|
|
|
row->downloads,
|
|
|
|
row->downloaders,
|
|
|
|
response->min_request_interval,
|
2021-09-30 21:33:31 +00:00
|
|
|
std::empty(response->errmsg) ? "none" : response->errmsg.c_str());
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->isScraping = false;
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->lastScrapeTime = now;
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastScrapeSucceeded = false;
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->lastScrapeTimedOut = response->did_timeout;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!response->did_connect)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
on_scrape_error(session, tier, _("Could not connect to tracker"));
|
2015-01-02 11:15:31 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (response->did_timeout)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
on_scrape_error(session, tier, _("Tracker did not respond"));
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2021-09-30 21:33:31 +00:00
|
|
|
else if (!std::empty(response->errmsg))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2021-09-30 21:33:31 +00:00
|
|
|
on_scrape_error(session, tier, response->errmsg.c_str());
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastScrapeSucceeded = true;
|
2021-10-21 00:39:05 +00:00
|
|
|
tier->scrapeIntervalSec = std::max(int{ DefaultScrapeIntervalSec }, response->min_request_interval);
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time(session, tier, tier->scrapeIntervalSec);
|
|
|
|
tr_logAddTorDbg(tier->tor, "Scrape successful. Rescraping in %d seconds.", tier->scrapeIntervalSec);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
tr_tracker* const tracker = tier->currentTracker;
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tracker != nullptr)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (row->seeders >= 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-10-14 00:27:14 +00:00
|
|
|
tracker->seederCount = row->seeders;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (row->leechers >= 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-10-14 00:27:14 +00:00
|
|
|
tracker->leecherCount = row->leechers;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (row->downloads >= 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-10-14 00:27:14 +00:00
|
|
|
tracker->downloadCount = row->downloads;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
tracker->downloaderCount = row->downloaders;
|
2011-03-14 16:52:47 +00:00
|
|
|
tracker->consecutiveFailures = 0;
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2021-09-09 20:25:30 +00:00
|
|
|
|
|
|
|
if (row->seeders >= 0 && row->leechers >= 0 && row->downloads >= 0)
|
|
|
|
{
|
|
|
|
publishPeerCounts(tier, row->seeders, row->leechers);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
checkMultiscrapeMax(announcer, response);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static void scrape_request_delegate(
|
|
|
|
tr_announcer* announcer,
|
|
|
|
tr_scrape_request const* request,
|
|
|
|
tr_scrape_response_func callback,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* callback_data)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = announcer->session;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
auto const scrape_sv = tr_quark_get_string_view(request->scrape_url);
|
|
|
|
|
2021-11-10 02:42:18 +00:00
|
|
|
if (tr_strvStartsWith(scrape_sv, "http://"sv) || tr_strvStartsWith(scrape_sv, "https://"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tracker_http_scrape(session, request, callback, callback_data);
|
|
|
|
}
|
2021-11-10 02:42:18 +00:00
|
|
|
else if (tr_strvStartsWith(scrape_sv, "udp://"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tracker_udp_scrape(session, request, callback, callback_data);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
tr_logAddError("Unsupported url: %" TR_PRIsv, TR_PRIsv_ARG(scrape_sv));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:32:07 +00:00
|
|
|
static void multiscrape(tr_announcer* announcer, std::vector<tr_tier*> const& tiers)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-10 00:28:22 +00:00
|
|
|
size_t request_count = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2021-10-21 00:39:05 +00:00
|
|
|
tr_scrape_request requests[MaxScrapesPerUpkeep] = {};
|
2011-03-11 15:11:37 +00:00
|
|
|
|
|
|
|
/* batch as many info_hashes into a request as we can */
|
2021-09-15 14:32:07 +00:00
|
|
|
for (auto* tier : tiers)
|
2011-03-11 15:11:37 +00:00
|
|
|
{
|
2019-02-16 20:19:38 +00:00
|
|
|
struct tr_scrape_info* const scrape_info = tier->currentTracker->scrape_info;
|
2017-05-13 22:38:31 +00:00
|
|
|
bool found = false;
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(scrape_info != nullptr);
|
2017-07-04 18:22:26 +00:00
|
|
|
|
2011-03-11 15:11:37 +00:00
|
|
|
/* if there's a request with this scrape URL and a free slot, use it */
|
2021-09-10 00:28:22 +00:00
|
|
|
for (size_t j = 0; !found && j < request_count; ++j)
|
2011-03-11 15:11:37 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_scrape_request* req = &requests[j];
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2019-02-16 20:19:38 +00:00
|
|
|
if (req->info_hash_count >= scrape_info->multiscrape_max)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 15:11:37 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:00:01 +00:00
|
|
|
if (scrape_info->scrape_url != req->scrape_url)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 15:11:37 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2021-11-25 22:39:19 +00:00
|
|
|
req->info_hash[req->info_hash_count] = tr_torrentInfoHash(tier->tor);
|
|
|
|
++req->info_hash_count;
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->isScraping = true;
|
2011-03-11 15:11:37 +00:00
|
|
|
tier->lastScrapeStartTime = now;
|
2017-05-13 22:38:31 +00:00
|
|
|
found = true;
|
2011-03-11 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise, if there's room for another request, build a new one */
|
2021-10-21 00:39:05 +00:00
|
|
|
if (!found && request_count < MaxScrapesPerUpkeep)
|
2011-03-11 15:11:37 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_scrape_request* req = &requests[request_count++];
|
2021-11-02 23:00:01 +00:00
|
|
|
req->scrape_url = scrape_info->scrape_url;
|
2017-04-19 12:04:45 +00:00
|
|
|
tier_build_log_name(tier, req->log_name, sizeof(req->log_name));
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2021-11-25 22:39:19 +00:00
|
|
|
req->info_hash[req->info_hash_count] = tr_torrentInfoHash(tier->tor);
|
|
|
|
++req->info_hash_count;
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->isScraping = true;
|
2011-03-11 15:11:37 +00:00
|
|
|
tier->lastScrapeStartTime = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the requests we just built */
|
2021-09-10 00:28:22 +00:00
|
|
|
for (size_t i = 0; i < request_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
scrape_request_delegate(announcer, &requests[i], on_scrape_done, announcer->session);
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void flushCloseMessages(tr_announcer* announcer)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
auto& stops = announcer->stops;
|
|
|
|
std::for_each(
|
|
|
|
std::begin(stops),
|
|
|
|
std::end(stops),
|
|
|
|
[&announcer](auto* stop) { announce_request_delegate(announcer, stop, nullptr, nullptr); });
|
|
|
|
stops.clear();
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
static constexpr bool tierNeedsToAnnounce(tr_tier const* tier, time_t const now)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return !tier->isAnnouncing && !tier->isScraping && tier->announceAt != 0 && tier->announceAt <= now &&
|
2019-02-10 11:05:16 +00:00
|
|
|
tier->announce_event_count > 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
static constexpr bool tierNeedsToScrape(tr_tier const* tier, time_t const now)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return !tier->isScraping && tier->scrapeAt != 0 && tier->scrapeAt <= now && tier->currentTracker != nullptr &&
|
|
|
|
tier->currentTracker->scrape_info != nullptr;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
static constexpr int countDownloaders(tr_tier const* tier)
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2021-09-10 00:28:22 +00:00
|
|
|
tr_tracker const* const tracker = tier->currentTracker;
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return tracker == nullptr ? 0 : tracker->downloaderCount + tracker->leecherCount;
|
2021-09-10 00:28:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 14:32:07 +00:00
|
|
|
static int compareAnnounceTiers(tr_tier const* a, tr_tier const* b)
|
2021-09-10 00:28:22 +00:00
|
|
|
{
|
|
|
|
/* prefer higher-priority events */
|
|
|
|
int const priority_a = a->announce_event_priority;
|
|
|
|
int const priority_b = b->announce_event_priority;
|
|
|
|
if (priority_a != priority_b)
|
|
|
|
{
|
|
|
|
return priority_a > priority_b ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prefer swarms where we might upload */
|
|
|
|
int const downloader_count_a = countDownloaders(a);
|
|
|
|
int const downloader_count_b = countDownloaders(b);
|
|
|
|
if (downloader_count_a != downloader_count_b)
|
|
|
|
{
|
|
|
|
return downloader_count_a > downloader_count_b ? -1 : 1;
|
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
/* prefer swarms where we might download */
|
|
|
|
bool const is_seed_a = tr_torrentIsSeed(a->tor);
|
|
|
|
bool const is_seed_b = tr_torrentIsSeed(b->tor);
|
|
|
|
if (is_seed_a != is_seed_b)
|
|
|
|
{
|
|
|
|
return is_seed_a ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prefer larger stats, to help ensure stats get recorded when stopping on shutdown */
|
2021-09-29 23:42:50 +00:00
|
|
|
auto const xa = a->byteCounts[TR_ANN_UP] + a->byteCounts[TR_ANN_DOWN];
|
|
|
|
auto const xb = b->byteCounts[TR_ANN_UP] + b->byteCounts[TR_ANN_DOWN];
|
|
|
|
if (xa != xb)
|
2021-09-10 00:28:22 +00:00
|
|
|
{
|
2021-09-29 23:42:50 +00:00
|
|
|
return xa > xb ? -1 : 1;
|
2021-09-10 00:28:22 +00:00
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
// announcements that have been waiting longer go first
|
|
|
|
if (a->announceAt != b->announceAt)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-10 00:28:22 +00:00
|
|
|
return a->announceAt < b->announceAt ? -1 : 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2021-09-11 13:11:46 +00:00
|
|
|
// the tiers are effectively equal priority, but add an arbitrary
|
|
|
|
// differentiation because ptrArray sorted mode hates equal items.
|
|
|
|
return a < b ? -1 : 1;
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
static void scrapeAndAnnounceMore(tr_announcer* announcer)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* build a list of tiers that need to be announced */
|
2021-09-15 14:32:07 +00:00
|
|
|
auto announce_me = std::vector<tr_tier*>{};
|
|
|
|
auto scrape_me = std::vector<tr_tier*>{};
|
2021-09-24 23:31:02 +00:00
|
|
|
for (auto* tor : announcer->session->torrents)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
struct tr_torrent_tiers* tt = tor->tiers;
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
for (int i = 0; tt != nullptr && i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tier* tier = &tt->tiers[i];
|
|
|
|
|
|
|
|
if (tierNeedsToAnnounce(tier, now))
|
|
|
|
{
|
2021-09-15 14:32:07 +00:00
|
|
|
announce_me.push_back(tier);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-09-10 00:28:22 +00:00
|
|
|
|
|
|
|
if (tierNeedsToScrape(tier, now))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 14:32:07 +00:00
|
|
|
scrape_me.push_back(tier);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
/* First, scrape what we can. We handle scrapes first because
|
|
|
|
* we can work through that queue much faster than announces
|
|
|
|
* (thanks to multiscrape) _and_ the scrape responses will tell
|
|
|
|
* us which swarms are interesting and should be announced next. */
|
2021-09-15 14:32:07 +00:00
|
|
|
multiscrape(announcer, scrape_me);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
/* Second, announce what we can. If there aren't enough slots
|
|
|
|
* available, use compareAnnounceTiers to prioritize. */
|
2021-10-21 00:39:05 +00:00
|
|
|
if (announce_me.size() > MaxAnnouncesPerUpkeep)
|
2021-09-15 14:32:07 +00:00
|
|
|
{
|
|
|
|
std::partial_sort(
|
|
|
|
std::begin(announce_me),
|
2021-10-21 00:39:05 +00:00
|
|
|
std::begin(announce_me) + MaxAnnouncesPerUpkeep,
|
2021-09-15 14:32:07 +00:00
|
|
|
std::end(announce_me),
|
|
|
|
[](auto const* a, auto const* b) { return compareAnnounceTiers(a, b) < 0; });
|
2021-10-21 00:39:05 +00:00
|
|
|
announce_me.resize(MaxAnnouncesPerUpkeep);
|
2021-09-15 14:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto*& tier : announce_me)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_logAddTorDbg(tier->tor, "%s", "Announcing to tracker");
|
|
|
|
tierAnnounce(announcer, tier);
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-12 23:16:51 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void onUpkeepTimer(evutil_socket_t /*fd*/, short /*what*/, void* vannouncer)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* announcer = static_cast<tr_announcer*>(vannouncer);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = announcer->session;
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = session->unique_lock();
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const is_closing = session->isClosed;
|
|
|
|
time_t const now = tr_time();
|
2011-03-17 18:51:31 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
/* maybe send out some "stopped" messages for closed torrents */
|
2017-04-19 12:04:45 +00:00
|
|
|
flushCloseMessages(announcer);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2021-09-10 00:28:22 +00:00
|
|
|
/* maybe kick off some scrapes / announces whose time has come */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!is_closing)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-10 00:28:22 +00:00
|
|
|
scrapeAndAnnounceMore(announcer);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-13 06:38:54 +00:00
|
|
|
/* TAU upkeep */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (announcer->tauUpkeepAt <= now)
|
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
announcer->tauUpkeepAt = now + TauUpkeepIntervalSecs;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker_udp_upkeep(session);
|
2011-03-13 06:38:54 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
/* set up the next timer */
|
2021-10-21 00:39:05 +00:00
|
|
|
tr_timerAddMsec(announcer->upkeepTimer, UpkeepIntervalMsec);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_tracker_stat* tr_announcerStats(tr_torrent const* torrent, int* setmeTrackerCount)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tr_isTorrent(torrent));
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
|
|
|
|
int out = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
struct tr_torrent_tiers const* const tt = torrent->tiers;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* alloc the stats */
|
2011-03-31 20:26:43 +00:00
|
|
|
*setmeTrackerCount = tt->tracker_count;
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_tracker_stat* const ret = tr_new0(tr_tracker_stat, tt->tracker_count);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* populate the stats */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tt->tier_count; ++i)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_tier const* const tier = &tt->tiers[i];
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int j = 0; j < tier->tracker_count; ++j)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_tracker const* const tracker = &tier->trackers[j];
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker_stat* st = &ret[out++];
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-01-13 22:46:22 +00:00
|
|
|
st->id = tracker->id;
|
2021-11-02 23:00:01 +00:00
|
|
|
st->host = tr_quark_get_string(tracker->key);
|
|
|
|
st->announce = tr_quark_get_string(tracker->announce_url);
|
2010-02-14 04:48:12 +00:00
|
|
|
st->tier = i;
|
2009-10-10 00:17:00 +00:00
|
|
|
st->isBackup = tracker != tier->currentTracker;
|
2009-09-25 21:05:59 +00:00
|
|
|
st->lastScrapeStartTime = tier->lastScrapeStartTime;
|
2021-11-02 23:00:01 +00:00
|
|
|
st->scrape = tracker->scrape_info == nullptr ? "" : tr_quark_get_string(tracker->scrape_info->scrape_url);
|
2009-11-21 03:13:27 +00:00
|
|
|
st->seederCount = tracker->seederCount;
|
|
|
|
st->leecherCount = tracker->leecherCount;
|
|
|
|
st->downloadCount = tracker->downloadCount;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (st->isBackup)
|
2009-09-30 20:49:52 +00:00
|
|
|
{
|
2009-10-09 21:30:34 +00:00
|
|
|
st->scrapeState = TR_TRACKER_INACTIVE;
|
|
|
|
st->announceState = TR_TRACKER_INACTIVE;
|
2009-09-30 20:49:52 +00:00
|
|
|
st->nextScrapeTime = 0;
|
|
|
|
st->nextAnnounceTime = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2009-09-30 20:49:52 +00:00
|
|
|
else
|
|
|
|
{
|
2021-11-16 19:25:30 +00:00
|
|
|
st->hasScraped = tier->lastScrapeTime;
|
|
|
|
if (st->hasScraped != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-09-30 20:49:52 +00:00
|
|
|
st->lastScrapeTime = tier->lastScrapeTime;
|
|
|
|
st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
|
2010-02-25 23:06:05 +00:00
|
|
|
st->lastScrapeTimedOut = tier->lastScrapeTimedOut;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(st->lastScrapeResult, tier->lastScrapeStr, sizeof(st->lastScrapeResult));
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2009-09-30 20:49:52 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier->isScraping)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-10-09 21:30:34 +00:00
|
|
|
st->scrapeState = TR_TRACKER_ACTIVE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (tier->scrapeAt == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-10-28 04:53:39 +00:00
|
|
|
st->scrapeState = TR_TRACKER_INACTIVE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (tier->scrapeAt > now)
|
2009-10-10 20:25:05 +00:00
|
|
|
{
|
2009-10-09 21:30:34 +00:00
|
|
|
st->scrapeState = TR_TRACKER_WAITING;
|
2009-11-10 17:03:23 +00:00
|
|
|
st->nextScrapeTime = tier->scrapeAt;
|
2009-10-10 20:25:05 +00:00
|
|
|
}
|
2009-10-10 20:14:26 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-10-10 20:14:26 +00:00
|
|
|
st->scrapeState = TR_TRACKER_QUEUED;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-30 20:49:52 +00:00
|
|
|
|
|
|
|
st->lastAnnounceStartTime = tier->lastAnnounceStartTime;
|
|
|
|
|
2021-11-16 19:25:30 +00:00
|
|
|
st->hasAnnounced = tier->lastAnnounceTime;
|
|
|
|
if (st->hasAnnounced != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-09-30 20:49:52 +00:00
|
|
|
st->lastAnnounceTime = tier->lastAnnounceTime;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(st->lastAnnounceResult, tier->lastAnnounceStr, sizeof(st->lastAnnounceResult));
|
2010-02-01 00:35:38 +00:00
|
|
|
st->lastAnnounceSucceeded = tier->lastAnnounceSucceeded;
|
2010-02-05 05:16:18 +00:00
|
|
|
st->lastAnnounceTimedOut = tier->lastAnnounceTimedOut;
|
2010-02-01 00:35:38 +00:00
|
|
|
st->lastAnnouncePeerCount = tier->lastAnnouncePeerCount;
|
2009-09-30 20:49:52 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier->isAnnouncing)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-10-09 21:30:34 +00:00
|
|
|
st->announceState = TR_TRACKER_ACTIVE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (!torrent->isRunning || tier->announceAt == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-10-09 21:30:34 +00:00
|
|
|
st->announceState = TR_TRACKER_INACTIVE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (tier->announceAt > now)
|
2009-10-10 20:25:05 +00:00
|
|
|
{
|
2009-10-09 21:30:34 +00:00
|
|
|
st->announceState = TR_TRACKER_WAITING;
|
2009-11-10 17:03:23 +00:00
|
|
|
st->nextAnnounceTime = tier->announceAt;
|
2009-10-10 20:25:05 +00:00
|
|
|
}
|
2009-10-10 20:14:26 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2009-10-10 20:14:26 +00:00
|
|
|
st->announceState = TR_TRACKER_QUEUED;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
void tr_announcerStatsFree(tr_tracker_stat* trackers, int /*trackerCount*/)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(trackers);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void copy_tier_attributes_impl(struct tr_tier* tgt, int trackerIndex, tr_tier const* src)
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
/* sanity clause */
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(trackerIndex < tgt->tracker_count);
|
2021-11-02 23:00:01 +00:00
|
|
|
TR_ASSERT(tgt->trackers[trackerIndex].announce_url == src->currentTracker->announce_url);
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
tr_tier const keep = *tgt;
|
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* bitwise copy will handle most of tr_tier's fields... */
|
|
|
|
*tgt = *src;
|
|
|
|
|
|
|
|
/* ...fix the fields that can't be cleanly bitwise-copied */
|
|
|
|
tgt->wasCopied = true;
|
|
|
|
tgt->trackers = keep.trackers;
|
2011-04-11 19:44:16 +00:00
|
|
|
tgt->tracker_count = keep.tracker_count;
|
2021-09-12 17:41:49 +00:00
|
|
|
tgt->announce_events = static_cast<tr_announce_event*>(
|
|
|
|
tr_memdup(src->announce_events, sizeof(tr_announce_event) * src->announce_event_count));
|
2021-09-10 00:28:22 +00:00
|
|
|
tgt->announce_event_priority = src->announce_event_priority;
|
2011-03-31 20:26:43 +00:00
|
|
|
tgt->announce_event_count = src->announce_event_count;
|
|
|
|
tgt->announce_event_alloc = src->announce_event_count;
|
|
|
|
tgt->currentTrackerIndex = trackerIndex;
|
|
|
|
tgt->currentTracker = &tgt->trackers[trackerIndex];
|
|
|
|
tgt->currentTracker->seederCount = src->currentTracker->seederCount;
|
|
|
|
tgt->currentTracker->leecherCount = src->currentTracker->leecherCount;
|
|
|
|
tgt->currentTracker->downloadCount = src->currentTracker->downloadCount;
|
|
|
|
tgt->currentTracker->downloaderCount = src->currentTracker->downloaderCount;
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void copy_tier_attributes(struct tr_torrent_tiers* tt, tr_tier const* src)
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
bool found = false;
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* find a tier (if any) which has a match for src->currentTracker */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; !found && i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int j = 0; !found && j < tt->tiers[i].tracker_count; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-02 23:00:01 +00:00
|
|
|
if (src->currentTracker->announce_url == tt->tiers[i].trackers[j].announce_url)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-08-27 23:41:26 +00:00
|
|
|
found = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
copy_tier_attributes_impl(&tt->tiers[i], j, src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
void tr_announcerResetTorrent(tr_announcer* /*announcer*/, tr_torrent* tor)
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(tor->tiers != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_torrent_tiers* tt = tor->tiers;
|
2011-03-31 20:26:43 +00:00
|
|
|
tr_torrent_tiers old = *tt;
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* remove the old tiers / trackers */
|
2021-09-15 00:18:09 +00:00
|
|
|
tt->tiers = nullptr;
|
|
|
|
tt->trackers = nullptr;
|
2011-03-31 20:26:43 +00:00
|
|
|
tt->tier_count = 0;
|
|
|
|
tt->tracker_count = 0;
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* create the new tiers / trackers */
|
2017-04-19 12:04:45 +00:00
|
|
|
addTorrentToTier(tt, tor);
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* copy the old tiers' states into their replacements */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < old.tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (old.tiers[i].currentTracker != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
copy_tier_attributes(tt, &old.tiers[i]);
|
|
|
|
}
|
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
|
|
|
/* kickstart any tiers that didn't get started */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor->isRunning)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!tt->tiers[i].wasCopied)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tier_announce_event_push(&tt->tiers[i], TR_ANNOUNCE_EVENT_STARTED, now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2017-04-19 12:04:45 +00:00
|
|
|
tiersDestruct(&old);
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|