source: trunk/libtransmission/announcer.c @ 9593

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

(trunk libT) #2610 "avoid unnecessary calls to time(NULL)"

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