source: trunk/libtransmission/announcer.c @ 9710

Last change on this file since 9710 was 9710, checked in by charles, 13 years ago

(trunk libT) use curl_multi_socket_action(), even on OS X.

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