source: trunk/libtransmission/announcer.c @ 10063

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

(trunk libT) ignore the peer count from `stopped' responses when calculating lastAnnouncePeerCount

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