source: trunk/libtransmission/announcer.c @ 11730

Last change on this file since 11730 was 11730, checked in by jordan, 11 years ago

(trunk libT) #3870 "transmission doesn't recheck after getting initial 404" -- fixed.

The exponentially-growing interval between retries had a bug that caused intervals to be too long if no successful announce had ever been made. This commit fixes the code that calculates the interval.

  • Property svn:keywords set to Date Rev Author Id
File size: 59.6 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: announcer.c 11730 2011-01-20 22:51:05Z jordan $
11 */
12
13#include <assert.h>
14#include <limits.h>
15
16#include <event2/buffer.h>
17#include <event2/event.h>
18#include <event2/http.h> /* for HTTP_OK */
19
20#include "transmission.h"
21#include "announcer.h"
22#include "crypto.h"
23#include "net.h"
24#include "ptrarray.h"
25#include "session.h"
26#include "tr-dht.h"
27#include "tr-lpd.h"
28#include "torrent.h"
29#include "utils.h"
30#include "web.h"
31
32#define STARTED "started"
33
34#define dbgmsg( tier, ... ) \
35if( tr_deepLoggingIsActive( ) ) do { \
36  char name[128]; \
37  tr_snprintf( name, sizeof( name ), "[%s--%s]", tr_torrentName( tier->tor ), \
38      ( tier->currentTracker ? tier->currentTracker->host->name : "" ) ); \
39  tr_deepLog( __FILE__, __LINE__, name, __VA_ARGS__ ); \
40} while( 0 )
41
42enum
43{
44    /* unless the tracker says otherwise, rescrape this frequently */
45    DEFAULT_SCRAPE_INTERVAL_SEC = ( 60 * 30 ),
46
47    /* unless the tracker says otherwise, this is the announce interval */
48    DEFAULT_ANNOUNCE_INTERVAL_SEC = ( 60 * 10 ),
49
50    /* unless the tracker says otherwise, this is the announce min_interval */
51    DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC = ( 60 * 2 ),
52
53    /* the length of the 'key' argument passed in tracker requests */
54    KEYLEN = 8,
55
56    /* how many web tasks we allow at one time */
57    MAX_CONCURRENT_TASKS = 48,
58
59    /* if a tracker takes more than this long to respond,
60     * we treat it as nonresponsive */
61    MAX_TRACKER_RESPONSE_TIME_SECS = ( 60 * 2 ),
62
63    /* the value of the 'numwant' argument passed in tracker requests. */
64    NUMWANT = 80,
65
66    /* how long to put slow (nonresponsive) trackers in the penalty box */
67    SLOW_HOST_PENALTY_SECS = ( 60 * 10 ),
68
69    UPKEEP_INTERVAL_SECS = 1,
70
71    /* this is an upper limit for the frequency of LDS announces */
72    LPD_HOUSEKEEPING_INTERVAL_SECS = 30
73
74};
75
76/***
77****
78***/
79
80static int
81compareTransfer( uint64_t a_uploaded, uint64_t a_downloaded,
82                 uint64_t b_uploaded, uint64_t b_downloaded )
83{
84    /* higher upload count goes first */
85    if( a_uploaded != b_uploaded )
86        return a_uploaded > b_uploaded ? -1 : 1;
87
88    /* then higher download count goes first */
89    if( a_downloaded != b_downloaded )
90        return a_downloaded > b_downloaded ? -1 : 1;
91
92    return 0;
93}
94
95/***
96****
97***/
98
99/**
100 * used by tr_announcer to recognize nonresponsive
101 * trackers and de-prioritize them
102 */
103typedef struct
104{
105    char * name;
106
107    /* how many seconds it took to get the last tracker response */
108    int lastResponseInterval;
109
110    /* the last time we sent an announce or scrape message */
111    time_t lastRequestTime;
112
113    /* the last successful announce/scrape time for this host */
114    time_t lastSuccessfulRequest;
115}
116tr_host;
117
118static int
119compareHosts( const void * va, const void * vb )
120{
121    const tr_host * a = va;
122    const tr_host * b = vb;
123    return strcmp( a->name, b->name );
124}
125
126static int
127compareHostToName( const void * va, const void * vb )
128{
129    const tr_host * a = va;
130    return strcmp( a->name, vb );
131}
132
133/* format: hostname + ':' + port */
134static char *
135getHostName( const char * url )
136{
137    int port = 0;
138    char * host = NULL;
139    char * ret;
140    tr_urlParse( url, -1, NULL, &host, &port, NULL );
141    ret = tr_strdup_printf( "%s:%d", ( host ? host : "invalid" ), port );
142    tr_free( host );
143    return ret;
144}
145
146static tr_host*
147hostNew( const char * name )
148{
149    tr_host * host = tr_new0( tr_host, 1 );
150    host->name = tr_strdup( name );
151    return host;
152}
153
154static void
155hostFree( void * vhost )
156{
157    tr_host * host = vhost;
158
159    tr_free( host->name );
160    tr_free( host );
161}
162
163/***
164****
165***/
166
167/**
168 * Since we can't poll a tr_torrent's fields after it's destroyed,
169 * we pre-build the "stop" announcement message when a torrent
170 * is removed from Transmission
171 */
172struct stop_message
173{
174    tr_host * host;
175    char * url;
176    uint64_t up;
177    uint64_t down;
178};
179
180static void
181stopFree( struct stop_message * stop )
182{
183    tr_free( stop->url );
184    tr_free( stop );
185}
186
187static int
188compareStops( const void * va, const void * vb )
189{
190    const struct stop_message * a = va;
191    const struct stop_message * b = vb;
192    return compareTransfer( a->up, a->down, b->up, b->down);
193}
194
195/***
196****
197***/
198
199/**
200 * "global" (per-tr_session) fields
201 */
202typedef struct tr_announcer
203{
204    tr_ptrArray hosts; /* tr_host */
205    tr_ptrArray stops; /* struct stop_message */
206    tr_session * session;
207    struct event * upkeepTimer;
208    int slotsAvailable;
209    time_t lpdHouseKeepingAt;
210}
211tr_announcer;
212
213tr_bool
214tr_announcerHasBacklog( const struct tr_announcer * announcer )
215{
216    return announcer->slotsAvailable < 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
239static inline time_t
240calcRescheduleWithJitter( const int minPeriod )
241{
242    const double jitterFac = 0.1;
243
244    assert( minPeriod > 0 );
245
246    return tr_time()
247        + minPeriod
248        + tr_cryptoWeakRandInt( (int) ( minPeriod * jitterFac ) + 1 );
249}
250
251void
252tr_announcerInit( tr_session * session )
253{
254    tr_announcer * a;
255
256    const time_t relaxUntil =
257        calcRescheduleWithJitter( LPD_HOUSEKEEPING_INTERVAL_SECS / 3 );
258
259    assert( tr_isSession( session ) );
260
261    a = tr_new0( tr_announcer, 1 );
262    a->hosts = TR_PTR_ARRAY_INIT;
263    a->stops = TR_PTR_ARRAY_INIT;
264    a->session = session;
265    a->slotsAvailable = MAX_CONCURRENT_TASKS;
266    a->lpdHouseKeepingAt = relaxUntil;
267    a->upkeepTimer = evtimer_new( session->event_base, onUpkeepTimer, a );
268    tr_timerAdd( a->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
269
270    session->announcer = a;
271}
272
273static void flushCloseMessages( tr_announcer * announcer );
274
275void
276tr_announcerClose( tr_session * session )
277{
278    tr_announcer * announcer = session->announcer;
279
280    flushCloseMessages( announcer );
281
282    event_free( announcer->upkeepTimer );
283    announcer->upkeepTimer = NULL;
284
285    tr_ptrArrayDestruct( &announcer->stops, NULL );
286    tr_ptrArrayDestruct( &announcer->hosts, hostFree );
287
288    session->announcer = NULL;
289    tr_free( announcer );
290}
291
292/***
293****
294***/
295
296/* a row in tr_tier's list of trackers */
297typedef struct
298{
299    tr_host * host;
300
301    char * announce;
302    char * scrape;
303
304    char * tracker_id;
305
306    int seederCount;
307    int leecherCount;
308    int downloadCount;
309    int downloaderCount;
310
311    int consecutiveAnnounceFailures;
312
313    uint32_t id;
314
315    /* sent as the "key" argument in tracker requests
316     * to verify us if our IP address changes.
317     * This is immutable for the life of the tracker object.
318     * The +1 is for '\0' */
319    unsigned char key_param[KEYLEN + 1];
320}
321tr_tracker_item;
322
323static void
324trackerItemCopyAttributes( tr_tracker_item * t, const tr_tracker_item * o )
325{
326    assert( t != o );
327    assert( t != NULL );
328    assert( o != NULL );
329
330    t->seederCount = o->seederCount;
331    t->leecherCount = o->leecherCount;
332    t->downloadCount = o->downloadCount;
333    t->downloaderCount = o->downloaderCount;
334    memcpy( t->key_param, o->key_param, sizeof( t->key_param ) );
335}
336
337static void
338generateKeyParam( unsigned char * msg, size_t msglen )
339{
340    size_t i;
341    const char * pool = "abcdefghijklmnopqrstuvwxyz0123456789";
342    const int poolSize = 36;
343
344    tr_cryptoRandBuf( msg, msglen );
345    for( i=0; i<msglen; ++i )
346        msg[i] = pool[ msg[i] % poolSize ];
347    msg[msglen] = '\0';
348}
349
350static tr_tracker_item*
351trackerNew( tr_announcer  * announcer,
352            const char    * announce,
353            const char    * scrape,
354            uint32_t        id )
355{
356    tr_tracker_item * tracker = tr_new0( tr_tracker_item, 1  );
357    tracker->host = getHost( announcer, announce );
358    tracker->announce = tr_strdup( announce );
359    tracker->scrape = tr_strdup( scrape );
360    tracker->id = id;
361    generateKeyParam( tracker->key_param, KEYLEN );
362    tracker->seederCount = -1;
363    tracker->leecherCount = -1;
364    tracker->downloadCount = -1;
365    return tracker;
366}
367
368static void
369trackerFree( void * vtracker )
370{
371    tr_tracker_item * tracker = vtracker;
372
373    tr_free( tracker->tracker_id );
374    tr_free( tracker->announce );
375    tr_free( tracker->scrape );
376    tr_free( tracker );
377}
378
379/***
380****
381***/
382
383struct tr_torrent_tiers;
384
385/** @brief A group of trackers in a single tier, as per the multitracker spec */
386typedef struct
387{
388    /* number of up/down/corrupt bytes since the last time we sent an
389     * "event=stopped" message that was acknowledged by the tracker */
390    uint64_t byteCounts[3];
391
392    tr_ptrArray trackers; /* tr_tracker_item */
393    tr_tracker_item * currentTracker;
394    int currentTrackerIndex;
395
396    tr_torrent * tor;
397
398    time_t scrapeAt;
399    time_t lastScrapeStartTime;
400    time_t lastScrapeTime;
401    tr_bool lastScrapeSucceeded;
402    tr_bool lastScrapeTimedOut;
403
404    time_t announceAt;
405    time_t manualAnnounceAllowedAt;
406    time_t lastAnnounceStartTime;
407    time_t lastAnnounceTime;
408    tr_bool lastAnnounceSucceeded;
409    tr_bool lastAnnounceTimedOut;
410
411    tr_ptrArray announceEvents; /* const char* */
412
413    /* unique lookup key */
414    int key;
415
416    int scrapeIntervalSec;
417    int announceIntervalSec;
418    int announceMinIntervalSec;
419
420    int lastAnnouncePeerCount;
421
422    tr_bool isRunning;
423    tr_bool isAnnouncing;
424    tr_bool isScraping;
425    tr_bool wasCopied;
426
427    char lastAnnounceStr[128];
428    char lastScrapeStr[128];
429}
430tr_tier;
431
432static tr_tier *
433tierNew( tr_torrent * tor )
434{
435    tr_tier * t;
436    static int nextKey = 1;
437    const time_t now = tr_time( );
438
439    t = tr_new0( tr_tier, 1 );
440    t->key = nextKey++;
441    t->announceEvents = TR_PTR_ARRAY_INIT;
442    t->trackers = TR_PTR_ARRAY_INIT;
443    t->currentTracker = NULL;
444    t->currentTrackerIndex = -1;
445    t->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
446    t->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
447    t->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
448    t->scrapeAt = now + tr_cryptoWeakRandInt( 60*5 );
449    t->tor = tor;
450
451    return t;
452}
453
454static void
455tierFree( void * vtier )
456{
457    tr_tier * tier = vtier;
458    tr_ptrArrayDestruct( &tier->trackers, trackerFree );
459    tr_ptrArrayDestruct( &tier->announceEvents, NULL );
460    tr_free( tier );
461}
462
463static void
464tierCopyAttributes( tr_tier * t, const tr_tier * o )
465{
466    int i, n;
467    tr_tier bak;
468
469    assert( t != NULL );
470    assert( o != NULL );
471    assert( t != o );
472
473    bak = *t;
474    *t = *o;
475    t->tor = bak.tor;
476    t->trackers = bak.trackers;
477    t->announceEvents = bak.announceEvents;
478    t->currentTracker = bak.currentTracker;
479    t->currentTrackerIndex = bak.currentTrackerIndex;
480
481    tr_ptrArrayClear( &t->announceEvents );
482    for( i=0, n=tr_ptrArraySize(&o->announceEvents); i<n; ++i )
483        tr_ptrArrayAppend( &t->announceEvents, tr_ptrArrayNth((tr_ptrArray*)&o->announceEvents,i) );
484}
485
486static void
487tierIncrementTracker( tr_tier * tier )
488{
489    /* move our index to the next tracker in the tier */
490    const int i = ( tier->currentTrackerIndex + 1 )
491                        % tr_ptrArraySize( &tier->trackers );
492    tier->currentTracker = tr_ptrArrayNth( &tier->trackers, i );
493    tier->currentTrackerIndex = i;
494
495    /* reset some of the tier's fields */
496    tier->scrapeIntervalSec = DEFAULT_SCRAPE_INTERVAL_SEC;
497    tier->announceIntervalSec = DEFAULT_ANNOUNCE_INTERVAL_SEC;
498    tier->announceMinIntervalSec = DEFAULT_ANNOUNCE_MIN_INTERVAL_SEC;
499    tier->isAnnouncing = FALSE;
500    tier->isScraping = FALSE;
501    tier->lastAnnounceStartTime = 0;
502    tier->lastScrapeStartTime = 0;
503}
504
505static void
506tierAddTracker( tr_announcer * announcer,
507                tr_tier      * tier,
508                const char   * announce,
509                const char   * scrape,
510                uint32_t       id )
511{
512    tr_tracker_item * tracker = trackerNew( announcer, announce, scrape, id );
513
514    tr_ptrArrayAppend( &tier->trackers, tracker );
515    dbgmsg( tier, "adding tracker %s", announce );
516
517    if( !tier->currentTracker )
518        tierIncrementTracker( tier );
519}
520
521
522/***
523****
524***/
525
526/**
527 * @brief Opaque, per-torrent data structure for tracker announce information
528 *
529 * this opaque data structure can be found in tr_torrent.tiers
530 */
531typedef struct tr_torrent_tiers
532{
533    tr_ptrArray tiers;
534    tr_tracker_callback * callback;
535    void * callbackData;
536}
537tr_torrent_tiers;
538
539static tr_torrent_tiers*
540tiersNew( void )
541{
542    tr_torrent_tiers * tiers = tr_new0( tr_torrent_tiers, 1 );
543    tiers->tiers = TR_PTR_ARRAY_INIT;
544    return tiers;
545}
546
547static void
548tiersFree( tr_torrent_tiers * tiers )
549{
550    tr_ptrArrayDestruct( &tiers->tiers, tierFree );
551    tr_free( tiers );
552}
553
554static tr_tier*
555getTier( tr_announcer * announcer, int torrentId, int tierId )
556{
557    tr_tier * tier = NULL;
558
559    if( announcer )
560    {
561        tr_torrent * tor = tr_torrentFindFromId( announcer->session, torrentId );
562
563        if( tor && tor->tiers )
564        {
565            int i;
566            tr_ptrArray * tiers = &tor->tiers->tiers;
567            const int n = tr_ptrArraySize( tiers );
568            for( i=0; !tier && i<n; ++i )
569            {
570                tr_tier * tmp = tr_ptrArrayNth( tiers, i );
571                if( tmp->key == tierId )
572                    tier = tmp;
573            }
574        }
575    }
576
577    return tier;
578}
579
580/***
581****  PUBLISH
582***/
583
584static const tr_tracker_event emptyEvent = { 0, NULL, NULL, NULL, 0, 0 };
585
586static void
587publishMessage( tr_tier * tier, const char * msg, int type )
588{
589    if( tier && tier->tor && tier->tor->tiers )
590    {
591        tr_torrent_tiers * tiers = tier->tor->tiers;
592        tr_tracker_event event = emptyEvent;
593        event.messageType = type;
594        event.text = msg;
595        event.tracker = tier->currentTracker ? tier->currentTracker->announce : NULL;
596
597        if( tiers->callback != NULL )
598            tiers->callback( tier->tor, &event, tiers->callbackData );
599    }
600}
601
602static void
603publishErrorClear( tr_tier * tier )
604{
605    publishMessage( tier, NULL, TR_TRACKER_ERROR_CLEAR );
606}
607
608static void
609publishErrorMessageAndStop( tr_tier * tier, const char * msg )
610{
611    tier->isRunning = FALSE;
612
613    publishMessage( tier, msg, TR_TRACKER_ERROR );
614}
615
616static void
617publishWarning( tr_tier * tier, const char * msg )
618{
619    publishMessage( tier, msg, TR_TRACKER_WARNING );
620}
621
622static int8_t
623getSeedProbability( int seeds, int leechers )
624{
625    if( !seeds )
626        return 0;
627
628    if( seeds>=0 && leechers>=0 )
629        return (int8_t)((100.0*seeds)/(seeds+leechers));
630
631    return -1; /* unknown */
632}
633
634static int
635publishNewPeers( tr_tier * tier, int seeds, int leechers,
636                 const void * compact, int compactLen )
637{
638    tr_tracker_event e = emptyEvent;
639
640    e.messageType = TR_TRACKER_PEERS;
641    e.seedProbability = getSeedProbability( seeds, leechers );
642    e.compact = compact;
643    e.compactLen = compactLen;
644
645    if( tier->tor->tiers->callback != NULL )
646        tier->tor->tiers->callback( tier->tor, &e, NULL );
647
648    return compactLen / 6;
649}
650
651static int
652publishNewPeersCompact( tr_tier * tier, int seeds, int leechers,
653                        const void * compact, int compactLen )
654{
655    int i;
656    const uint8_t *compactWalk;
657    uint8_t *array, *walk;
658    const int peerCount = compactLen / 6;
659    const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
660    tr_address addr;
661    tr_port port;
662
663    addr.type = TR_AF_INET;
664    memset( &addr.addr, 0, sizeof( addr.addr ) );
665    array = tr_new( uint8_t, arrayLen );
666    for ( i=0, walk=array, compactWalk=compact ; i<peerCount ; ++i )
667    {
668        memcpy( &addr.addr.addr4, compactWalk, 4 );
669        memcpy( &port, compactWalk + 4, 2 );
670
671        memcpy( walk, &addr, sizeof( addr ) );
672        memcpy( walk + sizeof( addr ), &port, 2 );
673
674        walk += sizeof( tr_address ) + 2;
675        compactWalk += 6;
676    }
677
678    publishNewPeers( tier, seeds, leechers, array, arrayLen );
679
680    tr_free( array );
681
682    return peerCount;
683}
684
685static int
686publishNewPeersCompact6( tr_tier * tier, int seeds, int leechers,
687                         const void * compact, int compactLen )
688{
689    int i;
690    const uint8_t *compactWalk;
691    uint8_t *array, *walk;
692    const int peerCount = compactLen / 18;
693    const int arrayLen = peerCount * ( sizeof( tr_address ) + 2 );
694    tr_address addr;
695    tr_port port;
696
697    addr.type = TR_AF_INET6;
698    memset( &addr.addr, 0, sizeof( addr.addr ) );
699    array = tr_new( uint8_t, arrayLen );
700    for ( i = 0, walk = array, compactWalk = compact ; i < peerCount ; ++i )
701    {
702        memcpy( &addr.addr.addr6, compactWalk, 16 );
703        memcpy( &port, compactWalk + 16, 2 );
704        compactWalk += 18;
705
706        memcpy( walk, &addr, sizeof( addr ) );
707        memcpy( walk + sizeof( addr ), &port, 2 );
708        walk += sizeof( tr_address ) + 2;
709    }
710
711    publishNewPeers( tier, seeds, leechers, array, arrayLen );
712    tr_free( array );
713
714    return peerCount;
715}
716
717static char*
718createAnnounceURL( const tr_announcer     * announcer,
719                   const tr_torrent       * torrent,
720                   const tr_tier          * tier,
721                   const char             * eventName )
722{
723    const int isStopping = !strcmp( eventName, "stopped" );
724    const int numwant = isStopping ? 0 : NUMWANT;
725    const tr_tracker_item  * tracker = tier->currentTracker;
726    const char * ann = tracker->announce;
727    struct evbuffer * buf = evbuffer_new( );
728    const char * str;
729    const unsigned char * ipv6;
730
731    evbuffer_expand( buf, 2048 );
732
733    evbuffer_add_printf( buf, "%s"
734                              "%c"
735                              "info_hash=%s"
736                              "&peer_id=%s"
737                              "&port=%d"
738                              "&uploaded=%" PRIu64
739                              "&downloaded=%" PRIu64
740                              "&left=%" PRIu64
741                              "&numwant=%d"
742                              "&key=%s"
743                              "&compact=1"
744                              "&supportcrypto=1",
745                              ann,
746                              strchr( ann, '?' ) ? '&' : '?',
747                              torrent->info.hashEscaped,
748                              torrent->peer_id,
749                              (int)tr_sessionGetPublicPeerPort( announcer->session ),
750                              tier->byteCounts[TR_ANN_UP],
751                              tier->byteCounts[TR_ANN_DOWN],
752                              tr_cpLeftUntilComplete( &torrent->completion ),
753                              numwant,
754                              tracker->key_param );
755
756    if( announcer->session->encryptionMode == TR_ENCRYPTION_REQUIRED )
757        evbuffer_add_printf( buf, "&requirecrypto=1" );
758
759    if( tier->byteCounts[TR_ANN_CORRUPT] )
760        evbuffer_add_printf( buf, "&corrupt=%" PRIu64, tier->byteCounts[TR_ANN_CORRUPT] );
761
762    str = eventName;
763    if( str && *str )
764        evbuffer_add_printf( buf, "&event=%s", str );
765
766    str = tracker->tracker_id;
767    if( str && *str )
768        evbuffer_add_printf( buf, "&trackerid=%s", str );
769
770    /* There are two incompatible techniques for announcing an IPv6 address.
771       BEP-7 suggests adding an "ipv6=" parameter to the announce URL,
772       while OpenTracker requires that peers announce twice, once over IPv4
773       and once over IPv6.
774
775       To be safe, we should do both: add the "ipv6=" parameter and
776       announce twice. At any rate, we're already computing our IPv6
777       address (for the LTEP handshake), so this comes for free. */
778
779    ipv6 = tr_globalIPv6( );
780    if( ipv6 ) {
781        char ipv6_readable[INET6_ADDRSTRLEN];
782        inet_ntop( AF_INET6, ipv6, ipv6_readable, INET6_ADDRSTRLEN );
783        evbuffer_add_printf( buf, "&ipv6=");
784        tr_http_escape( buf, ipv6_readable, -1, TRUE );
785    }
786
787    return evbuffer_free_to_str( buf );
788}
789
790
791/***
792****
793***/
794
795static void
796addTorrentToTier( tr_announcer * announcer, tr_torrent_tiers * tiers, tr_torrent * tor )
797{
798    int i, n;
799    const tr_tracker_info ** infos;
800    const int trackerCount = tor->info.trackerCount;
801    const tr_tracker_info  * trackers = tor->info.trackers;
802
803    /* get the trackers that we support... */
804    infos = tr_new0( const tr_tracker_info*, trackerCount );
805    for( i=n=0; i<trackerCount; ++i )
806        if( tr_urlIsValidTracker( trackers[i].announce ) )
807            infos[n++] = &trackers[i];
808
809    /* build our private table of tiers... */
810    if( n > 0 )
811    {
812        int tierIndex = -1;
813        tr_tier * tier = NULL;
814
815        for( i=0; i<n; ++i )
816        {
817            const tr_tracker_info * info = infos[i];
818
819            if( info->tier != tierIndex )
820                tier = NULL;
821
822            tierIndex = info->tier;
823
824            if( tier == NULL ) {
825                tier = tierNew( tor );
826                dbgmsg( tier, "adding tier" );
827                tr_ptrArrayAppend( &tiers->tiers, tier );
828            }
829
830            tierAddTracker( announcer, tier, info->announce, info->scrape, info->id );
831        }
832    }
833
834    tr_free( infos );
835}
836
837tr_torrent_tiers *
838tr_announcerAddTorrent( tr_announcer * announcer, tr_torrent * tor,
839                        tr_tracker_callback * callback, void * callbackData )
840{
841    tr_torrent_tiers * tiers;
842
843    assert( announcer != NULL );
844    assert( tr_isTorrent( tor ) );
845
846    tiers = tiersNew( );
847    tiers->callback = callback;
848    tiers->callbackData = callbackData;
849
850    addTorrentToTier( announcer, tiers, tor );
851
852    return tiers;
853}
854
855static void
856tierAddAnnounce( tr_tier * tier, const char * announceEvent, time_t announceAt );
857
858void
859tr_announcerResetTorrent( tr_announcer * announcer, tr_torrent * tor )
860{
861    tr_ptrArray oldTiers = TR_PTR_ARRAY_INIT;
862
863    /* if we had tiers already, make a backup of them */
864    if( tor->tiers != NULL )
865    {
866        oldTiers = tor->tiers->tiers;
867        tor->tiers->tiers = TR_PTR_ARRAY_INIT;
868    }
869
870    /* create the new tier/tracker structs */
871    addTorrentToTier( announcer, tor->tiers, tor );
872
873    /* if we had tiers already, merge their state into the new structs */
874    if( !tr_ptrArrayEmpty( &oldTiers ) )
875    {
876        int i, in;
877        for( i=0, in=tr_ptrArraySize(&oldTiers); i<in; ++i )
878        {
879            int j, jn;
880            const tr_tier * o = tr_ptrArrayNth( &oldTiers, i );
881
882            if( o->currentTracker == NULL )
883                continue;
884
885            for( j=0, jn=tr_ptrArraySize(&tor->tiers->tiers); j<jn; ++j )
886            {
887                int k, kn;
888                tr_tier * t = tr_ptrArrayNth(&tor->tiers->tiers,j);
889
890                for( k=0, kn=tr_ptrArraySize(&t->trackers); k<kn; ++k )
891                {
892                    tr_tracker_item * item = tr_ptrArrayNth(&t->trackers,k);
893                    if( strcmp( o->currentTracker->announce, item->announce ) )
894                        continue;
895                    tierCopyAttributes( t, o );
896                    t->currentTracker = item;
897                    t->currentTrackerIndex = k;
898                    t->wasCopied = TRUE;
899                    trackerItemCopyAttributes( item, o->currentTracker );
900                    dbgmsg( t, "attributes copied to tier %d, tracker %d"
901                                               "from tier %d, tracker %d",
902                            i, o->currentTrackerIndex, j, k );
903
904                }
905            }
906        }
907    }
908
909    /* kickstart any tiers that didn't get started */
910    if( tor->isRunning )
911    {
912        int i, n;
913        const time_t now = tr_time( );
914        tr_tier ** tiers = (tr_tier**) tr_ptrArrayPeek( &tor->tiers->tiers, &n );
915        for( i=0; i<n; ++i ) {
916            tr_tier * tier = tiers[i];
917            if( !tier->wasCopied )
918                tierAddAnnounce( tier, STARTED, now );
919        }
920    }
921
922    /* cleanup */
923    tr_ptrArrayDestruct( &oldTiers, tierFree );
924}
925
926static tr_bool
927tierCanManualAnnounce( const tr_tier * tier )
928{
929    return tier->isRunning
930        && tier->manualAnnounceAllowedAt <= tr_time( );
931}
932
933tr_bool
934tr_announcerCanManualAnnounce( const tr_torrent * tor )
935{
936    int i;
937    int n;
938    const tr_tier ** tiers;
939
940    assert( tr_isTorrent( tor ) );
941    assert( tor->tiers != NULL );
942
943    n = tr_ptrArraySize( &tor->tiers->tiers );
944    tiers = (const tr_tier**) tr_ptrArrayBase( &tor->tiers->tiers );
945    for( i=0; i<n; ++i )
946        if( tierCanManualAnnounce( tiers[i] ) )
947            return TRUE;
948
949    return FALSE;
950}
951
952time_t
953tr_announcerNextManualAnnounce( const tr_torrent * tor )
954{
955    int i;
956    int n;
957    const tr_torrent_tiers * tiers;
958    time_t ret = ~(time_t)0;
959
960    assert( tr_isTorrent( tor  ) );
961
962    tiers = tor->tiers;
963    n = tr_ptrArraySize( &tiers->tiers );
964    for( i=0; i<n; ++i ) {
965        tr_tier * tier = tr_ptrArrayNth( (tr_ptrArray*)&tiers->tiers, i );
966        if( tier->isRunning )
967            ret = MIN( ret, tier->manualAnnounceAllowedAt );
968    }
969
970    return ret;
971}
972
973static void
974tierAddAnnounce( tr_tier * tier, const char * announceEvent, time_t announceAt )
975{
976    assert( tier != NULL );
977    assert( announceEvent != NULL );
978
979    tr_ptrArrayAppend( &tier->announceEvents, (void*)announceEvent );
980    tier->announceAt = announceAt;
981
982    dbgmsg( tier, "appended event \"%s\"; announcing in %d seconds", announceEvent, (int)difftime(announceAt,time(NULL)) );
983}
984
985static void
986torrentAddAnnounce( tr_torrent * tor, const char * announceEvent, time_t announceAt )
987{
988    int i;
989    int n;
990    tr_torrent_tiers * tiers;
991
992    assert( tr_isTorrent( tor ) );
993
994    tiers = tor->tiers;
995    n = tr_ptrArraySize( &tiers->tiers );
996    for( i=0; i<n; ++i )
997        tierAddAnnounce( tr_ptrArrayNth( &tiers->tiers, i ), announceEvent, announceAt );
998}
999
1000void
1001tr_announcerTorrentStarted( tr_torrent * tor )
1002{
1003    torrentAddAnnounce( tor, STARTED, tr_time( ) );
1004}
1005void
1006tr_announcerManualAnnounce( tr_torrent * tor )
1007{
1008    torrentAddAnnounce( tor, "", tr_time( ) );
1009}
1010void
1011tr_announcerTorrentStopped( tr_torrent * tor )
1012{
1013    torrentAddAnnounce( tor, "stopped", tr_time( ) );
1014}
1015void
1016tr_announcerTorrentCompleted( tr_torrent * tor )
1017{
1018    torrentAddAnnounce( tor, "completed", tr_time( ) );
1019}
1020void
1021tr_announcerChangeMyPort( tr_torrent * tor )
1022{
1023    tr_announcerTorrentStarted( tor );
1024}
1025
1026/***
1027****
1028***/
1029
1030void
1031tr_announcerAddBytes( tr_torrent * tor, int type, uint32_t byteCount )
1032{
1033    int i, n;
1034    tr_torrent_tiers * tiers;
1035
1036    assert( tr_isTorrent( tor ) );
1037    assert( type==TR_ANN_UP || type==TR_ANN_DOWN || type==TR_ANN_CORRUPT );
1038
1039    tiers = tor->tiers;
1040    n = tr_ptrArraySize( &tiers->tiers );
1041    for( i=0; i<n; ++i )
1042    {
1043        tr_tier * tier = tr_ptrArrayNth( &tiers->tiers, i );
1044        tier->byteCounts[ type ] += byteCount;
1045    }
1046}
1047
1048/***
1049****
1050***/
1051
1052void
1053tr_announcerRemoveTorrent( tr_announcer * announcer, tr_torrent * tor )
1054{
1055    assert( tr_isTorrent( tor ) );
1056
1057    if( tor->tiers )
1058    {
1059        int i;
1060        const int n = tr_ptrArraySize( &tor->tiers->tiers );
1061        for( i=0; i<n; ++i )
1062        {
1063            tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
1064
1065            if( tier->isRunning )
1066            {
1067                struct stop_message * s = tr_new0( struct stop_message, 1 );
1068                s->up = tier->byteCounts[TR_ANN_UP];
1069                s->down = tier->byteCounts[TR_ANN_DOWN];
1070                s->url = createAnnounceURL( announcer, tor, tier, "stopped" );
1071                s->host = tier->currentTracker->host;
1072                tr_ptrArrayInsertSorted( &announcer->stops, s, compareStops );
1073            }
1074        }
1075
1076        tiersFree( tor->tiers );
1077        tor->tiers = NULL;
1078    }
1079}
1080
1081/* return true if (1) we've tried it recently AND (2) it didn't respond... */
1082static tr_bool
1083hostIsNotResponding( const tr_host * host, const time_t now )
1084{
1085    tr_bool b = ( host->lastRequestTime )
1086             && ( host->lastRequestTime >= ( now - SLOW_HOST_PENALTY_SECS ) )
1087             && ( host->lastResponseInterval > MAX_TRACKER_RESPONSE_TIME_SECS );
1088    return b;
1089}
1090
1091static tr_bool
1092tierIsNotResponding( const tr_tier * tier, const time_t now )
1093{
1094    return !tier->currentTracker
1095        || hostIsNotResponding( tier->currentTracker->host, now );
1096}
1097
1098static int
1099getRetryInterval( const tr_tracker_item * t )
1100{
1101    int minutes;
1102    const int jitter_seconds = tr_cryptoWeakRandInt( 120 );
1103
1104    switch( t->consecutiveAnnounceFailures ) {
1105        case 0:  minutes =  0; break;
1106        case 1:  minutes =  1; break;
1107        case 2:  minutes =  2; break;
1108        case 3:  minutes =  4; break;
1109        case 4:  minutes =  8; break;
1110        case 5:  minutes = 16; break;
1111        default: minutes = 32; break;
1112    }
1113
1114    return ( minutes * 60 ) + jitter_seconds;
1115}
1116
1117static int
1118compareTiers( const void * va, const void * vb )
1119{
1120    int ret = 0;
1121    tr_bool af, bf;
1122    const tr_tier * a = *(const tr_tier**)va;
1123    const tr_tier * b = *(const tr_tier**)vb;
1124
1125    /* working domains come before non-working */
1126    if( !ret ) {
1127        const time_t now = tr_time( );
1128        af = tierIsNotResponding( a, now );
1129        bf = tierIsNotResponding( b, now );
1130        if( af != bf )
1131            ret = !af ? -1 : 1;
1132    }
1133
1134    /* stops come before starts */
1135    if( !ret ) {
1136        af = a->tor->isRunning;
1137        bf = b->tor->isRunning;
1138        if( af != bf )
1139            ret = af ? 1 : -1;
1140    }
1141
1142    /* upload comes before download */
1143    if( !ret )
1144        ret = compareTransfer( a->byteCounts[TR_ANN_UP], a->byteCounts[TR_ANN_DOWN],
1145                               b->byteCounts[TR_ANN_UP], b->byteCounts[TR_ANN_DOWN] );
1146
1147    /* incomplete comes before complete */
1148    if( !ret ) {
1149        af = a->tor->completeness == TR_LEECH;
1150        bf = b->tor->completeness == TR_LEECH;
1151        if( af != bf )
1152            return af ? -1 : 1;
1153    }
1154
1155    /* private before public */
1156    if( !ret ) {
1157        af = tr_torrentIsPrivate( a->tor );
1158        bf = tr_torrentIsPrivate( b->tor );
1159        if( af != bf )
1160            ret = af ? -1 : 1;
1161    }
1162
1163    return ret;
1164}
1165
1166static uint8_t *
1167parseOldPeers( tr_benc * bePeers, size_t * byteCount )
1168{
1169    int       i;
1170    uint8_t * array, *walk;
1171    const int peerCount = bePeers->val.l.count;
1172
1173    assert( tr_bencIsList( bePeers ) );
1174
1175    array = tr_new( uint8_t, peerCount * ( sizeof( tr_address ) + 2 ) );
1176
1177    for( i = 0, walk = array; i < peerCount; ++i )
1178    {
1179        const char * s;
1180        int64_t      itmp;
1181        tr_address   addr;
1182        tr_port      port;
1183        tr_benc    * peer = &bePeers->val.l.vals[i];
1184
1185        if( tr_bencDictFindStr( peer, "ip", &s ) )
1186            if( tr_pton( s, &addr ) == NULL )
1187                continue;
1188
1189        if( !tr_bencDictFindInt( peer, "port", &itmp )
1190                || itmp < 0
1191                || itmp > USHRT_MAX )
1192            continue;
1193
1194        memcpy( walk, &addr, sizeof( tr_address ) );
1195        port = htons( (uint16_t)itmp );
1196        memcpy( walk + sizeof( tr_address ), &port, 2 );
1197        walk += sizeof( tr_address ) + 2;
1198    }
1199
1200    *byteCount = peerCount * sizeof( tr_address ) + 2;
1201    return array;
1202}
1203
1204static tr_bool
1205parseAnnounceResponse( tr_tier     * tier,
1206                       const char  * response,
1207                       size_t        responseLen,
1208                       tr_bool     * gotScrape )
1209{
1210    tr_benc benc;
1211    tr_bool success = FALSE;
1212    int scrapeFields = 0;
1213    const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
1214
1215    if( getenv( "TR_CURL_VERBOSE" ) != NULL )
1216    {
1217        char * str = tr_bencToStr( &benc, TR_FMT_JSON, NULL );
1218        fprintf( stderr, "Announce response:\n< %s\n", str );
1219        tr_free( str );
1220    }
1221
1222    dbgmsg( tier, "response len: %d, isBenc: %d", (int)responseLen, (int)bencLoaded );
1223    publishErrorClear( tier );
1224    if( bencLoaded && tr_bencIsDict( &benc ) )
1225    {
1226        int peerCount = 0;
1227        size_t rawlen;
1228        int64_t i;
1229        tr_benc * tmp;
1230        const char * str;
1231        const uint8_t * raw;
1232        tr_bool gotPeers = FALSE;
1233
1234        success = TRUE;
1235
1236        if( tr_bencDictFindStr( &benc, "failure reason", &str ) )
1237        {
1238            tr_strlcpy( tier->lastAnnounceStr, str,
1239                        sizeof( tier->lastAnnounceStr ) );
1240            dbgmsg( tier, "tracker gave \"%s\"", str );
1241            publishMessage( tier, str, TR_TRACKER_ERROR );
1242            success = FALSE;
1243        }
1244
1245        if( tr_bencDictFindStr( &benc, "warning message", &str ) )
1246        {
1247            tr_strlcpy( tier->lastAnnounceStr, str,
1248                        sizeof( tier->lastAnnounceStr ) );
1249            dbgmsg( tier, "tracker gave \"%s\"", str );
1250            publishWarning( tier, str );
1251        }
1252
1253        if( tr_bencDictFindInt( &benc, "interval", &i ) )
1254        {
1255            dbgmsg( tier, "setting interval to %d", (int)i );
1256            tier->announceIntervalSec = i;
1257        }
1258
1259        if( tr_bencDictFindInt( &benc, "min interval", &i ) )
1260        {
1261            dbgmsg( tier, "setting min interval to %d", (int)i );
1262            tier->announceMinIntervalSec = i;
1263        }
1264
1265        if( tr_bencDictFindStr( &benc, "tracker id", &str ) )
1266        {
1267            tier->currentTracker->tracker_id = tr_strdup( str );
1268        }
1269
1270        if( !tr_bencDictFindInt( &benc, "complete", &i ) )
1271            tier->currentTracker->seederCount = 0;
1272        else {
1273            ++scrapeFields;
1274            tier->currentTracker->seederCount = i;
1275        }
1276
1277        if( !tr_bencDictFindInt( &benc, "incomplete", &i ) )
1278            tier->currentTracker->leecherCount = 0;
1279        else {
1280            ++scrapeFields;
1281            tier->currentTracker->leecherCount = i;
1282        }
1283
1284        if( tr_bencDictFindInt( &benc, "downloaded", &i ) )
1285        {
1286            ++scrapeFields;
1287            tier->currentTracker->downloadCount = i;
1288        }
1289
1290        if( tr_bencDictFindRaw( &benc, "peers", &raw, &rawlen ) )
1291        {
1292            /* "compact" extension */
1293            const int seeders = tier->currentTracker->seederCount;
1294            const int leechers = tier->currentTracker->leecherCount;
1295            peerCount += publishNewPeersCompact( tier, seeders, leechers, raw, rawlen );
1296            gotPeers = TRUE;
1297        }
1298        else if( tr_bencDictFindList( &benc, "peers", &tmp ) )
1299        {
1300            /* original version of peers */
1301            const int seeders = tier->currentTracker->seederCount;
1302            const int leechers = tier->currentTracker->leecherCount;
1303            size_t byteCount = 0;
1304            uint8_t * array = parseOldPeers( tmp, &byteCount );
1305            peerCount += publishNewPeers( tier, seeders, leechers, array, byteCount );
1306            gotPeers = TRUE;
1307            tr_free( array );
1308        }
1309
1310        if( tr_bencDictFindRaw( &benc, "peers6", &raw, &rawlen ) )
1311        {
1312            /* "compact" extension */
1313            const int seeders = tier->currentTracker->seederCount;
1314            const int leechers = tier->currentTracker->leecherCount;
1315            peerCount += publishNewPeersCompact6( tier, seeders, leechers, raw, rawlen );
1316            gotPeers = TRUE;
1317        }
1318
1319        if( tier->lastAnnounceStr[0] == '\0' )
1320            tr_strlcpy( tier->lastAnnounceStr, _( "Success" ),
1321                        sizeof( tier->lastAnnounceStr ) );
1322
1323        if( gotPeers )
1324            tier->lastAnnouncePeerCount = peerCount;
1325    }
1326
1327    if( bencLoaded )
1328        tr_bencFree( &benc );
1329
1330    *gotScrape = scrapeFields >= 2;
1331
1332    return success;
1333}
1334
1335struct announce_data
1336{
1337    int torrentId;
1338    int tierId;
1339    time_t timeSent;
1340    const char * event;
1341
1342    /** If the request succeeds, the value for tier's "isRunning" flag */
1343    tr_bool isRunningOnSuccess;
1344};
1345
1346static void
1347onAnnounceDone( tr_session   * session,
1348                long           responseCode,
1349                const void   * response,
1350                size_t         responseLen,
1351                void         * vdata )
1352{
1353    tr_announcer * announcer = session->announcer;
1354    struct announce_data * data = vdata;
1355    tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
1356    tr_bool gotScrape = FALSE;
1357    tr_bool success = FALSE;
1358    const time_t now = time ( NULL );
1359    const char * announceEvent = data->event;
1360    const tr_bool isStopped = !strcmp( announceEvent, "stopped" );
1361
1362    if( announcer && tier )
1363    {
1364        if( tier->currentTracker->host )
1365        {
1366            tr_host * host = tier->currentTracker->host;
1367            host->lastRequestTime = data->timeSent;
1368            host->lastResponseInterval = now - data->timeSent;
1369        }
1370
1371        tier->lastAnnounceTime = now;
1372
1373        if( responseCode == HTTP_OK )
1374        {
1375            tier->currentTracker->consecutiveAnnounceFailures = 0;
1376            success = parseAnnounceResponse( tier, response, responseLen, &gotScrape );
1377            dbgmsg( tier, "success is %d", success );
1378
1379            if( isStopped )
1380            {
1381                /* now that we've successfully stopped the torrent,
1382                 * we can reset the up/down/corrupt count we've kept
1383                 * for this tracker */
1384                tier->byteCounts[ TR_ANN_UP ] = 0;
1385                tier->byteCounts[ TR_ANN_DOWN ] = 0;
1386                tier->byteCounts[ TR_ANN_CORRUPT ] = 0;
1387            }
1388        }
1389        else if( responseCode )
1390        {
1391            /* %1$ld - http status code, such as 404
1392             * %2$s - human-readable explanation of the http status code */
1393            char * buf = tr_strdup_printf(
1394                _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1395                responseCode,
1396                tr_webGetResponseStr( responseCode ) );
1397
1398            tr_strlcpy( tier->lastAnnounceStr, buf,
1399                        sizeof( tier->lastAnnounceStr ) );
1400
1401            ++tier->currentTracker->consecutiveAnnounceFailures;
1402
1403            /* if the response is serious, *and* if the response may require
1404             * human intervention, then notify the user... otherwise just log it */
1405            if( responseCode >= 400 )
1406                if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
1407                    publishWarning( tier, buf );
1408            tr_torinf( tier->tor, "%s", buf );
1409            dbgmsg( tier, "%s", buf );
1410
1411            tr_free( buf );
1412        }
1413        else
1414        {
1415            tr_strlcpy( tier->lastAnnounceStr,
1416                        _( "tracker did not respond" ),
1417                        sizeof( tier->lastAnnounceStr ) );
1418            dbgmsg( tier, "%s", tier->lastAnnounceStr );
1419        }
1420    }
1421
1422    if( tier )
1423    {
1424        tier->isAnnouncing = FALSE;
1425
1426        if( responseCode == 0 )
1427        {
1428            const int interval = getRetryInterval( tier->currentTracker );
1429            dbgmsg( tier, "No response from tracker... retrying in %d seconds.", interval );
1430            tier->manualAnnounceAllowedAt = ~(time_t)0;
1431            tierAddAnnounce( tier, announceEvent, now + interval );
1432        }
1433        else if( 200 <= responseCode && responseCode <= 299 )
1434        {
1435            const int interval = tier->announceIntervalSec;
1436            dbgmsg( tier, "request succeeded. reannouncing in %d seconds", interval );
1437
1438            if( gotScrape )
1439            {
1440                tier->lastScrapeTime = now;
1441                tier->lastScrapeSucceeded = 1;
1442                tier->scrapeAt = now + tier->scrapeIntervalSec;
1443            }
1444
1445            tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
1446
1447            /* if we're running and the queue is empty, add the next update */
1448            if( !isStopped && !tr_ptrArraySize( &tier->announceEvents ) )
1449            {
1450                tierAddAnnounce( tier, "", now + interval );
1451            }
1452        }
1453        else if( 300 <= responseCode && responseCode <= 399 )
1454        {
1455            /* how did this get here?  libcurl handles this */
1456            const int interval = 5;
1457            dbgmsg( tier, "got a redirect. retrying in %d seconds", interval );
1458            tierAddAnnounce( tier, announceEvent, now + interval );
1459            tier->manualAnnounceAllowedAt = now + tier->announceMinIntervalSec;
1460        }
1461        else if( ( responseCode == 404 ) || ( 500 <= responseCode && responseCode <= 599 ) )
1462        {
1463            /* 404: The requested resource could not be found but may be
1464             * available again in the future. Subsequent requests by
1465             * the client are permissible. */
1466
1467            /* 5xx: indicate cases in which the server is aware that it
1468             * has erred or is incapable of performing the request.
1469             * So we pause a bit and try again. */
1470
1471            const int interval = getRetryInterval( tier->currentTracker );
1472            tier->manualAnnounceAllowedAt = ~(time_t)0;
1473            tierAddAnnounce( tier, announceEvent, now + interval );
1474        }
1475        else if( 400 <= responseCode && responseCode <= 499 )
1476        {
1477            /* The request could not be understood by the server due to
1478             * malformed syntax. The client SHOULD NOT repeat the
1479             * request without modifications. */
1480            if( tr_torrentIsPrivate( tier->tor ) || ( tier->tor->info.trackerCount < 2 ) )
1481                publishErrorMessageAndStop( tier, _( "Tracker returned a 4xx message" ) );
1482            tier->announceAt = 0;
1483            tier->manualAnnounceAllowedAt = ~(time_t)0;
1484        }
1485        else
1486        {
1487            /* WTF did we get?? */
1488            const int interval = 120;
1489            dbgmsg( tier, "Invalid response from tracker... retrying in two minutes." );
1490            tier->manualAnnounceAllowedAt = ~(time_t)0;
1491            tierAddAnnounce( tier, announceEvent, now + interval );
1492        }
1493
1494        tier->lastAnnounceSucceeded = success;
1495        tier->lastAnnounceTimedOut = responseCode == 0;
1496
1497        if( success )
1498        {
1499            tier->isRunning = data->isRunningOnSuccess;
1500
1501            if( tier->currentTracker->host )
1502                tier->currentTracker->host->lastSuccessfulRequest = now;
1503        }
1504        else if( responseCode != HTTP_OK )
1505        {
1506            tierIncrementTracker( tier );
1507
1508            tr_ptrArrayInsert( &tier->announceEvents, (void*)announceEvent, 0 );
1509        }
1510    }
1511
1512    if( announcer != NULL )
1513    {
1514        ++announcer->slotsAvailable;
1515    }
1516
1517    tr_free( data );
1518}
1519
1520static const char*
1521getNextAnnounceEvent( tr_tier * tier )
1522{
1523    int i, n;
1524    int pos = -1;
1525    tr_ptrArray tmp;
1526    const char ** events;
1527    const char * str = NULL;
1528
1529    assert( tier != NULL );
1530    assert( tr_isTorrent( tier->tor ) );
1531
1532    events = (const char**) tr_ptrArrayPeek( &tier->announceEvents, &n );
1533
1534    /* special case #1: if "stopped" is in the queue,
1535     * ignore everything before it except "completed" */
1536    if( pos == -1 ) {
1537        tr_bool completed = FALSE;
1538        for( i = 0; i < n; ++i ) {
1539            if( !strcmp( events[i], "completed" ) )
1540                completed = TRUE;
1541            if( !strcmp( events[i], "stopped" ) )
1542                break;
1543        }
1544        if( !completed && ( i <  n ) )
1545            pos = i;
1546    }
1547
1548    /* special case #2: don't use empty strings if something follows them */
1549    if( pos == -1 ) {
1550        for( i = 0; i < n; ++i )
1551            if( *events[i] )
1552                break;
1553        if( i < n )
1554            pos = i;
1555    }
1556
1557    /* default: use the next in the queue */
1558    if( ( pos == -1 ) && ( n > 0 ) )
1559        pos = 0;
1560
1561    /* special case #3: if there are duplicate requests in a row, skip to the last one */
1562    if( pos >= 0 ) {
1563        for( i=pos+1; i<n; ++i )
1564            if( strcmp( events[pos], events[i] ) )
1565                break;
1566        pos = i - 1;
1567    }
1568
1569    /* special case #4: BEP 21: "In order to tell the tracker that a peer is a
1570     * partial seed, it MUST send an event=paused parameter in every
1571     * announce while it is a partial seed." */
1572    str = pos>=0 ? events[pos] : NULL;
1573    if( tr_cpGetStatus( &tier->tor->completion ) == TR_PARTIAL_SEED )
1574        if( !str || strcmp( str, "stopped" ) )
1575            str = "paused";
1576
1577#if 0
1578for( i=0; i<n; ++i ) fprintf( stderr, "(%d)\"%s\" ", i, events[i] );
1579fprintf( stderr, "\n" );
1580fprintf( stderr, "using (%d)\"%s\"\n", pos, events[pos] );
1581if( strcmp( events[pos], str ) ) fprintf( stderr, "...but really using [%s]\n", str );
1582#endif
1583
1584    /* announceEvents array upkeep */
1585    tmp = TR_PTR_ARRAY_INIT;
1586    for( i=pos+1; i<n; ++i )
1587        tr_ptrArrayAppend( &tmp, (void*)events[i] );
1588    tr_ptrArrayDestruct( &tier->announceEvents, NULL );
1589    tier->announceEvents = tmp;
1590
1591    return str;
1592}
1593
1594static void
1595tierAnnounce( tr_announcer * announcer, tr_tier * tier )
1596{
1597    const char * announceEvent = getNextAnnounceEvent( tier );
1598
1599    assert( !tier->isAnnouncing );
1600
1601    if( announceEvent != NULL )
1602    {
1603        char * url;
1604        struct announce_data * data;
1605        const tr_torrent * tor = tier->tor;
1606        const time_t now = tr_time( );
1607
1608        data = tr_new0( struct announce_data, 1 );
1609        data->torrentId = tr_torrentId( tor );
1610        data->tierId = tier->key;
1611        data->isRunningOnSuccess = tor->isRunning;
1612        data->timeSent = now;
1613        data->event = announceEvent;
1614        url = createAnnounceURL( announcer, tor, tier, data->event );
1615
1616        tier->isAnnouncing = TRUE;
1617        tier->lastAnnounceStartTime = now;
1618        --announcer->slotsAvailable;
1619        tr_webRun( announcer->session, url, NULL, onAnnounceDone, data );
1620
1621        tr_free( url );
1622    }
1623}
1624
1625static tr_bool
1626parseScrapeResponse( tr_tier     * tier,
1627                     const char  * response,
1628                     size_t        responseLen,
1629                     char        * result,
1630                     size_t        resultlen )
1631{
1632    tr_bool success = FALSE;
1633    tr_benc benc, *files;
1634    const int bencLoaded = !tr_bencLoad( response, responseLen, &benc, NULL );
1635    if( bencLoaded && tr_bencDictFindDict( &benc, "files", &files ) )
1636    {
1637        const char * key;
1638        tr_benc * val;
1639        int i = 0;
1640        while( tr_bencDictChild( files, i++, &key, &val ))
1641        {
1642            int64_t intVal;
1643            tr_benc * flags;
1644
1645            if( memcmp( tier->tor->info.hash, key, SHA_DIGEST_LENGTH ) )
1646                continue;
1647
1648            success = TRUE;
1649            publishErrorClear( tier );
1650
1651            if( ( tr_bencDictFindInt( val, "complete", &intVal ) ) )
1652                tier->currentTracker->seederCount = intVal;
1653
1654            if( ( tr_bencDictFindInt( val, "incomplete", &intVal ) ) )
1655                tier->currentTracker->leecherCount = intVal;
1656
1657            if( ( tr_bencDictFindInt( val, "downloaded", &intVal ) ) )
1658                tier->currentTracker->downloadCount = intVal;
1659
1660            if( ( tr_bencDictFindInt( val, "downloaders", &intVal ) ) )
1661                tier->currentTracker->downloaderCount = intVal;
1662
1663            if( tr_bencDictFindDict( val, "flags", &flags ) )
1664                if( ( tr_bencDictFindInt( flags, "min_request_interval", &intVal ) ) )
1665                    tier->scrapeIntervalSec = MAX( DEFAULT_SCRAPE_INTERVAL_SEC, (int)intVal );
1666
1667            tr_tordbg( tier->tor,
1668                       "Scrape successful. Rescraping in %d seconds.",
1669                       tier->scrapeIntervalSec );
1670        }
1671    }
1672
1673    if( bencLoaded )
1674        tr_bencFree( &benc );
1675
1676    if( success )
1677        tr_strlcpy( result, _( "Success" ), resultlen );
1678    else
1679        tr_strlcpy( result, _( "Error parsing response" ), resultlen );
1680
1681    return success;
1682}
1683
1684static void
1685onScrapeDone( tr_session   * session,
1686              long           responseCode,
1687              const void   * response,
1688              size_t         responseLen,
1689              void         * vdata )
1690{
1691    tr_bool success = FALSE;
1692    tr_announcer * announcer = session->announcer;
1693    struct announce_data * data = vdata;
1694    tr_tier * tier = getTier( announcer, data->torrentId, data->tierId );
1695    const time_t now = tr_time( );
1696
1697    if( announcer )
1698        ++announcer->slotsAvailable;
1699
1700    if( announcer && tier )
1701    {
1702        tier->isScraping = FALSE;
1703        tier->lastScrapeTime = now;
1704
1705        if( tier->currentTracker->host )
1706        {
1707            tr_host * host = tier->currentTracker->host;
1708            host->lastRequestTime = data->timeSent;
1709            host->lastResponseInterval = now - data->timeSent;
1710        }
1711
1712        if( 200 <= responseCode && responseCode <= 299 )
1713        {
1714            const int interval = tier->scrapeIntervalSec;
1715            tier->scrapeAt = now + interval;
1716
1717            if( responseCode == HTTP_OK )
1718                success = parseScrapeResponse( tier, response, responseLen,
1719                                               tier->lastScrapeStr, sizeof( tier->lastScrapeStr ) );
1720            else
1721                tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1722                             _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1723                             responseCode, tr_webGetResponseStr( responseCode ) );
1724            dbgmsg( tier, "%s", tier->lastScrapeStr );
1725        }
1726        else if( 300 <= responseCode && responseCode <= 399 )
1727        {
1728            /* this shouldn't happen; libcurl should handle this */
1729            const int interval = 5;
1730            tier->scrapeAt = now + interval;
1731            tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1732                         "Got a redirect. Retrying in %d seconds", interval );
1733            dbgmsg( tier, "%s", tier->lastScrapeStr );
1734        }
1735        else
1736        {
1737            const int interval = getRetryInterval( tier->currentTracker );
1738
1739            /* Don't retry on a 4xx.
1740             * Retry at growing intervals on a 5xx */
1741            if( 400 <= responseCode && responseCode <= 499 )
1742                tier->scrapeAt = 0;
1743            else
1744                tier->scrapeAt = now + interval;
1745
1746            /* %1$ld - http status code, such as 404
1747             * %2$s - human-readable explanation of the http status code */
1748            if( !responseCode )
1749                tr_strlcpy( tier->lastScrapeStr, _( "tracker did not respond" ),
1750                            sizeof( tier->lastScrapeStr ) );
1751            else
1752                tr_snprintf( tier->lastScrapeStr, sizeof( tier->lastScrapeStr ),
1753                             _( "tracker gave HTTP Response Code %1$ld (%2$s)" ),
1754                             responseCode, tr_webGetResponseStr( responseCode ) );
1755        }
1756
1757        tier->lastScrapeSucceeded = success;
1758        tier->lastScrapeTimedOut = responseCode == 0;
1759
1760        if( success && tier->currentTracker->host )
1761            tier->currentTracker->host->lastSuccessfulRequest = now;
1762    }
1763
1764    tr_free( data );
1765}
1766
1767static void
1768tierScrape( tr_announcer * announcer, tr_tier * tier )
1769{
1770    char * url;
1771    const char * scrape;
1772    struct announce_data * data;
1773    const time_t now = tr_time( );
1774
1775    assert( tier );
1776    assert( !tier->isScraping );
1777    assert( tier->currentTracker != NULL );
1778    assert( tr_isTorrent( tier->tor ) );
1779
1780    data = tr_new0( struct announce_data, 1 );
1781    data->torrentId = tr_torrentId( tier->tor );
1782    data->tierId = tier->key;
1783
1784    scrape = tier->currentTracker->scrape;
1785
1786    url = tr_strdup_printf( "%s%cinfo_hash=%s",
1787                            scrape,
1788                            strchr( scrape, '?' ) ? '&' : '?',
1789                            tier->tor->info.hashEscaped );
1790
1791    tier->isScraping = TRUE;
1792    tier->lastScrapeStartTime = now;
1793    --announcer->slotsAvailable;
1794    dbgmsg( tier, "scraping \"%s\"", url );
1795    tr_webRun( announcer->session, url, NULL, onScrapeDone, data );
1796
1797    tr_free( url );
1798}
1799
1800static void
1801flushCloseMessages( tr_announcer * announcer )
1802{
1803    int i;
1804    const int n = tr_ptrArraySize( &announcer->stops );
1805
1806    for( i=0; i<n; ++i )
1807    {
1808        struct stop_message * stop = tr_ptrArrayNth( &announcer->stops, i );
1809        tr_webRun( announcer->session, stop->url, NULL, NULL, NULL );
1810        stopFree( stop );
1811    }
1812
1813    tr_ptrArrayClear( &announcer->stops );
1814}
1815
1816static tr_bool
1817tierNeedsToAnnounce( const tr_tier * tier, const time_t now )
1818{
1819    return !tier->isAnnouncing
1820        && !tier->isScraping
1821        && ( tier->announceAt != 0 )
1822        && ( tier->announceAt <= now )
1823        && ( tr_ptrArraySize( &tier->announceEvents ) != 0 );
1824}
1825
1826static tr_bool
1827tierNeedsToScrape( const tr_tier * tier, const time_t now )
1828{
1829    return ( !tier->isScraping )
1830        && ( tier->scrapeAt != 0 )
1831        && ( tier->scrapeAt <= now )
1832        && ( tier->currentTracker != NULL )
1833        && ( tier->currentTracker->scrape != NULL );
1834}
1835
1836static void
1837announceMore( tr_announcer * announcer )
1838{
1839    tr_torrent * tor = NULL;
1840    const time_t now = tr_time( );
1841
1842    if( announcer->slotsAvailable > 0 )
1843    {
1844        int i;
1845        int n;
1846        tr_ptrArray announceMe = TR_PTR_ARRAY_INIT;
1847        tr_ptrArray scrapeMe = TR_PTR_ARRAY_INIT;
1848
1849        /* build a list of tiers that need to be announced */
1850        while(( tor = tr_torrentNext( announcer->session, tor ))) {
1851            if( tor->tiers ) {
1852                n = tr_ptrArraySize( &tor->tiers->tiers );
1853                for( i=0; i<n; ++i ) {
1854                    tr_tier * tier = tr_ptrArrayNth( &tor->tiers->tiers, i );
1855                    if( tierNeedsToAnnounce( tier, now ) )
1856                        tr_ptrArrayAppend( &announceMe, tier );
1857                    else if( tierNeedsToScrape( tier, now ) )
1858                        tr_ptrArrayAppend( &scrapeMe, tier );
1859                }
1860            }
1861        }
1862
1863        /* if there are more tiers than slots available, prioritize */
1864        n = tr_ptrArraySize( &announceMe );
1865        if( n > announcer->slotsAvailable )
1866            qsort( tr_ptrArrayBase( &announceMe ), n, sizeof( tr_tier * ), compareTiers );
1867
1868        /* announce some */
1869        n = MIN( tr_ptrArraySize( &announceMe ), announcer->slotsAvailable );
1870        for( i=0; i<n; ++i ) {
1871            tr_tier * tier = tr_ptrArrayNth( &announceMe, i );
1872            dbgmsg( tier, "announcing tier %d of %d", i, n );
1873            tierAnnounce( announcer, tier );
1874        }
1875
1876        /* scrape some */
1877        n = MIN( tr_ptrArraySize( &scrapeMe ), announcer->slotsAvailable );
1878        for( i=0; i<n; ++i ) {
1879            tr_tier * tier = tr_ptrArrayNth( &scrapeMe, i );
1880            dbgmsg( tier, "scraping tier %d of %d", (i+1), n );
1881            tierScrape( announcer, tier );
1882        }
1883
1884#if 0
1885char timebuf[64];
1886tr_getLogTimeStr( timebuf, 64 );
1887fprintf( 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) );
1888#endif
1889
1890        /* cleanup */
1891        tr_ptrArrayDestruct( &scrapeMe, NULL );
1892        tr_ptrArrayDestruct( &announceMe, NULL );
1893    }
1894
1895    tor = NULL;
1896    while(( tor = tr_torrentNext( announcer->session, tor ))) {
1897        if( tor->dhtAnnounceAt <= now ) {
1898            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
1899                int rc;
1900                rc = tr_dhtAnnounce(tor, AF_INET, 1);
1901                if(rc == 0)
1902                    /* The DHT is not ready yet. Try again soon. */
1903                    tor->dhtAnnounceAt = now + 5 + tr_cryptoWeakRandInt( 5 );
1904                else
1905                    /* We should announce at least once every 30 minutes. */
1906                    tor->dhtAnnounceAt =
1907                        now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
1908            }
1909        }
1910
1911        if( tor->dhtAnnounce6At <= now ) {
1912            if( tor->isRunning && tr_torrentAllowsDHT(tor) ) {
1913                int rc;
1914                rc = tr_dhtAnnounce(tor, AF_INET6, 1);
1915                if(rc == 0)
1916                    tor->dhtAnnounce6At = now + 5 + tr_cryptoWeakRandInt( 5 );
1917                else
1918                    tor->dhtAnnounce6At =
1919                        now + 25 * 60 + tr_cryptoWeakRandInt( 3 * 60 );
1920            }
1921        }
1922    }
1923
1924    /* Local Peer Discovery */
1925    if( announcer->lpdHouseKeepingAt <= now )
1926    {
1927        tr_lpdAnnounceMore( now, LPD_HOUSEKEEPING_INTERVAL_SECS );
1928
1929        /* reschedule more LDS announces for ( the future + jitter ) */
1930        announcer->lpdHouseKeepingAt =
1931            calcRescheduleWithJitter( LPD_HOUSEKEEPING_INTERVAL_SECS );
1932    }
1933}
1934
1935static void
1936onUpkeepTimer( int foo UNUSED, short bar UNUSED, void * vannouncer )
1937{
1938    tr_announcer * announcer = vannouncer;
1939    tr_sessionLock( announcer->session );
1940
1941    /* maybe send out some "stopped" messages for closed torrents */
1942    flushCloseMessages( announcer );
1943
1944    /* maybe send out some announcements to trackers */
1945    announceMore( announcer );
1946
1947    /* set up the next timer */
1948    tr_timerAdd( announcer->upkeepTimer, UPKEEP_INTERVAL_SECS, 0 );
1949
1950    tr_sessionUnlock( announcer->session );
1951}
1952
1953/***
1954****
1955***/
1956
1957tr_tracker_stat *
1958tr_announcerStats( const tr_torrent * torrent,
1959                   int              * setmeTrackerCount )
1960{
1961    int i;
1962    int n;
1963    int out = 0;
1964    int tierCount;
1965    tr_tracker_stat * ret;
1966    const time_t now = tr_time( );
1967
1968    assert( tr_isTorrent( torrent ) );
1969    assert( tr_torrentIsLocked( torrent ) );
1970
1971    /* count the trackers... */
1972    for( i=n=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i ) {
1973        const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
1974        n += tr_ptrArraySize( &tier->trackers );
1975    }
1976
1977    /* alloc the stats */
1978    *setmeTrackerCount = n;
1979    ret = tr_new0( tr_tracker_stat, n );
1980
1981    /* populate the stats */
1982    for( i=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); i<tierCount; ++i )
1983    {
1984        int j;
1985        const tr_tier * tier = tr_ptrArrayNth( &torrent->tiers->tiers, i );
1986        n = tr_ptrArraySize( &tier->trackers );
1987        for( j=0; j<n; ++j )
1988        {
1989            const tr_tracker_item * tracker = tr_ptrArrayNth( (tr_ptrArray*)&tier->trackers, j );
1990            tr_tracker_stat * st = ret + out++;
1991
1992            st->id = tracker->id;
1993            tr_strlcpy( st->host, tracker->host->name, sizeof( st->host ) );
1994            tr_strlcpy( st->announce, tracker->announce, sizeof( st->announce ) );
1995            st->tier = i;
1996            st->isBackup = tracker != tier->currentTracker;
1997            st->lastScrapeStartTime = tier->lastScrapeStartTime;
1998            if( tracker->scrape )
1999                tr_strlcpy( st->scrape, tracker->scrape, sizeof( st->scrape ) );
2000            else
2001                st->scrape[0] = '\0';
2002
2003            st->seederCount = tracker->seederCount;
2004            st->leecherCount = tracker->leecherCount;
2005            st->downloadCount = tracker->downloadCount;
2006
2007            if( st->isBackup )
2008            {
2009                st->scrapeState = TR_TRACKER_INACTIVE;
2010                st->announceState = TR_TRACKER_INACTIVE;
2011                st->nextScrapeTime = 0;
2012                st->nextAnnounceTime = 0;
2013            }
2014            else
2015            {
2016                if(( st->hasScraped = tier->lastScrapeTime != 0 )) {
2017                    st->lastScrapeTime = tier->lastScrapeTime;
2018                    st->lastScrapeSucceeded = tier->lastScrapeSucceeded;
2019                    st->lastScrapeTimedOut = tier->lastScrapeTimedOut;
2020                    tr_strlcpy( st->lastScrapeResult, tier->lastScrapeStr, sizeof( st->lastScrapeResult ) );
2021                }
2022
2023                if( tier->isScraping )
2024                    st->scrapeState = TR_TRACKER_ACTIVE;
2025                else if( !tier->scrapeAt )
2026                    st->scrapeState = TR_TRACKER_INACTIVE;
2027                else if( tier->scrapeAt > now )
2028                {
2029                    st->scrapeState = TR_TRACKER_WAITING;
2030                    st->nextScrapeTime = tier->scrapeAt;
2031                }
2032                else
2033                    st->scrapeState = TR_TRACKER_QUEUED;
2034
2035                st->lastAnnounceStartTime = tier->lastAnnounceStartTime;
2036
2037                if(( st->hasAnnounced = tier->lastAnnounceTime != 0 )) {
2038                    st->lastAnnounceTime = tier->lastAnnounceTime;
2039                    tr_strlcpy( st->lastAnnounceResult, tier->lastAnnounceStr, sizeof( st->lastAnnounceResult ) );
2040                    st->lastAnnounceSucceeded = tier->lastAnnounceSucceeded;
2041                    st->lastAnnounceTimedOut = tier->lastAnnounceTimedOut;
2042                    st->lastAnnouncePeerCount = tier->lastAnnouncePeerCount;
2043                }
2044
2045                if( tier->isAnnouncing )
2046                    st->announceState = TR_TRACKER_ACTIVE;
2047                else if( !torrent->isRunning || !tier->announceAt )
2048                    st->announceState = TR_TRACKER_INACTIVE;
2049                else if( tier->announceAt > now )
2050                {
2051                    st->announceState = TR_TRACKER_WAITING;
2052                    st->nextAnnounceTime = tier->announceAt;
2053                }
2054                else
2055                    st->announceState = TR_TRACKER_QUEUED;
2056            }
2057        }
2058    }
2059
2060    return ret;
2061}
2062
2063void
2064tr_announcerStatsFree( tr_tracker_stat * trackers,
2065                       int trackerCount UNUSED )
2066{
2067    tr_free( trackers );
2068}
Note: See TracBrowser for help on using the repository browser.