source: trunk/libtransmission/announcer.c @ 9651

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

(trunk libT) experimental: Reduce SO_SNDBUF and SO_RCVBUF for tracker announce/scrape messages. Reduce SO_RCVBUF for outgoing peer connections on seeding torrents.

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