source: trunk/libtransmission/tracker.c @ 3731

Last change on this file since 3731 was 3731, checked in by charles, 15 years ago

use Thomas Bernard's miniupnp library instead of rolling our own.

  • Property svn:keywords set to Date Rev Author Id
File size: 28.7 KB
Line 
1/*
2 * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: tracker.c 3731 2007-11-06 16:02:50Z charles $
11 */
12
13#include <assert.h>
14#include <ctype.h> /* isalnum */
15#include <stdio.h> /* snprintf */
16#include <stdlib.h>
17#include <string.h> /* strcmp, strchr */
18#include <libgen.h> /* basename */
19
20#include <sys/queue.h> /* libevent needs this */
21#include <sys/types.h> /* libevent needs this */
22#include <event.h>
23#include <evhttp.h>
24
25#include "transmission.h"
26#include "bencode.h"
27#include "completion.h"
28#include "net.h"
29#include "publish.h"
30#include "shared.h"
31#include "tracker.h"
32#include "trevent.h"
33#include "utils.h"
34
35enum
36{
37    /* unless the tracker says otherwise, rescrape this frequently */
38    DEFAULT_SCRAPE_INTERVAL_SEC = (60 * 15),
39
40    /* unless the tracker says otherwise, this is the announce interval */
41    DEFAULT_ANNOUNCE_INTERVAL_SEC = (60 * 4),
42
43    /* unless the tracker says otherwise, this is the announce min_interval */
44    DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = (60 * 2),
45
46    /* this is how long we'll leave a request hanging before timeout */
47    TIMEOUT_INTERVAL_SEC = 45,
48
49    /* this is how long we'll leave a 'stop' request hanging before timeout.
50       we wait less time for this so it doesn't slow down shutdowns */
51    STOP_TIMEOUT_INTERVAL_SEC = 5,
52
53    /* the value of the 'numwant' argument passed in tracker requests. */
54    NUMWANT = 200,
55
56    /* the length of the 'key' argument passed in tracker requests */
57    KEYLEN = 10
58};
59
60/**
61***
62**/
63
64struct tr_tracker
65{
66    tr_handle * handle;
67
68    /* these are set from the latest scrape or tracker response */
69    int announceIntervalSec;
70    int announceMinIntervalSec;
71    int scrapeIntervalSec;
72
73    tr_tracker_info * redirect;
74    tr_tracker_info * addresses;
75    int addressIndex;
76    int addressCount;
77    int * tierFronts;
78
79    /* sent as the "key" argument in tracker requests
80       to verify us if our IP address changes.
81       This is immutable for the life of the tracker object. */
82    char key_param[KEYLEN+1];
83
84    tr_publisher_t * publisher;
85
86    /* torrent hash string */
87    uint8_t hash[SHA_DIGEST_LENGTH];
88    char escaped[SHA_DIGEST_LENGTH * 3 + 1];
89    char * name;
90
91    /* corresponds to the peer_id sent as a tracker request parameter.
92       one tracker admin says: "When the same torrent is opened and
93       closed and opened again without quitting Transmission ...
94       change the peerid. It would help sometimes if a stopped event
95       was missed to ensure that we didn't think someone was cheating. */
96    char peer_id[TR_ID_LEN + 1];
97
98    /* these are set from the latest tracker response... -1 is 'unknown' */
99    int timesDownloaded;
100    int seederCount;
101    int leecherCount;
102    char * trackerID;
103
104    /* the last tracker request we sent. (started, stopped, etc.)
105       automatic announces are an empty string;
106       NULL means no message has ever been sent */
107    char * lastRequest;
108
109    time_t manualAnnounceAllowedAt;
110
111    tr_timer * scrapeTimer;
112    tr_timer * reannounceTimer;
113
114    unsigned int isRunning : 1;
115};
116
117/**
118***
119**/
120
121static void
122myDebug( const char * file, int line, const tr_tracker * t, const char * fmt, ... )
123{   
124    FILE * fp = tr_getLog( );
125    if( fp != NULL )
126    {
127        va_list args;
128        char timestr[64];
129        struct evbuffer * buf = evbuffer_new( );
130        char * myfile = tr_strdup( file );
131
132        evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof(timestr) ) );
133        if( t != NULL )
134            evbuffer_add_printf( buf, "%s ", t->name );
135        va_start( args, fmt );
136        evbuffer_add_vprintf( buf, fmt, args );
137        va_end( args );
138        evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
139        fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
140
141        tr_free( myfile );
142        evbuffer_free( buf );
143    }
144}
145
146#define dbgmsg(t, fmt...) myDebug(__FILE__, __LINE__, t, ##fmt )
147
148
149/***
150****  Connections that know how to clean up after themselves
151***/
152
153static int
154freeConnection( void * evcon )
155{
156    evhttp_connection_free( evcon );
157    return FALSE;
158}
159
160static void
161connectionClosedCB( struct evhttp_connection * evcon, void * handle )
162{
163    /* libevent references evcon right after calling this function,
164       so we can't free it yet... defer it to after this call chain
165       has played out */
166    tr_timerNew( handle, freeConnection, evcon, 100 );
167}
168
169static struct evhttp_connection*
170getConnection( tr_tracker * t, const char * address, int port )
171{
172    struct evhttp_connection * c = evhttp_connection_new( address, port );
173    evhttp_connection_set_closecb( c, connectionClosedCB, t->handle );
174    return c;
175}
176
177/***
178****  PUBLISH
179***/
180
181static const tr_tracker_event emptyEvent = { 0, NULL, NULL, NULL, 0 };
182
183static void
184publishMessage( tr_tracker * t, const char * msg, int type )
185{
186    tr_tracker_event event = emptyEvent;
187    event.hash = t->hash;
188    event.messageType = type;
189    event.text = msg;
190    tr_publisherPublish( t->publisher, t, &event );
191}
192
193static void
194publishErrorClear( tr_tracker * t )
195{
196    publishMessage( t, NULL, TR_TRACKER_ERROR_CLEAR );
197}
198
199static void
200publishErrorMessage( tr_tracker * t, const char * msg )
201{
202    publishMessage( t, msg, TR_TRACKER_ERROR );
203}
204
205static void
206publishWarning( tr_tracker * t, const char * msg )
207{
208    publishMessage( t, msg, TR_TRACKER_WARNING );
209}
210
211static void
212publishNewPeers( tr_tracker * t, int count, uint8_t * peers )
213{
214    tr_tracker_event event = emptyEvent;
215    event.hash = t->hash;
216    event.messageType = TR_TRACKER_PEERS;
217    event.peerCount = count;
218    event.peerCompact = peers;
219    tr_inf( "Torrent \"%s\" got %d new peers", t->name, count );
220    tr_publisherPublish( t->publisher, t, &event );
221}
222
223/***
224****  LIFE CYCLE
225***/
226
227static void
228generateKeyParam( char * msg, int len )
229{
230    int i;
231    const char * pool = "abcdefghijklmnopqrstuvwxyz0123456789";
232    const int poolSize = strlen( pool );
233    for( i=0; i<len; ++i )
234        *msg++ = pool[tr_rand(poolSize)];
235    *msg = '\0';
236}
237
238static void
239escape( char * out, const uint8_t * in, int in_len ) /* rfc2396 */
240{
241    const uint8_t *end = in + in_len;
242    while( in != end )
243        if( isalnum(*in) )
244            *out++ = (char) *in++;
245        else 
246            out += snprintf( out, 4, "%%%02X", (unsigned int)*in++ );
247    *out = '\0';
248}
249
250static void scrapeNow( tr_tracker * );
251
252tr_tracker *
253tr_trackerNew( const tr_torrent * torrent )
254{
255    const tr_info * info = &torrent->info;
256    int i, j, sum, *iwalk;
257    tr_tracker_info * nwalk;
258    tr_tracker * t;
259
260    tr_dbg( "making a new tracker for \"%s\"", info->primaryAddress );
261
262    t = tr_new0( tr_tracker, 1 );
263    t->handle = torrent->handle;
264    t->scrapeIntervalSec       = DEFAULT_SCRAPE_INTERVAL_SEC;
265    t->announceIntervalSec     = DEFAULT_ANNOUNCE_INTERVAL_SEC;
266    t->announceMinIntervalSec  = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
267    generateKeyParam( t->key_param, KEYLEN );
268
269    t->publisher = tr_publisherNew( );
270    t->timesDownloaded = -1;
271    t->seederCount = -1;
272    t->leecherCount = -1;
273    t->manualAnnounceAllowedAt = ~(time_t)0;
274    t->name = tr_strdup( info->name );
275    memcpy( t->hash, info->hash, SHA_DIGEST_LENGTH );
276    escape( t->escaped, info->hash, SHA_DIGEST_LENGTH );
277
278    for( sum=i=0; i<info->trackerTiers; ++i )
279         sum += info->trackerList[i].count;
280    t->addresses = nwalk = tr_new0( tr_tracker_info, sum );
281    t->addressIndex = 0;
282    t->addressCount = sum;
283    t->tierFronts = iwalk = tr_new0( int, sum );
284
285    for( i=0; i<info->trackerTiers; ++i )
286    {
287        const int tierFront = nwalk - t->addresses;
288
289        for( j=0; j<info->trackerList[i].count; ++j )
290        {
291            const tr_tracker_info * src = &info->trackerList[i].list[j];
292            nwalk->address = tr_strdup( src->address );
293            nwalk->port = src->port;
294            nwalk->announce = tr_strdup( src->announce );
295            nwalk->scrape = tr_strdup( src->scrape );
296            ++nwalk;
297
298            *iwalk++ = tierFront;
299        }
300    }
301
302    assert( nwalk - t->addresses == sum );
303    assert( iwalk - t->tierFronts == sum );
304
305    scrapeNow( t );
306    return t;
307}
308
309static void
310onTrackerFreeNow( void * vt )
311{
312    int i;
313    tr_tracker * t = vt;
314
315    tr_timerFree( &t->scrapeTimer );
316    tr_timerFree( &t->reannounceTimer );
317    tr_publisherFree( &t->publisher );
318    tr_free( t->name );
319    tr_free( t->trackerID );
320    tr_free( t->lastRequest );
321
322    /* addresses... */
323    for( i=0; i<t->addressCount; ++i )
324        tr_trackerInfoClear( &t->addresses[i] );
325    tr_free( t->addresses );
326    tr_free( t->tierFronts );
327
328    /* redirect... */
329    if( t->redirect ) {
330        tr_trackerInfoClear( t->redirect );
331        tr_free( t->redirect );
332    }
333
334    tr_free( t );
335}
336
337void
338tr_trackerFree( tr_tracker * t )
339{
340    tr_runInEventThread( t->handle, onTrackerFreeNow, t );
341}
342
343/***
344****  UTIL
345***/
346
347static int
348parseBencResponse( struct evhttp_request * req, benc_val_t * setme )
349{
350    const unsigned char * body = EVBUFFER_DATA( req->input_buffer );
351    const int bodylen = EVBUFFER_LENGTH( req->input_buffer );
352    int ret = 1;
353    int i;
354
355    for( i=0; ret && i<bodylen; ++i )
356        if( !tr_bencLoad( body+i, bodylen-1, setme, NULL ) )
357            ret = 0;
358
359    return ret;
360}
361
362static const char*
363updateAddresses( tr_tracker * t, const struct evhttp_request * req )
364{
365    const char * ret = NULL;
366    int moveToNextAddress = FALSE;
367
368    if( !req )
369    {
370        ret = "Tracker hasn't responded yet.  Retrying...";
371        tr_inf( ret );
372
373        moveToNextAddress = TRUE;
374    }
375    else if( req->response_code == HTTP_OK )
376    {
377        if( t->redirect != NULL )
378        {
379            /* multitracker spec: "if a connection with a tracker is
380               successful, it will be moved to the front of the tier." */
381            const int i = t->addressIndex;
382            const int j = t->tierFronts[i];
383            const tr_tracker_info swap = t->addresses[i];
384            t->addresses[i] = t->addresses[j];
385            t->addresses[j] = swap;
386        }
387    }
388    else if(    ( req->response_code == HTTP_MOVEPERM )
389             || ( req->response_code == HTTP_MOVETEMP ) )
390    {
391        const char * loc = evhttp_find_header( req->input_headers, "Location" );
392        tr_tracker_info tmp;
393        if( tr_trackerInfoInit( &tmp, loc, -1 ) ) /* a bad redirect? */
394        {
395            moveToNextAddress = TRUE;
396        }
397        else if( req->response_code == HTTP_MOVEPERM )
398        {
399            tr_tracker_info * cur = &t->addresses[t->addressIndex];
400            tr_trackerInfoClear( cur );
401            *cur = tmp;
402        }
403        else if( req->response_code == HTTP_MOVETEMP )
404        {
405            if( t->redirect == NULL )
406                t->redirect = tr_new0( tr_tracker_info, 1 );
407            else
408                tr_trackerInfoClear( t->redirect );
409            *t->redirect = tmp;
410        }
411    }
412    else 
413    {
414        moveToNextAddress = TRUE;
415    }
416
417    if( moveToNextAddress )
418        if ( ++t->addressIndex >= t->addressCount )
419            t->addressIndex = 0;
420
421    return ret;
422}
423
424static tr_tracker_info *
425getCurrentAddress( const tr_tracker * t )
426{
427    assert( t->addresses != NULL );
428    assert( t->addressIndex >= 0 );
429    assert( t->addressIndex < t->addressCount );
430
431    return &t->addresses[t->addressIndex];
432}
433static int
434trackerSupportsScrape( const tr_tracker * t )
435{
436    const tr_tracker_info * info = getCurrentAddress( t );
437
438    return ( info != NULL )
439        && ( info->scrape != NULL )
440        && ( info->scrape[0] != '\0' );
441}
442
443
444static void
445addCommonHeaders( const tr_tracker * t,
446                  struct evhttp_request * req )
447{
448    char buf[1024];
449    const tr_tracker_info * address = getCurrentAddress( t );
450    snprintf( buf, sizeof(buf), "%s:%d", address->address, address->port );
451    evhttp_add_header( req->output_headers, "Host", buf );
452    evhttp_add_header( req->output_headers, "Connection", "close" );
453    evhttp_add_header( req->output_headers, "Content-Length", "0" );
454    evhttp_add_header( req->output_headers, "User-Agent",
455                                         TR_NAME "/" LONG_VERSION_STRING );
456}
457
458/**
459***
460**/
461
462struct torrent_hash
463{
464    tr_handle * handle;
465    uint8_t hash[SHA_DIGEST_LENGTH];
466};
467
468static struct torrent_hash*
469torrentHashNew( tr_tracker * t )
470{
471    struct torrent_hash * data = tr_new( struct torrent_hash, 1 );
472    data->handle = t->handle;
473    memcpy( data->hash, t->hash, SHA_DIGEST_LENGTH );
474    return data;
475}
476
477tr_tracker *
478findTrackerFromHash( struct torrent_hash * data )
479{
480    tr_torrent * torrent = tr_torrentFindFromHash( data->handle, data->hash );
481    return torrent ? torrent->tracker : NULL;
482}
483
484/***
485****
486****  SCRAPE
487****
488***/
489
490static int
491onScrapeNow( void * vt );
492
493static void
494onScrapeResponse( struct evhttp_request * req, void * vhash )
495{
496    const char * warning;
497    time_t nextScrapeSec = 60;
498    tr_tracker * t;
499
500    t = findTrackerFromHash( vhash );
501    tr_free( vhash );
502    if( t == NULL ) /* tracker's been closed... */
503        return;
504
505    tr_inf( "Got scrape response for  '%s': %s",
506            t->name,
507            ( ( req && req->response_code_line ) ? req->response_code_line
508                                                 : "(null)") );
509
510    if( req && ( req->response_code == HTTP_OK ) )
511    {
512        benc_val_t benc, *files;
513        const int bencLoaded = !parseBencResponse( req, &benc );
514
515        if( bencLoaded
516            && (( files = tr_bencDictFind( &benc, "files" ) ))
517            && ( files->type == TYPE_DICT ) )
518        {
519            int i;
520            for( i=0; i<files->val.l.count; i+=2 )
521            {
522                const uint8_t* hash =
523                        (const uint8_t*) files->val.l.vals[i].val.s.s;
524                benc_val_t *tmp, *flags;
525                benc_val_t *tordict = &files->val.l.vals[i+1];
526                if( memcmp( t->hash, hash, SHA_DIGEST_LENGTH ) )
527                    continue;
528
529                publishErrorClear( t );
530
531                if(( tmp = tr_bencDictFind( tordict, "complete" )))
532                    t->seederCount = tmp->val.i;
533
534                if(( tmp = tr_bencDictFind( tordict, "incomplete" )))
535                    t->leecherCount = tmp->val.i;
536
537                if(( tmp = tr_bencDictFind( tordict, "downloaded" )))
538                    t->timesDownloaded = tmp->val.i;
539
540                if(( flags = tr_bencDictFind( tordict, "flags" )))
541                    if(( tmp = tr_bencDictFind( flags, "min_request_interval")))
542                        t->scrapeIntervalSec = tmp->val.i;
543
544                tr_dbg( "Torrent '%s' scrape successful."
545                        "  Rescraping in %d seconds",
546                        t->name, t->scrapeIntervalSec );
547
548                nextScrapeSec = t->scrapeIntervalSec;
549            }
550        }
551
552        if( bencLoaded )
553            tr_bencFree( &benc );
554    }
555
556    if (( warning = updateAddresses( t, req ) )) {
557        tr_err( warning );
558        publishWarning( t, warning );
559    }
560
561    tr_timerFree( &t->scrapeTimer );
562
563    t->scrapeTimer = tr_timerNew( t->handle,
564                                  onScrapeNow, t,
565                                  nextScrapeSec*1000 );
566}
567
568static int
569onScrapeNow( void * vt )
570{
571    tr_tracker * t = vt;
572    const tr_tracker_info * address = getCurrentAddress( t );
573
574    if( trackerSupportsScrape( t ) )
575    {
576        char * uri;
577        struct evhttp_connection * evcon;
578        struct evhttp_request *req;
579        struct evbuffer * buf = evbuffer_new( );
580
581        evbuffer_add_printf( buf, "%s%sinfo_hash=%s",
582                             address->scrape,
583                             ( strchr(address->scrape, '?') == NULL ? "?" : "&" ),
584                             t->escaped );
585        uri = tr_strdup( (char*) EVBUFFER_DATA( buf ) );
586        evbuffer_free( buf );
587
588        tr_inf( "Sending scrape to tracker %s:%d: %s",
589                address->address, address->port, uri );
590
591        evcon = getConnection( t, address->address, address->port );
592        evhttp_connection_set_timeout( evcon, TIMEOUT_INTERVAL_SEC );
593        req = evhttp_request_new( onScrapeResponse, torrentHashNew( t ) );
594        addCommonHeaders( t, req );
595        tr_evhttp_make_request( t->handle, evcon, req, EVHTTP_REQ_GET, uri );
596    }
597
598    t->scrapeTimer = NULL;
599    return FALSE;
600}
601
602static void
603scrapeNow( tr_tracker * t )
604{
605    onScrapeNow( t );
606}
607
608/***
609****
610****  TRACKER REQUESTS
611****
612***/
613
614static char*
615buildTrackerRequestURI( const tr_tracker  * t,
616                        const tr_torrent  * torrent,
617                        const char        * eventName )
618{
619    const int isStopping = !strcmp( eventName, "stopped" );
620    const int numwant = isStopping ? 0 : NUMWANT;
621    struct evbuffer * buf = evbuffer_new( );
622    char * ret;
623
624    char * ann = getCurrentAddress(t)->announce;
625   
626    evbuffer_add_printf( buf, "%s"
627                              "%cinfo_hash=%s"
628                              "&peer_id=%s"
629                              "&port=%d"
630                              "&uploaded=%"PRIu64
631                              "&downloaded=%"PRIu64
632                              "&corrupt=%"PRIu64
633                              "&left=%"PRIu64
634                              "&compact=1"
635                              "&numwant=%d"
636                              "&key=%s"
637                              "&supportcrypto=1"
638                              "&requirecrypto=%d"
639                              "%s%s"
640                              "%s%s",
641        ann,
642        strchr(ann, '?') ? '&' : '?',
643        t->escaped,
644        t->peer_id,
645        tr_sharedGetPublicPort( t->handle->shared ),
646        torrent->uploadedCur,
647        torrent->downloadedCur,
648        torrent->corruptCur,
649        tr_cpLeftUntilComplete( torrent->completion ),
650        numwant,
651        t->key_param,
652        ( t->handle->encryptionMode==TR_ENCRYPTION_REQUIRED ? 1 : 0 ),
653        ( ( eventName && *eventName ) ? "&event=" : "" ),
654        ( ( eventName && *eventName ) ? eventName : "" ),
655        ( ( t->trackerID && *t->trackerID ) ? "&trackerid=" : "" ),
656        ( ( t->trackerID && *t->trackerID ) ? t->trackerID : "" ) );
657
658    ret = tr_strdup( (char*) EVBUFFER_DATA( buf ) );
659    evbuffer_free( buf );
660    return ret;
661}
662
663/* Convert to compact form */
664static uint8_t *
665parseOldPeers( benc_val_t * bePeers, int * setmePeerCount )
666{
667    int i;
668    uint8_t *compact, *walk;
669    const int peerCount = bePeers->val.l.count;
670
671    assert( bePeers->type == TYPE_LIST );
672
673    compact = tr_new( uint8_t, peerCount*6 );
674
675    for( i=0, walk=compact; i<peerCount; ++i )
676    {
677        struct in_addr addr;
678        tr_port_t port;
679        benc_val_t * val;
680        benc_val_t * peer = &bePeers->val.l.vals[i];
681
682        val = tr_bencDictFind( peer, "ip" );
683        if( !val || val->type!=TYPE_STR || tr_netResolve(val->val.s.s, &addr) )
684            continue;
685
686        memcpy( walk, &addr, 4 );
687        walk += 4;
688
689        val = tr_bencDictFind( peer, "port" );
690        if( !val || val->type!=TYPE_INT || val->val.i<0 || val->val.i>0xffff )
691            continue;
692
693        port = htons( val->val.i );
694        memcpy( walk, &port, 2 );
695        walk += 2;
696    }
697
698    *setmePeerCount = peerCount;
699    return compact;
700}
701
702static int onRetry( void * vt );
703static int onReannounce( void * vt );
704
705static void
706onStoppedResponse( struct evhttp_request * req UNUSED, void * handle UNUSED )
707{
708    dbgmsg( NULL, "got a response to some `stop' message" );
709}
710
711static void
712onTrackerResponse( struct evhttp_request * req, void * torrent_hash )
713{
714    const char * warning;
715    tr_tracker * t;
716    int err = 0;
717    int responseCode;
718
719    t = findTrackerFromHash( torrent_hash );
720    tr_free( torrent_hash );
721    if( t == NULL ) /* tracker has been closed */
722        return;
723
724    dbgmsg( t, "got response from tracker: \"%s\"",
725            ( req && req->response_code_line ) ?  req->response_code_line
726                                               : "(null)" );
727
728    tr_inf( "Torrent \"%s\" tracker response: %s",
729            t->name,
730            ( req ? req->response_code_line : "(null)") );
731
732    if( req && ( req->response_code == HTTP_OK ) )
733    {
734        benc_val_t benc;
735        const int bencLoaded = !parseBencResponse( req, &benc );
736
737        publishErrorClear( t );
738
739        if( bencLoaded && benc.type==TYPE_DICT )
740        {
741            benc_val_t * tmp;
742
743            if(( tmp = tr_bencDictFind( &benc, "failure reason" ))) {
744                dbgmsg( t, "got failure message [%s]", tmp->val.s.s );
745                publishErrorMessage( t, tmp->val.s.s );
746            }
747
748            if(( tmp = tr_bencDictFind( &benc, "warning message" ))) {
749                dbgmsg( t, "got warning message [%s]", tmp->val.s.s );
750                publishWarning( t, tmp->val.s.s );
751            }
752
753            if(( tmp = tr_bencDictFind( &benc, "interval" ))) {
754                dbgmsg( t, "setting interval to %d", tmp->val.i );
755                t->announceIntervalSec = tmp->val.i;
756            }
757
758            if(( tmp = tr_bencDictFind( &benc, "min interval" ))) {
759                dbgmsg( t, "setting min interval to %d", tmp->val.i );
760                t->announceMinIntervalSec = tmp->val.i;
761            }
762
763            if(( tmp = tr_bencDictFind( &benc, "tracker id" )))
764                t->trackerID = tr_strndup( tmp->val.s.s, tmp->val.s.i );
765
766            if(( tmp = tr_bencDictFind( &benc, "complete" )))
767                t->seederCount = tmp->val.i;
768
769            if(( tmp = tr_bencDictFind( &benc, "incomplete" )))
770                t->leecherCount = tmp->val.i;
771
772            if(( tmp = tr_bencDictFind( &benc, "peers" )))
773            {
774                int peerCount = 0;
775                uint8_t * peerCompact = NULL;
776
777                if( tmp->type == TYPE_LIST ) /* original protocol */
778                {
779                    if( tmp->val.l.count > 0 )
780                        peerCompact = parseOldPeers( tmp, &peerCount );
781                }
782                else if( tmp->type == TYPE_STR ) /* "compact" extension */
783                {
784                    if( tmp->val.s.i >= 6 )
785                    {
786                        peerCount = tmp->val.s.i / 6;
787                        peerCompact = tr_new( uint8_t, tmp->val.s.i );
788                        memcpy( peerCompact, tmp->val.s.s, tmp->val.s.i );
789                    }
790                }
791
792                publishNewPeers( t, peerCount, peerCompact );
793                tr_free( peerCompact );
794            }
795        }
796
797        if( bencLoaded )
798            tr_bencFree( &benc );
799    }
800    else
801    {
802        tr_inf( "Bad response for torrent '%s' on request '%s' "
803                "... trying again in 30 seconds",
804                t->name, t->lastRequest );
805
806        err = 1;
807    }
808
809    if (( warning = updateAddresses( t, req ) )) {
810        publishWarning( t, warning );
811        tr_err( warning );
812    }
813
814    /**
815    ***
816    **/
817
818    responseCode = req ? req->response_code : 503;
819
820    if( 200<=responseCode && responseCode<=299 )
821    {
822        dbgmsg( t, "request succeeded. reannouncing in %d seconds",
823                   t->announceIntervalSec );
824        t->manualAnnounceAllowedAt = time(NULL)
825                                   + t->announceMinIntervalSec;
826        t->reannounceTimer = tr_timerNew( t->handle,
827                                          onReannounce, t,
828                                          t->announceIntervalSec * 1000 );
829    }
830    else if( 300<=responseCode && responseCode<=399 )
831    {
832        dbgmsg( t, "got a redirect; retrying immediately" );
833
834        /* it's a redirect... updateAddresses() has already
835         * parsed the redirect, all that's left is to retry */
836        onRetry( t );
837    }
838    else if( 400<=responseCode && responseCode<=499 )
839    {
840        dbgmsg( t, "got a 4xx error." );
841
842        /* The request could not be understood by the server due to
843         * malformed syntax. The client SHOULD NOT repeat the
844         * request without modifications. */
845        if( req && req->response_code_line )
846            publishErrorMessage( t, req->response_code_line );
847        t->manualAnnounceAllowedAt = ~(time_t)0;
848        t->reannounceTimer = NULL;
849    }
850    else if( 500<=responseCode && responseCode<=599 )
851    {
852        dbgmsg( t, "got a 5xx error... retrying in 15 seconds." );
853
854        /* Response status codes beginning with the digit "5" indicate
855         * cases in which the server is aware that it has erred or is
856         * incapable of performing the request.  So we pause a bit and
857         * try again. */
858        if( req && req->response_code_line )
859            publishWarning( t, req->response_code_line );
860        t->manualAnnounceAllowedAt = ~(time_t)0;
861        t->reannounceTimer = tr_timerNew( t->handle, onRetry, t, 15 * 1000 );
862    }
863    else
864    {
865        dbgmsg( t, "unhandled condition... retrying in 120 seconds." );
866
867        /* WTF did we get?? */
868        if( req && req->response_code_line )
869            publishErrorMessage( t, req->response_code_line );
870        t->manualAnnounceAllowedAt = ~(time_t)0;
871        t->reannounceTimer = tr_timerNew( t->handle, onRetry, t, 120 * 1000 );
872    }
873}
874
875static int
876sendTrackerRequest( void * vt, const char * eventName )
877{
878    tr_tracker * t = vt;
879    const int isStopping = eventName && !strcmp( eventName, "stopped" );
880    const tr_tracker_info * address = getCurrentAddress( t );
881    char * uri;
882    struct evhttp_connection * evcon;
883    const tr_torrent * tor;
884
885    tor = tr_torrentFindFromHash( t->handle, t->hash );
886    if( tor == NULL )
887        return FALSE;   
888
889    uri = buildTrackerRequestURI( t, tor, eventName );
890
891    tr_inf( "Torrent \"%s\" sending '%s' to tracker %s:%d: %s",
892            t->name,
893            (eventName ? eventName : "periodic announce"),
894            address->address, address->port,
895            uri );
896
897    /* kill any pending requests */
898    dbgmsg( t, "clearing announce timer" );
899    tr_timerFree( &t->reannounceTimer );
900
901    evcon = getConnection( t, address->address, address->port );
902    if ( !evcon ) {
903        tr_err( "Can't make a connection to %s:%d", address->address, address->port );
904        tr_free( uri );
905    } else {
906        struct evhttp_request * req;
907        tr_free( t->lastRequest );
908        t->lastRequest = tr_strdup( eventName );
909        if( isStopping ) {
910            evhttp_connection_set_timeout( evcon, STOP_TIMEOUT_INTERVAL_SEC );
911            req = evhttp_request_new( onStoppedResponse, t->handle );
912        } else {
913            evhttp_connection_set_timeout( evcon, TIMEOUT_INTERVAL_SEC );
914            req = evhttp_request_new( onTrackerResponse, torrentHashNew(t) );
915        }
916        dbgmsg( t, "sending \"%s\" request to tracker", eventName ? eventName : "reannounce" );
917
918        addCommonHeaders( t, req );
919        tr_evhttp_make_request( t->handle, evcon,
920                                req, EVHTTP_REQ_GET, uri );
921    }
922
923    return FALSE;
924}
925
926static int
927onReannounce( void * vt )
928{
929    tr_tracker * t = vt;
930    dbgmsg( t, "onReannounce" );
931    sendTrackerRequest( t, "" );
932    dbgmsg( t, "onReannounce setting announceTimer to NULL" );
933    t->reannounceTimer = NULL;
934    return FALSE;
935}
936
937static int
938onRetry( void * vt )
939{
940    tr_tracker * t = vt;
941    dbgmsg( t, "onRetry" );
942    sendTrackerRequest( t, t->lastRequest );
943    dbgmsg( t, "onRetry setting announceTimer to NULL" );
944    t->reannounceTimer = NULL;
945    return FALSE;
946}
947
948/***
949****  PUBLIC
950***/
951
952tr_publisher_tag
953tr_trackerSubscribe( tr_tracker          * t,
954                     tr_delivery_func      func,
955                     void                * user_data )
956{
957    return tr_publisherSubscribe( t->publisher, func, user_data );
958}
959
960void
961tr_trackerUnsubscribe( tr_tracker        * t,
962                       tr_publisher_tag    tag )
963{
964    tr_publisherUnsubscribe( t->publisher, tag );
965}
966
967const tr_tracker_info *
968tr_trackerGetAddress( const tr_tracker   * t )
969{
970    return getCurrentAddress( t );
971}
972
973int
974tr_trackerCanManualAnnounce ( const tr_tracker * t)
975{
976    return t->isRunning
977            && ( time(NULL) >= t->manualAnnounceAllowedAt );
978}
979
980void
981tr_trackerGetCounts( const tr_tracker  * t,
982                     int               * setme_completedCount,
983                     int               * setme_leecherCount,
984                     int               * setme_seederCount )
985{
986    if( setme_completedCount )
987       *setme_completedCount = t->timesDownloaded;
988
989    if( setme_leecherCount )
990       *setme_leecherCount = t->leecherCount;
991
992    if( setme_seederCount )
993       *setme_seederCount = t->seederCount;
994}
995
996void
997tr_trackerStart( tr_tracker * t )
998{
999    tr_peerIdNew( t->peer_id, sizeof(t->peer_id) );
1000
1001    if( !t->reannounceTimer && !t->isRunning )
1002    {
1003        t->isRunning = 1;
1004        sendTrackerRequest( t, "started" );
1005    }
1006}
1007
1008void
1009tr_trackerReannounce( tr_tracker * t )
1010{
1011    sendTrackerRequest( t, "started" );
1012}
1013
1014void
1015tr_trackerCompleted( tr_tracker * t )
1016{
1017    sendTrackerRequest( t, "completed" );
1018}
1019
1020void
1021tr_trackerStop( tr_tracker * t )
1022{
1023    if( t->isRunning )
1024    {
1025        t->isRunning = 0;
1026        sendTrackerRequest( t, "stopped" );
1027    }
1028}
1029
1030void
1031tr_trackerChangeMyPort( tr_tracker * t )
1032{
1033    if( t->isRunning )
1034        tr_trackerReannounce( t );
1035}
Note: See TracBrowser for help on using the repository browser.