2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2007-01-27 21:17:10 +00:00
|
|
|
#include "shared.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
struct tclist
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
tr_tracker_info_t * tl_inf;
|
|
|
|
int tl_badscrape;
|
|
|
|
SLIST_ENTRY( tclist ) next;
|
2006-12-17 16:36:27 +00:00
|
|
|
};
|
2007-04-08 05:41:44 +00:00
|
|
|
SLIST_HEAD( tchead, tclist );
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
struct tr_tracker_s
|
|
|
|
{
|
|
|
|
tr_torrent_t * tor;
|
|
|
|
|
|
|
|
char * id;
|
2006-12-02 01:46:54 +00:00
|
|
|
char * trackerid;
|
2007-02-27 04:00:38 +00:00
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
struct tchead * tiers;
|
|
|
|
size_t tierCount;
|
|
|
|
size_t tierIndex;
|
|
|
|
struct tclist * tcCur;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
#define TC_CHANGE_NO 0
|
|
|
|
#define TC_CHANGE_NEXT 1
|
|
|
|
#define TC_CHANGE_NONEXT 2
|
|
|
|
#define TC_CHANGE_REDIRECT 4
|
|
|
|
int shouldChangeAnnounce;
|
|
|
|
|
|
|
|
char * redirectAddress;
|
|
|
|
int redirectAddressLen;
|
|
|
|
char * redirectScrapeAddress;
|
|
|
|
int redirectScrapeAddressLen;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
char started;
|
|
|
|
char completed;
|
|
|
|
char stopped;
|
|
|
|
|
|
|
|
int interval;
|
2006-12-02 01:46:54 +00:00
|
|
|
int minInterval;
|
|
|
|
int scrapeInterval;
|
2006-07-16 19:39:23 +00:00
|
|
|
int seeders;
|
|
|
|
int leechers;
|
|
|
|
int hasManyPeers;
|
2006-12-02 01:46:54 +00:00
|
|
|
int complete;
|
2006-12-17 16:36:27 +00:00
|
|
|
int randOffset;
|
|
|
|
|
|
|
|
int completelyUnconnectable;
|
2006-12-23 21:18:58 +00:00
|
|
|
int allUnreachIfError;
|
|
|
|
int lastError;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
uint64_t dateTry;
|
|
|
|
uint64_t dateOk;
|
2006-12-02 01:46:54 +00:00
|
|
|
uint64_t dateScrape;
|
|
|
|
int lastScrapeFailed;
|
|
|
|
int scrapeNeeded;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
tr_http_t * http;
|
2006-12-02 01:46:54 +00:00
|
|
|
tr_http_t * httpScrape;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-27 21:17:10 +00:00
|
|
|
int publicPort;
|
2006-07-16 19:39:23 +00:00
|
|
|
};
|
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
static void setAnnounce ( tr_tracker_t * tc, struct tclist * new );
|
2006-12-17 16:36:27 +00:00
|
|
|
static void failureAnnouncing( tr_tracker_t * tc );
|
2006-12-02 01:46:54 +00:00
|
|
|
static tr_http_t * getQuery ( tr_tracker_t * tc );
|
|
|
|
static tr_http_t * getScrapeQuery ( tr_tracker_t * tc );
|
2007-01-19 11:19:20 +00:00
|
|
|
static void readAnswer ( tr_tracker_t * tc, const char *, int,
|
|
|
|
int * peerCount, uint8_t ** peerCompact );
|
2006-12-02 01:46:54 +00:00
|
|
|
static void readScrapeAnswer ( tr_tracker_t * tc, const char *, int );
|
2007-01-21 07:16:18 +00:00
|
|
|
static void killHttp ( tr_http_t ** http );
|
2007-01-27 21:17:10 +00:00
|
|
|
static int shouldChangePort( tr_tracker_t * tc );
|
2007-03-30 00:12:39 +00:00
|
|
|
static uint8_t * parseOriginalPeers( benc_val_t * bePeers, int * peerCount );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
tr_tracker_t * tr_trackerInit( tr_torrent_t * tor )
|
|
|
|
{
|
2006-12-17 16:36:27 +00:00
|
|
|
tr_info_t * inf = &tor->info;
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_tracker_t * tc;
|
2007-04-08 05:41:44 +00:00
|
|
|
struct tclist * item;
|
|
|
|
size_t ii, jj;
|
|
|
|
|
|
|
|
tc = calloc( 1, sizeof *tc );
|
|
|
|
if( NULL == tc )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
tc->tor = tor;
|
|
|
|
tc->id = tor->id;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
tc->started = 1;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
tc->shouldChangeAnnounce = TC_CHANGE_NO;
|
|
|
|
tc->redirectAddress = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
tc->interval = 300;
|
2007-01-17 01:06:52 +00:00
|
|
|
tc->scrapeInterval = 1200;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-28 01:23:21 +00:00
|
|
|
tc->lastError = 0;
|
2006-12-23 21:18:58 +00:00
|
|
|
tc->allUnreachIfError = 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-27 21:17:10 +00:00
|
|
|
tc->publicPort = tor->publicPort;
|
2007-04-08 05:41:44 +00:00
|
|
|
|
|
|
|
assert( 0 <= inf->trackerTiers );
|
|
|
|
assert( sizeof( struct tchead ) == sizeof *tc->tiers );
|
|
|
|
tc->tiers = calloc( inf->trackerTiers, sizeof *tc->tiers );
|
|
|
|
tc->tierCount = inf->trackerTiers;
|
|
|
|
for( ii = 0; tc->tierCount > ii; ii++ )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
assert( 0 <= inf->trackerList[ii].count );
|
|
|
|
SLIST_INIT( &tc->tiers[ii] );
|
|
|
|
for( jj = inf->trackerList[ii].count; 0 < jj; jj-- )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
item = calloc( 1, sizeof *item );
|
|
|
|
if( NULL == item )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
tr_trackerClose( tc );
|
|
|
|
return NULL;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2007-04-08 05:41:44 +00:00
|
|
|
item->tl_inf = &inf->trackerList[ii].list[jj-1];
|
|
|
|
SLIST_INSERT_HEAD( &tc->tiers[ii], item, next );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-08 05:41:44 +00:00
|
|
|
|
|
|
|
setAnnounce( tc, SLIST_FIRST( &tc->tiers[0] ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
return tc;
|
|
|
|
}
|
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
static void setAnnounce( tr_tracker_t * tc, struct tclist * new )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
tc->tcCur = new;
|
2007-02-27 04:00:38 +00:00
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
/* Needs a new scrape */
|
|
|
|
tc->seeders = -1;
|
|
|
|
tc->leechers = -1;
|
|
|
|
tc->complete = -1;
|
|
|
|
tc->dateScrape = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void failureAnnouncing( tr_tracker_t * tc )
|
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
if( NULL != SLIST_NEXT( tc->tcCur, next ) ||
|
|
|
|
tc->tierIndex + 1 < tc->tierCount )
|
|
|
|
{
|
|
|
|
tc->shouldChangeAnnounce = TC_CHANGE_NEXT;
|
|
|
|
}
|
|
|
|
else
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
tc->shouldChangeAnnounce = TC_CHANGE_NONEXT;
|
2006-12-17 16:36:27 +00:00
|
|
|
tc->completelyUnconnectable = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
static int shouldConnect( tr_tracker_t * tc )
|
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
tr_torrent_t * tor = tc->tor;
|
|
|
|
uint64_t now;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
/* Last tracker failed, try next */
|
|
|
|
if( tc->shouldChangeAnnounce == TC_CHANGE_NEXT
|
|
|
|
|| tc->shouldChangeAnnounce == TC_CHANGE_REDIRECT )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
now = tr_date();
|
2006-12-23 21:18:58 +00:00
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
/* If last attempt was an error and it did not change trackers,
|
|
|
|
then all must have been errors */
|
2006-12-23 21:18:58 +00:00
|
|
|
if( tc->lastError )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
/* Unreachable trackers, wait 10 seconds + random value before
|
|
|
|
trying again */
|
2006-12-23 21:18:58 +00:00
|
|
|
if( tc->allUnreachIfError )
|
|
|
|
{
|
|
|
|
if( now < tc->dateTry + tc->randOffset + 10000 )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-04-08 05:41:44 +00:00
|
|
|
/* The tracker rejected us (like 4XX code, unauthorized
|
|
|
|
IP...), don't hammer it - we'll probably get the same
|
|
|
|
answer next time anyway */
|
2006-12-23 21:18:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if( now < tc->dateTry + 1000 * tc->interval + tc->randOffset )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
/* since starting at the top of the list, reset if any
|
|
|
|
were reached previously */
|
|
|
|
tc->allUnreachIfError = 1;
|
2006-12-23 21:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we need to send an event? */
|
2007-01-27 21:17:10 +00:00
|
|
|
if( tc->started || tc->completed || tc->stopped || shouldChangePort( tc ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should we try and get more peers? */
|
2006-12-17 16:36:27 +00:00
|
|
|
if( now > tc->dateOk + 1000 * tc->interval + tc->randOffset )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there is quite a lot of people on this torrent, stress
|
|
|
|
the tracker a bit until we get a decent number of peers */
|
2006-12-02 01:46:54 +00:00
|
|
|
if( tc->hasManyPeers && !tr_cpIsSeeding( tor->completion ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
/* reannounce in 10 seconds if we have less than 5 peers */
|
|
|
|
if( tor->peerCount < 5 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
if( now > tc->dateOk + 1000 * MAX( 10, tc->minInterval ) )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
/* reannounce in 20 seconds if we have less than 10 peers */
|
|
|
|
else if( tor->peerCount < 10 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
if( now > tc->dateOk + 1000 * MAX( 20, tc->minInterval ) )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
/* reannounce in 30 seconds if we have less than 15 peers */
|
|
|
|
else if( tor->peerCount < 15 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
if( now > tc->dateOk + 1000 * MAX( 30, tc->minInterval ) )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
static int shouldScrape( tr_tracker_t * tc )
|
|
|
|
{
|
|
|
|
uint64_t now, interval;
|
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
/* in process of changing tracker or scrape not supported */
|
2007-02-27 04:00:38 +00:00
|
|
|
if( tc->shouldChangeAnnounce != TC_CHANGE_NO ||
|
2007-04-08 05:41:44 +00:00
|
|
|
NULL == tc->tcCur->tl_inf->scrape || tc->tcCur->tl_badscrape ||
|
|
|
|
tc->stopped )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
now = tr_date();
|
2007-01-17 01:06:52 +00:00
|
|
|
interval = 1000 * MAX( tc->scrapeInterval, 600 );
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-01-17 01:06:52 +00:00
|
|
|
/* scrape more often if needed */
|
|
|
|
if( tc->scrapeNeeded || tc->lastScrapeFailed )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
2007-01-17 01:06:52 +00:00
|
|
|
interval /= 2;
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return now > tc->dateScrape + interval;
|
|
|
|
}
|
|
|
|
|
2007-01-19 11:19:20 +00:00
|
|
|
void tr_trackerAnnouncePulse( tr_tracker_t * tc, int * peerCount,
|
|
|
|
uint8_t ** peerCompact, int manual )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
const char * data;
|
2006-12-17 16:36:27 +00:00
|
|
|
char * address, * announce;
|
2007-04-08 05:41:44 +00:00
|
|
|
int len, port;
|
|
|
|
struct tclist * next;
|
|
|
|
struct tchead * tier;
|
2007-01-19 11:19:20 +00:00
|
|
|
|
|
|
|
*peerCount = 0;
|
|
|
|
*peerCompact = NULL;
|
2006-12-26 04:17:37 +00:00
|
|
|
|
|
|
|
if( ( NULL == tc->http ) && ( manual || shouldConnect( tc ) ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
/* if announcing manually, don't consider not reaching a
|
|
|
|
tracker an error */
|
2006-12-26 04:17:37 +00:00
|
|
|
if( manual )
|
|
|
|
{
|
|
|
|
tc->allUnreachIfError = 0;
|
|
|
|
}
|
2006-12-17 16:36:27 +00:00
|
|
|
tc->completelyUnconnectable = 0;
|
2006-12-26 04:17:37 +00:00
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
tc->randOffset = tr_rand( 60000 );
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
tc->dateTry = tr_date();
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
/* Use redirected address */
|
|
|
|
if( tc->shouldChangeAnnounce == TC_CHANGE_REDIRECT )
|
|
|
|
{
|
|
|
|
if( !tr_httpParseUrl( tc->redirectAddress, tc->redirectAddressLen,
|
|
|
|
&address, &port, &announce ) )
|
|
|
|
{
|
|
|
|
tr_err( "Tracker: redirected URL: %s:%d", address, port );
|
2007-04-08 05:41:44 +00:00
|
|
|
tc->http = tr_httpClient( TR_HTTP_GET, address, port,
|
|
|
|
"%s", announce );
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
free( address );
|
|
|
|
free( announce );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( tc->redirectAddress );
|
|
|
|
tc->redirectAddress = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Need to change to next address in list */
|
|
|
|
if( tc->shouldChangeAnnounce == TC_CHANGE_NEXT )
|
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
tr_inf( "Tracker: failed to connect to %s:%i, trying next",
|
|
|
|
tc->tcCur->tl_inf->address, tc->tcCur->tl_inf->port );
|
|
|
|
next = SLIST_NEXT( tc->tcCur, next );
|
|
|
|
if( NULL == next )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
assert( tc->tierCount > tc->tierIndex + 1 );
|
|
|
|
tc->tierIndex++;
|
|
|
|
next = SLIST_FIRST( &tc->tiers[tc->tierIndex] );
|
|
|
|
/* XXX will there always be at least one tracker
|
|
|
|
in a tier? */
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
tr_inf( "Tracker: switching to tracker http://%s:%i%s",
|
|
|
|
next->tl_inf->address, next->tl_inf->port,
|
|
|
|
next->tl_inf->announce );
|
|
|
|
setAnnounce( tc, next );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
/* Need to change to first in list */
|
2007-04-08 05:41:44 +00:00
|
|
|
else if( SLIST_FIRST( &tc->tiers[0] ) != tc->tcCur )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
tier = &tc->tiers[tc->tierIndex];
|
|
|
|
/* Check if the last announce was successful and
|
|
|
|
wasn't the first in the sublist */
|
|
|
|
if( tc->shouldChangeAnnounce == TC_CHANGE_NO &&
|
|
|
|
SLIST_FIRST( tier ) != tc->tcCur )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
SLIST_REMOVE( tier, tc->tcCur, tclist, next );
|
|
|
|
SLIST_INSERT_HEAD( tier, tc->tcCur, next );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
setAnnounce( tc, SLIST_FIRST( tier ) );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2007-04-08 05:41:44 +00:00
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
tc->http = getQuery( tc );
|
|
|
|
|
|
|
|
tr_inf( "Tracker: connecting to %s:%d (%s)",
|
2007-04-08 05:41:44 +00:00
|
|
|
tc->tcCur->tl_inf->address, tc->tcCur->tl_inf->port,
|
2006-12-17 16:36:27 +00:00
|
|
|
tc->started ? "sending 'started'" :
|
|
|
|
( tc->completed ? "sending 'completed'" :
|
|
|
|
( tc->stopped ? "sending 'stopped'" :
|
2007-04-08 05:41:44 +00:00
|
|
|
( shouldChangePort( tc ) ?
|
|
|
|
"sending 'stopped' to change port" :
|
2006-12-17 16:36:27 +00:00
|
|
|
"getting peers" ) ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
tc->shouldChangeAnnounce = TC_CHANGE_NO;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
if( NULL != tc->http )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
switch( tr_httpPulse( tc->http, &data, &len ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_WAIT:
|
2006-12-02 01:46:54 +00:00
|
|
|
break;
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_ERROR:
|
2007-01-21 07:16:18 +00:00
|
|
|
killHttp( &tc->http );
|
2006-09-25 18:37:45 +00:00
|
|
|
tc->dateTry = tr_date();
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
failureAnnouncing( tc );
|
2006-12-23 21:18:58 +00:00
|
|
|
|
|
|
|
tc->lastError = 1;
|
2006-12-02 01:46:54 +00:00
|
|
|
break;
|
2006-09-25 18:37:45 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_OK:
|
2007-01-19 11:19:20 +00:00
|
|
|
readAnswer( tc, data, len, peerCount, peerCompact );
|
2007-01-21 07:16:18 +00:00
|
|
|
killHttp( &tc->http );
|
2006-12-02 01:46:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( NULL == tc->httpScrape ) && shouldScrape( tc ) )
|
|
|
|
{
|
|
|
|
tc->dateScrape = tr_date();
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
if ( tc->redirectScrapeAddress != NULL )
|
|
|
|
{
|
|
|
|
/* Use redirected address */
|
2007-04-08 05:41:44 +00:00
|
|
|
if( !tr_httpParseUrl( tc->redirectScrapeAddress,
|
|
|
|
tc->redirectScrapeAddressLen,
|
|
|
|
&address, &port, &announce ) )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
|
|
|
tr_err( "Scrape: redirected URL: %s:%d", address, port );
|
2007-04-08 05:41:44 +00:00
|
|
|
tc->httpScrape = tr_httpClient( TR_HTTP_GET, address, port,
|
|
|
|
"%s", announce );
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
free( address );
|
|
|
|
free( announce );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( tc->redirectScrapeAddress );
|
|
|
|
tc->redirectScrapeAddress = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tc->httpScrape = getScrapeQuery( tc );
|
2007-04-08 05:41:44 +00:00
|
|
|
tr_inf( "Scrape: sent HTTP request for http://%s:%d%s",
|
|
|
|
tc->tcCur->tl_inf->address,
|
|
|
|
tc->tcCur->tl_inf->port,
|
|
|
|
tc->tcCur->tl_inf->scrape );
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( NULL != tc->httpScrape )
|
|
|
|
{
|
|
|
|
switch( tr_httpPulse( tc->httpScrape, &data, &len ) )
|
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_WAIT:
|
2006-12-02 01:46:54 +00:00
|
|
|
break;
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_ERROR:
|
2007-01-21 07:16:18 +00:00
|
|
|
killHttp( &tc->httpScrape );
|
2006-12-02 01:46:54 +00:00
|
|
|
tc->lastScrapeFailed = 1;
|
|
|
|
break;
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_OK:
|
2006-12-02 01:46:54 +00:00
|
|
|
readScrapeAnswer( tc, data, len );
|
2007-01-21 07:16:18 +00:00
|
|
|
killHttp( &tc->httpScrape );
|
2006-09-25 18:37:45 +00:00
|
|
|
break;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_trackerCompleted( tr_tracker_t * tc )
|
|
|
|
{
|
|
|
|
tc->started = 0;
|
|
|
|
tc->completed = 1;
|
|
|
|
tc->stopped = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tr_trackerStopped( tr_tracker_t * tc )
|
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
/* If we are already sending a query at the moment, we need to
|
|
|
|
reconnect */
|
2007-01-21 07:16:18 +00:00
|
|
|
killHttp( &tc->http );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
tc->started = 0;
|
|
|
|
tc->completed = 0;
|
|
|
|
tc->stopped = 1;
|
|
|
|
|
|
|
|
/* Even if we have connected recently, reconnect right now */
|
2006-09-25 18:37:45 +00:00
|
|
|
tc->dateTry = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_trackerClose( tr_tracker_t * tc )
|
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
size_t ii;
|
|
|
|
struct tclist * dead;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
killHttp( &tc->http );
|
|
|
|
killHttp( &tc->httpScrape );
|
2007-01-20 05:07:00 +00:00
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
for( ii = 0; tc->tierCount > ii; ii++ )
|
2007-01-20 05:07:00 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
while( !SLIST_EMPTY( &tc->tiers[ii] ) )
|
2007-01-20 05:07:00 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
dead = SLIST_FIRST( &tc->tiers[ii] );
|
|
|
|
SLIST_REMOVE_HEAD( &tc->tiers[ii], next );
|
|
|
|
free( dead );
|
2007-01-20 05:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-08 05:41:44 +00:00
|
|
|
free( tc->tiers );
|
2007-01-20 05:07:00 +00:00
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
free( tc->trackerid );
|
2006-07-16 19:39:23 +00:00
|
|
|
free( tc );
|
|
|
|
}
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
static tr_http_t * getQuery( tr_tracker_t * tc )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tr_torrent_t * tor = tc->tor;
|
2007-04-08 05:41:44 +00:00
|
|
|
tr_tracker_info_t * tcInf = tc->tcCur->tl_inf;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
char * event, * trackerid, * idparam;
|
2006-09-25 18:37:45 +00:00
|
|
|
uint64_t left;
|
|
|
|
uint64_t down;
|
|
|
|
uint64_t up;
|
2006-12-02 01:46:54 +00:00
|
|
|
char start;
|
2006-11-09 04:38:32 +00:00
|
|
|
int numwant = 50;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-09-26 22:36:04 +00:00
|
|
|
down = tor->downloadedCur;
|
2006-12-02 01:46:54 +00:00
|
|
|
up = tor->uploadedCur;
|
2006-07-16 19:39:23 +00:00
|
|
|
if( tc->started )
|
|
|
|
{
|
|
|
|
event = "&event=started";
|
2006-12-02 01:46:54 +00:00
|
|
|
down = 0;
|
|
|
|
up = 0;
|
|
|
|
|
2007-01-27 21:17:10 +00:00
|
|
|
if( shouldChangePort( tc ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-27 21:17:10 +00:00
|
|
|
tc->publicPort = tor->publicPort;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( tc->completed )
|
|
|
|
{
|
|
|
|
event = "&event=completed";
|
|
|
|
}
|
2007-01-27 21:17:10 +00:00
|
|
|
else if( tc->stopped || shouldChangePort( tc ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
event = "&event=stopped";
|
2006-11-09 04:38:32 +00:00
|
|
|
numwant = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
event = "";
|
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
if( NULL == tc->trackerid )
|
2006-10-13 07:42:55 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
trackerid = "";
|
|
|
|
idparam = "";
|
2006-10-13 07:42:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
trackerid = tc->trackerid;
|
|
|
|
idparam = "&trackerid=";
|
2006-10-13 07:42:55 +00:00
|
|
|
}
|
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
start = ( strchr( tcInf->announce, '?' ) ? '&' : '?' );
|
2006-12-02 01:46:54 +00:00
|
|
|
left = tr_cpLeftBytes( tor->completion );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
return tr_httpClient( TR_HTTP_GET, tcInf->address, tcInf->port,
|
2006-12-02 01:46:54 +00:00
|
|
|
"%s%c"
|
2006-09-25 18:37:45 +00:00
|
|
|
"info_hash=%s&"
|
|
|
|
"peer_id=%s&"
|
|
|
|
"port=%d&"
|
|
|
|
"uploaded=%"PRIu64"&"
|
|
|
|
"downloaded=%"PRIu64"&"
|
|
|
|
"left=%"PRIu64"&"
|
|
|
|
"compact=1&"
|
2006-11-09 04:38:32 +00:00
|
|
|
"numwant=%d&"
|
2006-09-25 18:37:45 +00:00
|
|
|
"key=%s"
|
2006-12-02 01:46:54 +00:00
|
|
|
"%s%s"
|
2006-09-27 22:23:42 +00:00
|
|
|
"%s",
|
2007-04-08 05:41:44 +00:00
|
|
|
tcInf->announce, start, tor->escapedHashString,
|
2007-01-27 21:17:10 +00:00
|
|
|
tc->id, tc->publicPort, up, down, left, numwant,
|
2006-12-02 01:46:54 +00:00
|
|
|
tor->key, idparam, trackerid, event );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_http_t * getScrapeQuery( tr_tracker_t * tc )
|
|
|
|
{
|
|
|
|
tr_torrent_t * tor = tc->tor;
|
2007-04-08 05:41:44 +00:00
|
|
|
tr_tracker_info_t * tcInf = tc->tcCur->tl_inf;
|
2006-12-02 01:46:54 +00:00
|
|
|
char start;
|
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
start = ( strchr( tcInf->scrape, '?' ) ? '&' : '?' );
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
return tr_httpClient( TR_HTTP_GET, tcInf->address, tcInf->port,
|
2006-12-02 01:46:54 +00:00
|
|
|
"%s%c"
|
|
|
|
"info_hash=%s",
|
2007-04-08 05:41:44 +00:00
|
|
|
tcInf->scrape, start, tor->escapedHashString );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-19 11:19:20 +00:00
|
|
|
static void readAnswer( tr_tracker_t * tc, const char * data, int len,
|
|
|
|
int * _peerCount, uint8_t ** _peerCompact )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tr_torrent_t * tor = tc->tor;
|
|
|
|
int i;
|
2006-09-25 18:37:45 +00:00
|
|
|
int code;
|
2006-07-16 19:39:23 +00:00
|
|
|
benc_val_t beAll;
|
|
|
|
benc_val_t * bePeers, * beFoo;
|
2006-09-25 18:37:45 +00:00
|
|
|
const uint8_t * body;
|
2006-12-02 01:46:54 +00:00
|
|
|
int bodylen, shouldfree, scrapeNeeded;
|
2006-12-26 18:43:46 +00:00
|
|
|
char * address;
|
2007-01-19 11:19:20 +00:00
|
|
|
int peerCount;
|
|
|
|
uint8_t * peerCompact;
|
|
|
|
|
|
|
|
*_peerCount = peerCount = 0;
|
|
|
|
*_peerCompact = peerCompact = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
tc->dateTry = tr_date();
|
2006-09-25 18:37:45 +00:00
|
|
|
code = tr_httpResponseCode( data, len );
|
2006-12-17 16:36:27 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
if( 0 > code )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
/* We don't have a valid HTTP status line */
|
|
|
|
tr_inf( "Tracker: invalid HTTP status line" );
|
2006-12-23 21:18:58 +00:00
|
|
|
tc->lastError = 1;
|
2006-12-17 16:36:27 +00:00
|
|
|
failureAnnouncing( tc );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( code == 301 || code == 302 )
|
|
|
|
{
|
|
|
|
tr_http_header_t hdr[] = { { "Location", NULL, 0 }, { NULL, NULL, 0 } };
|
2007-01-14 08:51:15 +00:00
|
|
|
|
|
|
|
tr_err( "Tracker: HTTP status code: %i", code );
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
tr_httpParse( data, len, hdr );
|
|
|
|
|
2006-12-26 18:43:46 +00:00
|
|
|
address = calloc( sizeof( char ), hdr->len+1 );
|
2006-12-17 16:36:27 +00:00
|
|
|
snprintf( address, hdr->len+1, "%s", hdr->data );
|
|
|
|
|
|
|
|
tc->shouldChangeAnnounce = TC_CHANGE_REDIRECT;
|
|
|
|
tc->redirectAddress = address;
|
|
|
|
tc->redirectAddressLen = hdr->len;
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
if( !TR_HTTP_STATUS_OK( code ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
/* we didn't get a 2xx status code */
|
2006-09-25 18:37:45 +00:00
|
|
|
tr_err( "Tracker: invalid HTTP status code: %i", code );
|
2006-12-23 21:18:58 +00:00
|
|
|
tc->lastError = 1;
|
|
|
|
tc->allUnreachIfError = 0;
|
2006-12-17 16:36:27 +00:00
|
|
|
failureAnnouncing( tc );
|
2006-07-16 19:39:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the end of the http headers */
|
2006-09-25 18:37:45 +00:00
|
|
|
body = (uint8_t *) tr_httpParse( data, len, NULL );
|
|
|
|
if( NULL == body )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tr_err( "Tracker: could not find end of HTTP headers" );
|
2006-12-23 21:18:58 +00:00
|
|
|
tc->lastError = 1;
|
2006-12-17 16:36:27 +00:00
|
|
|
failureAnnouncing( tc );
|
2006-07-16 19:39:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
bodylen = len - ( body - (const uint8_t*)data );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Find and load the dictionary */
|
|
|
|
shouldfree = 0;
|
|
|
|
for( i = 0; i < bodylen; i++ )
|
|
|
|
{
|
|
|
|
if( !tr_bencLoad( &body[i], bodylen - i, &beAll, NULL ) )
|
|
|
|
{
|
|
|
|
shouldfree = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i >= bodylen )
|
|
|
|
{
|
2007-01-27 21:17:10 +00:00
|
|
|
if( tc->stopped || shouldChangePort( tc ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-23 21:18:58 +00:00
|
|
|
tc->lastError = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
goto nodict;
|
|
|
|
}
|
|
|
|
tr_err( "Tracker: no valid dictionary found in answer" );
|
2006-12-23 21:18:58 +00:00
|
|
|
tc->lastError = 1;
|
|
|
|
tc->allUnreachIfError = 0;
|
2006-12-17 16:36:27 +00:00
|
|
|
failureAnnouncing( tc );
|
2006-07-16 19:39:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
/* tr_bencPrint( &beAll ); */
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
if( ( bePeers = tr_bencDictFind( &beAll, "failure reason" ) ) )
|
|
|
|
{
|
2006-12-17 16:36:27 +00:00
|
|
|
tr_err( "Tracker: Error - %s", bePeers->val.s.s );
|
2007-01-14 12:00:21 +00:00
|
|
|
tor->error = TR_ERROR_TC_ERROR;
|
|
|
|
snprintf( tor->errorString, sizeof( tor->errorString ),
|
2006-07-16 19:39:23 +00:00
|
|
|
"%s", bePeers->val.s.s );
|
2006-12-23 21:18:58 +00:00
|
|
|
tc->lastError = 1;
|
|
|
|
tc->allUnreachIfError = 0;
|
2006-12-17 16:36:27 +00:00
|
|
|
failureAnnouncing( tc );
|
2006-07-16 19:39:23 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2006-12-17 16:36:27 +00:00
|
|
|
else if( ( bePeers = tr_bencDictFind( &beAll, "warning message" ) ) )
|
|
|
|
{
|
|
|
|
tr_err( "Tracker: Warning - %s", bePeers->val.s.s );
|
2007-01-14 12:00:21 +00:00
|
|
|
tor->error = TR_ERROR_TC_WARNING;
|
|
|
|
snprintf( tor->errorString, sizeof( tor->errorString ),
|
2006-12-17 16:36:27 +00:00
|
|
|
"%s", bePeers->val.s.s );
|
|
|
|
}
|
2007-01-14 12:00:21 +00:00
|
|
|
else if( tor->error & TR_ERROR_TC_MASK )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
tor->error = TR_OK;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-12-23 21:18:58 +00:00
|
|
|
tc->lastError = 0;
|
|
|
|
tc->allUnreachIfError = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
/* Get the tracker interval, force to between
|
|
|
|
10 sec and 5 mins */
|
|
|
|
beFoo = tr_bencDictFind( &beAll, "interval" );
|
|
|
|
if( !beFoo || TYPE_INT != beFoo->type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
tr_err( "Tracker: no 'interval' field" );
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
tc->interval = beFoo->val.i;
|
|
|
|
tr_inf( "Tracker: interval = %d seconds", tc->interval );
|
|
|
|
tc->interval = MAX( 10, tc->interval );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
/* Get the tracker minimum interval, force to between
|
|
|
|
10 sec and 5 mins */
|
|
|
|
beFoo = tr_bencDictFind( &beAll, "min interval" );
|
|
|
|
if( beFoo && TYPE_INT == beFoo->type )
|
|
|
|
{
|
|
|
|
tc->minInterval = beFoo->val.i;
|
|
|
|
tr_inf( "Tracker: min interval = %d seconds", tc->minInterval );
|
|
|
|
tc->minInterval = MAX( 10, tc->minInterval );
|
|
|
|
|
|
|
|
if( tc->interval < tc->minInterval )
|
|
|
|
{
|
|
|
|
tc->interval = tc->minInterval;
|
|
|
|
tr_inf( "Tracker: 'interval' less than 'min interval', "
|
|
|
|
"using 'min interval'" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tc->minInterval = 0;
|
|
|
|
tr_inf( "Tracker: no 'min interval' field" );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
scrapeNeeded = 0;
|
|
|
|
beFoo = tr_bencDictFind( &beAll, "complete" );
|
|
|
|
if( beFoo && TYPE_INT == beFoo->type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tc->seeders = beFoo->val.i;
|
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
scrapeNeeded = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
beFoo = tr_bencDictFind( &beAll, "incomplete" );
|
|
|
|
if( beFoo && TYPE_INT == beFoo->type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tc->leechers = beFoo->val.i;
|
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
scrapeNeeded = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tc->scrapeNeeded = scrapeNeeded;
|
|
|
|
if( !scrapeNeeded )
|
|
|
|
{
|
|
|
|
tc->hasManyPeers = ( tc->seeders + tc->leechers >= 50 );
|
|
|
|
}
|
|
|
|
|
|
|
|
beFoo = tr_bencDictFind( &beAll, "tracker id" );
|
|
|
|
if( beFoo )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
free( tc->trackerid );
|
|
|
|
tc->trackerid = strdup( beFoo->val.s.s );
|
|
|
|
tr_inf( "Tracker: tracker id = %s", tc->trackerid );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
bePeers = tr_bencDictFind( &beAll, "peers" );
|
|
|
|
if( !bePeers )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-27 21:17:10 +00:00
|
|
|
if( tc->stopped || shouldChangePort( tc ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
goto nodict;
|
|
|
|
}
|
|
|
|
tr_err( "Tracker: no \"peers\" field" );
|
2006-12-17 16:36:27 +00:00
|
|
|
failureAnnouncing( tc );
|
2006-07-16 19:39:23 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2007-03-30 00:12:39 +00:00
|
|
|
if( TYPE_LIST == bePeers->type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
/* Original protocol */
|
2007-01-19 11:19:20 +00:00
|
|
|
if( bePeers->val.l.count > 0 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-03-30 00:12:39 +00:00
|
|
|
peerCompact = parseOriginalPeers( bePeers, &peerCount );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-30 00:12:39 +00:00
|
|
|
else if( TYPE_STR == bePeers->type )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
/* "Compact" extension */
|
2007-01-19 11:19:20 +00:00
|
|
|
if( bePeers->val.s.i >= 6 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-19 11:19:20 +00:00
|
|
|
peerCount = bePeers->val.s.i / 6;
|
|
|
|
peerCompact = malloc( bePeers->val.s.i );
|
|
|
|
memcpy( peerCompact, bePeers->val.s.s, bePeers->val.s.i );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-19 11:19:20 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-19 11:19:20 +00:00
|
|
|
if( peerCount > 0 )
|
|
|
|
{
|
|
|
|
tr_inf( "Tracker: got %d peers", peerCount );
|
|
|
|
if( peerCount >= 50 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tc->hasManyPeers = 1;
|
|
|
|
}
|
2007-01-19 11:19:20 +00:00
|
|
|
*_peerCount = peerCount;
|
|
|
|
*_peerCompact = peerCompact;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodict:
|
|
|
|
/* Success */
|
|
|
|
tc->started = 0;
|
|
|
|
tc->completed = 0;
|
|
|
|
tc->dateOk = tr_date();
|
|
|
|
|
|
|
|
if( tc->stopped )
|
|
|
|
{
|
|
|
|
tor->status = TR_STATUS_STOPPED;
|
|
|
|
tc->stopped = 0;
|
|
|
|
}
|
2007-01-27 21:17:10 +00:00
|
|
|
else if( shouldChangePort( tc ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tc->started = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if( shouldfree )
|
|
|
|
{
|
|
|
|
tr_bencFree( &beAll );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
static void readScrapeAnswer( tr_tracker_t * tc, const char * data, int len )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
int code;
|
|
|
|
const uint8_t * body;
|
|
|
|
int bodylen, ii;
|
|
|
|
benc_val_t scrape, * val1, * val2;
|
2006-12-26 18:43:46 +00:00
|
|
|
char * address;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
code = tr_httpResponseCode( data, len );
|
|
|
|
if( 0 > code )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
/* We don't have a valid HTTP status line */
|
|
|
|
tr_inf( "Scrape: invalid HTTP status line" );
|
|
|
|
tc->lastScrapeFailed = 1;
|
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
if( code == 301 || code == 302 )
|
|
|
|
{
|
|
|
|
tr_http_header_t hdr[] = { { "Location", NULL, 0 }, { NULL, NULL, 0 } };
|
|
|
|
|
2007-01-14 08:51:15 +00:00
|
|
|
tr_err( "Scrape: HTTP status code: %i", code );
|
|
|
|
|
2006-12-17 16:36:27 +00:00
|
|
|
tr_httpParse( data, len, hdr );
|
|
|
|
|
2006-12-26 18:43:46 +00:00
|
|
|
address = calloc( sizeof( char ), hdr->len+1 );
|
2006-12-17 16:36:27 +00:00
|
|
|
snprintf( address, hdr->len+1, "%s", hdr->data );
|
|
|
|
|
|
|
|
/* Needs a new scrape */
|
|
|
|
tc->dateScrape = 0;
|
|
|
|
|
|
|
|
tc->redirectScrapeAddress = address;
|
|
|
|
tc->redirectScrapeAddressLen = hdr->len;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2006-09-25 18:37:45 +00:00
|
|
|
if( !TR_HTTP_STATUS_OK( code ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
/* we didn't get a 2xx status code */
|
|
|
|
tr_err( "Scrape: invalid HTTP status code: %i", code );
|
2006-12-17 16:36:27 +00:00
|
|
|
if( TR_HTTP_STATUS_FAIL_CLIENT( code ) )
|
2007-02-27 04:00:38 +00:00
|
|
|
{
|
2007-04-08 05:41:44 +00:00
|
|
|
tc->tcCur->tl_badscrape = 1;
|
2007-02-27 04:00:38 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
tc->lastScrapeFailed = 1;
|
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
/* find the end of the http headers */
|
|
|
|
body = (uint8_t *) tr_httpParse( data, len, NULL );
|
2006-09-25 18:37:45 +00:00
|
|
|
if( NULL == body )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
tr_err( "Scrape: could not find end of HTTP headers" );
|
|
|
|
tc->lastScrapeFailed = 1;
|
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
tc->lastScrapeFailed = 0;
|
|
|
|
bodylen = len - ( body - (const uint8_t*)data );
|
|
|
|
|
|
|
|
for( ii = 0; ii < bodylen; ii++ )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
if( !tr_bencLoad( body + ii, bodylen - ii, &scrape, NULL ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
if( ii >= bodylen )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-12-02 01:46:54 +00:00
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val1 = tr_bencDictFind( &scrape, "files" );
|
2007-01-23 20:54:54 +00:00
|
|
|
if( !val1 || val1->type != TYPE_DICT || val1->val.l.count < 1 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
tr_bencFree( &scrape );
|
2006-12-02 01:46:54 +00:00
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
val1 = &val1->val.l.vals[1];
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
val2 = tr_bencDictFind( val1, "complete" );
|
|
|
|
if( !val2 )
|
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
tr_bencFree( &scrape );
|
2006-12-02 01:46:54 +00:00
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
tc->seeders = val2->val.i;
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
val2 = tr_bencDictFind( val1, "incomplete" );
|
|
|
|
if( !val2 )
|
|
|
|
{
|
2006-09-25 18:37:45 +00:00
|
|
|
tr_bencFree( &scrape );
|
2006-12-02 01:46:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tc->leechers = val2->val.i;
|
|
|
|
|
|
|
|
val2 = tr_bencDictFind( val1, "downloaded" );
|
|
|
|
if( !val2 )
|
|
|
|
{
|
|
|
|
tr_bencFree( &scrape );
|
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
tc->complete = val2->val.i;
|
|
|
|
|
2007-01-12 05:05:11 +00:00
|
|
|
val2 = tr_bencDictFind( &scrape, "flags" );
|
2006-12-02 01:46:54 +00:00
|
|
|
if( val2 )
|
|
|
|
{
|
|
|
|
val2 = tr_bencDictFind( val2, "min_request_interval" );
|
|
|
|
if( val2 )
|
|
|
|
{
|
|
|
|
tc->scrapeInterval = val2->val.i;
|
2007-01-12 05:05:11 +00:00
|
|
|
tr_inf( "Scrape: min_request_interval = %d seconds", tc->scrapeInterval );
|
2006-12-02 01:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tc->hasManyPeers = ( tc->seeders + tc->leechers >= 50 );
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_bencFree( &scrape );
|
|
|
|
}
|
|
|
|
|
|
|
|
int tr_trackerSeeders( tr_tracker_t * tc )
|
|
|
|
{
|
|
|
|
if( !tc )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return tc->seeders;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tr_trackerLeechers( tr_tracker_t * tc )
|
|
|
|
{
|
|
|
|
if( !tc )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return tc->leechers;
|
|
|
|
}
|
2006-12-02 01:46:54 +00:00
|
|
|
|
|
|
|
int tr_trackerDownloaded( tr_tracker_t * tc )
|
|
|
|
{
|
|
|
|
if( !tc )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return tc->complete;
|
|
|
|
}
|
|
|
|
|
2007-02-27 04:00:38 +00:00
|
|
|
tr_tracker_info_t * tr_trackerGet( tr_tracker_t * tc )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
|
|
|
if( !tc )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-04-08 05:41:44 +00:00
|
|
|
return tc->tcCur->tl_inf;
|
2006-12-17 16:36:27 +00:00
|
|
|
}
|
|
|
|
|
2006-12-17 17:09:00 +00:00
|
|
|
int tr_trackerCannotConnect( tr_tracker_t * tc )
|
2006-12-17 16:36:27 +00:00
|
|
|
{
|
|
|
|
if( !tc )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return tc->completelyUnconnectable;
|
|
|
|
}
|
|
|
|
|
2006-12-02 01:46:54 +00:00
|
|
|
/* Blocking version */
|
|
|
|
int tr_trackerScrape( tr_torrent_t * tor, int * s, int * l, int * d )
|
|
|
|
{
|
|
|
|
tr_tracker_t * tc;
|
|
|
|
tr_http_t * http;
|
|
|
|
const char * data;
|
|
|
|
int len;
|
|
|
|
int ret;
|
2006-12-17 16:36:27 +00:00
|
|
|
|
|
|
|
tc = tr_trackerInit( tor );
|
2006-12-02 01:46:54 +00:00
|
|
|
|
2007-04-08 05:41:44 +00:00
|
|
|
if( NULL == tc->tcCur->tl_inf->scrape || tc->tcCur->tl_badscrape )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
http = getScrapeQuery( tc );
|
|
|
|
|
|
|
|
for( data = NULL; !data; tr_wait( 10 ) )
|
|
|
|
{
|
|
|
|
switch( tr_httpPulse( http, &data, &len ) )
|
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_WAIT:
|
2006-12-02 01:46:54 +00:00
|
|
|
break;
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_ERROR:
|
2006-12-02 01:46:54 +00:00
|
|
|
goto scrapeDone;
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
case TR_NET_OK:
|
2006-12-02 01:46:54 +00:00
|
|
|
readScrapeAnswer( tc, data, len );
|
|
|
|
goto scrapeDone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scrapeDone:
|
|
|
|
tr_httpClose( http );
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
if( tc->seeders > -1 && tc->leechers > -1 && tc->complete > -1 )
|
|
|
|
{
|
|
|
|
*s = tc->seeders;
|
|
|
|
*l = tc->leechers;
|
|
|
|
*d = tc->complete;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_trackerClose( tc );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
static void killHttp( tr_http_t ** http )
|
2006-12-02 01:46:54 +00:00
|
|
|
{
|
|
|
|
if( NULL != *http )
|
|
|
|
{
|
|
|
|
tr_httpClose( *http );
|
|
|
|
*http = NULL;
|
|
|
|
}
|
|
|
|
}
|
2007-01-27 21:17:10 +00:00
|
|
|
|
|
|
|
static int shouldChangePort( tr_tracker_t * tc )
|
|
|
|
{
|
|
|
|
tr_torrent_t * tor = tc->tor;
|
|
|
|
|
|
|
|
return ( tor->publicPort != tc->publicPort );
|
|
|
|
}
|
2007-03-30 00:12:39 +00:00
|
|
|
|
|
|
|
/* Convert to compact form */
|
|
|
|
static uint8_t *
|
|
|
|
parseOriginalPeers( benc_val_t * bePeers, int * peerCount )
|
|
|
|
{
|
|
|
|
struct in_addr addr;
|
|
|
|
in_port_t port;
|
|
|
|
uint8_t * peerCompact;
|
|
|
|
benc_val_t * peer, * addrval, * portval;
|
|
|
|
int ii, count;
|
|
|
|
|
|
|
|
assert( TYPE_LIST == bePeers->type );
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
peerCompact = malloc( 6 * bePeers->val.l.count );
|
|
|
|
if( NULL == peerCompact )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-03-31 17:40:28 +00:00
|
|
|
for( ii = 0; bePeers->val.l.count > ii; ii++ )
|
2007-03-30 00:12:39 +00:00
|
|
|
{
|
2007-03-31 17:40:28 +00:00
|
|
|
peer = &bePeers->val.l.vals[ii];
|
2007-03-30 00:12:39 +00:00
|
|
|
addrval = tr_bencDictFind( peer, "ip" );
|
|
|
|
if( NULL == addrval || TYPE_STR != addrval->type ||
|
|
|
|
tr_netResolve( addrval->val.s.s, &addr ) )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy( &peerCompact[6 * count], &addr, 4 );
|
|
|
|
|
|
|
|
portval = tr_bencDictFind( peer, "port" );
|
|
|
|
if( NULL == portval || TYPE_INT != portval->type ||
|
|
|
|
0 > portval->val.i || 0xffff < portval->val.i )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
port = htons( portval->val.i );
|
|
|
|
memcpy( &peerCompact[6 * count + 4], &port, 2 );
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*peerCount = count;
|
|
|
|
|
|
|
|
return peerCompact;
|
|
|
|
}
|