2009-09-25 21:05:59 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2009-2010 Mnemosyne LLC
|
2009-09-25 21:05:59 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* $Id:$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <event.h>
|
|
|
|
#include <evhttp.h> /* for HTTP_OK */
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "announcer.h"
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "ptrarray.h"
|
|
|
|
#include "publish.h"
|
|
|
|
#include "session.h"
|
2009-10-12 23:16:51 +00:00
|
|
|
#include "tr-dht.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 ), \
|
|
|
|
( tier->currentTracker ? tier->currentTracker->host->name : "" ) ); \
|
|
|
|
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
|
|
|
|
|
|
|
/* if a tracker takes more than this long to respond,
|
|
|
|
* we treat it as nonresponsive */
|
|
|
|
MAX_TRACKER_RESPONSE_TIME_SECS = ( 60 * 2 ),
|
|
|
|
|
|
|
|
/* the value of the 'numwant' argument passed in tracker requests. */
|
|
|
|
NUMWANT = 200,
|
|
|
|
|
|
|
|
/* how long to put slow (nonresponsive) trackers in the penalty box */
|
|
|
|
SLOW_HOST_PENALTY_SECS = ( 60 * 10 ),
|
|
|
|
|
|
|
|
UPKEEP_INTERVAL_SECS = 1
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareTransfer( int a_uploaded, int a_downloaded,
|
|
|
|
int b_uploaded, int 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* used by tr_announcer to recognize nonresponsive
|
|
|
|
* trackers and de-prioritize them
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char * name;
|
|
|
|
|
|
|
|
/* how many seconds it took to get the last tracker response */
|
|
|
|
int lastResponseInterval;
|
|
|
|
|
|
|
|
/* the last time we sent an announce or scrape message */
|
|
|
|
time_t lastRequestTime;
|
2010-02-05 01:47:33 +00:00
|
|
|
|
|
|
|
/* the last successful announce/scrape time for this host */
|
|
|
|
time_t lastSuccessfulRequest;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
tr_host;
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareHosts( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const tr_host * a = va;
|
|
|
|
const tr_host * b = vb;
|
|
|
|
return strcmp( a->name, b->name );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareHostToName( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const tr_host * a = va;
|
|
|
|
return strcmp( a->name, vb );
|
|
|
|
}
|
|
|
|
|
2009-12-02 15:16:29 +00:00
|
|
|
/* format: hostname + ':' + port */
|
2009-09-25 21:05:59 +00:00
|
|
|
static char *
|
|
|
|
getHostName( const char * url )
|
|
|
|
{
|
|
|
|
int port = 0;
|
|
|
|
char * host = NULL;
|
|
|
|
char * ret;
|
|
|
|
tr_httpParseURL( url, strlen( url ), &host, &port, NULL );
|
|
|
|
ret = tr_strdup_printf( "%s:%d", ( host ? host : "invalid" ), port );
|
|
|
|
tr_free( host );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_host*
|
|
|
|
hostNew( const char * name )
|
|
|
|
{
|
|
|
|
tr_host * host = tr_new0( tr_host, 1 );
|
|
|
|
host->name = tr_strdup( name );
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hostFree( void * vhost )
|
|
|
|
{
|
|
|
|
tr_host * host = vhost;
|
|
|
|
|
|
|
|
tr_free( host->name );
|
|
|
|
tr_free( host );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
|
|
|
tr_host * host;
|
|
|
|
char * url;
|
|
|
|
int up;
|
|
|
|
int down;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
stopFree( struct stop_message * stop )
|
|
|
|
{
|
|
|
|
tr_free( stop->url );
|
|
|
|
tr_free( stop );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 hosts; /* tr_host */
|
|
|
|
tr_ptrArray stops; /* struct stop_message */
|
|
|
|
tr_session * session;
|
|
|
|
struct event * upkeepTimer;
|
2010-02-05 01:16:30 +00:00
|
|
|
int slotsAvailable;
|
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
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static tr_host *
|
|
|
|
getHost( tr_announcer * announcer, const char * url )
|
|
|
|
{
|
|
|
|
char * name = getHostName( url );
|
|
|
|
tr_host * host;
|
|
|
|
|
|
|
|
host = tr_ptrArrayFindSorted( &announcer->hosts, name, compareHostToName );
|
|
|
|
if( host == NULL )
|
|
|
|
{
|
|
|
|
host = hostNew( name );
|
|
|
|
tr_ptrArrayInsertSorted( &announcer->hosts, host, compareHosts );
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free( name );
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer );
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_announcerInit( tr_session * session )
|
|
|
|
{
|
|
|
|
tr_announcer * a;
|
|
|
|
|
|
|
|
assert( tr_isSession( session ) );
|
|
|
|
|
|
|
|
a = tr_new0( tr_announcer, 1 );
|
|
|
|
a->hosts = TR_PTR_ARRAY_INIT;
|
|
|
|
a->stops = TR_PTR_ARRAY_INIT;
|
|
|
|
a->session = session;
|
2010-02-05 01:16:30 +00:00
|
|
|
a->slotsAvailable = MAX_CONCURRENT_TASKS;
|
2009-09-25 21:05:59 +00:00
|
|
|
a->upkeepTimer = tr_new0( struct event, 1 );
|
|
|
|
evtimer_set( a->upkeepTimer, onUpkeepTimer, a );
|
|
|
|
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 );
|
|
|
|
|
|
|
|
evtimer_del( announcer->upkeepTimer );
|
|
|
|
tr_free( announcer->upkeepTimer );
|
|
|
|
announcer->upkeepTimer = NULL;
|
|
|
|
|
|
|
|
tr_ptrArrayDestruct( &announcer->stops, NULL );
|
|
|
|
tr_ptrArrayDestruct( &announcer->hosts, hostFree );
|
|
|
|
|
|
|
|
session->announcer = NULL;
|
|
|
|
tr_free( announcer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* a row in tr_tier's list of trackers */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
tr_host * host;
|
|
|
|
|
|
|
|
char * announce;
|
|
|
|
char * scrape;
|
|
|
|
|
|
|
|
char * tracker_id;
|
|
|
|
|
|
|
|
int seederCount;
|
|
|
|
int leecherCount;
|
|
|
|
int downloadCount;
|
|
|
|
int downloaderCount;
|
2010-02-13 21:58:38 +00:00
|
|
|
|
2010-01-13 22:46:22 +00:00
|
|
|
uint32_t id;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* sent as the "key" argument in tracker requests
|
|
|
|
* 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' */
|
2009-09-25 21:05:59 +00:00
|
|
|
char key_param[KEYLEN + 1];
|
|
|
|
}
|
|
|
|
tr_tracker_item;
|
|
|
|
|
2010-02-14 03:28:54 +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;
|
|
|
|
t->id = o->id;
|
|
|
|
memcpy( t->key_param, o->key_param, sizeof( t->key_param ) );
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static void
|
|
|
|
generateKeyParam( char * msg, size_t msglen )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const char * pool = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
const int poolSize = strlen( pool );
|
|
|
|
|
|
|
|
for( i=0; i<msglen; ++i )
|
|
|
|
*msg++ = pool[tr_cryptoRandInt( poolSize )];
|
|
|
|
*msg = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_tracker_item*
|
|
|
|
trackerNew( tr_announcer * announcer,
|
|
|
|
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
|
|
|
{
|
|
|
|
tr_tracker_item * tracker = tr_new0( tr_tracker_item, 1 );
|
|
|
|
tracker->host = getHost( announcer, announce );
|
|
|
|
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->announce );
|
|
|
|
tr_free( tracker->scrape );
|
|
|
|
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 */
|
|
|
|
uint64_t byteCounts[3];
|
|
|
|
|
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-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 );
|
|
|
|
}
|
|
|
|
|
2010-02-13 21:58:38 +00:00
|
|
|
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) );
|
|
|
|
}
|
|
|
|
|
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 */
|
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
|
|
|
|
tierAddTracker( tr_announcer * announcer,
|
|
|
|
tr_tier * tier,
|
|
|
|
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
|
|
|
{
|
2010-01-13 22:46:22 +00:00
|
|
|
tr_tracker_item * tracker = trackerNew( announcer, 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
|
|
|
|
{
|
|
|
|
tr_ptrArray tiers;
|
|
|
|
tr_publisher publisher;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
tiers->publisher = TR_PUBLISHER_INIT;
|
|
|
|
return tiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tiersFree( tr_torrent_tiers * tiers )
|
|
|
|
{
|
|
|
|
tr_publisherDestruct( &tiers->publisher );
|
|
|
|
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
|
|
|
|
***/
|
|
|
|
|
|
|
|
static const tr_tracker_event emptyEvent = { 0, NULL, NULL, 0, 0 };
|
|
|
|
|
|
|
|
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;
|
|
|
|
tr_publisherPublish( &tiers->publisher, tier, &event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
publishErrorClear( tr_tier * tier )
|
|
|
|
{
|
|
|
|
publishMessage( tier, NULL, TR_TRACKER_ERROR_CLEAR );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
publishErrorMessageAndStop( tr_tier * tier, const char * msg )
|
|
|
|
{
|
|
|
|
tier->isRunning = FALSE;
|
|
|
|
|
|
|
|
publishMessage( tier, msg, TR_TRACKER_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
publishWarning( tr_tier * tier, const char * msg )
|
|
|
|
{
|
|
|
|
publishMessage( tier, msg, TR_TRACKER_WARNING );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
publishNewPeers( tr_tier * tier, tr_bool allAreSeeds,
|
|
|
|
const void * compact, int compactLen )
|
|
|
|
{
|
|
|
|
tr_tracker_event e = emptyEvent;
|
|
|
|
|
|
|
|
e.messageType = TR_TRACKER_PEERS;
|
|
|
|
e.allAreSeeds = allAreSeeds;
|
|
|
|
e.compact = compact;
|
|
|
|
e.compactLen = compactLen;
|
|
|
|
|
|
|
|
if( compactLen )
|
|
|
|
tr_publisherPublish( &tier->tor->tiers->publisher, tier, &e );
|
|
|
|
|
|
|
|
return compactLen / 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
publishNewPeersCompact( tr_tier * tier, tr_bool allAreSeeds,
|
|
|
|
const void * compact, int compactLen )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const uint8_t *compactWalk;
|
|
|
|
uint8_t *array, *walk;
|
|
|
|
const int peerCount = compactLen / 6;
|
|
|
|
const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
|
|
|
|
tr_address addr;
|
|
|
|
tr_port port;
|
|
|
|
|
|
|
|
addr.type = TR_AF_INET;
|
|
|
|
memset( &addr.addr, 0, sizeof( addr.addr ) );
|
|
|
|
array = tr_new( uint8_t, arrayLen );
|
|
|
|
for ( i=0, walk=array, compactWalk=compact ; i<peerCount ; ++i )
|
|
|
|
{
|
|
|
|
memcpy( &addr.addr.addr4, compactWalk, 4 );
|
|
|
|
memcpy( &port, compactWalk + 4, 2 );
|
|
|
|
|
|
|
|
memcpy( walk, &addr, sizeof( addr ) );
|
|
|
|
memcpy( walk + sizeof( addr ), &port, 2 );
|
|
|
|
|
|
|
|
walk += sizeof( tr_address ) + 2;
|
|
|
|
compactWalk += 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
publishNewPeers( tier, allAreSeeds, array, arrayLen );
|
|
|
|
|
|
|
|
tr_free( array );
|
|
|
|
|
|
|
|
return peerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
publishNewPeersCompact6( tr_tier * tier, tr_bool allAreSeeds,
|
|
|
|
const void * compact, int compactLen )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const uint8_t *compactWalk;
|
|
|
|
uint8_t *array, *walk;
|
|
|
|
const int peerCount = compactLen / 18;
|
|
|
|
const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
|
|
|
|
tr_address addr;
|
|
|
|
tr_port port;
|
|
|
|
|
|
|
|
addr.type = TR_AF_INET6;
|
|
|
|
memset( &addr.addr, 0, sizeof( addr.addr ) );
|
|
|
|
array = tr_new( uint8_t, arrayLen );
|
|
|
|
for ( i = 0, walk = array, compactWalk = compact ; i < peerCount ; ++i )
|
|
|
|
{
|
|
|
|
memcpy( &addr.addr.addr6, compactWalk, 16 );
|
|
|
|
memcpy( &port, compactWalk + 16, 2 );
|
|
|
|
compactWalk += 18;
|
|
|
|
|
|
|
|
memcpy( walk, &addr, sizeof( addr ) );
|
|
|
|
memcpy( walk + sizeof( addr ), &port, 2 );
|
|
|
|
walk += sizeof( tr_address ) + 2;
|
|
|
|
}
|
|
|
|
publishNewPeers( tier, allAreSeeds, array, arrayLen );
|
|
|
|
tr_free( array );
|
|
|
|
|
|
|
|
return peerCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
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( );
|
|
|
|
char * ret;
|
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"
|
|
|
|
"&uploaded=%" PRIu64
|
|
|
|
"&downloaded=%" PRIu64
|
|
|
|
"&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-01-24 23:32:49 +00:00
|
|
|
(int)tr_sessionGetPeerPort( 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] )
|
|
|
|
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
|
|
|
|
announce twice. At any rate, we're already computing our IPv6
|
|
|
|
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=");
|
2009-11-29 08:05:47 +00:00
|
|
|
tr_http_escape( buf, ipv6_readable, strlen(ipv6_readable), TRUE );
|
2009-11-10 17:03:23 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
|
|
|
|
dbgmsg( tier, "announce URL is \"%s\"", ret );
|
2010-02-03 06:32:14 +00:00
|
|
|
evbuffer_free( buf );
|
2009-09-25 21:05:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-10-02 02:03:30 +00:00
|
|
|
static tr_bool
|
|
|
|
announceURLIsSupported( const char * announce )
|
|
|
|
{
|
|
|
|
return ( announce != NULL )
|
2009-12-10 19:05:21 +00:00
|
|
|
&& ( strstr( announce, "tracker.thepiratebay.org" ) == NULL ) /* dead */
|
2009-10-02 02:03:30 +00:00
|
|
|
&& ( ( strstr( announce, "http://" ) == announce ) ||
|
|
|
|
( strstr( announce, "https://" ) == announce ) );
|
|
|
|
}
|
|
|
|
|
2009-10-02 04:54:02 +00:00
|
|
|
static void
|
|
|
|
addTorrentToTier( tr_announcer * announcer, tr_torrent_tiers * tiers, 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 )
|
|
|
|
if( announceURLIsSupported( trackers[i].announce ) )
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2010-01-13 22:40:07 +00:00
|
|
|
tierAddTracker( announcer, 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 *
|
|
|
|
tr_announcerAddTorrent( tr_announcer * announcer, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
tr_torrent_tiers * tiers;
|
|
|
|
|
|
|
|
assert( announcer != NULL );
|
|
|
|
assert( tr_isTorrent( tor ) );
|
|
|
|
|
|
|
|
tiers = tiersNew( );
|
|
|
|
|
|
|
|
addTorrentToTier( announcer, tiers, tor );
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
return tiers;
|
|
|
|
}
|
|
|
|
|
2010-02-14 03:40:39 +00:00
|
|
|
static void
|
|
|
|
tierAddAnnounce( tr_tier * tier, const char * announceEvent, time_t announceAt );
|
|
|
|
|
2009-10-02 04:54:02 +00:00
|
|
|
void
|
|
|
|
tr_announcerResetTorrent( tr_announcer * announcer, tr_torrent * tor )
|
|
|
|
{
|
2010-02-13 21:58:38 +00:00
|
|
|
tr_ptrArray oldTiers = TR_PTR_ARRAY_INIT;
|
|
|
|
|
|
|
|
/* if we had tiers already, make a backup of them */
|
2009-10-02 04:54:02 +00:00
|
|
|
if( tor->tiers != NULL )
|
|
|
|
{
|
2010-02-13 21:58:38 +00:00
|
|
|
oldTiers = tor->tiers->tiers;
|
|
|
|
tor->tiers->tiers = TR_PTR_ARRAY_INIT;
|
|
|
|
}
|
2009-10-02 04:54:02 +00:00
|
|
|
|
2010-02-13 21:58:38 +00:00
|
|
|
/* create the new tier/tracker structs */
|
|
|
|
addTorrentToTier( announcer, tor->tiers, tor );
|
2009-10-12 06:08:28 +00:00
|
|
|
|
2010-02-13 21:58:38 +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;
|
2010-02-14 05:34:03 +00:00
|
|
|
t->wasCopied = TRUE;
|
2010-02-14 03:28:54 +00:00
|
|
|
trackerItemCopyAttributes( item, o->currentTracker );
|
2010-02-13 21:58:38 +00:00
|
|
|
dbgmsg( t, "attributes copied to tier %d, tracker %d"
|
|
|
|
"from tier %d, tracker %d",
|
|
|
|
i, o->currentTrackerIndex, j, k );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-14 03:40:39 +00:00
|
|
|
|
|
|
|
/* kickstart any tiers that didn't get started */
|
|
|
|
if( tor->isRunning )
|
2010-02-13 21:58:38 +00:00
|
|
|
{
|
2010-02-14 03:40:39 +00:00
|
|
|
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];
|
2010-02-14 05:34:03 +00:00
|
|
|
if( !tier->wasCopied )
|
2010-02-14 03:40:39 +00:00
|
|
|
tierAddAnnounce( tier, STARTED, now );
|
|
|
|
}
|
2009-10-02 04:54:02 +00:00
|
|
|
}
|
2010-02-13 21:58:38 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_ptrArrayDestruct( &oldTiers, tierFree );
|
2009-10-02 04:54:02 +00:00
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_publisher_tag
|
|
|
|
tr_announcerSubscribe( struct tr_torrent_tiers * tiers,
|
|
|
|
tr_delivery_func func,
|
|
|
|
void * userData )
|
|
|
|
{
|
|
|
|
return tr_publisherSubscribe( &tiers->publisher, func, userData );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_announcerUnsubscribe( struct tr_torrent_tiers * tiers,
|
|
|
|
tr_publisher_tag tag )
|
|
|
|
{
|
|
|
|
if( tiers )
|
|
|
|
tr_publisherUnsubscribe( &tiers->publisher, tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
tierCanManualAnnounce( const tr_tier * tier )
|
|
|
|
{
|
|
|
|
return tier->isRunning
|
2009-11-26 18:47:08 +00:00
|
|
|
&& 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 );
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
dbgmsg( tier, "appended event \"%s\"; announcing in %d seconds\n", 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_announcerSubtractBytes( 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 );
|
|
|
|
uint64_t * setme = &tier->byteCounts[type];
|
|
|
|
*setme -= MIN( *setme, 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" );
|
|
|
|
s->host = tier->currentTracker->host;
|
|
|
|
tr_ptrArrayInsertSorted( &announcer->stops, s, compareStops );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tiersFree( tor->tiers );
|
|
|
|
tor->tiers = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return true if (1) we've tried it recently AND (2) it didn't respond... */
|
|
|
|
static tr_bool
|
|
|
|
hostIsNotResponding( const tr_host * host, const time_t now )
|
|
|
|
{
|
|
|
|
tr_bool b = ( host->lastRequestTime )
|
|
|
|
&& ( host->lastRequestTime >= ( now - SLOW_HOST_PENALTY_SECS ) )
|
|
|
|
&& ( host->lastResponseInterval > MAX_TRACKER_RESPONSE_TIME_SECS );
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_bool
|
|
|
|
tierIsNotResponding( const tr_tier * tier, const time_t now )
|
|
|
|
{
|
|
|
|
return !tier->currentTracker
|
|
|
|
|| hostIsNotResponding( tier->currentTracker->host, now );
|
|
|
|
}
|
|
|
|
|
2010-02-05 01:47:33 +00:00
|
|
|
static int
|
|
|
|
getRetryInterval( const tr_host * host )
|
|
|
|
{
|
|
|
|
int interval;
|
|
|
|
const int jitter = tr_cryptoWeakRandInt( 120 );
|
|
|
|
const time_t timeSinceLastSuccess = tr_time() - host->lastSuccessfulRequest;
|
|
|
|
if( timeSinceLastSuccess < 15*60 ) interval = 0;
|
|
|
|
else if( timeSinceLastSuccess < 30*60 ) interval = 60*4;
|
|
|
|
else if( timeSinceLastSuccess < 45*60 ) interval = 60*8;
|
|
|
|
else if( timeSinceLastSuccess < 60*60 ) interval = 60*16;
|
|
|
|
else interval = 60*32;
|
|
|
|
return interval + jitter;
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
static int
|
|
|
|
compareTiers( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
tr_bool af, bf;
|
|
|
|
const tr_tier * a = *(const tr_tier**)va;
|
|
|
|
const tr_tier * b = *(const tr_tier**)vb;
|
|
|
|
|
|
|
|
/* working domains come before non-working */
|
|
|
|
if( !ret ) {
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
af = tierIsNotResponding( a, now );
|
|
|
|
bf = tierIsNotResponding( b, now );
|
|
|
|
if( af != bf )
|
|
|
|
ret = !af ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stops come before starts */
|
|
|
|
if( !ret ) {
|
|
|
|
af = a->tor->isRunning;
|
|
|
|
bf = b->tor->isRunning;
|
|
|
|
if( af != bf )
|
|
|
|
ret = af ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* upload comes before download */
|
|
|
|
if( !ret )
|
2010-02-08 16:47:30 +00:00
|
|
|
ret = compareTransfer( a->byteCounts[TR_ANN_UP], a->byteCounts[TR_ANN_DOWN],
|
|
|
|
b->byteCounts[TR_ANN_UP], b->byteCounts[TR_ANN_DOWN] );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/* incomplete comes before complete */
|
|
|
|
if( !ret ) {
|
|
|
|
af = a->tor->completeness == TR_LEECH;
|
|
|
|
bf = b->tor->completeness == TR_LEECH;
|
|
|
|
if( af != bf )
|
|
|
|
return af ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* private before public */
|
|
|
|
if( !ret ) {
|
|
|
|
af = tr_torrentIsPrivate( a->tor );
|
|
|
|
bf = tr_torrentIsPrivate( b->tor );
|
|
|
|
if( af != bf )
|
|
|
|
ret = af ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *
|
|
|
|
parseOldPeers( tr_benc * bePeers, size_t * byteCount )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t * array, *walk;
|
|
|
|
const int peerCount = bePeers->val.l.count;
|
|
|
|
|
|
|
|
assert( tr_bencIsList( bePeers ) );
|
|
|
|
|
|
|
|
array = tr_new( uint8_t, peerCount * ( sizeof( tr_address ) + 2 ) );
|
|
|
|
|
|
|
|
for( i = 0, walk = array; i < peerCount; ++i )
|
|
|
|
{
|
|
|
|
const char * s;
|
|
|
|
int64_t itmp;
|
|
|
|
tr_address addr;
|
|
|
|
tr_port port;
|
|
|
|
tr_benc * peer = &bePeers->val.l.vals[i];
|
|
|
|
|
|
|
|
if( tr_bencDictFindStr( peer, "ip", &s ) )
|
|
|
|
if( tr_pton( s, &addr ) == NULL )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( !tr_bencDictFindInt( peer, "port", &itmp )
|
|
|
|
|| itmp < 0
|
|
|
|
|| itmp > USHRT_MAX )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy( walk, &addr, sizeof( tr_address ) );
|
|
|
|
port = htons( itmp );
|
|
|
|
memcpy( walk + sizeof( tr_address ), &port, 2 );
|
|
|
|
walk += sizeof( tr_address ) + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
*byteCount = peerCount * sizeof( tr_address ) + 2;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
dbgmsg( tier, "response len: %d, isBenc: %d", (int)responseLen, (int)bencLoaded );
|
|
|
|
publishErrorClear( tier );
|
|
|
|
if( bencLoaded && tr_bencIsDict( &benc ) )
|
|
|
|
{
|
2010-02-01 01:13:25 +00:00
|
|
|
int peerCount = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
int incomplete = -1;
|
|
|
|
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;
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindInt( &benc, "complete", &i ) )
|
|
|
|
{
|
|
|
|
++scrapeFields;
|
|
|
|
tier->currentTracker->seederCount = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindInt( &benc, "incomplete", &i ) )
|
|
|
|
{
|
|
|
|
++scrapeFields;
|
|
|
|
tier->currentTracker->leecherCount = incomplete = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindInt( &benc, "downloaded", &i ) )
|
|
|
|
{
|
|
|
|
++scrapeFields;
|
|
|
|
tier->currentTracker->downloadCount = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindRaw( &benc, "peers", &raw, &rawlen ) )
|
|
|
|
{
|
|
|
|
/* "compact" extension */
|
|
|
|
const int allAreSeeds = incomplete == 0;
|
2010-02-01 03:39:54 +00:00
|
|
|
peerCount += publishNewPeersCompact( tier, allAreSeeds, 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 */
|
|
|
|
const tr_bool allAreSeeds = incomplete == 0;
|
|
|
|
size_t byteCount = 0;
|
|
|
|
uint8_t * array = parseOldPeers( tmp, &byteCount );
|
2010-02-01 03:39:54 +00:00
|
|
|
peerCount += publishNewPeers( tier, allAreSeeds, array, byteCount );
|
2010-02-11 03:12:03 +00:00
|
|
|
gotPeers = TRUE;
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_free( array );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindRaw( &benc, "peers6", &raw, &rawlen ) )
|
|
|
|
{
|
|
|
|
/* "compact" extension */
|
|
|
|
const tr_bool allAreSeeds = incomplete == 0;
|
2010-02-01 01:13:25 +00:00
|
|
|
peerCount += publishNewPeersCompact6( tier, allAreSeeds, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct announce_data
|
|
|
|
{
|
|
|
|
int torrentId;
|
|
|
|
int tierId;
|
|
|
|
time_t timeSent;
|
2010-02-01 03:49:59 +00:00
|
|
|
const char * event;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
/** If the request succeeds, the value for tier's "isRunning" flag */
|
|
|
|
tr_bool isRunningOnSuccess;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
onAnnounceDone( tr_session * session,
|
|
|
|
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 );
|
|
|
|
tr_bool gotScrape = FALSE;
|
|
|
|
tr_bool success = FALSE;
|
|
|
|
const time_t now = time ( NULL );
|
2010-02-10 02:59:15 +00:00
|
|
|
const char * announceEvent = data->event;
|
|
|
|
const tr_bool isStopped = !strcmp( announceEvent, "stopped" );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
if( announcer && tier )
|
|
|
|
{
|
|
|
|
if( tier->currentTracker->host )
|
|
|
|
{
|
|
|
|
tr_host * host = tier->currentTracker->host;
|
|
|
|
host->lastRequestTime = data->timeSent;
|
|
|
|
host->lastResponseInterval = now - data->timeSent;
|
|
|
|
}
|
|
|
|
|
2010-02-01 00:28:04 +00:00
|
|
|
tier->lastAnnounceTime = now;
|
|
|
|
|
2009-09-25 21:05:59 +00:00
|
|
|
if( responseCode == HTTP_OK )
|
|
|
|
{
|
2010-02-11 03:12:03 +00:00
|
|
|
success = parseAnnounceResponse( tier, response, responseLen, &gotScrape );
|
2009-09-25 21:05:59 +00:00
|
|
|
dbgmsg( tier, "success is %d", success );
|
2010-02-08 16:47:30 +00:00
|
|
|
|
|
|
|
if( isStopped )
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
else if( responseCode )
|
|
|
|
{
|
|
|
|
/* %1$ld - http status code, such as 404
|
|
|
|
* %2$s - human-readable explanation of the http status code */
|
|
|
|
char * buf = tr_strdup_printf(
|
2010-02-11 02:52:54 +00:00
|
|
|
_( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
|
2009-09-25 21:05:59 +00:00
|
|
|
responseCode,
|
|
|
|
tr_webGetResponseStr( responseCode ) );
|
|
|
|
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr, buf,
|
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
|
|
|
|
2009-10-22 01:28:39 +00:00
|
|
|
/* if the response is serious, *and* if the response may require
|
|
|
|
* human intervention, then notify the user... otherwise just log it */
|
2009-09-25 21:05:59 +00:00
|
|
|
if( responseCode >= 400 )
|
2009-10-22 01:28:39 +00:00
|
|
|
if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
|
|
|
|
publishWarning( tier, buf );
|
2009-09-25 21:05:59 +00:00
|
|
|
tr_torinf( tier->tor, "%s", buf );
|
|
|
|
dbgmsg( tier, "%s", buf );
|
|
|
|
|
|
|
|
tr_free( buf );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_strlcpy( tier->lastAnnounceStr,
|
|
|
|
_( "tracker did not respond" ),
|
|
|
|
sizeof( tier->lastAnnounceStr ) );
|
|
|
|
dbgmsg( tier, "%s", tier->lastAnnounceStr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tier )
|
|
|
|
{
|
|
|
|
tier->isAnnouncing = FALSE;
|
|
|
|
|
|
|
|
if( responseCode == 0 )
|
|
|
|
{
|
2010-02-05 01:47:33 +00:00
|
|
|
const int interval = getRetryInterval( tier->currentTracker->host );
|
2010-01-28 19:33:48 +00:00
|
|
|
dbgmsg( tier, "No response from tracker... retrying in %d seconds.", interval );
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->manualAnnounceAllowedAt = ~(time_t)0;
|
2010-02-10 02:59:15 +00:00
|
|
|
tierAddAnnounce( tier, announceEvent, now + interval );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
else if( 200 <= responseCode && responseCode <= 299 )
|
|
|
|
{
|
2009-10-03 14:08:05 +00:00
|
|
|
const int interval = tier->announceIntervalSec;
|
2009-09-25 21:05:59 +00:00
|
|
|
dbgmsg( tier, "request succeeded. reannouncing in %d seconds", interval );
|
|
|
|
|
|
|
|
if( gotScrape )
|
|
|
|
{
|
|
|
|
tier->lastScrapeTime = now;
|
2009-12-14 17:17:05 +00:00
|
|
|
tier->lastScrapeSucceeded = 1;
|
2009-10-03 14:08:05 +00:00
|
|
|
tier->scrapeAt = now + tier->scrapeIntervalSec;
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
|
|
|
|
|
2010-02-10 02:59:15 +00:00
|
|
|
/* if we're running and the queue is empty, add the next update */
|
|
|
|
if( !isStopped && !tr_ptrArraySize( &tier->announceEvents ) )
|
|
|
|
{
|
|
|
|
tierAddAnnounce( tier, "", now + interval );
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
else if( 300 <= responseCode && responseCode <= 399 )
|
|
|
|
{
|
|
|
|
/* how did this get here? libcurl handles this */
|
|
|
|
const int interval = 5;
|
|
|
|
dbgmsg( tier, "got a redirect. retrying in %d seconds", interval );
|
2010-02-10 02:59:15 +00:00
|
|
|
tierAddAnnounce( tier, announceEvent, now + interval );
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
|
|
|
|
}
|
|
|
|
else if( 400 <= responseCode && responseCode <= 499 )
|
|
|
|
{
|
|
|
|
/* The request could not be understood by the server due to
|
|
|
|
* malformed syntax. The client SHOULD NOT repeat the
|
|
|
|
* request without modifications. */
|
2009-10-27 21:03:13 +00:00
|
|
|
if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
|
|
|
|
publishErrorMessageAndStop( tier, _( "Tracker returned a 4xx message" ) );
|
2010-02-14 05:16:04 +00:00
|
|
|
tier->announceAt = 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->manualAnnounceAllowedAt = ~(time_t)0;
|
|
|
|
}
|
|
|
|
else if( 500 <= responseCode && responseCode <= 599 )
|
|
|
|
{
|
|
|
|
/* Response status codes beginning with the digit "5" indicate
|
|
|
|
* cases in which the server is aware that it has erred or is
|
|
|
|
* incapable of performing the request. So we pause a bit and
|
|
|
|
* try again. */
|
2010-02-05 01:47:33 +00:00
|
|
|
const int interval = getRetryInterval( tier->currentTracker->host );
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->manualAnnounceAllowedAt = ~(time_t)0;
|
2010-02-10 02:59:15 +00:00
|
|
|
tierAddAnnounce( tier, announceEvent, now + interval );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* WTF did we get?? */
|
2009-10-03 14:08:05 +00:00
|
|
|
const int interval = 120;
|
2009-09-25 21:05:59 +00:00
|
|
|
dbgmsg( tier, "Invalid response from tracker... retrying in two minutes." );
|
|
|
|
tier->manualAnnounceAllowedAt = ~(time_t)0;
|
2010-02-10 02:59:15 +00:00
|
|
|
tierAddAnnounce( tier, announceEvent, now + interval );
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tier->lastAnnounceSucceeded = success;
|
2010-02-05 05:16:18 +00:00
|
|
|
tier->lastAnnounceTimedOut = responseCode == 0;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
if( success )
|
2010-02-05 01:47:33 +00:00
|
|
|
{
|
2009-09-25 21:05:59 +00:00
|
|
|
tier->isRunning = data->isRunningOnSuccess;
|
|
|
|
|
2010-02-05 01:47:33 +00:00
|
|
|
if( tier->currentTracker->host )
|
|
|
|
tier->currentTracker->host->lastSuccessfulRequest = now;
|
|
|
|
}
|
2010-02-10 02:59:15 +00:00
|
|
|
else if( responseCode != HTTP_OK )
|
|
|
|
{
|
2009-09-25 21:05:59 +00:00
|
|
|
tierIncrementTracker( tier );
|
2010-02-10 02:59:15 +00:00
|
|
|
|
|
|
|
tr_ptrArrayInsert( &tier->announceEvents, (void*)announceEvent, 0 );
|
|
|
|
}
|
2009-09-25 21:05:59 +00:00
|
|
|
}
|
|
|
|
|
2009-09-27 00:27:51 +00:00
|
|
|
if( announcer != NULL )
|
|
|
|
{
|
2010-02-05 01:16:30 +00:00
|
|
|
++announcer->slotsAvailable;
|
2009-09-27 00:27:51 +00:00
|
|
|
}
|
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;
|
|
|
|
char ** events;
|
|
|
|
const char * str = NULL;
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
assert( tier != NULL );
|
|
|
|
assert( tr_isTorrent( tier->tor ) );
|
|
|
|
|
2010-02-10 04:42:34 +00:00
|
|
|
/* rule #1: if "stopped" is in the queue, ignore everything before it */
|
2010-02-10 02:59:15 +00:00
|
|
|
events = (char**) tr_ptrArrayPeek( &tier->announceEvents, &n );
|
|
|
|
for( i=0; pos<0 && i<n; ++i )
|
|
|
|
if( !strcmp( events[i], "stopped" ) )
|
|
|
|
pos = i;
|
|
|
|
|
|
|
|
/* rule #2: if the next two events are the same, ignore the first one */
|
|
|
|
for( i=0; pos<0 && i<=n-2; ++i )
|
|
|
|
if( strcmp( events[i], events[i+1] ) )
|
|
|
|
pos = i;
|
|
|
|
|
|
|
|
/* otherwise use the next announce event in line */
|
|
|
|
if( ( pos < 0 ) && ( n > 0 ) )
|
|
|
|
pos = 0;
|
|
|
|
|
|
|
|
/* rule #3: BEP 21: "In order to tell the tracker that a peer is a
|
|
|
|
* 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-10 02:59:15 +00:00
|
|
|
if( !str || !strcmp( str, "stopped" ) )
|
|
|
|
str = "paused";
|
|
|
|
|
|
|
|
/* 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-10 02:59:15 +00:00
|
|
|
tr_ptrArrayAppend( &tmp, events[i] );
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
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( tier->currentTracker->host )
|
|
|
|
{
|
|
|
|
tr_host * host = tier->currentTracker->host;
|
|
|
|
host->lastRequestTime = data->timeSent;
|
|
|
|
host->lastResponseInterval = now - data->timeSent;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
tr_tordbg( tier->tor, "%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 );
|
|
|
|
tr_tordbg( tier->tor, "%s", tier->lastScrapeStr );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-05 01:47:33 +00:00
|
|
|
const int interval = getRetryInterval( tier->currentTracker->host );
|
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 */
|
|
|
|
if( !responseCode )
|
|
|
|
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;
|
2010-02-05 01:47:33 +00:00
|
|
|
|
|
|
|
if( success && tier->currentTracker->host )
|
|
|
|
tier->currentTracker->host->lastSuccessfulRequest = now;
|
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;
|
2009-11-26 18:47:08 +00:00
|
|
|
const time_t now = tr_time( );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
tier->lastScrapeStartTime = now;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
/* The DHT is not ready yet. Try again soon. */
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
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 ) );
|
|
|
|
|
|
|
|
/* 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;
|
2009-09-26 03:36:58 +00:00
|
|
|
tr_strlcpy( st->host, tracker->host->name, sizeof( st->host ) );
|
|
|
|
tr_strlcpy( st->announce, tracker->announce, sizeof( st->announce ) );
|
2010-02-14 04:48:12 +00:00
|
|
|
st->tier = i;
|
2009-10-10 00:17:00 +00:00
|
|
|
st->isBackup = tracker != tier->currentTracker;
|
2009-09-25 21:05:59 +00:00
|
|
|
st->lastScrapeStartTime = tier->lastScrapeStartTime;
|
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;
|
|
|
|
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 );
|
|
|
|
}
|