source: trunk/libtransmission/announcer.c @ 10741

Last change on this file since 10741 was 10741, checked in by charles, 12 years ago

(trunk libT) when TR_CURL_VERBOSE is set, also log the benc announce response

File size: 59.9 KB
Line 
1/*
2 * This file Copyright (C) 2009-2010 Mnemosyne LLC
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:$
11 */
12
13#include <assert.h>
14#include <limits.h>
15
16#include <event.h>
17#include <evhttp.h> /* for HTTP_OK */
18
19#include "transmission.h"
20#include "announcer.h"
21#include "crypto.h"
22#include "net.h"
23#include "ptrarray.h"
24#include "publish.h"
25#include "session.h"
26#include "tr-dht.h"
27#include "tr-lpd.h"
28#include "torrent.h"
29#include "utils.h"
30#include "web.h"
31
32#define STARTED "started"
33
34#define dbgmsg( tier, ... ) \
35if( tr_deepLoggingIsActive( ) ) do { \
36  char name[128]; \
37  tr_snprintf( name, sizeof( name ), "[%s--%s]", tr_torrentName( tier->tor ), \
38      ( tier->currentTracker ? tier->currentTracker->host->name : "" ) ); \
39  tr_deepLog( __FILE__, __LINE__, name, __VA_ARGS__ ); \
40} while( 0 )
41
42enum
43{
44    /* unless the tracker says otherwise, rescrape this frequently */
45    DEFAULT_SCRAPE_INTERVAL_SEC = ( 60 * 30 ),
46
47    /* unless the tracker says otherwise, this is the announce interval */
48    DEFAULT_ANNOUNCE_INTERVAL_SEC = ( 60 * 10 ),
49
50    /* unless the tracker says otherwise, this is the announce min_interval */
51    DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = ( 60 * 2 ),
52
53    /* the length of the 'key' argument passed in tracker requests */
54    KEYLEN = 8,
55
56    /* how many web tasks we allow at one time */
57    MAX_CONCURRENT_TASKS = 48,
58
59    /* if a tracker takes more than this long to respond,
60     * we treat it as nonresponsive */
61    MAX_TRACKER_RESPONSE_TIME_SECS = ( 60 * 2 ),
62
63    /* the value of the 'numwant' argument passed in tracker requests. */
64    NUMWANT = 80,
65
66    /* how long to put slow (nonresponsive) trackers in the penalty box */
67    SLOW_HOST_PENALTY_SECS = ( 60 * 10 ),
68
69    UPKEEP_INTERVAL_SECS = 1,
70
71    /* this is an upper limit for the frequency of LDS announces */
72    LPD_HOUSEKEEPING_INTERVAL_SECS = 30
73
74};
75
76/***
77****
78***/
79
80static int
81compareTransfer( int a_uploaded, int a_downloaded,
82                 int b_uploaded, int b_downloaded )
83{
84    /* higher upload count goes first */
85    if( a_uploaded != b_uploaded )
86        return a_uploaded > b_uploaded ? -1 : 1;
87
88    /* then higher download count goes first */
89    if( a_downloaded != b_downloaded )
90        return a_downloaded > b_downloaded ? -1 : 1;
91
92    return 0;
93}
94
95/***
96****
97***/
98
99/**
100 * used by tr_announcer to recognize nonresponsive
101 * trackers and de-prioritize them
102 */
103typedef struct
104{
105    char * name;
106
107    /* how many seconds it took to get the last tracker response */
108    int lastResponseInterval;
109
110    /* the last time we sent an announce or scrape message */
111    time_t lastRequestTime;
112
113    /* the last successful announce/scrape time for this host */
114    time_t lastSuccessfulRequest;
115}
116tr_host;
117
118static int
119compareHosts( const void * va, const void * vb )
120{
121    const tr_host * a = va;
122    const tr_host * b = vb;
123    return strcmp( a->name, b->name );
124}
125
126static int
127compareHostToName( const void * va, const void * vb )
128{
129    const tr_host * a = va;
130    return strcmp( a->name, vb );
131}
132
133/* format: hostname + ':' + port */
134static char *
135getHostName( const char * url )
136{
137    int port = 0;
138    char * host = NULL;
139    char * ret;
140    tr_urlParse( url, strlen( url ), NULL, &host, &port, NULL );
141    ret = tr_strdup_printf( "%s:%d", ( host ? host : "invalid" ), port );
142    tr_free( host );
143    return ret;
144}
145
146static tr_host*
147hostNew( const char * name )
148{
149    tr_host * host = tr_new0( tr_host, 1 );
150    host->name = tr_strdup( name );
151    return host;
152}
153
154static void
155hostFree( void * vhost )
156{
157    tr_host * host = vhost;
158
159    tr_free( host->name );
160    tr_free( host );
161}
162
163/***
164****
165***/
166
167/**
168 * Since we can't poll a tr_torrent's fields after it's destroyed,
169 * we pre-build the "stop" announcement message when a torrent
170 * is removed from Transmission
171 */
172struct stop_message
173{
174    tr_host * host;
175    char * url;
176    int up;
177    int down;
178};
179
180static void
181stopFree( struct stop_message * stop )
182{
183    tr_free( stop->url );
184    tr_free( stop );
185}
186
187static int
188compareStops( const void * va, const void * vb )
189{
190    const struct stop_message * a = va;
191    const struct stop_message * b = vb;
192    return compareTransfer( a->up, a->down, b->up, b->down);
193}
194
195/***
196****
197***/
198
199/**
200 * "global" (per-tr_session) fields
201 */
202typedef struct tr_announcer
203{
204    tr_ptrArray hosts; /* tr_host */
205    tr_ptrArray stops; /* struct stop_message */
206    tr_session * session;
207    struct event * upkeepTimer;
208    int slotsAvailable;
209    time_t lpdHouseKeepingAt;
210}
211tr_announcer;
212
213tr_bool
214tr_announcerHasBacklog( const struct tr_announcer * announcer )
215{
216    return announcer->slotsAvailable < 1;
217}
218
219static tr_host *
220getHost( tr_announcer * announcer, const char * url )
221{
222    char * name = getHostName( url );
223    tr_host * host;
224
225    host = tr_ptrArrayFindSorted( &announcer->hosts, name, compareHostToName );
226    if( host == NULL )
227    {
228        host = hostNew( name );
229        tr_ptrArrayInsertSorted( &announcer->hosts, host, compareHosts );
230    }
231
232    tr_free( name );
233    return host;
234}
235
236static void
237onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer );
238
239static inline time_t
240calcRescheduleWithJitter( const int minPeriod )
241{
242    const double jitterFac = 0.1;
243
244    assert( minPeriod > 0 );
245
246    return tr_time()
247        + minPeriod
248        + tr_cryptoWeakRandInt( (int) ( minPeriod * jitterFac ) + 1 );
249}
250
251void
252tr_announcerInit( tr_session * session )
253{
254    tr_announcer * a;
255
256    const time_t relaxUntil =
257        calcRescheduleWithJitter( LPD_HOUSEKEEPING_INTERVAL_SECS / 3 );
258
259    assert( tr_isSession( session ) );
260
261    a = tr_new0( tr_announcer, 1 );
262    a->hosts = TR_PTR_ARRAY_INIT;
263    a->stops = TR_PTR_ARRAY_INIT;
264    a->session = session;
265    a->slotsAvailable = MAX_CONCURRENT_TASKS;
266    a->lpdHouseKeepingAt = relaxUntil;
267    a->upkeepTimer = tr_new0( struct event, 1 );
268    evtimer_set( a->upkeepTimer, onUpkeepTimer, a );
269    tr_timerAdd( a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
270
271    session->announcer = a;
272}
273
274static void flushCloseMessages( tr_announcer * announcer );
275
276void
277tr_announcerClose( tr_session * session )
278{
279    tr_announcer * announcer = session->announcer;
280
281    flushCloseMessages( announcer );
282
283    evtimer_del( announcer->upkeepTimer );
284    tr_free( announcer->upkeepTimer );
285    announcer->upkeepTimer = NULL;
286
287    tr_ptrArrayDestruct( &announcer->stops, NULL );
288    tr_ptrArrayDestruct( &announcer->hosts, hostFree );
289
290    session->announcer = NULL;
291    tr_free( announcer );
292}
293
294/***
295****
296***/
297
298/* a row in tr_tier's list of trackers */
299typedef struct
300{
301    tr_host * host;
302
303    char * announce;
304    char * scrape;
305
306    char * tracker_id;
307
308    int seederCount;
309    int leecherCount;
310    int downloadCount;
311    int downloaderCount;
312
313    uint32_t id;
314
315    /* sent as the "key" argument in tracker requests
316     * to verify us if our IP address changes.
317     * This is immutable for the life of the tracker object.
318     * The +1 is for '\0' */
319    char key_param[KEYLEN + 1];
320}
321tr_tracker_item;
322
323static void
324trackerItemCopyAttributes( tr_tracker_item * t, const tr_tracker_item * o )
325{
326    assert( t != o );
327    assert( t != NULL );
328    assert( o != NULL );
329
330    t->seederCount = o->seederCount;
331    t->leecherCount = o->leecherCount;
332    t->downloadCount = o->downloadCount;
333    t->downloaderCount = o->downloaderCount;
334    memcpy( t->key_param, o->key_param, sizeof( t->key_param ) );
335}
336
337static void
338generateKeyParam( char * msg, size_t msglen )
339{
340    size_t i;
341    const char * pool = "abcdefghijklmnopqrstuvwxyz0123456789";
342    const int poolSize = strlen( pool );
343
344    for( i=0; i<msglen; ++i )
345        *msg++ = pool[tr_cryptoRandInt( poolSize )];
346    *msg = '\0';
347}
348
349static tr_tracker_item*
350trackerNew( tr_announcer  * announcer,
351            const char    * announce,
352            const char    * scrape,
353            uint32_t        id )
354{
355    tr_tracker_item * tracker = tr_new0( tr_tracker_item, 1  );
356    tracker->host = getHost( announcer, announce );
357    tracker->announce = tr_strdup( announce );
358    tracker->scrape = tr_strdup( scrape );
359    tracker->id = id;
360    generateKeyParam( tracker->key_param, KEYLEN );
361    tracker->seederCount = -1;
362    tracker->leecherCount = -1;
363    tracker->downloadCount = -1;
364    return tracker;
365}
366
367static void
368trackerFree( void * vtracker )
369{
370    tr_tracker_item * tracker = vtracker;
371
372    tr_free( tracker->tracker_id );
373    tr_free( tracker->announce );
374    tr_free( tracker->scrape );
375    tr_free( tracker );
376}
377
378/***
379****
380***/
381
382struct tr_torrent_tiers;
383
384/** @brief A group of trackers in a single tier, as per the multitracker spec */
385typedef struct
386{
387    /* number of up/down/corrupt bytes since the last time we sent an
388     * "event=stopped" message that was acknowledged by the tracker */
389    uint64_t byteCounts[3];
390
391    tr_ptrArray trackers; /* tr_tracker_item */
392    tr_tracker_item * currentTracker;
393    int currentTrackerIndex;
394
395    tr_torrent * tor;
396
397    time_t scrapeAt;
398    time_t lastScrapeStartTime;
399    time_t lastScrapeTime;
400    tr_bool lastScrapeSucceeded;
401    tr_bool lastScrapeTimedOut;
402
403    time_t announceAt;
404    time_t manualAnnounceAllowedAt;
405    time_t lastAnnounceStartTime;
406    time_t lastAnnounceTime;
407    tr_bool lastAnnounceSucceeded;
408    tr_bool lastAnnounceTimedOut;
409
410    tr_ptrArray announceEvents; /* const char* */
411
412    /* unique lookup key */
413    int key;
414
415    int scrapeIntervalSec;
416    int announceIntervalSec;
417    int announceMinIntervalSec;
418
419    int lastAnnouncePeerCount;
420
421    tr_bool isRunning;
422    tr_bool isAnnouncing;
423    tr_bool isScraping;
424    tr_bool wasCopied;
425
426    char lastAnnounceStr[128];
427    char lastScrapeStr[128];
428}
429tr_tier;
430
431static tr_tier *
432tierNew( tr_torrent * tor )
433{
434    tr_tier * t;
435    static int nextKey = 1;
436    const time_t now = tr_time( );
437
438    t = tr_new0( tr_tier, 1 );
439    t->key = nextKey++;
440    t->announceEvents = TR_PTR_ARRAY_INIT;
441    t->trackers = TR_PTR_ARRAY_INIT;
442    t->currentTracker = NULL;
443    t->currentTrackerIndex = -1;
444    t->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
445    t->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
446    t->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
447    t->scrapeAt = now + tr_cryptoWeakRandInt( 60*5 );
448    t->tor = tor;
449
450    return t;
451}
452
453static void
454tierFree( void * vtier )
455{
456    tr_tier * tier = vtier;
457    tr_ptrArrayDestruct( &tier->trackers, trackerFree );
458    tr_ptrArrayDestruct( &tier->announceEvents, NULL );
459    tr_free( tier );
460}
461
462static void
463tierCopyAttributes( tr_tier * t, const tr_tier * o )
464{
465    int i, n;
466    tr_tier bak;
467
468    assert( t != NULL );
469    assert( o != NULL );
470    assert( t != o );
471
472    bak = *t;
473    *t = *o;
474    t->tor = bak.tor;
475    t->trackers = bak.trackers;
476    t->announceEvents = bak.announceEvents;
477    t->currentTracker = bak.currentTracker;
478    t->currentTrackerIndex = bak.currentTrackerIndex;
479
480    tr_ptrArrayClear( &t->announceEvents );
481    for( i=0, n=tr_ptrArraySize(&o->announceEvents); i<n; ++i )
482        tr_ptrArrayAppend( &t->announceEvents, tr_ptrArrayNth((tr_ptrArray*)&o->announceEvents,i) );
483}
484
485static void
486tierIncrementTracker( tr_tier * tier )
487{
488    /* move our index to the next tracker in the tier */
489    const int i = ( tier->currentTrackerIndex + 1 )
490                        % tr_ptrArraySize( &tier->trackers );
491    tier->currentTracker = tr_ptrArrayNth( &tier->trackers, i );
492    tier->currentTrackerIndex = i;
493
494    /* reset some of the tier's fields */
495    tier->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
496    tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
497    tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
498    tier->isAnnouncing = FALSE;
499    tier->isScraping = FALSE;
500    tier->lastAnnounceStartTime = 0;
501    tier->lastScrapeStartTime = 0;
502}
503
504static void
505tierAddTracker( tr_announcer * announcer,
506                tr_tier      * tier,
507                const char   * announce,
508                const char   * scrape,
509                uint32_t       id )
510{
511    tr_tracker_item * tracker = trackerNew( announcer, announce, scrape, id );
512
513    tr_ptrArrayAppend( &tier->trackers, tracker );
514    dbgmsg( tier, "adding tracker %s", announce );
515
516    if( !tier->currentTracker )
517        tierIncrementTracker( tier );
518}
519
520
521/***
522****
523***/
524
525/**
526 * @brief Opaque, per-torrent data structure for tracker announce information
527 *
528 * this opaque data structure can be found in tr_torrent.tiers
529 */
530typedef struct tr_torrent_tiers
531{
532    tr_ptrArray tiers;
533    tr_publisher publisher;
534}
535tr_torrent_tiers;
536
537static tr_torrent_tiers*
538tiersNew( void )
539{
540    tr_torrent_tiers * tiers = tr_new0( tr_torrent_tiers, 1 );
541    tiers->tiers = TR_PTR_ARRAY_INIT;
542    tiers->publisher = TR_PUBLISHER_INIT;
543    return tiers;
544}
545
546static void
547tiersFree( tr_torrent_tiers * tiers )
548{
549    tr_publisherDestruct( &tiers->publisher );
550    tr_ptrArrayDestruct( &tiers->tiers, tierFree );
551    tr_free( tiers );
552}
553
554static tr_tier*
555getTier( tr_announcer * announcer, int torrentId, int tierId )
556{
557    tr_tier * tier = NULL;
558
559    if( announcer )
560    {
561        tr_torrent * tor = tr_torrentFindFromId( announcer->session, torrentId );
562
563        if( tor && tor->tiers )
564        {
565            int i;
566            tr_ptrArray * tiers = &tor->tiers->tiers;
567            const int n = tr_ptrArraySize( tiers );
568            for( i=0; !tier && i<n; ++i )
569            {
570                tr_tier * tmp = tr_ptrArrayNth( tiers, i );
571                if( tmp->key == tierId )
572                    tier = tmp;
573            }
574        }
575    }
576
577    return tier;
578}
579
580/***
581****  PUBLISH
582***/
583
584static const tr_tracker_event emptyEvent = { 0, NULL, NULL, NULL, 0, 0 };
585
586static void
587publishMessage( tr_tier * tier, const char * msg, int type )
588{
589    if( tier && tier->tor && tier->tor->tiers )
590    {
591        tr_torrent_tiers * tiers = tier->tor->tiers;
592        tr_tracker_event event = emptyEvent;
593        event.messageType = type;
594        event.text = msg;
595        event.tracker = tier->currentTracker ? tier->currentTracker->announce : NULL;
596        tr_publisherPublish( &tiers->publisher, tier, &event );
597    }
598}
599
600static void
601publishErrorClear( tr_tier * tier )
602{
603    publishMessage( tier, NULL, TR_TRACKER_ERROR_CLEAR );
604}
605
606static void
607publishErrorMessageAndStop( tr_tier * tier, const char * msg )
608{
609    tier->isRunning = FALSE;
610
611    publishMessage( tier, msg, TR_TRACKER_ERROR );
612}
613
614static void
615publishWarning( tr_tier * tier, const char * msg )
616{
617    publishMessage( tier, msg, TR_TRACKER_WARNING );
618}
619
620static int
621getSeedProbability( int seeds, int leechers )
622{
623    if( !seeds )
624        return 0;
625
626    if( seeds>=0 && leechers>=0 )
627        return (int)((100.0*seeds)/(seeds+leechers));
628
629    return -1; /* unknown */
630}
631
632static int
633publishNewPeers( tr_tier * tier, int seeds, int leechers,
634                 const void * compact, int compactLen )
635{
636    tr_tracker_event e = emptyEvent;
637
638    e.messageType = TR_TRACKER_PEERS;
639    e.seedProbability = getSeedProbability( seeds, leechers );
640    e.compact = compact;
641    e.compactLen = compactLen;
642
643    tr_publisherPublish( &tier->tor->tiers->publisher, tier, &e );
644
645    return compactLen / 6;
646}
647
648static int
649publishNewPeersCompact( tr_tier * tier, int seeds, int leechers,
650                        const void * compact, int compactLen )
651{
652    int i;
653    const uint8_t *compactWalk;
654    uint8_t *array, *walk;
655    const int peerCount = compactLen / 6;
656    const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
657    tr_address addr;
658    tr_port port;
659
660    addr.type = TR_AF_INET;
661    memset( &addr.addr, 0, sizeof( addr.addr ) );
662    array = tr_new( uint8_t, arrayLen );
663    for ( i=0, walk=array, compactWalk=compact ; i<peerCount ; ++i )
664    {
665        memcpy( &addr.addr.addr4, compactWalk, 4 );
666        memcpy( &port, compactWalk + 4, 2 );
667
668        memcpy( walk, &addr, sizeof( addr ) );
669        memcpy( walk + sizeof( addr ), &port, 2 );
670
671        walk += sizeof( tr_address ) + 2;
672        compactWalk += 6;
673    }
674
675    publishNewPeers( tier, seeds, leechers, array, arrayLen );
676
677    tr_free( array );
678
679    return peerCount;
680}
681
682static int
683publishNewPeersCompact6( tr_tier * tier, int seeds, int leechers,
684                         const void * compact, int compactLen )
685{
686    int i;
687    const uint8_t *compactWalk;
688    uint8_t *array, *walk;
689    const int peerCount = compactLen / 18;
690    const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
691    tr_address addr;
692    tr_port port;
693
694    addr.type = TR_AF_INET6;
695    memset( &addr.addr, 0, sizeof( addr.addr ) );
696    array = tr_new( uint8_t, arrayLen );
697    for ( i = 0, walk = array, compactWalk = compact ; i < peerCount ; ++i )
698    {
699        memcpy( &addr.addr.addr6, compactWalk, 16 );
700        memcpy( &port, compactWalk + 16, 2 );
701        compactWalk += 18;
702
703        memcpy( walk, &addr, sizeof( addr ) );
704        memcpy( walk + sizeof( addr ), &port, 2 );
705        walk += sizeof( tr_address ) + 2;
706    }
707
708    publishNewPeers( tier, seeds, leechers, array, arrayLen );
709    tr_free( array );
710
711    return peerCount;
712}
713
714static char*
715createAnnounceURL( const tr_announcer     * announcer,
716                   const tr_torrent       * torrent,
717                   const tr_tier          * tier,
718                   const char             * eventName )
719{
720    const int isStopping = !strcmp( eventName, "stopped" );
721    const int numwant = isStopping ? 0 : NUMWANT;
722    const tr_tracker_item  * tracker = tier->currentTracker;
723    const char * ann = tracker->announce;
724    struct evbuffer * buf = evbuffer_new( );
725    char * ret;
726    const char * str;
727    const unsigned char * ipv6;
728
729    evbuffer_expand( buf, 2048 );
730
731    evbuffer_add_printf( buf, "%s"
732                              "%c"
733                              "info_hash=%s"
734                              "&peer_id=%s"
735                              "&port=%d"
736                              "&uploaded=%" PRIu64
737                              "&downloaded=%" PRIu64
738                              "&left=%" PRIu64
739                              "&numwant=%d"
740                              "&key=%s"
741                              "&compact=1"
742                              "&supportcrypto=1",
743                              ann,
744                              strchr( ann, '?' ) ? '&' : '?',
745                              torrent->info.hashEscaped,
746                              torrent->peer_id,
747                              (int)tr_sessionGetPeerPort( announcer->session ),
748                              tier->byteCounts[TR_ANN_UP],
749                              tier->byteCounts[TR_ANN_DOWN],
750                              tr_cpLeftUntilComplete( &torrent->completion ),
751                              numwant,
752                              tracker->key_param );
753
754    if( announcer->session->encryptionMode == TR_ENCRYPTION_REQUIRED )
755        evbuffer_add_printf( buf, "&requirecrypto=1" );
756
757    if( tier->byteCounts[TR_ANN_CORRUPT] )
758        evbuffer_add_printf( buf, "&corrupt=%" PRIu64, tier->byteCounts[TR_ANN_CORRUPT] );
759
760    str = eventName;
761    if( str && *str )
762        evbuffer_add_printf( buf, "&event=%s", str );
763
764    str = tracker->tracker_id;
765    if( str && *str )
766        evbuffer_add_printf( buf, "&trackerid=%s", str );
767
768    /* There are two incompatible techniques for announcing an IPv6 address.
769       BEP-7 suggests adding an "ipv6=" parameter to the announce URL,
770       while OpenTracker requires that peers announce twice, once over IPv4
771       and once over IPv6.
772
773       To be safe, we should do both: add the "ipv6=" parameter and
774       announce twice.  At any rate, we're already computing our IPv6
775       address (for the LTEP handshake), so this comes for free. */
776
777    ipv6 = tr_globalIPv6( );
778    if( ipv6 ) {
779        char ipv6_readable[INET6_ADDRSTRLEN];
780        inet_ntop( AF_INET6, ipv6, ipv6_readable, INET6_ADDRSTRLEN );
781        evbuffer_add_printf( buf, "&ipv6=");
782        tr_http_escape( buf, ipv6_readable, strlen(ipv6_readable), TRUE );
783    }
784
785    ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
786    dbgmsg( tier, "announce URL is \"%s\"", ret );
787    evbuffer_free( buf );
788    return ret;
789}
790
791
792/***
793****
794***/
795
796static tr_bool
797announceURLIsSupported( const char * announce )
798{
799    return ( announce != NULL )
800        && ( ( strstr( announce, "http://" ) == announce ) ||
801             ( strstr( announce, "https://" ) == announce ) );
802}
803
804static void
805addTorrentToTier( tr_announcer * announcer, tr_torrent_tiers * tiers, tr_torrent * tor )
806{
807    int i, n;
808    const tr_tracker_info ** infos;
809    const int trackerCount = tor->info.trackerCount;
810    const tr_tracker_info  * trackers = tor->info.trackers;
811
812    /* get the trackers that we support... */
813    infos = tr_new0( const tr_tracker_info*, trackerCount );
814    for( i=n=0; i<trackerCount; ++i )
815        if( announceURLIsSupported( trackers[i].announce ) )
816            infos[n++] = &trackers[i];
817
818    /* build our private table of tiers... */
819    if( n > 0 )
820    {
821        int tierIndex = -1;
822        tr_tier * tier = NULL;
823
824        for( i=0; i<n; ++i )
825        {
826            const tr_tracker_info * info = infos[i];
827
828            if( info->tier != tierIndex )
829                tier = NULL;
830
831            tierIndex = info->tier;
832
833            if( tier == NULL ) {
834                tier = tierNew( tor );
835                dbgmsg( tier, "adding tier" );
836                tr_ptrArrayAppend( &tiers->tiers, tier );
837            }
838
839            tierAddTracker( announcer, tier, info->announce, info->scrape, info->id );
840        }
841    }
842
843    tr_free( infos );
844}
845
846tr_torrent_tiers *
847tr_announcerAddTorrent( tr_announcer * announcer, tr_torrent * tor )
848{
849    tr_torrent_tiers * tiers;
850
851    assert( announcer != NULL );
852    assert( tr_isTorrent( tor ) );
853
854    tiers = tiersNew( );
855
856    addTorrentToTier( announcer, tiers, tor );
857
858    return tiers;
859}
860
861static void
862tierAddAnnounce( tr_tier * tier, const char * announceEvent, time_t announceAt );
863
864void
865tr_announcerResetTorrent( tr_announcer * announcer, tr_torrent * tor )
866{
867    tr_ptrArray oldTiers = TR_PTR_ARRAY_INIT;
868
869    /* if we had tiers already, make a backup of them */
870    if( tor->tiers != NULL )
871    {
872        oldTiers = tor->tiers->tiers;
873        tor->tiers->tiers = TR_PTR_ARRAY_INIT;
874    }
875
876    /* create the new tier/tracker structs */
877    addTorrentToTier( announcer, tor->tiers, tor );
878
879    /* if we had tiers already, merge their state into the new structs */
880    if( !tr_ptrArrayEmpty( &oldTiers ) )
881    {
882        int i, in;
883        for( i=0, in=tr_ptrArraySize(&oldTiers); i<in; ++i )
884        {
885            int j, jn;
886            const tr_tier * o = tr_ptrArrayNth( &oldTiers, i );
887
888            if( o->currentTracker == NULL )
889                continue;
890
891            for( j=0, jn=tr_ptrArraySize(&tor->tiers->tiers); j<jn; ++j )
892            {
893                int k, kn;
894                tr_tier * t = tr_ptrArrayNth(&tor->tiers->tiers,j);
895
896                for( k=0, kn=tr_ptrArraySize(&t->trackers); k<kn; ++k )
897                {
898                    tr_tracker_item * item = tr_ptrArrayNth(&t->trackers,k);
899                    if( strcmp( o->currentTracker->announce, item->announce ) )
900                        continue;
901                    tierCopyAttributes( t, o );
902                    t->currentTracker = item;
903                    t->currentTrackerIndex = k;
904                    t->wasCopied = TRUE;
905                    trackerItemCopyAttributes( item, o->currentTracker );
906                    dbgmsg( t, "attributes copied to tier %d, tracker %d"
907                                               "from tier %d, tracker %d",
908                            i, o->currentTrackerIndex, j, k );
909
910                }
911            }
912        }
913    }
914
915    /* kickstart any tiers that didn't get started */
916    if( tor->isRunning )
917    {
918        int i, n;
919        const time_t now = tr_time( );
920        tr_tier ** tiers = (tr_tier**) tr_ptrArrayPeek( &tor->tiers->tiers, &n );
921        for( i=0; i<n; ++i ) {
922            tr_tier * tier = tiers[i];
923            if( !tier->wasCopied )
924                tierAddAnnounce( tier, STARTED, now );
925        }
926    }
927
928    /* cleanup */
929    tr_ptrArrayDestruct( &oldTiers, tierFree );
930}
931
932tr_publisher_tag
933tr_announcerSubscribe( struct tr_torrent_tiers   * tiers,
934                       tr_delivery_func            func,
935                       void                      * userData )
936{
937    return tr_publisherSubscribe( &tiers->publisher, func, userData );
938}
939
940void
941tr_announcerUnsubscribe( struct tr_torrent_tiers  * tiers,
942                         tr_publisher_tag           tag )
943{
944    if( tiers )
945        tr_publisherUnsubscribe( &tiers->publisher, tag );
946}
947
948static tr_bool
949tierCanManualAnnounce( const tr_tier * tier )
950{
951    return tier->isRunning
952        && tier->manualAnnounceAllowedAt <= tr_time( );
953}
954
955tr_bool
956tr_announcerCanManualAnnounce( const tr_torrent * tor )
957{
958    int i;
959    int n;
960    const tr_tier ** tiers;
961
962    assert( tr_isTorrent( tor ) );
963    assert( tor->tiers != NULL );
964
965    n = tr_ptrArraySize( &tor->tiers->tiers );
966    tiers = (const tr_tier**) tr_ptrArrayBase( &tor->tiers->tiers );
967    for( i=0; i<n; ++i )
968        if( tierCanManualAnnounce( tiers[i] ) )
969            return TRUE;
970
971    return FALSE;
972}
973
974time_t
975tr_announcerNextManualAnnounce( const tr_torrent * tor )
976{
977    int i;
978    int n;
979    const tr_torrent_tiers * tiers;
980    time_t ret = ~(time_t)0;
981
982    assert( tr_isTorrent( tor  ) );
983
984    tiers = tor->tiers;
985    n = tr_ptrArraySize( &tiers->tiers );
986    for( i=0; i<n; ++i ) {
987        tr_tier * tier = tr_ptrArrayNth( (tr_ptrArray*)&tiers->tiers, i );
988        if( tier->isRunning )
989            ret = MIN( ret, tier->manualAnnounceAllowedAt );
990    }
991
992    return ret;
993}
994
995static void
996tierAddAnnounce( tr_tier * tier, const char * announceEvent, time_t announceAt )
997{
998    assert( tier != NULL );
999    assert( announceEvent != NULL );
1000
1001    tr_ptrArrayAppend( &tier->announceEvents, (void*)announceEvent );
1002    tier->announceAt = announceAt;
1003
1004    dbgmsg( tier, "appended event \"%s\"; announcing in %d seconds\n", announceEvent, (int)difftime(announceAt,time(NULL)) );
1005}
1006
1007static void
1008torrentAddAnnounce( tr_torrent * tor, const char * announceEvent, time_t announceAt )
1009{
1010    int i;
1011    int n;
1012    tr_torrent_tiers * tiers;
1013
1014    assert( tr_isTorrent( tor ) );
1015
1016    tiers = tor->tiers;
1017    n = tr_ptrArraySize( &tiers->tiers );
1018    for( i=0; i<n; ++i )
1019        tierAddAnnounce( tr_ptrArrayNth( &tiers->tiers, i ), announceEvent, announceAt );
1020}
1021
1022void
1023tr_announcerTorrentStarted( tr_torrent * tor )
1024{
1025    torrentAddAnnounce( tor, STARTED, tr_time( ) );
1026}
1027void
1028tr_announcerManualAnnounce( tr_torrent * tor )
1029{
1030    torrentAddAnnounce( tor, "", tr_time( ) );
1031}
1032void
1033tr_announcerTorrentStopped( tr_torrent * tor )
1034{
1035    torrentAddAnnounce( tor, "stopped", tr_time( ) );
1036}
1037void
1038tr_announcerTorrentCompleted( tr_torrent * tor )
1039{
1040    torrentAddAnnounce( tor, "completed", tr_time( ) );
1041}
1042void
1043tr_announcerChangeMyPort( tr_torrent * tor )
1044{
1045    tr_announcerTorrentStarted( tor );
1046}
1047
1048/***
1049****
1050***/
1051
1052void
1053tr_announcerAddBytes( tr_torrent * tor, int type, uint32_t byteCount )
1054{
1055    int i, n;
1056    tr_torrent_tiers * tiers;
1057
1058    assert( tr_isTorrent( tor ) );
1059    assert( type==TR_ANN_UP || type==TR_ANN_DOWN || type==TR_ANN_CORRUPT );
1060
1061    tiers = tor->tiers;
1062    n = tr_ptrArraySize( &tiers->tiers );
1063    for( i=0; i<n; ++i )
1064    {
1065        tr_tier * tier = tr_ptrArrayNth( &tiers->tiers, i );
1066        tier->byteCounts[ type ] += byteCount;
1067    }
1068}
1069
1070/***
1071****
1072***/
1073
1074void
1075tr_announcerRemoveTorrent( tr_announcer * announcer, tr_torrent * tor )
1076{
1077    assert( tr_isTorrent( tor ) );
1078
1079    if( tor->tiers )
1080    {
1081        int i;
1082        const int n = tr_ptrArraySize( &tor->tiers->tiers );
1083        for( i=0; i<n; ++i )
1084        {
1085            tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
1086
1087            if( tier->isRunning )
1088            {
1089                struct stop_message * s = tr_new0( struct stop_message, 1 );
1090                s->up = tier->byteCounts[TR_ANN_UP];
1091                s->down = tier->byteCounts[TR_ANN_DOWN];
1092                s->url = createAnnounceURL( announcer, tor, tier, "stopped" );
1093                s->host = tier->currentTracker->host;
1094                tr_ptrArrayInsertSorted( &announcer->stops, s, compareStops );
1095            }
1096        }
1097
1098        tiersFree( tor->tiers );
1099        tor->tiers = NULL;
1100    }
1101}
1102
1103/* return true if (1) we've tried it recently AND (2) it didn't respond... */
1104static tr_bool
1105hostIsNotResponding( const tr_host * host, const time_t now )
1106{
1107    tr_bool b = ( host->lastRequestTime )
1108             && ( host->lastRequestTime >= ( now - SLOW_HOST_PENALTY_SECS ) )
1109             && ( host->lastResponseInterval > MAX_TRACKER_RESPONSE_TIME_SECS );
1110    return b;
1111}
1112
1113static tr_bool
1114tierIsNotResponding( const tr_tier * tier, const time_t now )
1115{
1116    return !tier->currentTracker
1117        || hostIsNotResponding( tier->currentTracker->host, now );
1118}
1119
1120static int
1121getRetryInterval( const tr_host * host )
1122{
1123    int interval;
1124    const int jitter = tr_cryptoWeakRandInt( 120 );
1125    const time_t timeSinceLastSuccess = tr_time() - host->lastSuccessfulRequest;
1126         if( timeSinceLastSuccess < 15*60 ) interval = 0;
1127    else if( timeSinceLastSuccess < 30*60 ) interval = 60*4;
1128    else if( timeSinceLastSuccess < 45*60 ) interval = 60*8;
1129    else if( timeSinceLastSuccess < 60*60 ) interval = 60*16;
1130    else                                    interval = 60*32;
1131    return interval + jitter;
1132}
1133
1134static int
1135compareTiers( const void * va, const void * vb )
1136{
1137    int ret = 0;
1138    tr_bool af, bf;
1139    const tr_tier * a = *(const tr_tier**)va;
1140    const tr_tier * b = *(const tr_tier**)vb;
1141
1142    /* working domains come before non-working */
1143    if( !ret ) {
1144        const time_t now = tr_time( );
1145        af = tierIsNotResponding( a, now );
1146        bf = tierIsNotResponding( b, now );
1147        if( af != bf )
1148            ret = !af ? -1 : 1;
1149    }
1150
1151    /* stops come before starts */
1152    if( !ret ) {
1153        af = a->tor->isRunning;
1154        bf = b->tor->isRunning;
1155        if( af != bf )
1156            ret = af ? 1 : -1;
1157    }
1158
1159    /* upload comes before download */
1160    if( !ret )
1161        ret = compareTransfer( a->byteCounts[TR_ANN_UP], a->byteCounts[TR_ANN_DOWN],
1162                               b->byteCounts[TR_ANN_UP], b->byteCounts[TR_ANN_DOWN] );
1163
1164    /* incomplete comes before complete */
1165    if( !ret ) {
1166        af = a->tor->completeness == TR_LEECH;
1167        bf = b->tor->completeness == TR_LEECH;
1168        if( af != bf )
1169            return af ? -1 : 1;
1170    }
1171
1172    /* private before public */
1173    if( !ret ) {
1174        af = tr_torrentIsPrivate( a->tor );
1175        bf = tr_torrentIsPrivate( b->tor );
1176        if( af != bf )
1177            ret = af ? -1 : 1;
1178    }
1179
1180    return ret;
1181}
1182
1183static uint8_t *
1184parseOldPeers( tr_benc * bePeers, size_t *  byteCount )
1185{
1186    int       i;
1187    uint8_t * array, *walk;
1188    const int peerCount = bePeers->val.l.count;
1189
1190    assert( tr_bencIsList( bePeers ) );
1191
1192    array = tr_new( uint8_t, peerCount * ( sizeof( tr_address ) + 2 ) );
1193
1194    for( i = 0, walk = array; i < peerCount; ++i )
1195    {
1196        const char * s;
1197        int64_t      itmp;
1198        tr_address   addr;
1199        tr_port      port;
1200        tr_benc    * peer = &bePeers->val.l.vals[i];
1201
1202        if( tr_bencDictFindStr( peer, "ip", &s ) )
1203            if( tr_pton( s, &addr ) == NULL )
1204                continue;
1205
1206        if( !tr_bencDictFindInt( peer, "port", &itmp )
1207                || itmp < 0
1208                || itmp > USHRT_MAX )
1209            continue;
1210
1211        memcpy( walk, &addr, sizeof( tr_address ) );
1212        port = htons( itmp );
1213        memcpy( walk + sizeof( tr_address ), &port, 2 );
1214        walk += sizeof( tr_address ) + 2;
1215    }
1216
1217    *byteCount = peerCount * sizeof( tr_address ) + 2;
1218    return array;
1219}
1220
1221static tr_bool
1222parseAnnounceResponse( tr_tier     * tier,
1223                       const char  * response,
1224                       size_t        responseLen,
1225                       tr_bool     * gotScrape )
1226{
1227    tr_benc benc;
1228    tr_bool success = FALSE;
1229    int scrapeFields = 0;
1230    const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
1231
1232    if( getenv( "TR_CURL_VERBOSE" ) != NULL )
1233    {
1234        char * str = tr_bencToStr( &benc, TR_FMT_JSON, NULL );
1235        fprintf( stderr, "Announce response:\n< %s\n", str );
1236        tr_free( str );
1237    }
1238
1239    dbgmsg( tier, "response len: %d, isBenc: %d", (int)responseLen, (int)bencLoaded );
1240    publishErrorClear( tier );
1241    if( bencLoaded && tr_bencIsDict( &benc ) )
1242    {
1243        int peerCount = 0;
1244        size_t rawlen;
1245        int64_t i;
1246        tr_benc * tmp;
1247        const char * str;
1248        const uint8_t * raw;
1249        tr_bool gotPeers = FALSE;
1250
1251        success = TRUE;
1252
1253        if( tr_bencDictFindStr( &benc, "failure reason", &str ) )
1254        {
1255            tr_strlcpy( tier->lastAnnounceStr, str,
1256                        sizeof( tier->lastAnnounceStr ) );
1257            dbgmsg( tier, "tracker gave \"%s\"", str );
1258            publishMessage( tier, str, TR_TRACKER_ERROR );
1259            success = FALSE;
1260        }
1261
1262        if( tr_bencDictFindStr( &benc, "warning message", &str ) )
1263        {
1264            tr_strlcpy( tier->lastAnnounceStr, str,
1265                        sizeof( tier->lastAnnounceStr ) );
1266            dbgmsg( tier, "tracker gave \"%s\"", str );
1267            publishWarning( tier, str );
1268        }
1269
1270        if( tr_bencDictFindInt( &benc, "interval", &i ) )
1271        {
1272            dbgmsg( tier, "setting interval to %d", (int)i );
1273            tier->announceIntervalSec = i;
1274        }
1275
1276        if( tr_bencDictFindInt( &benc, "min interval", &i ) )
1277        {
1278            dbgmsg( tier, "setting min interval to %d", (int)i );
1279            tier->announceMinIntervalSec = i;
1280        }
1281
1282        if( tr_bencDictFindStr( &benc, "tracker id", &str ) )
1283        {
1284            tier->currentTracker->tracker_id = tr_strdup( str );
1285        }
1286
1287        if( !tr_bencDictFindInt( &benc, "complete", &i ) )
1288            tier->currentTracker->seederCount = 0;
1289        else {
1290            ++scrapeFields;
1291            tier->currentTracker->seederCount = i;
1292        }
1293
1294        if( !tr_bencDictFindInt( &benc, "incomplete", &i ) )
1295            tier->currentTracker->leecherCount = 0;
1296        else {
1297            ++scrapeFields;
1298            tier->currentTracker->leecherCount = i;
1299        }
1300
1301        if( tr_bencDictFindInt( &benc, "downloaded", &i ) )
1302        {
1303            ++scrapeFields;
1304            tier->currentTracker->downloadCount = i;
1305        }
1306
1307        if( tr_bencDictFindRaw( &benc, "peers", &raw, &rawlen ) )
1308        {
1309            /* "compact" extension */
1310            const int seeders = tier->currentTracker->seederCount;
1311            const int leechers = tier->currentTracker->leecherCount;
1312            peerCount += publishNewPeersCompact( tier, seeders, leechers, raw, rawlen );
1313            gotPeers = TRUE;
1314        }
1315        else if( tr_bencDictFindList( &benc, "peers", &tmp ) )
1316        {
1317            /* original version of peers */
1318            const int seeders = tier->currentTracker->seederCount;
1319            const int leechers = tier->currentTracker->leecherCount;
1320            size_t byteCount = 0;
1321            uint8_t * array = parseOldPeers( tmp, &byteCount );
1322            peerCount += publishNewPeers( tier, seeders, leechers, array, byteCount );
1323            gotPeers = TRUE;
1324            tr_free( array );
1325        }
1326
1327        if( tr_bencDictFindRaw( &benc, "peers6", &raw, &rawlen ) )
1328        {
1329            /* "compact" extension */
1330            const int seeders = tier->currentTracker->seederCount;
1331            const int leechers = tier->currentTracker->leecherCount;
1332            peerCount += publishNewPeersCompact6( tier, seeders, leechers, raw, rawlen );
1333            gotPeers = TRUE;
1334        }
1335
1336        if( tier->lastAnnounceStr[0] == '\0' )
1337            tr_strlcpy( tier->lastAnnounceStr, _( "Success" ),
1338                        sizeof( tier->lastAnnounceStr ) );
1339
1340        if( gotPeers )
1341            tier->lastAnnouncePeerCount = peerCount;
1342    }
1343
1344    if( bencLoaded )
1345        tr_bencFree( &benc );
1346
1347    *gotScrape = scrapeFields >= 2;
1348
1349    return success;
1350}
1351
1352struct announce_data
1353{
1354    int torrentId;
1355    int tierId;
1356    time_t timeSent;
1357    const char * event;
1358
1359    /** If the request succeeds, the value for tier's "isRunning" flag */
1360    tr_bool isRunningOnSuccess;
1361};
1362
1363static void
1364onAnnounceDone( tr_session   * session,
1365                long           responseCode,
1366                const void   * response,
1367                size_t         responseLen,
1368                void         * vdata )
1369{
1370    tr_announcer * announcer = session->announcer;
1371    struct announce_data * data = vdata;
1372    tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
1373    tr_bool gotScrape = FALSE;
1374    tr_bool success = FALSE;
1375    const time_t now = time ( NULL );
1376    const char * announceEvent = data->event;
1377    const tr_bool isStopped = !strcmp( announceEvent, "stopped" );
1378
1379    if( announcer && tier )
1380    {
1381        if( tier->currentTracker->host )
1382        {
1383            tr_host * host = tier->currentTracker->host;
1384            host->lastRequestTime = data->timeSent;
1385            host->lastResponseInterval = now - data->timeSent;
1386        }
1387
1388        tier->lastAnnounceTime = now;
1389
1390        if( responseCode == HTTP_OK )
1391        {
1392            success = parseAnnounceResponse( tier, response, responseLen, &gotScrape );
1393            dbgmsg( tier, "success is %d", success );
1394
1395            if( isStopped )
1396            {
1397                /* now that we've successfully stopped the torrent,
1398                 * we can reset the up/down/corrupt count we've kept
1399                 * for this tracker */
1400                tier->byteCounts[ TR_ANN_UP ] = 0;
1401                tier->byteCounts[ TR_ANN_DOWN ] = 0;
1402                tier->byteCounts[ TR_ANN_CORRUPT ] = 0;
1403            }
1404        }
1405        else if( responseCode )
1406        {
1407            /* %1$ld - http status code, such as 404
1408             * %2$s - human-readable explanation of the http status code */
1409            char * buf = tr_strdup_printf(
1410                _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1411                responseCode,
1412                tr_webGetResponseStr( responseCode ) );
1413
1414            tr_strlcpy( tier->lastAnnounceStr, buf,
1415                        sizeof( tier->lastAnnounceStr ) );
1416
1417            /* if the response is serious, *and* if the response may require
1418             * human intervention, then notify the user... otherwise just log it */
1419            if( responseCode >= 400 )
1420                if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
1421                    publishWarning( tier, buf );
1422            tr_torinf( tier->tor, "%s", buf );
1423            dbgmsg( tier, "%s", buf );
1424
1425            tr_free( buf );
1426        }
1427        else
1428        {
1429            tr_strlcpy( tier->lastAnnounceStr,
1430                        _( "tracker did not respond" ),
1431                        sizeof( tier->lastAnnounceStr ) );
1432            dbgmsg( tier, "%s", tier->lastAnnounceStr );
1433        }
1434    }
1435
1436    if( tier )
1437    {
1438        tier->isAnnouncing = FALSE;
1439
1440        if( responseCode == 0 )
1441        {
1442            const int interval = getRetryInterval( tier->currentTracker->host );
1443            dbgmsg( tier, "No response from tracker... retrying in %d seconds.", interval );
1444            tier->manualAnnounceAllowedAt = ~(time_t)0;
1445            tierAddAnnounce( tier, announceEvent, now + interval );
1446        }
1447        else if( 200 <= responseCode && responseCode <= 299 )
1448        {
1449            const int interval = tier->announceIntervalSec;
1450            dbgmsg( tier, "request succeeded. reannouncing in %d seconds", interval );
1451
1452            if( gotScrape )
1453            {
1454                tier->lastScrapeTime = now;
1455                tier->lastScrapeSucceeded = 1;
1456                tier->scrapeAt = now + tier->scrapeIntervalSec;
1457            }
1458
1459            tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
1460
1461            /* if we're running and the queue is empty, add the next update */
1462            if( !isStopped && !tr_ptrArraySize( &tier->announceEvents ) )
1463            {
1464                tierAddAnnounce( tier, "", now + interval );
1465            }
1466        }
1467        else if( 300 <= responseCode && responseCode <= 399 )
1468        {
1469            /* how did this get here?  libcurl handles this */
1470            const int interval = 5;
1471            dbgmsg( tier, "got a redirect. retrying in %d seconds", interval );
1472            tierAddAnnounce( tier, announceEvent, now + interval );
1473            tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
1474        }
1475        else if( ( responseCode == 404 ) || ( 500 <= responseCode && responseCode <= 599 ) )
1476        {
1477            /* 404: The requested resource could not be found but may be
1478             * available again in the future.  Subsequent requests by
1479             * the client are permissible. */
1480
1481            /* 5xx: indicate cases in which the server is aware that it
1482             * has erred or is incapable of performing the request.
1483             * So we pause a bit and try again. */
1484
1485            const int interval = getRetryInterval( tier->currentTracker->host );
1486            tier->manualAnnounceAllowedAt = ~(time_t)0;
1487            tierAddAnnounce( tier, announceEvent, now + interval );
1488        }
1489        else if( 400 <= responseCode && responseCode <= 499 )
1490        {
1491            /* The request could not be understood by the server due to
1492             * malformed syntax. The client SHOULD NOT repeat the
1493             * request without modifications. */
1494            if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
1495                publishErrorMessageAndStop( tier, _( "Tracker returned a 4xx message" ) );
1496            tier->announceAt = 0;
1497            tier->manualAnnounceAllowedAt = ~(time_t)0;
1498        }
1499        else
1500        {
1501            /* WTF did we get?? */
1502            const int interval = 120;
1503            dbgmsg( tier, "Invalid response from tracker... retrying in two minutes." );
1504            tier->manualAnnounceAllowedAt = ~(time_t)0;
1505            tierAddAnnounce( tier, announceEvent, now + interval );
1506        }
1507
1508        tier->lastAnnounceSucceeded = success;
1509        tier->lastAnnounceTimedOut = responseCode == 0;
1510
1511        if( success )
1512        {
1513            tier->isRunning = data->isRunningOnSuccess;
1514
1515            if( tier->currentTracker->host )
1516                tier->currentTracker->host->lastSuccessfulRequest = now;
1517        }
1518        else if( responseCode != HTTP_OK )
1519        {
1520            tierIncrementTracker( tier );
1521
1522            tr_ptrArrayInsert( &tier->announceEvents, (void*)announceEvent, 0 );
1523        }
1524    }
1525
1526    if( announcer != NULL )
1527    {
1528        ++announcer->slotsAvailable;
1529    }
1530
1531    tr_free( data );
1532}
1533
1534static const char*
1535getNextAnnounceEvent( tr_tier * tier )
1536{
1537    int i, n;
1538    int pos = -1;
1539    tr_ptrArray tmp;
1540    const char ** events;
1541    const char * str = NULL;
1542
1543    assert( tier != NULL );
1544    assert( tr_isTorrent( tier->tor ) );
1545
1546    events = (const char**) tr_ptrArrayPeek( &tier->announceEvents, &n );
1547
1548    /* special case #1: if "stopped" is in the queue,
1549     * ignore everything before it except "completed" */
1550    if( pos == -1 ) {
1551        tr_bool completed = FALSE;
1552        for( i = 0; i < n; ++i ) {
1553            if( !strcmp( events[i], "completed" ) )
1554                completed = TRUE;
1555            if( !strcmp( events[i], "stopped" ) )
1556                break;
1557        }
1558        if( !completed && ( i <  n ) )
1559            pos = i;
1560    }
1561
1562    /* special case #2: don't use empty strings if something follows them */
1563    if( pos == -1 ) {
1564        for( i = 0; i < n; ++i )
1565            if( *events[i] )
1566                break;
1567        if( i < n )
1568            pos = i;
1569    }
1570
1571    /* default: use the next in the queue */
1572    if( ( pos == -1 ) && ( n > 0 ) )
1573        pos = 0;
1574
1575    /* special case #3: if there are duplicate requests in a row, skip to the last one */
1576    if( pos >= 0 ) {
1577        for( i=pos+1; i<n; ++i )
1578            if( strcmp( events[pos], events[i] ) )
1579                break;
1580        pos = i - 1;
1581    }
1582
1583    /* special case #4: BEP 21: "In order to tell the tracker that a peer is a
1584     * partial seed, it MUST send an event=paused parameter in every
1585     * announce while it is a partial seed." */
1586    str = pos>=0 ? events[pos] : NULL;
1587    if( tr_cpGetStatus( &tier->tor->completion ) == TR_PARTIAL_SEED )
1588        if( !str || strcmp( str, "stopped" ) )
1589            str = "paused";
1590
1591#if 0
1592for( i=0; i<n; ++i ) fprintf( stderr, "(%d)\"%s\" ", i, events[i] );
1593fprintf( stderr, "\n" );
1594fprintf( stderr, "using (%d)\"%s\"\n", pos, events[pos] );
1595if( strcmp( events[pos], str ) ) fprintf( stderr, "...but really using [%s]\n", str );
1596#endif
1597
1598    /* announceEvents array upkeep */
1599    tmp = TR_PTR_ARRAY_INIT;
1600    for( i=pos+1; i<n; ++i )
1601        tr_ptrArrayAppend( &tmp, (void*)events[i] );
1602    tr_ptrArrayDestruct( &tier->announceEvents, NULL );
1603    tier->announceEvents = tmp;
1604
1605    return str;
1606}
1607
1608static void
1609tierAnnounce( tr_announcer * announcer, tr_tier * tier )
1610{
1611    const char * announceEvent = getNextAnnounceEvent( tier );
1612
1613    assert( !tier->isAnnouncing );
1614
1615    if( announceEvent != NULL )
1616    {
1617        char * url;
1618        struct announce_data * data;
1619        const tr_torrent * tor = tier->tor;
1620        const time_t now = tr_time( );
1621
1622        data = tr_new0( struct announce_data, 1 );
1623        data->torrentId = tr_torrentId( tor );
1624        data->tierId = tier->key;
1625        data->isRunningOnSuccess = tor->isRunning;
1626        data->timeSent = now;
1627        data->event = announceEvent;
1628        url = createAnnounceURL( announcer, tor, tier, data->event );
1629
1630        tier->isAnnouncing = TRUE;
1631        tier->lastAnnounceStartTime = now;
1632        --announcer->slotsAvailable;
1633        tr_webRun( announcer->session, url, NULL, onAnnounceDone, data );
1634
1635        tr_free( url );
1636    }
1637}
1638
1639static tr_bool
1640parseScrapeResponse( tr_tier     * tier,
1641                     const char  * response,
1642                     size_t        responseLen,
1643                     char        * result,
1644                     size_t        resultlen )
1645{
1646    tr_bool success = FALSE;
1647    tr_benc benc, *files;
1648    const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
1649    if( bencLoaded && tr_bencDictFindDict( &benc, "files", &files ) )
1650    {
1651        const char * key;
1652        tr_benc * val;
1653        int i = 0;
1654        while( tr_bencDictChild( files, i++, &key, &val ))
1655        {
1656            int64_t intVal;
1657            tr_benc * flags;
1658
1659            if( memcmp( tier->tor->info.hash, key, SHA_DIGEST_LENGTH ) )
1660                continue;
1661
1662            success = TRUE;
1663            publishErrorClear( tier );
1664
1665            if( ( tr_bencDictFindInt( val, "complete", &intVal ) ) )
1666                tier->currentTracker->seederCount = intVal;
1667
1668            if( ( tr_bencDictFindInt( val, "incomplete", &intVal ) ) )
1669                tier->currentTracker->leecherCount = intVal;
1670
1671            if( ( tr_bencDictFindInt( val, "downloaded", &intVal ) ) )
1672                tier->currentTracker->downloadCount = intVal;
1673
1674            if( ( tr_bencDictFindInt( val, "downloaders", &intVal ) ) )
1675                tier->currentTracker->downloaderCount = intVal;
1676
1677            if( tr_bencDictFindDict( val, "flags", &flags ) )
1678                if( ( tr_bencDictFindInt( flags, "min_request_interval", &intVal ) ) )
1679                    tier->scrapeIntervalSec = MAX( DEFAULT_SCRAPE_INTERVAL_SEC, (int)intVal );
1680
1681            tr_tordbg( tier->tor,
1682                       "Scrape successful. Rescraping in %d seconds.",
1683                       tier->scrapeIntervalSec );
1684        }
1685    }
1686
1687    if( bencLoaded )
1688        tr_bencFree( &benc );
1689
1690    if( success )
1691        tr_strlcpy( result, _( "Success" ), resultlen );
1692    else
1693        tr_strlcpy( result, _( "Error parsing response" ), resultlen );
1694
1695    return success;
1696}
1697
1698static void
1699onScrapeDone( tr_session   * session,
1700              long           responseCode,
1701              const void   * response,
1702              size_t         responseLen,
1703              void         * vdata )
1704{
1705    tr_bool success = FALSE;
1706    tr_announcer * announcer = session->announcer;
1707    struct announce_data * data = vdata;
1708    tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
1709    const time_t now = tr_time( );
1710
1711    if( announcer )
1712        ++announcer->slotsAvailable;
1713
1714    if( announcer && tier )
1715    {
1716        tier->isScraping = FALSE;
1717        tier->lastScrapeTime = now;
1718
1719        if( tier->currentTracker->host )
1720        {
1721            tr_host * host = tier->currentTracker->host;
1722            host->lastRequestTime = data->timeSent;
1723            host->lastResponseInterval = now - data->timeSent;
1724        }
1725
1726        if( 200 <= responseCode && responseCode <= 299 )
1727        {
1728            const int interval = tier->scrapeIntervalSec;
1729            tier->scrapeAt = now + interval;
1730
1731            if( responseCode == HTTP_OK )
1732                success = parseScrapeResponse( tier, response, responseLen,
1733                                               tier->lastScrapeStr, sizeof( tier->lastScrapeStr ) );
1734            else
1735                tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1736                             _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1737                             responseCode, tr_webGetResponseStr( responseCode ) );
1738            tr_tordbg( tier->tor, "%s", tier->lastScrapeStr );
1739        }
1740        else if( 300 <= responseCode && responseCode <= 399 )
1741        {
1742            /* this shouldn't happen; libcurl should handle this */
1743            const int interval = 5;
1744            tier->scrapeAt = now + interval;
1745            tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1746                         "Got a redirect. Retrying in %d seconds", interval );
1747            tr_tordbg( tier->tor, "%s", tier->lastScrapeStr );
1748        }
1749        else
1750        {
1751            const int interval = getRetryInterval( tier->currentTracker->host );
1752
1753            /* Don't retry on a 4xx.
1754             * Retry at growing intervals on a 5xx */
1755            if( 400 <= responseCode && responseCode <= 499 )
1756                tier->scrapeAt = 0;
1757            else
1758                tier->scrapeAt = now + interval;
1759
1760            /* %1$ld - http status code, such as 404
1761             * %2$s - human-readable explanation of the http status code */
1762            if( !responseCode )
1763                tr_strlcpy( tier->lastScrapeStr, _( "tracker did not respond" ),
1764                            sizeof( tier->lastScrapeStr ) );
1765            else
1766                tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1767                             _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1768                             responseCode, tr_webGetResponseStr( responseCode ) );
1769        }
1770
1771        tier->lastScrapeSucceeded = success;
1772        tier->lastScrapeTimedOut = responseCode == 0;
1773
1774        if( success && tier->currentTracker->host )
1775            tier->currentTracker->host->lastSuccessfulRequest = now;
1776    }
1777
1778    tr_free( data );
1779}
1780
1781static void
1782tierScrape( tr_announcer * announcer, tr_tier * tier )
1783{
1784    char * url;
1785    const char * scrape;
1786    struct announce_data * data;
1787    const time_t now = tr_time( );
1788
1789    assert( tier );
1790    assert( !tier->isScraping );
1791    assert( tier->currentTracker != NULL );
1792    assert( tr_isTorrent( tier->tor ) );
1793
1794    data = tr_new0( struct announce_data, 1 );
1795    data->torrentId = tr_torrentId( tier->tor );
1796    data->tierId = tier->key;
1797
1798    scrape = tier->currentTracker->scrape;
1799
1800    url = tr_strdup_printf( "%s%cinfo_hash=%s",
1801                            scrape,
1802                            strchr( scrape, '?' ) ? '&' : '?',
1803                            tier->tor->info.hashEscaped );
1804
1805    tier->isScraping = TRUE;
1806    tier->lastScrapeStartTime = now;
1807    --announcer->slotsAvailable;
1808    dbgmsg( tier, "scraping \"%s\"", url );
1809    tr_webRun( announcer->session, url, NULL, onScrapeDone, data );
1810
1811    tr_free( url );
1812}
1813
1814static void
1815flushCloseMessages( tr_announcer * announcer )
1816{
1817    int i;
1818    const int n = tr_ptrArraySize( &announcer->stops );
1819
1820    for( i=0; i<n; ++i )
1821    {
1822        struct stop_message * stop = tr_ptrArrayNth( &announcer->stops, i );
1823        tr_webRun( announcer->session, stop->url, NULL, NULL, NULL );
1824        stopFree( stop );
1825    }
1826
1827    tr_ptrArrayClear( &announcer->stops );
1828}
1829
1830static tr_bool
1831tierNeedsToAnnounce( const tr_tier * tier, const time_t now )
1832{
1833    return !tier->isAnnouncing
1834        && !tier->isScraping
1835        && ( tier->announceAt != 0 )
1836        && ( tier->announceAt <= now )
1837        && ( tr_ptrArraySize( &tier->announceEvents ) != 0 );
1838}
1839
1840static tr_bool
1841tierNeedsToScrape( const tr_tier * tier, const time_t now )
1842{
1843    return ( !tier->isScraping )
1844        && ( tier->scrapeAt != 0 )
1845        && ( tier->scrapeAt <= now )
1846        && ( tier->currentTracker != NULL )
1847        && ( tier->currentTracker->scrape != NULL );
1848}
1849
1850static void
1851announceMore( tr_announcer * announcer )
1852{
1853    tr_torrent * tor = NULL;
1854    const time_t now = tr_time( );
1855
1856    if( announcer->slotsAvailable > 0 )
1857    {
1858        int i;
1859        int n;
1860        tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
1861        tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
1862
1863        /* build a list of tiers that need to be announced */
1864        while(( tor = tr_torrentNext( announcer->session, tor ))) {
1865            if( tor->tiers ) {
1866                n = tr_ptrArraySize( &tor->tiers->tiers );
1867                for( i=0; i<n; ++i ) {
1868                    tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
1869                    if( tierNeedsToAnnounce( tier, now ) )
1870                        tr_ptrArrayAppend( &announceMe, tier );
1871                    else if( tierNeedsToScrape( tier, now ) )
1872                        tr_ptrArrayAppend( &scrapeMe, tier );
1873                }
1874            }
1875        }
1876
1877        /* if there are more tiers than slots available, prioritize */
1878        n = tr_ptrArraySize( &announceMe );
1879        if( n > announcer->slotsAvailable )
1880            qsort( tr_ptrArrayBase( &announceMe ), n, sizeof( tr_tier * ), compareTiers );
1881
1882        /* announce some */
1883        n = MIN( tr_ptrArraySize( &announceMe ), announcer->slotsAvailable );
1884        for( i=0; i<n; ++i ) {
1885            tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
1886            dbgmsg( tier, "announcing tier %d of %d", i, n );
1887            tierAnnounce( announcer, tier );
1888        }
1889
1890        /* scrape some */
1891        n = MIN( tr_ptrArraySize( &scrapeMe ), announcer->slotsAvailable );
1892        for( i=0; i<n; ++i ) {
1893            tr_tier * tier = tr_ptrArrayNth( &scrapeMe, i );
1894            dbgmsg( tier, "scraping tier %d of %d", (i+1), n );
1895            tierScrape( announcer, tier );
1896        }
1897
1898#if 0
1899char timebuf[64];
1900tr_getLogTimeStr( timebuf, 64 );
1901fprintf( stderr, "[%s] announce.c has %d requests ready to send (announce: %d, scrape: %d)\n", timebuf, (int)(tr_ptrArraySize(&announceMe)+tr_ptrArraySize(&scrapeMe)), (int)tr_ptrArraySize(&announceMe), (int)tr_ptrArraySize(&scrapeMe) );
1902#endif
1903
1904        /* cleanup */
1905        tr_ptrArrayDestruct( &scrapeMe, NULL );
1906        tr_ptrArrayDestruct( &announceMe, NULL );
1907    }
1908
1909    tor = NULL;
1910    while(( tor = tr_torrentNext( announcer->session, tor ))) {
1911        if( tor->dhtAnnounceAt <= now ) {
1912            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
1913                int rc;
1914                rc = tr_dhtAnnounce(tor, AF_INET, 1);
1915                if(rc == 0)
1916                    /* The DHT is not ready yet.  Try again soon. */
1917                    tor->dhtAnnounceAt = now + 5 + tr_cryptoWeakRandInt( 5 );
1918                else
1919                    /* We should announce at least once every 30 minutes. */
1920                    tor->dhtAnnounceAt =
1921                        now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
1922            }
1923        }
1924
1925        if( tor->dhtAnnounce6At <= now ) {
1926            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
1927                int rc;
1928                rc = tr_dhtAnnounce(tor, AF_INET6, 1);
1929                if(rc == 0)
1930                    tor->dhtAnnounce6At = now + 5 + tr_cryptoWeakRandInt( 5 );
1931                else
1932                    tor->dhtAnnounce6At =
1933                        now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
1934            }
1935        }
1936    }
1937
1938    /* Local Peer Discovery */
1939    if( announcer->lpdHouseKeepingAt <= now )
1940    {
1941        tr_lpdAnnounceMore( now, LPD_HOUSEKEEPING_INTERVAL_SECS );
1942
1943        /* reschedule more LDS announces for ( the future + jitter ) */
1944        announcer->lpdHouseKeepingAt =
1945            calcRescheduleWithJitter( LPD_HOUSEKEEPING_INTERVAL_SECS );
1946    }
1947}
1948
1949static void
1950onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer )
1951{
1952    tr_announcer * announcer = vannouncer;
1953    tr_sessionLock( announcer->session );
1954
1955    /* maybe send out some "stopped" messages for closed torrents */
1956    flushCloseMessages( announcer );
1957
1958    /* maybe send out some announcements to trackers */
1959    announceMore( announcer );
1960
1961    /* set up the next timer */
1962    tr_timerAdd( announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
1963
1964    tr_sessionUnlock( announcer->session );
1965}
1966
1967/***
1968****
1969***/
1970
1971tr_tracker_stat *
1972tr_announcerStats( const tr_torrent * torrent,
1973                   int              * setmeTrackerCount )
1974{
1975    int i;
1976    int n;
1977    int out = 0;
1978    int tierCount;
1979    tr_tracker_stat * ret;
1980    const time_t now = tr_time( );
1981
1982    assert( tr_isTorrent( torrent ) );
1983
1984    /* count the trackers... */
1985    for( i=n=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i ) {
1986        const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
1987        n += tr_ptrArraySize( &tier->trackers );
1988    }
1989
1990    /* alloc the stats */
1991    *setmeTrackerCount = n;
1992    ret = tr_new0( tr_tracker_stat, n );
1993
1994    /* populate the stats */
1995    for( i=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i )
1996    {
1997        int j;
1998        const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
1999        n = tr_ptrArraySize( &tier->trackers );
2000        for( j=0; j<n; ++j )
2001        {
2002            const tr_tracker_item * tracker = tr_ptrArrayNth( (tr_ptrArray*)&tier->trackers, j );
2003            tr_tracker_stat * st = ret + out++;
2004
2005            st->id = tracker->id;
2006            tr_strlcpy( st->host, tracker->host->name, sizeof( st->host ) );
2007            tr_strlcpy( st->announce, tracker->announce, sizeof( st->announce ) );
2008            st->tier = i;
2009            st->isBackup = tracker != tier->currentTracker;
2010            st->lastScrapeStartTime = tier->lastScrapeStartTime;
2011
2012            st->seederCount = tracker->seederCount;
2013            st->leecherCount = tracker->leecherCount;
2014            st->downloadCount = tracker->downloadCount;
2015
2016            if( st->isBackup )
2017            {
2018                st->scrapeState = TR_TRACKER_INACTIVE;
2019                st->announceState = TR_TRACKER_INACTIVE;
2020                st->nextScrapeTime = 0;
2021                st->nextAnnounceTime = 0;
2022            }
2023            else
2024            {
2025                if(( st->hasScraped = tier->lastScrapeTime != 0 )) {
2026                    st->lastScrapeTime = tier->lastScrapeTime;
2027                    st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
2028                    st->lastScrapeTimedOut = tier->lastScrapeTimedOut;
2029                    tr_strlcpy( st->lastScrapeResult, tier->lastScrapeStr, sizeof( st->lastScrapeResult ) );
2030                }
2031
2032                if( tier->isScraping )
2033                    st->scrapeState = TR_TRACKER_ACTIVE;
2034                else if( !tier->scrapeAt )
2035                    st->scrapeState = TR_TRACKER_INACTIVE;
2036                else if( tier->scrapeAt > now )
2037                {
2038                    st->scrapeState = TR_TRACKER_WAITING;
2039                    st->nextScrapeTime = tier->scrapeAt;
2040                }
2041                else
2042                    st->scrapeState = TR_TRACKER_QUEUED;
2043
2044                st->lastAnnounceStartTime = tier->lastAnnounceStartTime;
2045
2046                if(( st->hasAnnounced = tier->lastAnnounceTime != 0 )) {
2047                    st->lastAnnounceTime = tier->lastAnnounceTime;
2048                    tr_strlcpy( st->lastAnnounceResult, tier->lastAnnounceStr, sizeof( st->lastAnnounceResult ) );
2049                    st->lastAnnounceSucceeded = tier->lastAnnounceSucceeded;
2050                    st->lastAnnounceTimedOut = tier->lastAnnounceTimedOut;
2051                    st->lastAnnouncePeerCount = tier->lastAnnouncePeerCount;
2052                }
2053
2054                if( tier->isAnnouncing )
2055                    st->announceState = TR_TRACKER_ACTIVE;
2056                else if( !torrent->isRunning || !tier->announceAt )
2057                    st->announceState = TR_TRACKER_INACTIVE;
2058                else if( tier->announceAt > now )
2059                {
2060                    st->announceState = TR_TRACKER_WAITING;
2061                    st->nextAnnounceTime = tier->announceAt;
2062                }
2063                else
2064                    st->announceState = TR_TRACKER_QUEUED;
2065            }
2066        }
2067    }
2068
2069    return ret;
2070}
2071
2072void
2073tr_announcerStatsFree( tr_tracker_stat * trackers,
2074                       int trackerCount UNUSED )
2075{
2076    tr_free( trackers );
2077}
Note: See TracBrowser for help on using the repository browser.