source: trunk/libtransmission/announcer.c @ 10064

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

(trunk libT) a better way to ignore peerCount from `stopped' responses

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