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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-03-24 21:49:42 +00:00
|
|
|
#include <limits.h> /* INT_MAX */
|
2011-03-16 18:04:23 +00:00
|
|
|
#include <stdio.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <stdlib.h> /* qsort() */
|
|
|
|
#include <string.h> /* strcmp(), memcpy(), strncmp() */
|
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 */
|
|
|
|
|
2015-05-04 19:58:34 +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 "ptrarray.h"
|
|
|
|
#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"
|
|
|
|
|
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
|
|
|
} \
|
|
|
|
} \
|
2017-04-19 12:04:45 +00:00
|
|
|
while (0)
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* unless the tracker says otherwise, rescrape this frequently */
|
2012-12-05 17:29:46 +00:00
|
|
|
DEFAULT_SCRAPE_INTERVAL_SEC = (60 * 30),
|
2009-09-25 21:05:59 +00:00
|
|
|
/* unless the tracker says otherwise, this is the announce interval */
|
2012-12-05 17:29:46 +00:00
|
|
|
DEFAULT_ANNOUNCE_INTERVAL_SEC = (60 * 10),
|
2009-09-25 21:05:59 +00:00
|
|
|
/* unless the tracker says otherwise, this is the announce min_interval */
|
2012-12-05 17:29:46 +00:00
|
|
|
DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = (60 * 2),
|
2010-02-05 01:16:30 +00:00
|
|
|
/* how many web tasks we allow at one time */
|
|
|
|
MAX_CONCURRENT_TASKS = 48,
|
2009-09-25 21:05:59 +00:00
|
|
|
/* the value of the 'numwant' argument passed in tracker requests. */
|
2010-05-28 01:41:30 +00:00
|
|
|
NUMWANT = 80,
|
2017-04-19 12:04:45 +00:00
|
|
|
/* */
|
2010-05-01 16:04:00 +00:00
|
|
|
UPKEEP_INTERVAL_SECS = 1,
|
2011-03-13 06:38:54 +00:00
|
|
|
/* this is how often to call the UDP tracker upkeep */
|
|
|
|
TAU_UPKEEP_INTERVAL_SECS = 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
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int compareTransfer(uint64_t a_uploaded, uint64_t a_downloaded, uint64_t b_uploaded, uint64_t b_downloaded)
|
2011-02-05 18:46:10 +00:00
|
|
|
{
|
|
|
|
/* higher upload count goes first */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (a_uploaded != b_uploaded)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-05 18:46:10 +00:00
|
|
|
return a_uploaded > b_uploaded ? -1 : 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-05 18:46:10 +00:00
|
|
|
|
|
|
|
/* then higher download count goes first */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (a_downloaded != b_downloaded)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-05 18:46:10 +00:00
|
|
|
return a_downloaded > b_downloaded ? -1 : 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-02-05 18:46:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-19 18:26:04 +00:00
|
|
|
/**
|
|
|
|
* Comparison function for tr_announce_requests.
|
|
|
|
*
|
|
|
|
* The primary key (amount of data transferred) is used to prioritize
|
|
|
|
* tracker announcements of active torrents. The remaining keys are
|
|
|
|
* used to satisfy the uniqueness requirement of a sorted tr_ptrArray.
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
static int compareStops(void const* va, void const* vb)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-06-19 18:26:04 +00:00
|
|
|
int i;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_announce_request const* a = va;
|
|
|
|
tr_announce_request const* b = vb;
|
2011-06-19 18:26:04 +00:00
|
|
|
|
|
|
|
/* primary key: volume of data transferred. */
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((i = compareTransfer(a->up, a->down, b->up, b->down)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-06-19 18:26:04 +00:00
|
|
|
return i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-06-19 18:26:04 +00:00
|
|
|
|
|
|
|
/* secondary key: the torrent's info_hash */
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((i = memcmp(a->info_hash, b->info_hash, SHA_DIGEST_LENGTH)) != 0)
|
|
|
|
{
|
2011-06-19 18:26:04 +00:00
|
|
|
return i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-06-19 18:26:04 +00:00
|
|
|
|
|
|
|
/* tertiary key: the tracker's announec url */
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_strcmp0(a->url, b->url);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "global" (per-tr_session) fields
|
|
|
|
*/
|
|
|
|
typedef struct tr_announcer
|
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
tr_ptrArray stops; /* tr_announce_request */
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session;
|
|
|
|
struct event* upkeepTimer;
|
2010-02-05 01:16:30 +00:00
|
|
|
int slotsAvailable;
|
2011-03-10 11:46:22 +00:00
|
|
|
int key;
|
2011-03-13 06:38:54 +00:00
|
|
|
time_t tauUpkeepAt;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
tr_announcer;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_announcerHasBacklog(struct tr_announcer const* announcer)
|
2009-12-02 05:30:46 +00:00
|
|
|
{
|
2010-02-05 01:16:30 +00:00
|
|
|
return announcer->slotsAvailable < 1;
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void onUpkeepTimer(evutil_socket_t foo UNUSED, short bar UNUSED, 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
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
tr_announcer* a = tr_new0(tr_announcer, 1);
|
2009-09-25 21:05:59 +00:00
|
|
|
a->stops = TR_PTR_ARRAY_INIT;
|
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;
|
2010-02-05 01:16:30 +00:00
|
|
|
a->slotsAvailable = MAX_CONCURRENT_TASKS;
|
2017-04-19 12:04:45 +00:00
|
|
|
a->upkeepTimer = evtimer_new(session->event_base, onUpkeepTimer, a);
|
|
|
|
tr_timerAdd(a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0);
|
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);
|
2009-09-25 21:05:59 +00:00
|
|
|
announcer->upkeepTimer = NULL;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayDestruct(&announcer->stops, NULL);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
session->announcer = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(announcer);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* a row in tr_tier's list of trackers */
|
|
|
|
typedef struct
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* key;
|
|
|
|
char* announce;
|
|
|
|
char* scrape;
|
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;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_tracker;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* format: host+':'+ port */
|
2017-04-20 16:02:19 +00:00
|
|
|
static char* getKey(char const* url)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* ret;
|
|
|
|
char* scheme = NULL;
|
|
|
|
char* host = NULL;
|
2011-03-15 17:03:09 +00:00
|
|
|
int port = 0;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_urlParse(url, TR_BAD_SIZE, &scheme, &host, &port, NULL);
|
2017-04-30 16:25:26 +00:00
|
|
|
ret = tr_strdup_printf("%s://%s:%d", scheme != NULL ? scheme : "invalid", host != NULL ? host : "invalid", port);
|
2011-03-15 17:03:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(host);
|
|
|
|
tr_free(scheme);
|
2011-03-11 04:19:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void trackerConstruct(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));
|
|
|
|
tracker->key = getKey(inf->announce);
|
|
|
|
tracker->announce = tr_strdup(inf->announce);
|
|
|
|
tracker->scrape = tr_strdup(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);
|
|
|
|
tr_free(tracker->scrape);
|
|
|
|
tr_free(tracker->announce);
|
|
|
|
tr_free(tracker->key);
|
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 */
|
2011-03-11 04:19:01 +00:00
|
|
|
typedef 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;
|
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];
|
|
|
|
}
|
|
|
|
tr_tier;
|
|
|
|
|
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-04-10 03:57:37 +00:00
|
|
|
time_t ret;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
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
|
|
|
{
|
2011-07-23 17:14:12 +00:00
|
|
|
ret = 0;
|
2017-04-19 12:04:45 +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. */
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
|
|
|
{
|
2011-07-23 17:14:12 +00:00
|
|
|
ret = now + interval;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (ret % 10 != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-07-23 17:14:12 +00:00
|
|
|
++ret;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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;
|
|
|
|
tier->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
|
|
|
|
tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
|
|
|
|
tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time(tor->session, tier, tr_rand_int_weak(180));
|
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
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
tr_snprintf(buf, buflen, "[%s---%s]", (tier != NULL && tier->tor != NULL) ? tr_torrentName(tier->tor) : "?",
|
|
|
|
(tier != NULL && tier->currentTracker != NULL) ? tier->currentTracker->key : "?");
|
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 */
|
2017-04-30 16:25:26 +00:00
|
|
|
int const i = tier->currentTracker == NULL ? 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 */
|
|
|
|
tier->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
|
|
|
|
tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
|
|
|
|
tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
|
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
|
|
|
|
*/
|
|
|
|
typedef struct tr_torrent_tiers
|
|
|
|
{
|
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;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
tr_torrent_tiers;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static tr_tier* getTier(tr_announcer* announcer, uint8_t const* info_hash, int tierId)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tier* tier = NULL;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (announcer != NULL)
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tor != NULL && tor->tiers != NULL)
|
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
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; tier == NULL && 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
|
|
|
|
***/
|
|
|
|
|
2017-06-24 10:17:04 +00:00
|
|
|
static tr_tracker_event const TRACKER_EVENT_INIT =
|
|
|
|
{
|
|
|
|
.messageType = TR_TRACKER_WARNING,
|
|
|
|
.text = NULL,
|
|
|
|
.tracker = NULL,
|
|
|
|
.pex = NULL,
|
|
|
|
.pexCount = 0,
|
|
|
|
.seedProbability = 0
|
|
|
|
};
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void publishMessage(tr_tier* tier, char const* msg, int type)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tier != NULL && tier->tor != NULL && tier->tor->tiers != NULL && tier->tor->tiers->callback != NULL)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent_tiers* tiers = tier->tor->tiers;
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_tracker_event event = TRACKER_EVENT_INIT;
|
2009-09-25 21:05:59 +00:00
|
|
|
event.messageType = type;
|
|
|
|
event.text = msg;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tier->currentTracker != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
event.tracker = tier->currentTracker->announce;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
publishMessage(tier, NULL, 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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int8_t getSeedProbability(tr_tier* tier, int seeds, int leechers, int pex_count)
|
2010-05-18 15:14:54 +00:00
|
|
|
{
|
2011-03-11 04:57:53 +00:00
|
|
|
/* special case optimization:
|
|
|
|
ocelot omits seeds from peer lists sent to seeds on private trackers.
|
|
|
|
so check for that case... */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (leechers == pex_count && tr_torrentIsPrivate(tier->tor) && tr_torrentIsSeed(tier->tor) && seeds + leechers < NUMWANT)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:57:53 +00:00
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:57:53 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (seeds >= 0 && leechers >= 0 && seeds + leechers > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
return (int8_t)(100.0 * seeds / (seeds + leechers));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-05-18 15:14:54 +00:00
|
|
|
|
|
|
|
return -1; /* unknown */
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void publishPeersPex(tr_tier* tier, int seeds, int leechers, tr_pex const* pex, int n)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tier->tor->tiers->callback != NULL)
|
2011-03-14 02:40:39 +00:00
|
|
|
{
|
|
|
|
tr_tracker_event e = TRACKER_EVENT_INIT;
|
|
|
|
e.messageType = TR_TRACKER_PEERS;
|
2017-04-19 12:04:45 +00:00
|
|
|
e.seedProbability = getSeedProbability(tier, seeds, leechers, n);
|
2011-03-14 02:40:39 +00:00
|
|
|
e.pex = pex;
|
|
|
|
e.pexCount = n;
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tier, "got %d peers; seed prob %d", n, (int)e.seedProbability);
|
2011-03-11 04:57:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
(*tier->tor->tiers->callback)(tier->tor, &e, NULL);
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-03-15 03:21:25 +00:00
|
|
|
struct ann_tracker_info
|
|
|
|
{
|
|
|
|
tr_tracker_info info;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* scheme;
|
|
|
|
char* host;
|
|
|
|
char* path;
|
2011-03-15 03:21:25 +00:00
|
|
|
int port;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* primary key: tier
|
|
|
|
* secondary key: udp comes before http */
|
2017-04-20 16:02:19 +00:00
|
|
|
static int filter_trackers_compare_func(void const* va, void const* vb)
|
2011-03-15 03:21:25 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
struct ann_tracker_info const* a = va;
|
|
|
|
struct ann_tracker_info const* b = vb;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (a->info.tier != b->info.tier)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-15 03:21:25 +00:00
|
|
|
return a->info.tier - b->info.tier;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -strcmp(a->scheme, b->scheme);
|
2011-03-15 03:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Massages the incoming list of trackers into something we can use.
|
|
|
|
*/
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_tracker_info* filter_trackers(tr_tracker_info* input, int input_count, int* setme_count)
|
2011-03-15 03:21:25 +00:00
|
|
|
{
|
|
|
|
int n = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_tracker_info* ret;
|
|
|
|
struct ann_tracker_info* tmp = tr_new0(struct ann_tracker_info, input_count);
|
2011-03-15 03:21:25 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/*
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < input_count; ++i)
|
2017-04-21 07:40:57 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "IN: [%d][%s]\n", input[i].tier, input[i].announce);
|
|
|
|
}
|
|
|
|
*/
|
2011-03-15 03:21:25 +00:00
|
|
|
|
|
|
|
/* build a list of valid trackers */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < input_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (tr_urlIsValidTracker(input[i].announce))
|
|
|
|
{
|
2011-03-15 03:21:25 +00:00
|
|
|
int port;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* scheme;
|
|
|
|
char* host;
|
|
|
|
char* path;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool is_duplicate = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_urlParse(input[i].announce, TR_BAD_SIZE, &scheme, &host, &port, &path);
|
2011-03-15 03:21:25 +00:00
|
|
|
|
|
|
|
/* weed out one common source of duplicates:
|
|
|
|
* "http://tracker/announce" +
|
|
|
|
* "http://tracker:80/announce"
|
|
|
|
*/
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int j = 0; !is_duplicate && j < n; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
is_duplicate = tmp[j].port == port && strcmp(tmp[j].scheme, scheme) == 0 && strcmp(tmp[j].host, host) == 0 &&
|
2017-04-19 12:04:45 +00:00
|
|
|
strcmp(tmp[j].path, path) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_duplicate)
|
|
|
|
{
|
|
|
|
tr_free(path);
|
|
|
|
tr_free(host);
|
|
|
|
tr_free(scheme);
|
2011-03-15 03:21:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2011-03-15 03:21:25 +00:00
|
|
|
tmp[n].info = input[i];
|
|
|
|
tmp[n].scheme = scheme;
|
|
|
|
tmp[n].host = host;
|
|
|
|
tmp[n].port = port;
|
|
|
|
tmp[n].path = path;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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...) */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int j = i + 1; j < n; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tmp[i].info.tier != tmp[j].info.tier && tmp[i].port == tmp[j].port &&
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strcmp0(tmp[i].host, tmp[j].host) == 0 && tr_strcmp0(tmp[i].path, tmp[j].path) == 0)
|
|
|
|
{
|
2011-03-15 03:21:25 +00:00
|
|
|
tmp[j].info.tier = tmp[i].info.tier;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-15 03:21:25 +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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
qsort(tmp, n, sizeof(struct ann_tracker_info), filter_trackers_compare_func);
|
2011-03-15 03:21:25 +00:00
|
|
|
|
|
|
|
/* build the output */
|
|
|
|
*setme_count = n;
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = tr_new0(tr_tracker_info, n);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-15 03:21:25 +00:00
|
|
|
ret[i] = tmp[i].info;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-15 03:21:25 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(tmp[i].path);
|
|
|
|
tr_free(tmp[i].host);
|
|
|
|
tr_free(tmp[i].scheme);
|
2011-03-15 03:21:25 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_free(tmp);
|
2011-03-15 03:21:25 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
/*
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "OUT: [%d][%s]\n", ret[i].tier, ret[i].announce);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
int n;
|
2011-03-31 20:26:43 +00:00
|
|
|
int tier_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tier* tier;
|
|
|
|
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
|
|
|
{
|
|
|
|
trackerConstruct(&tt->trackers[i], &infos[i]);
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* count how many tiers there are */
|
|
|
|
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_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 */
|
|
|
|
tier = NULL;
|
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));
|
|
|
|
TR_ASSERT(tor->tiers != NULL);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
struct tr_torrent_tiers* tt = NULL;
|
|
|
|
|
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 */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; tt != NULL && 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;
|
|
|
|
struct tr_torrent_tiers* 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 */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; tt != NULL && 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
|
|
|
{
|
|
|
|
ret = MIN(ret, tt->tiers[i].manualAnnounceAllowedAt);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
char* message;
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
message = evbuffer_free_to_str(buf, NULL);
|
|
|
|
tr_logAddDeep(__FILE__, __LINE__, name, "announce queue is %s", message);
|
|
|
|
tr_free(message);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tier != NULL);
|
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;
|
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 */
|
|
|
|
tier->announce_events[tier->announce_event_count++] = e;
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->announceAt = announceAt;
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_removeElementFromArray(tier->announce_events, 0, sizeof(tr_announce_event), tier->announce_event_count--);
|
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
|
|
|
|
2017-04-20 16:02:19 +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);
|
|
|
|
req->url = tr_strdup(tier->currentTracker->announce);
|
|
|
|
req->tracker_id_str = tr_strdup(tier->currentTracker->tracker_id_str);
|
|
|
|
memcpy(req->info_hash, tor->info.hash, SHA_DIGEST_LENGTH);
|
|
|
|
memcpy(req->peer_id, tr_torrentGetPeerId(tor), PEER_ID_LEN);
|
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];
|
2017-05-01 09:31:01 +00:00
|
|
|
req->leftUntilComplete = tr_torrentHasMetadata(tor) ? tor->info.totalSize - tr_torrentHaveTotal(tor) : INT64_MAX;
|
2011-03-11 04:19:01 +00:00
|
|
|
req->event = event;
|
|
|
|
req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : NUMWANT;
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_torrent_tiers* tt = tor->tiers;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tt != NULL)
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tier* tier = &tt->tiers[i];
|
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_ptrArrayFindSorted(&announcer->stops, req, compareStops) != NULL)
|
|
|
|
{
|
|
|
|
announce_request_free(req);
|
|
|
|
}
|
2011-06-19 18:26:04 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_ptrArrayInsertSorted(&announcer->stops, req, compareStops);
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tiersFree(tor->tiers);
|
2009-09-25 21:05:59 +00:00
|
|
|
tor->tiers = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int interval;
|
|
|
|
|
2011-03-14 16:52:47 +00:00
|
|
|
/* increment the error count */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier->currentTracker != NULL)
|
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
|
|
|
dbgmsg(tier, "%s", err);
|
|
|
|
tr_logAddTorInfo(tier->tor, "%s", err);
|
|
|
|
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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
interval = getRetryInterval(tier->currentTracker);
|
|
|
|
dbgmsg(tier, "Retrying announce in %d seconds.", interval);
|
|
|
|
tr_logAddTorInfo(tier->tor, "Retrying announce in %d seconds.", interval);
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct announce_data* data = vdata;
|
|
|
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (announcer != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 05:48:57 +00:00
|
|
|
++announcer->slotsAvailable;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 05:48:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier != NULL)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker* tracker;
|
|
|
|
|
|
|
|
dbgmsg(tier,
|
|
|
|
"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,
|
2017-04-30 16:25:26 +00:00
|
|
|
response->tracker_id_str != NULL ? response->tracker_id_str : "none",
|
2017-04-19 12:04:45 +00:00
|
|
|
response->pex_count,
|
|
|
|
response->pex6_count,
|
2017-04-30 16:25:26 +00:00
|
|
|
response->errmsg != NULL ? response->errmsg : "none",
|
|
|
|
response->warning != NULL ? 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
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (response->errmsg != NULL)
|
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
|
|
|
|
{
|
2011-03-11 15:44:24 +00:00
|
|
|
int i;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2011-10-09 00:18:18 +00:00
|
|
|
int scrape_fields = 0;
|
2011-10-14 00:27:14 +00:00
|
|
|
int seeders = 0;
|
|
|
|
int leechers = 0;
|
|
|
|
int downloads = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isStopped = event == TR_ANNOUNCE_EVENT_STOPPED;
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((tracker = tier->currentTracker) != NULL)
|
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
|
|
|
{
|
2011-10-14 00:27:14 +00:00
|
|
|
tracker->downloadCount = downloads = 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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((str = response->tracker_id_str) != NULL)
|
2011-03-31 03:37:50 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tracker->tracker_id_str);
|
|
|
|
tracker->tracker_id_str = tr_strdup(str);
|
2011-03-31 03:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((str = response->warning) != NULL)
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((i = response->min_interval) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->announceMinIntervalSec = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((i = response->interval) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->announceIntervalSec = i;
|
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
|
|
|
|
|
|
|
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 */
|
2017-05-29 05:50:40 +00:00
|
|
|
if (scrape_fields >= 3 || (scrape_fields >= 1 && tracker->scrape == NULL))
|
2011-07-31 00:58:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddTorDbg(tier->tor, "Announce response contained scrape info; "
|
|
|
|
"rescheduling next scrape to %d seconds from now.", tier->scrapeIntervalSec);
|
|
|
|
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 */
|
2011-03-11 04:19:01 +00:00
|
|
|
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->url);
|
|
|
|
tr_free(req);
|
2011-06-19 18:26:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void announce_request_delegate(tr_announcer* announcer, tr_announce_request* request, tr_announce_response_func callback,
|
|
|
|
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
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (strncmp(request->url, "http", 4) == 0)
|
|
|
|
{
|
|
|
|
tr_tracker_http_announce(session, request, callback, callback_data);
|
|
|
|
}
|
|
|
|
else if (strncmp(request->url, "udp://", 6) == 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
tr_logAddError("Unsupported url: %s", request->url);
|
|
|
|
}
|
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;
|
|
|
|
--announcer->slotsAvailable;
|
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
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_scrape_error(tr_session* session, tr_tier* tier, char const* errmsg)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2011-04-10 14:15:52 +00:00
|
|
|
int interval;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 16:52:47 +00:00
|
|
|
/* increment the error count */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier->currentTracker != NULL)
|
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
|
|
|
dbgmsg(tier, "Scrape error: %s", errmsg);
|
|
|
|
tr_logAddTorInfo(tier->tor, "Scrape error: %s", errmsg);
|
|
|
|
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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
interval = getRetryInterval(tier->currentTracker);
|
|
|
|
dbgmsg(tier, "Retrying scrape in %zu seconds.", (size_t)interval);
|
|
|
|
tr_logAddTorInfo(tier->tor, "Retrying scrape in %zu seconds.", (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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static tr_tier* find_tier(tr_torrent* tor, char const* scrape)
|
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
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; tt != NULL && 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
|
|
|
|
|
|
|
if (tracker != NULL && tr_strcmp0(scrape, tracker->scrape) == 0)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
return NULL;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_session* session = vsession;
|
|
|
|
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
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor != NULL)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tier* tier = find_tier(tor, response->url);
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tier != NULL)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(tier,
|
|
|
|
"scraped url:%s -- "
|
|
|
|
"did_connect:%d "
|
|
|
|
"did_timeout:%d "
|
|
|
|
"seeders:%d "
|
|
|
|
"leechers:%d "
|
|
|
|
"downloads:%d "
|
|
|
|
"downloaders:%d "
|
|
|
|
"min_request_interval:%d "
|
|
|
|
"err:%s ",
|
|
|
|
response->url,
|
|
|
|
(int)response->did_connect,
|
|
|
|
(int)response->did_timeout,
|
|
|
|
row->seeders,
|
|
|
|
row->leechers,
|
|
|
|
row->downloads,
|
|
|
|
row->downloaders,
|
|
|
|
response->min_request_interval,
|
2017-04-30 16:25:26 +00:00
|
|
|
response->errmsg != NULL ? response->errmsg : "none");
|
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
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (response->errmsg != NULL)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
on_scrape_error(session, tier, response->errmsg);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tracker* tracker;
|
2009-10-28 04:53:39 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastScrapeSucceeded = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
tier->scrapeIntervalSec = MAX(DEFAULT_SCRAPE_INTERVAL_SEC, response->min_request_interval);
|
|
|
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((tracker = tier->currentTracker) != NULL)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (announcer)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
++announcer->slotsAvailable;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (strncmp(request->url, "http", 4) == 0)
|
|
|
|
{
|
|
|
|
tr_tracker_http_scrape(session, request, callback, callback_data);
|
|
|
|
}
|
|
|
|
else if (strncmp(request->url, "udp://", 6) == 0)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
tr_logAddError("Unsupported url: %s", request->url);
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void multiscrape(tr_announcer* announcer, tr_ptrArray* tiers)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 15:11:37 +00:00
|
|
|
int request_count = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
|
|
|
int const tier_count = tr_ptrArraySize(tiers);
|
|
|
|
int const max_request_count = MIN(announcer->slotsAvailable, tier_count);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_scrape_request* requests = tr_new0(tr_scrape_request, max_request_count);
|
2011-03-11 15:11:37 +00:00
|
|
|
|
|
|
|
/* batch as many info_hashes into a request as we can */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < tier_count; ++i)
|
2011-03-11 15:11:37 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_tier* tier = tr_ptrArrayNth(tiers, i);
|
|
|
|
char* url = tier->currentTracker->scrape;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* hash = tier->tor->info.hash;
|
2017-05-13 22:38:31 +00:00
|
|
|
bool found = false;
|
2011-03-11 15:11:37 +00:00
|
|
|
|
2017-07-04 18:22:26 +00:00
|
|
|
TR_ASSERT(url != NULL);
|
|
|
|
|
2011-03-11 15:11:37 +00:00
|
|
|
/* if there's a request with this scrape URL and a free slot, use it */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int 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
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (req->info_hash_count >= TR_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
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_strcmp0(req->url, url) != 0)
|
|
|
|
{
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH);
|
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 */
|
2017-05-13 22:38:31 +00:00
|
|
|
if (!found && request_count < max_request_count)
|
2011-03-11 15:11:37 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_scrape_request* req = &requests[request_count++];
|
2011-03-11 15:11:37 +00:00
|
|
|
req->url = 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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(req->info_hash[req->info_hash_count++], hash, SHA_DIGEST_LENGTH);
|
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 */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int 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);
|
|
|
|
}
|
2011-03-11 15:11:37 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(requests);
|
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
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0, n = tr_ptrArraySize(&announcer->stops); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
announce_request_delegate(announcer, tr_ptrArrayNth(&announcer->stops, i), NULL, NULL);
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayClear(&announcer->stops);
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool tierNeedsToScrape(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->isScraping && tier->scrapeAt != 0 && tier->scrapeAt <= now && tier->currentTracker != NULL &&
|
2019-02-10 11:05:16 +00:00
|
|
|
tier->currentTracker->scrape != NULL;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int compareTiers(void const* va, void const* vb)
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2011-02-05 18:46:10 +00:00
|
|
|
int ret;
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_tier const* a = *(tr_tier const**)va;
|
|
|
|
tr_tier const* b = *(tr_tier const**)vb;
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2011-02-05 18:46:10 +00:00
|
|
|
/* primary key: larger stats come before smaller */
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = compareTransfer(a->byteCounts[TR_ANN_UP], a->byteCounts[TR_ANN_DOWN], b->byteCounts[TR_ANN_UP],
|
|
|
|
b->byteCounts[TR_ANN_DOWN]);
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2011-02-05 18:46:10 +00:00
|
|
|
/* secondary key: announcements that have been waiting longer go first */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (ret == 0 && a->announceAt != b->announceAt)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-02-05 18:46:10 +00:00
|
|
|
ret = a->announceAt < b->announceAt ? -1 : 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void announceMore(tr_announcer* announcer)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
int n;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_torrent* tor;
|
2011-03-14 02:40:39 +00:00
|
|
|
tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
|
|
|
|
tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
|
2017-04-20 16:02:19 +00:00
|
|
|
time_t const now = tr_time();
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(NULL, "announceMore: slotsAvailable is %d", announcer->slotsAvailable);
|
2011-03-11 05:48:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (announcer->slotsAvailable < 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-14 02:40:39 +00:00
|
|
|
|
|
|
|
/* build a list of tiers that need to be announced */
|
|
|
|
tor = NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((tor = tr_torrentNext(announcer->session, tor)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
struct tr_torrent_tiers* tt = tor->tiers;
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; tt != NULL && i < tt->tier_count; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tier* tier = &tt->tiers[i];
|
|
|
|
|
|
|
|
if (tierNeedsToAnnounce(tier, now))
|
|
|
|
{
|
|
|
|
tr_ptrArrayAppend(&announceMe, tier);
|
|
|
|
}
|
|
|
|
else if (tierNeedsToScrape(tier, now))
|
|
|
|
{
|
|
|
|
tr_ptrArrayAppend(&scrapeMe, tier);
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
n = tr_ptrArraySize(&announceMe);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
/* if there are more tiers than slots available, prioritize */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (n > announcer->slotsAvailable)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
qsort(tr_ptrArrayBase(&announceMe), n, sizeof(tr_tier*), compareTiers);
|
2017-05-13 22:38:31 +00:00
|
|
|
n = announcer->slotsAvailable;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* announce some */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_tier* tier = tr_ptrArrayNth(&announceMe, i);
|
|
|
|
tr_logAddTorDbg(tier->tor, "%s", "Announcing to tracker");
|
|
|
|
dbgmsg(tier, "announcing tier %d of %d", i, n);
|
|
|
|
tierAnnounce(announcer, tier);
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* scrape some */
|
2017-04-19 12:04:45 +00:00
|
|
|
multiscrape(announcer, &scrapeMe);
|
2009-12-02 05:30:46 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* cleanup */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayDestruct(&scrapeMe, NULL);
|
|
|
|
tr_ptrArrayDestruct(&announceMe, NULL);
|
2011-03-14 02:40:39 +00:00
|
|
|
}
|
2009-10-12 23:16:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void onUpkeepTimer(evutil_socket_t foo UNUSED, short bar UNUSED, void* vannouncer)
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announcer* announcer = vannouncer;
|
|
|
|
tr_session* session = announcer->session;
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionLock(session);
|
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
|
|
|
|
|
|
|
/* maybe send out some announcements to trackers */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!is_closing)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
announceMore(announcer);
|
|
|
|
}
|
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)
|
|
|
|
{
|
2011-03-13 06:38:54 +00:00
|
|
|
announcer->tauUpkeepAt = now + TAU_UPKEEP_INTERVAL_SECS;
|
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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_timerAdd(announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0);
|
2010-01-04 09:11:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_sessionUnlock(session);
|
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;
|
|
|
|
tr_tracker_stat* ret;
|
|
|
|
struct tr_torrent_tiers* 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;
|
2017-04-19 12:04:45 +00:00
|
|
|
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;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(st->host, tracker->key, sizeof(st->host));
|
|
|
|
tr_strlcpy(st->announce, tracker->announce, sizeof(st->announce));
|
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;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tracker->scrape != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_strlcpy(st->scrape, tracker->scrape, sizeof(st->scrape));
|
|
|
|
}
|
2010-09-03 04:30:43 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2010-09-03 04:30:43 +00:00
|
|
|
st->scrape[0] = '\0';
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-30 20:49:52 +00:00
|
|
|
|
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
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((st->hasScraped = tier->lastScrapeTime != 0))
|
|
|
|
{
|
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;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((st->hasAnnounced = tier->lastAnnounceTime != 0))
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerStatsFree(tr_tracker_stat* trackers, int trackerCount UNUSED)
|
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);
|
|
|
|
TR_ASSERT(tr_strcmp0(tgt->trackers[trackerIndex].announce, src->currentTracker->announce) == 0);
|
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;
|
2017-04-19 12:04:45 +00:00
|
|
|
tgt->announce_events = tr_memdup(src->announce_events, sizeof(tr_announce_event) * src->announce_event_count);
|
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
|
|
|
{
|
|
|
|
if ((found = tr_strcmp0(src->currentTracker->announce, tt->tiers[i].trackers[j].announce) == 0))
|
|
|
|
{
|
|
|
|
copy_tier_attributes_impl(&tt->tiers[i], j, src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_announcerResetTorrent(tr_announcer* announcer UNUSED, tr_torrent* tor)
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(tor->tiers != NULL);
|
|
|
|
|
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 */
|
|
|
|
tt->tiers = NULL;
|
|
|
|
tt->trackers = NULL;
|
|
|
|
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
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (old.tiers[i].currentTracker != NULL)
|
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
|
|
|
}
|