source: trunk/libtransmission/announcer.c @ 10059

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

(trunk libT) another announcer tweak related to #2828 -- keep the last known good value of lastAnnouncePeerCount even if subsequent announces fail

File size: 52.3 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 incomplete = -1;
1089        size_t rawlen;
1090        int64_t i;
1091        tr_benc * tmp;
1092        const char * str;
1093        const uint8_t * raw;
1094
1095        success = TRUE;
1096
1097        tier->retryAnnounceIntervalSec = FIRST_ANNOUNCE_RETRY_INTERVAL_SEC;
1098
1099        if( tr_bencDictFindStr( &benc, "failure reason", &str ) )
1100        {
1101            tr_strlcpy( tier->lastAnnounceStr, str,
1102                        sizeof( tier->lastAnnounceStr ) );
1103            dbgmsg( tier, "tracker gave \"%s\"", str );
1104            publishMessage( tier, str, TR_TRACKER_ERROR );
1105            success = FALSE;
1106        }
1107
1108        if( tr_bencDictFindStr( &benc, "warning message", &str ) )
1109        {
1110            tr_strlcpy( tier->lastAnnounceStr, str,
1111                        sizeof( tier->lastAnnounceStr ) );
1112            dbgmsg( tier, "tracker gave \"%s\"", str );
1113            publishWarning( tier, str );
1114        }
1115
1116        if( tr_bencDictFindInt( &benc, "interval", &i ) )
1117        {
1118            dbgmsg( tier, "setting interval to %d", (int)i );
1119            tier->announceIntervalSec = i;
1120        }
1121
1122        if( tr_bencDictFindInt( &benc, "min interval", &i ) )
1123        {
1124            dbgmsg( tier, "setting min interval to %d", (int)i );
1125            tier->announceMinIntervalSec = i;
1126        }
1127
1128        if( tr_bencDictFindStr( &benc, "tracker id", &str ) )
1129        {
1130            tier->currentTracker->tracker_id = tr_strdup( str );
1131        }
1132
1133        if( tr_bencDictFindInt( &benc, "complete", &i ) )
1134        {
1135            ++scrapeFields;
1136            tier->currentTracker->seederCount = i;
1137        }
1138
1139        if( tr_bencDictFindInt( &benc, "incomplete", &i ) )
1140        {
1141            ++scrapeFields;
1142            tier->currentTracker->leecherCount = incomplete = i;
1143        }
1144
1145        if( tr_bencDictFindInt( &benc, "downloaded", &i ) )
1146        {
1147            ++scrapeFields;
1148            tier->currentTracker->downloadCount = i;
1149        }
1150
1151        if( tr_bencDictFindRaw( &benc, "peers", &raw, &rawlen ) )
1152        {
1153            /* "compact" extension */
1154            const int allAreSeeds = incomplete == 0;
1155            tier->lastAnnouncePeerCount = publishNewPeersCompact( tier, allAreSeeds, raw, rawlen );
1156        }
1157        else if( tr_bencDictFindList( &benc, "peers", &tmp ) )
1158        {
1159            /* original version of peers */
1160            const tr_bool allAreSeeds = incomplete == 0;
1161            size_t byteCount = 0;
1162            uint8_t * array = parseOldPeers( tmp, &byteCount );
1163            tier->lastAnnouncePeerCount = publishNewPeers( tier, allAreSeeds, array, byteCount );
1164            tr_free( array );
1165        }
1166
1167        if( tr_bencDictFindRaw( &benc, "peers6", &raw, &rawlen ) )
1168        {
1169            /* "compact" extension */
1170            const tr_bool allAreSeeds = incomplete == 0;
1171            tier->lastAnnouncePeerCount += publishNewPeersCompact6( tier, allAreSeeds, raw, rawlen );
1172        }
1173
1174        if( tier->lastAnnounceStr[0] == '\0' )
1175            tr_strlcpy( tier->lastAnnounceStr, _( "Success" ),
1176                        sizeof( tier->lastAnnounceStr ) );
1177    }
1178
1179    if( bencLoaded )
1180        tr_bencFree( &benc );
1181
1182    *gotScrape = scrapeFields >= 2;
1183
1184    return success;
1185}
1186
1187struct announce_data
1188{
1189    int torrentId;
1190    int tierId;
1191    time_t timeSent;
1192
1193    /** If the request succeeds, the value for tier's "isRunning" flag */
1194    tr_bool isRunningOnSuccess;
1195};
1196
1197static void
1198onAnnounceDone( tr_session   * session,
1199                long           responseCode,
1200                const void   * response,
1201                size_t         responseLen,
1202                void         * vdata )
1203{
1204    tr_announcer * announcer = session->announcer;
1205    struct announce_data * data = vdata;
1206    tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
1207    tr_bool gotScrape = FALSE;
1208    tr_bool success = FALSE;
1209    const time_t now = time ( NULL );
1210
1211    if( announcer && tier )
1212    {
1213        tier->lastAnnouncePeerCount = 0;
1214
1215        if( tier->currentTracker->host )
1216        {
1217            tr_host * host = tier->currentTracker->host;
1218            host->lastRequestTime = data->timeSent;
1219            host->lastResponseInterval = now - data->timeSent;
1220        }
1221
1222        tier->lastAnnounceTime = now;
1223
1224        if( responseCode == HTTP_OK )
1225        {
1226            success = parseAnnounceResponse( tier, response, responseLen, &gotScrape );
1227            dbgmsg( tier, "success is %d", success );
1228        }
1229        else if( responseCode )
1230        {
1231            /* %1$ld - http status code, such as 404
1232             * %2$s - human-readable explanation of the http status code */
1233            char * buf = tr_strdup_printf(
1234                _( "Announce failed: tracker gave HTTP Response Code %1$ld (%2$s)" ),
1235                responseCode,
1236                tr_webGetResponseStr( responseCode ) );
1237
1238            tr_strlcpy( tier->lastAnnounceStr, buf,
1239                        sizeof( tier->lastAnnounceStr ) );
1240
1241            /* if the response is serious, *and* if the response may require
1242             * human intervention, then notify the user... otherwise just log it */
1243            if( responseCode >= 400 )
1244                if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
1245                    publishWarning( tier, buf );
1246            tr_torinf( tier->tor, "%s", buf );
1247            dbgmsg( tier, "%s", buf );
1248
1249            tr_free( buf );
1250        }
1251        else
1252        {
1253            tr_strlcpy( tier->lastAnnounceStr,
1254                        _( "tracker did not respond" ),
1255                        sizeof( tier->lastAnnounceStr ) );
1256            dbgmsg( tier, "%s", tier->lastAnnounceStr );
1257        }
1258    }
1259
1260    if( tier )
1261    {
1262        tier->isAnnouncing = FALSE;
1263
1264        if( responseCode == 0 )
1265        {
1266            const int interval = 120 + tr_cryptoWeakRandInt( 120 );
1267            dbgmsg( tier, "No response from tracker... retrying in %d seconds.", interval );
1268            tier->manualAnnounceAllowedAt = ~(time_t)0;
1269            tierSetNextAnnounce( tier, tier->announceEvent, now + interval );
1270        }
1271        else if( 200 <= responseCode && responseCode <= 299 )
1272        {
1273            const int interval = tier->announceIntervalSec;
1274            dbgmsg( tier, "request succeeded. reannouncing in %d seconds", interval );
1275
1276            if( gotScrape )
1277            {
1278                tier->lastScrapeTime = now;
1279                tier->lastScrapeSucceeded = 1;
1280                tier->scrapeAt = now + tier->scrapeIntervalSec;
1281            }
1282
1283            tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
1284
1285            if( strcmp( tier->announceEvent, "stopped" ) == 0 )
1286                tierClearNextAnnounce( tier );
1287            else
1288                tierSetNextAnnounce( tier, "", now + interval );
1289        }
1290        else if( 300 <= responseCode && responseCode <= 399 )
1291        {
1292            /* how did this get here?  libcurl handles this */
1293            const int interval = 5;
1294            dbgmsg( tier, "got a redirect. retrying in %d seconds", interval );
1295            tierSetNextAnnounce( tier, tier->announceEvent, now + interval );
1296            tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
1297        }
1298        else if( 400 <= responseCode && responseCode <= 499 )
1299        {
1300            /* The request could not be understood by the server due to
1301             * malformed syntax. The client SHOULD NOT repeat the
1302             * request without modifications. */
1303            if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
1304                publishErrorMessageAndStop( tier, _( "Tracker returned a 4xx message" ) );
1305            tier->manualAnnounceAllowedAt = ~(time_t)0;
1306            tierClearNextAnnounce( tier );
1307        }
1308        else if( 500 <= responseCode && responseCode <= 599 )
1309        {
1310            /* Response status codes beginning with the digit "5" indicate
1311             * cases in which the server is aware that it has erred or is
1312             * incapable of performing the request.  So we pause a bit and
1313             * try again. */
1314            tier->manualAnnounceAllowedAt = ~(time_t)0;
1315            tierSetNextAnnounce( tier, tier->announceEvent, now + tier->retryAnnounceIntervalSec );
1316            tier->retryAnnounceIntervalSec *= 2;
1317        }
1318        else
1319        {
1320            /* WTF did we get?? */
1321            const int interval = 120;
1322            dbgmsg( tier, "Invalid response from tracker... retrying in two minutes." );
1323            tier->manualAnnounceAllowedAt = ~(time_t)0;
1324            tierSetNextAnnounce( tier, tier->announceEvent, now + interval );
1325        }
1326
1327        tier->lastAnnounceSucceeded = success;
1328
1329        if( success )
1330            tier->isRunning = data->isRunningOnSuccess;
1331
1332        if( !success )
1333            tierIncrementTracker( tier );
1334    }
1335
1336    if( announcer != NULL )
1337    {
1338        ++announcer->announceSlotsAvailable;
1339    }
1340
1341    tr_free( data );
1342}
1343
1344static const char *
1345getAnnounceEvent( const tr_tier * tier )
1346{
1347    const char * event;
1348
1349    assert( tier != NULL );
1350    assert( tier->announceEvent != NULL );
1351    assert( tr_isTorrent( tier->tor ) );
1352
1353    /* BEP 21: "In order to tell the tracker that a peer is a partial seed,
1354     * it MUST send an event=paused parameter in every announce while
1355     * it is a partial seed." */
1356    if( tr_cpGetStatus( &tier->tor->completion ) == TR_PARTIAL_SEED )
1357        return "paused";
1358
1359    event = tier->announceEvent;
1360
1361    if( !strcmp( event, "manual" ) )
1362        return "started";
1363
1364    return event;
1365}
1366
1367static void
1368tierAnnounce( tr_announcer * announcer, tr_tier * tier )
1369{
1370    char * url;
1371    struct announce_data * data;
1372    const tr_torrent * tor = tier->tor;
1373    const time_t now = tr_time( );
1374
1375    assert( !tier->isAnnouncing );
1376
1377    data = tr_new0( struct announce_data, 1 );
1378    data->torrentId = tr_torrentId( tor );
1379    data->tierId = tier->key;
1380    data->isRunningOnSuccess = tor->isRunning;
1381    data->timeSent = now;
1382
1383    url = createAnnounceURL( announcer, tor, tier, getAnnounceEvent( tier ) );
1384
1385    tier->isAnnouncing = TRUE;
1386    tier->lastAnnounceStartTime = now;
1387    --announcer->announceSlotsAvailable;
1388    tr_webRun( announcer->session, url, NULL, onAnnounceDone, data );
1389
1390    tr_free( url );
1391}
1392
1393static tr_bool
1394parseScrapeResponse( tr_tier     * tier,
1395                     const char  * response,
1396                     size_t        responseLen,
1397                     char        * result,
1398                     size_t        resultlen )
1399{
1400    tr_bool success = FALSE;
1401    tr_benc benc, *files;
1402    const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
1403    if( bencLoaded && tr_bencDictFindDict( &benc, "files", &files ) )
1404    {
1405        const char * key;
1406        tr_benc * val;
1407        int i = 0;
1408        while( tr_bencDictChild( files, i++, &key, &val ))
1409        {
1410            int64_t intVal;
1411            tr_benc * flags;
1412
1413            if( memcmp( tier->tor->info.hash, key, SHA_DIGEST_LENGTH ) )
1414                continue;
1415
1416            success = TRUE;
1417            publishErrorClear( tier );
1418
1419            if( ( tr_bencDictFindInt( val, "complete", &intVal ) ) )
1420                tier->currentTracker->seederCount = intVal;
1421
1422            if( ( tr_bencDictFindInt( val, "incomplete", &intVal ) ) )
1423                tier->currentTracker->leecherCount = intVal;
1424
1425            if( ( tr_bencDictFindInt( val, "downloaded", &intVal ) ) )
1426                tier->currentTracker->downloadCount = intVal;
1427
1428            if( ( tr_bencDictFindInt( val, "downloaders", &intVal ) ) )
1429                tier->currentTracker->downloaderCount = intVal;
1430
1431            if( tr_bencDictFindDict( val, "flags", &flags ) )
1432                if( ( tr_bencDictFindInt( flags, "min_request_interval", &intVal ) ) )
1433                    tier->scrapeIntervalSec = intVal;
1434
1435            /* as per ticket #1045, safeguard against trackers returning
1436             * a very low min_request_interval... */
1437            if( tier->scrapeIntervalSec < DEFAULT_SCRAPE_INTERVAL_SEC )
1438                tier->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
1439
1440            tr_tordbg( tier->tor,
1441                       "Scrape successful. Rescraping in %d seconds.",
1442                       tier->scrapeIntervalSec );
1443
1444            tier->retryScrapeIntervalSec = FIRST_SCRAPE_RETRY_INTERVAL_SEC;
1445        }
1446    }
1447
1448    if( bencLoaded )
1449        tr_bencFree( &benc );
1450
1451    if( success )
1452        tr_strlcpy( result, _( "Success" ), resultlen );
1453    else
1454        tr_strlcpy( result, _( "Error parsing response" ), resultlen );
1455
1456    return success;
1457}
1458
1459static void
1460onScrapeDone( tr_session   * session,
1461              long           responseCode,
1462              const void   * response,
1463              size_t         responseLen,
1464              void         * vdata )
1465{
1466    tr_bool success = FALSE;
1467    tr_announcer * announcer = session->announcer;
1468    struct announce_data * data = vdata;
1469    tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
1470    const time_t now = tr_time( );
1471
1472    if( announcer )
1473        ++announcer->scrapeSlotsAvailable;
1474
1475    if( announcer && tier )
1476    {
1477        tier->isScraping = FALSE;
1478        tier->lastScrapeTime = now;
1479
1480        if( tier->currentTracker->host )
1481        {
1482            tr_host * host = tier->currentTracker->host;
1483            host->lastRequestTime = data->timeSent;
1484            host->lastResponseInterval = now - data->timeSent;
1485        }
1486
1487        if( 200 <= responseCode && responseCode <= 299 )
1488        {
1489            const int interval = tier->scrapeIntervalSec;
1490            tier->scrapeAt = now + interval;
1491
1492            if( responseCode == HTTP_OK )
1493                success = parseScrapeResponse( tier, response, responseLen,
1494                                               tier->lastScrapeStr, sizeof( tier->lastScrapeStr ) );
1495            else
1496                tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1497                             _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1498                             responseCode, tr_webGetResponseStr( responseCode ) );
1499            tr_tordbg( tier->tor, "%s", tier->lastScrapeStr );
1500        }
1501        else if( 300 <= responseCode && responseCode <= 399 )
1502        {
1503            /* this shouldn't happen; libcurl should handle this */
1504            const int interval = 5;
1505            tier->scrapeAt = now + interval;
1506            tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1507                         "Got a redirect. Retrying in %d seconds", interval );
1508            tr_tordbg( tier->tor, "%s", tier->lastScrapeStr );
1509        }
1510        else
1511        {
1512            const int interval = tier->retryScrapeIntervalSec;
1513            tier->retryScrapeIntervalSec *= 2;
1514
1515            /* Don't retry on a 4xx.
1516             * Retry at growing intervals on a 5xx */
1517            if( 400 <= responseCode && responseCode <= 499 )
1518                tier->scrapeAt = 0;
1519            else
1520                tier->scrapeAt = now + interval;
1521
1522            /* %1$ld - http status code, such as 404
1523             * %2$s - human-readable explanation of the http status code */
1524            if( !responseCode )
1525                tr_strlcpy( tier->lastScrapeStr, _( "tracker did not respond" ),
1526                            sizeof( tier->lastScrapeStr ) );
1527            else
1528                tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1529                             _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1530                             responseCode, tr_webGetResponseStr( responseCode ) );
1531        }
1532
1533        tier->lastScrapeSucceeded = success;
1534    }
1535
1536    tr_free( data );
1537}
1538
1539static void
1540tierScrape( tr_announcer * announcer, tr_tier * tier )
1541{
1542    char * url;
1543    const char * scrape;
1544    struct announce_data * data;
1545    const time_t now = tr_time( );
1546
1547    assert( tier );
1548    assert( !tier->isScraping );
1549    assert( tier->currentTracker != NULL );
1550    assert( tr_isTorrent( tier->tor ) );
1551
1552    data = tr_new0( struct announce_data, 1 );
1553    data->torrentId = tr_torrentId( tier->tor );
1554    data->tierId = tier->key;
1555
1556    scrape = tier->currentTracker->scrape;
1557
1558    url = tr_strdup_printf( "%s%cinfo_hash=%s",
1559                            scrape,
1560                            strchr( scrape, '?' ) ? '&' : '?',
1561                            tier->tor->info.hashEscaped );
1562
1563    tier->isScraping = TRUE;
1564    tier->lastScrapeStartTime = now;
1565    --announcer->scrapeSlotsAvailable;
1566    dbgmsg( tier, "scraping \"%s\"", url );
1567    tr_webRun( announcer->session, url, NULL, onScrapeDone, data );
1568
1569    tr_free( url );
1570}
1571
1572static void
1573flushCloseMessages( tr_announcer * announcer )
1574{
1575    int i;
1576    const int n = tr_ptrArraySize( &announcer->stops );
1577
1578    for( i=0; i<n; ++i )
1579    {
1580        struct stop_message * stop = tr_ptrArrayNth( &announcer->stops, i );
1581        tr_webRun( announcer->session, stop->url, NULL, NULL, NULL );
1582        stopFree( stop );
1583    }
1584
1585    tr_ptrArrayClear( &announcer->stops );
1586}
1587
1588static tr_bool
1589tierNeedsToAnnounce( const tr_tier * tier, const time_t now )
1590{
1591    return !tier->isAnnouncing
1592        && !tier->isScraping
1593        && ( tier->announceEvent != NULL )
1594        && ( tier->announceAt <= now );
1595}
1596
1597static tr_bool
1598tierNeedsToScrape( const tr_tier * tier, const time_t now )
1599{
1600    return !tier->isScraping
1601        && ( tier->scrapeAt != 0 )
1602        && ( tier->scrapeAt <= now )
1603        && ( tier->currentTracker != NULL )
1604        && ( tier->currentTracker->scrape != NULL );
1605}
1606
1607static void
1608announceMore( tr_announcer * announcer )
1609{
1610    const tr_bool canAnnounce = announcer->announceSlotsAvailable > 0;
1611    const tr_bool canScrape = announcer->scrapeSlotsAvailable > 0;
1612    tr_torrent * tor = NULL;
1613    const time_t now = tr_time( );
1614
1615    if( announcer->announceSlotsAvailable > 0 )
1616    {
1617        int i;
1618        int n;
1619        tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
1620        tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
1621
1622        /* build a list of tiers that need to be announced */
1623        while(( tor = tr_torrentNext( announcer->session, tor ))) {
1624            if( tor->tiers ) {
1625                n = tr_ptrArraySize( &tor->tiers->tiers );
1626                for( i=0; i<n; ++i ) {
1627                    tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
1628                    if( canAnnounce && tierNeedsToAnnounce( tier, now ) )
1629                        tr_ptrArrayAppend( &announceMe, tier );
1630                    else if( canScrape && tierNeedsToScrape( tier, now ) )
1631                        tr_ptrArrayAppend( &scrapeMe, tier );
1632                }
1633            }
1634        }
1635
1636        /* if there are more tiers than slots available, prioritize */
1637        n = tr_ptrArraySize( &announceMe );
1638        if( n > announcer->announceSlotsAvailable )
1639            qsort( tr_ptrArrayBase( &announceMe ), n, sizeof( tr_tier * ), compareTiers );
1640
1641        /* announce some */
1642        n = MIN( tr_ptrArraySize( &announceMe ), announcer->announceSlotsAvailable );
1643        for( i=0; i<n; ++i ) {
1644            tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
1645            dbgmsg( tier, "announcing tier %d of %d", i, n );
1646            tierAnnounce( announcer, tier );
1647        }
1648
1649
1650        /* scrape some */
1651        n = MIN( tr_ptrArraySize( &scrapeMe ), announcer->scrapeSlotsAvailable );
1652        for( i=0; i<n; ++i ) {
1653            tr_tier * tier = tr_ptrArrayNth( &scrapeMe, i );
1654            dbgmsg( tier, "scraping tier %d of %d", (i+1), n );
1655            tierScrape( announcer, tier );
1656        }
1657
1658#if 0
1659char timebuf[64];
1660tr_getLogTimeStr( timebuf, 64 );
1661fprintf( 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) );
1662#endif
1663
1664        /* cleanup */
1665        tr_ptrArrayDestruct( &scrapeMe, NULL );
1666        tr_ptrArrayDestruct( &announceMe, NULL );
1667    }
1668
1669    tor = NULL;
1670    while(( tor = tr_torrentNext( announcer->session, tor ))) {
1671        if( tor->dhtAnnounceAt <= now ) {
1672            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
1673                int rc;
1674                rc = tr_dhtAnnounce(tor, AF_INET, 1);
1675                if(rc == 0)
1676                    /* The DHT is not ready yet.  Try again soon. */
1677                    tor->dhtAnnounceAt = now + 5 + tr_cryptoWeakRandInt( 5 );
1678                else
1679                    /* We should announce at least once every 30 minutes. */
1680                    tor->dhtAnnounceAt =
1681                        now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
1682            }
1683        }
1684
1685        if( tor->dhtAnnounce6At <= now ) {
1686            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
1687                int rc;
1688                rc = tr_dhtAnnounce(tor, AF_INET6, 1);
1689                if(rc == 0)
1690                    tor->dhtAnnounce6At = now + 5 + tr_cryptoWeakRandInt( 5 );
1691                else
1692                    tor->dhtAnnounce6At =
1693                        now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
1694            }
1695        }
1696    }
1697}
1698
1699static void
1700onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer )
1701{
1702    tr_announcer * announcer = vannouncer;
1703    tr_sessionLock( announcer->session );
1704
1705    /* maybe send out some "stopped" messages for closed torrents */
1706    flushCloseMessages( announcer );
1707
1708    /* maybe send out some announcements to trackers */
1709    announceMore( announcer );
1710
1711    /* set up the next timer */
1712    tr_timerAdd( announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
1713
1714    tr_sessionUnlock( announcer->session );
1715}
1716
1717/***
1718****
1719***/
1720
1721tr_tracker_stat *
1722tr_announcerStats( const tr_torrent * torrent,
1723                   int              * setmeTrackerCount )
1724{
1725    int i;
1726    int n;
1727    int out = 0;
1728    int tierCount;
1729    tr_tracker_stat * ret;
1730    const time_t now = tr_time( );
1731
1732    assert( tr_isTorrent( torrent ) );
1733
1734    /* count the trackers... */
1735    for( i=n=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i ) {
1736        const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
1737        n += tr_ptrArraySize( &tier->trackers );
1738    }
1739
1740    /* alloc the stats */
1741    *setmeTrackerCount = n;
1742    ret = tr_new0( tr_tracker_stat, n );
1743
1744    /* populate the stats */
1745    for( i=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i )
1746    {
1747        int j;
1748        const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
1749        n = tr_ptrArraySize( &tier->trackers );
1750        for( j=0; j<n; ++j )
1751        {
1752            const tr_tracker_item * tracker = tr_ptrArrayNth( (tr_ptrArray*)&tier->trackers, j );
1753            tr_tracker_stat * st = ret + out++;
1754
1755            st->id = tracker->id;
1756            tr_strlcpy( st->host, tracker->host->name, sizeof( st->host ) );
1757            tr_strlcpy( st->announce, tracker->announce, sizeof( st->announce ) );
1758            st->tier = i + 1;
1759            st->isBackup = tracker != tier->currentTracker;
1760            st->lastScrapeStartTime = tier->lastScrapeStartTime;
1761
1762            st->seederCount = tracker->seederCount;
1763            st->leecherCount = tracker->leecherCount;
1764            st->downloadCount = tracker->downloadCount;
1765
1766            if( st->isBackup )
1767            {
1768                st->scrapeState = TR_TRACKER_INACTIVE;
1769                st->announceState = TR_TRACKER_INACTIVE;
1770                st->nextScrapeTime = 0;
1771                st->nextAnnounceTime = 0;
1772            }
1773            else
1774            {
1775                if(( st->hasScraped = tier->lastScrapeTime != 0 )) {
1776                    st->lastScrapeTime = tier->lastScrapeTime;
1777                    st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
1778                    tr_strlcpy( st->lastScrapeResult, tier->lastScrapeStr, sizeof( st->lastScrapeResult ) );
1779                }
1780
1781                if( tier->isScraping )
1782                    st->scrapeState = TR_TRACKER_ACTIVE;
1783                else if( !tier->scrapeAt )
1784                    st->scrapeState = TR_TRACKER_INACTIVE;
1785                else if( tier->scrapeAt > now )
1786                {
1787                    st->scrapeState = TR_TRACKER_WAITING;
1788                    st->nextScrapeTime = tier->scrapeAt;
1789                }
1790                else
1791                    st->scrapeState = TR_TRACKER_QUEUED;
1792
1793                st->lastAnnounceStartTime = tier->lastAnnounceStartTime;
1794
1795                if(( st->hasAnnounced = tier->lastAnnounceTime != 0 )) {
1796                    st->lastAnnounceTime = tier->lastAnnounceTime;
1797                    tr_strlcpy( st->lastAnnounceResult, tier->lastAnnounceStr, sizeof( st->lastAnnounceResult ) );
1798                    st->lastAnnounceSucceeded = tier->lastAnnounceSucceeded;
1799                    st->lastAnnouncePeerCount = tier->lastAnnouncePeerCount;
1800                }
1801
1802                if( tier->isAnnouncing )
1803                    st->announceState = TR_TRACKER_ACTIVE;
1804                else if( !torrent->isRunning || !tier->announceAt )
1805                    st->announceState = TR_TRACKER_INACTIVE;
1806                else if( tier->announceAt > now )
1807                {
1808                    st->announceState = TR_TRACKER_WAITING;
1809                    st->nextAnnounceTime = tier->announceAt;
1810                }
1811                else
1812                    st->announceState = TR_TRACKER_QUEUED;
1813            }
1814        }
1815    }
1816
1817    return ret;
1818}
1819
1820void
1821tr_announcerStatsFree( tr_tracker_stat * trackers,
1822                       int trackerCount UNUSED )
1823{
1824    tr_free( trackers );
1825}
Note: See TracBrowser for help on using the repository browser.