source: trunk/libtransmission/announcer.c @ 11588

Last change on this file since 11588 was 11588, checked in by charles, 11 years ago

(trunk libT) #3836 "libevent2 support" -- finish moving to the libevent2 API mode. don't include the backwards-compatable API headers.

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