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>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/event.h>
|
|
|
|
#include <event2/http.h> /* for HTTP_OK */
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "announcer.h"
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "net.h"
|
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"
|
2009-10-12 23:16:51 +00:00
|
|
|
#include "tr-dht.h"
|
2010-05-08 08:42:45 +00:00
|
|
|
#include "tr-lpd.h"
|
2009-09-25 21:05:59 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "web.h"
|
|
|
|
|
2010-02-14 03:40:39 +00:00
|
|
|
#define STARTED "started"
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
#define dbgmsg( tier, ... ) \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) do { \
|
|
|
|
char name[128]; \
|
|
|
|
tr_snprintf( name, sizeof( name ), "[%s--%s]", tr_torrentName( tier->tor ), \
|
2011-02-05 18:46:10 +00:00
|
|
|
( tier->currentTracker ? tier->currentTracker->hostname : "" ) ); \
|
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 ),
|
|
|
|
|
|
|
|
/* the length of the 'key' argument passed in tracker requests */
|
|
|
|
KEYLEN = 8,
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
/* this is an upper limit for the frequency of LDS announces */
|
2010-05-08 08:42:45 +00:00
|
|
|
LPD_HOUSEKEEPING_INTERVAL_SECS = 30
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Since we can't poll a tr_torrent's fields after it's destroyed,
|
|
|
|
* we pre-build the "stop" announcement message when a torrent
|
|
|
|
* is removed from Transmission
|
|
|
|
*/
|
|
|
|
struct stop_message
|
|
|
|
{
|
|
|
|
char * url;
|
2010-10-24 21:35:23 +00:00
|
|
|
uint64_t up;
|
|
|
|
uint64_t down;
|
2009-09-25 21:05:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
stopFree( struct stop_message * stop )
|
|
|
|
{
|
|
|
|
tr_free( stop->url );
|
|
|
|
tr_free( stop );
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static int
|
|
|
|
compareStops( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const struct stop_message * a = va;
|
|
|
|
const struct stop_message * b = vb;
|
|
|
|
return compareTransfer( a->up, a->down, b->up, b->down);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "global" (per-tr_session) fields
|
|
|
|
*/
|
|
|
|
typedef struct tr_announcer
|
|
|
|
{
|
|
|
|
tr_ptrArray stops; /* struct stop_message */
|
|
|
|
tr_session * session;
|
|
|
|
struct event * upkeepTimer;
|
2010-02-05 01:16:30 +00:00
|
|
|
int slotsAvailable;
|
2010-05-08 08:42:45 +00:00
|
|
|
time_t lpdHouseKeepingAt;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
tr_announcer;
|
|
|
|
|
2009-12-02 05:30:46 +00:00
|
|
|
tr_bool
|
|
|
|
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 18:46:10 +00:00
|
|
|
/* format: hostname + ':' + port */
|
|
|
|
static char *
|
|
|
|
getHostName( const char * url )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-02-05 18:46:10 +00:00
|
|
|
int port = 0;
|
|
|
|
char * host = NULL;
|
|
|
|
char * ret;
|
|
|
|
tr_urlParse( url, -1, NULL, &host, &port, NULL );
|
|
|
|
ret = tr_strdup_printf( "%s:%d", ( host ? host : "invalid" ), port );
|
|
|
|
tr_free( host );
|
|
|
|
return ret;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2010-05-01 16:04:00 +00:00
|
|
|
static inline time_t
|
|
|
|
calcRescheduleWithJitter( const int minPeriod )
|
|
|
|
{
|
|
|
|
const double jitterFac = 0.1;
|
|
|
|
|
|
|
|
assert( minPeriod > 0 );
|
|
|
|
|
|
|
|
return tr_time()
|
|
|
|
+ minPeriod
|
|
|
|
+ tr_cryptoWeakRandInt( (int) ( minPeriod * jitterFac ) + 1 );
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
const time_t lpdAt =
|
2010-05-08 08:42:45 +00:00
|
|
|
calcRescheduleWithJitter( LPD_HOUSEKEEPING_INTERVAL_SECS / 3 );
|
2010-05-01 16:04:00 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
a = tr_new0( tr_announcer, 1 );
|
|
|
|
a->stops = TR_PTR_ARRAY_INIT;
|
|
|
|
a->session = session;
|
2010-02-05 01:16:30 +00:00
|
|
|
a->slotsAvailable = MAX_CONCURRENT_TASKS;
|
2011-02-05 16:45:06 +00:00
|
|
|
a->lpdHouseKeepingAt = lpdAt;
|
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 );
|
|
|
|
|
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-02-05 18:46:10 +00:00
|
|
|
char * hostname;
|
2009-09-25 21:05:59 +00:00
|
|
|
char * announce;
|
|
|
|
char * scrape;
|
|
|
|
|
|
|
|
char * tracker_id;
|
|
|
|
|
|
|
|
int seederCount;
|
|
|
|
int leecherCount;
|
|
|
|
int downloadCount;
|
|
|
|
int downloaderCount;
|
2010-02-13 21:58:38 +00:00
|
|
|
|
2011-01-20 22:51:05 +00:00
|
|
|
int consecutiveAnnounceFailures;
|
|
|
|
|
2010-01-13 22:46:22 +00:00
|
|
|
uint32_t id;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
/* Sent as the "key" argument in tracker requests
|
2009-09-25 21:05:59 +00:00
|
|
|
* to verify us if our IP address changes.
|
2009-10-02 04:54:02 +00:00
|
|
|
* This is immutable for the life of the tracker object.
|
|
|
|
* The +1 is for '\0' */
|
2010-10-17 18:32:55 +00:00
|
|
|
unsigned char key_param[KEYLEN + 1];
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
tr_tracker_item;
|
|
|
|
|
|
|
|
static void
|
2010-10-17 18:32:55 +00:00
|
|
|
generateKeyParam( unsigned char * msg, size_t msglen )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const char * pool = "abcdefghijklmnopqrstuvwxyz0123456789";
|
2010-07-01 05:14:34 +00:00
|
|
|
const int poolSize = 36;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-10-17 18:32:55 +00:00
|
|
|
tr_cryptoRandBuf( msg, msglen );
|
2009-09-25 21:05:59 +00:00
|
|
|
for( i=0; i<msglen; ++i )
|
2010-10-17 18:32:55 +00:00
|
|
|
msg[i] = pool[ msg[i] % poolSize ];
|
|
|
|
msg[msglen] = '\0';
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tr_tracker_item*
|
2011-02-05 18:46:10 +00:00
|
|
|
trackerNew( const char * announce,
|
|
|
|
const char * scrape,
|
|
|
|
uint32_t id )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
tr_tracker_item * tracker = tr_new0( tr_tracker_item, 1 );
|
2011-02-05 18:46:10 +00:00
|
|
|
tracker->hostname = getHostName( announce );
|
2009-09-25 21:05:59 +00:00
|
|
|
tracker->announce = tr_strdup( announce );
|
|
|
|
tracker->scrape = tr_strdup( scrape );
|
2010-01-13 22:46:22 +00:00
|
|
|
tracker->id = id;
|
2009-09-25 21:05:59 +00:00
|
|
|
generateKeyParam( tracker->key_param, KEYLEN );
|
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
|
|
|
return tracker;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
trackerFree( void * vtracker )
|
|
|
|
{
|
|
|
|
tr_tracker_item * tracker = vtracker;
|
|
|
|
|
|
|
|
tr_free( tracker->tracker_id );
|
|
|
|
tr_free( tracker->scrape );
|
2011-02-05 16:45:06 +00:00
|
|
|
tr_free( tracker->announce );
|
2011-02-05 18:46:10 +00:00
|
|
|
tr_free( tracker->hostname );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_free( tracker );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
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 */
|
2009-09-25 21:05:59 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
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
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_ptrArray trackers; /* tr_tracker_item */
|
|
|
|
tr_tracker_item * 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;
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_bool lastScrapeSucceeded;
|
2010-02-25 23:06:05 +00:00
|
|
|
tr_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;
|
|
|
|
tr_bool lastAnnounceSucceeded;
|
|
|
|
tr_bool lastAnnounceTimedOut;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-10 03:57:41 +00:00
|
|
|
tr_ptrArray announceEvents; /* const char* */
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* unique lookup key */
|
|
|
|
int key;
|
|
|
|
|
|
|
|
int scrapeIntervalSec;
|
|
|
|
int announceIntervalSec;
|
|
|
|
int announceMinIntervalSec;
|
|
|
|
|
|
|
|
int lastAnnouncePeerCount;
|
|
|
|
|
|
|
|
tr_bool isRunning;
|
|
|
|
tr_bool isAnnouncing;
|
|
|
|
tr_bool isScraping;
|
2010-02-14 05:34:03 +00:00
|
|
|
tr_bool wasCopied;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
char lastAnnounceStr[128];
|
|
|
|
char lastScrapeStr[128];
|
|
|
|
}
|
|
|
|
tr_tier;
|
|
|
|
|
|
|
|
static tr_tier *
|
|
|
|
tierNew( tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_tier * t;
|
|
|
|
static int nextKey = 1;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
t = tr_new0( tr_tier, 1 );
|
|
|
|
t->key = nextKey++;
|
2010-02-10 02:59:15 +00:00
|
|
|
t->announceEvents = TR_PTR_ARRAY_INIT;
|
2009-09-25 21:05:59 +00:00
|
|
|
t->trackers = TR_PTR_ARRAY_INIT;
|
|
|
|
t->currentTracker = NULL;
|
|
|
|
t->currentTrackerIndex = -1;
|
|
|
|
t->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
|
|
|
|
t->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
|
|
|
|
t->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
|
2010-02-11 03:12:03 +00:00
|
|
|
t->scrapeAt = now + tr_cryptoWeakRandInt( 60*5 );
|
2009-09-25 21:05:59 +00:00
|
|
|
t->tor = tor;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tierFree( void * vtier )
|
|
|
|
{
|
|
|
|
tr_tier * tier = vtier;
|
|
|
|
tr_ptrArrayDestruct( &tier->trackers, trackerFree );
|
2010-02-10 02:59:15 +00:00
|
|
|
tr_ptrArrayDestruct( &tier->announceEvents, NULL );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_free( tier );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tierIncrementTracker( tr_tier * tier )
|
|
|
|
{
|
2009-12-02 15:16:29 +00:00
|
|
|
/* move our index to the next tracker in the tier */
|
2009-09-25 21:05:59 +00:00
|
|
|
const int i = ( tier->currentTrackerIndex + 1 )
|
|
|
|
% tr_ptrArraySize( &tier->trackers );
|
|
|
|
tier->currentTracker = tr_ptrArrayNth( &tier->trackers, i );
|
|
|
|
tier->currentTrackerIndex = i;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
tier->isAnnouncing = FALSE;
|
|
|
|
tier->isScraping = FALSE;
|
|
|
|
tier->lastAnnounceStartTime = 0;
|
|
|
|
tier->lastScrapeStartTime = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-05 18:46:10 +00:00
|
|
|
tierAddTracker( tr_tier * tier,
|
2009-09-25 21:05:59 +00:00
|
|
|
const char * announce,
|
2010-01-10 02:34:00 +00:00
|
|
|
const char * scrape,
|
2010-01-13 22:46:22 +00:00
|
|
|
uint32_t id )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-02-05 18:46:10 +00:00
|
|
|
tr_tracker_item * tracker = trackerNew( announce, scrape, id );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
tr_ptrArrayAppend( &tier->trackers, tracker );
|
|
|
|
dbgmsg( tier, "adding tracker %s", announce );
|
|
|
|
|
|
|
|
if( !tier->currentTracker )
|
|
|
|
tierIncrementTracker( tier );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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-02-05 16:45:06 +00:00
|
|
|
tr_ptrArray tiers; /* tr_tier */
|
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 )
|
|
|
|
{
|
|
|
|
tr_torrent_tiers * tiers = tr_new0( tr_torrent_tiers, 1 );
|
|
|
|
tiers->tiers = TR_PTR_ARRAY_INIT;
|
|
|
|
return tiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tiersFree( tr_torrent_tiers * tiers )
|
|
|
|
{
|
|
|
|
tr_ptrArrayDestruct( &tiers->tiers, tierFree );
|
|
|
|
tr_free( tiers );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_tier*
|
|
|
|
getTier( tr_announcer * announcer, int torrentId, int tierId )
|
|
|
|
{
|
|
|
|
tr_tier * tier = NULL;
|
|
|
|
|
|
|
|
if( announcer )
|
|
|
|
{
|
|
|
|
tr_torrent * tor = tr_torrentFindFromId( announcer->session, torrentId );
|
|
|
|
|
|
|
|
if( tor && tor->tiers )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
tr_ptrArray * tiers = &tor->tiers->tiers;
|
|
|
|
const int n = tr_ptrArraySize( tiers );
|
|
|
|
for( i=0; !tier && i<n; ++i )
|
|
|
|
{
|
|
|
|
tr_tier * tmp = tr_ptrArrayNth( tiers, i );
|
|
|
|
if( tmp->key == tierId )
|
|
|
|
tier = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** PUBLISH
|
|
|
|
***/
|
|
|
|
|
2010-05-19 19:02:25 +00:00
|
|
|
static const tr_tracker_event emptyEvent = { 0, NULL, NULL, NULL, 0, 0 };
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
publishMessage( tr_tier * tier, const char * msg, int type )
|
|
|
|
{
|
|
|
|
if( tier && tier->tor && tier->tor->tiers )
|
|
|
|
{
|
|
|
|
tr_torrent_tiers * tiers = tier->tor->tiers;
|
|
|
|
tr_tracker_event event = emptyEvent;
|
|
|
|
event.messageType = type;
|
|
|
|
event.text = msg;
|
2010-05-19 19:02:25 +00:00
|
|
|
event.tracker = tier->currentTracker ? tier->currentTracker->announce : NULL;
|
2010-06-19 14:33:10 +00:00
|
|
|
|
|
|
|
if( tiers->callback != NULL )
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2010-07-01 05:14:34 +00:00
|
|
|
static int8_t
|
2010-05-18 15:14:54 +00:00
|
|
|
getSeedProbability( int seeds, int leechers )
|
|
|
|
{
|
|
|
|
if( !seeds )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( seeds>=0 && 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
|
|
|
{
|
|
|
|
tr_tracker_event e = emptyEvent;
|
|
|
|
|
|
|
|
e.messageType = TR_TRACKER_PEERS;
|
2010-05-18 15:14:54 +00:00
|
|
|
e.seedProbability = getSeedProbability( seeds, leechers );
|
2011-01-22 17:45:54 +00:00
|
|
|
e.pex = pex;
|
|
|
|
e.pexCount = n;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-06-19 14:33:10 +00:00
|
|
|
if( tier->tor->tiers->callback != NULL )
|
|
|
|
tier->tor->tiers->callback( tier->tor, &e, NULL );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 17:45:54 +00:00
|
|
|
static size_t
|
|
|
|
publishPeersCompact( tr_tier * tier, int seeds, int leechers,
|
2009-09-25 21:05:59 +00:00
|
|
|
const void * compact, int compactLen )
|
|
|
|
{
|
2011-01-22 17:45:54 +00:00
|
|
|
size_t n = 0;
|
|
|
|
tr_pex * pex = tr_peerMgrCompactToPex( compact, compactLen, NULL, 0, &n );
|
|
|
|
publishPeersPex( tier, seeds, leechers, pex, n );
|
|
|
|
dbgmsg( tier, "got IPv4 list of %zu peers", n );
|
|
|
|
tr_free( pex );
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
publishPeersCompact6( tr_tier * tier, int seeds, int leechers,
|
|
|
|
const void * compact, int compactLen )
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
tr_pex * pex = tr_peerMgrCompact6ToPex( compact, compactLen, NULL, 0, &n );
|
|
|
|
dbgmsg( tier, "got IPv6 list of %zu peers", n );
|
|
|
|
publishPeersPex( tier, seeds, leechers, pex, n );
|
|
|
|
tr_free( pex );
|
|
|
|
return n;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 17:45:54 +00:00
|
|
|
static size_t
|
|
|
|
publishPeersDict( tr_tier * tier, int seeds, int leechers, tr_benc * peerList )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-01-22 17:45:54 +00:00
|
|
|
size_t i;
|
|
|
|
size_t n;
|
|
|
|
const size_t len = tr_bencListSize( peerList );
|
|
|
|
tr_pex * pex = tr_new0( tr_pex, len );
|
|
|
|
|
|
|
|
for( i=n=0; i<len; ++i )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-01-22 17:45:54 +00:00
|
|
|
int64_t port;
|
|
|
|
const char * ip;
|
|
|
|
tr_address addr;
|
|
|
|
tr_benc * peer = tr_bencListChild( peerList, i );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-01-22 17:45:54 +00:00
|
|
|
if( peer == NULL )
|
|
|
|
continue;
|
|
|
|
if( !tr_bencDictFindStr( peer, "ip", &ip ) )
|
|
|
|
continue;
|
|
|
|
if( tr_pton( ip, &addr ) == NULL )
|
|
|
|
continue;
|
|
|
|
if( !tr_bencDictFindInt( peer, "port", &port ) )
|
|
|
|
continue;
|
|
|
|
if( ( port < 0 ) || ( port > USHRT_MAX ) )
|
|
|
|
continue;
|
|
|
|
if( !tr_isValidPeerAddress( &addr, port ) )
|
|
|
|
continue;
|
2010-04-20 21:54:03 +00:00
|
|
|
|
2011-01-22 17:45:54 +00:00
|
|
|
pex[n].addr = addr;
|
|
|
|
pex[n].port = htons( (uint16_t)port );
|
|
|
|
++n;
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-01-22 17:45:54 +00:00
|
|
|
dbgmsg( tier, "got benc list of %zu peers", n );
|
|
|
|
publishPeersPex( tier, seeds, leechers, pex, n );
|
|
|
|
tr_free( pex );
|
|
|
|
return n;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
createAnnounceURL( const tr_announcer * announcer,
|
|
|
|
const tr_torrent * torrent,
|
|
|
|
const tr_tier * tier,
|
|
|
|
const char * eventName )
|
|
|
|
{
|
|
|
|
const int isStopping = !strcmp( eventName, "stopped" );
|
|
|
|
const int numwant = isStopping ? 0 : NUMWANT;
|
|
|
|
const tr_tracker_item * tracker = tier->currentTracker;
|
|
|
|
const char * ann = tracker->announce;
|
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
2010-02-03 06:32:14 +00:00
|
|
|
const char * str;
|
2009-11-10 17:03:23 +00:00
|
|
|
const unsigned char * ipv6;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-11 00:43:47 +00:00
|
|
|
evbuffer_expand( buf, 2048 );
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
evbuffer_add_printf( buf, "%s"
|
|
|
|
"%c"
|
|
|
|
"info_hash=%s"
|
|
|
|
"&peer_id=%s"
|
|
|
|
"&port=%d"
|
2010-10-24 21:35:23 +00:00
|
|
|
"&uploaded=%" PRIu64
|
|
|
|
"&downloaded=%" PRIu64
|
2009-09-25 21:05:59 +00:00
|
|
|
"&left=%" PRIu64
|
|
|
|
"&numwant=%d"
|
|
|
|
"&key=%s"
|
2010-02-11 00:43:47 +00:00
|
|
|
"&compact=1"
|
|
|
|
"&supportcrypto=1",
|
2009-09-25 21:05:59 +00:00
|
|
|
ann,
|
|
|
|
strchr( ann, '?' ) ? '&' : '?',
|
|
|
|
torrent->info.hashEscaped,
|
|
|
|
torrent->peer_id,
|
2010-07-05 21:04:17 +00:00
|
|
|
(int)tr_sessionGetPublicPeerPort( announcer->session ),
|
2010-02-08 16:47:30 +00:00
|
|
|
tier->byteCounts[TR_ANN_UP],
|
|
|
|
tier->byteCounts[TR_ANN_DOWN],
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_cpLeftUntilComplete( &torrent->completion ),
|
|
|
|
numwant,
|
|
|
|
tracker->key_param );
|
|
|
|
|
2010-02-13 21:58:38 +00:00
|
|
|
if( announcer->session->encryptionMode == TR_ENCRYPTION_REQUIRED )
|
|
|
|
evbuffer_add_printf( buf, "&requirecrypto=1" );
|
2010-02-11 00:43:47 +00:00
|
|
|
|
2010-02-08 16:47:30 +00:00
|
|
|
if( tier->byteCounts[TR_ANN_CORRUPT] )
|
2010-10-24 21:35:23 +00:00
|
|
|
evbuffer_add_printf( buf, "&corrupt=%" PRIu64, tier->byteCounts[TR_ANN_CORRUPT] );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-03 06:32:14 +00:00
|
|
|
str = eventName;
|
|
|
|
if( str && *str )
|
|
|
|
evbuffer_add_printf( buf, "&event=%s", str );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-03 06:32:14 +00:00
|
|
|
str = tracker->tracker_id;
|
|
|
|
if( str && *str )
|
|
|
|
evbuffer_add_printf( buf, "&trackerid=%s", str );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2009-11-10 17:03:23 +00:00
|
|
|
/* There are two incompatible techniques for announcing an IPv6 address.
|
|
|
|
BEP-7 suggests adding an "ipv6=" parameter to the announce URL,
|
|
|
|
while OpenTracker requires that peers announce twice, once over IPv4
|
|
|
|
and once over IPv6.
|
|
|
|
|
|
|
|
To be safe, we should do both: add the "ipv6=" parameter and
|
2010-12-27 19:18:17 +00:00
|
|
|
announce twice. At any rate, we're already computing our IPv6
|
2009-11-10 17:03:23 +00:00
|
|
|
address (for the LTEP handshake), so this comes for free. */
|
|
|
|
|
|
|
|
ipv6 = tr_globalIPv6( );
|
|
|
|
if( ipv6 ) {
|
|
|
|
char ipv6_readable[INET6_ADDRSTRLEN];
|
|
|
|
inet_ntop( AF_INET6, ipv6, ipv6_readable, INET6_ADDRSTRLEN );
|
|
|
|
evbuffer_add_printf( buf, "&ipv6=");
|
2010-07-01 05:14:34 +00:00
|
|
|
tr_http_escape( buf, ipv6_readable, -1, TRUE );
|
2009-11-10 17:03:23 +00:00
|
|
|
}
|
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
return evbuffer_free_to_str( buf );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-10-02 04:54:02 +00:00
|
|
|
static void
|
2011-02-05 18:46:10 +00:00
|
|
|
addTorrentToTier( tr_torrent_tiers * tiers,
|
2011-02-05 16:45:06 +00:00
|
|
|
tr_torrent * tor )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2009-10-02 02:03:30 +00:00
|
|
|
int i, n;
|
2009-10-03 13:15:32 +00:00
|
|
|
const tr_tracker_info ** infos;
|
2009-10-02 04:54:02 +00:00
|
|
|
const int trackerCount = tor->info.trackerCount;
|
|
|
|
const tr_tracker_info * trackers = tor->info.trackers;
|
2009-10-02 02:03:30 +00:00
|
|
|
|
|
|
|
/* get the trackers that we support... */
|
2009-10-03 13:15:32 +00:00
|
|
|
infos = tr_new0( const tr_tracker_info*, trackerCount );
|
2009-10-02 04:54:02 +00:00
|
|
|
for( i=n=0; i<trackerCount; ++i )
|
2010-10-30 16:19:27 +00:00
|
|
|
if( tr_urlIsValidTracker( trackers[i].announce ) )
|
2009-10-02 04:54:02 +00:00
|
|
|
infos[n++] = &trackers[i];
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* build our private table of tiers... */
|
2009-10-02 02:03:30 +00:00
|
|
|
if( n > 0 )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
int tierIndex = -1;
|
|
|
|
tr_tier * tier = NULL;
|
|
|
|
|
2009-12-09 04:28:53 +00:00
|
|
|
for( i=0; i<n; ++i )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2009-10-02 02:03:30 +00:00
|
|
|
const tr_tracker_info * info = infos[i];
|
|
|
|
|
|
|
|
if( info->tier != tierIndex )
|
2009-09-25 21:05:59 +00:00
|
|
|
tier = NULL;
|
|
|
|
|
2009-10-02 02:03:30 +00:00
|
|
|
tierIndex = info->tier;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
if( tier == NULL ) {
|
|
|
|
tier = tierNew( tor );
|
|
|
|
dbgmsg( tier, "adding tier" );
|
|
|
|
tr_ptrArrayAppend( &tiers->tiers, tier );
|
|
|
|
}
|
|
|
|
|
2011-02-05 18:46:10 +00:00
|
|
|
tierAddTracker( tier, info->announce, info->scrape, info->id );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static tr_bool
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
tr_bool
|
|
|
|
tr_announcerCanManualAnnounce( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
const tr_tier ** tiers;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
assert( tor->tiers != NULL );
|
|
|
|
|
2011-01-22 19:56:01 +00:00
|
|
|
if( !tor->isRunning )
|
|
|
|
return FALSE;
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
n = tr_ptrArraySize( &tor->tiers->tiers );
|
|
|
|
tiers = (const tr_tier**) tr_ptrArrayBase( &tor->tiers->tiers );
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
if( tierCanManualAnnounce( tiers[i] ) )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
tr_announcerNextManualAnnounce( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
const tr_torrent_tiers * tiers;
|
|
|
|
time_t ret = ~(time_t)0;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
tiers = tor->tiers;
|
|
|
|
n = tr_ptrArraySize( &tiers->tiers );
|
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
tr_tier * tier = tr_ptrArrayNth( (tr_ptrArray*)&tiers->tiers, i );
|
|
|
|
if( tier->isRunning )
|
|
|
|
ret = MIN( ret, tier->manualAnnounceAllowedAt );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-02-10 02:59:15 +00:00
|
|
|
tierAddAnnounce( tr_tier * tier, const char * announceEvent, time_t announceAt )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
assert( tier != NULL );
|
2010-02-10 04:42:34 +00:00
|
|
|
assert( announceEvent != NULL );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
tr_ptrArrayAppend( &tier->announceEvents, (void*)announceEvent );
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->announceAt = announceAt;
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2011-01-12 05:05:48 +00:00
|
|
|
dbgmsg( tier, "appended event \"%s\"; announcing in %d seconds", announceEvent, (int)difftime(announceAt,time(NULL)) );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-02-10 02:59:15 +00:00
|
|
|
torrentAddAnnounce( tr_torrent * tor, const char * announceEvent, time_t announceAt )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
tr_torrent_tiers * tiers;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
tiers = tor->tiers;
|
|
|
|
n = tr_ptrArraySize( &tiers->tiers );
|
|
|
|
for( i=0; i<n; ++i )
|
2010-02-10 02:59:15 +00:00
|
|
|
tierAddAnnounce( tr_ptrArrayNth( &tiers->tiers, i ), announceEvent, 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
|
|
|
{
|
2010-02-14 03:40:39 +00:00
|
|
|
torrentAddAnnounce( tor, 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
|
|
|
{
|
2010-02-10 02:59:15 +00:00
|
|
|
torrentAddAnnounce( tor, "", tr_time( ) );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
void
|
|
|
|
tr_announcerTorrentStopped( tr_torrent * tor )
|
|
|
|
{
|
2010-02-10 02:59:15 +00:00
|
|
|
torrentAddAnnounce( tor, "stopped", tr_time( ) );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
void
|
|
|
|
tr_announcerTorrentCompleted( tr_torrent * tor )
|
|
|
|
{
|
2010-02-10 02:59:15 +00:00
|
|
|
torrentAddAnnounce( tor, "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 )
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
tr_torrent_tiers * tiers;
|
|
|
|
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
assert( type==TR_ANN_UP || type==TR_ANN_DOWN || type==TR_ANN_CORRUPT );
|
|
|
|
|
|
|
|
tiers = tor->tiers;
|
|
|
|
n = tr_ptrArraySize( &tiers->tiers );
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
{
|
|
|
|
tr_tier * tier = tr_ptrArrayNth( &tiers->tiers, i );
|
|
|
|
tier->byteCounts[ type ] += byteCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_announcerRemoveTorrent( tr_announcer * announcer, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
if( tor->tiers )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const int n = tr_ptrArraySize( &tor->tiers->tiers );
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
{
|
|
|
|
tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
|
|
|
|
|
|
|
|
if( tier->isRunning )
|
|
|
|
{
|
|
|
|
struct stop_message * s = tr_new0( struct stop_message, 1 );
|
2010-02-08 16:47:30 +00:00
|
|
|
s->up = tier->byteCounts[TR_ANN_UP];
|
|
|
|
s->down = tier->byteCounts[TR_ANN_DOWN];
|
2009-09-25 21:05:59 +00:00
|
|
|
s->url = createAnnounceURL( announcer, tor, tier, "stopped" );
|
|
|
|
tr_ptrArrayInsertSorted( &announcer->stops, s, compareStops );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tiersFree( tor->tiers );
|
|
|
|
tor->tiers = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
parseAnnounceResponse( tr_tier * tier,
|
|
|
|
const char * response,
|
|
|
|
size_t responseLen,
|
|
|
|
tr_bool * gotScrape )
|
|
|
|
{
|
|
|
|
tr_benc benc;
|
|
|
|
tr_bool success = FALSE;
|
|
|
|
int scrapeFields = 0;
|
|
|
|
const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
|
|
|
|
|
2010-06-10 15:18:02 +00:00
|
|
|
if( getenv( "TR_CURL_VERBOSE" ) != NULL )
|
|
|
|
{
|
|
|
|
char * str = tr_bencToStr( &benc, TR_FMT_JSON, NULL );
|
|
|
|
fprintf( stderr, "Announce response:\n< %s\n", str );
|
|
|
|
tr_free( str );
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
dbgmsg( tier, "response len: %d, isBenc: %d", (int)responseLen, (int)bencLoaded );
|
|
|
|
publishErrorClear( tier );
|
|
|
|
if( bencLoaded && tr_bencIsDict( &benc ) )
|
|
|
|
{
|
|
|
|
size_t rawlen;
|
|
|
|
int64_t i;
|
|
|
|
tr_benc * tmp;
|
|
|
|
const char * str;
|
|
|
|
const uint8_t * raw;
|
2010-02-11 03:12:03 +00:00
|
|
|
tr_bool gotPeers = FALSE;
|
2011-02-05 16:45:06 +00:00
|
|
|
int peerCount = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
success = TRUE;
|
|
|
|
|
|
|
|
if( tr_bencDictFindStr( &benc, "failure reason", &str ) )
|
|
|
|
{
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr, str,
|
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
|
|
|
dbgmsg( tier, "tracker gave \"%s\"", str );
|
|
|
|
publishMessage( tier, str, TR_TRACKER_ERROR );
|
|
|
|
success = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindStr( &benc, "warning message", &str ) )
|
|
|
|
{
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr, str,
|
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
|
|
|
dbgmsg( tier, "tracker gave \"%s\"", str );
|
|
|
|
publishWarning( tier, str );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindInt( &benc, "interval", &i ) )
|
|
|
|
{
|
|
|
|
dbgmsg( tier, "setting interval to %d", (int)i );
|
|
|
|
tier->announceIntervalSec = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindInt( &benc, "min interval", &i ) )
|
|
|
|
{
|
|
|
|
dbgmsg( tier, "setting min interval to %d", (int)i );
|
|
|
|
tier->announceMinIntervalSec = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindStr( &benc, "tracker id", &str ) )
|
|
|
|
{
|
|
|
|
tier->currentTracker->tracker_id = tr_strdup( str );
|
|
|
|
}
|
|
|
|
|
2010-04-20 21:54:03 +00:00
|
|
|
if( !tr_bencDictFindInt( &benc, "complete", &i ) )
|
|
|
|
tier->currentTracker->seederCount = 0;
|
|
|
|
else {
|
2009-09-25 21:05:59 +00:00
|
|
|
++scrapeFields;
|
|
|
|
tier->currentTracker->seederCount = i;
|
|
|
|
}
|
|
|
|
|
2010-04-20 21:54:03 +00:00
|
|
|
if( !tr_bencDictFindInt( &benc, "incomplete", &i ) )
|
|
|
|
tier->currentTracker->leecherCount = 0;
|
|
|
|
else {
|
2009-09-25 21:05:59 +00:00
|
|
|
++scrapeFields;
|
2010-04-20 21:54:03 +00:00
|
|
|
tier->currentTracker->leecherCount = i;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindInt( &benc, "downloaded", &i ) )
|
|
|
|
{
|
|
|
|
++scrapeFields;
|
|
|
|
tier->currentTracker->downloadCount = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindRaw( &benc, "peers", &raw, &rawlen ) )
|
|
|
|
{
|
|
|
|
/* "compact" extension */
|
2010-04-20 21:54:03 +00:00
|
|
|
const int seeders = tier->currentTracker->seederCount;
|
|
|
|
const int leechers = tier->currentTracker->leecherCount;
|
2011-01-22 17:45:54 +00:00
|
|
|
peerCount += publishPeersCompact( tier, seeders, leechers, raw, rawlen );
|
2010-02-11 03:12:03 +00:00
|
|
|
gotPeers = TRUE;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
else if( tr_bencDictFindList( &benc, "peers", &tmp ) )
|
|
|
|
{
|
|
|
|
/* original version of peers */
|
2010-04-20 21:54:03 +00:00
|
|
|
const int seeders = tier->currentTracker->seederCount;
|
|
|
|
const int leechers = tier->currentTracker->leecherCount;
|
2011-01-22 17:45:54 +00:00
|
|
|
peerCount += publishPeersDict( tier, seeders, leechers, tmp );
|
2010-02-11 03:12:03 +00:00
|
|
|
gotPeers = TRUE;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindRaw( &benc, "peers6", &raw, &rawlen ) )
|
|
|
|
{
|
|
|
|
/* "compact" extension */
|
2010-04-20 21:54:03 +00:00
|
|
|
const int seeders = tier->currentTracker->seederCount;
|
|
|
|
const int leechers = tier->currentTracker->leecherCount;
|
2011-01-22 17:45:54 +00:00
|
|
|
peerCount += publishPeersCompact6( tier, seeders, leechers, raw, rawlen );
|
2010-02-11 03:12:03 +00:00
|
|
|
gotPeers = TRUE;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( tier->lastAnnounceStr[0] == '\0' )
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr, _( "Success" ),
|
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
2010-02-01 01:13:25 +00:00
|
|
|
|
2010-02-11 03:12:03 +00:00
|
|
|
if( gotPeers )
|
2010-02-01 03:39:54 +00:00
|
|
|
tier->lastAnnouncePeerCount = peerCount;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( bencLoaded )
|
|
|
|
tr_bencFree( &benc );
|
|
|
|
|
|
|
|
*gotScrape = scrapeFields >= 2;
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
static int
|
|
|
|
getRetryInterval( const tr_tracker_item * t )
|
|
|
|
{
|
|
|
|
int minutes;
|
|
|
|
const unsigned int jitter_seconds = tr_cryptoWeakRandInt( 60 );
|
|
|
|
switch( t->consecutiveAnnounceFailures ) {
|
|
|
|
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 torrentId;
|
|
|
|
int tierId;
|
|
|
|
time_t timeSent;
|
|
|
|
const char * event;
|
|
|
|
|
|
|
|
/** If the request succeeds, the value for tier's "isRunning" flag */
|
|
|
|
tr_bool isRunningOnSuccess;
|
|
|
|
};
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static void
|
|
|
|
onAnnounceDone( tr_session * session,
|
2011-02-17 02:26:24 +00:00
|
|
|
tr_bool didConnect,
|
|
|
|
tr_bool didTimeout,
|
2009-09-25 21:05:59 +00:00
|
|
|
long responseCode,
|
|
|
|
const void * response,
|
|
|
|
size_t responseLen,
|
|
|
|
void * vdata )
|
|
|
|
{
|
|
|
|
tr_announcer * announcer = session->announcer;
|
|
|
|
struct announce_data * data = vdata;
|
|
|
|
tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
|
2011-01-29 19:04:02 +00:00
|
|
|
const time_t now = tr_time( );
|
2010-02-10 02:59:15 +00:00
|
|
|
const char * announceEvent = data->event;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-01-29 19:04:02 +00:00
|
|
|
if( tier )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-01-29 19:04:02 +00:00
|
|
|
tr_tracker_item * tracker;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-01 00:28:04 +00:00
|
|
|
tier->lastAnnounceTime = now;
|
2011-02-17 02:26:24 +00:00
|
|
|
tier->lastAnnounceTimedOut = didTimeout;
|
2011-01-29 19:04:02 +00:00
|
|
|
tier->lastAnnounceSucceeded = FALSE;
|
|
|
|
tier->isAnnouncing = FALSE;
|
|
|
|
tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
|
2010-02-01 00:28:04 +00:00
|
|
|
|
2011-01-29 19:04:02 +00:00
|
|
|
if(( tracker = tier->currentTracker ))
|
|
|
|
++tracker->consecutiveAnnounceFailures;
|
2010-02-08 16:47:30 +00:00
|
|
|
|
2011-01-29 19:04:02 +00:00
|
|
|
if( responseCode == HTTP_OK )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2011-01-29 19:04:02 +00:00
|
|
|
tr_bool gotScrape;
|
|
|
|
const tr_bool isStopped = !strcmp( announceEvent, "stopped" );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-01-29 19:04:02 +00:00
|
|
|
if( parseAnnounceResponse( tier, response, responseLen, &gotScrape ) )
|
|
|
|
{
|
|
|
|
tier->lastAnnounceSucceeded = TRUE;
|
|
|
|
tier->isRunning = data->isRunningOnSuccess;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-01-29 19:04:02 +00:00
|
|
|
if(( tracker = tier->currentTracker ))
|
|
|
|
{
|
|
|
|
tracker->consecutiveAnnounceFailures = 0;
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2011-01-29 19:04:02 +00:00
|
|
|
if( gotScrape )
|
|
|
|
{
|
|
|
|
tier->lastScrapeTime = now;
|
|
|
|
tier->lastScrapeSucceeded = TRUE;
|
|
|
|
tier->scrapeAt = now + tier->scrapeIntervalSec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
if( !isStopped && !tr_ptrArraySize( &tier->announceEvents ) )
|
|
|
|
{
|
2011-01-29 19:04:02 +00:00
|
|
|
/* the queue is empty, so enqueue a perodic update */
|
|
|
|
const int interval = tier->announceIntervalSec;
|
|
|
|
dbgmsg( tier, "Sending periodic reannounce in %d seconds", interval );
|
2010-02-10 02:59:15 +00:00
|
|
|
tierAddAnnounce( tier, "", now + interval );
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-29 19:04:02 +00:00
|
|
|
int interval;
|
|
|
|
|
2011-02-17 02:26:24 +00:00
|
|
|
if( !didConnect )
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr, _( "Could not connect to tracker" ),
|
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
|
|
|
else if( !responseCode )
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr, _( "Tracker did not respond" ),
|
2011-01-29 19:04:02 +00:00
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
|
|
|
else {
|
|
|
|
/* %1$ld - http status code, such as 404
|
|
|
|
* %2$s - human-readable explanation of the http status code */
|
|
|
|
tr_snprintf( tier->lastAnnounceStr, sizeof( tier->lastAnnounceStr ),
|
|
|
|
_( "Tracker gave HTTP response code %1$ld (%2$s)" ),
|
|
|
|
responseCode,
|
|
|
|
tr_webGetResponseStr( responseCode ) );
|
|
|
|
if( responseCode >= 400 )
|
|
|
|
if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount == 1 ) )
|
|
|
|
publishWarning( tier, tier->lastAnnounceStr );
|
|
|
|
}
|
|
|
|
dbgmsg( tier, "%s", tier->lastAnnounceStr );
|
|
|
|
tr_torinf( tier->tor, "%s", tier->lastAnnounceStr );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
tierIncrementTracker( tier );
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2011-01-29 19:04:02 +00:00
|
|
|
/* schedule the next announce */
|
|
|
|
interval = getRetryInterval( tier->currentTracker );
|
|
|
|
dbgmsg( tier, "Retrying announce in %d seconds.", interval );
|
|
|
|
tierAddAnnounce( tier, announceEvent, now + interval );
|
2010-02-10 02:59:15 +00:00
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-01-29 19:04:02 +00:00
|
|
|
if( announcer )
|
2010-02-05 01:16:30 +00:00
|
|
|
++announcer->slotsAvailable;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
tr_free( data );
|
|
|
|
}
|
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
static const char*
|
|
|
|
getNextAnnounceEvent( tr_tier * tier )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
2010-02-10 02:59:15 +00:00
|
|
|
int i, n;
|
|
|
|
int pos = -1;
|
|
|
|
tr_ptrArray tmp;
|
2010-02-20 17:17:57 +00:00
|
|
|
const char ** events;
|
2010-02-10 02:59:15 +00:00
|
|
|
const char * str = NULL;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
assert( tier != NULL );
|
|
|
|
assert( tr_isTorrent( tier->tor ) );
|
|
|
|
|
2010-02-20 17:17:57 +00:00
|
|
|
events = (const char**) tr_ptrArrayPeek( &tier->announceEvents, &n );
|
2010-06-03 16:37:50 +00:00
|
|
|
|
|
|
|
/* special case #1: if "stopped" is in the queue,
|
|
|
|
* ignore everything before it except "completed" */
|
2010-02-20 15:36:25 +00:00
|
|
|
if( pos == -1 ) {
|
2010-06-03 16:37:50 +00:00
|
|
|
tr_bool completed = FALSE;
|
|
|
|
for( i = 0; i < n; ++i ) {
|
|
|
|
if( !strcmp( events[i], "completed" ) )
|
|
|
|
completed = TRUE;
|
2010-02-20 15:36:25 +00:00
|
|
|
if( !strcmp( events[i], "stopped" ) )
|
|
|
|
break;
|
2010-06-03 16:37:50 +00:00
|
|
|
}
|
|
|
|
if( !completed && ( i < n ) )
|
2010-02-10 02:59:15 +00:00
|
|
|
pos = i;
|
2010-02-20 15:36:25 +00:00
|
|
|
}
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2010-02-20 15:36:25 +00:00
|
|
|
/* special case #2: don't use empty strings if something follows them */
|
|
|
|
if( pos == -1 ) {
|
|
|
|
for( i = 0; i < n; ++i )
|
|
|
|
if( *events[i] )
|
|
|
|
break;
|
|
|
|
if( i < n )
|
2010-02-10 02:59:15 +00:00
|
|
|
pos = i;
|
2010-02-20 15:36:25 +00:00
|
|
|
}
|
2010-02-10 02:59:15 +00:00
|
|
|
|
2010-02-20 15:36:25 +00:00
|
|
|
/* default: use the next in the queue */
|
|
|
|
if( ( pos == -1 ) && ( n > 0 ) )
|
2010-02-10 02:59:15 +00:00
|
|
|
pos = 0;
|
|
|
|
|
2010-02-20 15:36:25 +00:00
|
|
|
/* special case #3: if there are duplicate requests in a row, skip to the last one */
|
|
|
|
if( pos >= 0 ) {
|
|
|
|
for( i=pos+1; i<n; ++i )
|
2010-02-20 17:17:57 +00:00
|
|
|
if( strcmp( events[pos], events[i] ) )
|
2010-02-20 15:36:25 +00:00
|
|
|
break;
|
|
|
|
pos = i - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* special case #4: BEP 21: "In order to tell the tracker that a peer is a
|
2010-02-10 02:59:15 +00:00
|
|
|
* partial seed, it MUST send an event=paused parameter in every
|
|
|
|
* announce while it is a partial seed." */
|
|
|
|
str = pos>=0 ? events[pos] : NULL;
|
2009-09-25 21:05:59 +00:00
|
|
|
if( tr_cpGetStatus( &tier->tor->completion ) == TR_PARTIAL_SEED )
|
2010-02-20 21:03:49 +00:00
|
|
|
if( !str || strcmp( str, "stopped" ) )
|
2010-02-10 02:59:15 +00:00
|
|
|
str = "paused";
|
|
|
|
|
2010-02-20 21:03:49 +00:00
|
|
|
#if 0
|
|
|
|
for( i=0; i<n; ++i ) fprintf( stderr, "(%d)\"%s\" ", i, events[i] );
|
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
fprintf( stderr, "using (%d)\"%s\"\n", pos, events[pos] );
|
|
|
|
if( strcmp( events[pos], str ) ) fprintf( stderr, "...but really using [%s]\n", str );
|
|
|
|
#endif
|
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
/* announceEvents array upkeep */
|
|
|
|
tmp = TR_PTR_ARRAY_INIT;
|
2010-02-10 03:57:41 +00:00
|
|
|
for( i=pos+1; i<n; ++i )
|
2010-02-20 17:17:57 +00:00
|
|
|
tr_ptrArrayAppend( &tmp, (void*)events[i] );
|
2010-02-10 02:59:15 +00:00
|
|
|
tr_ptrArrayDestruct( &tier->announceEvents, NULL );
|
|
|
|
tier->announceEvents = tmp;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
return str;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tierAnnounce( tr_announcer * announcer, tr_tier * tier )
|
|
|
|
{
|
2010-02-10 02:59:15 +00:00
|
|
|
const char * announceEvent = getNextAnnounceEvent( tier );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
assert( !tier->isAnnouncing );
|
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
if( announceEvent != NULL )
|
|
|
|
{
|
|
|
|
char * url;
|
|
|
|
struct announce_data * data;
|
|
|
|
const tr_torrent * tor = tier->tor;
|
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
data = tr_new0( struct announce_data, 1 );
|
|
|
|
data->torrentId = tr_torrentId( tor );
|
|
|
|
data->tierId = tier->key;
|
|
|
|
data->isRunningOnSuccess = tor->isRunning;
|
|
|
|
data->timeSent = now;
|
|
|
|
data->event = announceEvent;
|
|
|
|
url = createAnnounceURL( announcer, tor, tier, data->event );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
tier->isAnnouncing = TRUE;
|
|
|
|
tier->lastAnnounceStartTime = now;
|
|
|
|
--announcer->slotsAvailable;
|
|
|
|
tr_webRun( announcer->session, url, NULL, onAnnounceDone, data );
|
|
|
|
|
|
|
|
tr_free( url );
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2011-02-05 16:45:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static tr_bool
|
|
|
|
parseScrapeResponse( tr_tier * tier,
|
|
|
|
const char * response,
|
2009-10-11 04:39:24 +00:00
|
|
|
size_t responseLen,
|
|
|
|
char * result,
|
|
|
|
size_t resultlen )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
tr_bool success = FALSE;
|
|
|
|
tr_benc benc, *files;
|
|
|
|
const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
|
|
|
|
if( bencLoaded && tr_bencDictFindDict( &benc, "files", &files ) )
|
|
|
|
{
|
|
|
|
const char * key;
|
|
|
|
tr_benc * val;
|
|
|
|
int i = 0;
|
|
|
|
while( tr_bencDictChild( files, i++, &key, &val ))
|
|
|
|
{
|
|
|
|
int64_t intVal;
|
|
|
|
tr_benc * flags;
|
|
|
|
|
|
|
|
if( memcmp( tier->tor->info.hash, key, SHA_DIGEST_LENGTH ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
success = TRUE;
|
|
|
|
publishErrorClear( tier );
|
|
|
|
|
|
|
|
if( ( tr_bencDictFindInt( val, "complete", &intVal ) ) )
|
|
|
|
tier->currentTracker->seederCount = intVal;
|
|
|
|
|
|
|
|
if( ( tr_bencDictFindInt( val, "incomplete", &intVal ) ) )
|
|
|
|
tier->currentTracker->leecherCount = intVal;
|
|
|
|
|
|
|
|
if( ( tr_bencDictFindInt( val, "downloaded", &intVal ) ) )
|
|
|
|
tier->currentTracker->downloadCount = intVal;
|
|
|
|
|
|
|
|
if( ( tr_bencDictFindInt( val, "downloaders", &intVal ) ) )
|
|
|
|
tier->currentTracker->downloaderCount = intVal;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( val, "flags", &flags ) )
|
|
|
|
if( ( tr_bencDictFindInt( flags, "min_request_interval", &intVal ) ) )
|
2010-02-05 05:16:18 +00:00
|
|
|
tier->scrapeIntervalSec = MAX( DEFAULT_SCRAPE_INTERVAL_SEC, (int)intVal );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
tr_tordbg( tier->tor,
|
|
|
|
"Scrape successful. Rescraping in %d seconds.",
|
|
|
|
tier->scrapeIntervalSec );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( bencLoaded )
|
|
|
|
tr_bencFree( &benc );
|
|
|
|
|
2009-10-11 04:39:24 +00:00
|
|
|
if( success )
|
|
|
|
tr_strlcpy( result, _( "Success" ), resultlen );
|
|
|
|
else
|
|
|
|
tr_strlcpy( result, _( "Error parsing response" ), resultlen );
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
onScrapeDone( tr_session * session,
|
2011-02-17 02:26:24 +00:00
|
|
|
tr_bool didConnect,
|
|
|
|
tr_bool didTimeout,
|
2009-09-25 21:05:59 +00:00
|
|
|
long responseCode,
|
|
|
|
const void * response,
|
|
|
|
size_t responseLen,
|
|
|
|
void * vdata )
|
|
|
|
{
|
2009-12-02 15:16:29 +00:00
|
|
|
tr_bool success = FALSE;
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_announcer * announcer = session->announcer;
|
|
|
|
struct announce_data * data = vdata;
|
|
|
|
tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
if( announcer )
|
2010-02-05 01:16:30 +00:00
|
|
|
++announcer->slotsAvailable;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
if( announcer && tier )
|
|
|
|
{
|
|
|
|
tier->isScraping = FALSE;
|
|
|
|
tier->lastScrapeTime = now;
|
2009-11-10 17:03:23 +00:00
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
if( 200 <= responseCode && responseCode <= 299 )
|
|
|
|
{
|
2009-10-03 14:08:05 +00:00
|
|
|
const int interval = tier->scrapeIntervalSec;
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->scrapeAt = now + interval;
|
|
|
|
|
2009-10-11 04:39:24 +00:00
|
|
|
if( responseCode == HTTP_OK )
|
|
|
|
success = parseScrapeResponse( tier, response, responseLen,
|
|
|
|
tier->lastScrapeStr, sizeof( tier->lastScrapeStr ) );
|
|
|
|
else
|
|
|
|
tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
|
|
|
|
_( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
|
|
|
|
responseCode, tr_webGetResponseStr( responseCode ) );
|
2011-01-20 20:49:09 +00:00
|
|
|
dbgmsg( tier, "%s", tier->lastScrapeStr );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
else if( 300 <= responseCode && responseCode <= 399 )
|
|
|
|
{
|
|
|
|
/* this shouldn't happen; libcurl should handle this */
|
|
|
|
const int interval = 5;
|
|
|
|
tier->scrapeAt = now + interval;
|
|
|
|
tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
|
|
|
|
"Got a redirect. Retrying in %d seconds", interval );
|
2011-01-20 20:49:09 +00:00
|
|
|
dbgmsg( tier, "%s", tier->lastScrapeStr );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-20 22:51:05 +00:00
|
|
|
const int interval = getRetryInterval( tier->currentTracker );
|
2009-10-28 04:53:39 +00:00
|
|
|
|
|
|
|
/* Don't retry on a 4xx.
|
|
|
|
* Retry at growing intervals on a 5xx */
|
|
|
|
if( 400 <= responseCode && responseCode <= 499 )
|
|
|
|
tier->scrapeAt = 0;
|
|
|
|
else
|
|
|
|
tier->scrapeAt = now + interval;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* %1$ld - http status code, such as 404
|
|
|
|
* %2$s - human-readable explanation of the http status code */
|
2011-02-17 02:26:24 +00:00
|
|
|
if( !didConnect )
|
|
|
|
tr_strlcpy( tier->lastScrapeStr, _( "Could not connect to tracker" ),
|
|
|
|
sizeof( tier->lastScrapeStr ) );
|
|
|
|
else if( !responseCode )
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_strlcpy( tier->lastScrapeStr, _( "tracker did not respond" ),
|
|
|
|
sizeof( tier->lastScrapeStr ) );
|
|
|
|
else
|
|
|
|
tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
|
|
|
|
_( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
|
|
|
|
responseCode, tr_webGetResponseStr( responseCode ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
tier->lastScrapeSucceeded = success;
|
2011-02-17 02:26:24 +00:00
|
|
|
tier->lastScrapeTimedOut = didTimeout;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tierScrape( tr_announcer * announcer, tr_tier * tier )
|
|
|
|
{
|
2009-12-02 15:16:29 +00:00
|
|
|
char * url;
|
2009-09-25 21:05:59 +00:00
|
|
|
const char * scrape;
|
|
|
|
struct announce_data * data;
|
|
|
|
|
|
|
|
assert( tier );
|
|
|
|
assert( !tier->isScraping );
|
|
|
|
assert( tier->currentTracker != NULL );
|
|
|
|
assert( tr_isTorrent( tier->tor ) );
|
|
|
|
|
|
|
|
data = tr_new0( struct announce_data, 1 );
|
|
|
|
data->torrentId = tr_torrentId( tier->tor );
|
|
|
|
data->tierId = tier->key;
|
|
|
|
|
|
|
|
scrape = tier->currentTracker->scrape;
|
|
|
|
|
2009-12-02 15:16:29 +00:00
|
|
|
url = tr_strdup_printf( "%s%cinfo_hash=%s",
|
|
|
|
scrape,
|
|
|
|
strchr( scrape, '?' ) ? '&' : '?',
|
|
|
|
tier->tor->info.hashEscaped );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
tier->isScraping = TRUE;
|
2011-02-05 16:45:06 +00:00
|
|
|
tier->lastScrapeStartTime = tr_time( );
|
2010-02-05 01:16:30 +00:00
|
|
|
--announcer->slotsAvailable;
|
2009-12-02 15:16:29 +00:00
|
|
|
dbgmsg( tier, "scraping \"%s\"", url );
|
|
|
|
tr_webRun( announcer->session, url, NULL, onScrapeDone, data );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2009-12-02 15:16:29 +00:00
|
|
|
tr_free( url );
|
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 )
|
|
|
|
{
|
|
|
|
struct stop_message * stop = tr_ptrArrayNth( &announcer->stops, i );
|
|
|
|
tr_webRun( announcer->session, stop->url, NULL, NULL, NULL );
|
|
|
|
stopFree( stop );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_ptrArrayClear( &announcer->stops );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
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 )
|
|
|
|
&& ( tr_ptrArraySize( &tier->announceEvents ) != 0 );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
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-02-05 16:45:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static void
|
|
|
|
announceMore( tr_announcer * announcer )
|
|
|
|
{
|
2009-10-12 23:16:51 +00:00
|
|
|
tr_torrent * tor = NULL;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
2010-02-05 01:16:30 +00:00
|
|
|
if( announcer->slotsAvailable > 0 )
|
2009-09-25 21:05:59 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
|
|
|
|
tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
|
|
|
|
|
|
|
|
/* build a list of tiers that need to be announced */
|
|
|
|
while(( tor = tr_torrentNext( announcer->session, tor ))) {
|
|
|
|
if( tor->tiers ) {
|
|
|
|
n = tr_ptrArraySize( &tor->tiers->tiers );
|
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
|
2010-02-05 01:16:30 +00:00
|
|
|
if( tierNeedsToAnnounce( tier, now ) )
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_ptrArrayAppend( &announceMe, tier );
|
2010-02-05 01:16:30 +00:00
|
|
|
else if( tierNeedsToScrape( tier, now ) )
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_ptrArrayAppend( &scrapeMe, tier );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if there are more tiers than slots available, prioritize */
|
|
|
|
n = tr_ptrArraySize( &announceMe );
|
2010-02-05 01:16:30 +00:00
|
|
|
if( n > announcer->slotsAvailable )
|
2009-09-25 21:05:59 +00:00
|
|
|
qsort( tr_ptrArrayBase( &announceMe ), n, sizeof( tr_tier * ), compareTiers );
|
|
|
|
|
|
|
|
/* announce some */
|
2010-02-05 01:16:30 +00:00
|
|
|
n = MIN( tr_ptrArraySize( &announceMe ), announcer->slotsAvailable );
|
2009-09-25 21:05:59 +00:00
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
|
|
|
|
dbgmsg( tier, "announcing tier %d of %d", i, n );
|
|
|
|
tierAnnounce( announcer, tier );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* scrape some */
|
2010-02-05 01:16:30 +00:00
|
|
|
n = MIN( tr_ptrArraySize( &scrapeMe ), announcer->slotsAvailable );
|
2009-09-25 21:05:59 +00:00
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
tr_tier * tier = tr_ptrArrayNth( &scrapeMe, i );
|
|
|
|
dbgmsg( tier, "scraping tier %d of %d", (i+1), n );
|
|
|
|
tierScrape( announcer, tier );
|
|
|
|
}
|
|
|
|
|
2009-12-02 05:30:46 +00:00
|
|
|
#if 0
|
|
|
|
char timebuf[64];
|
|
|
|
tr_getLogTimeStr( timebuf, 64 );
|
|
|
|
fprintf( stderr, "[%s] announce.c has %d requests ready to send (announce: %d, scrape: %d)\n", timebuf, (int)(tr_ptrArraySize(&announceMe)+tr_ptrArraySize(&scrapeMe)), (int)tr_ptrArraySize(&announceMe), (int)tr_ptrArraySize(&scrapeMe) );
|
|
|
|
#endif
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
/* cleanup */
|
|
|
|
tr_ptrArrayDestruct( &scrapeMe, NULL );
|
|
|
|
tr_ptrArrayDestruct( &announceMe, NULL );
|
|
|
|
}
|
2009-10-12 23:16:51 +00:00
|
|
|
|
|
|
|
tor = NULL;
|
|
|
|
while(( tor = tr_torrentNext( announcer->session, tor ))) {
|
|
|
|
if( tor->dhtAnnounceAt <= now ) {
|
2009-11-24 01:59:51 +00:00
|
|
|
if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
|
|
|
|
int rc;
|
|
|
|
rc = tr_dhtAnnounce(tor, AF_INET, 1);
|
|
|
|
if(rc == 0)
|
2010-12-27 19:18:17 +00:00
|
|
|
/* The DHT is not ready yet. Try again soon. */
|
2009-11-24 01:59:51 +00:00
|
|
|
tor->dhtAnnounceAt = now + 5 + tr_cryptoWeakRandInt( 5 );
|
|
|
|
else
|
|
|
|
/* We should announce at least once every 30 minutes. */
|
|
|
|
tor->dhtAnnounceAt =
|
|
|
|
now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tor->dhtAnnounce6At <= now ) {
|
|
|
|
if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
|
|
|
|
int rc;
|
|
|
|
rc = tr_dhtAnnounce(tor, AF_INET6, 1);
|
|
|
|
if(rc == 0)
|
|
|
|
tor->dhtAnnounce6At = now + 5 + tr_cryptoWeakRandInt( 5 );
|
|
|
|
else
|
|
|
|
tor->dhtAnnounce6At =
|
|
|
|
now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
|
|
|
|
}
|
2009-10-12 23:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* Local Peer Discovery */
|
2010-05-08 08:42:45 +00:00
|
|
|
if( announcer->lpdHouseKeepingAt <= now )
|
2010-05-01 16:04:00 +00:00
|
|
|
{
|
2010-05-08 08:42:45 +00:00
|
|
|
tr_lpdAnnounceMore( now, LPD_HOUSEKEEPING_INTERVAL_SECS );
|
2010-05-01 16:04:00 +00:00
|
|
|
|
|
|
|
/* reschedule more LDS announces for ( the future + jitter ) */
|
2010-05-08 08:42:45 +00:00
|
|
|
announcer->lpdHouseKeepingAt =
|
|
|
|
calcRescheduleWithJitter( LPD_HOUSEKEEPING_INTERVAL_SECS );
|
2010-05-01 16:04:00 +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;
|
2010-01-04 09:11:27 +00:00
|
|
|
tr_sessionLock( announcer->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 */
|
|
|
|
announceMore( announcer );
|
|
|
|
|
|
|
|
/* set up the next timer */
|
|
|
|
tr_timerAdd( announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
|
2010-01-04 09:11:27 +00:00
|
|
|
|
|
|
|
tr_sessionUnlock( announcer->session );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_tracker_stat *
|
|
|
|
tr_announcerStats( const tr_torrent * torrent,
|
|
|
|
int * setmeTrackerCount )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
|
|
|
int out = 0;
|
|
|
|
int tierCount;
|
|
|
|
tr_tracker_stat * ret;
|
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
|
|
|
|
|
|
|
/* count the trackers... */
|
|
|
|
for( i=n=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i ) {
|
|
|
|
const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
|
|
|
|
n += tr_ptrArraySize( &tier->trackers );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* alloc the stats */
|
|
|
|
*setmeTrackerCount = n;
|
|
|
|
ret = tr_new0( tr_tracker_stat, n );
|
|
|
|
|
|
|
|
/* populate the stats */
|
|
|
|
for( i=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i )
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
|
|
|
|
n = tr_ptrArraySize( &tier->trackers );
|
|
|
|
for( j=0; j<n; ++j )
|
|
|
|
{
|
|
|
|
const tr_tracker_item * tracker = tr_ptrArrayNth( (tr_ptrArray*)&tier->trackers, j );
|
|
|
|
tr_tracker_stat * st = ret + out++;
|
|
|
|
|
2010-01-13 22:46:22 +00:00
|
|
|
st->id = tracker->id;
|
2011-02-05 18:46:10 +00:00
|
|
|
tr_strlcpy( st->host, tracker->hostname, 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;
|
2009-09-30 20:49:52 +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;
|
|
|
|
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
|
|
|
|
trackerItemCopyAttributes( tr_tracker_item * t, const tr_tracker_item * o )
|
|
|
|
{
|
|
|
|
assert( t != o );
|
|
|
|
assert( t != NULL );
|
|
|
|
assert( o != NULL );
|
|
|
|
|
|
|
|
t->seederCount = o->seederCount;
|
|
|
|
t->leecherCount = o->leecherCount;
|
|
|
|
t->downloadCount = o->downloadCount;
|
|
|
|
t->downloaderCount = o->downloaderCount;
|
|
|
|
memcpy( t->key_param, o->key_param, sizeof( t->key_param ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tierCopyAttributes( tr_tier * t, const tr_tier * o )
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
tr_tier bak;
|
|
|
|
|
|
|
|
assert( t != NULL );
|
|
|
|
assert( o != NULL );
|
|
|
|
assert( t != o );
|
|
|
|
|
|
|
|
bak = *t;
|
|
|
|
*t = *o;
|
|
|
|
t->tor = bak.tor;
|
|
|
|
t->trackers = bak.trackers;
|
|
|
|
t->announceEvents = bak.announceEvents;
|
|
|
|
t->currentTracker = bak.currentTracker;
|
|
|
|
t->currentTrackerIndex = bak.currentTrackerIndex;
|
|
|
|
|
|
|
|
tr_ptrArrayClear( &t->announceEvents );
|
|
|
|
for( i=0, n=tr_ptrArraySize(&o->announceEvents); i<n; ++i )
|
|
|
|
tr_ptrArrayAppend( &t->announceEvents, tr_ptrArrayNth((tr_ptrArray*)&o->announceEvents,i) );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
tr_ptrArray oldTiers = TR_PTR_ARRAY_INIT;
|
|
|
|
|
|
|
|
/* if we had tiers already, make a backup of them */
|
|
|
|
if( tor->tiers != NULL )
|
|
|
|
{
|
|
|
|
oldTiers = tor->tiers->tiers;
|
|
|
|
tor->tiers->tiers = TR_PTR_ARRAY_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create the new tier/tracker structs */
|
2011-02-05 18:46:10 +00:00
|
|
|
addTorrentToTier( tor->tiers, tor );
|
2011-02-05 16:45:06 +00:00
|
|
|
|
|
|
|
/* if we had tiers already, merge their state into the new structs */
|
|
|
|
if( !tr_ptrArrayEmpty( &oldTiers ) )
|
|
|
|
{
|
|
|
|
int i, in;
|
|
|
|
for( i=0, in=tr_ptrArraySize(&oldTiers); i<in; ++i )
|
|
|
|
{
|
|
|
|
int j, jn;
|
|
|
|
const tr_tier * o = tr_ptrArrayNth( &oldTiers, i );
|
|
|
|
|
|
|
|
if( o->currentTracker == NULL )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for( j=0, jn=tr_ptrArraySize(&tor->tiers->tiers); j<jn; ++j )
|
|
|
|
{
|
|
|
|
int k, kn;
|
|
|
|
tr_tier * t = tr_ptrArrayNth(&tor->tiers->tiers,j);
|
|
|
|
|
|
|
|
for( k=0, kn=tr_ptrArraySize(&t->trackers); k<kn; ++k )
|
|
|
|
{
|
|
|
|
tr_tracker_item * item = tr_ptrArrayNth(&t->trackers,k);
|
|
|
|
if( strcmp( o->currentTracker->announce, item->announce ) )
|
|
|
|
continue;
|
|
|
|
tierCopyAttributes( t, o );
|
|
|
|
t->currentTracker = item;
|
|
|
|
t->currentTrackerIndex = k;
|
|
|
|
t->wasCopied = TRUE;
|
|
|
|
trackerItemCopyAttributes( item, o->currentTracker );
|
|
|
|
dbgmsg( t, "attributes copied to tier %d, tracker %d"
|
|
|
|
"from tier %d, tracker %d",
|
|
|
|
i, o->currentTrackerIndex, j, k );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* kickstart any tiers that didn't get started */
|
|
|
|
if( tor->isRunning )
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
const time_t now = tr_time( );
|
|
|
|
tr_tier ** tiers = (tr_tier**) tr_ptrArrayPeek( &tor->tiers->tiers, &n );
|
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
tr_tier * tier = tiers[i];
|
|
|
|
if( !tier->wasCopied )
|
|
|
|
tierAddAnnounce( tier, STARTED, now );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_ptrArrayDestruct( &oldTiers, tierFree );
|
|
|
|
}
|