2007-08-16 20:00:06 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2007-08-16 20:00:06 +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.
|
2007-08-18 17:19:49 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
2007-08-16 20:00:06 +00:00
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <assert.h>
|
2007-08-16 20:00:06 +00:00
|
|
|
#include <ctype.h> /* isalnum */
|
|
|
|
#include <limits.h> /* INT_MAX */
|
|
|
|
#include <stdio.h> /* snprintf */
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <stdlib.h>
|
2007-08-16 20:00:06 +00:00
|
|
|
#include <string.h> /* strcmp, strchr */
|
|
|
|
#include <sys/queue.h> /* for evhttp */
|
|
|
|
#include <sys/types.h> /* for evhttp */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
#include <event.h>
|
|
|
|
#include <evhttp.h>
|
2007-07-14 16:29:21 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "bencode.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "completion.h"
|
2007-07-09 20:10:42 +00:00
|
|
|
#include "net.h"
|
2007-08-16 20:00:06 +00:00
|
|
|
#include "ptrarray.h"
|
|
|
|
#include "publish.h"
|
2007-09-21 05:31:29 +00:00
|
|
|
#include "shared.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "tracker.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "trevent.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
#define MINUTES_TO_MSEC(N) ((N) * 60 * 1000)
|
|
|
|
|
|
|
|
/* manual announces via "update tracker" are allowed this frequently */
|
|
|
|
#define MANUAL_ANNOUNCE_INTERVAL_MSEC (MINUTES_TO_MSEC(10))
|
|
|
|
|
|
|
|
/* unless the tracker tells us otherwise, rescrape this frequently */
|
|
|
|
#define DEFAULT_SCRAPE_INTERVAL_MSEC (MINUTES_TO_MSEC(15))
|
|
|
|
|
|
|
|
/* unless the tracker tells us otherwise, reannounce this frequently */
|
|
|
|
#define DEFAULT_ANNOUNCE_INTERVAL_MSEC (MINUTES_TO_MSEC(20))
|
|
|
|
|
2007-10-07 16:07:19 +00:00
|
|
|
/* this is how long we'll leave a request hanging before timeout */
|
2007-10-08 17:05:39 +00:00
|
|
|
#define TIMEOUT_INTERVAL_SEC 40
|
|
|
|
#define STOPPING_TIMEOUT_INTERVAL_SEC 8
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-08-20 02:29:36 +00:00
|
|
|
/* the value of the 'numwant' argument passed in tracker requests */
|
2007-10-06 18:20:52 +00:00
|
|
|
#define NUMWANT 128
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
/* the length of the 'key' argument passed in tracker requests */
|
2007-08-19 00:02:23 +00:00
|
|
|
#define TR_KEY_LEN 10
|
2007-07-30 17:11:00 +00:00
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2007-07-30 17:11:00 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
typedef struct
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_handle * handle;
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArray * torrents;
|
|
|
|
tr_ptrArray * scraping;
|
|
|
|
tr_ptrArray * scrapeQueue;
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
/* these are set from the latest scrape or tracker response */
|
|
|
|
int announceIntervalMsec;
|
|
|
|
int minAnnounceIntervalMsec;
|
|
|
|
int scrapeIntervalMsec;
|
|
|
|
|
|
|
|
/* calculated when we get fewer scrapes
|
|
|
|
back than we asked for */
|
|
|
|
int multiscrapeMax;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_tracker_info * redirect;
|
|
|
|
tr_tracker_info * addresses;
|
2007-08-16 20:00:06 +00:00
|
|
|
int addressIndex;
|
|
|
|
int addressCount;
|
|
|
|
int * tierFronts;
|
|
|
|
|
|
|
|
char * primaryAddress;
|
|
|
|
|
|
|
|
/* sent as the "key" argument in tracker requests
|
|
|
|
to verify us if our IP address changes.
|
|
|
|
This is immutable for the life of the tracker object. */
|
|
|
|
char key_param[TR_KEY_LEN+1];
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_timer * scrapeTimer;
|
2007-08-16 20:00:06 +00:00
|
|
|
}
|
|
|
|
Tracker;
|
|
|
|
|
|
|
|
/* this is the Torrent struct, but since it's the pointer
|
|
|
|
passed around in the public API of this tracker module,
|
2007-09-20 16:32:01 +00:00
|
|
|
its *public* name is tr_tracker... wheee */
|
|
|
|
typedef struct tr_tracker
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_publisher_t * publisher;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/* torrent hash string */
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
|
|
|
char escaped[SHA_DIGEST_LENGTH * 3 + 1];
|
2007-10-04 19:58:30 +00:00
|
|
|
char * name;
|
2007-04-08 05:41:44 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/* corresponds to the peer_id sent as a tracker request parameter.
|
|
|
|
OiNK's op TooMuchTime says: "When the same torrent is opened and
|
|
|
|
closed and opened again without quitting Transmission ...
|
|
|
|
change the peerid. It would help sometimes if a stopped event
|
|
|
|
was missed to ensure that we didn't think someone was cheating. */
|
|
|
|
char peer_id[TR_ID_LEN + 1];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-18 03:11:08 +00:00
|
|
|
/* these are set from the latest scrape or tracker response...
|
|
|
|
-1 means 'unknown' */
|
2007-08-16 20:00:06 +00:00
|
|
|
int timesDownloaded;
|
|
|
|
int seeders;
|
|
|
|
int leechers;
|
|
|
|
char * trackerID;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/* the last tracker request we sent. (started, stopped, etc.)
|
|
|
|
automatic announces are an empty string;
|
|
|
|
NULL means no message has ever been sent */
|
|
|
|
char * lastRequest;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
uint64_t manualAnnounceAllowedAt;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
Tracker * tracker;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_timer * scrapeTimer;
|
|
|
|
tr_timer * reannounceTimer;
|
2007-04-08 05:41:44 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
unsigned int isRunning : 1;
|
2007-08-16 20:00:06 +00:00
|
|
|
}
|
|
|
|
Torrent;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static int
|
|
|
|
trackerCompare( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const Tracker * a = ( const Tracker * ) va;
|
|
|
|
const Tracker * b = ( const Tracker * ) vb;
|
|
|
|
return strcmp( a->primaryAddress, b->primaryAddress );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static int
|
|
|
|
torrentCompare( const void * va, const void * vb )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
const Torrent * a = (const Torrent*) va;
|
|
|
|
const Torrent * b = (const Torrent*) vb;
|
|
|
|
return memcmp( a->hash, b->hash, SHA_DIGEST_LENGTH );
|
|
|
|
}
|
|
|
|
|
2007-08-18 08:28:57 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-10-07 16:07:19 +00:00
|
|
|
static int
|
|
|
|
freeConnection( void * evcon )
|
2007-08-18 08:28:57 +00:00
|
|
|
{
|
2007-10-07 16:07:19 +00:00
|
|
|
evhttp_connection_free( evcon );
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-08-18 08:28:57 +00:00
|
|
|
|
2007-10-07 16:07:19 +00:00
|
|
|
static void
|
|
|
|
connectionClosedCB( struct evhttp_connection * evcon, void * handle )
|
|
|
|
{
|
|
|
|
tr_timerNew( handle, freeConnection, evcon, 100 );
|
|
|
|
}
|
2007-08-18 08:28:57 +00:00
|
|
|
|
2007-10-07 16:07:19 +00:00
|
|
|
static struct evhttp_connection*
|
|
|
|
getConnection( Tracker * tracker, const char * address, int port )
|
|
|
|
{
|
|
|
|
struct evhttp_connection * c = evhttp_connection_new( address, port );
|
|
|
|
evhttp_connection_set_closecb( c, connectionClosedCB, tracker->handle );
|
|
|
|
return c;
|
2007-08-18 08:28:57 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/***
|
|
|
|
**** PUBLISH
|
|
|
|
***/
|
2007-02-27 04:00:38 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static const tr_tracker_event_t emptyEvent = { 0, NULL, NULL, NULL, 0 };
|
|
|
|
|
|
|
|
static void
|
|
|
|
publishMessage( Torrent * tor, const char * msg, int type )
|
|
|
|
{
|
|
|
|
tr_tracker_event_t event = emptyEvent;
|
|
|
|
event.hash = tor->hash;
|
|
|
|
event.messageType = type;
|
|
|
|
event.text = msg;
|
|
|
|
tr_publisherPublish( tor->publisher, tor, &event );
|
|
|
|
}
|
2007-09-25 18:39:58 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
publishErrorClear( Torrent * tor )
|
|
|
|
{
|
|
|
|
publishMessage( tor, NULL, TR_TRACKER_ERROR_CLEAR );
|
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static void
|
|
|
|
publishErrorMessage( Torrent * tor, const char * msg )
|
|
|
|
{
|
|
|
|
publishMessage( tor, msg, TR_TRACKER_ERROR );
|
|
|
|
}
|
2007-09-25 18:39:58 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static void
|
|
|
|
publishWarningMessage( Torrent * tor, const char * msg )
|
|
|
|
{
|
|
|
|
publishMessage( tor, msg, TR_TRACKER_WARNING );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static void
|
|
|
|
publishNewPeers( Torrent * tor, int count, uint8_t * peers )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_tracker_event_t event = emptyEvent;
|
|
|
|
event.hash = tor->hash;
|
|
|
|
event.messageType = TR_TRACKER_PEERS;
|
|
|
|
event.peerCount = count;
|
|
|
|
event.peerCompact = peers;
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_inf( "Torrent \"%s\" got %d new peers", tor->name, count );
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_publisherPublish( tor->publisher, tor, &event );
|
2007-07-30 17:11:00 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/***
|
|
|
|
**** LIFE CYCLE
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_ptrArray *
|
2007-08-16 20:00:06 +00:00
|
|
|
getTrackerLookupTable( void )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_ptrArray * myTrackers = NULL;
|
2007-08-16 20:00:06 +00:00
|
|
|
if( !myTrackers )
|
|
|
|
myTrackers = tr_ptrArrayNew( );
|
|
|
|
return myTrackers;
|
|
|
|
}
|
2007-07-30 17:11:00 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static void
|
|
|
|
generateKeyParam( char * msg, int len )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char * pool = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
for( i=0; i<len; ++i )
|
|
|
|
*msg++ = pool[tr_rand(36)];
|
|
|
|
*msg = '\0';
|
|
|
|
}
|
2007-08-02 02:38:42 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static int onTrackerScrapeNow( void* );
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_trackerScrapeSoon( Tracker * t )
|
|
|
|
{
|
2007-08-17 03:03:28 +00:00
|
|
|
/* don't start more than one scrape at once for the same tracker... */
|
|
|
|
if( !tr_ptrArrayEmpty( t->scraping ) )
|
|
|
|
return;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
if( !t->scrapeTimer )
|
2007-09-25 18:39:58 +00:00
|
|
|
t->scrapeTimer = tr_timerNew( t->handle, onTrackerScrapeNow, t, 5000 );
|
2007-08-16 20:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Tracker*
|
2007-10-08 19:09:20 +00:00
|
|
|
getExistingTracker( const char * primaryAddress )
|
2007-08-16 20:00:06 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArray * trackers = getTrackerLookupTable( );
|
2007-10-08 19:09:20 +00:00
|
|
|
Tracker tmp;
|
|
|
|
assert( primaryAddress && *primaryAddress );
|
|
|
|
tmp.primaryAddress = (char*) primaryAddress;
|
|
|
|
return tr_ptrArrayFindSorted( trackers, &tmp, trackerCompare );
|
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-10-08 19:09:20 +00:00
|
|
|
static Tracker*
|
|
|
|
tr_trackerGet( const tr_torrent * tor )
|
|
|
|
{
|
|
|
|
const tr_info * info = &tor->info;
|
|
|
|
Tracker * t = getExistingTracker( info->primaryAddress );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
if( t == NULL ) /* no such tracker.... create one */
|
|
|
|
{
|
|
|
|
int i, j, sum, *iwalk;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_tracker_info * nwalk;
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_dbg( "making a new tracker for \"%s\"", info->primaryAddress );
|
|
|
|
|
|
|
|
t = tr_new0( Tracker, 1 );
|
2007-08-18 03:02:32 +00:00
|
|
|
t->handle = tor->handle;
|
2007-08-16 20:00:06 +00:00
|
|
|
t->primaryAddress = tr_strdup( info->primaryAddress );
|
|
|
|
t->scrapeIntervalMsec = DEFAULT_SCRAPE_INTERVAL_MSEC;
|
|
|
|
t->announceIntervalMsec = DEFAULT_ANNOUNCE_INTERVAL_MSEC;
|
|
|
|
t->minAnnounceIntervalMsec = DEFAULT_ANNOUNCE_INTERVAL_MSEC;
|
|
|
|
t->multiscrapeMax = INT_MAX;
|
|
|
|
t->torrents = tr_ptrArrayNew( );
|
|
|
|
t->scraping = tr_ptrArrayNew( );
|
|
|
|
t->scrapeQueue = tr_ptrArrayNew( );
|
|
|
|
generateKeyParam( t->key_param, TR_KEY_LEN );
|
|
|
|
|
|
|
|
for( sum=i=0; i<info->trackerTiers; ++i )
|
|
|
|
sum += info->trackerList[i].count;
|
2007-09-20 16:32:01 +00:00
|
|
|
t->addresses = nwalk = tr_new0( tr_tracker_info, sum );
|
2007-08-16 20:00:06 +00:00
|
|
|
t->addressIndex = 0;
|
|
|
|
t->addressCount = sum;
|
|
|
|
t->tierFronts = iwalk = tr_new0( int, sum );
|
|
|
|
|
|
|
|
for( i=0; i<info->trackerTiers; ++i )
|
2006-12-23 21:18:58 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
const int tierFront = nwalk - t->addresses;
|
|
|
|
|
|
|
|
for( j=0; j<info->trackerList[i].count; ++j )
|
2006-12-23 21:18:58 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_tracker_info * src = &info->trackerList[i].list[j];
|
2007-08-16 20:00:06 +00:00
|
|
|
nwalk->address = tr_strdup( src->address );
|
|
|
|
nwalk->port = src->port;
|
|
|
|
nwalk->announce = tr_strdup( src->announce );
|
|
|
|
nwalk->scrape = tr_strdup( src->scrape );
|
|
|
|
++nwalk;
|
|
|
|
|
|
|
|
*iwalk++ = tierFront;
|
2006-12-23 21:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
assert( nwalk - t->addresses == sum );
|
|
|
|
assert( iwalk - t->tierFronts == sum );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-10-08 19:09:20 +00:00
|
|
|
tr_ptrArrayInsertSorted( getTrackerLookupTable( ), t, trackerCompare );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
return t;
|
|
|
|
}
|
2007-08-14 20:08:57 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static Torrent *
|
|
|
|
getExistingTorrent( Tracker * t, const uint8_t hash[SHA_DIGEST_LENGTH] )
|
|
|
|
{
|
|
|
|
Torrent tmp;
|
|
|
|
memcpy( tmp.hash, hash, SHA_DIGEST_LENGTH );
|
|
|
|
return tr_ptrArrayFindSorted( t->torrents, &tmp, torrentCompare );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static void
|
2007-08-20 02:29:36 +00:00
|
|
|
escape( char * out, const uint8_t * in, int in_len ) /* rfc2396 */
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
const uint8_t *end = in + in_len;
|
|
|
|
while( in != end )
|
2007-08-19 00:02:23 +00:00
|
|
|
if( isalnum(*in) )
|
|
|
|
*out++ = (char) *in++;
|
|
|
|
else
|
|
|
|
out += snprintf( out, 4, "%%%02X", (unsigned int)*in++ );
|
2007-08-16 20:00:06 +00:00
|
|
|
*out = '\0';
|
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2007-08-21 00:01:14 +00:00
|
|
|
onTorrentFreeNow( void * vtor )
|
2007-08-16 20:00:06 +00:00
|
|
|
{
|
2007-08-21 00:01:14 +00:00
|
|
|
Torrent * tor = (Torrent *) vtor;
|
2007-08-16 20:00:06 +00:00
|
|
|
Tracker * t = tor->tracker;
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_ptrArrayRemoveSorted( t->torrents, tor, torrentCompare );
|
2007-08-19 02:55:11 +00:00
|
|
|
tr_ptrArrayRemoveSorted( t->scrapeQueue, tor, torrentCompare );
|
|
|
|
tr_ptrArrayRemoveSorted( t->scraping, tor, torrentCompare );
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_timerFree( &tor->scrapeTimer );
|
|
|
|
tr_timerFree( &tor->reannounceTimer );
|
|
|
|
tr_publisherFree( &tor->publisher );
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_free( tor->name );
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_free( tor->trackerID );
|
|
|
|
tr_free( tor->lastRequest );
|
|
|
|
tr_free( tor );
|
|
|
|
|
|
|
|
if( tr_ptrArrayEmpty( t->torrents ) ) /* last one.. free the tracker too */
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
int i;
|
|
|
|
tr_ptrArrayRemoveSorted( getTrackerLookupTable( ), t, trackerCompare );
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
tr_ptrArrayFree( t->torrents, NULL );
|
|
|
|
tr_ptrArrayFree( t->scrapeQueue, NULL );
|
|
|
|
tr_ptrArrayFree( t->scraping, NULL );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
for( i=0; i<t->addressCount; ++i )
|
|
|
|
tr_trackerInfoClear( &t->addresses[i] );
|
|
|
|
|
|
|
|
if( t->redirect ) {
|
|
|
|
tr_trackerInfoClear( t->redirect );
|
|
|
|
tr_free( t->redirect );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_timerFree( &t->scrapeTimer );
|
2007-08-19 02:55:11 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_free( t->primaryAddress );
|
|
|
|
tr_free( t->addresses );
|
|
|
|
tr_free( t->tierFronts );
|
|
|
|
tr_free( t );
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
2007-08-21 00:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_trackerFree( Torrent * tor )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_runInEventThread( tor->tracker->handle, onTorrentFreeNow, tor );
|
2007-08-16 20:00:06 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
Torrent*
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_trackerNew( tr_torrent * torrent )
|
2007-08-16 20:00:06 +00:00
|
|
|
{
|
|
|
|
Torrent * tor;
|
2007-08-18 03:02:32 +00:00
|
|
|
Tracker * t = tr_trackerGet( torrent );
|
2007-08-16 20:00:06 +00:00
|
|
|
assert( getExistingTorrent( t, torrent->info.hash ) == NULL );
|
|
|
|
|
|
|
|
/* create a new Torrent and queue it for scraping */
|
|
|
|
tor = tr_new0( Torrent, 1 );
|
|
|
|
tor->publisher = tr_publisherNew( );
|
|
|
|
tor->tracker = t;
|
2007-10-04 19:58:30 +00:00
|
|
|
/*tor->torrent = torrent;*/
|
2007-08-18 03:11:08 +00:00
|
|
|
tor->timesDownloaded = -1;
|
|
|
|
tor->seeders = -1;
|
|
|
|
tor->leechers = -1;
|
2007-08-16 20:00:06 +00:00
|
|
|
tor->manualAnnounceAllowedAt = ~0;
|
2007-10-04 19:58:30 +00:00
|
|
|
tor->name = tr_strdup( torrent->info.name );
|
2007-08-16 20:00:06 +00:00
|
|
|
memcpy( tor->hash, torrent->info.hash, SHA_DIGEST_LENGTH );
|
|
|
|
escape( tor->escaped, torrent->info.hash, SHA_DIGEST_LENGTH );
|
|
|
|
tr_ptrArrayInsertSorted( t->torrents, tor, torrentCompare );
|
|
|
|
tr_ptrArrayInsertSorted( t->scrapeQueue, tor, torrentCompare );
|
|
|
|
tr_trackerScrapeSoon( t );
|
|
|
|
return tor;
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/***
|
|
|
|
**** UTIL
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
|
|
|
parseBencResponse( struct evhttp_request * req, benc_val_t * setme )
|
2007-07-30 17:11:00 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
const unsigned char * body = EVBUFFER_DATA( req->input_buffer );
|
|
|
|
const int bodylen = EVBUFFER_LENGTH( req->input_buffer );
|
|
|
|
int ret = 1;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i=0; ret && i<bodylen; ++i )
|
|
|
|
if( !tr_bencLoad( body+i, bodylen-1, setme, NULL ) )
|
|
|
|
ret = 0;
|
|
|
|
return ret;
|
2007-07-30 17:11:00 +00:00
|
|
|
}
|
|
|
|
|
2007-08-18 06:06:53 +00:00
|
|
|
static char*
|
2007-08-16 20:00:06 +00:00
|
|
|
updateAddresses( Tracker * t, const struct evhttp_request * req )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-18 06:06:53 +00:00
|
|
|
char * ret = NULL;
|
2007-08-16 20:00:06 +00:00
|
|
|
int moveToNextAddress = FALSE;
|
|
|
|
|
|
|
|
if( !req )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-25 18:39:58 +00:00
|
|
|
ret = tr_strdup( "No response from tracker -- will keep trying." );
|
2007-08-18 06:06:53 +00:00
|
|
|
tr_inf( ret );
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
moveToNextAddress = TRUE;
|
|
|
|
}
|
|
|
|
else if( req->response_code == HTTP_OK )
|
|
|
|
{
|
|
|
|
if( t->redirect != NULL )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
/* multitracker spec: "if a connection with a tracker is
|
|
|
|
successful, it will be moved to the front of the tier." */
|
|
|
|
const int i = t->addressIndex;
|
|
|
|
const int j = t->tierFronts[i];
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_tracker_info swap = t->addresses[i];
|
2007-08-21 20:38:34 +00:00
|
|
|
t->addresses[i] = t->addresses[j];
|
|
|
|
t->addresses[j] = swap;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
else if( ( req->response_code == HTTP_MOVEPERM )
|
|
|
|
|| ( req->response_code == HTTP_MOVETEMP ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
const char * loc = evhttp_find_header( req->input_headers, "Location" );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_tracker_info tmp;
|
2007-08-16 20:00:06 +00:00
|
|
|
if( tr_trackerInfoInit( &tmp, loc, -1 ) ) /* a bad redirect? */
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
moveToNextAddress = TRUE;
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
else if( req->response_code == HTTP_MOVEPERM )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_tracker_info * cur = &t->addresses[t->addressIndex];
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_trackerInfoClear( cur );
|
|
|
|
*cur = tmp;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
else if( req->response_code == HTTP_MOVETEMP )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
if( t->redirect == NULL )
|
2007-09-20 16:32:01 +00:00
|
|
|
t->redirect = tr_new0( tr_tracker_info, 1 );
|
2007-08-16 20:00:06 +00:00
|
|
|
else
|
|
|
|
tr_trackerInfoClear( t->redirect );
|
|
|
|
*t->redirect = tmp;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
else
|
|
|
|
{
|
2007-09-25 18:39:58 +00:00
|
|
|
ret = tr_strdup( "No response from tracker -- will keep trying." );
|
2007-08-16 20:00:06 +00:00
|
|
|
moveToNextAddress = TRUE;
|
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( moveToNextAddress )
|
|
|
|
if ( ++t->addressIndex >= t->addressCount )
|
|
|
|
t->addressIndex = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_tracker_info *
|
2007-08-16 20:00:06 +00:00
|
|
|
getCurrentAddress( const Tracker * t )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
assert( t->addresses != NULL );
|
|
|
|
assert( t->addressIndex >= 0 );
|
|
|
|
assert( t->addressIndex < t->addressCount );
|
2007-08-20 23:37:08 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
return &t->addresses[t->addressIndex];
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-08-20 23:37:08 +00:00
|
|
|
static int
|
|
|
|
trackerSupportsScrape( const Tracker * t )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_tracker_info * info = getCurrentAddress( t );
|
2007-08-20 23:37:08 +00:00
|
|
|
|
|
|
|
return ( info != NULL )
|
|
|
|
&& ( info->scrape != NULL )
|
|
|
|
&& ( info->scrape[0] != '\0' );
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static void
|
|
|
|
addCommonHeaders( const Tracker * t,
|
|
|
|
struct evhttp_request * req )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
char buf[1024];
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_tracker_info * address = getCurrentAddress( t );
|
2007-08-16 20:00:06 +00:00
|
|
|
snprintf( buf, sizeof(buf), "%s:%d", address->address, address->port );
|
|
|
|
evhttp_add_header( req->output_headers, "Host", buf );
|
2007-08-19 00:02:23 +00:00
|
|
|
evhttp_add_header( req->output_headers, "Connection", "close" );
|
|
|
|
evhttp_add_header( req->output_headers, "Content-Length", "0" );
|
2007-08-16 20:00:06 +00:00
|
|
|
evhttp_add_header( req->output_headers, "User-Agent",
|
|
|
|
TR_NAME "/" LONG_VERSION_STRING );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** SCRAPE
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static int
|
|
|
|
onTorrentScrapeNow( void * vtor )
|
|
|
|
{
|
|
|
|
Torrent * tor = (Torrent *) vtor;
|
2007-10-07 04:14:34 +00:00
|
|
|
if( trackerSupportsScrape( tor->tracker ) )
|
|
|
|
{
|
|
|
|
if( tr_ptrArrayFindSorted( tor->tracker->scrapeQueue, tor, torrentCompare) == NULL )
|
|
|
|
tr_ptrArrayInsertSorted( tor->tracker->scrapeQueue, tor, torrentCompare );
|
2007-08-20 23:37:08 +00:00
|
|
|
tr_trackerScrapeSoon( tor->tracker );
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
tor->scrapeTimer = NULL;
|
2007-08-16 20:00:06 +00:00
|
|
|
return FALSE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static void
|
2007-10-08 19:09:20 +00:00
|
|
|
onScrapeResponse( struct evhttp_request * req, void * primaryAddress )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-18 06:06:53 +00:00
|
|
|
char * errmsg;
|
2007-10-08 19:09:20 +00:00
|
|
|
Tracker * t;
|
2007-06-26 18:45:03 +00:00
|
|
|
|
2007-08-20 02:29:36 +00:00
|
|
|
tr_inf( "Got scrape response from '%s': %s",
|
2007-10-08 19:09:20 +00:00
|
|
|
primaryAddress,
|
2007-08-16 20:00:06 +00:00
|
|
|
(req ? req->response_code_line : "(null)") );
|
2007-01-20 05:07:00 +00:00
|
|
|
|
2007-10-08 19:09:20 +00:00
|
|
|
t = getExistingTracker( primaryAddress );
|
|
|
|
tr_free( primaryAddress );
|
|
|
|
if( t == NULL ) /* tracker has been closed... */
|
|
|
|
return;
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( req && ( req->response_code == HTTP_OK ) )
|
2007-01-20 05:07:00 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
int numResponses = 0;
|
|
|
|
benc_val_t benc, *files;
|
2007-08-20 02:29:36 +00:00
|
|
|
const int n_scraping = tr_ptrArraySize( t->scraping );
|
2007-08-16 20:00:06 +00:00
|
|
|
const int bencLoaded = !parseBencResponse( req, &benc );
|
|
|
|
|
|
|
|
if( bencLoaded
|
|
|
|
&& (( files = tr_bencDictFind( &benc, "files" ) ))
|
|
|
|
&& ( files->type == TYPE_DICT ) )
|
2007-01-20 05:07:00 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
int i;
|
|
|
|
for( i=0; i<files->val.l.count; i+=2 )
|
|
|
|
{
|
|
|
|
const uint8_t* hash =
|
|
|
|
(const uint8_t*) files->val.l.vals[i].val.s.s;
|
|
|
|
benc_val_t *tmp, *flags;
|
|
|
|
benc_val_t *tordict = &files->val.l.vals[i+1];
|
|
|
|
Torrent * tor = getExistingTorrent( t, hash );
|
|
|
|
++numResponses;
|
|
|
|
|
2007-10-07 16:25:02 +00:00
|
|
|
if( !tor )
|
2007-08-16 20:00:06 +00:00
|
|
|
continue;
|
2007-01-20 05:07:00 +00:00
|
|
|
|
2007-09-25 18:39:58 +00:00
|
|
|
publishErrorClear( tor );
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if(( tmp = tr_bencDictFind( tordict, "complete" )))
|
|
|
|
tor->seeders = tmp->val.i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if(( tmp = tr_bencDictFind( tordict, "incomplete" )))
|
|
|
|
tor->leechers = tmp->val.i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if(( tmp = tr_bencDictFind( tordict, "downloaded" )))
|
|
|
|
tor->timesDownloaded = tmp->val.i;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if(( flags = tr_bencDictFind( tordict, "flags" )))
|
|
|
|
if(( tmp = tr_bencDictFind( flags, "min_request_interval")))
|
|
|
|
t->scrapeIntervalMsec = tmp->val.i * 1000;
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_ptrArrayRemoveSorted( t->scraping, tor, torrentCompare );
|
|
|
|
|
2007-09-28 16:14:19 +00:00
|
|
|
tr_timerFree( &tor->scrapeTimer );
|
2007-09-20 16:32:01 +00:00
|
|
|
tor->scrapeTimer = tr_timerNew( t->handle, onTorrentScrapeNow, tor, t->scrapeIntervalMsec );
|
2007-08-20 02:29:36 +00:00
|
|
|
tr_dbg( "Torrent '%s' scrape successful."
|
|
|
|
" Rescraping in %d seconds",
|
2007-10-04 19:58:30 +00:00
|
|
|
tor->name, t->scrapeIntervalMsec/1000 );
|
2007-08-16 20:00:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !files->val.l.count )
|
|
|
|
{
|
|
|
|
/* got an empty files dictionary! This probably means the
|
|
|
|
torrents we're scraping have expired from the tracker,
|
|
|
|
so make sure they're stopped. It also means any previous
|
|
|
|
changes to multiscrapeMax are suspect, so reset that. */
|
|
|
|
|
|
|
|
int n;
|
|
|
|
Torrent ** torrents = (Torrent**)
|
|
|
|
tr_ptrArrayPeek( t->scraping, &n );
|
|
|
|
for( i=0; i<n; ++i )
|
|
|
|
tr_trackerStop( torrents[i] );
|
|
|
|
tr_ptrArrayClear( t->scraping );
|
|
|
|
|
|
|
|
t->multiscrapeMax = INT_MAX;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( bencLoaded )
|
|
|
|
tr_bencFree( &benc );
|
|
|
|
|
|
|
|
/* if the tracker gave us back fewer torrents than we
|
|
|
|
thought we should get, maybe our multiscrape string
|
|
|
|
is too big... limit it based on how many we got back */
|
|
|
|
if( ( 0 < numResponses ) && ( numResponses < n_scraping ) )
|
|
|
|
t->multiscrapeMax = numResponses;
|
2006-10-13 07:42:55 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-09-25 18:39:58 +00:00
|
|
|
if (( errmsg = updateAddresses( t, req ) ))
|
2007-08-18 06:06:53 +00:00
|
|
|
tr_err( errmsg );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
if( !tr_ptrArrayEmpty( t->scraping ) )
|
2006-10-13 07:42:55 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
int i, n;
|
|
|
|
Torrent ** torrents =
|
|
|
|
(Torrent**) tr_ptrArrayPeek( t->scraping, &n );
|
2007-09-25 18:39:58 +00:00
|
|
|
for( i=0; i<n; ++i ) {
|
|
|
|
if( errmsg != NULL )
|
|
|
|
publishErrorMessage( torrents[i], errmsg );
|
2007-08-16 20:00:06 +00:00
|
|
|
onTorrentScrapeNow( torrents[i] );
|
2007-09-25 18:39:58 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_ptrArrayClear( t->scraping );
|
2006-10-13 07:42:55 +00:00
|
|
|
}
|
2007-09-25 18:39:58 +00:00
|
|
|
tr_free( errmsg );
|
2006-10-13 07:42:55 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( !tr_ptrArrayEmpty( t->scrapeQueue ) )
|
|
|
|
tr_trackerScrapeSoon( t );
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static int
|
|
|
|
onTrackerScrapeNow( void * vt )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
Tracker * t = (Tracker*) vt;
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_tracker_info * address = getCurrentAddress( t );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-08-17 03:13:42 +00:00
|
|
|
assert( tr_ptrArrayEmpty( t->scraping ) );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-08-20 23:37:08 +00:00
|
|
|
if( trackerSupportsScrape( t ) && !tr_ptrArrayEmpty( t->scrapeQueue ) )
|
2007-08-16 20:00:06 +00:00
|
|
|
{
|
|
|
|
int i, n, len, addr_len, ask_n;
|
|
|
|
char *march, *uri;
|
|
|
|
Torrent ** torrents =
|
|
|
|
(Torrent**) tr_ptrArrayPeek( t->scrapeQueue, &n );
|
2007-10-07 16:07:19 +00:00
|
|
|
struct evhttp_connection * evcon;
|
|
|
|
struct evhttp_request *req;
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
ask_n = n;
|
|
|
|
if( ask_n > t->multiscrapeMax )
|
|
|
|
ask_n = t->multiscrapeMax;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*** Build the scrape request
|
|
|
|
**/
|
|
|
|
|
|
|
|
len = addr_len = strlen( address->scrape );
|
|
|
|
for( i=0; i<ask_n; ++i )
|
|
|
|
len += strlen("&info_hash=") + strlen(torrents[i]->escaped);
|
|
|
|
++len; /* for nul */
|
|
|
|
uri = march = tr_new( char, len );
|
|
|
|
memcpy( march, address->scrape, addr_len ); march += addr_len;
|
|
|
|
for( i=0; i<ask_n; ++i ) {
|
|
|
|
const int elen = strlen( torrents[i]->escaped );
|
|
|
|
*march++ = i?'&':'?';
|
|
|
|
memcpy( march, "info_hash=", 10); march += 10;
|
|
|
|
memcpy( march, torrents[i]->escaped, elen ); march += elen;
|
|
|
|
}
|
|
|
|
*march++ = '\0';
|
|
|
|
assert( march - uri == len );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/* move the first n_ask torrents from scrapeQueue to scraping */
|
|
|
|
for( i=0; i<ask_n; ++i )
|
|
|
|
tr_ptrArrayInsertSorted( t->scraping, torrents[i], torrentCompare );
|
|
|
|
tr_ptrArrayErase( t->scrapeQueue, 0, ask_n );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/* ping the tracker */
|
2007-08-20 02:29:36 +00:00
|
|
|
tr_inf( "Sending scrape to tracker %s:%d: %s",
|
|
|
|
address->address, address->port, uri );
|
2007-09-20 16:32:01 +00:00
|
|
|
evcon = getConnection( t, address->address, address->port );
|
2007-10-08 17:05:39 +00:00
|
|
|
evhttp_connection_set_timeout( evcon, TIMEOUT_INTERVAL_SEC );
|
2007-10-08 19:09:20 +00:00
|
|
|
req = evhttp_request_new( onScrapeResponse, tr_strdup(t->primaryAddress) );
|
2007-08-16 20:00:06 +00:00
|
|
|
assert( req );
|
|
|
|
addCommonHeaders( t, req );
|
2007-08-18 03:02:32 +00:00
|
|
|
tr_evhttp_make_request( t->handle, evcon, req, EVHTTP_REQ_GET, uri );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
t->scrapeTimer = NULL;
|
2007-08-16 20:00:06 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** TRACKER REQUESTS
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static int
|
2007-08-20 02:29:36 +00:00
|
|
|
torrentIsRunning( const Torrent * tor )
|
2007-08-16 20:00:06 +00:00
|
|
|
{
|
2007-10-04 20:31:19 +00:00
|
|
|
return tor && tor->isRunning;
|
2007-08-16 20:00:06 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static char*
|
2007-10-04 19:58:30 +00:00
|
|
|
buildTrackerRequestURI( const Torrent * tor,
|
|
|
|
const tr_torrent * torrent,
|
|
|
|
const char * eventName )
|
2007-08-16 20:00:06 +00:00
|
|
|
{
|
|
|
|
const int stopping = !strcmp( eventName, "stopped" );
|
|
|
|
const int numwant = stopping ? 0 : NUMWANT;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
snprintf( buf, sizeof(buf), "%s"
|
|
|
|
"?info_hash=%s"
|
|
|
|
"&peer_id=%s"
|
|
|
|
"&port=%d"
|
|
|
|
"&uploaded=%"PRIu64
|
|
|
|
"&downloaded=%"PRIu64
|
2007-08-21 15:17:02 +00:00
|
|
|
"&corrupt=%"PRIu64
|
2007-08-16 20:00:06 +00:00
|
|
|
"&left=%"PRIu64
|
|
|
|
"&compact=1"
|
|
|
|
"&numwant=%d"
|
|
|
|
"&key=%s"
|
|
|
|
"%s%s"
|
|
|
|
"%s%s",
|
|
|
|
getCurrentAddress(tor->tracker)->announce,
|
|
|
|
tor->escaped,
|
|
|
|
tor->peer_id,
|
2007-09-21 05:31:29 +00:00
|
|
|
tr_sharedGetPublicPort( torrent->handle->shared ),
|
2007-08-16 20:00:06 +00:00
|
|
|
torrent->uploadedCur,
|
|
|
|
torrent->downloadedCur,
|
2007-08-21 15:17:02 +00:00
|
|
|
torrent->corruptCur,
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_cpLeftUntilComplete( torrent->completion ),
|
|
|
|
numwant,
|
|
|
|
tor->tracker->key_param,
|
|
|
|
( ( eventName && *eventName ) ? "&event=" : "" ),
|
|
|
|
( ( eventName && *eventName ) ? eventName : "" ),
|
|
|
|
( ( tor->trackerID && *tor->trackerID ) ? "&trackerid=" : "" ),
|
|
|
|
( ( tor->trackerID && *tor->trackerID ) ? tor->trackerID : "" ) );
|
|
|
|
|
|
|
|
return tr_strdup( buf );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/* Convert to compact form */
|
|
|
|
static uint8_t *
|
|
|
|
parseOldPeers( benc_val_t * bePeers, int * peerCount )
|
|
|
|
{
|
|
|
|
int i, count;
|
|
|
|
uint8_t * compact;
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
assert( bePeers->type == TYPE_LIST );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
compact = tr_new( uint8_t, 6 * bePeers->val.l.count );
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
for( i=count=0; i<bePeers->val.l.count; ++i )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
struct in_addr addr;
|
|
|
|
tr_port_t port;
|
|
|
|
benc_val_t * val;
|
|
|
|
benc_val_t * peer = &bePeers->val.l.vals[i];
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
val = tr_bencDictFind( peer, "ip" );
|
|
|
|
if( !val || val->type!=TYPE_STR || tr_netResolve(val->val.s.s, &addr) )
|
|
|
|
continue;
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
memcpy( &compact[6 * count], &addr, 4 );
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
val = tr_bencDictFind( peer, "port" );
|
|
|
|
if( !val || val->type!=TYPE_INT || val->val.i<0 || val->val.i>0xffff )
|
|
|
|
continue;
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
port = htons( val->val.i );
|
|
|
|
memcpy( &compact[6 * count + 4], &port, 2 );
|
|
|
|
++count;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
*peerCount = count;
|
|
|
|
return compact;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/* handle braindead trackers whose minimums is higher
|
|
|
|
than the interval. */
|
|
|
|
static void
|
|
|
|
setAnnounceInterval( Tracker * t,
|
|
|
|
int minimum,
|
|
|
|
int interval )
|
|
|
|
{
|
|
|
|
assert( t != NULL );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( minimum > 0 )
|
|
|
|
t->minAnnounceIntervalMsec = minimum;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( interval > 0 )
|
2007-08-21 20:38:34 +00:00
|
|
|
t->announceIntervalMsec = interval;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( t->announceIntervalMsec < t->minAnnounceIntervalMsec )
|
|
|
|
t->announceIntervalMsec = t->minAnnounceIntervalMsec;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-20 02:29:36 +00:00
|
|
|
static int onReannounceNow( void * vtor );
|
|
|
|
|
2007-10-04 20:31:19 +00:00
|
|
|
struct response_user_data
|
|
|
|
{
|
|
|
|
tr_handle * handle;
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct response_user_data*
|
|
|
|
onTrackerResponseDataNew( Torrent * tor )
|
|
|
|
{
|
|
|
|
struct response_user_data * data = tr_new( struct response_user_data, 1 );
|
|
|
|
data->handle = tor->tracker->handle;
|
|
|
|
memcpy( data->hash, tor->hash, SHA_DIGEST_LENGTH );
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2007-10-07 16:07:19 +00:00
|
|
|
static void
|
|
|
|
onStoppedResponse( struct evhttp_request * req UNUSED, void * handle UNUSED )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static void
|
2007-10-04 20:31:19 +00:00
|
|
|
onTrackerResponse( struct evhttp_request * req, void * vdata )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-18 06:06:53 +00:00
|
|
|
char * errmsg;
|
2007-10-04 20:31:19 +00:00
|
|
|
Torrent * tor;
|
|
|
|
int isStopped;
|
2007-08-16 20:00:06 +00:00
|
|
|
int reannounceInterval;
|
2007-10-04 20:31:19 +00:00
|
|
|
struct response_user_data * data;
|
|
|
|
tr_torrent * t;
|
|
|
|
|
|
|
|
data = vdata;
|
|
|
|
t = tr_torrentFindFromHash( data->handle, data->hash );
|
|
|
|
tr_free( data );
|
|
|
|
if( t == NULL ) /* torrent has been closed */
|
|
|
|
return;
|
|
|
|
|
|
|
|
tor = t->tracker;
|
|
|
|
isStopped = !torrentIsRunning( tor );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-20 02:29:36 +00:00
|
|
|
tr_inf( "Torrent \"%s\" tracker response: %s",
|
2007-10-04 19:58:30 +00:00
|
|
|
tor->name,
|
2007-08-16 20:00:06 +00:00
|
|
|
( req ? req->response_code_line : "(null)") );
|
|
|
|
|
|
|
|
if( req && ( req->response_code == HTTP_OK ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
benc_val_t benc;
|
|
|
|
const int bencLoaded = !parseBencResponse( req, &benc );
|
|
|
|
|
2007-09-25 18:39:58 +00:00
|
|
|
publishErrorClear( tor );
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if( bencLoaded && benc.type==TYPE_DICT )
|
2007-02-27 04:00:38 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
benc_val_t * tmp;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if(( tmp = tr_bencDictFind( &benc, "failure reason" )))
|
|
|
|
publishErrorMessage( tor, tmp->val.s.s );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if(( tmp = tr_bencDictFind( &benc, "warning message" )))
|
|
|
|
publishWarningMessage( tor, tmp->val.s.s );
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
if(( tmp = tr_bencDictFind( &benc, "interval" )))
|
|
|
|
setAnnounceInterval( tor->tracker, -1, tmp->val.i * 1000 );
|
|
|
|
|
|
|
|
if(( tmp = tr_bencDictFind( &benc, "min interval" )))
|
|
|
|
setAnnounceInterval( tor->tracker, tmp->val.i * 1000, -1 );
|
|
|
|
|
|
|
|
if(( tmp = tr_bencDictFind( &benc, "tracker id" )))
|
|
|
|
tor->trackerID = tr_strndup( tmp->val.s.s, tmp->val.s.i );
|
|
|
|
|
|
|
|
if(( tmp = tr_bencDictFind( &benc, "complete" )))
|
|
|
|
tor->seeders = tmp->val.i;
|
|
|
|
|
|
|
|
if(( tmp = tr_bencDictFind( &benc, "incomplete" )))
|
|
|
|
tor->leechers = tmp->val.i;
|
|
|
|
|
|
|
|
if(( tmp = tr_bencDictFind( &benc, "peers" )))
|
|
|
|
{
|
|
|
|
int peerCount = 0;
|
|
|
|
uint8_t * peerCompact = NULL;
|
|
|
|
|
|
|
|
if( tmp->type == TYPE_LIST ) /* original protocol */
|
|
|
|
{
|
|
|
|
if( tmp->val.l.count > 0 )
|
|
|
|
peerCompact = parseOldPeers( tmp, &peerCount );
|
|
|
|
}
|
|
|
|
else if( tmp->type == TYPE_STR ) /* "compact" extension */
|
|
|
|
{
|
|
|
|
if( tmp->val.s.i >= 6 )
|
|
|
|
{
|
|
|
|
peerCount = tmp->val.s.i / 6;
|
|
|
|
peerCompact = tr_new( uint8_t, tmp->val.s.i );
|
|
|
|
memcpy( peerCompact, tmp->val.s.s, tmp->val.s.i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
publishNewPeers( tor, peerCount, peerCompact );
|
|
|
|
tr_free( peerCompact );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
reannounceInterval = isStopped
|
|
|
|
? -1
|
|
|
|
: tor->tracker->announceIntervalMsec;
|
2007-08-19 00:18:43 +00:00
|
|
|
|
|
|
|
if( bencLoaded )
|
|
|
|
tr_bencFree( &benc );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
else
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-18 08:28:57 +00:00
|
|
|
tr_inf( "Bad response from tracker '%s' on request '%s' "
|
2007-08-16 20:00:06 +00:00
|
|
|
"for torrent '%s'... trying again in 30 seconds",
|
|
|
|
tor->tracker->primaryAddress,
|
|
|
|
tor->lastRequest,
|
2007-10-04 19:58:30 +00:00
|
|
|
tor->name );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
reannounceInterval = 30 * 1000;
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-08-18 06:06:53 +00:00
|
|
|
if (( errmsg = updateAddresses( tor->tracker, req ) )) {
|
|
|
|
publishErrorMessage( tor, errmsg );
|
|
|
|
tr_err( errmsg );
|
|
|
|
tr_free( errmsg );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( !isStopped && reannounceInterval>0 ) {
|
2007-08-18 18:33:47 +00:00
|
|
|
tr_dbg( "torrent '%s' reannouncing in %d seconds",
|
2007-10-04 19:58:30 +00:00
|
|
|
tor->name, (reannounceInterval/1000) );
|
2007-09-29 15:05:20 +00:00
|
|
|
tr_timerFree( &tor->reannounceTimer );
|
2007-09-20 16:32:01 +00:00
|
|
|
tor->reannounceTimer = tr_timerNew( tor->tracker->handle, onReannounceNow, tor, reannounceInterval );
|
2007-08-16 20:00:06 +00:00
|
|
|
tor->manualAnnounceAllowedAt
|
|
|
|
= tr_date() + MANUAL_ANNOUNCE_INTERVAL_MSEC;
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static int
|
2007-10-04 19:58:30 +00:00
|
|
|
sendTrackerRequest( void * vt, const char * eventName )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-10-04 19:58:30 +00:00
|
|
|
Torrent * t = (Torrent *) vt;
|
|
|
|
const tr_tracker_info * address = getCurrentAddress( t->tracker );
|
|
|
|
char * uri;
|
2007-10-07 16:07:19 +00:00
|
|
|
struct evhttp_connection * evcon;
|
2007-10-04 19:58:30 +00:00
|
|
|
const tr_torrent * tor;
|
|
|
|
|
|
|
|
tor = tr_torrentFindFromHash( t->tracker->handle, t->hash );
|
|
|
|
if( tor == NULL )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
uri = buildTrackerRequestURI( t, tor, eventName );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-08-20 02:29:36 +00:00
|
|
|
tr_inf( "Torrent \"%s\" sending '%s' to tracker %s:%d: %s",
|
2007-10-04 19:58:30 +00:00
|
|
|
t->name,
|
2007-08-20 02:29:36 +00:00
|
|
|
(eventName ? eventName : "periodic announce"),
|
|
|
|
address->address, address->port,
|
|
|
|
uri );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
/* kill any pending requests */
|
2007-10-04 19:58:30 +00:00
|
|
|
tr_timerFree( &t->reannounceTimer );
|
2007-08-16 20:00:06 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
evcon = getConnection( t->tracker, address->address, address->port );
|
2007-08-18 23:32:07 +00:00
|
|
|
if ( !evcon ) {
|
2007-08-17 03:03:28 +00:00
|
|
|
tr_err( "Can't make a connection to %s:%d", address->address, address->port );
|
2007-08-18 23:32:07 +00:00
|
|
|
tr_free( uri );
|
|
|
|
} else {
|
2007-10-04 19:58:30 +00:00
|
|
|
struct evhttp_request * httpReq;
|
|
|
|
tr_free( t->lastRequest );
|
|
|
|
t->lastRequest = tr_strdup( eventName );
|
2007-10-08 17:05:39 +00:00
|
|
|
if( !eventName || !strcmp( eventName, "stopped" ) ) {
|
|
|
|
evhttp_connection_set_timeout( evcon, TIMEOUT_INTERVAL_SEC );
|
2007-10-07 16:07:19 +00:00
|
|
|
httpReq = evhttp_request_new( onTrackerResponse, onTrackerResponseDataNew(t) );
|
2007-10-08 17:05:39 +00:00
|
|
|
} else {
|
|
|
|
evhttp_connection_set_timeout( evcon, STOPPING_TIMEOUT_INTERVAL_SEC );
|
|
|
|
httpReq = evhttp_request_new( onStoppedResponse, t->tracker->handle );
|
|
|
|
}
|
2007-10-04 19:58:30 +00:00
|
|
|
addCommonHeaders( t->tracker, httpReq );
|
|
|
|
tr_evhttp_make_request( t->tracker->handle, evcon,
|
|
|
|
httpReq, EVHTTP_REQ_GET, uri );
|
2007-08-17 03:03:28 +00:00
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
static int
|
|
|
|
onReannounceNow( void * vtor )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-20 02:29:36 +00:00
|
|
|
Torrent * tor = (Torrent *) vtor;
|
|
|
|
sendTrackerRequest( tor, "" );
|
2007-09-20 16:32:01 +00:00
|
|
|
tor->reannounceTimer = NULL;
|
2007-08-16 20:00:06 +00:00
|
|
|
return FALSE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
/***
|
|
|
|
**** PUBLIC
|
|
|
|
***/
|
|
|
|
|
|
|
|
tr_publisher_tag
|
|
|
|
tr_trackerSubscribe( Torrent * tor,
|
|
|
|
tr_delivery_func func,
|
|
|
|
void * user_data )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
return tr_publisherSubscribe( tor->publisher, func, user_data );
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
void
|
|
|
|
tr_trackerUnsubscribe( Torrent * tor,
|
|
|
|
tr_publisher_tag tag )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_publisherUnsubscribe( tor->publisher, tag );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_tracker_info *
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_trackerGetAddress( const Torrent * tor )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
return getCurrentAddress( tor->tracker );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
int
|
|
|
|
tr_trackerCanManualAnnounce ( const Torrent * tor )
|
2007-07-12 20:48:13 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
/* return true if this torrent's currently running
|
|
|
|
and it's been long enough since the last announce */
|
2007-08-20 02:29:36 +00:00
|
|
|
return ( torrentIsRunning( tor ) )
|
2007-08-16 20:00:06 +00:00
|
|
|
&& ( tr_date() >= tor->manualAnnounceAllowedAt );
|
2007-07-12 20:48:13 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
void
|
|
|
|
tr_trackerGetCounts( const Torrent * tor,
|
|
|
|
int * setme_completedCount,
|
|
|
|
int * setme_leecherCount,
|
|
|
|
int * setme_seederCount )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
2007-08-20 02:29:36 +00:00
|
|
|
if( setme_completedCount )
|
2007-08-16 20:00:06 +00:00
|
|
|
*setme_completedCount = tor->timesDownloaded;
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-20 02:29:36 +00:00
|
|
|
if( setme_leecherCount )
|
2007-08-16 20:00:06 +00:00
|
|
|
*setme_leecherCount = tor->leechers;
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-08-20 02:29:36 +00:00
|
|
|
if( setme_seederCount )
|
2007-08-16 20:00:06 +00:00
|
|
|
*setme_seederCount = tor->seeders;
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
void
|
|
|
|
tr_trackerStart( Torrent * tor )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
tr_peerIdNew( tor->peer_id, sizeof(tor->peer_id) );
|
2007-08-17 03:13:42 +00:00
|
|
|
|
2007-10-04 19:58:30 +00:00
|
|
|
if( !tor->reannounceTimer && !tor->isRunning )
|
|
|
|
{
|
|
|
|
tor->isRunning = 1;
|
2007-08-17 03:13:42 +00:00
|
|
|
sendTrackerRequest( tor, "started" );
|
2007-10-04 19:58:30 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
2007-01-27 21:17:10 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
void
|
|
|
|
tr_trackerReannounce( Torrent * tor )
|
2007-01-27 21:17:10 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
sendTrackerRequest( tor, "started" );
|
2007-01-27 21:17:10 +00:00
|
|
|
}
|
2007-03-30 00:12:39 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
void
|
|
|
|
tr_trackerCompleted( Torrent * tor )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2007-08-16 20:00:06 +00:00
|
|
|
sendTrackerRequest( tor, "completed" );
|
|
|
|
}
|
2007-03-30 00:12:39 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
void
|
|
|
|
tr_trackerStop( Torrent * tor )
|
|
|
|
{
|
2007-10-04 19:58:30 +00:00
|
|
|
if( tor->isRunning )
|
|
|
|
{
|
|
|
|
tor->isRunning = 0;
|
|
|
|
sendTrackerRequest( tor, "stopped" );
|
|
|
|
}
|
2007-08-16 20:00:06 +00:00
|
|
|
}
|
2007-03-30 00:12:39 +00:00
|
|
|
|
2007-08-16 20:00:06 +00:00
|
|
|
void
|
|
|
|
tr_trackerChangeMyPort( Torrent * tor )
|
|
|
|
{
|
|
|
|
if( torrentIsRunning( tor ) )
|
|
|
|
tr_trackerReannounce( tor );
|
2007-03-30 00:12:39 +00:00
|
|
|
}
|