source: trunk/libtransmission/announcer.c @ 10918

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

(trunk libT) fix more issues found by -Wconversion

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