2009-09-25 21:05:59 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-09-25 21:05:59 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2009-09-25 21:05:59 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2009-09-25 21:05:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2011-03-24 21:49:42 +00:00
|
|
|
#include <limits.h> /* INT_MAX */
|
2011-03-16 18:04:23 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> /* qsort() */
|
|
|
|
#include <string.h> /* strcmp(), memcpy() */
|
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 */
|
|
|
|
|
|
|
|
#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"
|
2011-03-25 01:21:31 +00:00
|
|
|
#include "crypto.h" /* tr_cryptoRandInt(), tr_cryptoWeakRandInt() */
|
2011-01-22 17:45:54 +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"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
struct tr_tier;
|
|
|
|
|
|
|
|
static void tier_build_log_name( const struct tr_tier * tier,
|
|
|
|
char * buf, size_t buflen );
|
2010-02-14 03:40:39 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
#define dbgmsg( tier, ... ) \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) do { \
|
|
|
|
char name[128]; \
|
2011-03-11 04:19:01 +00:00
|
|
|
tier_build_log_name( tier, name, sizeof( name ) ); \
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_deepLog( __FILE__, __LINE__, name, __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* unless the tracker says otherwise, rescrape this frequently */
|
2010-02-05 05:16:18 +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 */
|
|
|
|
DEFAULT_ANNOUNCE_INTERVAL_SEC = ( 60 * 10 ),
|
|
|
|
|
|
|
|
/* unless the tracker says otherwise, this is the announce min_interval */
|
|
|
|
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,
|
2009-09-25 21:05:59 +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
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
const char*
|
|
|
|
tr_announce_event_get_string( tr_announce_event e )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
switch( e )
|
|
|
|
{
|
|
|
|
case TR_ANNOUNCE_EVENT_COMPLETED: return "completed";
|
|
|
|
case TR_ANNOUNCE_EVENT_STARTED: return "started";
|
|
|
|
case TR_ANNOUNCE_EVENT_STOPPED: return "stopped";
|
|
|
|
default: return "";
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-02-05 18:46:10 +00:00
|
|
|
static int
|
|
|
|
compareTransfer( uint64_t a_uploaded, uint64_t a_downloaded,
|
|
|
|
uint64_t b_uploaded, uint64_t b_downloaded )
|
|
|
|
{
|
|
|
|
/* higher upload count goes first */
|
|
|
|
if( a_uploaded != b_uploaded )
|
|
|
|
return a_uploaded > b_uploaded ? -1 : 1;
|
|
|
|
|
|
|
|
/* then higher download count goes first */
|
|
|
|
if( a_downloaded != b_downloaded )
|
|
|
|
return a_downloaded > b_downloaded ? -1 : 1;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2009-09-25 21:05:59 +00:00
|
|
|
static int
|
|
|
|
compareStops( const void * va, const void * vb )
|
|
|
|
{
|
2011-06-19 18:26:04 +00:00
|
|
|
int i;
|
2011-03-11 04:19:01 +00:00
|
|
|
const tr_announce_request * a = va;
|
|
|
|
const tr_announce_request * b = vb;
|
2011-06-19 18:26:04 +00:00
|
|
|
|
|
|
|
/* primary key: volume of data transferred. */
|
|
|
|
if(( i = compareTransfer( a->up, a->down, b->up, b->down)))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
/* secondary key: the torrent's info_hash */
|
|
|
|
if(( i = memcmp( a->info_hash, b->info_hash, SHA_DIGEST_LENGTH )))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
/* tertiary key: the tracker's announec url */
|
|
|
|
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
|
|
|
|
2009-09-25 21:05:59 +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;
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2009-12-02 05:30:46 +00:00
|
|
|
tr_announcerHasBacklog( const struct tr_announcer * announcer )
|
|
|
|
{
|
2010-02-05 01:16:30 +00:00
|
|
|
return announcer->slotsAvailable < 1;
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
static void
|
|
|
|
onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer );
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
void
|
|
|
|
tr_announcerInit( tr_session * session )
|
|
|
|
{
|
|
|
|
tr_announcer * a;
|
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
a = tr_new0( tr_announcer, 1 );
|
|
|
|
a->stops = TR_PTR_ARRAY_INIT;
|
2011-03-10 11:46:22 +00:00
|
|
|
a->key = tr_cryptoRandInt( 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;
|
2010-12-24 08:58:41 +00:00
|
|
|
a->upkeepTimer = evtimer_new( session->event_base, onUpkeepTimer, a );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_timerAdd( a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
|
|
|
|
|
|
|
|
session->announcer = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void flushCloseMessages( tr_announcer * announcer );
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_announcerClose( tr_session * session )
|
|
|
|
{
|
|
|
|
tr_announcer * announcer = session->announcer;
|
|
|
|
|
|
|
|
flushCloseMessages( announcer );
|
|
|
|
|
2011-03-17 18:51:31 +00:00
|
|
|
tr_tracker_udp_start_shutdown( session );
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
event_free( announcer->upkeepTimer );
|
2009-09-25 21:05:59 +00:00
|
|
|
announcer->upkeepTimer = NULL;
|
|
|
|
|
|
|
|
tr_ptrArrayDestruct( &announcer->stops, NULL );
|
|
|
|
|
|
|
|
session->announcer = NULL;
|
|
|
|
tr_free( announcer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* a row in tr_tier's list of trackers */
|
|
|
|
typedef struct
|
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
char * key;
|
2009-09-25 21:05:59 +00:00
|
|
|
char * announce;
|
|
|
|
char * scrape;
|
|
|
|
|
2011-03-11 04:19:01 +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 */
|
2011-03-11 04:19:01 +00:00
|
|
|
static char *
|
2011-03-14 02:40:39 +00:00
|
|
|
getKey( const char * url )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
char * ret;
|
2011-03-15 17:03:09 +00:00
|
|
|
char * scheme = NULL;
|
|
|
|
char * host = NULL;
|
|
|
|
int port = 0;
|
|
|
|
|
|
|
|
tr_urlParse( url, -1, &scheme, &host, &port, NULL );
|
|
|
|
ret = tr_strdup_printf( "%s://%s:%d", (scheme?scheme:"invalid"), (host?host:"invalid"), port );
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_free( host );
|
2011-03-15 17:03:09 +00:00
|
|
|
tr_free( scheme );
|
2011-03-11 04:19:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
static void
|
|
|
|
trackerConstruct( tr_tracker * tracker, const tr_tracker_info * inf )
|
|
|
|
{
|
|
|
|
memset( tracker, 0, sizeof( tr_tracker ) );
|
|
|
|
tracker->key = getKey( inf->announce );
|
|
|
|
tracker->announce = tr_strdup( inf->announce );
|
|
|
|
tracker->scrape = tr_strdup( inf->scrape );
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-31 20:26:43 +00:00
|
|
|
trackerDestruct( tr_tracker * tracker )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_free( tracker->tracker_id_str );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_free( tracker->scrape );
|
2011-02-05 16:45:06 +00:00
|
|
|
tr_free( tracker->announce );
|
2011-03-14 02:40:39 +00:00
|
|
|
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
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
tr_tracker * trackers;
|
|
|
|
int tracker_count;
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_tracker * currentTracker;
|
2010-02-13 21:58:38 +00:00
|
|
|
int currentTrackerIndex;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
tr_torrent * tor;
|
|
|
|
|
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
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_announce_event * announce_events;
|
|
|
|
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;
|
|
|
|
|
2011-04-02 07:44:19 +00:00
|
|
|
static time_t
|
2011-07-23 17:14:12 +00:00
|
|
|
get_next_scrape_time( const tr_session * session, const tr_tier * tier, int interval )
|
2011-04-02 07:44:19 +00:00
|
|
|
{
|
2011-04-10 03:57:37 +00:00
|
|
|
time_t ret;
|
2011-04-02 07:44:19 +00:00
|
|
|
const time_t now = tr_time( );
|
|
|
|
|
2011-07-23 17:14:12 +00:00
|
|
|
/* Maybe don't scrape paused torrents */
|
|
|
|
if( !tier->isRunning && !session->scrapePausedTorrents )
|
|
|
|
ret = 0;
|
|
|
|
|
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. */
|
2011-07-23 17:14:12 +00:00
|
|
|
else {
|
|
|
|
ret = now + interval;
|
|
|
|
while( ret % 10 )
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
|
2011-04-10 03:57:37 +00:00
|
|
|
return ret;
|
2011-04-02 07:44:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
static void
|
|
|
|
tierConstruct( tr_tier * tier, tr_torrent * tor )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
static int nextKey = 1;
|
|
|
|
|
2011-03-31 20:26:43 +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;
|
2011-07-23 17:14:12 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time( tor->session, tier, tr_cryptoWeakRandInt( 180 ) );
|
2011-03-31 20:26:43 +00:00
|
|
|
tier->tor = tor;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-31 20:26:43 +00:00
|
|
|
tierDestruct( tr_tier * tier )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_free( tier->announce_events );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
static void
|
|
|
|
tier_build_log_name( const tr_tier * tier, char * buf, size_t buflen )
|
|
|
|
{
|
|
|
|
tr_snprintf( buf, buflen, "[%s---%s]",
|
|
|
|
( tier && tier->tor ) ? tr_torrentName( tier->tor ) : "?",
|
2011-03-14 02:40:39 +00:00
|
|
|
( tier && tier->currentTracker ) ? tier->currentTracker->key : "?" );
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static void
|
|
|
|
tierIncrementTracker( tr_tier * tier )
|
|
|
|
{
|
2009-12-02 15:16:29 +00:00
|
|
|
/* move our index to the next tracker in the tier */
|
2011-03-15 03:21:25 +00:00
|
|
|
const int i = ( tier->currentTracker == NULL )
|
|
|
|
? 0
|
2011-03-31 20:26:43 +00:00
|
|
|
: ( 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
|
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
tr_tier * tiers;
|
|
|
|
int tier_count;
|
|
|
|
|
|
|
|
tr_tracker * trackers;
|
|
|
|
int tracker_count;
|
|
|
|
|
2010-06-19 14:33:10 +00:00
|
|
|
tr_tracker_callback * callback;
|
|
|
|
void * callbackData;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
tr_torrent_tiers;
|
|
|
|
|
|
|
|
static tr_torrent_tiers*
|
|
|
|
tiersNew( void )
|
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
return tr_new0( tr_torrent_tiers, 1 );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-31 20:26:43 +00:00
|
|
|
tiersDestruct( tr_torrent_tiers * tt )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i=0; i<tt->tracker_count; ++i )
|
|
|
|
trackerDestruct( &tt->trackers[i] );
|
|
|
|
tr_free( tt->trackers );
|
|
|
|
|
|
|
|
for( i=0; i<tt->tier_count; ++i )
|
|
|
|
tierDestruct( &tt->tiers[i] );
|
|
|
|
tr_free( tt->tiers );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tiersFree( tr_torrent_tiers * tt )
|
|
|
|
{
|
|
|
|
tiersDestruct( tt );
|
|
|
|
tr_free( tt );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tr_tier*
|
2011-03-11 04:19:01 +00:00
|
|
|
getTier( tr_announcer * announcer, const uint8_t * info_hash, int tierId )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
tr_tier * tier = NULL;
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if( announcer != NULL )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_session * session = announcer->session;
|
|
|
|
tr_torrent * tor = tr_torrentFindFromHash( session, info_hash );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
if( tor && tor->tiers )
|
|
|
|
{
|
|
|
|
int i;
|
2011-03-31 20:26:43 +00:00
|
|
|
tr_torrent_tiers * tt = tor->tiers;
|
|
|
|
|
|
|
|
for( i=0; !tier && i<tt->tier_count; ++i )
|
|
|
|
if( tt->tiers[i].key == tierId )
|
|
|
|
tier = &tt->tiers[i];
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** PUBLISH
|
|
|
|
***/
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
static const tr_tracker_event TRACKER_EVENT_INIT = { 0, 0, 0, 0, 0, 0 };
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
publishMessage( tr_tier * tier, const char * msg, int type )
|
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
if( tier && tier->tor && tier->tor->tiers && tier->tor->tiers->callback )
|
2009-09-25 21:05:59 +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;
|
2011-03-11 04:19:01 +00:00
|
|
|
if( tier->currentTracker )
|
|
|
|
event.tracker = tier->currentTracker->announce;
|
2010-06-19 14:33:10 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
tiers->callback( tier->tor, &event, tiers->callbackData );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
publishErrorClear( tr_tier * tier )
|
|
|
|
{
|
|
|
|
publishMessage( tier, NULL, TR_TRACKER_ERROR_CLEAR );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
publishWarning( tr_tier * tier, const char * msg )
|
|
|
|
{
|
|
|
|
publishMessage( tier, msg, TR_TRACKER_WARNING );
|
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
static void
|
|
|
|
publishError( tr_tier * tier, const char * msg )
|
|
|
|
{
|
|
|
|
publishMessage( tier, msg, TR_TRACKER_ERROR );
|
|
|
|
}
|
|
|
|
|
2010-07-01 05:14:34 +00:00
|
|
|
static int8_t
|
2011-03-11 04:57:53 +00:00
|
|
|
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... */
|
|
|
|
if( ( leechers == pex_count ) && tr_torrentIsPrivate( tier->tor )
|
|
|
|
&& tr_torrentIsSeed( tier->tor )
|
|
|
|
&& ( seeds + leechers < NUMWANT ) )
|
|
|
|
return 0;
|
|
|
|
|
2011-06-25 19:29:25 +00:00
|
|
|
if( seeds>=0 && leechers>=0 && (seeds+leechers>0) )
|
2010-07-01 05:14:34 +00:00
|
|
|
return (int8_t)((100.0*seeds)/(seeds+leechers));
|
2010-05-18 15:14:54 +00:00
|
|
|
|
|
|
|
return -1; /* unknown */
|
|
|
|
}
|
|
|
|
|
2011-01-22 17:45:54 +00:00
|
|
|
static void
|
|
|
|
publishPeersPex( tr_tier * tier, int seeds, int leechers,
|
|
|
|
const tr_pex * pex, int n )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
if( tier->tor->tiers->callback )
|
|
|
|
{
|
|
|
|
tr_tracker_event e = TRACKER_EVENT_INIT;
|
|
|
|
e.messageType = TR_TRACKER_PEERS;
|
|
|
|
e.seedProbability = getSeedProbability( tier, seeds, leechers, n );
|
|
|
|
e.pex = pex;
|
|
|
|
e.pexCount = n;
|
|
|
|
dbgmsg( tier, "got %d peers; seed prob %d", n, (int)e.seedProbability );
|
2011-03-11 04:57:53 +00:00
|
|
|
|
2010-06-19 14:33:10 +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;
|
|
|
|
|
|
|
|
char * scheme;
|
|
|
|
char * host;
|
|
|
|
char * path;
|
|
|
|
int port;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* primary key: tier
|
|
|
|
* secondary key: udp comes before http */
|
|
|
|
static int
|
|
|
|
filter_trackers_compare_func( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const struct ann_tracker_info * a = va;
|
|
|
|
const struct ann_tracker_info * b = vb;
|
|
|
|
if( a->info.tier != b->info.tier )
|
|
|
|
return a->info.tier - b->info.tier;
|
|
|
|
return -strcmp( a->scheme, b->scheme );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Massages the incoming list of trackers into something we can use.
|
|
|
|
*/
|
|
|
|
static tr_tracker_info *
|
|
|
|
filter_trackers( tr_tracker_info * input, int input_count, int * setme_count )
|
|
|
|
{
|
|
|
|
int i, in;
|
|
|
|
int j, jn;
|
|
|
|
int n = 0;
|
|
|
|
struct tr_tracker_info * ret;
|
|
|
|
struct ann_tracker_info * tmp = tr_new0( struct ann_tracker_info, input_count );
|
|
|
|
|
|
|
|
/*for( i=0, in=input_count; i<in; ++i ) fprintf( stderr, "IN: [%d][%s]\n", input[i].tier, input[i].announce );*/
|
|
|
|
|
|
|
|
/* build a list of valid trackers */
|
|
|
|
for( i=0, in=input_count; i<in; ++i ) {
|
|
|
|
if( tr_urlIsValidTracker( input[i].announce ) ) {
|
|
|
|
int port;
|
|
|
|
char * scheme;
|
|
|
|
char * host;
|
|
|
|
char * path;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool is_duplicate = false;
|
2011-03-15 03:21:25 +00:00
|
|
|
tr_urlParse( input[i].announce, -1, &scheme, &host, &port, &path );
|
|
|
|
|
|
|
|
/* weed out one common source of duplicates:
|
|
|
|
* "http://tracker/announce" +
|
|
|
|
* "http://tracker:80/announce"
|
|
|
|
*/
|
|
|
|
for( j=0, jn=n; !is_duplicate && j<jn; ++j )
|
|
|
|
is_duplicate = (tmp[j].port==port)
|
|
|
|
&& !strcmp(tmp[j].scheme,scheme)
|
|
|
|
&& !strcmp(tmp[j].host,host)
|
|
|
|
&& !strcmp(tmp[j].path,path);
|
|
|
|
|
|
|
|
if( is_duplicate ) {
|
|
|
|
tr_free( path );
|
|
|
|
tr_free( host );
|
|
|
|
tr_free( scheme );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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...) */
|
2011-03-31 03:37:50 +00:00
|
|
|
for( i=0, in=n; i<in; ++i )
|
2011-04-03 03:25:13 +00:00
|
|
|
for( j=i+1, jn=n; j<jn; ++j )
|
|
|
|
if( (tmp[i].info.tier!=tmp[j].info.tier)
|
|
|
|
&& (tmp[i].port==tmp[j].port)
|
2011-03-15 03:21:25 +00:00
|
|
|
&& !tr_strcmp0(tmp[i].host,tmp[j].host)
|
|
|
|
&& !tr_strcmp0(tmp[i].path,tmp[j].path) )
|
|
|
|
tmp[j].info.tier = tmp[i].info.tier;
|
|
|
|
|
|
|
|
/* sort them, for two reasons:
|
|
|
|
* (1) unjumble the tiers from the previous step
|
|
|
|
* (2) move the UDP trackers to the front of each tier */
|
|
|
|
qsort( tmp, n, sizeof(struct ann_tracker_info), filter_trackers_compare_func );
|
|
|
|
|
|
|
|
/* build the output */
|
|
|
|
*setme_count = n;
|
|
|
|
ret = tr_new0( tr_tracker_info, n );
|
|
|
|
for( i=0, in=n; i<in; ++i )
|
|
|
|
ret[i] = tmp[i].info;
|
|
|
|
|
|
|
|
/* cleanup */
|
2011-03-31 03:37:50 +00:00
|
|
|
for( i=0, in=n; i<in; ++i ) {
|
2011-03-15 03:21:25 +00:00
|
|
|
tr_free( tmp[i].path );
|
|
|
|
tr_free( tmp[i].host );
|
|
|
|
tr_free( tmp[i].scheme );
|
|
|
|
}
|
|
|
|
tr_free( tmp );
|
|
|
|
|
|
|
|
/*for( i=0, in=n; i<in; ++i ) fprintf( stderr, "OUT: [%d][%s]\n", ret[i].tier, ret[i].announce );*/
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-02 04:54:02 +00:00
|
|
|
static void
|
2011-03-31 20:26:43 +00:00
|
|
|
addTorrentToTier( tr_torrent_tiers * tt, tr_torrent * tor )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2009-10-02 02:03:30 +00:00
|
|
|
int i, n;
|
2011-03-31 20:26:43 +00:00
|
|
|
int tier_count;
|
|
|
|
tr_tier * tier;
|
2011-03-15 03:21:25 +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 */
|
2011-03-31 20:26:43 +00:00
|
|
|
tt->trackers = tr_new0( tr_tracker, n );
|
|
|
|
tt->tracker_count = n;
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
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;
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
if( !i || ( infos[i].tier != infos[i-1].tier ) )
|
|
|
|
++tier_count;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* build the array of tiers */
|
|
|
|
tier = NULL;
|
|
|
|
tt->tiers = tr_new0( tr_tier, tier_count );
|
|
|
|
tt->tier_count = 0;
|
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
if( i && ( infos[i].tier == infos[i-1].tier ) )
|
|
|
|
++tier->tracker_count;
|
|
|
|
else {
|
|
|
|
tier = &tt->tiers[tt->tier_count++];
|
|
|
|
tierConstruct( tier, tor );
|
|
|
|
tier->trackers = &tt->trackers[i];
|
|
|
|
tier->tracker_count = 1;
|
|
|
|
tierIncrementTracker( tier );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
/* cleanup */
|
2009-10-02 02:03:30 +00:00
|
|
|
tr_free( infos );
|
2009-10-02 04:54:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_torrent_tiers *
|
2011-02-07 23:09:36 +00:00
|
|
|
tr_announcerAddTorrent( tr_torrent * tor,
|
|
|
|
tr_tracker_callback * callback,
|
|
|
|
void * callbackData )
|
2009-10-02 04:54:02 +00:00
|
|
|
{
|
|
|
|
tr_torrent_tiers * tiers;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
tiers = tiersNew( );
|
2010-06-19 14:33:10 +00:00
|
|
|
tiers->callback = callback;
|
|
|
|
tiers->callbackData = callbackData;
|
2009-10-02 04:54:02 +00:00
|
|
|
|
2011-02-05 18:46:10 +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
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2009-09-25 21:05:59 +00:00
|
|
|
tierCanManualAnnounce( const tr_tier * tier )
|
|
|
|
{
|
2011-01-22 19:56:01 +00:00
|
|
|
return tier->manualAnnounceAllowedAt <= tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_announcerCanManualAnnounce( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
int i;
|
2011-04-02 23:33:51 +00:00
|
|
|
struct tr_torrent_tiers * tt = NULL;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
assert( tor->tiers != NULL );
|
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
if( tor->isRunning )
|
|
|
|
tt = tor->tiers;
|
2011-01-22 19:56:01 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* return true if any tier can manual announce */
|
2011-03-31 20:26:43 +00:00
|
|
|
for( i=0; tt && i<tt->tier_count; ++i )
|
|
|
|
if( tierCanManualAnnounce( &tt->tiers[i] ) )
|
2011-03-22 15:19:54 +00:00
|
|
|
return true;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
tr_announcerNextManualAnnounce( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
time_t ret = ~(time_t)0;
|
2011-03-31 20:26:43 +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
|
|
|
/* find the earliest manual announce time from all peers */
|
2011-03-31 20:26:43 +00:00
|
|
|
for( i=0; tt && i<tt->tier_count; ++i )
|
|
|
|
if( tt->tiers[i].isRunning )
|
|
|
|
ret = MIN( ret, tt->tiers[i].manualAnnounceAllowedAt );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-11 04:19:01 +00:00
|
|
|
dbgmsg_tier_announce_queue( const tr_tier * tier )
|
|
|
|
{
|
|
|
|
if( tr_deepLoggingIsActive( ) )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char name[128];
|
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
|
|
|
|
|
|
|
tier_build_log_name( tier, name, sizeof( name ) );
|
|
|
|
for( i=0; i<tier->announce_event_count; ++i )
|
|
|
|
{
|
|
|
|
const tr_announce_event e = tier->announce_events[i];
|
|
|
|
const char * str = tr_announce_event_get_string( e );
|
|
|
|
evbuffer_add_printf( buf, "[%d:%s]", i, str );
|
|
|
|
}
|
2011-04-27 20:41:47 +00:00
|
|
|
|
|
|
|
tr_deepLog( __FILE__, __LINE__, name, "announce queue is %s", evbuffer_pullup( buf, -1 ) );
|
|
|
|
evbuffer_free( buf );
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tier_announce_remove_trailing( tr_tier * tier, tr_announce_event e )
|
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
while( ( tier->announce_event_count > 0 )
|
|
|
|
&& ( tier->announce_events[tier->announce_event_count-1] == e ) )
|
2011-03-11 04:19:01 +00:00
|
|
|
--tier->announce_event_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-14 02:40:39 +00:00
|
|
|
tier_announce_event_push( tr_tier * tier,
|
|
|
|
tr_announce_event e,
|
|
|
|
time_t announceAt )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
int i;
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
assert( tier != NULL );
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
dbgmsg_tier_announce_queue( tier );
|
2011-03-14 02:40:39 +00:00
|
|
|
dbgmsg( tier, "queued \"%s\"", tr_announce_event_get_string( e ) );
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
if( tier->announce_event_count > 0 )
|
|
|
|
{
|
|
|
|
/* special case #1: if we're adding a "stopped" event,
|
|
|
|
* dump everything leading up to it except "completed" */
|
|
|
|
if( e == TR_ANNOUNCE_EVENT_STOPPED ) {
|
2011-03-22 15:19:54 +00:00
|
|
|
bool has_completed = false;
|
2011-03-11 04:19:01 +00:00
|
|
|
const tr_announce_event c = TR_ANNOUNCE_EVENT_COMPLETED;
|
|
|
|
for( i=0; !has_completed && i<tier->announce_event_count; ++i )
|
|
|
|
has_completed = c == tier->announce_events[i];
|
|
|
|
tier->announce_event_count = 0;
|
|
|
|
if( has_completed )
|
|
|
|
tier->announce_events[tier->announce_event_count++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* special case #2: dump all empty strings leading up to this event */
|
|
|
|
tier_announce_remove_trailing( tier, TR_ANNOUNCE_EVENT_NONE );
|
|
|
|
|
|
|
|
/* special case #3: no consecutive duplicates */
|
|
|
|
tier_announce_remove_trailing( tier, e );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make room in the array for another event */
|
|
|
|
if( tier->announce_event_alloc <= tier->announce_event_count ) {
|
|
|
|
tier->announce_event_alloc += 4;
|
|
|
|
tier->announce_events = tr_renew( tr_announce_event,
|
|
|
|
tier->announce_events,
|
|
|
|
tier->announce_event_alloc );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
dbgmsg_tier_announce_queue( tier );
|
|
|
|
dbgmsg( tier, "announcing in %d seconds", (int)difftime(announceAt,tr_time()) );
|
|
|
|
}
|
|
|
|
|
2011-03-11 05:48:57 +00:00
|
|
|
static tr_announce_event
|
|
|
|
tier_announce_event_pull( tr_tier * tier )
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2011-03-11 05:48:57 +00:00
|
|
|
const tr_announce_event e = tier->announce_events[0];
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2011-03-11 05:48:57 +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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-11 04:19:01 +00:00
|
|
|
torrentAddAnnounce( tr_torrent * tor, tr_announce_event e, time_t announceAt )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
int i;
|
2011-03-31 20:26:43 +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 */
|
2011-03-31 20:26:43 +00:00
|
|
|
for( i=0; i<tt->tier_count; ++i )
|
|
|
|
tier_announce_event_push( &tt->tiers[i], e, announceAt );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-10 02:59:15 +00:00
|
|
|
tr_announcerTorrentStarted( tr_torrent * tor )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
torrentAddAnnounce( tor, TR_ANNOUNCE_EVENT_STARTED, tr_time( ) );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
void
|
2010-02-10 02:59:15 +00:00
|
|
|
tr_announcerManualAnnounce( tr_torrent * tor )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
torrentAddAnnounce( tor, TR_ANNOUNCE_EVENT_NONE, tr_time( ) );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
void
|
|
|
|
tr_announcerTorrentStopped( tr_torrent * tor )
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
torrentAddAnnounce( tor, TR_ANNOUNCE_EVENT_STOPPED, tr_time( ) );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
void
|
|
|
|
tr_announcerTorrentCompleted( tr_torrent * tor )
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
torrentAddAnnounce( tor, TR_ANNOUNCE_EVENT_COMPLETED, tr_time( ) );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
void
|
|
|
|
tr_announcerChangeMyPort( tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_announcerTorrentStarted( tor );
|
|
|
|
}
|
|
|
|
|
2010-02-08 16:47:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_announcerAddBytes( tr_torrent * tor, int type, uint32_t byteCount )
|
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
int i;
|
|
|
|
struct tr_torrent_tiers * tt = tor->tiers;
|
2010-02-08 16:47:30 +00:00
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
assert( type==TR_ANN_UP || type==TR_ANN_DOWN || type==TR_ANN_CORRUPT );
|
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
for( i=0; i<tt->tier_count; ++i )
|
|
|
|
tt->tiers[i].byteCounts[ type ] += byteCount;
|
2010-02-08 16:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
static tr_announce_request *
|
|
|
|
announce_request_new( const tr_announcer * announcer,
|
|
|
|
const tr_torrent * tor,
|
|
|
|
const tr_tier * tier,
|
|
|
|
tr_announce_event event )
|
|
|
|
{
|
|
|
|
tr_announce_request * req = tr_new0( tr_announce_request, 1 );
|
2011-03-13 00:18:11 +00:00
|
|
|
req->port = tr_sessionGetPublicPeerPort( announcer->session );
|
2011-03-11 04:19:01 +00:00
|
|
|
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, tor->peer_id, PEER_ID_LEN );
|
|
|
|
req->up = tier->byteCounts[TR_ANN_UP];
|
|
|
|
req->down = tier->byteCounts[TR_ANN_DOWN];
|
|
|
|
req->corrupt = tier->byteCounts[TR_ANN_CORRUPT];
|
|
|
|
req->left = tr_cpLeftUntilComplete( &tor->completion ),
|
|
|
|
req->event = event;
|
|
|
|
req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : NUMWANT;
|
|
|
|
req->key = announcer->key;
|
|
|
|
req->partial_seed = tr_cpGetStatus( &tor->completion ) == TR_PARTIAL_SEED;
|
|
|
|
tier_build_log_name( tier, req->log_name, sizeof( req->log_name ) );
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2011-06-19 18:26:04 +00:00
|
|
|
static void announce_request_free( tr_announce_request * req );
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
void
|
|
|
|
tr_announcerRemoveTorrent( tr_announcer * announcer, tr_torrent * tor )
|
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
struct tr_torrent_tiers * tt = tor->tiers;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
if( tt != NULL )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
int i;
|
2011-03-31 20:26:43 +00:00
|
|
|
for( i=0; i<tt->tier_count; ++i )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
tr_tier * tier = &tt->tiers[i];
|
2009-09-25 21:05:59 +00:00
|
|
|
if( tier->isRunning )
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
const tr_announce_event e = TR_ANNOUNCE_EVENT_STOPPED;
|
|
|
|
tr_announce_request * req = announce_request_new( announcer, tor, tier, e );
|
2011-06-19 18:26:04 +00:00
|
|
|
|
|
|
|
if( tr_ptrArrayFindSorted( &announcer->stops, req, compareStops ) != NULL )
|
|
|
|
announce_request_free( req );
|
|
|
|
else
|
|
|
|
tr_ptrArrayInsertSorted( &announcer->stops, req, compareStops );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tiersFree( tor->tiers );
|
|
|
|
tor->tiers = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
static int
|
2011-03-11 04:19:01 +00:00
|
|
|
getRetryInterval( const tr_tracker * t )
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
|
|
|
int minutes;
|
|
|
|
const unsigned int jitter_seconds = tr_cryptoWeakRandInt( 60 );
|
2011-03-14 16:52:47 +00:00
|
|
|
switch( t->consecutiveFailures ) {
|
2011-02-05 16:45:06 +00:00
|
|
|
case 0: minutes = 1; break;
|
2011-02-05 18:46:10 +00:00
|
|
|
case 1: minutes = 5; break;
|
|
|
|
case 2: minutes = 15; break;
|
|
|
|
case 3: minutes = 30; break;
|
|
|
|
case 4: minutes = 60; break;
|
|
|
|
default: minutes = 120; break;
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
return ( minutes * 60 ) + jitter_seconds;
|
|
|
|
}
|
|
|
|
|
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;
|
2011-03-26 12:06:04 +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
|
|
|
};
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static void
|
2011-03-11 04:19:01 +00:00
|
|
|
on_announce_error( tr_tier * tier, const char * err, tr_announce_event e )
|
|
|
|
{
|
|
|
|
int interval;
|
|
|
|
|
2011-03-14 16:52:47 +00:00
|
|
|
/* increment the error count */
|
|
|
|
if( tier->currentTracker != NULL )
|
|
|
|
++tier->currentTracker->consecutiveFailures;
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/* set the error message */
|
|
|
|
dbgmsg( tier, "%s", err );
|
|
|
|
tr_torinf( tier->tor, "%s", err );
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr, err, sizeof( tier->lastAnnounceStr ) );
|
|
|
|
|
|
|
|
/* switch to the next tracker */
|
|
|
|
tierIncrementTracker( tier );
|
|
|
|
|
|
|
|
/* schedule a reannounce */
|
|
|
|
interval = getRetryInterval( tier->currentTracker );
|
|
|
|
dbgmsg( tier, "Retrying announce in %d seconds.", interval );
|
2011-04-10 16:26:55 +00:00
|
|
|
tr_torinf( tier->tor, "Retrying announce in %d seconds.", interval );
|
2011-03-11 04:19:01 +00:00
|
|
|
tier_announce_event_push( tier, e, tr_time( ) + interval );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-26 12:06:04 +00:00
|
|
|
on_announce_done( const tr_announce_response * response,
|
2011-03-11 04:19:01 +00:00
|
|
|
void * vdata )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
struct announce_data * data = vdata;
|
2011-03-26 12:06:04 +00:00
|
|
|
tr_announcer * announcer = data->session->announcer;
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_tier * tier = getTier( announcer, response->info_hash, data->tierId );
|
2011-01-29 19:04:02 +00:00
|
|
|
const time_t now = tr_time( );
|
2011-03-11 04:19:01 +00:00
|
|
|
const tr_announce_event event = data->event;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 05:48:57 +00:00
|
|
|
if( announcer )
|
|
|
|
++announcer->slotsAvailable;
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if( tier != NULL )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +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,
|
|
|
|
response->tracker_id_str ? response->tracker_id_str : "none",
|
|
|
|
response->pex_count,
|
|
|
|
response->pex6_count,
|
|
|
|
response->errmsg ? response->errmsg : "none",
|
|
|
|
response->warning ? 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
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if( !response->did_connect )
|
|
|
|
{
|
|
|
|
on_announce_error( tier, _( "Could not connect to tracker" ), event );
|
|
|
|
}
|
|
|
|
else if( response->did_timeout )
|
|
|
|
{
|
|
|
|
on_announce_error( tier, _( "Tracker did not respond" ), event );
|
|
|
|
}
|
|
|
|
else if( response->errmsg )
|
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... */
|
|
|
|
if( tier->tor->info.trackerCount < 2 )
|
|
|
|
publishError( tier, response->errmsg );
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
on_announce_error( tier, response->errmsg, event );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-11 15:44:24 +00:00
|
|
|
int i;
|
2011-03-11 04:19:01 +00:00
|
|
|
const char * 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;
|
2011-03-22 15:19:54 +00:00
|
|
|
const bool isStopped = event == TR_ANNOUNCE_EVENT_STOPPED;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
publishErrorClear( tier );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if(( tracker = tier->currentTracker ))
|
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
|
|
|
|
2011-10-09 00:18:18 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( response->leechers >= 0 )
|
|
|
|
{
|
2011-10-14 00:27:14 +00:00
|
|
|
tracker->leecherCount = leechers = response->leechers;
|
2011-10-09 00:18:18 +00:00
|
|
|
++scrape_fields;
|
|
|
|
}
|
|
|
|
if( response->downloads >= 0 )
|
|
|
|
{
|
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
|
|
|
|
|
|
|
if(( str = response->tracker_id_str ))
|
|
|
|
{
|
|
|
|
tr_free( tracker->tracker_id_str );
|
|
|
|
tracker->tracker_id_str = tr_strdup( str );
|
|
|
|
}
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
if(( str = response->warning ))
|
2011-01-29 19:04:02 +00:00
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
tr_strlcpy( tier->lastAnnounceStr, str,
|
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
2011-03-11 04:19:01 +00:00
|
|
|
dbgmsg( tier, "tracker gave \"%s\"", str );
|
|
|
|
publishWarning( tier, str );
|
|
|
|
}
|
2011-10-09 00:21:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr, _( "Success" ),
|
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if(( i = response->min_interval ))
|
|
|
|
tier->announceMinIntervalSec = i;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if(( i = response->interval ))
|
|
|
|
tier->announceIntervalSec = i;
|
|
|
|
|
|
|
|
if( response->pex_count > 0 )
|
2011-10-14 00:27:14 +00:00
|
|
|
publishPeersPex( tier, seeders, leechers,
|
2011-03-11 04:19:01 +00:00
|
|
|
response->pex, response->pex_count );
|
|
|
|
|
|
|
|
if( response->pex6_count > 0 )
|
2011-10-14 00:27:14 +00:00
|
|
|
publishPeersPex( tier, seeders, leechers,
|
2011-03-11 04:19:01 +00:00
|
|
|
response->pex6, response->pex6_count );
|
|
|
|
|
|
|
|
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 */
|
|
|
|
if( scrape_fields >= 3 )
|
2011-07-31 00:58:05 +00:00
|
|
|
{
|
2011-08-14 14:45:54 +00:00
|
|
|
tr_tordbg( tier->tor, "Announce response contained scrape info; "
|
|
|
|
"rescheduling next scrape to %d seconds from now.",
|
|
|
|
tier->scrapeIntervalSec );
|
2011-07-31 00:58:05 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time( announcer->session, tier, tier->scrapeIntervalSec );
|
|
|
|
tier->lastScrapeTime = now;
|
|
|
|
tier->lastScrapeSucceeded = true;
|
|
|
|
}
|
|
|
|
else if( tier->lastScrapeTime + tier->scrapeIntervalSec <= now )
|
|
|
|
{
|
|
|
|
tier->scrapeAt = get_next_scrape_time( announcer->session, tier, 0 );
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastAnnounceSucceeded = true;
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->lastAnnouncePeerCount = response->pex_count
|
|
|
|
+ response->pex6_count;
|
|
|
|
|
2011-01-29 19:04:02 +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 */
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if( !isStopped && !tier->announce_event_count )
|
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;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( data );
|
|
|
|
}
|
|
|
|
|
2011-06-19 18:26:04 +00:00
|
|
|
static void
|
|
|
|
announce_request_free( tr_announce_request * req )
|
|
|
|
{
|
|
|
|
tr_free( req->tracker_id_str );
|
|
|
|
tr_free( req->url );
|
|
|
|
tr_free( req );
|
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +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
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_session * session = announcer->session;
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2011-03-11 05:48:57 +00:00
|
|
|
if( !memcmp( request->url, "http", 4 ) )
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_tracker_http_announce( session, request, callback, callback_data );
|
2011-03-13 00:18:11 +00:00
|
|
|
else if( !memcmp( request->url, "udp://", 6 ) )
|
|
|
|
tr_tracker_udp_announce( session, request, callback, callback_data );
|
2011-03-11 04:19:01 +00:00
|
|
|
else
|
2011-03-20 15:33:45 +00:00
|
|
|
tr_err( "Unsupported url: %s", request->url );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-06-19 18:26:04 +00:00
|
|
|
announce_request_free( request );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tierAnnounce( tr_announcer * announcer, tr_tier * tier )
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_announce_event announce_event;
|
2011-03-11 05:48:57 +00:00
|
|
|
tr_announce_request * req;
|
|
|
|
struct announce_data * data;
|
|
|
|
const tr_torrent * tor = tier->tor;
|
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
assert( !tier->isAnnouncing );
|
2011-03-11 05:48:57 +00:00
|
|
|
assert( tier->announce_event_count > 0 );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 05:48:57 +00:00
|
|
|
announce_event = tier_announce_event_pull( tier );
|
|
|
|
req = announce_request_new( announcer, tor, tier, announce_event );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 05:48:57 +00:00
|
|
|
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
|
|
|
|
2011-03-11 05:48:57 +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
|
|
|
***/
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
static void
|
2011-07-23 17:14:12 +00:00
|
|
|
on_scrape_error( tr_session * session, tr_tier * tier, const char * 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 */
|
|
|
|
if( tier->currentTracker != NULL )
|
|
|
|
++tier->currentTracker->consecutiveFailures;
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/* set the error message */
|
|
|
|
dbgmsg( tier, "Scrape error: %s", errmsg );
|
|
|
|
tr_torinf( 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 */
|
|
|
|
tierIncrementTracker( tier );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
/* schedule a rescrape */
|
2011-04-10 14:15:52 +00:00
|
|
|
interval = getRetryInterval( tier->currentTracker );
|
2011-04-10 00:05:18 +00:00
|
|
|
dbgmsg( tier, "Retrying scrape in %zu seconds.", (size_t)interval );
|
|
|
|
tr_torinf( tier->tor, "Retrying scrape in %zu seconds.", (size_t)interval );
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastScrapeSucceeded = false;
|
2011-07-23 17:14:12 +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
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
static tr_tier *
|
2011-03-31 20:26:43 +00:00
|
|
|
find_tier( tr_torrent * tor, const char * scrape )
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
2011-03-31 20:26:43 +00:00
|
|
|
struct tr_torrent_tiers * tt = tor->tiers;
|
2011-03-11 15:44:24 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
for( i=0; tt && i<tt->tier_count; ++i ) {
|
|
|
|
const tr_tracker * const tracker = tt->tiers[i].currentTracker;
|
|
|
|
if( tracker && !tr_strcmp0( scrape, tracker->scrape ) )
|
|
|
|
return &tt->tiers[i];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-30 00:08:24 +00:00
|
|
|
on_scrape_done( const tr_scrape_response * response, void * vsession )
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2011-03-26 12:06:04 +00:00
|
|
|
tr_session * session = vsession;
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_announcer * announcer = session->announcer;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
for( i=0; i<response->row_count; ++i )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
const struct tr_scrape_response_row * row = &response->rows[i];
|
|
|
|
tr_torrent * tor = tr_torrentFindFromHash( session, row->info_hash );
|
2009-11-10 17:03:23 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if( tor != NULL )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_tier * tier = find_tier( tor, response->url );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if( tier != NULL )
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
response->errmsg ? response->errmsg : "none" );
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if( !response->did_connect )
|
|
|
|
{
|
2011-07-23 17:14:12 +00:00
|
|
|
on_scrape_error( session, tier, _( "Could not connect to tracker" ) );
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else if( response->did_timeout )
|
|
|
|
{
|
2011-07-23 17:14:12 +00:00
|
|
|
on_scrape_error( session, tier, _( "Tracker did not respond" ) );
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else if( response->errmsg )
|
|
|
|
{
|
2011-07-23 17:14:12 +00:00
|
|
|
on_scrape_error( session, tier, response->errmsg );
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_tracker * tracker;
|
2009-10-28 04:53:39 +00:00
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
tier->lastScrapeSucceeded = true;
|
2011-03-11 04:19:01 +00:00
|
|
|
tier->scrapeIntervalSec = MAX( DEFAULT_SCRAPE_INTERVAL_SEC,
|
|
|
|
response->min_request_interval );
|
2011-07-23 17:14:12 +00:00
|
|
|
tier->scrapeAt = get_next_scrape_time( session, tier, tier->scrapeIntervalSec );
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_tordbg( tier->tor, "Scrape successful. Rescraping in %d seconds.",
|
|
|
|
tier->scrapeIntervalSec );
|
|
|
|
|
|
|
|
if(( tracker = tier->currentTracker ))
|
|
|
|
{
|
2011-10-14 00:27:14 +00:00
|
|
|
if( row->seeders >= 0 )
|
|
|
|
tracker->seederCount = row->seeders;
|
|
|
|
if( row->leechers >= 0 )
|
|
|
|
tracker->leecherCount = row->leechers;
|
|
|
|
if( row->downloads >= 0 )
|
|
|
|
tracker->downloadCount = row->downloads;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
if( announcer )
|
|
|
|
++announcer->slotsAvailable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
scrape_request_delegate( tr_announcer * announcer,
|
2011-04-27 17:30:53 +00:00
|
|
|
const tr_scrape_request * request,
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_scrape_response_func * callback,
|
|
|
|
void * callback_data )
|
|
|
|
{
|
|
|
|
tr_session * session = announcer->session;
|
|
|
|
|
2011-03-11 13:04:09 +00:00
|
|
|
if( !memcmp( request->url, "http", 4 ) )
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_tracker_http_scrape( session, request, callback, callback_data );
|
2011-03-13 00:18:11 +00:00
|
|
|
else if( !memcmp( request->url, "udp://", 6 ) )
|
|
|
|
tr_tracker_udp_scrape( session, request, callback, callback_data );
|
2011-03-11 04:19:01 +00:00
|
|
|
else
|
2011-03-20 15:33:45 +00:00
|
|
|
tr_err( "Unsupported url: %s", request->url );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-11 15:11:37 +00:00
|
|
|
multiscrape( tr_announcer * announcer, tr_ptrArray * tiers )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-11 15:11:37 +00:00
|
|
|
int i;
|
|
|
|
int request_count = 0;
|
|
|
|
const time_t now = tr_time( );
|
|
|
|
const int tier_count = tr_ptrArraySize( tiers );
|
|
|
|
const int max_request_count = MIN( announcer->slotsAvailable, tier_count );
|
|
|
|
tr_scrape_request * requests = tr_new0( tr_scrape_request, max_request_count );
|
|
|
|
|
|
|
|
/* batch as many info_hashes into a request as we can */
|
|
|
|
for( i=0; i<tier_count; ++i )
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
tr_tier * tier = tr_ptrArrayNth( tiers, i );
|
|
|
|
char * url = tier->currentTracker->scrape;
|
|
|
|
const uint8_t * hash = tier->tor->info.hash;
|
|
|
|
|
|
|
|
/* if there's a request with this scrape URL and a free slot, use it */
|
|
|
|
for( j=0; j<request_count; ++j )
|
|
|
|
{
|
|
|
|
tr_scrape_request * req = &requests[j];
|
|
|
|
|
|
|
|
if( req->info_hash_count >= TR_MULTISCRAPE_MAX )
|
|
|
|
continue;
|
|
|
|
if( tr_strcmp0( req->url, url ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise, if there's room for another request, build a new one */
|
|
|
|
if( ( j==request_count ) && ( request_count < max_request_count ) )
|
|
|
|
{
|
|
|
|
tr_scrape_request * req = &requests[request_count++];
|
|
|
|
req->url = url;
|
|
|
|
tier_build_log_name( tier, req->log_name, sizeof( req->log_name ) );
|
|
|
|
|
|
|
|
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 */
|
|
|
|
for( i=0; i<request_count; ++i )
|
2011-03-26 12:06:04 +00:00
|
|
|
scrape_request_delegate( announcer, &requests[i], on_scrape_done, announcer->session );
|
2011-03-11 15:11:37 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( requests );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flushCloseMessages( tr_announcer * announcer )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int n = tr_ptrArraySize( &announcer->stops );
|
|
|
|
|
|
|
|
for( i=0; i<n; ++i )
|
2011-03-11 04:19:01 +00:00
|
|
|
announce_request_delegate( announcer, tr_ptrArrayNth( &announcer->stops, i ), NULL, NULL );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
tr_ptrArrayClear( &announcer->stops );
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2009-09-25 21:05:59 +00:00
|
|
|
tierNeedsToAnnounce( const tr_tier * tier, const time_t now )
|
|
|
|
{
|
2009-11-10 17:03:23 +00:00
|
|
|
return !tier->isAnnouncing
|
2009-09-25 21:05:59 +00:00
|
|
|
&& !tier->isScraping
|
2010-02-14 05:16:04 +00:00
|
|
|
&& ( tier->announceAt != 0 )
|
2010-02-10 02:59:15 +00:00
|
|
|
&& ( tier->announceAt <= now )
|
2011-03-11 04:19:01 +00:00
|
|
|
&& ( tier->announce_event_count > 0 );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2009-09-25 21:05:59 +00:00
|
|
|
tierNeedsToScrape( const tr_tier * tier, const time_t now )
|
|
|
|
{
|
2010-02-05 01:16:30 +00:00
|
|
|
return ( !tier->isScraping )
|
2009-10-28 04:53:39 +00:00
|
|
|
&& ( tier->scrapeAt != 0 )
|
2009-09-26 06:43:44 +00:00
|
|
|
&& ( tier->scrapeAt <= now )
|
|
|
|
&& ( tier->currentTracker != NULL )
|
|
|
|
&& ( tier->currentTracker->scrape != NULL );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
static int
|
|
|
|
compareTiers( const void * va, const void * vb )
|
|
|
|
{
|
2011-02-05 18:46:10 +00:00
|
|
|
int ret;
|
2011-02-05 16:45:06 +00:00
|
|
|
const tr_tier * a = *(const tr_tier**)va;
|
|
|
|
const tr_tier * b = *(const tr_tier**)vb;
|
|
|
|
|
2011-02-05 18:46:10 +00:00
|
|
|
/* primary key: larger stats come before smaller */
|
|
|
|
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 */
|
|
|
|
if( !ret && ( a->announceAt != b->announceAt ) )
|
|
|
|
ret = a->announceAt < b->announceAt ? -1 : 1;
|
2011-03-04 23:26:10 +00:00
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static void
|
|
|
|
announceMore( tr_announcer * announcer )
|
|
|
|
{
|
2011-03-14 02:40:39 +00:00
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
tr_torrent * tor;
|
|
|
|
tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
|
|
|
|
tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-11 05:48:57 +00:00
|
|
|
dbgmsg( NULL, "announceMore: slotsAvailable is %d", announcer->slotsAvailable );
|
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
if( announcer->slotsAvailable < 1 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* build a list of tiers that need to be announced */
|
|
|
|
tor = NULL;
|
|
|
|
while(( tor = tr_torrentNext( announcer->session, tor ))) {
|
2011-03-31 20:26:43 +00:00
|
|
|
struct tr_torrent_tiers * tt = tor->tiers;
|
|
|
|
for( i=0; tt && i<tt->tier_count; ++i ) {
|
|
|
|
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
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* if there are more tiers than slots available, prioritize */
|
|
|
|
n = tr_ptrArraySize( &announceMe );
|
|
|
|
if( n > announcer->slotsAvailable )
|
|
|
|
qsort( tr_ptrArrayBase(&announceMe), n, sizeof(tr_tier*), compareTiers );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* announce some */
|
|
|
|
n = MIN( tr_ptrArraySize( &announceMe ), announcer->slotsAvailable );
|
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
|
2011-04-07 19:54:30 +00:00
|
|
|
tr_tordbg( tier->tor, "%s", "Announcing to tracker" );
|
2011-03-14 02:40:39 +00:00
|
|
|
dbgmsg( tier, "announcing tier %d of %d", i, n );
|
|
|
|
tierAnnounce( announcer, tier );
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* scrape some */
|
|
|
|
multiscrape( announcer, &scrapeMe );
|
2009-12-02 05:30:46 +00:00
|
|
|
|
2011-03-14 02:40:39 +00:00
|
|
|
/* cleanup */
|
|
|
|
tr_ptrArrayDestruct( &scrapeMe, NULL );
|
|
|
|
tr_ptrArrayDestruct( &announceMe, NULL );
|
|
|
|
}
|
2009-10-12 23:16:51 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static void
|
|
|
|
onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer )
|
|
|
|
{
|
|
|
|
tr_announcer * announcer = vannouncer;
|
2011-03-17 18:51:31 +00:00
|
|
|
tr_session * session = announcer->session;
|
2011-03-22 15:19:54 +00:00
|
|
|
const bool is_closing = session->isClosed;
|
2011-03-17 18:51:31 +00:00
|
|
|
const time_t now = tr_time( );
|
|
|
|
|
|
|
|
tr_sessionLock( session );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* maybe send out some "stopped" messages for closed torrents */
|
|
|
|
flushCloseMessages( announcer );
|
|
|
|
|
|
|
|
/* maybe send out some announcements to trackers */
|
2011-03-28 13:37:46 +00:00
|
|
|
if( !is_closing )
|
2011-03-17 18:51:31 +00:00
|
|
|
announceMore( announcer );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-13 06:38:54 +00:00
|
|
|
/* TAU upkeep */
|
|
|
|
if( announcer->tauUpkeepAt <= now ) {
|
|
|
|
announcer->tauUpkeepAt = now + TAU_UPKEEP_INTERVAL_SECS;
|
2011-03-17 18:51:31 +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 */
|
|
|
|
tr_timerAdd( announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
|
2010-01-04 09:11:27 +00:00
|
|
|
|
2011-03-17 18:51:31 +00:00
|
|
|
tr_sessionUnlock( session );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_tracker_stat *
|
2011-03-14 02:40:39 +00:00
|
|
|
tr_announcerStats( const tr_torrent * torrent, int * setmeTrackerCount )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int out = 0;
|
|
|
|
tr_tracker_stat * ret;
|
2011-03-31 20:26:43 +00:00
|
|
|
struct tr_torrent_tiers * tt;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
assert( tr_isTorrent( torrent ) );
|
2011-01-18 23:44:36 +00:00
|
|
|
assert( tr_torrentIsLocked( torrent ) );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
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;
|
|
|
|
ret = tr_new0( tr_tracker_stat, tt->tracker_count );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* populate the stats */
|
2011-03-31 20:26:43 +00:00
|
|
|
for( i=0; i<tt->tier_count; ++i )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
int j;
|
2011-03-31 20:26:43 +00:00
|
|
|
const tr_tier * const tier = &tt->tiers[i];
|
|
|
|
for( j=0; j<tier->tracker_count; ++j )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
const tr_tracker * const tracker = &tier->trackers[j];
|
|
|
|
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;
|
2011-03-14 02:40:39 +00:00
|
|
|
tr_strlcpy( st->host, tracker->key, sizeof( st->host ) );
|
2009-09-26 03:36:58 +00:00
|
|
|
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;
|
2010-09-03 04:30:43 +00:00
|
|
|
if( tracker->scrape )
|
|
|
|
tr_strlcpy( st->scrape, tracker->scrape, sizeof( st->scrape ) );
|
|
|
|
else
|
|
|
|
st->scrape[0] = '\0';
|
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;
|
|
|
|
|
2009-10-10 00:17:00 +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
|
|
|
|
{
|
|
|
|
if(( st->hasScraped = tier->lastScrapeTime != 0 )) {
|
|
|
|
st->lastScrapeTime = tier->lastScrapeTime;
|
|
|
|
st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
|
2010-02-25 23:06:05 +00:00
|
|
|
st->lastScrapeTimedOut = tier->lastScrapeTimedOut;
|
2011-03-14 02:40:39 +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
|
|
|
|
2009-10-09 21:30:34 +00:00
|
|
|
if( tier->isScraping )
|
|
|
|
st->scrapeState = TR_TRACKER_ACTIVE;
|
2009-10-28 04:53:39 +00:00
|
|
|
else if( !tier->scrapeAt )
|
|
|
|
st->scrapeState = TR_TRACKER_INACTIVE;
|
2009-10-10 20:14:26 +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
|
|
|
|
st->scrapeState = TR_TRACKER_QUEUED;
|
2009-09-30 20:49:52 +00:00
|
|
|
|
|
|
|
st->lastAnnounceStartTime = tier->lastAnnounceStartTime;
|
|
|
|
|
|
|
|
if(( st->hasAnnounced = tier->lastAnnounceTime != 0 )) {
|
|
|
|
st->lastAnnounceTime = tier->lastAnnounceTime;
|
2011-03-14 02:40:39 +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
|
|
|
}
|
|
|
|
|
2009-10-09 21:30:34 +00:00
|
|
|
if( tier->isAnnouncing )
|
|
|
|
st->announceState = TR_TRACKER_ACTIVE;
|
2009-10-28 04:53:39 +00:00
|
|
|
else if( !torrent->isRunning || !tier->announceAt )
|
2009-10-09 21:30:34 +00:00
|
|
|
st->announceState = TR_TRACKER_INACTIVE;
|
2009-10-10 20:14:26 +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
|
|
|
|
st->announceState = TR_TRACKER_QUEUED;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_announcerStatsFree( tr_tracker_stat * trackers,
|
2009-09-26 03:36:58 +00:00
|
|
|
int trackerCount UNUSED )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
tr_free( trackers );
|
|
|
|
}
|
2011-02-05 16:45:06 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2011-03-31 20:26:43 +00:00
|
|
|
copy_tier_attributes_impl( struct tr_tier * tgt, int trackerIndex, const tr_tier * src )
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
const tr_tier keep = *tgt;
|
|
|
|
|
|
|
|
/* sanity clause */
|
|
|
|
assert( trackerIndex < tgt->tracker_count );
|
|
|
|
assert( !tr_strcmp0( tgt->trackers[trackerIndex].announce, src->currentTracker->announce ) );
|
2011-02-05 16:45:06 +00:00
|
|
|
|
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;
|
2011-03-31 20:26:43 +00:00
|
|
|
tgt->announce_events = tr_memdup( src->announce_events, sizeof( tr_announce_event ) * src->announce_event_count );
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-31 20:26:43 +00:00
|
|
|
copy_tier_attributes( struct tr_torrent_tiers * tt, const tr_tier * src )
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
int i, j;
|
|
|
|
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 */
|
|
|
|
for( i=0; !found && i<tt->tier_count; ++i )
|
|
|
|
for( j=0; !found && j<tt->tiers[i].tracker_count; ++j )
|
|
|
|
if(( found = !tr_strcmp0( src->currentTracker->announce, tt->tiers[i].trackers[j].announce )))
|
|
|
|
copy_tier_attributes_impl( &tt->tiers[i], j, src );
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-02-05 18:46:10 +00:00
|
|
|
tr_announcerResetTorrent( tr_announcer * announcer UNUSED, tr_torrent * tor )
|
2011-02-05 16:45:06 +00:00
|
|
|
{
|
2011-03-31 20:26:43 +00:00
|
|
|
int i;
|
|
|
|
const time_t now = tr_time( );
|
|
|
|
struct tr_torrent_tiers * tt = tor->tiers;
|
|
|
|
tr_torrent_tiers old = *tt;
|
2011-02-05 16:45:06 +00:00
|
|
|
|
2011-03-31 20:26:43 +00:00
|
|
|
assert( tt != NULL );
|
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 */
|
|
|
|
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 */
|
|
|
|
for( i=0; i<old.tier_count; ++i )
|
|
|
|
if( old.tiers[i].currentTracker != NULL )
|
|
|
|
copy_tier_attributes( tt, &old.tiers[i] );
|
2011-02-05 16:45:06 +00:00
|
|
|
|
|
|
|
/* kickstart any tiers that didn't get started */
|
|
|
|
if( tor->isRunning )
|
2011-03-31 20:26:43 +00:00
|
|
|
for( i=0; i<tt->tier_count; ++i )
|
|
|
|
if( !tt->tiers[i].wasCopied )
|
|
|
|
tier_announce_event_push( &tt->tiers[i], TR_ANNOUNCE_EVENT_STARTED, now );
|
2011-02-05 16:45:06 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
2011-03-31 20:26:43 +00:00
|
|
|
tiersDestruct( &old );
|
2011-02-05 16:45:06 +00:00
|
|
|
}
|